diff --git a/pkg/storage/unified/apistore/prepare.go b/pkg/storage/unified/apistore/prepare.go index b371444c66a..e4bb5ed7d18 100644 --- a/pkg/storage/unified/apistore/prepare.go +++ b/pkg/storage/unified/apistore/prepare.go @@ -3,17 +3,18 @@ package apistore import ( "bytes" "context" + "errors" "fmt" "math" "time" "github.com/google/uuid" + authtypes "github.com/grafana/authlib/types" "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/types" "k8s.io/apiserver/pkg/storage" "k8s.io/klog/v2" - "github.com/grafana/grafana/pkg/apimachinery/identity" "github.com/grafana/grafana/pkg/apimachinery/utils" "github.com/grafana/grafana/pkg/storage/unified/resource" ) @@ -37,9 +38,9 @@ func formatBytes(numBytes int) string { // Called on create func (s *Storage) prepareObjectForStorage(ctx context.Context, newObject runtime.Object) ([]byte, error) { - user, err := identity.GetRequester(ctx) - if err != nil { - return nil, err + info, ok := authtypes.AuthInfoFrom(ctx) + if !ok { + return nil, errors.New("missing auth info") } obj, err := utils.MetaAccessor(newObject) @@ -80,7 +81,7 @@ func (s *Storage) prepareObjectForStorage(ctx context.Context, newObject runtime obj.SetRepositoryInfo(repo) obj.SetUpdatedBy("") obj.SetUpdatedTimestamp(nil) - obj.SetCreatedBy(user.GetUID()) + obj.SetCreatedBy(info.GetUID()) var buf bytes.Buffer if err = s.codec.Encode(newObject, &buf); err != nil { @@ -91,9 +92,9 @@ func (s *Storage) prepareObjectForStorage(ctx context.Context, newObject runtime // Called on update func (s *Storage) prepareObjectForUpdate(ctx context.Context, updateObject runtime.Object, previousObject runtime.Object) ([]byte, error) { - user, err := identity.GetRequester(ctx) - if err != nil { - return nil, err + info, ok := authtypes.AuthInfoFrom(ctx) + if !ok { + return nil, errors.New("missing auth info") } obj, err := utils.MetaAccessor(updateObject) @@ -141,7 +142,7 @@ func (s *Storage) prepareObjectForUpdate(ctx context.Context, updateObject runti return nil, err } obj.SetRepositoryInfo(repo) - obj.SetUpdatedBy(user.GetUID()) + obj.SetUpdatedBy(info.GetUID()) obj.SetUpdatedTimestampMillis(time.Now().UnixMilli()) var buf bytes.Buffer diff --git a/pkg/storage/unified/apistore/prepare_test.go b/pkg/storage/unified/apistore/prepare_test.go index d24b31a5a91..18d87952615 100644 --- a/pkg/storage/unified/apistore/prepare_test.go +++ b/pkg/storage/unified/apistore/prepare_test.go @@ -6,10 +6,10 @@ import ( "time" "github.com/bwmarrin/snowflake" + authtypes "github.com/grafana/authlib/types" "github.com/grafana/grafana/pkg/apimachinery/identity" "github.com/grafana/grafana/pkg/apimachinery/utils" "github.com/grafana/grafana/pkg/apis/dashboard/v0alpha1" - "github.com/grafana/grafana/pkg/services/user" "github.com/stretchr/testify/require" "golang.org/x/exp/rand" "k8s.io/apimachinery/pkg/api/apitesting" @@ -32,12 +32,13 @@ func TestPrepareObjectForStorage(t *testing.T) { LargeObjectSupport: nil, }, } - ctx := identity.WithRequester(context.Background(), &user.SignedInUser{UserID: 1, UserUID: "user-uid"}) - t.Run("Error getting requester from context", func(t *testing.T) { + ctx := authtypes.WithAuthInfo(context.Background(), &identity.StaticRequester{UserID: 1, UserUID: "user-uid", Type: authtypes.TypeUser}) + + t.Run("Error getting auth info from context", func(t *testing.T) { _, err := s.prepareObjectForStorage(context.Background(), nil) require.Error(t, err) - require.Contains(t, err.Error(), "a Requester was not found in the context") + require.Contains(t, err.Error(), "missing auth info") }) t.Run("Error on missing name", func(t *testing.T) {