summaryrefslogtreecommitdiffstats
path: root/modules
diff options
context:
space:
mode:
authorKN4CK3R <admin@oldschoolhack.me>2022-11-09 07:34:27 +0100
committerGitHub <noreply@github.com>2022-11-09 14:34:27 +0800
commit20674dd05da909b42cbdd07a6682fdf1d980f011 (patch)
treef51b4a6b907380d27381705e5b2e6a1187af167b /modules
parentcb83288530b1860677b07d72bc4ce8349e3c0d67 (diff)
downloadgitea-20674dd05da909b42cbdd07a6682fdf1d980f011.tar.gz
gitea-20674dd05da909b42cbdd07a6682fdf1d980f011.zip
Add package registry quota limits (#21584)
Related #20471 This PR adds global quota limits for the package registry. Settings for individual users/orgs can be added in a seperate PR using the settings table. Co-authored-by: Lauris BH <lauris@nix.lv> Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
Diffstat (limited to 'modules')
-rw-r--r--modules/setting/packages.go50
-rw-r--r--modules/setting/packages_test.go31
2 files changed, 80 insertions, 1 deletions
diff --git a/modules/setting/packages.go b/modules/setting/packages.go
index 5e0f2a3b03..62201032c7 100644
--- a/modules/setting/packages.go
+++ b/modules/setting/packages.go
@@ -5,11 +5,15 @@
package setting
import (
+ "math"
"net/url"
"os"
"path/filepath"
"code.gitea.io/gitea/modules/log"
+
+ "github.com/dustin/go-humanize"
+ ini "gopkg.in/ini.v1"
)
// Package registry settings
@@ -19,8 +23,24 @@ var (
Enabled bool
ChunkedUploadPath string
RegistryHost string
+
+ LimitTotalOwnerCount int64
+ LimitTotalOwnerSize int64
+ LimitSizeComposer int64
+ LimitSizeConan int64
+ LimitSizeContainer int64
+ LimitSizeGeneric int64
+ LimitSizeHelm int64
+ LimitSizeMaven int64
+ LimitSizeNpm int64
+ LimitSizeNuGet int64
+ LimitSizePub int64
+ LimitSizePyPI int64
+ LimitSizeRubyGems int64
+ LimitSizeVagrant int64
}{
- Enabled: true,
+ Enabled: true,
+ LimitTotalOwnerCount: -1,
}
)
@@ -43,4 +63,32 @@ func newPackages() {
if err := os.MkdirAll(Packages.ChunkedUploadPath, os.ModePerm); err != nil {
log.Error("Unable to create chunked upload directory: %s (%v)", Packages.ChunkedUploadPath, err)
}
+
+ Packages.LimitTotalOwnerSize = mustBytes(sec, "LIMIT_TOTAL_OWNER_SIZE")
+ Packages.LimitSizeComposer = mustBytes(sec, "LIMIT_SIZE_COMPOSER")
+ Packages.LimitSizeConan = mustBytes(sec, "LIMIT_SIZE_CONAN")
+ Packages.LimitSizeContainer = mustBytes(sec, "LIMIT_SIZE_CONTAINER")
+ Packages.LimitSizeGeneric = mustBytes(sec, "LIMIT_SIZE_GENERIC")
+ Packages.LimitSizeHelm = mustBytes(sec, "LIMIT_SIZE_HELM")
+ Packages.LimitSizeMaven = mustBytes(sec, "LIMIT_SIZE_MAVEN")
+ Packages.LimitSizeNpm = mustBytes(sec, "LIMIT_SIZE_NPM")
+ Packages.LimitSizeNuGet = mustBytes(sec, "LIMIT_SIZE_NUGET")
+ Packages.LimitSizePub = mustBytes(sec, "LIMIT_SIZE_PUB")
+ Packages.LimitSizePyPI = mustBytes(sec, "LIMIT_SIZE_PYPI")
+ Packages.LimitSizeRubyGems = mustBytes(sec, "LIMIT_SIZE_RUBYGEMS")
+ Packages.LimitSizeVagrant = mustBytes(sec, "LIMIT_SIZE_VAGRANT")
+}
+
+func mustBytes(section *ini.Section, key string) int64 {
+ const noLimit = "-1"
+
+ value := section.Key(key).MustString(noLimit)
+ if value == noLimit {
+ return -1
+ }
+ bytes, err := humanize.ParseBytes(value)
+ if err != nil || bytes > math.MaxInt64 {
+ return -1
+ }
+ return int64(bytes)
}
diff --git a/modules/setting/packages_test.go b/modules/setting/packages_test.go
new file mode 100644
index 0000000000..059273dce4
--- /dev/null
+++ b/modules/setting/packages_test.go
@@ -0,0 +1,31 @@
+// Copyright 2022 The Gitea Authors. All rights reserved.
+// Use of this source code is governed by a MIT-style
+// license that can be found in the LICENSE file.
+
+package setting
+
+import (
+ "testing"
+
+ "github.com/stretchr/testify/assert"
+ ini "gopkg.in/ini.v1"
+)
+
+func TestMustBytes(t *testing.T) {
+ test := func(value string) int64 {
+ sec, _ := ini.Empty().NewSection("test")
+ sec.NewKey("VALUE", value)
+
+ return mustBytes(sec, "VALUE")
+ }
+
+ assert.EqualValues(t, -1, test(""))
+ assert.EqualValues(t, -1, test("-1"))
+ assert.EqualValues(t, 0, test("0"))
+ assert.EqualValues(t, 1, test("1"))
+ assert.EqualValues(t, 10000, test("10000"))
+ assert.EqualValues(t, 1000000, test("1 mb"))
+ assert.EqualValues(t, 1048576, test("1mib"))
+ assert.EqualValues(t, 1782579, test("1.7mib"))
+ assert.EqualValues(t, -1, test("1 yib")) // too large
+}