Access Control: Add fine-grained access control to ldap handlers (#35525)
* Add new accesscontrol action for ldap config reload * Update ldapAdminEditRole with new ldap config reload permission * wrap /ldap/reload with accesscontrol authorize middleware * document new action and update fixed:ldap:admin:edit with said action * add fake accesscontrol implementation for tests * Add accesscontrol tests for ldap handlers Co-authored-by: Ursula Kallio <73951760+osg-grafana@users.noreply.github.com>
This commit is contained in:
co-authored by
Ursula Kallio
parent
6707b61434
commit
36c997a625
+1
-1
@@ -441,7 +441,7 @@ func (hs *HTTPServer) registerRoutes() {
|
||||
adminRoute.Post("/provisioning/plugins/reload", reqGrafanaAdmin, routing.Wrap(hs.AdminProvisioningReloadPlugins))
|
||||
adminRoute.Post("/provisioning/datasources/reload", reqGrafanaAdmin, routing.Wrap(hs.AdminProvisioningReloadDatasources))
|
||||
adminRoute.Post("/provisioning/notifications/reload", reqGrafanaAdmin, routing.Wrap(hs.AdminProvisioningReloadNotifications))
|
||||
adminRoute.Post("/ldap/reload", reqGrafanaAdmin, routing.Wrap(hs.ReloadLDAPCfg))
|
||||
adminRoute.Post("/ldap/reload", authorize(reqGrafanaAdmin, accesscontrol.ActionLDAPConfigReload), routing.Wrap(hs.ReloadLDAPCfg))
|
||||
adminRoute.Post("/ldap/sync/:id", authorize(reqGrafanaAdmin, accesscontrol.ActionLDAPUsersSync), routing.Wrap(hs.PostSyncUserWithLDAP))
|
||||
adminRoute.Get("/ldap/:username", authorize(reqGrafanaAdmin, accesscontrol.ActionLDAPUsersRead), routing.Wrap(hs.GetUserFromLDAP))
|
||||
adminRoute.Get("/ldap/status", authorize(reqGrafanaAdmin, accesscontrol.ActionLDAPStatusRead), routing.Wrap(hs.GetLDAPStatus))
|
||||
|
||||
@@ -1,12 +1,16 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/grafana/grafana/pkg/services/accesscontrol"
|
||||
"github.com/grafana/grafana/pkg/services/accesscontrol/evaluator"
|
||||
|
||||
"github.com/grafana/grafana/pkg/api/response"
|
||||
"github.com/grafana/grafana/pkg/api/routing"
|
||||
"github.com/grafana/grafana/pkg/bus"
|
||||
@@ -229,3 +233,40 @@ type fakeRenderService struct {
|
||||
func (s *fakeRenderService) Init() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
var _ accesscontrol.AccessControl = new(fakeAccessControl)
|
||||
|
||||
type fakeAccessControl struct {
|
||||
isDisabled bool
|
||||
permissions []*accesscontrol.Permission
|
||||
}
|
||||
|
||||
func (f *fakeAccessControl) Evaluate(ctx context.Context, user *models.SignedInUser, permission string, scope ...string) (bool, error) {
|
||||
return evaluator.Evaluate(ctx, f, user, permission, scope...)
|
||||
}
|
||||
|
||||
func (f *fakeAccessControl) GetUserPermissions(ctx context.Context, user *models.SignedInUser) ([]*accesscontrol.Permission, error) {
|
||||
return f.permissions, nil
|
||||
}
|
||||
|
||||
func (f *fakeAccessControl) IsDisabled() bool {
|
||||
return f.isDisabled
|
||||
}
|
||||
|
||||
func setupAccessControlScenarioContext(t *testing.T, cfg *setting.Cfg, url string, permissions []*accesscontrol.Permission) *scenarioContext {
|
||||
cfg.FeatureToggles = make(map[string]bool)
|
||||
cfg.FeatureToggles["accesscontrol"] = true
|
||||
|
||||
hs := &HTTPServer{
|
||||
Cfg: cfg,
|
||||
RouteRegister: routing.NewRouteRegister(),
|
||||
AccessControl: &fakeAccessControl{permissions: permissions},
|
||||
}
|
||||
|
||||
sc := setupScenarioContext(t, url)
|
||||
|
||||
hs.registerRoutes()
|
||||
hs.RouteRegister.Register(sc.m.Router)
|
||||
|
||||
return sc
|
||||
}
|
||||
|
||||
@@ -4,8 +4,11 @@ import (
|
||||
"errors"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/grafana/grafana/pkg/services/accesscontrol"
|
||||
|
||||
"github.com/grafana/grafana/pkg/api/response"
|
||||
"github.com/grafana/grafana/pkg/api/routing"
|
||||
"github.com/grafana/grafana/pkg/bus"
|
||||
@@ -573,3 +576,142 @@ func TestPostSyncUserWithLDAPAPIEndpoint_WhenUserNotInLDAP(t *testing.T) {
|
||||
|
||||
assert.JSONEq(t, expected, sc.resp.Body.String())
|
||||
}
|
||||
|
||||
// ***
|
||||
// Access control tests for ldap endpoints
|
||||
// ***
|
||||
|
||||
type ldapAccessControlTestCase struct {
|
||||
expectedCode int
|
||||
desc string
|
||||
url string
|
||||
method string
|
||||
permissions []*accesscontrol.Permission
|
||||
}
|
||||
|
||||
func TestLDAP_AccessControl(t *testing.T) {
|
||||
tests := []ldapAccessControlTestCase{
|
||||
{
|
||||
url: "/api/admin/ldap/reload",
|
||||
method: http.MethodPost,
|
||||
desc: "ReloadLDAPCfg should return 200 for user with correct permissions",
|
||||
expectedCode: http.StatusOK,
|
||||
permissions: []*accesscontrol.Permission{
|
||||
{Action: accesscontrol.ActionLDAPConfigReload},
|
||||
},
|
||||
},
|
||||
{
|
||||
url: "/api/admin/ldap/reload",
|
||||
method: http.MethodPost,
|
||||
desc: "ReloadLDAPCfg should return 403 for user without required permissions",
|
||||
expectedCode: http.StatusForbidden,
|
||||
permissions: []*accesscontrol.Permission{
|
||||
{Action: "wrong"},
|
||||
},
|
||||
},
|
||||
{
|
||||
url: "/api/admin/ldap/status",
|
||||
method: http.MethodGet,
|
||||
desc: "GetLDAPStatus should return 200 for user without required permissions",
|
||||
expectedCode: http.StatusOK,
|
||||
permissions: []*accesscontrol.Permission{
|
||||
{Action: accesscontrol.ActionLDAPStatusRead},
|
||||
},
|
||||
},
|
||||
{
|
||||
url: "/api/admin/ldap/status",
|
||||
method: http.MethodGet,
|
||||
desc: "GetLDAPStatus should return 200 for user without required permissions",
|
||||
expectedCode: http.StatusForbidden,
|
||||
permissions: []*accesscontrol.Permission{
|
||||
{Action: "wrong"},
|
||||
},
|
||||
},
|
||||
{
|
||||
url: "/api/admin/ldap/test",
|
||||
method: http.MethodGet,
|
||||
desc: "GetUserFromLDAP should return 200 for user with required permissions",
|
||||
expectedCode: http.StatusOK,
|
||||
permissions: []*accesscontrol.Permission{
|
||||
{Action: accesscontrol.ActionLDAPUsersRead},
|
||||
},
|
||||
},
|
||||
{
|
||||
url: "/api/admin/ldap/test",
|
||||
method: http.MethodGet,
|
||||
desc: "GetUserFromLDAP should return 403 for user without required permissions",
|
||||
expectedCode: http.StatusForbidden,
|
||||
permissions: []*accesscontrol.Permission{
|
||||
{Action: "wrong"},
|
||||
},
|
||||
},
|
||||
{
|
||||
url: "/api/admin/ldap/sync/test",
|
||||
method: http.MethodPost,
|
||||
desc: "PostSyncUserWithLDAP should return 200 for user without required permissions",
|
||||
expectedCode: http.StatusOK,
|
||||
permissions: []*accesscontrol.Permission{
|
||||
{Action: accesscontrol.ActionLDAPUsersSync},
|
||||
},
|
||||
},
|
||||
{
|
||||
url: "/api/admin/ldap/sync/test",
|
||||
method: http.MethodPost,
|
||||
desc: "PostSyncUserWithLDAP should return 200 for user without required permissions",
|
||||
expectedCode: http.StatusForbidden,
|
||||
permissions: []*accesscontrol.Permission{
|
||||
{Action: "wrong"},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.desc, func(t *testing.T) {
|
||||
t.Helper()
|
||||
|
||||
enabled := setting.LDAPEnabled
|
||||
configFile := setting.LDAPConfigFile
|
||||
|
||||
t.Cleanup(func() {
|
||||
setting.LDAPEnabled = enabled
|
||||
setting.LDAPConfigFile = configFile
|
||||
})
|
||||
|
||||
setting.LDAPEnabled = true
|
||||
path, err := filepath.Abs("../../conf/ldap.toml")
|
||||
assert.NoError(t, err)
|
||||
setting.LDAPConfigFile = path
|
||||
|
||||
cfg := setting.NewCfg()
|
||||
cfg.LDAPEnabled = true
|
||||
|
||||
sc := setupAccessControlScenarioContext(t, cfg, test.url, test.permissions)
|
||||
sc.resp = httptest.NewRecorder()
|
||||
sc.req, err = http.NewRequest(test.method, test.url, nil)
|
||||
assert.NoError(t, err)
|
||||
|
||||
// Add minimal setup to pass handler
|
||||
userSearchResult = &models.ExternalUserInfo{}
|
||||
userSearchError = nil
|
||||
newLDAP = func(_ []*ldap.ServerConfig) multildap.IMultiLDAP {
|
||||
return &LDAPMock{}
|
||||
}
|
||||
|
||||
bus.AddHandler("test", func(q *models.GetUserByIdQuery) error {
|
||||
q.Result = &models.User{}
|
||||
return nil
|
||||
})
|
||||
|
||||
bus.AddHandler("test", func(q *models.GetAuthInfoQuery) error {
|
||||
return nil
|
||||
})
|
||||
|
||||
bus.AddHandler("test", func(cmd *models.UpsertUserCommand) error {
|
||||
return nil
|
||||
})
|
||||
|
||||
sc.exec()
|
||||
assert.Equal(t, test.expectedCode, sc.resp.Code)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user