From 6e4cbf22301638051bd0b649b18da4ed622c976e Mon Sep 17 00:00:00 2001 From: Christian Richter Date: Wed, 14 May 2025 13:40:35 +0200 Subject: [PATCH] add new function Signed-off-by: Christian Richter --- .../systemstorageclient.go | 60 ++++++++++++------- services/graph/pkg/config/config.go | 8 +-- .../pkg/config/defaults/defaultconfig.go | 9 +++ services/graph/pkg/service/v0/photo.go | 6 +- 4 files changed, 56 insertions(+), 27 deletions(-) diff --git a/pkg/systemstorageclient/systemstorageclient.go b/pkg/systemstorageclient/systemstorageclient.go index 87c9e6f254..99b063e02d 100644 --- a/pkg/systemstorageclient/systemstorageclient.go +++ b/pkg/systemstorageclient/systemstorageclient.go @@ -2,61 +2,81 @@ package systemstorageclient import ( "context" - "fmt" - "github.com/opencloud-eu/opencloud/pkg/log" + "path" + "sync" + "github.com/opencloud-eu/opencloud/pkg/log" "github.com/opencloud-eu/reva/v2/pkg/storage/utils/metadata" ) +var ( + managerName = "systemdata" +) + type SystemDataStorageClient struct { mds metadata.Storage + l *sync.Mutex } -func (s SystemDataStorageClient) SimpleDownload(ctx context.Context, userID, identifier string) ([]byte, error) { +func (s *SystemDataStorageClient) SimpleDownload(ctx context.Context, userID, identifier string) ([]byte, error) { //TODO implement me panic("implement me") } -func (s SystemDataStorageClient) SimpleUpload(ctx context.Context, userID, identifier string, content []byte) error { - s.mds.SimpleUpload(ctx, fmt.Sprintf("%s/%s", userID, identifier), content) - return nil +func (s *SystemDataStorageClient) SimpleUpload(ctx context.Context, userID, identifier string, content []byte) error { + return s.mds.SimpleUpload(ctx, path.Join(userID, identifier), content) } -func (s SystemDataStorageClient) Delete(ctx context.Context, userID, identifier string) error { +func (s *SystemDataStorageClient) Delete(ctx context.Context, userID, identifier string) error { //TODO implement me panic("implement me") } -func (s SystemDataStorageClient) ReadDir(ctx context.Context, userID, identifier string) ([]string, error) { +func (s *SystemDataStorageClient) ReadDir(ctx context.Context, userID, identifier string) ([]string, error) { //TODO implement me panic("implement me") } -func (s SystemDataStorageClient) MakeDirIfNotExist(ctx context.Context, userID, identifier string) error { +func (s *SystemDataStorageClient) MakeDirIfNotExist(ctx context.Context, userID, identifier string) error { //TODO implement me panic("implement me") } -func (s SystemDataStorageClient) Init(ctx context.Context, userID, identifier string) error { - //TODO implement me - panic("implement me") +// New initialize the store once, later calls are noops +func (s *SystemDataStorageClient) New(ctx context.Context, + logger *log.Logger, + scope, namespace string, + gatewayAddress, storageAddress, systemUserID, systemUserIDP, systemUserAPIKey string, +) { + if s.mds != nil { + return + } + + s.l.Lock() + defer s.l.Unlock() + + if s.mds != nil { + return + } + + mds, err := metadata.NewCS3Storage(gatewayAddress, storageAddress, systemUserID, systemUserIDP, systemUserAPIKey) + if err != nil { + logger.Fatal().Err(err).Msg("could not create profile storage client") + } + s.mds = mds } // NewProfileStorageClient creates a new ProfileStorageClient func NewSystemStorageClient(scope, nameSpace string, logger *log.Logger, - gatewayAddress, storageAddress, systemUserID, systemUserIDP, systemUserAPIKey string) SystemDataStorageClient { + gatewayAddress, storageAddress, systemUserID, systemUserIDP, systemUserAPIKey string) *SystemDataStorageClient { // scope: the scope the data should be persistet in (e.g. user) // namespace: the namespace the data should be persistet in (e.g. profilephoto) // results in the following path: //*//* + // e.g. /user//profilephoto/profilephoto.jpg - sdsci := SystemDataStorageClient{} - logger.Debug().Msg("NewSystemStorageClient called") - sdsc, err := metadata.NewCS3Storage(gatewayAddress, storageAddress, systemUserID, systemUserIDP, systemUserAPIKey) - if err != nil { - logger.Fatal().Err(err).Msg("could not create profile storage client") - } - sdsci.mds = sdsc + sdsci := &SystemDataStorageClient{} + sdsci.New(context.TODO(), logger, scope, nameSpace, gatewayAddress, storageAddress, systemUserID, systemUserIDP, systemUserAPIKey) return sdsci } diff --git a/services/graph/pkg/config/config.go b/services/graph/pkg/config/config.go index ce5d1a16d7..3ae85a571c 100644 --- a/services/graph/pkg/config/config.go +++ b/services/graph/pkg/config/config.go @@ -158,11 +158,11 @@ type ServiceAccount struct { // SystemStorageClient configures the metadata store to use type SystemStorageClient struct { - GatewayAddress string `yaml:"gateway_addr" env:"SETTINGS_STORAGE_GATEWAY_GRPC_ADDR;STORAGE_GATEWAY_GRPC_ADDR" desc:"GRPC address of the STORAGE-SYSTEM service." introductionVersion:"1.0.0"` - StorageAddress string `yaml:"storage_addr" env:"SETTINGS_STORAGE_GRPC_ADDR;STORAGE_GRPC_ADDR" desc:"GRPC address of the STORAGE-SYSTEM service." introductionVersion:"1.0.0"` + GatewayAddress string `yaml:"gateway_addr" env:"GRAPH_STORAGE_GATEWAY_GRPC_ADDR;STORAGE_GATEWAY_GRPC_ADDR" desc:"GRPC address of the STORAGE-SYSTEM service." introductionVersion:"1.0.0"` + StorageAddress string `yaml:"storage_addr" env:"GRAPH_STORAGE_GRPC_ADDR;STORAGE_GRPC_ADDR" desc:"GRPC address of the STORAGE-SYSTEM service." introductionVersion:"1.0.0"` - SystemUserID string `yaml:"system_user_id" env:"OC_SYSTEM_USER_ID;SETTINGS_SYSTEM_USER_ID" desc:"ID of the OpenCloud STORAGE-SYSTEM system user. Admins need to set the ID for the STORAGE-SYSTEM system user in this config option which is then used to reference the user. Any reasonable long string is possible, preferably this would be an UUIDv4 format." introductionVersion:"1.0.0"` - SystemUserIDP string `yaml:"system_user_idp" env:"OC_SYSTEM_USER_IDP;SETTINGS_SYSTEM_USER_IDP" desc:"IDP of the OpenCloud STORAGE-SYSTEM system user." introductionVersion:"1.0.0"` + SystemUserID string `yaml:"system_user_id" env:"OC_SYSTEM_USER_ID;GRAPH_SYSTEM_USER_ID" desc:"ID of the OpenCloud STORAGE-SYSTEM system user. Admins need to set the ID for the STORAGE-SYSTEM system user in this config option which is then used to reference the user. Any reasonable long string is possible, preferably this would be an UUIDv4 format." introductionVersion:"1.0.0"` + SystemUserIDP string `yaml:"system_user_idp" env:"OC_SYSTEM_USER_IDP;GRAPH_SYSTEM_USER_IDP" desc:"IDP of the OpenCloud STORAGE-SYSTEM system user." introductionVersion:"1.0.0"` SystemUserAPIKey string `yaml:"system_user_api_key" env:"OC_SYSTEM_USER_API_KEY" desc:"API key for the STORAGE-SYSTEM system user." introductionVersion:"1.0.0"` Cache *Cache `yaml:"cache"` } diff --git a/services/graph/pkg/config/defaults/defaultconfig.go b/services/graph/pkg/config/defaults/defaultconfig.go index 5c926c6ffc..bc30304e28 100644 --- a/services/graph/pkg/config/defaults/defaultconfig.go +++ b/services/graph/pkg/config/defaults/defaultconfig.go @@ -202,6 +202,15 @@ func EnsureDefaults(cfg *config.Config) { cfg.UnifiedRoles.AvailableRoles = append(cfg.UnifiedRoles.AvailableRoles, definition.GetId()) } } + + if cfg.SystemStorageClient.SystemUserAPIKey == "" && cfg.Commons != nil && cfg.Commons.SystemUserAPIKey != "" { + cfg.SystemStorageClient.SystemUserAPIKey = cfg.Commons.SystemUserAPIKey + } + + if cfg.SystemStorageClient.SystemUserID == "" && cfg.Commons != nil && cfg.Commons.SystemUserID != "" { + cfg.SystemStorageClient.SystemUserID = cfg.Commons.SystemUserID + } + } // Sanitize sanitized the configuration diff --git a/services/graph/pkg/service/v0/photo.go b/services/graph/pkg/service/v0/photo.go index cbb814a972..0c46537f3b 100644 --- a/services/graph/pkg/service/v0/photo.go +++ b/services/graph/pkg/service/v0/photo.go @@ -91,7 +91,7 @@ func (g Graph) UpdatePhoto(w http.ResponseWriter, r *http.Request) { func (g Graph) updatePhoto(w http.ResponseWriter, r *http.Request, u *userpb.UserId) { logger := g.logger.SubloggerWithRequestID(r.Context()) logger.Debug().Msg("UpdatePhoto called") - g.getSystemStorageClient() + client := g.getSystemStorageClient() content, err := io.ReadAll(r.Body) if err != nil { logger.Debug().Err(err).Msg("could not read body") @@ -103,7 +103,7 @@ func (g Graph) updatePhoto(w http.ResponseWriter, r *http.Request, u *userpb.Use errorcode.InvalidRequest.Render(w, r, http.StatusBadRequest, "empty body") return } - err = g.sdsc.SimpleUpload(r.Context(), u.GetOpaqueId(), identifier, content) + err = client.SimpleUpload(r.Context(), u.GetOpaqueId(), identifier, content) if err != nil { logger.Debug().Err(err).Msg("could not upload photo") errorcode.GeneralException.Render(w, r, http.StatusInternalServerError, "could not upload photo") @@ -115,7 +115,7 @@ func (g Graph) updatePhoto(w http.ResponseWriter, r *http.Request, u *userpb.Use func (g Graph) getSystemStorageClient() systemstorageclient.SystemDataStorageClient { // TODO: this needs a check if the client is already initialized and if not, initialize it - g.sdsc = systemstorageclient.NewSystemStorageClient( + g.sdsc = *systemstorageclient.NewSystemStorageClient( scope, namespace, g.logger,