Chore: Cleanup namespace and ID resolution (#79360)

* Chore: Cleanup namespace ID resolution

* Check for negative userID when relevant

* Reuse existing function for parsing ID as int

* Fix imports
This commit is contained in:
Vardan Torosyan
2023-12-21 20:42:05 +01:00
committed by GitHub
parent d160638c67
commit 63cd5a5625
8 changed files with 65 additions and 46 deletions

View File

@@ -10,6 +10,7 @@ import (
"github.com/grafana/grafana/pkg/components/satokengen"
"github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/services/apikey"
authidentity "github.com/grafana/grafana/pkg/services/auth/identity"
"github.com/grafana/grafana/pkg/services/authn"
"github.com/grafana/grafana/pkg/services/login"
"github.com/grafana/grafana/pkg/services/org"
@@ -175,8 +176,13 @@ func (s *APIKey) Hook(ctx context.Context, identity *authn.Identity, r *authn.Re
}
func (s *APIKey) getAPIKeyID(ctx context.Context, identity *authn.Identity, r *authn.Request) (apiKeyID int64, exists bool) {
namespace, id := identity.NamespacedID()
namespace, identifier := identity.GetNamespacedID()
id, err := authidentity.IntIdentifier(namespace, identifier)
if err != nil {
s.log.Warn("Failed to parse ID from identifier", "err", err)
return -1, false
}
if namespace == authn.NamespaceAPIKey {
return id, true
}

View File

@@ -12,6 +12,7 @@ import (
"time"
"github.com/grafana/grafana/pkg/infra/log"
authidentity "github.com/grafana/grafana/pkg/services/auth/identity"
"github.com/grafana/grafana/pkg/services/authn"
"github.com/grafana/grafana/pkg/services/login"
"github.com/grafana/grafana/pkg/services/user"
@@ -135,11 +136,16 @@ func (c *Proxy) Hook(ctx context.Context, identity *authn.Identity, r *authn.Req
return nil
}
namespace, id := identity.NamespacedID()
namespace, identifier := identity.GetNamespacedID()
if namespace != authn.NamespaceUser {
return nil
}
id, err := authidentity.IntIdentifier(namespace, identifier)
if err != nil {
c.log.Warn("Failed to cache proxy user", "error", err, "userId", identifier, "err", err)
return nil
}
c.log.FromContext(ctx).Debug("Cache proxy user", "userId", id)
bytes := []byte(strconv.FormatInt(id, 10))
if err := c.cache.Set(ctx, identity.ClientParams.CacheAuthProxyKey, bytes, time.Duration(c.cfg.AuthProxySyncTTL)*time.Minute); err != nil {