mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
LDAP: Use an interface instead of a bus to get group teams (#42165)
* Remove bus for GetTeams for LDAP * Fix lint
This commit is contained in:
parent
8b2fe460e2
commit
875e0736ec
@ -11,8 +11,6 @@ import (
|
|||||||
"path/filepath"
|
"path/filepath"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/stretchr/testify/require"
|
|
||||||
|
|
||||||
"github.com/grafana/grafana/pkg/api/response"
|
"github.com/grafana/grafana/pkg/api/response"
|
||||||
"github.com/grafana/grafana/pkg/api/routing"
|
"github.com/grafana/grafana/pkg/api/routing"
|
||||||
"github.com/grafana/grafana/pkg/bus"
|
"github.com/grafana/grafana/pkg/bus"
|
||||||
@ -31,6 +29,7 @@ import (
|
|||||||
"github.com/grafana/grafana/pkg/services/auth"
|
"github.com/grafana/grafana/pkg/services/auth"
|
||||||
"github.com/grafana/grafana/pkg/services/contexthandler"
|
"github.com/grafana/grafana/pkg/services/contexthandler"
|
||||||
"github.com/grafana/grafana/pkg/services/featuremgmt"
|
"github.com/grafana/grafana/pkg/services/featuremgmt"
|
||||||
|
"github.com/grafana/grafana/pkg/services/ldap"
|
||||||
"github.com/grafana/grafana/pkg/services/quota"
|
"github.com/grafana/grafana/pkg/services/quota"
|
||||||
"github.com/grafana/grafana/pkg/services/rendering"
|
"github.com/grafana/grafana/pkg/services/rendering"
|
||||||
"github.com/grafana/grafana/pkg/services/searchusers"
|
"github.com/grafana/grafana/pkg/services/searchusers"
|
||||||
@ -38,6 +37,7 @@ import (
|
|||||||
"github.com/grafana/grafana/pkg/services/sqlstore"
|
"github.com/grafana/grafana/pkg/services/sqlstore"
|
||||||
"github.com/grafana/grafana/pkg/setting"
|
"github.com/grafana/grafana/pkg/setting"
|
||||||
"github.com/grafana/grafana/pkg/web"
|
"github.com/grafana/grafana/pkg/web"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
func loggedInUserScenario(t *testing.T, desc string, url string, routePattern string, fn scenarioFunc) {
|
func loggedInUserScenario(t *testing.T, desc string, url string, routePattern string, fn scenarioFunc) {
|
||||||
@ -230,6 +230,7 @@ func setupAccessControlScenarioContext(t *testing.T, cfg *setting.Cfg, url strin
|
|||||||
RouteRegister: routing.NewRouteRegister(),
|
RouteRegister: routing.NewRouteRegister(),
|
||||||
AccessControl: accesscontrolmock.New().WithPermissions(permissions),
|
AccessControl: accesscontrolmock.New().WithPermissions(permissions),
|
||||||
searchUsersService: searchusers.ProvideUsersService(bus, filters.ProvideOSSSearchUserFilter()),
|
searchUsersService: searchusers.ProvideUsersService(bus, filters.ProvideOSSSearchUserFilter()),
|
||||||
|
ldapGroups: ldap.ProvideGroupsService(),
|
||||||
}
|
}
|
||||||
|
|
||||||
sc := setupScenarioContext(t, url)
|
sc := setupScenarioContext(t, url)
|
||||||
|
@ -38,6 +38,7 @@ import (
|
|||||||
"github.com/grafana/grafana/pkg/services/encryption"
|
"github.com/grafana/grafana/pkg/services/encryption"
|
||||||
"github.com/grafana/grafana/pkg/services/featuremgmt"
|
"github.com/grafana/grafana/pkg/services/featuremgmt"
|
||||||
"github.com/grafana/grafana/pkg/services/hooks"
|
"github.com/grafana/grafana/pkg/services/hooks"
|
||||||
|
"github.com/grafana/grafana/pkg/services/ldap"
|
||||||
"github.com/grafana/grafana/pkg/services/libraryelements"
|
"github.com/grafana/grafana/pkg/services/libraryelements"
|
||||||
"github.com/grafana/grafana/pkg/services/librarypanels"
|
"github.com/grafana/grafana/pkg/services/librarypanels"
|
||||||
"github.com/grafana/grafana/pkg/services/live"
|
"github.com/grafana/grafana/pkg/services/live"
|
||||||
@ -122,6 +123,7 @@ type HTTPServer struct {
|
|||||||
grafanaUpdateChecker *updatechecker.GrafanaService
|
grafanaUpdateChecker *updatechecker.GrafanaService
|
||||||
pluginsUpdateChecker *updatechecker.PluginsService
|
pluginsUpdateChecker *updatechecker.PluginsService
|
||||||
searchUsersService searchusers.Service
|
searchUsersService searchusers.Service
|
||||||
|
ldapGroups ldap.Groups
|
||||||
teamGuardian teamguardian.TeamGuardian
|
teamGuardian teamguardian.TeamGuardian
|
||||||
queryDataService *query.Service
|
queryDataService *query.Service
|
||||||
serviceAccountsService serviceaccounts.Service
|
serviceAccountsService serviceaccounts.Service
|
||||||
@ -152,7 +154,7 @@ func ProvideHTTPServer(opts ServerOptions, cfg *setting.Cfg, routeRegister routi
|
|||||||
encryptionService encryption.Internal, grafanaUpdateChecker *updatechecker.GrafanaService,
|
encryptionService encryption.Internal, grafanaUpdateChecker *updatechecker.GrafanaService,
|
||||||
pluginsUpdateChecker *updatechecker.PluginsService, searchUsersService searchusers.Service,
|
pluginsUpdateChecker *updatechecker.PluginsService, searchUsersService searchusers.Service,
|
||||||
dataSourcesService *datasources.Service, secretsService secrets.Service, queryDataService *query.Service,
|
dataSourcesService *datasources.Service, secretsService secrets.Service, queryDataService *query.Service,
|
||||||
teamGuardian teamguardian.TeamGuardian, serviceaccountsService serviceaccounts.Service,
|
ldapGroups ldap.Groups, teamGuardian teamguardian.TeamGuardian, serviceaccountsService serviceaccounts.Service,
|
||||||
authInfoService authinfoservice.Service, resourcePermissionServices *resourceservices.ResourceServices) (*HTTPServer, error) {
|
authInfoService authinfoservice.Service, resourcePermissionServices *resourceservices.ResourceServices) (*HTTPServer, error) {
|
||||||
web.Env = cfg.Env
|
web.Env = cfg.Env
|
||||||
m := web.New()
|
m := web.New()
|
||||||
@ -207,6 +209,7 @@ func ProvideHTTPServer(opts ServerOptions, cfg *setting.Cfg, routeRegister routi
|
|||||||
SecretsService: secretsService,
|
SecretsService: secretsService,
|
||||||
DataSourcesService: dataSourcesService,
|
DataSourcesService: dataSourcesService,
|
||||||
searchUsersService: searchUsersService,
|
searchUsersService: searchUsersService,
|
||||||
|
ldapGroups: ldapGroups,
|
||||||
teamGuardian: teamGuardian,
|
teamGuardian: teamGuardian,
|
||||||
queryDataService: queryDataService,
|
queryDataService: queryDataService,
|
||||||
serviceAccountsService: serviceaccountsService,
|
serviceAccountsService: serviceaccountsService,
|
||||||
|
@ -311,14 +311,11 @@ func (hs *HTTPServer) GetUserFromLDAP(c *models.ReqContext) response.Response {
|
|||||||
return response.Error(http.StatusBadRequest, "An organization was not found - Please verify your LDAP configuration", err)
|
return response.Error(http.StatusBadRequest, "An organization was not found - Please verify your LDAP configuration", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
cmd := &models.GetTeamsForLDAPGroupCommand{Groups: user.Groups}
|
u.Teams, err = hs.ldapGroups.GetTeams(user.Groups)
|
||||||
err = bus.Dispatch(c.Req.Context(), cmd)
|
if err != nil {
|
||||||
if err != nil && !errors.Is(err, bus.ErrHandlerNotFound) {
|
|
||||||
return response.Error(http.StatusBadRequest, "Unable to find the teams for this user", err)
|
return response.Error(http.StatusBadRequest, "Unable to find the teams for this user", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
u.Teams = cmd.Result
|
|
||||||
|
|
||||||
return response.JSON(200, u)
|
return response.JSON(200, u)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -62,7 +62,7 @@ func getUserFromLDAPContext(t *testing.T, requestURL string) *scenarioContext {
|
|||||||
setting.LDAPEnabled = true
|
setting.LDAPEnabled = true
|
||||||
t.Cleanup(func() { setting.LDAPEnabled = origLDAP })
|
t.Cleanup(func() { setting.LDAPEnabled = origLDAP })
|
||||||
|
|
||||||
hs := &HTTPServer{Cfg: setting.NewCfg()}
|
hs := &HTTPServer{Cfg: setting.NewCfg(), ldapGroups: ldap.ProvideGroupsService()}
|
||||||
|
|
||||||
sc.defaultHandler = routing.Wrap(func(c *models.ReqContext) response.Response {
|
sc.defaultHandler = routing.Wrap(func(c *models.ReqContext) response.Response {
|
||||||
sc.context = c
|
sc.context = c
|
||||||
@ -274,11 +274,6 @@ func TestGetUserFromLDAPAPIEndpoint_WithTeamHandler(t *testing.T) {
|
|||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
|
|
||||||
bus.AddHandler("test", func(ctx context.Context, cmd *models.GetTeamsForLDAPGroupCommand) error {
|
|
||||||
cmd.Result = []models.TeamOrgGroupDTO{}
|
|
||||||
return nil
|
|
||||||
})
|
|
||||||
|
|
||||||
getLDAPConfig = func(*setting.Cfg) (*ldap.Config, error) {
|
getLDAPConfig = func(*setting.Cfg) (*ldap.Config, error) {
|
||||||
return &ldap.Config{}, nil
|
return &ldap.Config{}, nil
|
||||||
}
|
}
|
||||||
@ -310,7 +305,7 @@ func TestGetUserFromLDAPAPIEndpoint_WithTeamHandler(t *testing.T) {
|
|||||||
"roles": [
|
"roles": [
|
||||||
{ "orgId": 1, "orgRole": "Admin", "orgName": "Main Org.", "groupDN": "cn=admins,ou=groups,dc=grafana,dc=org" }
|
{ "orgId": 1, "orgRole": "Admin", "orgName": "Main Org.", "groupDN": "cn=admins,ou=groups,dc=grafana,dc=org" }
|
||||||
],
|
],
|
||||||
"teams": []
|
"teams": null
|
||||||
}
|
}
|
||||||
`
|
`
|
||||||
|
|
||||||
|
@ -120,8 +120,3 @@ type TeamOrgGroupDTO struct {
|
|||||||
OrgName string `json:"orgName"`
|
OrgName string `json:"orgName"`
|
||||||
GroupDN string `json:"groupDN"`
|
GroupDN string `json:"groupDN"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type GetTeamsForLDAPGroupCommand struct {
|
|
||||||
Groups []string
|
|
||||||
Result []TeamOrgGroupDTO
|
|
||||||
}
|
|
||||||
|
@ -21,6 +21,7 @@ import (
|
|||||||
"github.com/grafana/grafana/pkg/services/encryption/ossencryption"
|
"github.com/grafana/grafana/pkg/services/encryption/ossencryption"
|
||||||
"github.com/grafana/grafana/pkg/services/kmsproviders"
|
"github.com/grafana/grafana/pkg/services/kmsproviders"
|
||||||
"github.com/grafana/grafana/pkg/services/kmsproviders/osskmsproviders"
|
"github.com/grafana/grafana/pkg/services/kmsproviders/osskmsproviders"
|
||||||
|
"github.com/grafana/grafana/pkg/services/ldap"
|
||||||
"github.com/grafana/grafana/pkg/services/licensing"
|
"github.com/grafana/grafana/pkg/services/licensing"
|
||||||
"github.com/grafana/grafana/pkg/services/login"
|
"github.com/grafana/grafana/pkg/services/login"
|
||||||
"github.com/grafana/grafana/pkg/services/login/authinfoservice"
|
"github.com/grafana/grafana/pkg/services/login/authinfoservice"
|
||||||
@ -70,6 +71,8 @@ var wireExtsBasicSet = wire.NewSet(
|
|||||||
wire.Bind(new(accesscontrol.PermissionsProvider), new(*acdb.AccessControlStore)),
|
wire.Bind(new(accesscontrol.PermissionsProvider), new(*acdb.AccessControlStore)),
|
||||||
osskmsproviders.ProvideService,
|
osskmsproviders.ProvideService,
|
||||||
wire.Bind(new(kmsproviders.Service), new(osskmsproviders.Service)),
|
wire.Bind(new(kmsproviders.Service), new(osskmsproviders.Service)),
|
||||||
|
ldap.ProvideGroupsService,
|
||||||
|
wire.Bind(new(ldap.Groups), new(*ldap.OSSGroups)),
|
||||||
)
|
)
|
||||||
|
|
||||||
var wireExtsSet = wire.NewSet(
|
var wireExtsSet = wire.NewSet(
|
||||||
|
17
pkg/services/ldap/ldap_groups.go
Normal file
17
pkg/services/ldap/ldap_groups.go
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
package ldap
|
||||||
|
|
||||||
|
import "github.com/grafana/grafana/pkg/models"
|
||||||
|
|
||||||
|
type Groups interface {
|
||||||
|
GetTeams(groups []string) ([]models.TeamOrgGroupDTO, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
type OSSGroups struct{}
|
||||||
|
|
||||||
|
func ProvideGroupsService() *OSSGroups {
|
||||||
|
return &OSSGroups{}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (*OSSGroups) GetTeams(_ []string) ([]models.TeamOrgGroupDTO, error) {
|
||||||
|
return nil, nil
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user