From baaaa774b31637e23798cf25e25b29b79ef8a2ea Mon Sep 17 00:00:00 2001 From: jkoberg Date: Mon, 8 Aug 2022 10:27:56 +0200 Subject: [PATCH] introduce max and default quota --- services/graph/pkg/config/config.go | 4 +- services/graph/pkg/service/v0/drives.go | 68 +++++++++++++++++--- services/graph/pkg/service/v0/graph.go | 3 + services/graph/pkg/service/v0/service.go | 7 ++ services/proxy/pkg/command/server.go | 1 + services/proxy/pkg/config/config.go | 1 + services/proxy/pkg/middleware/create_home.go | 7 +- services/proxy/pkg/middleware/options.go | 9 +++ 8 files changed, 89 insertions(+), 11 deletions(-) diff --git a/services/graph/pkg/config/config.go b/services/graph/pkg/config/config.go index 6fea8abc129..7c32df4044c 100644 --- a/services/graph/pkg/config/config.go +++ b/services/graph/pkg/config/config.go @@ -31,7 +31,9 @@ type Config struct { type Spaces struct { WebDavBase string `yaml:"webdav_base" env:"OCIS_URL;GRAPH_SPACES_WEBDAV_BASE" desc:"The public facing URL of WebDAV."` WebDavPath string `yaml:"webdav_path" env:"GRAPH_SPACES_WEBDAV_PATH" desc:"The WebDAV subpath for spaces."` - DefaultQuota string `yaml:"default_quota" env:"GRAPH_SPACES_DEFAULT_QUOTA" desc:"The default quota in bytes."` + DefaultQuota string `yaml:"default_quota" env:"GRAPH_SPACES_DEFAULT_QUOTA" desc:"The default quota for project spaces in bytes."` + MaxQuotaPersonal string `yaml:"max_quota_personal" env:"GRAPH_SPACES_MAX_QUOTA_PERSONAL" desc:"The maximum quota for a personal space in bytes."` + MaxQuotaProject string `yaml:"max_quota_project" env:"GRAPH_SPACES_MAX_QUOTA_PROJECT" desc:"The maximum quota for a project space in bytes."` ExtendedSpacePropertiesCacheTTL int `yaml:"extended_space_properties_cache_ttl" env:"GRAPH_SPACES_EXTENDED_SPACE_PROPERTIES_CACHE_TTL" desc:"Max TTL in seconds for the spaces property cache."` } diff --git a/services/graph/pkg/service/v0/drives.go b/services/graph/pkg/service/v0/drives.go index dfe702240a7..a85c06ef66c 100644 --- a/services/graph/pkg/service/v0/drives.go +++ b/services/graph/pkg/service/v0/drives.go @@ -16,6 +16,7 @@ import ( gateway "github.com/cs3org/go-cs3apis/cs3/gateway/v1beta1" userv1beta1 "github.com/cs3org/go-cs3apis/cs3/identity/user/v1beta1" cs3rpc "github.com/cs3org/go-cs3apis/cs3/rpc/v1beta1" + provider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1" storageprovider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1" types "github.com/cs3org/go-cs3apis/cs3/types/v1beta1" ctxpkg "github.com/cs3org/reva/v2/pkg/ctx" @@ -210,22 +211,32 @@ func (g Graph) CreateDrive(w http.ResponseWriter, r *http.Request) { return } - var driveType string + var ( + driveType string + maxQuota uint64 + ) if drive.DriveType != nil { driveType = *drive.DriveType } switch driveType { case "", "project": driveType = "project" + maxQuota = g.maxQuotaProject default: errorcode.GeneralException.Render(w, r, http.StatusBadRequest, fmt.Sprintf("drives of type %s cannot be created via this api", driveType)) return } + quota := getQuota(drive.Quota, g.defaultQuota) + if maxQuota != 0 && maxQuota < quota.QuotaMaxBytes { + errorcode.GeneralException.Render(w, r, http.StatusBadRequest, fmt.Sprintf("exceeding maximum drive quota. Max is %d", maxQuota)) + return + } + csr := storageprovider.CreateStorageSpaceRequest{ Type: driveType, Name: spaceName, - Quota: getQuota(drive.Quota, g.config.Spaces.DefaultQuota), + Quota: quota, } if drive.Description != nil { @@ -350,8 +361,50 @@ func (g Graph) UpdateDrive(w http.ResponseWriter, r *http.Request) { errorcode.GeneralException.Render(w, r, http.StatusUnauthorized, "user is not allowed to set the space quota") return } + + maxQuota := g.maxQuotaPersonal + if g.maxQuotaPersonal != g.maxQuotaProject { + // different quotas set - we need to find out if we are patching a personal or a project space + sres, err := client.ListStorageSpaces(r.Context(), &provider.ListStorageSpacesRequest{ + Filters: []*provider.ListStorageSpacesRequest_Filter{ + { + Type: provider.ListStorageSpacesRequest_Filter_TYPE_ID, + Term: &provider.ListStorageSpacesRequest_Filter_Id{ + Id: &provider.StorageSpaceId{OpaqueId: driveID}, + }, + }, + }, + }) + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + return + } + + if len(sres.StorageSpaces) != 1 { + errorcode.GeneralException.Render(w, r, http.StatusBadRequest, "drive to patch does not exist") + return + } + + switch sres.StorageSpaces[0].SpaceType { + case "personal": + maxQuota = g.maxQuotaPersonal + case "project": + maxQuota = g.maxQuotaProject + default: + errorcode.GeneralException.Render(w, r, http.StatusBadRequest, fmt.Sprintf("can't patch drive of type %s", sres.StorageSpaces[0].SpaceType)) + return + + } + } + + quota := uint64(*drive.Quota.Total) + if maxQuota != 0 && maxQuota < quota { + errorcode.GeneralException.Render(w, r, http.StatusBadRequest, fmt.Sprintf("exceeding maximum drive quota. Max is %d", maxQuota)) + return + } + updateSpaceRequest.StorageSpace.Quota = &storageprovider.Quota{ - QuotaMaxBytes: uint64(*drive.Quota.Total), + QuotaMaxBytes: quota, } } @@ -677,18 +730,15 @@ func calculateQuotaState(total int64, used int64) (state string) { } } -func getQuota(quota *libregraph.Quota, defaultQuota string) *storageprovider.Quota { +func getQuota(quota *libregraph.Quota, defaultQuota uint64) *storageprovider.Quota { switch { case quota != nil && quota.Total != nil: if q := *quota.Total; q >= 0 { return &storageprovider.Quota{QuotaMaxBytes: uint64(q)} } fallthrough - case defaultQuota != "": - if q, err := strconv.ParseInt(defaultQuota, 10, 64); err == nil && q >= 0 { - return &storageprovider.Quota{QuotaMaxBytes: uint64(q)} - } - fallthrough + case defaultQuota != 0: + return &storageprovider.Quota{QuotaMaxBytes: defaultQuota} default: return nil } diff --git a/services/graph/pkg/service/v0/graph.go b/services/graph/pkg/service/v0/graph.go index 2f1c98d8dc8..bd751cae719 100644 --- a/services/graph/pkg/service/v0/graph.go +++ b/services/graph/pkg/service/v0/graph.go @@ -78,6 +78,9 @@ type Graph struct { roleService settingssvc.RoleService spacePropertiesCache *ttlcache.Cache eventsPublisher events.Publisher + defaultQuota uint64 + maxQuotaPersonal uint64 + maxQuotaProject uint64 } // ServeHTTP implements the Service interface. diff --git a/services/graph/pkg/service/v0/service.go b/services/graph/pkg/service/v0/service.go index 4f6799339af..80573f59a70 100644 --- a/services/graph/pkg/service/v0/service.go +++ b/services/graph/pkg/service/v0/service.go @@ -57,12 +57,19 @@ func NewService(opts ...Option) Service { m := chi.NewMux() m.Use(options.Middleware...) + defaultQuota, _ := strconv.ParseUint(options.Config.Spaces.DefaultQuota, 10, 64) + personalQuota, _ := strconv.ParseUint(options.Config.Spaces.MaxQuotaPersonal, 10, 64) + projectQuota, _ := strconv.ParseUint(options.Config.Spaces.MaxQuotaProject, 10, 64) + svc := Graph{ config: options.Config, mux: m, logger: &options.Logger, spacePropertiesCache: ttlcache.NewCache(), eventsPublisher: options.EventsPublisher, + defaultQuota: defaultQuota, + maxQuotaPersonal: personalQuota, + maxQuotaProject: projectQuota, } if options.GatewayClient == nil { var err error diff --git a/services/proxy/pkg/command/server.go b/services/proxy/pkg/command/server.go index 5ce0d1373a7..1b037c8b2d4 100644 --- a/services/proxy/pkg/command/server.go +++ b/services/proxy/pkg/command/server.go @@ -227,6 +227,7 @@ func loadMiddlewares(ctx context.Context, logger log.Logger, cfg *config.Config) middleware.Logger(logger), middleware.TokenManagerConfig(*cfg.TokenManager), middleware.RevaGatewayClient(revaClient), + middleware.DefaultSpaceQuota(cfg.DefaultSpaceQuota), ), middleware.PublicShareAuth( middleware.Logger(logger), diff --git a/services/proxy/pkg/config/config.go b/services/proxy/pkg/config/config.go index 2b9118deabc..8ce80c4f7a8 100644 --- a/services/proxy/pkg/config/config.go +++ b/services/proxy/pkg/config/config.go @@ -33,6 +33,7 @@ type Config struct { EnableBasicAuth bool `yaml:"enable_basic_auth" env:"PROXY_ENABLE_BASIC_AUTH" desc:"Set this to true to enable 'basic' (username/password) authentication."` InsecureBackends bool `yaml:"insecure_backends" env:"PROXY_INSECURE_BACKENDS" desc:"Disable TLS certificate validation for all HTTP backend connections."` AuthMiddleware AuthMiddleware `yaml:"auth_middleware"` + DefaultSpaceQuota string `yaml:"default_space_quota" env:"OCIS_DEFAULT_SPACE_QUOTA" desc:"The default quota for personal spaces in bytes"` Context context.Context `yaml:"-" json:"-"` } diff --git a/services/proxy/pkg/middleware/create_home.go b/services/proxy/pkg/middleware/create_home.go index 5fa30a1a3e2..f254925dab8 100644 --- a/services/proxy/pkg/middleware/create_home.go +++ b/services/proxy/pkg/middleware/create_home.go @@ -8,6 +8,7 @@ import ( provider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1" revactx "github.com/cs3org/reva/v2/pkg/ctx" "github.com/cs3org/reva/v2/pkg/rgrpc/status" + "github.com/cs3org/reva/v2/pkg/utils" "github.com/owncloud/ocis/v2/ocis-pkg/log" "google.golang.org/grpc/metadata" ) @@ -22,6 +23,7 @@ func CreateHome(optionSetters ...Option) func(next http.Handler) http.Handler { next: next, logger: logger, revaGatewayClient: options.RevaGatewayClient, + defaultQuota: options.DefaultSpaceQuota, } } } @@ -30,6 +32,7 @@ type createHome struct { next http.Handler logger log.Logger revaGatewayClient gateway.GatewayAPIClient + defaultQuota string } func (m createHome) ServeHTTP(w http.ResponseWriter, req *http.Request) { @@ -45,8 +48,10 @@ func (m createHome) ServeHTTP(w http.ResponseWriter, req *http.Request) { ctx := metadata.AppendToOutgoingContext(req.Context(), revactx.TokenHeader, token) createHomeReq := &provider.CreateHomeRequest{} + if m.defaultQuota != "" { + createHomeReq.Opaque = utils.AppendPlainToOpaque(nil, "quota", m.defaultQuota) + } createHomeRes, err := m.revaGatewayClient.CreateHome(ctx, createHomeReq) - if err != nil { m.logger.Err(err).Msg("error calling CreateHome") } else if createHomeRes.Status.Code != rpc.Code_CODE_OK { diff --git a/services/proxy/pkg/middleware/options.go b/services/proxy/pkg/middleware/options.go index 2d07b2b772f..ed02dc4aca5 100644 --- a/services/proxy/pkg/middleware/options.go +++ b/services/proxy/pkg/middleware/options.go @@ -60,6 +60,8 @@ type Options struct { AccessTokenVerifyMethod string // JWKS sets the options for fetching the JWKS from the IDP JWKS config.JWKS + // DefaultSpaceQuota is the default space quota for personal drives + DefaultSpaceQuota string } // newOptions initializes the available default options. @@ -212,3 +214,10 @@ func JWKSOptions(jo config.JWKS) Option { o.JWKS = jo } } + +// DefaultSpaceQuota ssets the default space quota for personal spaces +func DefaultSpaceQuota(q string) Option { + return func(o *Options) { + o.DefaultSpaceQuota = q + } +}