mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Cfg: Deduplicate DefaultOrgID code (#93588)
Cfg: Expose DefaultOrgID function
This commit is contained in:
@@ -92,10 +92,7 @@ func (m *OrgRoleMapper) getDefaultOrgMapping(strictRoleMapping bool, directlyMap
|
|||||||
}
|
}
|
||||||
orgRoles := make(map[int64]org.RoleType, 0)
|
orgRoles := make(map[int64]org.RoleType, 0)
|
||||||
|
|
||||||
orgID := int64(1)
|
orgID := m.cfg.DefaultOrgID()
|
||||||
if m.cfg.AutoAssignOrg && m.cfg.AutoAssignOrgId > 0 {
|
|
||||||
orgID = int64(m.cfg.AutoAssignOrgId)
|
|
||||||
}
|
|
||||||
|
|
||||||
orgRoles[orgID] = directlyMappedRole
|
orgRoles[orgID] = directlyMappedRole
|
||||||
if !directlyMappedRole.IsValid() {
|
if !directlyMappedRole.IsValid() {
|
||||||
|
|||||||
@@ -101,7 +101,7 @@ func (s *ExtendedJWT) authenticateAsUser(
|
|||||||
accessTokenClaims authlib.Claims[authlib.AccessTokenClaims],
|
accessTokenClaims authlib.Claims[authlib.AccessTokenClaims],
|
||||||
) (*authn.Identity, error) {
|
) (*authn.Identity, error) {
|
||||||
// Only allow id tokens signed for namespace configured for this instance.
|
// Only allow id tokens signed for namespace configured for this instance.
|
||||||
if allowedNamespace := s.namespaceMapper(s.getDefaultOrgID()); !claims.NamespaceMatches(authlib.NewIdentityClaims(idTokenClaims), allowedNamespace) {
|
if allowedNamespace := s.namespaceMapper(s.cfg.DefaultOrgID()); !claims.NamespaceMatches(authlib.NewIdentityClaims(idTokenClaims), allowedNamespace) {
|
||||||
return nil, errExtJWTDisallowedNamespaceClaim.Errorf("unexpected id token namespace: %s", idTokenClaims.Rest.Namespace)
|
return nil, errExtJWTDisallowedNamespaceClaim.Errorf("unexpected id token namespace: %s", idTokenClaims.Rest.Namespace)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -138,7 +138,7 @@ func (s *ExtendedJWT) authenticateAsUser(
|
|||||||
return &authn.Identity{
|
return &authn.Identity{
|
||||||
ID: id,
|
ID: id,
|
||||||
Type: t,
|
Type: t,
|
||||||
OrgID: s.getDefaultOrgID(),
|
OrgID: s.cfg.DefaultOrgID(),
|
||||||
AccessTokenClaims: &accessTokenClaims,
|
AccessTokenClaims: &accessTokenClaims,
|
||||||
IDTokenClaims: &idTokenClaims,
|
IDTokenClaims: &idTokenClaims,
|
||||||
AuthenticatedBy: login.ExtendedJWTModule,
|
AuthenticatedBy: login.ExtendedJWTModule,
|
||||||
@@ -155,7 +155,7 @@ func (s *ExtendedJWT) authenticateAsUser(
|
|||||||
|
|
||||||
func (s *ExtendedJWT) authenticateAsService(accessTokenClaims authlib.Claims[authlib.AccessTokenClaims]) (*authn.Identity, error) {
|
func (s *ExtendedJWT) authenticateAsService(accessTokenClaims authlib.Claims[authlib.AccessTokenClaims]) (*authn.Identity, error) {
|
||||||
// Allow access tokens with that has a wildcard namespace or a namespace matching this instance.
|
// Allow access tokens with that has a wildcard namespace or a namespace matching this instance.
|
||||||
if allowedNamespace := s.namespaceMapper(s.getDefaultOrgID()); !claims.NamespaceMatches(authlib.NewAccessClaims(accessTokenClaims), allowedNamespace) {
|
if allowedNamespace := s.namespaceMapper(s.cfg.DefaultOrgID()); !claims.NamespaceMatches(authlib.NewAccessClaims(accessTokenClaims), allowedNamespace) {
|
||||||
return nil, errExtJWTDisallowedNamespaceClaim.Errorf("unexpected access token namespace: %s", accessTokenClaims.Rest.Namespace)
|
return nil, errExtJWTDisallowedNamespaceClaim.Errorf("unexpected access token namespace: %s", accessTokenClaims.Rest.Namespace)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -186,7 +186,7 @@ func (s *ExtendedJWT) authenticateAsService(accessTokenClaims authlib.Claims[aut
|
|||||||
ID: id,
|
ID: id,
|
||||||
UID: id,
|
UID: id,
|
||||||
Type: t,
|
Type: t,
|
||||||
OrgID: s.getDefaultOrgID(),
|
OrgID: s.cfg.DefaultOrgID(),
|
||||||
AccessTokenClaims: &accessTokenClaims,
|
AccessTokenClaims: &accessTokenClaims,
|
||||||
IDTokenClaims: nil,
|
IDTokenClaims: nil,
|
||||||
AuthenticatedBy: login.ExtendedJWTModule,
|
AuthenticatedBy: login.ExtendedJWTModule,
|
||||||
@@ -247,11 +247,3 @@ func (s *ExtendedJWT) retrieveAuthorizationToken(httpRequest *http.Request) stri
|
|||||||
// Strip the 'Bearer' prefix if it exists.
|
// Strip the 'Bearer' prefix if it exists.
|
||||||
return strings.TrimPrefix(jwtToken, "Bearer ")
|
return strings.TrimPrefix(jwtToken, "Bearer ")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *ExtendedJWT) getDefaultOrgID() int64 {
|
|
||||||
orgID := int64(1)
|
|
||||||
if s.cfg.AutoAssignOrg && s.cfg.AutoAssignOrgId > 0 {
|
|
||||||
orgID = int64(s.cfg.AutoAssignOrgId)
|
|
||||||
}
|
|
||||||
return orgID
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -20,11 +20,7 @@ func getRoles(cfg *setting.Cfg, extract roleExtractor) (map[int64]org.RoleType,
|
|||||||
return orgRoles, nil, nil
|
return orgRoles, nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
orgID := int64(1)
|
orgRoles[cfg.DefaultOrgID()] = role
|
||||||
if cfg.AutoAssignOrg && cfg.AutoAssignOrgId > 0 {
|
|
||||||
orgID = int64(cfg.AutoAssignOrgId)
|
|
||||||
}
|
|
||||||
orgRoles[orgID] = role
|
|
||||||
|
|
||||||
return orgRoles, isGrafanaAdmin, nil
|
return orgRoles, isGrafanaAdmin, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,21 +4,12 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
|
|
||||||
"github.com/grafana/grafana/pkg/services/accesscontrol"
|
"github.com/grafana/grafana/pkg/services/accesscontrol"
|
||||||
"github.com/grafana/grafana/pkg/setting"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
ServiceAccounts AuthProvider = "ServiceAccounts"
|
ServiceAccounts AuthProvider = "ServiceAccounts"
|
||||||
)
|
)
|
||||||
|
|
||||||
func DefaultOrgID(cfg *setting.Cfg) int64 {
|
|
||||||
orgID := int64(1)
|
|
||||||
if cfg.AutoAssignOrg && cfg.AutoAssignOrgId > 0 {
|
|
||||||
orgID = int64(cfg.AutoAssignOrgId)
|
|
||||||
}
|
|
||||||
return orgID
|
|
||||||
}
|
|
||||||
|
|
||||||
type AuthProvider string
|
type AuthProvider string
|
||||||
|
|
||||||
//go:generate mockery --name ExternalServiceRegistry --structname ExternalServiceRegistryMock --output tests --outpkg tests --filename extsvcregmock.go
|
//go:generate mockery --name ExternalServiceRegistry --structname ExternalServiceRegistryMock --output tests --outpkg tests --filename extsvcregmock.go
|
||||||
|
|||||||
@@ -40,7 +40,7 @@ func ProvideExtSvcAccountsService(acSvc ac.Service, cfg *setting.Cfg, bus bus.Bu
|
|||||||
logger := log.New("serviceauth.extsvcaccounts")
|
logger := log.New("serviceauth.extsvcaccounts")
|
||||||
esa := &ExtSvcAccountsService{
|
esa := &ExtSvcAccountsService{
|
||||||
acSvc: acSvc,
|
acSvc: acSvc,
|
||||||
defaultOrgID: extsvcauth.DefaultOrgID(cfg),
|
defaultOrgID: cfg.DefaultOrgID(),
|
||||||
logger: logger,
|
logger: logger,
|
||||||
saSvc: saSvc,
|
saSvc: saSvc,
|
||||||
features: features,
|
features: features,
|
||||||
|
|||||||
@@ -2051,3 +2051,10 @@ func (cfg *Cfg) readPublicDashboardsSettings() {
|
|||||||
publicDashboards := cfg.Raw.Section("public_dashboards")
|
publicDashboards := cfg.Raw.Section("public_dashboards")
|
||||||
cfg.PublicDashboardsEnabled = publicDashboards.Key("enabled").MustBool(true)
|
cfg.PublicDashboardsEnabled = publicDashboards.Key("enabled").MustBool(true)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (cfg *Cfg) DefaultOrgID() int64 {
|
||||||
|
if cfg.AutoAssignOrg && cfg.AutoAssignOrgId > 0 {
|
||||||
|
return int64(cfg.AutoAssignOrgId)
|
||||||
|
}
|
||||||
|
return int64(1)
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user