Auth: Allow service accounts to authenticate to ST Grafana (#115536)
* Allow SAs to authn ext_jwt * Address feedback
This commit is contained in:
@@ -131,7 +131,8 @@ func (s *ExtendedJWT) authenticateAsUser(
|
||||
return nil, errExtJWTInvalid.Errorf("failed to parse id token subject: %w", err)
|
||||
}
|
||||
|
||||
if !claims.IsIdentityType(t, claims.TypeUser) {
|
||||
// TODO: How to support other identity types like render and anonymous here?
|
||||
if !claims.IsIdentityType(t, claims.TypeUser, claims.TypeServiceAccount) {
|
||||
return nil, errExtJWTInvalidSubject.Errorf("unexpected identity: %s", idTokenClaims.Subject)
|
||||
}
|
||||
|
||||
|
||||
@@ -53,6 +53,17 @@ var (
|
||||
Namespace: "default", // org ID of 1 is special and translates to default
|
||||
},
|
||||
}
|
||||
validIDTokenClaimsWithServiceAccount = idTokenClaims{
|
||||
Claims: jwt.Claims{
|
||||
Subject: "service-account:3",
|
||||
Expiry: jwt.NewNumericDate(time.Date(2023, 5, 3, 0, 0, 0, 0, time.UTC)),
|
||||
IssuedAt: jwt.NewNumericDate(time.Date(2023, 5, 2, 0, 0, 0, 0, time.UTC)),
|
||||
},
|
||||
Rest: authnlib.IDTokenClaims{
|
||||
AuthenticatedBy: "extended_jwt",
|
||||
Namespace: "default", // org ID of 1 is special and translates to default
|
||||
},
|
||||
}
|
||||
validIDTokenClaimsWithStackSet = idTokenClaims{
|
||||
Claims: jwt.Claims{
|
||||
Subject: "user:2",
|
||||
@@ -118,7 +129,7 @@ var (
|
||||
}
|
||||
invalidSubjectIDTokenClaims = idTokenClaims{
|
||||
Claims: jwt.Claims{
|
||||
Subject: "service-account:2",
|
||||
Subject: "anonymous:2",
|
||||
Expiry: jwt.NewNumericDate(time.Date(2023, 5, 3, 0, 0, 0, 0, time.UTC)),
|
||||
IssuedAt: jwt.NewNumericDate(time.Date(2023, 5, 2, 0, 0, 0, 0, time.UTC)),
|
||||
},
|
||||
@@ -286,6 +297,29 @@ func TestExtendedJWT_Authenticate(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "should authenticate as service account",
|
||||
accessToken: &validAccessTokenClaims,
|
||||
idToken: &validIDTokenClaimsWithServiceAccount,
|
||||
orgID: 1,
|
||||
want: &authn.Identity{
|
||||
ID: "3",
|
||||
Type: claims.TypeServiceAccount,
|
||||
OrgID: 1,
|
||||
AccessTokenClaims: &validAccessTokenClaims,
|
||||
IDTokenClaims: &validIDTokenClaimsWithServiceAccount,
|
||||
Namespace: "default",
|
||||
AuthenticatedBy: "extendedjwt",
|
||||
AuthID: "access-policy:this-uid",
|
||||
ClientParams: authn.ClientParams{
|
||||
FetchSyncedUser: true,
|
||||
SyncPermissions: true,
|
||||
FetchPermissionsParams: authn.FetchPermissionsParams{
|
||||
RestrictedActions: []string{"dashboards:create", "folders:read", "datasources:explore", "datasources.insights:read"},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "should authenticate as user in the user namespace",
|
||||
accessToken: &validAccessTokenClaimsWildcard,
|
||||
|
||||
Reference in New Issue
Block a user