Chore: Fix error handling in postDashboard, remove UserDisplayDTO, fix live redis client initialization (#87206)
* clean up error handling in postDashboard and remove UserDisplayDTO * replace GetUserUID with GetUID and GetNamespacedUID, enforce namespace constant type * lint fix * lint fix * more lint fixes
This commit is contained in:
@@ -6,26 +6,43 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Namespace string
|
||||
|
||||
const (
|
||||
NamespaceUser = "user"
|
||||
NamespaceAPIKey = "api-key"
|
||||
NamespaceServiceAccount = "service-account"
|
||||
NamespaceAnonymous = "anonymous"
|
||||
NamespaceRenderService = "render"
|
||||
NamespaceAccessPolicy = "access-policy"
|
||||
NamespaceUser Namespace = "user"
|
||||
NamespaceAPIKey Namespace = "api-key"
|
||||
NamespaceServiceAccount Namespace = "service-account"
|
||||
NamespaceAnonymous Namespace = "anonymous"
|
||||
NamespaceRenderService Namespace = "render"
|
||||
NamespaceAccessPolicy Namespace = "access-policy"
|
||||
NamespaceEmpty Namespace = ""
|
||||
)
|
||||
|
||||
var AnonymousNamespaceID = MustNewNamespaceID(NamespaceAnonymous, 0)
|
||||
|
||||
var namespaceLookup = map[string]struct{}{
|
||||
NamespaceUser: {},
|
||||
NamespaceAPIKey: {},
|
||||
NamespaceServiceAccount: {},
|
||||
NamespaceAnonymous: {},
|
||||
NamespaceRenderService: {},
|
||||
NamespaceAccessPolicy: {},
|
||||
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 = MustNewNamespaceID(NamespaceAnonymous, 0)
|
||||
|
||||
func ParseNamespaceID(str string) (NamespaceID, error) {
|
||||
var namespaceID NamespaceID
|
||||
|
||||
@@ -34,13 +51,12 @@ func ParseNamespaceID(str string) (NamespaceID, error) {
|
||||
return namespaceID, ErrInvalidNamespaceID.Errorf("expected namespace id to have 2 parts")
|
||||
}
|
||||
|
||||
namespace, id := parts[0], parts[1]
|
||||
|
||||
if _, ok := namespaceLookup[namespace]; !ok {
|
||||
return namespaceID, ErrInvalidNamespaceID.Errorf("got invalid namespace %s", namespace)
|
||||
namespace, err := ParseNamespace(parts[0])
|
||||
if err != nil {
|
||||
return namespaceID, err
|
||||
}
|
||||
|
||||
namespaceID.id = id
|
||||
namespaceID.id = parts[1]
|
||||
namespaceID.namespace = namespace
|
||||
|
||||
return namespaceID, nil
|
||||
@@ -57,19 +73,16 @@ func MustParseNamespaceID(str string) NamespaceID {
|
||||
}
|
||||
|
||||
// NewNamespaceID creates a new NamespaceID, will fail for invalid namespace.
|
||||
func NewNamespaceID(namespace string, id int64) (NamespaceID, error) {
|
||||
var namespaceID NamespaceID
|
||||
if _, ok := namespaceLookup[namespace]; !ok {
|
||||
return namespaceID, ErrInvalidNamespaceID.Errorf("got invalid namespace %s", namespace)
|
||||
}
|
||||
namespaceID.id = strconv.FormatInt(id, 10)
|
||||
namespaceID.namespace = namespace
|
||||
return namespaceID, nil
|
||||
func NewNamespaceID(namespace Namespace, id int64) (NamespaceID, error) {
|
||||
return NamespaceID{
|
||||
id: strconv.FormatInt(id, 10),
|
||||
namespace: namespace,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// MustNewNamespaceID creates a new NamespaceID, will panic for invalid namespace.
|
||||
// Suitable to use in tests or when we can guarantee that we pass a correct format.
|
||||
func MustNewNamespaceID(namespace string, id int64) NamespaceID {
|
||||
func MustNewNamespaceID(namespace Namespace, id int64) NamespaceID {
|
||||
namespaceID, err := NewNamespaceID(namespace, id)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
@@ -79,17 +92,25 @@ func MustNewNamespaceID(namespace string, id int64) NamespaceID {
|
||||
|
||||
// NewNamespaceIDUnchecked creates a new NamespaceID without checking if namespace is valid.
|
||||
// It us up to the caller to ensure that namespace is valid.
|
||||
func NewNamespaceIDUnchecked(namespace string, id int64) NamespaceID {
|
||||
func NewNamespaceIDUnchecked(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 string
|
||||
namespace Namespace
|
||||
}
|
||||
|
||||
func (ni NamespaceID) ID() string {
|
||||
@@ -100,11 +121,11 @@ func (ni NamespaceID) ParseInt() (int64, error) {
|
||||
return strconv.ParseInt(ni.id, 10, 64)
|
||||
}
|
||||
|
||||
func (ni NamespaceID) Namespace() string {
|
||||
func (ni NamespaceID) Namespace() Namespace {
|
||||
return ni.namespace
|
||||
}
|
||||
|
||||
func (ni NamespaceID) IsNamespace(expected ...string) bool {
|
||||
func (ni NamespaceID) IsNamespace(expected ...Namespace) bool {
|
||||
return IsNamespace(ni.namespace, expected...)
|
||||
}
|
||||
|
||||
|
||||
@@ -12,7 +12,12 @@ type Requester interface {
|
||||
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.
|
||||
GetNamespacedID() (namespace string, identifier string)
|
||||
GetNamespacedID() (namespace Namespace, identifier string)
|
||||
// GetID returns namespaced id for the entity
|
||||
GetUID() NamespaceID
|
||||
// GetNamespacedID returns the namespace and ID of the active entity.
|
||||
// The namespace is one of the constants defined in pkg/services/auth/identity.
|
||||
GetNamespacedUID() (namespace Namespace, identifier string)
|
||||
// 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
|
||||
@@ -65,7 +70,7 @@ type Requester interface {
|
||||
}
|
||||
|
||||
// IsNamespace returns true if namespace matches any expected namespace
|
||||
func IsNamespace(namespace string, expected ...string) bool {
|
||||
func IsNamespace(namespace Namespace, expected ...Namespace) bool {
|
||||
for _, e := range expected {
|
||||
if namespace == e {
|
||||
return true
|
||||
@@ -78,7 +83,7 @@ func IsNamespace(namespace string, expected ...string) bool {
|
||||
// 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, identifier string) (int64, error) {
|
||||
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 {
|
||||
@@ -98,7 +103,7 @@ func IntIdentifier(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, identifier string) (int64, error) {
|
||||
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
|
||||
|
||||
@@ -82,7 +82,7 @@ func (s *Service) SignIdentity(ctx context.Context, id identity.Requester) (stri
|
||||
Claims: jwt.Claims{
|
||||
Issuer: s.cfg.AppURL,
|
||||
Audience: getAudience(id.GetOrgID()),
|
||||
Subject: getSubject(namespace, identifier),
|
||||
Subject: getSubject(namespace.String(), identifier),
|
||||
Expiry: jwt.NewNumericDate(now.Add(tokenTTL)),
|
||||
IssuedAt: jwt.NewNumericDate(now),
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user