Identity: Rename "namespace" to "type" in the requester interface (#90567)

This commit is contained in:
Ryan McKinley
2024-07-25 12:52:14 +03:00
committed by GitHub
parent 8cdf5ee824
commit 9db3bc926e
109 changed files with 649 additions and 632 deletions
+1 -1
View File
@@ -7,7 +7,7 @@ import (
)
var (
ErrInvalidNamespaceID = errutil.BadRequest("auth.identity.invalid-namespace-id")
ErrInvalidTypedID = errutil.BadRequest("auth.identity.invalid-typed-id")
ErrNotIntIdentifier = errors.New("identifier is not an int64")
ErrIdentifierNotInitialized = errors.New("identifier is not initialized")
)
+70 -59
View File
@@ -6,120 +6,131 @@ import (
"strings"
)
type Namespace string
type IdentityType string
const (
NamespaceUser Namespace = "user"
NamespaceAPIKey Namespace = "api-key"
NamespaceServiceAccount Namespace = "service-account"
NamespaceAnonymous Namespace = "anonymous"
NamespaceRenderService Namespace = "render"
NamespaceAccessPolicy Namespace = "access-policy"
NamespaceProvisioning Namespace = "provisioning"
NamespaceEmpty Namespace = ""
TypeUser IdentityType = "user"
TypeAPIKey IdentityType = "api-key"
TypeServiceAccount IdentityType = "service-account"
TypeAnonymous IdentityType = "anonymous"
TypeRenderService IdentityType = "render"
TypeAccessPolicy IdentityType = "access-policy"
TypeProvisioning IdentityType = "provisioning"
TypeEmpty IdentityType = ""
)
func (n Namespace) String() string {
func (n IdentityType) String() string {
return string(n)
}
func ParseNamespace(str string) (Namespace, error) {
func ParseType(str string) (IdentityType, 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
case string(TypeUser):
return TypeUser, nil
case string(TypeAPIKey):
return TypeAPIKey, nil
case string(TypeServiceAccount):
return TypeServiceAccount, nil
case string(TypeAnonymous):
return TypeAnonymous, nil
case string(TypeRenderService):
return TypeRenderService, nil
case string(TypeAccessPolicy):
return TypeAccessPolicy, nil
default:
return "", ErrInvalidNamespaceID.Errorf("got invalid namespace %s", str)
return "", ErrInvalidTypedID.Errorf("got invalid identity type %s", str)
}
}
var AnonymousNamespaceID = NewNamespaceID(NamespaceAnonymous, 0)
// IsIdentityType returns true if type matches any expected identity type
func IsIdentityType(typ IdentityType, expected ...IdentityType) bool {
for _, e := range expected {
if typ == e {
return true
}
}
func ParseNamespaceID(str string) (NamespaceID, error) {
var namespaceID NamespaceID
return false
}
var AnonymousTypedID = NewTypedID(TypeAnonymous, 0)
func ParseTypedID(str string) (TypedID, error) {
var typeID TypedID
parts := strings.Split(str, ":")
if len(parts) != 2 {
return namespaceID, ErrInvalidNamespaceID.Errorf("expected namespace id to have 2 parts")
return typeID, ErrInvalidTypedID.Errorf("expected typed id to have 2 parts")
}
namespace, err := ParseNamespace(parts[0])
t, err := ParseType(parts[0])
if err != nil {
return namespaceID, err
return typeID, err
}
namespaceID.id = parts[1]
namespaceID.namespace = namespace
typeID.id = parts[1]
typeID.t = t
return namespaceID, nil
return typeID, nil
}
// MustParseNamespaceID parses namespace id, it will panic if it fails to do so.
// MustParseTypedID 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)
func MustParseTypedID(str string) TypedID {
typeID, err := ParseTypedID(str)
if err != nil {
panic(err)
}
return namespaceID
return typeID
}
func NewNamespaceID(namespace Namespace, id int64) NamespaceID {
return NamespaceID{
id: strconv.FormatInt(id, 10),
namespace: namespace,
func NewTypedID(t IdentityType, id int64) TypedID {
return TypedID{
id: strconv.FormatInt(id, 10),
t: t,
}
}
// NewNamespaceIDString creates a new NamespaceID with a string id
func NewNamespaceIDString(namespace Namespace, id string) NamespaceID {
return NamespaceID{
id: id,
namespace: namespace,
// NewTypedIDString creates a new TypedID with a string id
func NewTypedIDString(t IdentityType, id string) TypedID {
return TypedID{
id: id,
t: t,
}
}
// FIXME: use this instead of encoded string through the codebase
type NamespaceID struct {
id string
namespace Namespace
type TypedID struct {
id string
t IdentityType
}
func (ni NamespaceID) ID() string {
func (ni TypedID) 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) {
func (ni TypedID) UserID() (int64, error) {
if ni.IsType(TypeUser, TypeServiceAccount) {
return ni.ParseInt()
}
return 0, nil
}
// ParseInt will try to parse the id as an int64 identifier.
func (ni NamespaceID) ParseInt() (int64, error) {
func (ni TypedID) ParseInt() (int64, error) {
return strconv.ParseInt(ni.id, 10, 64)
}
func (ni NamespaceID) Namespace() Namespace {
return ni.namespace
func (ni TypedID) Type() IdentityType {
return ni.t
}
func (ni NamespaceID) IsNamespace(expected ...Namespace) bool {
return IsNamespace(ni.namespace, expected...)
func (ni TypedID) IsType(expected ...IdentityType) bool {
return IsIdentityType(ni.t, expected...)
}
func (ni NamespaceID) String() string {
return fmt.Sprintf("%s:%s", ni.namespace, ni.id)
func (ni TypedID) String() string {
return fmt.Sprintf("%s:%s", ni.t, ni.id)
}
+10 -21
View File
@@ -7,13 +7,13 @@ import (
type Requester interface {
// GetID returns namespaced id for the entity
GetID() NamespaceID
// GetNamespacedID returns the namespace and ID of the active entity.
GetID() TypedID
// GetTypedID returns the namespace and ID of the active entity.
// The namespace is one of the constants defined in pkg/apimachinery/identity.
// Deprecated: use GetID instead
GetNamespacedID() (namespace Namespace, identifier string)
GetTypedID() (kind IdentityType, identifier string)
// GetUID returns namespaced uid for the entity
GetUID() NamespaceID
GetUID() TypedID
// 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
@@ -68,25 +68,14 @@ type Requester interface {
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) {
func IntIdentifier(kind IdentityType, identifier string) (int64, error) {
if IsIdentityType(kind, TypeUser, TypeAPIKey, TypeServiceAccount, TypeRenderService) {
id, err := strconv.ParseInt(identifier, 10, 64)
if err != nil {
return 0, fmt.Errorf("unrecognized format for valid namespace %s: %w", namespace, err)
return 0, fmt.Errorf("unrecognized format for valid type %s: %w", kind, err)
}
if id < 1 {
@@ -102,14 +91,14 @@ func IntIdentifier(namespace Namespace, identifier string) (int64, error) {
// 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)
func UserIdentifier(kind IdentityType, identifier string) (int64, error) {
userID, err := IntIdentifier(kind, identifier)
if err != nil {
// FIXME: return this error once entity namespaces are handled by stores
return 0, nil
}
if IsNamespace(namespace, NamespaceUser, NamespaceServiceAccount) {
if IsIdentityType(kind, TypeUser, TypeServiceAccount) {
return userID, nil
}
+8 -8
View File
@@ -9,7 +9,7 @@ var _ Requester = &StaticRequester{}
// This is mostly copied from:
// https://github.com/grafana/grafana/blob/v11.0.0/pkg/services/user/identity.go#L16
type StaticRequester struct {
Namespace Namespace
Type IdentityType
UserID int64
UserUID string
OrgID int64
@@ -105,19 +105,19 @@ func (u *StaticRequester) HasUniqueId() bool {
}
// GetID returns namespaced id for the entity
func (u *StaticRequester) GetID() NamespaceID {
return NewNamespaceIDString(u.Namespace, fmt.Sprintf("%d", u.UserID))
func (u *StaticRequester) GetID() TypedID {
return NewTypedIDString(u.Type, fmt.Sprintf("%d", u.UserID))
}
// GetUID returns namespaced uid for the entity
func (u *StaticRequester) GetUID() NamespaceID {
return NewNamespaceIDString(u.Namespace, u.UserUID)
func (u *StaticRequester) GetUID() TypedID {
return NewTypedIDString(u.Type, u.UserUID)
}
// GetNamespacedID returns the namespace and ID of the active entity
// GetTypedID returns the namespace and ID of the active entity
// The namespace is one of the constants defined in pkg/apimachinery/identity
func (u *StaticRequester) GetNamespacedID() (Namespace, string) {
return u.Namespace, fmt.Sprintf("%d", u.UserID)
func (u *StaticRequester) GetTypedID() (IdentityType, string) {
return u.Type, fmt.Sprintf("%d", u.UserID)
}
func (u *StaticRequester) GetAuthID() string {