mirror of
https://github.com/netbirdio/netbird.git
synced 2025-06-20 17:58:02 +02:00
[management] add permissions manager to geolocation handler (#3665)
This commit is contained in:
parent
7839d2c169
commit
4134b857b4
@ -83,6 +83,8 @@ func NewAPIHandler(
|
|||||||
users.AddEndpoints(accountManager, router)
|
users.AddEndpoints(accountManager, router)
|
||||||
setup_keys.AddEndpoints(accountManager, router)
|
setup_keys.AddEndpoints(accountManager, router)
|
||||||
policies.AddEndpoints(accountManager, LocationManager, router)
|
policies.AddEndpoints(accountManager, LocationManager, router)
|
||||||
|
policies.AddPostureCheckEndpoints(accountManager, LocationManager, router)
|
||||||
|
policies.AddLocationsEndpoints(accountManager, LocationManager, permissionsManager, router)
|
||||||
groups.AddEndpoints(accountManager, router)
|
groups.AddEndpoints(accountManager, router)
|
||||||
routes.AddEndpoints(accountManager, router)
|
routes.AddEndpoints(accountManager, router)
|
||||||
dns.AddEndpoints(accountManager, router)
|
dns.AddEndpoints(accountManager, router)
|
||||||
|
@ -10,6 +10,7 @@ import (
|
|||||||
"path/filepath"
|
"path/filepath"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/golang/mock/gomock"
|
||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
|
||||||
@ -17,6 +18,9 @@ import (
|
|||||||
"github.com/netbirdio/netbird/management/server/geolocation"
|
"github.com/netbirdio/netbird/management/server/geolocation"
|
||||||
"github.com/netbirdio/netbird/management/server/http/api"
|
"github.com/netbirdio/netbird/management/server/http/api"
|
||||||
"github.com/netbirdio/netbird/management/server/mock_server"
|
"github.com/netbirdio/netbird/management/server/mock_server"
|
||||||
|
"github.com/netbirdio/netbird/management/server/permissions"
|
||||||
|
"github.com/netbirdio/netbird/management/server/permissions/modules"
|
||||||
|
"github.com/netbirdio/netbird/management/server/permissions/operations"
|
||||||
"github.com/netbirdio/netbird/management/server/types"
|
"github.com/netbirdio/netbird/management/server/types"
|
||||||
"github.com/netbirdio/netbird/util"
|
"github.com/netbirdio/netbird/util"
|
||||||
)
|
)
|
||||||
@ -41,6 +45,14 @@ func initGeolocationTestData(t *testing.T) *geolocationsHandler {
|
|||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
t.Cleanup(func() { _ = geo.Stop() })
|
t.Cleanup(func() { _ = geo.Stop() })
|
||||||
|
|
||||||
|
ctrl := gomock.NewController(t)
|
||||||
|
permissionsManagerMock := permissions.NewMockManager(ctrl)
|
||||||
|
permissionsManagerMock.
|
||||||
|
EXPECT().
|
||||||
|
ValidateUserPermissions(gomock.Any(), gomock.Any(), gomock.Any(), modules.Policies, operations.Read).
|
||||||
|
Return(true, nil).
|
||||||
|
AnyTimes()
|
||||||
|
|
||||||
return &geolocationsHandler{
|
return &geolocationsHandler{
|
||||||
accountManager: &mock_server.MockAccountManager{
|
accountManager: &mock_server.MockAccountManager{
|
||||||
GetUserByIDFunc: func(ctx context.Context, id string) (*types.User, error) {
|
GetUserByIDFunc: func(ctx context.Context, id string) (*types.User, error) {
|
||||||
@ -48,6 +60,7 @@ func initGeolocationTestData(t *testing.T) *geolocationsHandler {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
geolocationManager: geo,
|
geolocationManager: geo,
|
||||||
|
permissionsManager: permissionsManagerMock,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -11,6 +11,9 @@ import (
|
|||||||
"github.com/netbirdio/netbird/management/server/geolocation"
|
"github.com/netbirdio/netbird/management/server/geolocation"
|
||||||
"github.com/netbirdio/netbird/management/server/http/api"
|
"github.com/netbirdio/netbird/management/server/http/api"
|
||||||
"github.com/netbirdio/netbird/management/server/http/util"
|
"github.com/netbirdio/netbird/management/server/http/util"
|
||||||
|
"github.com/netbirdio/netbird/management/server/permissions"
|
||||||
|
"github.com/netbirdio/netbird/management/server/permissions/modules"
|
||||||
|
"github.com/netbirdio/netbird/management/server/permissions/operations"
|
||||||
"github.com/netbirdio/netbird/management/server/status"
|
"github.com/netbirdio/netbird/management/server/status"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -22,19 +25,21 @@ var (
|
|||||||
type geolocationsHandler struct {
|
type geolocationsHandler struct {
|
||||||
accountManager account.Manager
|
accountManager account.Manager
|
||||||
geolocationManager geolocation.Geolocation
|
geolocationManager geolocation.Geolocation
|
||||||
|
permissionsManager permissions.Manager
|
||||||
}
|
}
|
||||||
|
|
||||||
func addLocationsEndpoint(accountManager account.Manager, locationManager geolocation.Geolocation, router *mux.Router) {
|
func AddLocationsEndpoints(accountManager account.Manager, locationManager geolocation.Geolocation, permissionsManager permissions.Manager, router *mux.Router) {
|
||||||
locationHandler := newGeolocationsHandlerHandler(accountManager, locationManager)
|
locationHandler := newGeolocationsHandlerHandler(accountManager, locationManager, permissionsManager)
|
||||||
router.HandleFunc("/locations/countries", locationHandler.getAllCountries).Methods("GET", "OPTIONS")
|
router.HandleFunc("/locations/countries", locationHandler.getAllCountries).Methods("GET", "OPTIONS")
|
||||||
router.HandleFunc("/locations/countries/{country}/cities", locationHandler.getCitiesByCountry).Methods("GET", "OPTIONS")
|
router.HandleFunc("/locations/countries/{country}/cities", locationHandler.getCitiesByCountry).Methods("GET", "OPTIONS")
|
||||||
}
|
}
|
||||||
|
|
||||||
// newGeolocationsHandlerHandler creates a new Geolocations handler
|
// newGeolocationsHandlerHandler creates a new Geolocations handler
|
||||||
func newGeolocationsHandlerHandler(accountManager account.Manager, geolocationManager geolocation.Geolocation) *geolocationsHandler {
|
func newGeolocationsHandlerHandler(accountManager account.Manager, geolocationManager geolocation.Geolocation, permissionsManager permissions.Manager) *geolocationsHandler {
|
||||||
return &geolocationsHandler{
|
return &geolocationsHandler{
|
||||||
accountManager: accountManager,
|
accountManager: accountManager,
|
||||||
geolocationManager: geolocationManager,
|
geolocationManager: geolocationManager,
|
||||||
|
permissionsManager: permissionsManager,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -98,20 +103,22 @@ func (l *geolocationsHandler) getCitiesByCountry(w http.ResponseWriter, r *http.
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (l *geolocationsHandler) authenticateUser(r *http.Request) error {
|
func (l *geolocationsHandler) authenticateUser(r *http.Request) error {
|
||||||
userAuth, err := nbcontext.GetUserAuthFromContext(r.Context())
|
ctx := r.Context()
|
||||||
|
|
||||||
|
userAuth, err := nbcontext.GetUserAuthFromContext(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
_, userID := userAuth.AccountId, userAuth.UserId
|
accountID, userID := userAuth.AccountId, userAuth.UserId
|
||||||
|
|
||||||
user, err := l.accountManager.GetUserByID(r.Context(), userID)
|
allowed, err := l.permissionsManager.ValidateUserPermissions(ctx, accountID, userID, modules.Policies, operations.Read)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return status.NewPermissionValidationError(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if !user.HasAdminPower() {
|
if !allowed {
|
||||||
return status.Errorf(status.PermissionDenied, "user is not allowed to perform this action")
|
return status.NewPermissionDeniedError()
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -28,7 +28,6 @@ func AddEndpoints(accountManager account.Manager, locationManager geolocation.Ge
|
|||||||
router.HandleFunc("/policies/{policyId}", policiesHandler.updatePolicy).Methods("PUT", "OPTIONS")
|
router.HandleFunc("/policies/{policyId}", policiesHandler.updatePolicy).Methods("PUT", "OPTIONS")
|
||||||
router.HandleFunc("/policies/{policyId}", policiesHandler.getPolicy).Methods("GET", "OPTIONS")
|
router.HandleFunc("/policies/{policyId}", policiesHandler.getPolicy).Methods("GET", "OPTIONS")
|
||||||
router.HandleFunc("/policies/{policyId}", policiesHandler.deletePolicy).Methods("DELETE", "OPTIONS")
|
router.HandleFunc("/policies/{policyId}", policiesHandler.deletePolicy).Methods("DELETE", "OPTIONS")
|
||||||
addPostureCheckEndpoint(accountManager, locationManager, router)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// newHandler creates a new policies handler
|
// newHandler creates a new policies handler
|
||||||
|
@ -21,14 +21,13 @@ type postureChecksHandler struct {
|
|||||||
geolocationManager geolocation.Geolocation
|
geolocationManager geolocation.Geolocation
|
||||||
}
|
}
|
||||||
|
|
||||||
func addPostureCheckEndpoint(accountManager account.Manager, locationManager geolocation.Geolocation, router *mux.Router) {
|
func AddPostureCheckEndpoints(accountManager account.Manager, locationManager geolocation.Geolocation, router *mux.Router) {
|
||||||
postureCheckHandler := newPostureChecksHandler(accountManager, locationManager)
|
postureCheckHandler := newPostureChecksHandler(accountManager, locationManager)
|
||||||
router.HandleFunc("/posture-checks", postureCheckHandler.getAllPostureChecks).Methods("GET", "OPTIONS")
|
router.HandleFunc("/posture-checks", postureCheckHandler.getAllPostureChecks).Methods("GET", "OPTIONS")
|
||||||
router.HandleFunc("/posture-checks", postureCheckHandler.createPostureCheck).Methods("POST", "OPTIONS")
|
router.HandleFunc("/posture-checks", postureCheckHandler.createPostureCheck).Methods("POST", "OPTIONS")
|
||||||
router.HandleFunc("/posture-checks/{postureCheckId}", postureCheckHandler.updatePostureCheck).Methods("PUT", "OPTIONS")
|
router.HandleFunc("/posture-checks/{postureCheckId}", postureCheckHandler.updatePostureCheck).Methods("PUT", "OPTIONS")
|
||||||
router.HandleFunc("/posture-checks/{postureCheckId}", postureCheckHandler.getPostureCheck).Methods("GET", "OPTIONS")
|
router.HandleFunc("/posture-checks/{postureCheckId}", postureCheckHandler.getPostureCheck).Methods("GET", "OPTIONS")
|
||||||
router.HandleFunc("/posture-checks/{postureCheckId}", postureCheckHandler.deletePostureCheck).Methods("DELETE", "OPTIONS")
|
router.HandleFunc("/posture-checks/{postureCheckId}", postureCheckHandler.deletePostureCheck).Methods("DELETE", "OPTIONS")
|
||||||
addLocationsEndpoint(accountManager, locationManager, router)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// newPostureChecksHandler creates a new PostureChecks handler
|
// newPostureChecksHandler creates a new PostureChecks handler
|
||||||
|
Loading…
x
Reference in New Issue
Block a user