Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix all possible setting error related storages and added some tests #23911

Merged
merged 57 commits into from
Jun 14, 2023
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
57 commits
Select commit Hold shift + click to select a range
02747fe
Fix all possible setting error related storages and added some tests
lunny Apr 4, 2023
ae66d8f
Fix copyright year and some documentations
lunny Apr 4, 2023
c98d78f
Merge branch 'main' into lunny/storage_bug_fix
lunny Apr 4, 2023
f70d4c6
Fix lint
lunny Apr 4, 2023
b8bbccf
Merge branch 'main' into lunny/storage_bug_fix
lunny Apr 4, 2023
40407b5
add some comments for getStorage
lunny Apr 10, 2023
d45b3af
Merge branch 'main' into lunny/storage_bug_fix
lunny Apr 10, 2023
35f2a41
Merge branch 'main' into lunny/storage_bug_fix
lunny Apr 10, 2023
7d16902
Fix bug
lunny Apr 10, 2023
7cdd5f8
Merge branch 'main' into lunny/storage_bug_fix
lunny Apr 10, 2023
f7b93f3
Merge branch 'main' into lunny/storage_bug_fix
6543 Apr 11, 2023
0e173c2
Merge branch 'main' into lunny/storage_bug_fix
6543 Apr 11, 2023
31300e7
Merge branch 'main' into lunny/storage_bug_fix
6543 Apr 12, 2023
e85e9fb
Update modules/setting/storage.go
lunny Apr 12, 2023
36d49ff
merge main branch
lunny Apr 27, 2023
7f61c62
merge
lunny Apr 27, 2023
fb1ed84
Merge branch 'main' into lunny/storage_bug_fix
6543 Apr 28, 2023
d08c714
merge main branch
lunny May 4, 2023
751d639
Merge branch 'lunny/storage_bug_fix' of github.com:lunny/gitea into l…
lunny May 4, 2023
48113e1
Remove support for [actions] storage type because actions may have ma…
lunny May 4, 2023
49ed138
Fix lint
lunny May 5, 2023
531eadb
Merge branch 'main' into lunny/storage_bug_fix
lunny May 5, 2023
a781649
Fix read storage configuration from actions
lunny May 5, 2023
a4bd69c
Merge branch 'main' into lunny/storage_bug_fix
lunny May 5, 2023
93714fd
refactor getStorage function
lunny May 5, 2023
163d086
Fix comment
lunny May 5, 2023
d9ab5a0
Merge branch 'main' into lunny/storage_bug_fix
lunny May 5, 2023
fa85e06
Fix storage override sequence
lunny May 6, 2023
f4db800
Merge branch 'main' into lunny/storage_bug_fix
lunny May 6, 2023
e3aa03e
Improve getStorage
lunny May 7, 2023
3e641b9
Merge branch 'main' into lunny/storage_bug_fix
lunny May 7, 2023
7f6369a
Fix override
lunny May 7, 2023
f139b8c
Fix storage
lunny May 8, 2023
2e28ce3
merge main branch
lunny May 14, 2023
e9853b1
Use explicit storage
lunny May 14, 2023
40f21cf
Merge branch 'main' into lunny/storage_bug_fix
lunny May 14, 2023
910bb3c
merge main branch
lunny May 22, 2023
a73e619
revert getstorage change
lunny Jun 2, 2023
f23e15e
merge main branch
lunny Jun 3, 2023
1309698
Merge branch 'main' into lunny/storage_bug_fix
lunny Jun 4, 2023
eb1368a
Merge branch 'main' into lunny/storage_bug_fix
lunny Jun 6, 2023
26e3aef
Fix all tests
lunny Jun 7, 2023
9ecc419
Merge branch 'main' into lunny/storage_bug_fix
lunny Jun 7, 2023
793936d
Fix lint
lunny Jun 7, 2023
82b4ec2
Remove unused interface
lunny Jun 7, 2023
a055e53
improve code
lunny Jun 7, 2023
a4fe93f
Fix test
lunny Jun 8, 2023
9bee6b8
Merge branch 'main' into lunny/storage_bug_fix
lunny Jun 8, 2023
c113c60
shadow possible token leak
lunny Jun 8, 2023
10169b9
Merge branch 'main' into lunny/storage_bug_fix
lunny Jun 8, 2023
1a7a50b
Follow wxiaoguang's suggestion
lunny Jun 8, 2023
9461c21
fix
lunny Jun 8, 2023
a772b6c
update storage configuration documents
lunny Jun 8, 2023
0f85d05
Merge branch 'main' into lunny/storage_bug_fix
lunny Jun 13, 2023
e47fb07
Follow wxiaoguang's suggestion
lunny Jun 13, 2023
2d5726b
Follow wxiaoguang's suggestion
lunny Jun 13, 2023
47e7b1b
Merge branch 'main' into lunny/storage_bug_fix
GiteaBot Jun 14, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions custom/conf/app.example.ini
Original file line number Diff line number Diff line change
Expand Up @@ -2484,6 +2484,10 @@ ROUTER = console
;; Enable/Disable package registry capabilities
;ENABLED = true
;;
;STORAGE_TYPE = local
;; override the minio base path if storage type is minio
;MINIO_BASE_PATH = packages/
;;
;; Path for chunked uploads. Defaults to APP_DATA_PATH + `tmp/package-upload`
;CHUNKED_UPLOAD_PATH = tmp/package-upload
;;
Expand Down Expand Up @@ -2534,6 +2538,19 @@ ROUTER = console
;; storage type
;STORAGE_TYPE = local

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; repo-archive storage will override storage
;;
;[repo-archive]
;STORAGE_TYPE = local
;;
;; Where your lfs files reside, default is data/lfs.
;PATH = data/repo-archive
;;
;; override the minio base path if storage type is minio
;MINIO_BASE_PATH = repo-archive/

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; settings for repository archives, will override storage setting
Expand All @@ -2553,6 +2570,9 @@ ROUTER = console
;;
;; Where your lfs files reside, default is data/lfs.
;PATH = data/lfs
;;
;; override the minio base path if storage type is minio
;MINIO_BASE_PATH = lfs/

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Expand Down Expand Up @@ -2602,6 +2622,9 @@ ROUTER = console
; [actions]
;; Enable/Disable actions capabilities
;ENABLED = false
;;
;STORAGE_TYPE = local
;;
lunny marked this conversation as resolved.
Show resolved Hide resolved
;; Default address to get action plugins, e.g. the default value means downloading from "https://gitea.com/actions/checkout" for "uses: actions/checkout@v3"
;DEFAULT_ACTIONS_URL = https://gitea.com

Expand Down
7 changes: 7 additions & 0 deletions docs/content/doc/administration/config-cheat-sheet.en-us.md
Original file line number Diff line number Diff line change
Expand Up @@ -1340,6 +1340,11 @@ is `data/repo-archive` and the default of `MINIO_BASE_PATH` is `repo-archive/`.
- `MINIO_USE_SSL`: **false**: Minio enabled ssl only available when `STORAGE_TYPE` is `minio`
- `MINIO_INSECURE_SKIP_VERIFY`: **false**: Minio skip SSL verification available when STORAGE_TYPE is `minio`

## Repository Archives (`repo-archive`)

- `STORAGE_TYPE`: **local**: Storage type for actions logs, `local` for local disk or `minio` for s3 compatible object storage service, default is `local` or other name defined with `[storage.xxx]`
- `MINIO_BASE_PATH`: **actions_log/**: Minio base path on the bucket only available when STORAGE_TYPE is `minio`

## Proxy (`proxy`)

- `PROXY_ENABLED`: **false**: Enable the proxy if true, all requests to external via HTTP will be affected, if false, no proxy will be used even environment http_proxy/https_proxy
Expand All @@ -1358,6 +1363,8 @@ PROXY_HOSTS = *.github.com

- `ENABLED`: **false**: Enable/Disable actions capabilities
- `DEFAULT_ACTIONS_URL`: **https://gitea.com**: Default address to get action plugins, e.g. the default value means downloading from "<https://gitea.com/actions/checkout>" for "uses: actions/checkout@v3"
- `STORAGE_TYPE`: **local**: Storage type for actions logs, `local` for local disk or `minio` for s3 compatible object storage service, default is `local` or other name defined with `[storage.xxx]`
- `MINIO_BASE_PATH`: **actions_log/**: Minio base path on the bucket only available when STORAGE_TYPE is `minio`

`DEFAULT_ACTIONS_URL` indicates where should we find the relative path action plugin. i.e. when use an action in a workflow file like

Expand Down
13 changes: 7 additions & 6 deletions modules/setting/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@

package setting

import (
"code.gitea.io/gitea/modules/log"
)
import "fmt"

// Actions settings
var (
Expand All @@ -19,11 +17,14 @@ var (
}
)

func loadActionsFrom(rootCfg ConfigProvider) {
func loadActionsFrom(rootCfg ConfigProvider) error {
sec := rootCfg.Section("actions")
if err := sec.MapTo(&Actions); err != nil {
log.Fatal("Failed to map Actions settings: %v", err)
return fmt.Errorf("failed to map Actions settings: %v", err)
}
sec.Key("MINIO_BASE_PATH").MustString("actions_log/")
storageType := sec.Key("STORAGE_TYPE").MustString("")

Actions.Storage = getStorage(rootCfg, "actions_log", "", nil)
Actions.Storage = getStorage(rootCfg, "actions_log", storageType, sec)
lunny marked this conversation as resolved.
Show resolved Hide resolved
return nil
}
64 changes: 64 additions & 0 deletions modules/setting/actions_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Copyright 2023 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT

package setting

import (
"testing"

"github.com/stretchr/testify/assert"
ini "gopkg.in/ini.v1"
)

func Test_getStorageInheritNameSectionTypeForActions(t *testing.T) {
iniStr := `
[storage]
STORAGE_TYPE = minio
`
cfg, err := ini.Load([]byte(iniStr))
assert.NoError(t, err)
assert.NoError(t, loadActionsFrom(cfg))

assert.EqualValues(t, "minio", Actions.Storage.Type)
assert.EqualValues(t, "actions_log/", cfg.Section("actions").Key("MINIO_BASE_PATH").MustString(""))

iniStr = `
[storage.actions_log]
STORAGE_TYPE = minio
`
cfg, err = ini.Load([]byte(iniStr))
assert.NoError(t, err)
assert.NoError(t, loadActionsFrom(cfg))

assert.EqualValues(t, "minio", Actions.Storage.Type)
assert.EqualValues(t, "actions_log/", cfg.Section("actions").Key("MINIO_BASE_PATH").MustString(""))

iniStr = `
[actions]
STORAGE_TYPE = my_minio

[storage.my_minio]
STORAGE_TYPE = minio
`
cfg, err = ini.Load([]byte(iniStr))
assert.NoError(t, err)
assert.NoError(t, loadActionsFrom(cfg))

assert.EqualValues(t, "minio", Actions.Storage.Type)
assert.EqualValues(t, "actions_log/", cfg.Section("actions").Key("MINIO_BASE_PATH").MustString(""))

iniStr = `
[actions]
STORAGE_TYPE = my_minio
MINIO_BASE_PATH = my_actions_log/

[storage.my_minio]
STORAGE_TYPE = minio
`
cfg, err = ini.Load([]byte(iniStr))
assert.NoError(t, err)
assert.NoError(t, loadActionsFrom(cfg))

assert.EqualValues(t, "minio", Actions.Storage.Type)
assert.EqualValues(t, "my_actions_log/", cfg.Section("actions").Key("MINIO_BASE_PATH").MustString(""))
}
4 changes: 3 additions & 1 deletion modules/setting/attachment.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ var Attachment = struct {
Enabled: true,
}

func loadAttachmentFrom(rootCfg ConfigProvider) {
func loadAttachmentFrom(rootCfg ConfigProvider) error {
sec := rootCfg.Section("attachment")
storageType := sec.Key("STORAGE_TYPE").MustString("")

Expand All @@ -30,4 +30,6 @@ func loadAttachmentFrom(rootCfg ConfigProvider) {
Attachment.MaxSize = sec.Key("MAX_SIZE").MustInt64(4)
Attachment.MaxFiles = sec.Key("MAX_FILES").MustInt(5)
Attachment.Enabled = sec.Key("ENABLED").MustBool(true)

return nil
}
114 changes: 114 additions & 0 deletions modules/setting/attachment_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
// Copyright 2023 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT

package setting

import (
"testing"

"github.com/stretchr/testify/assert"
ini "gopkg.in/ini.v1"
)

func Test_getStorageCustomType(t *testing.T) {
iniStr := `
[attachment]
STORAGE_TYPE = my_minio
MINIO_BUCKET = gitea-attachment

[storage.my_minio]
STORAGE_TYPE = minio
MINIO_ENDPOINT = my_minio:9000
`
cfg, err := ini.Load([]byte(iniStr))
assert.NoError(t, err)

assert.NoError(t, loadAttachmentFrom(cfg))

assert.EqualValues(t, "minio", Attachment.Storage.Type)
assert.EqualValues(t, "my_minio:9000", Attachment.Storage.Section.Key("MINIO_ENDPOINT").String())
assert.EqualValues(t, "gitea-attachment", Attachment.Storage.Section.Key("MINIO_BUCKET").String())
}

func Test_getStorageNameSectionOverridesTypeSection(t *testing.T) {
iniStr := `
[attachment]
STORAGE_TYPE = minio

[storage.attachments]
MINIO_BUCKET = gitea-attachment

[storage.minio]
MINIO_BUCKET = gitea
`
cfg, err := ini.Load([]byte(iniStr))
assert.NoError(t, err)

assert.NoError(t, loadAttachmentFrom(cfg))

assert.EqualValues(t, "minio", Attachment.Storage.Type)
assert.EqualValues(t, "gitea-attachment", Attachment.Storage.Section.Key("MINIO_BUCKET").String())
}

func Test_getStorageTypeSectionOverridesStorageSection(t *testing.T) {
iniStr := `
[attachment]
STORAGE_TYPE = minio

[storage.minio]
MINIO_BUCKET = gitea-minio

[storage]
MINIO_BUCKET = gitea
`
cfg, err := ini.Load([]byte(iniStr))
assert.NoError(t, err)

assert.NoError(t, loadAttachmentFrom(cfg))

assert.EqualValues(t, "minio", Attachment.Storage.Type)
assert.EqualValues(t, "gitea-minio", Attachment.Storage.Section.Key("MINIO_BUCKET").String())
}

func Test_getStorageSpecificOverridesStorage(t *testing.T) {
iniStr := `
[attachment]
STORAGE_TYPE = minio
MINIO_BUCKET = gitea-attachment

[storage.attachments]
MINIO_BUCKET = gitea

[storage]
STORAGE_TYPE = local
`
cfg, err := ini.Load([]byte(iniStr))
assert.NoError(t, err)

assert.NoError(t, loadAttachmentFrom(cfg))

assert.EqualValues(t, "minio", Attachment.Storage.Type)
assert.EqualValues(t, "gitea-attachment", Attachment.Storage.Section.Key("MINIO_BUCKET").String())
}

func Test_getStorageGetDefaults(t *testing.T) {
cfg, err := ini.Load([]byte(""))
assert.NoError(t, err)

assert.NoError(t, loadAttachmentFrom(cfg))

assert.EqualValues(t, "gitea", Attachment.Storage.Section.Key("MINIO_BUCKET").String())
}

func Test_getStorageInheritNameSectionType(t *testing.T) {
iniStr := `
[storage.attachments]
STORAGE_TYPE = minio
`
cfg, err := ini.Load([]byte(iniStr))
assert.NoError(t, err)

assert.NoError(t, loadAttachmentFrom(cfg))

assert.EqualValues(t, "minio", Attachment.Storage.Type)
}
38 changes: 20 additions & 18 deletions modules/setting/lfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ package setting

import (
"encoding/base64"
"fmt"
"time"

"code.gitea.io/gitea/modules/generate"
"code.gitea.io/gitea/modules/log"

ini "gopkg.in/ini.v1"
)
Expand All @@ -25,10 +25,10 @@ var LFS = struct {
Storage
}{}

func loadLFSFrom(rootCfg ConfigProvider) {
func loadLFSFrom(rootCfg ConfigProvider) error {
sec := rootCfg.Section("server")
if err := sec.MapTo(&LFS); err != nil {
log.Fatal("Failed to map LFS settings: %v", err)
return fmt.Errorf("failed to map LFS settings: %v", err)
}

lfsSec := rootCfg.Section("lfs")
Expand All @@ -50,21 +50,23 @@ func loadLFSFrom(rootCfg ConfigProvider) {

LFS.HTTPAuthExpiry = sec.Key("LFS_HTTP_AUTH_EXPIRY").MustDuration(20 * time.Minute)

if LFS.StartServer {
LFS.JWTSecretBytes = make([]byte, 32)
n, err := base64.RawURLEncoding.Decode(LFS.JWTSecretBytes, []byte(LFS.JWTSecretBase64))

if err != nil || n != 32 {
LFS.JWTSecretBase64, err = generate.NewJwtSecretBase64()
if err != nil {
log.Fatal("Error generating JWT Secret for custom config: %v", err)
return
}

// Save secret
CreateOrAppendToCustomConf("server.LFS_JWT_SECRET", func(cfg *ini.File) {
cfg.Section("server").Key("LFS_JWT_SECRET").SetValue(LFS.JWTSecretBase64)
})
if !LFS.StartServer {
return nil
}

LFS.JWTSecretBytes = make([]byte, 32)
n, err := base64.RawURLEncoding.Decode(LFS.JWTSecretBytes, []byte(LFS.JWTSecretBase64))

if err != nil || n != 32 {
LFS.JWTSecretBase64, err = generate.NewJwtSecretBase64()
if err != nil {
return fmt.Errorf("error generating JWT Secret for custom config: %v", err)
}

// Save secret
CreateOrAppendToCustomConf("server.LFS_JWT_SECRET", func(cfg *ini.File) {
cfg.Section("server").Key("LFS_JWT_SECRET").SetValue(LFS.JWTSecretBase64)
})
}
return nil
}
Loading