K8s/Folders: Fix mode 2 folder creation (#94796)

* Use user UID as identifier instead of ID
* Remove malformed error
This commit is contained in:
Arati R. 2024-10-16 10:44:09 +02:00 committed by GitHub
parent 3e9dd1239a
commit 65fc7cf004
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 24 additions and 25 deletions

View File

@ -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
}

View File

@ -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)

View File

@ -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
}

View File

@ -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"`