diff --git a/.woodpecker/testing-amd64.yml b/.woodpecker/testing-amd64.yml index 340ee55736..94fd9e1844 100644 --- a/.woodpecker/testing-amd64.yml +++ b/.woodpecker/testing-amd64.yml @@ -75,6 +75,14 @@ pipeline: commands: - ./build/test-env-prepare.sh + environment-to-ini: + image: *golang_image + environment: + GOPROXY_OVERRIDE: *goproxy_override + commands: + - *goproxy_setup + - go test contrib/environment-to-ini/environment-to-ini.go contrib/environment-to-ini/environment-to-ini_test.go + build: image: *test_image environment: diff --git a/contrib/environment-to-ini/environment-to-ini.go b/contrib/environment-to-ini/environment-to-ini.go index 3405d7d429..ba546efa7d 100644 --- a/contrib/environment-to-ini/environment-to-ini.go +++ b/contrib/environment-to-ini/environment-to-ini.go @@ -5,6 +5,7 @@ package main import ( "os" + "regexp" "strings" "code.gitea.io/gitea/modules/log" @@ -14,21 +15,21 @@ import ( ) // EnvironmentPrefix environment variables prefixed with this represent ini values to write -const EnvironmentPrefix = "GITEA" +const EnvironmentPrefix = "^(FORGEJO|GITEA)" func main() { app := cli.NewApp() app.Name = "environment-to-ini" app.Usage = "Use provided environment to update configuration ini" - app.Description = `As a helper to allow docker users to update the gitea configuration + app.Description = `As a helper to allow docker users to update the Forgejo configuration through the environment, this command allows environment variables to be mapped to values in the ini. - Environment variables of the form "GITEA__SECTION_NAME__KEY_NAME" + Environment variables of the form "FORGEJO__SECTION_NAME__KEY_NAME" will be mapped to the ini section "[section_name]" and the key "KEY_NAME" with the value as provided. - Environment variables of the form "GITEA__SECTION_NAME__KEY_NAME__FILE" + Environment variables of the form "FORGEJO__SECTION_NAME__KEY_NAME__FILE" will be mapped to the ini section "[section_name]" and the key "KEY_NAME" with the value loaded from the specified file. @@ -46,8 +47,8 @@ func main() { ... """ - You would set the environment variables: "GITEA__LOG_0x2E_CONSOLE__COLORIZE=false" - and "GITEA__LOG_0x2E_CONSOLE__STDERR=false". Other examples can be found + You would set the environment variables: "FORGEJO__LOG_0x2E_CONSOLE__COLORIZE=false" + and "FORGEJO__LOG_0x2E_CONSOLE__STDERR=false". Other examples can be found on the configuration cheat sheet.` app.Flags = []cli.Flag{ cli.StringFlag{ @@ -63,7 +64,7 @@ func main() { cli.StringFlag{ Name: "work-path, w", Value: setting.AppWorkPath, - Usage: "Set the gitea working path", + Usage: "Set the forgejo working path", }, cli.StringFlag{ Name: "out, o", @@ -89,6 +90,19 @@ func main() { } } +func splitEnvironmentVariable(prefixRegexp *regexp.Regexp, kv string) (string, string) { + idx := strings.IndexByte(kv, '=') + if idx < 0 { + return "", "" + } + k := kv[:idx] + loc := prefixRegexp.FindStringIndex(k) + if loc == nil { + return "", "" + } + return k[loc[1]:], kv[idx+1:] +} + func runEnvironmentToIni(c *cli.Context) error { providedCustom := c.String("custom-path") providedConf := c.String("config") @@ -119,15 +133,13 @@ func runEnvironmentToIni(c *cli.Context) error { // clear Gitea's specific environment variables if requested if c.Bool("clear") { + prefixRegexp := regexp.MustCompile(prefixGitea) for _, kv := range os.Environ() { - idx := strings.IndexByte(kv, '=') - if idx < 0 { + eKey, _ := splitEnvironmentVariable(prefixRegexp, kv) + if eKey == "" { continue } - eKey := kv[:idx] - if strings.HasPrefix(eKey, prefixGitea) { - _ = os.Unsetenv(eKey) - } + _ = os.Unsetenv(eKey) } } diff --git a/contrib/environment-to-ini/environment-to-ini_test.go b/contrib/environment-to-ini/environment-to-ini_test.go new file mode 100644 index 0000000000..5881888fc1 --- /dev/null +++ b/contrib/environment-to-ini/environment-to-ini_test.go @@ -0,0 +1,21 @@ +// Copyright 2023 The Forgejo Authors. All rights reserved. +// SPDX-License-Identifier: MIT + +package main + +import ( + "regexp" + "testing" + + "github.com/stretchr/testify/assert" +) + +func Test_splitEnvironmentVariable(t *testing.T) { + prefixRegexp := regexp.MustCompile(EnvironmentPrefix + "__") + k, v := splitEnvironmentVariable(prefixRegexp, "FORGEJO__KEY=VALUE") + assert.Equal(t, k, "KEY") + assert.Equal(t, v, "VALUE") + k, v = splitEnvironmentVariable(prefixRegexp, "nothing=interesting") + assert.Equal(t, k, "") + assert.Equal(t, v, "") +} diff --git a/modules/setting/config_env.go b/modules/setting/config_env.go index 6348803705..4ec2e61289 100644 --- a/modules/setting/config_env.go +++ b/modules/setting/config_env.go @@ -75,19 +75,21 @@ func decodeEnvSectionKey(encoded string) (ok bool, section, key string) { // decodeEnvironmentKey decode the environment key to section and key // The environment key is in the form of GITEA__SECTION__KEY or GITEA__SECTION__KEY__FILE -func decodeEnvironmentKey(prefixGitea, suffixFile, envKey string) (ok bool, section, key string, useFileValue bool) { - if !strings.HasPrefix(envKey, prefixGitea) { - return false, "", "", false - } +func decodeEnvironmentKey(prefixRegexp *regexp.Regexp, suffixFile, envKey string) (ok bool, section, key string, useFileValue bool) { if strings.HasSuffix(envKey, suffixFile) { useFileValue = true envKey = envKey[:len(envKey)-len(suffixFile)] } - ok, section, key = decodeEnvSectionKey(envKey[len(prefixGitea):]) + loc := prefixRegexp.FindStringIndex(envKey) + if loc == nil { + return false, "", "", false + } + ok, section, key = decodeEnvSectionKey(envKey[loc[1]:]) return ok, section, key, useFileValue } func EnvironmentToConfig(cfg ConfigProvider, prefixGitea, suffixFile string, envs []string) (changed bool) { + prefixRegexp := regexp.MustCompile(prefixGitea) for _, kv := range envs { idx := strings.IndexByte(kv, '=') if idx < 0 { @@ -97,7 +99,7 @@ func EnvironmentToConfig(cfg ConfigProvider, prefixGitea, suffixFile string, env // parse the environment variable to config section name and key name envKey := kv[:idx] envValue := kv[idx+1:] - ok, sectionName, keyName, useFileValue := decodeEnvironmentKey(prefixGitea, suffixFile, envKey) + ok, sectionName, keyName, useFileValue := decodeEnvironmentKey(prefixRegexp, suffixFile, envKey) if !ok { continue } diff --git a/modules/setting/config_env_test.go b/modules/setting/config_env_test.go index d574554bcc..41c4b4ac2a 100644 --- a/modules/setting/config_env_test.go +++ b/modules/setting/config_env_test.go @@ -5,6 +5,7 @@ package setting import ( "os" + "regexp" "testing" "github.com/stretchr/testify/assert" @@ -33,7 +34,7 @@ func TestDecodeEnvSectionKey(t *testing.T) { } func TestDecodeEnvironmentKey(t *testing.T) { - prefix := "GITEA__" + prefix := regexp.MustCompile("^(FORGEJO|GITEA)__") suffix := "__FILE" ok, section, key, file := decodeEnvironmentKey(prefix, suffix, "SEC__KEY") @@ -54,6 +55,12 @@ func TestDecodeEnvironmentKey(t *testing.T) { assert.Equal(t, "KEY", key) assert.False(t, file) + ok, section, key, file = decodeEnvironmentKey(prefix, suffix, "FORGEJO__SEC__KEY") + assert.True(t, ok) + assert.Equal(t, "sec", section) + assert.Equal(t, "KEY", key) + assert.False(t, file) + // with "__FILE" suffix, it doesn't support to write "[sec].FILE" to config (no such key FILE is used in Gitea) // but it could be fixed in the future by adding a new suffix like "__VALUE" (no such key VALUE is used in Gitea either) ok, section, key, file = decodeEnvironmentKey(prefix, suffix, "GITEA__SEC__FILE") @@ -72,7 +79,7 @@ func TestDecodeEnvironmentKey(t *testing.T) { func TestEnvironmentToConfig(t *testing.T) { cfg, _ := NewConfigProviderFromData("") - changed := EnvironmentToConfig(cfg, "GITEA__", "__FILE", nil) + changed := EnvironmentToConfig(cfg, "^(FORGEJO|GITEA)__", "__FILE", nil) assert.False(t, changed) cfg, err := NewConfigProviderFromData(` @@ -81,16 +88,16 @@ key = old `) assert.NoError(t, err) - changed = EnvironmentToConfig(cfg, "GITEA__", "__FILE", []string{"GITEA__sec__key=new"}) + changed = EnvironmentToConfig(cfg, "^(FORGEJO|GITEA)__", "__FILE", []string{"GITEA__sec__key=new"}) assert.True(t, changed) assert.Equal(t, "new", cfg.Section("sec").Key("key").String()) - changed = EnvironmentToConfig(cfg, "GITEA__", "__FILE", []string{"GITEA__sec__key=new"}) + changed = EnvironmentToConfig(cfg, "^(FORGEJO|GITEA)__", "__FILE", []string{"GITEA__sec__key=new"}) assert.False(t, changed) tmpFile := t.TempDir() + "/the-file" _ = os.WriteFile(tmpFile, []byte("value-from-file"), 0o644) - changed = EnvironmentToConfig(cfg, "GITEA__", "__FILE", []string{"GITEA__sec__key__FILE=" + tmpFile}) + changed = EnvironmentToConfig(cfg, "^(FORGEJO|GITEA)__", "__FILE", []string{"GITEA__sec__key__FILE=" + tmpFile}) assert.True(t, changed) assert.Equal(t, "value-from-file", cfg.Section("sec").Key("key").String()) }