@ -15,7 +15,6 @@ import (
"github.com/grafana/grafana/pkg/services/apikey"
"github.com/grafana/grafana/pkg/services/apikey"
"github.com/grafana/grafana/pkg/services/authn"
"github.com/grafana/grafana/pkg/services/authn"
"github.com/grafana/grafana/pkg/services/login"
"github.com/grafana/grafana/pkg/services/login"
"github.com/grafana/grafana/pkg/services/org"
"github.com/grafana/grafana/pkg/util"
"github.com/grafana/grafana/pkg/util"
)
)
@ -24,14 +23,11 @@ var (
errAPIKeyExpired = errutil . Unauthorized ( "api-key.expired" , errutil . WithPublicMessage ( "Expired API key" ) )
errAPIKeyExpired = errutil . Unauthorized ( "api-key.expired" , errutil . WithPublicMessage ( "Expired API key" ) )
errAPIKeyRevoked = errutil . Unauthorized ( "api-key.revoked" , errutil . WithPublicMessage ( "Revoked API key" ) )
errAPIKeyRevoked = errutil . Unauthorized ( "api-key.revoked" , errutil . WithPublicMessage ( "Revoked API key" ) )
errAPIKeyOrgMismatch = errutil . Unauthorized ( "api-key.organization-mismatch" , errutil . WithPublicMessage ( "API key does not belong to the requested organization" ) )
errAPIKeyOrgMismatch = errutil . Unauthorized ( "api-key.organization-mismatch" , errutil . WithPublicMessage ( "API key does not belong to the requested organization" ) )
errAPIKeyInvalidType = errutil . BadRequest ( "api-key.invalid-type-id" )
)
)
var (
var (
_ authn . HookClient = new ( APIKey )
_ authn . HookClient = new ( APIKey )
_ authn . ContextAwareClient = new ( APIKey )
_ authn . ContextAwareClient = new ( APIKey )
_ authn . IdentityResolverClient = new ( APIKey )
)
)
const (
const (
@ -80,11 +76,6 @@ func (s *APIKey) Authenticate(ctx context.Context, r *authn.Request) (*authn.Ide
r . SetMeta ( metaKeySkipLastUsed , "true" )
r . SetMeta ( metaKeySkipLastUsed , "true" )
}
}
// if the api key don't belong to a service account construct the identity and return it
if key . ServiceAccountId == nil || * key . ServiceAccountId < 1 {
return newAPIKeyIdentity ( key ) , nil
}
return newServiceAccountIdentity ( key ) , nil
return newServiceAccountIdentity ( key ) , nil
}
}
@ -152,38 +143,6 @@ func (s *APIKey) Priority() uint {
return 30
return 30
}
}
func ( s * APIKey ) IdentityType ( ) claims . IdentityType {
return claims . TypeAPIKey
}
func ( s * APIKey ) ResolveIdentity ( ctx context . Context , orgID int64 , typ claims . IdentityType , id string ) ( * authn . Identity , error ) {
if ! claims . IsIdentityType ( typ , claims . TypeAPIKey ) {
return nil , errAPIKeyInvalidType . Errorf ( "got unexpected type: %s" , typ )
}
apiKeyID , err := strconv . ParseInt ( id , 10 , 64 )
if err != nil {
return nil , err
}
key , err := s . apiKeyService . GetApiKeyById ( ctx , & apikey . GetByIDQuery {
ApiKeyID : apiKeyID ,
} )
if err != nil {
return nil , err
}
if err := validateApiKey ( orgID , key ) ; err != nil {
return nil , err
}
if key . ServiceAccountId != nil && * key . ServiceAccountId >= 1 {
return nil , errAPIKeyInvalidType . Errorf ( "api key belongs to service account" )
}
return newAPIKeyIdentity ( key ) , nil
}
func ( s * APIKey ) Hook ( ctx context . Context , identity * authn . Identity , r * authn . Request ) error {
func ( s * APIKey ) Hook ( ctx context . Context , identity * authn . Identity , r * authn . Request ) error {
if r . GetMeta ( metaKeySkipLastUsed ) != "" {
if r . GetMeta ( metaKeySkipLastUsed ) != "" {
return nil
return nil
@ -248,18 +207,12 @@ func validateApiKey(orgID int64, key *apikey.APIKey) error {
return errAPIKeyOrgMismatch . Errorf ( "API does not belong in Organization" )
return errAPIKeyOrgMismatch . Errorf ( "API does not belong in Organization" )
}
}
return nil
// plain API keys are no longer supported so an error is returned if the api key doesn't belong to a service account
}
if key . ServiceAccountId == nil || * key . ServiceAccountId < 1 {
return errAPIKeyInvalid . Errorf ( "API key does not belong to a service account" )
func newAPIKeyIdentity ( key * apikey . APIKey ) * authn . Identity {
return & authn . Identity {
ID : strconv . FormatInt ( key . ID , 10 ) ,
Type : claims . TypeAPIKey ,
OrgID : key . OrgID ,
OrgRoles : map [ int64 ] org . RoleType { key . OrgID : key . Role } ,
ClientParams : authn . ClientParams { SyncPermissions : true } ,
AuthenticatedBy : login . APIKeyAuthModule ,
}
}
return nil
}
}
func newServiceAccountIdentity ( key * apikey . APIKey ) * authn . Identity {
func newServiceAccountIdentity ( key * apikey . APIKey ) * authn . Identity {