Auth: Remove code for authenticating API keys (#105998)
* remove auth for plain API keys * move condition to validateApiKey() * fix typo * remove GetApiKeyById() method
This commit is contained in:
@@ -15,7 +15,6 @@ import (
|
||||
"github.com/grafana/grafana/pkg/services/apikey"
|
||||
"github.com/grafana/grafana/pkg/services/authn"
|
||||
"github.com/grafana/grafana/pkg/services/login"
|
||||
"github.com/grafana/grafana/pkg/services/org"
|
||||
"github.com/grafana/grafana/pkg/util"
|
||||
)
|
||||
|
||||
@@ -24,14 +23,11 @@ var (
|
||||
errAPIKeyExpired = errutil.Unauthorized("api-key.expired", errutil.WithPublicMessage("Expired 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"))
|
||||
|
||||
errAPIKeyInvalidType = errutil.BadRequest("api-key.invalid-type-id")
|
||||
)
|
||||
|
||||
var (
|
||||
_ authn.HookClient = new(APIKey)
|
||||
_ authn.ContextAwareClient = new(APIKey)
|
||||
_ authn.IdentityResolverClient = new(APIKey)
|
||||
_ authn.HookClient = new(APIKey)
|
||||
_ authn.ContextAwareClient = new(APIKey)
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -80,11 +76,6 @@ func (s *APIKey) Authenticate(ctx context.Context, r *authn.Request) (*authn.Ide
|
||||
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
|
||||
}
|
||||
|
||||
@@ -152,38 +143,6 @@ func (s *APIKey) Priority() uint {
|
||||
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 {
|
||||
if r.GetMeta(metaKeySkipLastUsed) != "" {
|
||||
return nil
|
||||
@@ -248,18 +207,12 @@ func validateApiKey(orgID int64, key *apikey.APIKey) error {
|
||||
return errAPIKeyOrgMismatch.Errorf("API does not belong in Organization")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
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,
|
||||
// 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")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func newServiceAccountIdentity(key *apikey.APIKey) *authn.Identity {
|
||||
|
||||
Reference in New Issue
Block a user