From 65fc7cf004a863fe3cd1c112d16522797f336886 Mon Sep 17 00:00:00 2001 From: "Arati R." <33031346+suntala@users.noreply.github.com> Date: Wed, 16 Oct 2024 10:44:09 +0200 Subject: [PATCH] K8s/Folders: Fix mode 2 folder creation (#94796) * Use user UID as identifier instead of ID * Remove malformed error --- pkg/api/folder.go | 33 +++++++++--------------- pkg/registry/apis/folders/conversions.go | 8 +++--- pkg/services/folder/folderimpl/folder.go | 6 +++++ pkg/services/folder/model.go | 2 ++ 4 files changed, 24 insertions(+), 25 deletions(-) diff --git a/pkg/api/folder.go b/pkg/api/folder.go index c9f2963f120..2dd2fdc234d 100644 --- a/pkg/api/folder.go +++ b/pkg/api/folder.go @@ -3,7 +3,6 @@ package api import ( "context" "errors" - "fmt" "net/http" "strconv" "strings" @@ -807,16 +806,13 @@ func (fk8s *folderK8sHandler) newToFolderDto(c *contextmodel.ReqContext, item un return dtos.Folder{}, err } - toID := func(rawIdentifier string) (int64, error) { + // #TODO Is there a preexisting function we can use instead, something along the lines of UserIdentifier? + toUID := func(rawIdentifier string) string { parts := strings.Split(rawIdentifier, ":") if len(parts) < 2 { - return 0, fmt.Errorf("invalid user identifier") + return "" } - userID, err := strconv.ParseInt(parts[1], 10, 64) - if err != nil { - return 0, fmt.Errorf("faild to parse user identifier") - } - return userID, nil + return parts[1] } toDTO := func(fold *folder.Folder, checkCanView bool) (dtos.Folder, error) { @@ -835,18 +831,10 @@ func (fk8s *folderK8sHandler) newToFolderDto(c *contextmodel.ReqContext, item un // #TODO refactor the various conversions of the folder so that we either set created by in folder.Folder or // we convert from unstructured to folder DTO without an intermediate conversion to folder.Folder if len(fDTO.CreatedBy) > 0 { - id, err := toID(fDTO.CreatedBy) - if err != nil { - return dtos.Folder{}, err - } - creator = fk8s.getUserLogin(ctx, id) + creator = fk8s.getUserLogin(ctx, toUID(fDTO.CreatedBy), orgID) } if len(fDTO.UpdatedBy) > 0 { - id, err := toID(fDTO.UpdatedBy) - if err != nil { - return dtos.Folder{}, err - } - updater = fk8s.getUserLogin(ctx, id) + updater = fk8s.getUserLogin(ctx, toUID(fDTO.UpdatedBy), orgID) } acMetadata, _ := fk8s.getFolderACMetadata(c, fold) @@ -917,12 +905,15 @@ func (fk8s *folderK8sHandler) newToFolderDto(c *contextmodel.ReqContext, item un return folderDTO, nil } -func (fk8s *folderK8sHandler) getUserLogin(ctx context.Context, userID int64) string { +func (fk8s *folderK8sHandler) getUserLogin(ctx context.Context, userUID string, orgID int64) string { ctx, span := tracer.Start(ctx, "api.getUserLogin") defer span.End() - query := user.GetUserByIDQuery{ID: userID} - user, err := fk8s.userService.GetByID(ctx, &query) + query := user.GetUserByUIDQuery{ + UID: userUID, + OrgID: orgID, + } + user, err := fk8s.userService.GetByUID(ctx, &query) if err != nil { return anonString } diff --git a/pkg/registry/apis/folders/conversions.go b/pkg/registry/apis/folders/conversions.go index 31e8c051f2b..b9a87f157fc 100644 --- a/pkg/registry/apis/folders/conversions.go +++ b/pkg/registry/apis/folders/conversions.go @@ -177,11 +177,11 @@ func convertToK8sResource(v *folder.Folder, namespacer request.NamespaceMapper) // #TODO: turns out these get overwritten by Unified Storage (see pkg/storage/unified/apistore/prepare.go) // We're going to have to align with that. For now we do need the user ID because the folder type stores it // as the only user identifier - if v.CreatedBy > 0 { - meta.SetCreatedBy(fmt.Sprintf("user:%d", v.CreatedBy)) + if v.CreatedByUID != "" { + meta.SetCreatedBy(v.UpdatedByUID) } - if v.UpdatedBy > 0 { - meta.SetUpdatedBy(fmt.Sprintf("user:%d", v.UpdatedBy)) + if v.UpdatedByUID != "" { + meta.SetUpdatedBy(v.UpdatedByUID) } if v.ParentUID != "" { meta.SetFolder(v.ParentUID) diff --git a/pkg/services/folder/folderimpl/folder.go b/pkg/services/folder/folderimpl/folder.go index a1608f85e71..091794cd00c 100644 --- a/pkg/services/folder/folderimpl/folder.go +++ b/pkg/services/folder/folderimpl/folder.go @@ -674,6 +674,12 @@ func (s *Service) Create(ctx context.Context, cmd *folder.CreateFolderCommand) ( } if s.features.IsEnabled(ctx, featuremgmt.FlagKubernetesFolders) { + // #TODO is some kind of intermediate conversion required as is the case with user id where + // it gets parsed using UserIdentifier(). Also is there some kind of validation taking place as + // part of the parsing? + f.CreatedByUID = user.GetUID() + f.UpdatedByUID = user.GetUID() + if f.ParentUID == "" { return f, nil } diff --git a/pkg/services/folder/model.go b/pkg/services/folder/model.go index 378127516c0..ccadb46f568 100644 --- a/pkg/services/folder/model.go +++ b/pkg/services/folder/model.go @@ -48,6 +48,8 @@ type Folder struct { URL string UpdatedBy int64 CreatedBy int64 + UpdatedByUID string + CreatedByUID string HasACL bool Fullpath string `xorm:"fullpath"` FullpathUIDs string `xorm:"fullpath_uids"`