Chore: Move identity and errutil to apimachinery module (#89116)
This commit is contained in:
@@ -7,8 +7,8 @@ import (
|
||||
|
||||
"golang.org/x/oauth2"
|
||||
|
||||
"github.com/grafana/grafana/pkg/apimachinery/identity"
|
||||
"github.com/grafana/grafana/pkg/services/auth"
|
||||
"github.com/grafana/grafana/pkg/services/auth/identity"
|
||||
"github.com/grafana/grafana/pkg/services/datasources"
|
||||
"github.com/grafana/grafana/pkg/services/login"
|
||||
"github.com/grafana/grafana/pkg/services/user"
|
||||
|
||||
@@ -8,8 +8,8 @@ import (
|
||||
"log/slog"
|
||||
"net/http"
|
||||
|
||||
"github.com/grafana/grafana/pkg/apimachinery/identity"
|
||||
"github.com/grafana/grafana/pkg/models/usertoken"
|
||||
"github.com/grafana/grafana/pkg/services/auth/identity"
|
||||
"github.com/grafana/grafana/pkg/setting"
|
||||
)
|
||||
|
||||
|
||||
@@ -5,7 +5,7 @@ import (
|
||||
|
||||
authnlib "github.com/grafana/authlib/authn"
|
||||
|
||||
"github.com/grafana/grafana/pkg/services/auth/identity"
|
||||
"github.com/grafana/grafana/pkg/apimachinery/identity"
|
||||
)
|
||||
|
||||
type IDService interface {
|
||||
|
||||
@@ -1,13 +0,0 @@
|
||||
package identity
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"github.com/grafana/grafana/pkg/util/errutil"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrInvalidNamespaceID = errutil.BadRequest("auth.identity.invalid-namespace-id")
|
||||
ErrNotIntIdentifier = errors.New("identifier is not an int64")
|
||||
ErrIdentifierNotInitialized = errors.New("identifier is not initialized")
|
||||
)
|
||||
@@ -1,124 +0,0 @@
|
||||
package identity
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Namespace string
|
||||
|
||||
const (
|
||||
NamespaceUser Namespace = "user"
|
||||
NamespaceAPIKey Namespace = "api-key"
|
||||
NamespaceServiceAccount Namespace = "service-account"
|
||||
NamespaceAnonymous Namespace = "anonymous"
|
||||
NamespaceRenderService Namespace = "render"
|
||||
NamespaceAccessPolicy Namespace = "access-policy"
|
||||
NamespaceEmpty Namespace = ""
|
||||
)
|
||||
|
||||
func (n Namespace) String() string {
|
||||
return string(n)
|
||||
}
|
||||
|
||||
func ParseNamespace(str string) (Namespace, error) {
|
||||
switch str {
|
||||
case string(NamespaceUser):
|
||||
return NamespaceUser, nil
|
||||
case string(NamespaceAPIKey):
|
||||
return NamespaceAPIKey, nil
|
||||
case string(NamespaceServiceAccount):
|
||||
return NamespaceServiceAccount, nil
|
||||
case string(NamespaceAnonymous):
|
||||
return NamespaceAnonymous, nil
|
||||
case string(NamespaceRenderService):
|
||||
return NamespaceRenderService, nil
|
||||
case string(NamespaceAccessPolicy):
|
||||
return NamespaceAccessPolicy, nil
|
||||
default:
|
||||
return "", ErrInvalidNamespaceID.Errorf("got invalid namespace %s", str)
|
||||
}
|
||||
}
|
||||
|
||||
var AnonymousNamespaceID = NewNamespaceID(NamespaceAnonymous, 0)
|
||||
|
||||
func ParseNamespaceID(str string) (NamespaceID, error) {
|
||||
var namespaceID NamespaceID
|
||||
|
||||
parts := strings.Split(str, ":")
|
||||
if len(parts) != 2 {
|
||||
return namespaceID, ErrInvalidNamespaceID.Errorf("expected namespace id to have 2 parts")
|
||||
}
|
||||
|
||||
namespace, err := ParseNamespace(parts[0])
|
||||
if err != nil {
|
||||
return namespaceID, err
|
||||
}
|
||||
|
||||
namespaceID.id = parts[1]
|
||||
namespaceID.namespace = namespace
|
||||
|
||||
return namespaceID, nil
|
||||
}
|
||||
|
||||
// MustParseNamespaceID parses namespace id, it will panic if it fails to do so.
|
||||
// Suitable to use in tests or when we can guarantee that we pass a correct format.
|
||||
func MustParseNamespaceID(str string) NamespaceID {
|
||||
namespaceID, err := ParseNamespaceID(str)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return namespaceID
|
||||
}
|
||||
|
||||
func NewNamespaceID(namespace Namespace, id int64) NamespaceID {
|
||||
return NamespaceID{
|
||||
id: strconv.FormatInt(id, 10),
|
||||
namespace: namespace,
|
||||
}
|
||||
}
|
||||
|
||||
// NewNamespaceIDString creates a new NamespaceID with a string id
|
||||
func NewNamespaceIDString(namespace Namespace, id string) NamespaceID {
|
||||
return NamespaceID{
|
||||
id: id,
|
||||
namespace: namespace,
|
||||
}
|
||||
}
|
||||
|
||||
// FIXME: use this instead of encoded string through the codebase
|
||||
type NamespaceID struct {
|
||||
id string
|
||||
namespace Namespace
|
||||
}
|
||||
|
||||
func (ni NamespaceID) ID() string {
|
||||
return ni.id
|
||||
}
|
||||
|
||||
// UserID will try to parse and int64 identifier if namespace is either user or service-account.
|
||||
// For all other namespaces '0' will be returned.
|
||||
func (ni NamespaceID) UserID() (int64, error) {
|
||||
if ni.IsNamespace(NamespaceUser, NamespaceServiceAccount) {
|
||||
return ni.ParseInt()
|
||||
}
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
// ParseInt will try to parse the id as an int64 identifier.
|
||||
func (ni NamespaceID) ParseInt() (int64, error) {
|
||||
return strconv.ParseInt(ni.id, 10, 64)
|
||||
}
|
||||
|
||||
func (ni NamespaceID) Namespace() Namespace {
|
||||
return ni.namespace
|
||||
}
|
||||
|
||||
func (ni NamespaceID) IsNamespace(expected ...Namespace) bool {
|
||||
return IsNamespace(ni.namespace, expected...)
|
||||
}
|
||||
|
||||
func (ni NamespaceID) String() string {
|
||||
return fmt.Sprintf("%s:%s", ni.namespace, ni.id)
|
||||
}
|
||||
@@ -1,116 +0,0 @@
|
||||
package identity
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
|
||||
"github.com/grafana/grafana/pkg/models/roletype"
|
||||
)
|
||||
|
||||
type Requester interface {
|
||||
// GetID returns namespaced id for the entity
|
||||
GetID() NamespaceID
|
||||
// GetNamespacedID returns the namespace and ID of the active entity.
|
||||
// The namespace is one of the constants defined in pkg/services/auth/identity.
|
||||
// Deprecated: use GetID instead
|
||||
GetNamespacedID() (namespace Namespace, identifier string)
|
||||
// GetUID returns namespaced uid for the entity
|
||||
GetUID() NamespaceID
|
||||
// GetDisplayName returns the display name of the active entity.
|
||||
// The display name is the name if it is set, otherwise the login or email.
|
||||
GetDisplayName() string
|
||||
// GetEmail returns the email of the active entity.
|
||||
// Can be empty.
|
||||
GetEmail() string
|
||||
// IsEmailVerified returns if email is verified for entity.
|
||||
IsEmailVerified() bool
|
||||
// GetIsGrafanaAdmin returns true if the user is a server admin
|
||||
GetIsGrafanaAdmin() bool
|
||||
// GetLogin returns the login of the active entity
|
||||
// Can be empty.
|
||||
GetLogin() string
|
||||
// GetOrgID returns the ID of the active organization
|
||||
GetOrgID() int64
|
||||
// GetOrgRole returns the role of the active entity in the active organization.
|
||||
GetOrgRole() roletype.RoleType
|
||||
// GetPermissions returns the permissions of the active entity.
|
||||
GetPermissions() map[string][]string
|
||||
// GetGlobalPermissions returns the permissions of the active entity that are available across all organizations.
|
||||
GetGlobalPermissions() map[string][]string
|
||||
// DEPRECATED: GetTeams returns the teams the entity is a member of.
|
||||
// Retrieve the teams from the team service instead of using this method.
|
||||
GetTeams() []int64
|
||||
// DEPRECATED: GetOrgName returns the name of the active organization.
|
||||
// Retrieve the organization name from the organization service instead of using this method.
|
||||
GetOrgName() string
|
||||
// GetAuthID returns external id for entity.
|
||||
GetAuthID() string
|
||||
// GetAuthenticatedBy returns the authentication method used to authenticate the entity.
|
||||
GetAuthenticatedBy() string
|
||||
// IsAuthenticatedBy returns true if entity was authenticated by any of supplied providers.
|
||||
IsAuthenticatedBy(providers ...string) bool
|
||||
// IsNil returns true if the identity is nil
|
||||
// FIXME: remove this method once all services are using an interface
|
||||
IsNil() bool
|
||||
|
||||
// Legacy
|
||||
|
||||
// HasRole returns true if the active entity has the given role in the active organization.
|
||||
HasRole(role roletype.RoleType) bool
|
||||
// GetCacheKey returns a unique key for the entity.
|
||||
// Add an extra prefix to avoid collisions with other caches
|
||||
GetCacheKey() string
|
||||
// HasUniqueId returns true if the entity has a unique id
|
||||
HasUniqueId() bool
|
||||
// GetIDToken returns a signed token representing the identity that can be forwarded to plugins and external services.
|
||||
// Will only be set when featuremgmt.FlagIdForwarding is enabled.
|
||||
GetIDToken() string
|
||||
}
|
||||
|
||||
// IsNamespace returns true if namespace matches any expected namespace
|
||||
func IsNamespace(namespace Namespace, expected ...Namespace) bool {
|
||||
for _, e := range expected {
|
||||
if namespace == e {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
// IntIdentifier converts a string identifier to an int64.
|
||||
// Applicable for users, service accounts, api keys and renderer service.
|
||||
// Errors if the identifier is not initialized or if namespace is not recognized.
|
||||
func IntIdentifier(namespace Namespace, identifier string) (int64, error) {
|
||||
if IsNamespace(namespace, NamespaceUser, NamespaceAPIKey, NamespaceServiceAccount, NamespaceRenderService) {
|
||||
id, err := strconv.ParseInt(identifier, 10, 64)
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("unrecognized format for valid namespace %s: %w", namespace, err)
|
||||
}
|
||||
|
||||
if id < 1 {
|
||||
return 0, ErrIdentifierNotInitialized
|
||||
}
|
||||
|
||||
return id, nil
|
||||
}
|
||||
|
||||
return 0, ErrNotIntIdentifier
|
||||
}
|
||||
|
||||
// UserIdentifier converts a string identifier to an int64.
|
||||
// Errors if the identifier is not initialized or if namespace is not recognized.
|
||||
// Returns 0 if the namespace is not user or service account
|
||||
func UserIdentifier(namespace Namespace, identifier string) (int64, error) {
|
||||
userID, err := IntIdentifier(namespace, identifier)
|
||||
if err != nil {
|
||||
// FIXME: return this error once entity namespaces are handled by stores
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
if IsNamespace(namespace, NamespaceUser, NamespaceServiceAccount) {
|
||||
return userID, nil
|
||||
}
|
||||
|
||||
return 0, nil
|
||||
}
|
||||
@@ -11,11 +11,11 @@ import (
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
"golang.org/x/sync/singleflight"
|
||||
|
||||
"github.com/grafana/grafana/pkg/apimachinery/identity"
|
||||
"github.com/grafana/grafana/pkg/infra/log"
|
||||
"github.com/grafana/grafana/pkg/infra/remotecache"
|
||||
"github.com/grafana/grafana/pkg/services/apiserver/endpoints/request"
|
||||
"github.com/grafana/grafana/pkg/services/auth"
|
||||
"github.com/grafana/grafana/pkg/services/auth/identity"
|
||||
"github.com/grafana/grafana/pkg/services/authn"
|
||||
"github.com/grafana/grafana/pkg/services/featuremgmt"
|
||||
"github.com/grafana/grafana/pkg/setting"
|
||||
|
||||
@@ -3,8 +3,8 @@ package idtest
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/grafana/grafana/pkg/apimachinery/identity"
|
||||
"github.com/grafana/grafana/pkg/services/auth"
|
||||
"github.com/grafana/grafana/pkg/services/auth/identity"
|
||||
)
|
||||
|
||||
var _ auth.IDService = (*MockService)(nil)
|
||||
|
||||
Reference in New Issue
Block a user