Auth: Use claims.AuthInfo in requester (#91739)

This commit is contained in:
Ryan McKinley
2024-08-09 19:46:56 +03:00
committed by GitHub
parent d52626be3f
commit 243c0935fc
19 changed files with 207 additions and 207 deletions
@@ -3,82 +3,34 @@ package request
import (
"context"
"fmt"
"strconv"
"strings"
"k8s.io/apiserver/pkg/endpoints/request"
"github.com/grafana/authlib/claims"
"github.com/grafana/grafana/pkg/apimachinery/identity"
"github.com/grafana/grafana/pkg/setting"
)
type NamespaceInfo struct {
// OrgID defined in namespace (1 when using stack ids)
OrgID int64
// The cloud stack ID (must match the value in cfg.Settings)
StackID string
// The original namespace string regardless the input
Value string
}
// NamespaceMapper converts an orgID into a namespace
type NamespaceMapper = func(orgId int64) string
type NamespaceMapper = claims.NamespaceFormatter
// GetNamespaceMapper returns a function that will convert orgIds into a consistent namespace
func GetNamespaceMapper(cfg *setting.Cfg) NamespaceMapper {
if cfg != nil && cfg.StackID != "" {
//val := claims.CloudNamespaceFormatter(cfg.Sta)
return func(orgId int64) string { return "stack-" + cfg.StackID }
}
return func(orgId int64) string {
if orgId == 1 {
return "default"
}
return fmt.Sprintf("org-%d", orgId)
}
return claims.OrgNamespaceFormatter
}
func NamespaceInfoFrom(ctx context.Context, requireOrgID bool) (NamespaceInfo, error) {
info, err := ParseNamespace(request.NamespaceValue(ctx))
func NamespaceInfoFrom(ctx context.Context, requireOrgID bool) (claims.NamespaceInfo, error) {
info, err := claims.ParseNamespace(request.NamespaceValue(ctx))
if err == nil && requireOrgID && info.OrgID < 1 {
return info, fmt.Errorf("expected valid orgId in namespace")
}
return info, err
}
func ParseNamespace(ns string) (NamespaceInfo, error) {
info := NamespaceInfo{Value: ns, OrgID: -1}
if ns == "default" {
info.OrgID = 1
return info, nil
}
if strings.HasPrefix(ns, "org-") {
id, err := strconv.Atoi(ns[4:])
if id < 1 {
return info, fmt.Errorf("invalid org id")
}
if id == 1 {
return info, fmt.Errorf("use default rather than org-1")
}
info.OrgID = int64(id)
return info, err
}
if strings.HasPrefix(ns, "stack-") {
stackIDStr := ns[6:]
stackID, err := strconv.Atoi(stackIDStr)
if err != nil || stackID < 1 {
return info, fmt.Errorf("invalid stack id")
}
info.StackID = stackIDStr
info.OrgID = 1
return info, nil
}
return info, nil
}
func OrgIDForList(ctx context.Context) (int64, error) {
ns := request.NamespaceValue(ctx)
if ns == "" {
@@ -88,6 +40,6 @@ func OrgIDForList(ctx context.Context) (int64, error) {
}
return -1, err
}
info, err := ParseNamespace(ns)
info, err := claims.ParseNamespace(ns)
return info.OrgID, err
}
@@ -9,134 +9,6 @@ import (
"github.com/grafana/grafana/pkg/setting"
)
func TestParseNamespace(t *testing.T) {
tests := []struct {
name string
namespace string
expected request.NamespaceInfo
expectErr bool
}{
{
name: "empty namespace",
expected: request.NamespaceInfo{
OrgID: -1,
},
},
{
name: "incorrect number of parts",
namespace: "org-123-a",
expectErr: true,
expected: request.NamespaceInfo{
OrgID: -1,
},
},
{
name: "org id not a number",
namespace: "org-invalid",
expectErr: true,
expected: request.NamespaceInfo{
OrgID: -1,
},
},
{
name: "valid org id",
namespace: "org-123",
expected: request.NamespaceInfo{
OrgID: 123,
},
},
{
name: "org should not be 1 in the namespace",
namespace: "org-1",
expectErr: true,
expected: request.NamespaceInfo{
OrgID: -1,
},
},
{
name: "can not be negative",
namespace: "org--5",
expectErr: true,
expected: request.NamespaceInfo{
OrgID: -1,
},
},
{
name: "can not be zero",
namespace: "org-0",
expectErr: true,
expected: request.NamespaceInfo{
OrgID: -1,
},
},
{
name: "default is org 1",
namespace: "default",
expected: request.NamespaceInfo{
OrgID: 1,
},
},
{
name: "invalid stack id (must be an int)",
expectErr: true,
namespace: "stack-abcdef",
expected: request.NamespaceInfo{
OrgID: -1,
},
},
{
name: "invalid stack id (must be provided)",
namespace: "stack-",
expectErr: true,
expected: request.NamespaceInfo{
OrgID: -1,
},
},
{
name: "invalid stack id (cannot be 0)",
namespace: "stack-0",
expectErr: true,
expected: request.NamespaceInfo{
OrgID: -1,
},
},
{
name: "valid stack",
namespace: "stack-1",
expected: request.NamespaceInfo{
OrgID: 1,
StackID: "1",
},
},
{
name: "other namespace",
namespace: "anything",
expected: request.NamespaceInfo{
OrgID: -1,
Value: "anything",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
info, err := request.ParseNamespace(tt.namespace)
if tt.expectErr != (err != nil) {
t.Errorf("ParseNamespace() returned %+v, expected an error", info)
}
if info.OrgID != tt.expected.OrgID {
t.Errorf("ParseNamespace() [OrgID] returned %d, expected %d", info.OrgID, tt.expected.OrgID)
}
if info.StackID != tt.expected.StackID {
t.Errorf("ParseNamespace() [StackID] returned %s, expected %s", info.StackID, tt.expected.StackID)
}
if info.Value != tt.namespace {
t.Errorf("ParseNamespace() [Value] returned %s, expected %s", info.Value, tt.namespace)
}
})
}
}
func TestNamespaceMapper(t *testing.T) {
tests := []struct {
name string