2022-07-29 20:37:09 +02:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
2024-02-22 12:27:08 +01:00
|
|
|
"context"
|
|
|
|
"fmt"
|
2023-02-03 21:47:20 +01:00
|
|
|
"net/http"
|
|
|
|
|
2022-07-29 20:37:09 +02:00
|
|
|
"github.com/gorilla/mux"
|
2023-02-28 15:01:24 +01:00
|
|
|
"github.com/rs/cors"
|
|
|
|
|
2023-10-17 17:19:47 +02:00
|
|
|
"github.com/netbirdio/management-integrations/integrations"
|
2024-07-03 11:33:02 +02:00
|
|
|
|
2022-07-29 20:37:09 +02:00
|
|
|
s "github.com/netbirdio/netbird/management/server"
|
2024-02-20 09:59:56 +01:00
|
|
|
"github.com/netbirdio/netbird/management/server/geolocation"
|
2022-07-29 20:37:09 +02:00
|
|
|
"github.com/netbirdio/netbird/management/server/http/middleware"
|
2024-04-15 12:08:38 +02:00
|
|
|
"github.com/netbirdio/netbird/management/server/integrated_validator"
|
2023-03-30 10:54:09 +02:00
|
|
|
"github.com/netbirdio/netbird/management/server/jwtclaims"
|
2022-10-22 13:29:39 +02:00
|
|
|
"github.com/netbirdio/netbird/management/server/telemetry"
|
2022-07-29 20:37:09 +02:00
|
|
|
)
|
|
|
|
|
2024-02-22 12:27:08 +01:00
|
|
|
const apiPrefix = "/api"
|
|
|
|
|
2023-02-03 21:47:20 +01:00
|
|
|
// AuthCfg contains parameters for authentication middleware
|
|
|
|
type AuthCfg struct {
|
|
|
|
Issuer string
|
|
|
|
Audience string
|
|
|
|
UserIDClaim string
|
|
|
|
KeysLocation string
|
|
|
|
}
|
|
|
|
|
2023-02-28 18:08:02 +01:00
|
|
|
type apiHandler struct {
|
2024-02-20 09:59:56 +01:00
|
|
|
Router *mux.Router
|
|
|
|
AccountManager s.AccountManager
|
|
|
|
geolocationManager *geolocation.Geolocation
|
|
|
|
AuthCfg AuthCfg
|
2023-02-28 18:08:02 +01:00
|
|
|
}
|
|
|
|
|
2023-03-30 10:54:09 +02:00
|
|
|
// EmptyObject is an empty struct used to return empty JSON object
|
2023-03-29 18:46:40 +02:00
|
|
|
type emptyObject struct {
|
|
|
|
}
|
|
|
|
|
2022-07-29 20:37:09 +02:00
|
|
|
// APIHandler creates the Management service HTTP API handler registering all the available endpoints.
|
2024-04-15 12:08:38 +02:00
|
|
|
func APIHandler(ctx context.Context, accountManager s.AccountManager, LocationManager *geolocation.Geolocation, jwtValidator jwtclaims.JWTValidator, appMetrics telemetry.AppMetrics, authCfg AuthCfg, integratedValidator integrated_validator.IntegratedValidator) (http.Handler, error) {
|
2023-12-11 16:59:15 +01:00
|
|
|
claimsExtractor := jwtclaims.NewClaimsExtractor(
|
|
|
|
jwtclaims.WithAudience(authCfg.Audience),
|
|
|
|
jwtclaims.WithUserIDClaim(authCfg.UserIDClaim),
|
|
|
|
)
|
|
|
|
|
2023-03-30 10:54:09 +02:00
|
|
|
authMiddleware := middleware.NewAuthMiddleware(
|
|
|
|
accountManager.GetAccountFromPAT,
|
|
|
|
jwtValidator.ValidateAndParse,
|
|
|
|
accountManager.MarkPATUsed,
|
2023-12-13 11:18:35 +01:00
|
|
|
accountManager.CheckUserAccessByJWTGroups,
|
2023-12-11 16:59:15 +01:00
|
|
|
claimsExtractor,
|
2023-09-01 18:09:59 +02:00
|
|
|
authCfg.Audience,
|
2023-12-11 16:59:15 +01:00
|
|
|
authCfg.UserIDClaim,
|
|
|
|
)
|
2022-07-29 20:37:09 +02:00
|
|
|
|
|
|
|
corsMiddleware := cors.AllowAll()
|
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
claimsExtractor = jwtclaims.NewClaimsExtractor(
|
|
|
|
jwtclaims.WithAudience(authCfg.Audience),
|
|
|
|
jwtclaims.WithUserIDClaim(authCfg.UserIDClaim),
|
|
|
|
)
|
|
|
|
|
2022-11-03 17:02:31 +01:00
|
|
|
acMiddleware := middleware.NewAccessControl(
|
2023-02-03 21:47:20 +01:00
|
|
|
authCfg.Audience,
|
|
|
|
authCfg.UserIDClaim,
|
2023-05-11 18:09:36 +02:00
|
|
|
accountManager.GetUser)
|
2022-07-29 20:37:09 +02:00
|
|
|
|
2022-10-21 16:24:13 +02:00
|
|
|
rootRouter := mux.NewRouter()
|
2022-10-22 13:29:39 +02:00
|
|
|
metricsMiddleware := appMetrics.HTTPMiddleware()
|
2022-10-21 16:24:13 +02:00
|
|
|
|
2024-02-22 12:27:08 +01:00
|
|
|
prefix := apiPrefix
|
|
|
|
router := rootRouter.PathPrefix(prefix).Subrouter()
|
2023-03-30 10:54:09 +02:00
|
|
|
router.Use(metricsMiddleware.Handler, corsMiddleware.Handler, authMiddleware.Handler, acMiddleware.Handler)
|
2023-02-28 18:08:02 +01:00
|
|
|
|
|
|
|
api := apiHandler{
|
2024-02-20 09:59:56 +01:00
|
|
|
Router: router,
|
|
|
|
AccountManager: accountManager,
|
|
|
|
geolocationManager: LocationManager,
|
|
|
|
AuthCfg: authCfg,
|
2023-02-28 18:08:02 +01:00
|
|
|
}
|
|
|
|
|
2024-04-15 12:08:38 +02:00
|
|
|
if _, err := integrations.RegisterHandlers(ctx, prefix, api.Router, accountManager, claimsExtractor, integratedValidator); err != nil {
|
2024-02-22 12:27:08 +01:00
|
|
|
return nil, fmt.Errorf("register integrations endpoints: %w", err)
|
|
|
|
}
|
|
|
|
|
2023-02-28 18:17:55 +01:00
|
|
|
api.addAccountsEndpoint()
|
|
|
|
api.addPeersEndpoint()
|
|
|
|
api.addUsersEndpoint()
|
2023-03-21 16:02:19 +01:00
|
|
|
api.addUsersTokensEndpoint()
|
2023-02-28 18:17:55 +01:00
|
|
|
api.addSetupKeysEndpoint()
|
2023-03-13 15:14:18 +01:00
|
|
|
api.addPoliciesEndpoint()
|
2023-02-28 18:17:55 +01:00
|
|
|
api.addGroupsEndpoint()
|
|
|
|
api.addRoutesEndpoint()
|
|
|
|
api.addDNSNameserversEndpoint()
|
|
|
|
api.addDNSSettingEndpoint()
|
|
|
|
api.addEventsEndpoint()
|
2024-02-20 09:59:56 +01:00
|
|
|
api.addPostureCheckEndpoint()
|
|
|
|
api.addLocationsEndpoint()
|
2023-02-28 18:08:02 +01:00
|
|
|
|
2022-10-21 16:24:13 +02:00
|
|
|
return rootRouter, nil
|
2022-07-29 20:37:09 +02:00
|
|
|
}
|
2023-02-28 18:08:02 +01:00
|
|
|
|
2023-02-28 18:17:55 +01:00
|
|
|
func (apiHandler *apiHandler) addAccountsEndpoint() {
|
2023-02-28 18:08:02 +01:00
|
|
|
accountsHandler := NewAccountsHandler(apiHandler.AccountManager, apiHandler.AuthCfg)
|
2023-05-03 00:15:25 +02:00
|
|
|
apiHandler.Router.HandleFunc("/accounts/{accountId}", accountsHandler.UpdateAccount).Methods("PUT", "OPTIONS")
|
2023-11-28 14:23:38 +01:00
|
|
|
apiHandler.Router.HandleFunc("/accounts/{accountId}", accountsHandler.DeleteAccount).Methods("DELETE", "OPTIONS")
|
2023-02-28 18:08:02 +01:00
|
|
|
apiHandler.Router.HandleFunc("/accounts", accountsHandler.GetAllAccounts).Methods("GET", "OPTIONS")
|
|
|
|
}
|
|
|
|
|
2023-02-28 18:17:55 +01:00
|
|
|
func (apiHandler *apiHandler) addPeersEndpoint() {
|
2023-02-28 18:08:02 +01:00
|
|
|
peersHandler := NewPeersHandler(apiHandler.AccountManager, apiHandler.AuthCfg)
|
|
|
|
apiHandler.Router.HandleFunc("/peers", peersHandler.GetAllPeers).Methods("GET", "OPTIONS")
|
2023-05-03 00:15:25 +02:00
|
|
|
apiHandler.Router.HandleFunc("/peers/{peerId}", peersHandler.HandlePeer).
|
2023-02-28 18:08:02 +01:00
|
|
|
Methods("GET", "PUT", "DELETE", "OPTIONS")
|
2024-09-12 15:19:27 +02:00
|
|
|
apiHandler.Router.HandleFunc("/peers/{peerId}/accessible-peers", peersHandler.GetAccessiblePeers).Methods("GET", "OPTIONS")
|
2023-02-28 18:08:02 +01:00
|
|
|
}
|
|
|
|
|
2023-02-28 18:17:55 +01:00
|
|
|
func (apiHandler *apiHandler) addUsersEndpoint() {
|
2023-02-28 18:08:02 +01:00
|
|
|
userHandler := NewUsersHandler(apiHandler.AccountManager, apiHandler.AuthCfg)
|
|
|
|
apiHandler.Router.HandleFunc("/users", userHandler.GetAllUsers).Methods("GET", "OPTIONS")
|
2023-05-03 00:15:25 +02:00
|
|
|
apiHandler.Router.HandleFunc("/users/{userId}", userHandler.UpdateUser).Methods("PUT", "OPTIONS")
|
|
|
|
apiHandler.Router.HandleFunc("/users/{userId}", userHandler.DeleteUser).Methods("DELETE", "OPTIONS")
|
2023-02-28 18:08:02 +01:00
|
|
|
apiHandler.Router.HandleFunc("/users", userHandler.CreateUser).Methods("POST", "OPTIONS")
|
2023-07-03 12:20:19 +02:00
|
|
|
apiHandler.Router.HandleFunc("/users/{userId}/invite", userHandler.InviteUser).Methods("POST", "OPTIONS")
|
2023-02-28 18:08:02 +01:00
|
|
|
}
|
|
|
|
|
2023-03-21 16:02:19 +01:00
|
|
|
func (apiHandler *apiHandler) addUsersTokensEndpoint() {
|
|
|
|
tokenHandler := NewPATsHandler(apiHandler.AccountManager, apiHandler.AuthCfg)
|
|
|
|
apiHandler.Router.HandleFunc("/users/{userId}/tokens", tokenHandler.GetAllTokens).Methods("GET", "OPTIONS")
|
|
|
|
apiHandler.Router.HandleFunc("/users/{userId}/tokens", tokenHandler.CreateToken).Methods("POST", "OPTIONS")
|
|
|
|
apiHandler.Router.HandleFunc("/users/{userId}/tokens/{tokenId}", tokenHandler.GetToken).Methods("GET", "OPTIONS")
|
|
|
|
apiHandler.Router.HandleFunc("/users/{userId}/tokens/{tokenId}", tokenHandler.DeleteToken).Methods("DELETE", "OPTIONS")
|
|
|
|
}
|
|
|
|
|
2023-02-28 18:17:55 +01:00
|
|
|
func (apiHandler *apiHandler) addSetupKeysEndpoint() {
|
2023-02-28 18:08:02 +01:00
|
|
|
keysHandler := NewSetupKeysHandler(apiHandler.AccountManager, apiHandler.AuthCfg)
|
|
|
|
apiHandler.Router.HandleFunc("/setup-keys", keysHandler.GetAllSetupKeys).Methods("GET", "OPTIONS")
|
|
|
|
apiHandler.Router.HandleFunc("/setup-keys", keysHandler.CreateSetupKey).Methods("POST", "OPTIONS")
|
2023-05-03 00:15:25 +02:00
|
|
|
apiHandler.Router.HandleFunc("/setup-keys/{keyId}", keysHandler.GetSetupKey).Methods("GET", "OPTIONS")
|
|
|
|
apiHandler.Router.HandleFunc("/setup-keys/{keyId}", keysHandler.UpdateSetupKey).Methods("PUT", "OPTIONS")
|
2023-02-28 18:08:02 +01:00
|
|
|
}
|
|
|
|
|
2023-03-13 15:14:18 +01:00
|
|
|
func (apiHandler *apiHandler) addPoliciesEndpoint() {
|
|
|
|
policiesHandler := NewPoliciesHandler(apiHandler.AccountManager, apiHandler.AuthCfg)
|
|
|
|
apiHandler.Router.HandleFunc("/policies", policiesHandler.GetAllPolicies).Methods("GET", "OPTIONS")
|
|
|
|
apiHandler.Router.HandleFunc("/policies", policiesHandler.CreatePolicy).Methods("POST", "OPTIONS")
|
2023-05-03 00:15:25 +02:00
|
|
|
apiHandler.Router.HandleFunc("/policies/{policyId}", policiesHandler.UpdatePolicy).Methods("PUT", "OPTIONS")
|
|
|
|
apiHandler.Router.HandleFunc("/policies/{policyId}", policiesHandler.GetPolicy).Methods("GET", "OPTIONS")
|
|
|
|
apiHandler.Router.HandleFunc("/policies/{policyId}", policiesHandler.DeletePolicy).Methods("DELETE", "OPTIONS")
|
2023-03-13 15:14:18 +01:00
|
|
|
}
|
|
|
|
|
2023-02-28 18:17:55 +01:00
|
|
|
func (apiHandler *apiHandler) addGroupsEndpoint() {
|
2023-02-28 18:08:02 +01:00
|
|
|
groupsHandler := NewGroupsHandler(apiHandler.AccountManager, apiHandler.AuthCfg)
|
|
|
|
apiHandler.Router.HandleFunc("/groups", groupsHandler.GetAllGroups).Methods("GET", "OPTIONS")
|
|
|
|
apiHandler.Router.HandleFunc("/groups", groupsHandler.CreateGroup).Methods("POST", "OPTIONS")
|
2023-05-03 00:15:25 +02:00
|
|
|
apiHandler.Router.HandleFunc("/groups/{groupId}", groupsHandler.UpdateGroup).Methods("PUT", "OPTIONS")
|
|
|
|
apiHandler.Router.HandleFunc("/groups/{groupId}", groupsHandler.GetGroup).Methods("GET", "OPTIONS")
|
|
|
|
apiHandler.Router.HandleFunc("/groups/{groupId}", groupsHandler.DeleteGroup).Methods("DELETE", "OPTIONS")
|
2023-02-28 18:08:02 +01:00
|
|
|
}
|
|
|
|
|
2023-02-28 18:17:55 +01:00
|
|
|
func (apiHandler *apiHandler) addRoutesEndpoint() {
|
2023-02-28 18:08:02 +01:00
|
|
|
routesHandler := NewRoutesHandler(apiHandler.AccountManager, apiHandler.AuthCfg)
|
|
|
|
apiHandler.Router.HandleFunc("/routes", routesHandler.GetAllRoutes).Methods("GET", "OPTIONS")
|
|
|
|
apiHandler.Router.HandleFunc("/routes", routesHandler.CreateRoute).Methods("POST", "OPTIONS")
|
2023-05-03 00:15:25 +02:00
|
|
|
apiHandler.Router.HandleFunc("/routes/{routeId}", routesHandler.UpdateRoute).Methods("PUT", "OPTIONS")
|
|
|
|
apiHandler.Router.HandleFunc("/routes/{routeId}", routesHandler.GetRoute).Methods("GET", "OPTIONS")
|
|
|
|
apiHandler.Router.HandleFunc("/routes/{routeId}", routesHandler.DeleteRoute).Methods("DELETE", "OPTIONS")
|
2023-02-28 18:08:02 +01:00
|
|
|
}
|
|
|
|
|
2023-02-28 18:17:55 +01:00
|
|
|
func (apiHandler *apiHandler) addDNSNameserversEndpoint() {
|
2023-02-28 18:08:02 +01:00
|
|
|
nameserversHandler := NewNameserversHandler(apiHandler.AccountManager, apiHandler.AuthCfg)
|
|
|
|
apiHandler.Router.HandleFunc("/dns/nameservers", nameserversHandler.GetAllNameservers).Methods("GET", "OPTIONS")
|
|
|
|
apiHandler.Router.HandleFunc("/dns/nameservers", nameserversHandler.CreateNameserverGroup).Methods("POST", "OPTIONS")
|
2023-05-03 00:15:25 +02:00
|
|
|
apiHandler.Router.HandleFunc("/dns/nameservers/{nsgroupId}", nameserversHandler.UpdateNameserverGroup).Methods("PUT", "OPTIONS")
|
|
|
|
apiHandler.Router.HandleFunc("/dns/nameservers/{nsgroupId}", nameserversHandler.GetNameserverGroup).Methods("GET", "OPTIONS")
|
|
|
|
apiHandler.Router.HandleFunc("/dns/nameservers/{nsgroupId}", nameserversHandler.DeleteNameserverGroup).Methods("DELETE", "OPTIONS")
|
2023-02-28 18:08:02 +01:00
|
|
|
}
|
|
|
|
|
2023-02-28 18:17:55 +01:00
|
|
|
func (apiHandler *apiHandler) addDNSSettingEndpoint() {
|
2023-02-28 18:08:02 +01:00
|
|
|
dnsSettingsHandler := NewDNSSettingsHandler(apiHandler.AccountManager, apiHandler.AuthCfg)
|
|
|
|
apiHandler.Router.HandleFunc("/dns/settings", dnsSettingsHandler.GetDNSSettings).Methods("GET", "OPTIONS")
|
|
|
|
apiHandler.Router.HandleFunc("/dns/settings", dnsSettingsHandler.UpdateDNSSettings).Methods("PUT", "OPTIONS")
|
|
|
|
}
|
|
|
|
|
2023-02-28 18:17:55 +01:00
|
|
|
func (apiHandler *apiHandler) addEventsEndpoint() {
|
2023-02-28 18:08:02 +01:00
|
|
|
eventsHandler := NewEventsHandler(apiHandler.AccountManager, apiHandler.AuthCfg)
|
|
|
|
apiHandler.Router.HandleFunc("/events", eventsHandler.GetAllEvents).Methods("GET", "OPTIONS")
|
|
|
|
}
|
2024-02-20 09:59:56 +01:00
|
|
|
|
|
|
|
func (apiHandler *apiHandler) addPostureCheckEndpoint() {
|
|
|
|
postureCheckHandler := NewPostureChecksHandler(apiHandler.AccountManager, apiHandler.geolocationManager, apiHandler.AuthCfg)
|
|
|
|
apiHandler.Router.HandleFunc("/posture-checks", postureCheckHandler.GetAllPostureChecks).Methods("GET", "OPTIONS")
|
|
|
|
apiHandler.Router.HandleFunc("/posture-checks", postureCheckHandler.CreatePostureCheck).Methods("POST", "OPTIONS")
|
|
|
|
apiHandler.Router.HandleFunc("/posture-checks/{postureCheckId}", postureCheckHandler.UpdatePostureCheck).Methods("PUT", "OPTIONS")
|
|
|
|
apiHandler.Router.HandleFunc("/posture-checks/{postureCheckId}", postureCheckHandler.GetPostureCheck).Methods("GET", "OPTIONS")
|
|
|
|
apiHandler.Router.HandleFunc("/posture-checks/{postureCheckId}", postureCheckHandler.DeletePostureCheck).Methods("DELETE", "OPTIONS")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (apiHandler *apiHandler) addLocationsEndpoint() {
|
|
|
|
locationHandler := NewGeolocationsHandlerHandler(apiHandler.AccountManager, apiHandler.geolocationManager, apiHandler.AuthCfg)
|
|
|
|
apiHandler.Router.HandleFunc("/locations/countries", locationHandler.GetAllCountries).Methods("GET", "OPTIONS")
|
|
|
|
apiHandler.Router.HandleFunc("/locations/countries/{country}/cities", locationHandler.GetCitiesByCountry).Methods("GET", "OPTIONS")
|
|
|
|
}
|