forgejo/modules/setting/config_env.go
Earl Warren 24fed529fc
[BRANDING] parse FORGEJO__* in the container environment
(cherry picked from commit b0759917479ee17ccb11773176dbc8a75323c5cb)
(cherry picked from commit da3f76228e24d2276784ad7e1a03a34d094750e0)
(cherry picked from commit 20d196e74f9d7827cd812e3eb714eb87e888b6db)
(cherry picked from commit 0bf8b1824eac416ddb40fd2e0d7605573ba087a3)
(cherry picked from commit 655bb770a7cf65ada5d3e214ec23219e6cb19df0)
(cherry picked from commit d69d5c2c46f00b041beeb67a6238c5193ce9710a)
(cherry picked from commit 00b55e5a5396a49a6798a3ae2588e412637a77c6)
(cherry picked from commit 456121fd8a4c0c30f7854bd04907f2a0a583d27f)
(cherry picked from commit 9716a158e48ad6333f46fc0806a838603606e5ea)
(cherry picked from commit 7d60a6f5116124b72927a52db0dd36af7a9c3621)
(cherry picked from commit d32a6d9437a38ac35320ad7329eece1ab2bd54d1)
(cherry picked from commit ee1de38527ea1fbd52c319cfbbca24deef951fc1)
(cherry picked from commit 54e7799d13fa041be172d4a05d036cee43e390d4)
(cherry picked from commit 4f04da7ab716ccb1f2816842cde62965e89a852a)
(cherry picked from commit 3e001364ebef3c670e21ed0b7c91a1b4ec8b1577)
(cherry picked from commit d2d5193b388d4b36006a36a5d2145356ff28eec9)
(cherry picked from commit eb1f015c4218c93067f92ab401d6a6973aabf952)
(cherry picked from commit c4587aa893f809dd64418519e2bfc7ebbde4c48f)
(cherry picked from commit 6e340be917725254000c4bc01c22ca00f48142bd)

Conflicts:
	contrib/environment-to-ini/environment-to-ini.go
	https://codeberg.org/forgejo/forgejo/pulls/1773
2023-11-13 14:00:15 +01:00

170 lines
4.7 KiB
Go

// Copyright 2023 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package setting
import (
"bytes"
"os"
"regexp"
"strconv"
"strings"
"code.gitea.io/gitea/modules/log"
)
const (
EnvConfigKeyPrefixGitea = "^(FORGEJO|GITEA)__"
EnvConfigKeySuffixFile = "__FILE"
)
const escapeRegexpString = "_0[xX](([0-9a-fA-F][0-9a-fA-F])+)_"
var escapeRegex = regexp.MustCompile(escapeRegexpString)
func CollectEnvConfigKeys() (keys []string) {
for _, env := range os.Environ() {
if strings.HasPrefix(env, EnvConfigKeyPrefixGitea) {
k, _, _ := strings.Cut(env, "=")
keys = append(keys, k)
}
}
return keys
}
func ClearEnvConfigKeys() {
for _, k := range CollectEnvConfigKeys() {
_ = os.Unsetenv(k)
}
}
// decodeEnvSectionKey will decode a portable string encoded Section__Key pair
// Portable strings are considered to be of the form [A-Z0-9_]*
// We will encode a disallowed value as the UTF8 byte string preceded by _0X and
// followed by _. E.g. _0X2C_ for a '-' and _0X2E_ for '.'
// Section and Key are separated by a plain '__'.
// The entire section can be encoded as a UTF8 byte string
func decodeEnvSectionKey(encoded string) (ok bool, section, key string) {
inKey := false
last := 0
escapeStringIndices := escapeRegex.FindAllStringIndex(encoded, -1)
for _, unescapeIdx := range escapeStringIndices {
preceding := encoded[last:unescapeIdx[0]]
if !inKey {
if splitter := strings.Index(preceding, "__"); splitter > -1 {
section += preceding[:splitter]
inKey = true
key += preceding[splitter+2:]
} else {
section += preceding
}
} else {
key += preceding
}
toDecode := encoded[unescapeIdx[0]+3 : unescapeIdx[1]-1]
decodedBytes := make([]byte, len(toDecode)/2)
for i := 0; i < len(toDecode)/2; i++ {
// Can ignore error here as we know these should be hexadecimal from the regexp
byteInt, _ := strconv.ParseInt(toDecode[2*i:2*i+2], 16, 0)
decodedBytes[i] = byte(byteInt)
}
if inKey {
key += string(decodedBytes)
} else {
section += string(decodedBytes)
}
last = unescapeIdx[1]
}
remaining := encoded[last:]
if !inKey {
if splitter := strings.Index(remaining, "__"); splitter > -1 {
section += remaining[:splitter]
key += remaining[splitter+2:]
} else {
section += remaining
}
} else {
key += remaining
}
section = strings.ToLower(section)
ok = key != ""
if !ok {
section = ""
key = ""
}
return ok, section, key
}
// 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(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)]
}
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, envs []string) (changed bool) {
prefixRegexp := regexp.MustCompile(EnvConfigKeyPrefixGitea)
for _, kv := range envs {
idx := strings.IndexByte(kv, '=')
if idx < 0 {
continue
}
// parse the environment variable to config section name and key name
envKey := kv[:idx]
envValue := kv[idx+1:]
ok, sectionName, keyName, useFileValue := decodeEnvironmentKey(prefixRegexp, EnvConfigKeySuffixFile, envKey)
if !ok {
continue
}
// use environment value as config value, or read the file content as value if the key indicates a file
keyValue := envValue
if useFileValue {
fileContent, err := os.ReadFile(envValue)
if err != nil {
log.Error("Error reading file for %s : %v", envKey, envValue, err)
continue
}
if bytes.HasSuffix(fileContent, []byte("\r\n")) {
fileContent = fileContent[:len(fileContent)-2]
} else if bytes.HasSuffix(fileContent, []byte("\n")) {
fileContent = fileContent[:len(fileContent)-1]
}
keyValue = string(fileContent)
}
// try to set the config value if necessary
section, err := cfg.GetSection(sectionName)
if err != nil {
section, err = cfg.NewSection(sectionName)
if err != nil {
log.Error("Error creating section: %s : %v", sectionName, err)
continue
}
}
key := ConfigSectionKey(section, keyName)
if key == nil {
changed = true
key, err = section.NewKey(keyName, keyValue)
if err != nil {
log.Error("Error creating key: %s in section: %s with value: %s : %v", keyName, sectionName, keyValue, err)
continue
}
}
oldValue := key.Value()
if !changed && oldValue != keyValue {
changed = true
}
key.SetValue(keyValue)
}
return changed
}