add new function

Signed-off-by: Christian Richter <c.richter@opencloud.eu>
This commit is contained in:
Christian Richter
2025-05-14 13:40:35 +02:00
committed by Florian Schade
parent 9e7f4487ad
commit 6e4cbf2230
4 changed files with 56 additions and 27 deletions
+40 -20
View File
@@ -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: /<scope>/*/<namespace>/*
// e.g. /user/<uid>/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
}
+4 -4
View File
@@ -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"`
}
@@ -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
+3 -3
View File
@@ -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,