From 83d85226f0db7e4f29d1cf6e51f0ff9f41e520dc Mon Sep 17 00:00:00 2001 From: jkoberg Date: Fri, 2 Sep 2022 16:25:15 +0200 Subject: [PATCH 1/2] configuration for setting default (group) quotas Signed-off-by: jkoberg --- ocis-pkg/bytesize/bytesize.go | 90 ---------------- ocis-pkg/bytesize/bytesize_test.go | 106 ------------------- services/gateway/pkg/config/config.go | 3 + services/gateway/pkg/revaconfig/config.go | 2 + services/graph/pkg/service/v0/service.go | 2 +- services/proxy/pkg/middleware/create_home.go | 41 +++---- 6 files changed, 28 insertions(+), 216 deletions(-) delete mode 100644 ocis-pkg/bytesize/bytesize.go delete mode 100644 ocis-pkg/bytesize/bytesize_test.go diff --git a/ocis-pkg/bytesize/bytesize.go b/ocis-pkg/bytesize/bytesize.go deleted file mode 100644 index f13c979a79a..00000000000 --- a/ocis-pkg/bytesize/bytesize.go +++ /dev/null @@ -1,90 +0,0 @@ -package bytesize - -import ( - "fmt" - "strconv" - "strings" -) - -// ByteSize is the size in bytes -type ByteSize uint64 - -// List of available byte sizes -// NOTE: max is exabyte as we convert to uint64 -const ( - KB ByteSize = 1000 - MB ByteSize = 1000 * KB - GB ByteSize = 1000 * MB - TB ByteSize = 1000 * GB - PB ByteSize = 1000 * TB - EB ByteSize = 1000 * PB - - KiB ByteSize = 1024 - MiB ByteSize = 1024 * KiB - GiB ByteSize = 1024 * MiB - TiB ByteSize = 1024 * GiB - PiB ByteSize = 1024 * TiB - EiB ByteSize = 1024 * PiB -) - -// Parse parses a Bytesize from a string -func Parse(s string) (ByteSize, error) { - sanitized := strings.TrimSpace(s) - if !strings.HasSuffix(sanitized, "B") { - u, err := strconv.Atoi(sanitized) - return ByteSize(u), err - } - - var ( - value int - unit string - ) - - template := "%d%s" - _, err := fmt.Sscanf(sanitized, template, &value, &unit) - if err != nil { - return 0, err - } - - bytes := ByteSize(value) - switch unit { - case "KB": - bytes *= KB - case "MB": - bytes *= MB - case "GB": - bytes *= GB - case "TB": - bytes *= TB - case "PB": - bytes *= PB - case "EB": - bytes *= EB - case "KiB": - bytes *= KiB - case "MiB": - bytes *= MiB - case "GiB": - bytes *= GiB - case "TiB": - bytes *= TiB - case "PiB": - bytes *= PiB - case "EiB": - bytes *= EiB - default: - return 0, fmt.Errorf("unknown unit '%s'. Use common abbreviations such as KB, MiB, GB", unit) - } - - return bytes, nil -} - -// Bytes converts the ByteSize to an uint64 -func (b ByteSize) Bytes() uint64 { - return uint64(b) -} - -// String converts the ByteSize to a string -func (b ByteSize) String() string { - return strconv.Itoa(int(b)) -} diff --git a/ocis-pkg/bytesize/bytesize_test.go b/ocis-pkg/bytesize/bytesize_test.go deleted file mode 100644 index d17819a13cf..00000000000 --- a/ocis-pkg/bytesize/bytesize_test.go +++ /dev/null @@ -1,106 +0,0 @@ -package bytesize_test - -import ( - "fmt" - "testing" - - "github.com/owncloud/ocis/v2/ocis-pkg/bytesize" - "github.com/test-go/testify/require" -) - -func TestParseSpecial(t *testing.T) { - testCases := []struct { - Alias string - Input string - ExpectedOutput uint64 - ExpectError bool - }{ - { - Alias: "it assumes bytes", - Input: "100", - ExpectedOutput: 100, - }, - { - Alias: "it accepts a space between value and unit", - Input: "1 MB", - ExpectedOutput: 1000000, - }, - { - Alias: "it accepts also more spaces between value and unit", - Input: "1 MB", - ExpectedOutput: 1000000, - }, - { - Alias: "it ignores trailing and leading spaces", - Input: " 1MB ", - ExpectedOutput: 1000000, - }, - { - Alias: "it errors on unknown units", - Input: "1SB", - ExpectedOutput: 0, - ExpectError: true, - }, - { - Alias: "it multiplies correctly", - Input: "16MB", - ExpectedOutput: 16000000, - }, - { - Alias: "it errors when no value is given", - Input: "GB", - ExpectedOutput: 0, - ExpectError: true, - }, - { - Alias: "it errors when bad input is given", - Input: ",as!@@delta", - ExpectedOutput: 0, - ExpectError: true, - }, - { - Alias: "it errors when using floats", - Input: "1.024GB", - ExpectedOutput: 0, - ExpectError: true, - }, - } - - for _, tc := range testCases { - actual, err := bytesize.Parse(tc.Input) - if tc.ExpectError { - require.Error(t, err) - } else { - require.NoError(t, err) - } - - require.Equal(t, tc.ExpectError, err != nil, tc.Alias) - require.Equal(t, int(tc.ExpectedOutput), int(actual), tc.Alias) - } -} - -func TestParseHappy(t *testing.T) { - testCases := []struct { - Input string - Expected uint64 - }{ - {Input: "1", Expected: 1}, - {Input: "1KB", Expected: 1000}, - {Input: "1MB", Expected: 1000000}, - {Input: "1GB", Expected: 1000000000}, - {Input: "1TB", Expected: 1000000000000}, - {Input: "1PB", Expected: 1000000000000000}, - {Input: "1EB", Expected: 1000000000000000000}, - {Input: "1MiB", Expected: 1048576}, - {Input: "1GiB", Expected: 1073741824}, - {Input: "1TiB", Expected: 1099511627776}, - {Input: "1PiB", Expected: 1125899906842624}, - {Input: "1EiB", Expected: 1152921504606846976}, - } - - for _, tc := range testCases { - actual, err := bytesize.Parse(tc.Input) - require.NoError(t, err) - require.Equal(t, int(tc.Expected), int(actual), fmt.Sprintf("case %s", tc.Input)) - } -} diff --git a/services/gateway/pkg/config/config.go b/services/gateway/pkg/config/config.go index b9ac6b537c8..3e387ea4262 100644 --- a/services/gateway/pkg/config/config.go +++ b/services/gateway/pkg/config/config.go @@ -44,6 +44,9 @@ type Config struct { StorageRegistry StorageRegistry `yaml:"storage_registry"` //TODO: should we even support switching this? + DefaultPersonalQuota string `yaml:"default_personal_quota" env:"PERSONAL_SPACES_DEFAULT_QUOTA" desc:"The default quota for personal spaces in bytes. Usable common abbreviations: [KB, KiB, GB, GiB, TB, TiB, PB, PiB, EB, EiB], example: 1GB. 0 means unlimited and is the default."` + GroupQuotaFile string `yaml:"group_quota_file" env:"GROUP_QUOTA_FILE" desc:"The path to the file containing the group quotas. The file needs to be json in the form groupname:quota. Note: If set and the target is not json or the entries are not parseable (see default_personal_quota) the service will refuse to start."` + Supervised bool `yaml:"-"` Context context.Context `yaml:"-"` } diff --git a/services/gateway/pkg/revaconfig/config.go b/services/gateway/pkg/revaconfig/config.go index 3d6fa39b3c9..a16d15345b4 100644 --- a/services/gateway/pkg/revaconfig/config.go +++ b/services/gateway/pkg/revaconfig/config.go @@ -52,6 +52,8 @@ func GatewayConfigFromStruct(cfg *config.Config, logger log.Logger) map[string]i "transfer_shared_secret": cfg.TransferSecret, "transfer_expires": cfg.TransferExpires, "etag_cache_ttl": cfg.EtagCacheTTL, + "personal_quota_default": cfg.DefaultPersonalQuota, + "group_quota_file": cfg.GroupQuotaFile, }, "authregistry": map[string]interface{}{ "driver": "static", diff --git a/services/graph/pkg/service/v0/service.go b/services/graph/pkg/service/v0/service.go index ae99880e4c1..b5c71684621 100644 --- a/services/graph/pkg/service/v0/service.go +++ b/services/graph/pkg/service/v0/service.go @@ -9,11 +9,11 @@ import ( "time" "github.com/ReneKroon/ttlcache/v2" + "github.com/cs3org/reva/v2/pkg/bytesize" "github.com/cs3org/reva/v2/pkg/rgrpc/todo/pool" "github.com/go-chi/chi/v5" "github.com/go-chi/chi/v5/middleware" - "github.com/owncloud/ocis/v2/ocis-pkg/bytesize" ocisldap "github.com/owncloud/ocis/v2/ocis-pkg/ldap" "github.com/owncloud/ocis/v2/ocis-pkg/roles" "github.com/owncloud/ocis/v2/ocis-pkg/service/grpc" diff --git a/services/proxy/pkg/middleware/create_home.go b/services/proxy/pkg/middleware/create_home.go index b8810deae04..542bdf74017 100644 --- a/services/proxy/pkg/middleware/create_home.go +++ b/services/proxy/pkg/middleware/create_home.go @@ -9,7 +9,6 @@ import ( 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/bytesize" "github.com/owncloud/ocis/v2/ocis-pkg/log" "google.golang.org/grpc/metadata" ) @@ -19,29 +18,11 @@ func CreateHome(optionSetters ...Option) func(next http.Handler) http.Handler { options := newOptions(optionSetters...) logger := options.Logger - // NOTE: reva currently expects bytes to work with - so we need to convert the human readable string - // to an uint64 and then convert this to a string again. - // It would possibly be simpler to convert bytes sizes on reva side, - // but then all byte handling endpoints should be able to do this - var defaultQuota string - if options.DefaultSpaceQuota != "" { - q, err := bytesize.Parse(options.DefaultSpaceQuota) - if err != nil { - logger.Error().Str("quota", options.DefaultSpaceQuota).Err(err).Msg("can't parse default quota size - using unlimited") - } - - if q != 0 { - defaultQuota = q.String() - } - - } - return func(next http.Handler) http.Handler { return &createHome{ next: next, logger: logger, revaGatewayClient: options.RevaGatewayClient, - defaultQuota: defaultQuota, } } } @@ -65,6 +46,28 @@ func (m createHome) ServeHTTP(w http.ResponseWriter, req *http.Request) { //ctx := tokenpkg.ContextSetToken(r.Context(), token) ctx := metadata.AppendToOutgoingContext(req.Context(), revactx.TokenHeader, token) + getHomeReq := &provider.ListStorageSpacesRequest{ + Filters: []*provider.ListStorageSpacesRequest_Filter{ + { + Type: provider.ListStorageSpacesRequest_Filter_TYPE_SPACE_TYPE, + Term: &provider.ListStorageSpacesRequest_Filter_SpaceType{ + SpaceType: "personal", + }, + }, + }, + } + + getHomeResp, err := m.revaGatewayClient.ListStorageSpaces(ctx, getHomeReq) + if err != nil || getHomeResp.Status.Code != rpc.Code_CODE_OK { + m.next.ServeHTTP(w, req) + return + } + + if len(getHomeResp.StorageSpaces) != 0 { + m.next.ServeHTTP(w, req) + return + } + createHomeReq := &provider.CreateHomeRequest{} if m.defaultQuota != "" { createHomeReq.Opaque = utils.AppendPlainToOpaque(nil, "quota", m.defaultQuota) From 13f1d705d23019423f838632b7de4fb930349f25 Mon Sep 17 00:00:00 2001 From: jkoberg Date: Mon, 19 Sep 2022 10:03:33 +0200 Subject: [PATCH 2/2] remove unneeded comment Signed-off-by: jkoberg --- services/proxy/pkg/middleware/create_home.go | 1 - 1 file changed, 1 deletion(-) diff --git a/services/proxy/pkg/middleware/create_home.go b/services/proxy/pkg/middleware/create_home.go index 542bdf74017..2edef1a0731 100644 --- a/services/proxy/pkg/middleware/create_home.go +++ b/services/proxy/pkg/middleware/create_home.go @@ -43,7 +43,6 @@ func (m createHome) ServeHTTP(w http.ResponseWriter, req *http.Request) { token := req.Header.Get("x-access-token") // we need to pass the token to authenticate the CreateHome request. - //ctx := tokenpkg.ContextSetToken(r.Context(), token) ctx := metadata.AppendToOutgoingContext(req.Context(), revactx.TokenHeader, token) getHomeReq := &provider.ListStorageSpacesRequest{