2022-07-29 20:37:09 +02:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
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"
|
|
|
|
|
2022-07-29 20:37:09 +02:00
|
|
|
s "github.com/netbirdio/netbird/management/server"
|
|
|
|
"github.com/netbirdio/netbird/management/server/http/middleware"
|
2022-10-22 13:29:39 +02:00
|
|
|
"github.com/netbirdio/netbird/management/server/telemetry"
|
2022-07-29 20:37:09 +02:00
|
|
|
)
|
|
|
|
|
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 {
|
|
|
|
Router *mux.Router
|
|
|
|
AccountManager s.AccountManager
|
|
|
|
AuthCfg AuthCfg
|
|
|
|
}
|
|
|
|
|
2022-07-29 20:37:09 +02:00
|
|
|
// APIHandler creates the Management service HTTP API handler registering all the available endpoints.
|
2023-02-03 21:47:20 +01:00
|
|
|
func APIHandler(accountManager s.AccountManager, appMetrics telemetry.AppMetrics, authCfg AuthCfg) (http.Handler, error) {
|
2022-07-29 20:37:09 +02:00
|
|
|
jwtMiddleware, err := middleware.NewJwtMiddleware(
|
2023-02-03 21:47:20 +01:00
|
|
|
authCfg.Issuer,
|
|
|
|
authCfg.Audience,
|
|
|
|
authCfg.KeysLocation)
|
2022-07-29 20:37:09 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
corsMiddleware := cors.AllowAll()
|
|
|
|
|
2022-11-03 17:02:31 +01:00
|
|
|
acMiddleware := middleware.NewAccessControl(
|
2023-02-03 21:47:20 +01:00
|
|
|
authCfg.Audience,
|
|
|
|
authCfg.UserIDClaim,
|
2022-07-29 20:37:09 +02:00
|
|
|
accountManager.IsUserAdmin)
|
|
|
|
|
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
|
|
|
|
2023-02-28 18:08:02 +01:00
|
|
|
router := rootRouter.PathPrefix("/api").Subrouter()
|
|
|
|
router.Use(metricsMiddleware.Handler, corsMiddleware.Handler, jwtMiddleware.Handler, acMiddleware.Handler)
|
|
|
|
|
|
|
|
api := apiHandler{
|
|
|
|
Router: router,
|
|
|
|
AccountManager: accountManager,
|
|
|
|
AuthCfg: authCfg,
|
|
|
|
}
|
|
|
|
|
|
|
|
api.
|
|
|
|
addAccountsEndpoint().
|
|
|
|
addPeersEndpoint().
|
|
|
|
addUsersEndpoint().
|
|
|
|
addSetupKeysEndpoint().
|
|
|
|
addRulesEndpoint().
|
|
|
|
addGroupsEndpoint().
|
|
|
|
addRoutesEndpoint().
|
|
|
|
addDNSNameserversEndpoint().
|
|
|
|
addDNSSettingEndpoint().
|
|
|
|
addEventsEndpoint()
|
|
|
|
|
|
|
|
err = api.Router.Walk(func(route *mux.Route, _ *mux.Router, _ []*mux.Route) error {
|
2022-10-21 16:24:13 +02:00
|
|
|
methods, err := route.GetMethods()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, method := range methods {
|
|
|
|
template, err := route.GetPathTemplate()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-10-22 11:50:21 +02:00
|
|
|
err = metricsMiddleware.AddHTTPRequestResponseCounter(template, method)
|
2022-10-21 16:24:13 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return rootRouter, nil
|
2022-07-29 20:37:09 +02:00
|
|
|
}
|
2023-02-28 18:08:02 +01:00
|
|
|
|
|
|
|
func (apiHandler *apiHandler) addAccountsEndpoint() *apiHandler {
|
|
|
|
accountsHandler := NewAccountsHandler(apiHandler.AccountManager, apiHandler.AuthCfg)
|
|
|
|
apiHandler.Router.HandleFunc("/accounts/{id}", accountsHandler.UpdateAccount).Methods("PUT", "OPTIONS")
|
|
|
|
apiHandler.Router.HandleFunc("/accounts", accountsHandler.GetAllAccounts).Methods("GET", "OPTIONS")
|
|
|
|
return apiHandler
|
|
|
|
}
|
|
|
|
|
|
|
|
func (apiHandler *apiHandler) addPeersEndpoint() *apiHandler {
|
|
|
|
peersHandler := NewPeersHandler(apiHandler.AccountManager, apiHandler.AuthCfg)
|
|
|
|
apiHandler.Router.HandleFunc("/peers", peersHandler.GetAllPeers).Methods("GET", "OPTIONS")
|
|
|
|
apiHandler.Router.HandleFunc("/peers/{id}", peersHandler.HandlePeer).
|
|
|
|
Methods("GET", "PUT", "DELETE", "OPTIONS")
|
|
|
|
return apiHandler
|
|
|
|
}
|
|
|
|
|
|
|
|
func (apiHandler *apiHandler) addUsersEndpoint() *apiHandler {
|
|
|
|
userHandler := NewUsersHandler(apiHandler.AccountManager, apiHandler.AuthCfg)
|
|
|
|
apiHandler.Router.HandleFunc("/users", userHandler.GetAllUsers).Methods("GET", "OPTIONS")
|
|
|
|
apiHandler.Router.HandleFunc("/users/{id}", userHandler.UpdateUser).Methods("PUT", "OPTIONS")
|
|
|
|
apiHandler.Router.HandleFunc("/users", userHandler.CreateUser).Methods("POST", "OPTIONS")
|
|
|
|
return apiHandler
|
|
|
|
}
|
|
|
|
|
|
|
|
func (apiHandler *apiHandler) addSetupKeysEndpoint() *apiHandler {
|
|
|
|
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")
|
|
|
|
apiHandler.Router.HandleFunc("/setup-keys/{id}", keysHandler.GetSetupKey).Methods("GET", "OPTIONS")
|
|
|
|
apiHandler.Router.HandleFunc("/setup-keys/{id}", keysHandler.UpdateSetupKey).Methods("PUT", "OPTIONS")
|
|
|
|
return apiHandler
|
|
|
|
}
|
|
|
|
|
|
|
|
func (apiHandler *apiHandler) addRulesEndpoint() *apiHandler {
|
|
|
|
rulesHandler := NewRulesHandler(apiHandler.AccountManager, apiHandler.AuthCfg)
|
|
|
|
apiHandler.Router.HandleFunc("/rules", rulesHandler.GetAllRules).Methods("GET", "OPTIONS")
|
|
|
|
apiHandler.Router.HandleFunc("/rules", rulesHandler.CreateRule).Methods("POST", "OPTIONS")
|
|
|
|
apiHandler.Router.HandleFunc("/rules/{id}", rulesHandler.UpdateRule).Methods("PUT", "OPTIONS")
|
|
|
|
apiHandler.Router.HandleFunc("/rules/{id}", rulesHandler.PatchRule).Methods("PATCH", "OPTIONS")
|
|
|
|
apiHandler.Router.HandleFunc("/rules/{id}", rulesHandler.GetRule).Methods("GET", "OPTIONS")
|
|
|
|
apiHandler.Router.HandleFunc("/rules/{id}", rulesHandler.DeleteRule).Methods("DELETE", "OPTIONS")
|
|
|
|
return apiHandler
|
|
|
|
}
|
|
|
|
|
|
|
|
func (apiHandler *apiHandler) addGroupsEndpoint() *apiHandler {
|
|
|
|
groupsHandler := NewGroupsHandler(apiHandler.AccountManager, apiHandler.AuthCfg)
|
|
|
|
apiHandler.Router.HandleFunc("/groups", groupsHandler.GetAllGroups).Methods("GET", "OPTIONS")
|
|
|
|
apiHandler.Router.HandleFunc("/groups", groupsHandler.CreateGroup).Methods("POST", "OPTIONS")
|
|
|
|
apiHandler.Router.HandleFunc("/groups/{id}", groupsHandler.UpdateGroup).Methods("PUT", "OPTIONS")
|
|
|
|
apiHandler.Router.HandleFunc("/groups/{id}", groupsHandler.PatchGroup).Methods("PATCH", "OPTIONS")
|
|
|
|
apiHandler.Router.HandleFunc("/groups/{id}", groupsHandler.GetGroup).Methods("GET", "OPTIONS")
|
|
|
|
apiHandler.Router.HandleFunc("/groups/{id}", groupsHandler.DeleteGroup).Methods("DELETE", "OPTIONS")
|
|
|
|
return apiHandler
|
|
|
|
}
|
|
|
|
|
|
|
|
func (apiHandler *apiHandler) addRoutesEndpoint() *apiHandler {
|
|
|
|
routesHandler := NewRoutesHandler(apiHandler.AccountManager, apiHandler.AuthCfg)
|
|
|
|
apiHandler.Router.HandleFunc("/routes", routesHandler.GetAllRoutes).Methods("GET", "OPTIONS")
|
|
|
|
apiHandler.Router.HandleFunc("/routes", routesHandler.CreateRoute).Methods("POST", "OPTIONS")
|
|
|
|
apiHandler.Router.HandleFunc("/routes/{id}", routesHandler.UpdateRoute).Methods("PUT", "OPTIONS")
|
|
|
|
apiHandler.Router.HandleFunc("/routes/{id}", routesHandler.PatchRoute).Methods("PATCH", "OPTIONS")
|
|
|
|
apiHandler.Router.HandleFunc("/routes/{id}", routesHandler.GetRoute).Methods("GET", "OPTIONS")
|
|
|
|
apiHandler.Router.HandleFunc("/routes/{id}", routesHandler.DeleteRoute).Methods("DELETE", "OPTIONS")
|
|
|
|
return apiHandler
|
|
|
|
}
|
|
|
|
|
|
|
|
func (apiHandler *apiHandler) addDNSNameserversEndpoint() *apiHandler {
|
|
|
|
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")
|
|
|
|
apiHandler.Router.HandleFunc("/dns/nameservers/{id}", nameserversHandler.UpdateNameserverGroup).Methods("PUT", "OPTIONS")
|
|
|
|
apiHandler.Router.HandleFunc("/dns/nameservers/{id}", nameserversHandler.PatchNameserverGroup).Methods("PATCH", "OPTIONS")
|
|
|
|
apiHandler.Router.HandleFunc("/dns/nameservers/{id}", nameserversHandler.GetNameserverGroup).Methods("GET", "OPTIONS")
|
|
|
|
apiHandler.Router.HandleFunc("/dns/nameservers/{id}", nameserversHandler.DeleteNameserverGroup).Methods("DELETE", "OPTIONS")
|
|
|
|
return apiHandler
|
|
|
|
}
|
|
|
|
|
|
|
|
func (apiHandler *apiHandler) addDNSSettingEndpoint() *apiHandler {
|
|
|
|
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")
|
|
|
|
return apiHandler
|
|
|
|
}
|
|
|
|
|
|
|
|
func (apiHandler *apiHandler) addEventsEndpoint() *apiHandler {
|
|
|
|
eventsHandler := NewEventsHandler(apiHandler.AccountManager, apiHandler.AuthCfg)
|
|
|
|
apiHandler.Router.HandleFunc("/events", eventsHandler.GetAllEvents).Methods("GET", "OPTIONS")
|
|
|
|
return apiHandler
|
|
|
|
}
|