Merge pull request #4507 from kobergj/DefaultGroupQuotas

[tests-only] Allow setting individual quotas per group
This commit is contained in:
kobergj authored and GitHub committed 2022-09-19 10:51:53 +02:00
commit 82d6c1a8db
6 files changed
+28 -217

No files matched your search

-90
View File
@@ -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))
}
-106
View File
@@ -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))
}
}
+3
View File
@@ -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:"-"`
}
@@ -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",
+1 -1
View File
@@ -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"
+22 -20
View File
@@ -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,
}
}
}
@@ -62,9 +43,30 @@ 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{
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)