-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Add cmd to allow generalised mapping of environment variables to Gitea Ini #7287
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
Closed
Closed
Changes from 11 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
94426b6
Add cmd to allow generalised mapping of environment variables to Gite…
zeripath 890d539
spelling mistake
zeripath 51338a1
Use full path to gitea
zeripath 64c15c2
Move to use double underscore as a separator
zeripath f73377b
Add encoding to the environment strings
zeripath 730e300
Remove unused separator
zeripath e0ca836
Add Documentation
zeripath 30a289f
Merge branch 'master' into environment-to-ini
zeripath f7d10ea
Remove unused args struct
zeripath 4ae3b4d
Merge branch 'environment-to-ini' of github.com:zeripath/gitea into e…
zeripath aba65bf
Merge branch 'master' into environment-to-ini
zeripath 80b54d1
Merge branch 'master' into environment-to-ini
zeripath fd82c14
Merge branch 'master' into environment-to-ini
zeripath fc52e23
Merge branch 'master' into environment-to-ini
zeripath 3f5e1ba
Provide example as per @silverwind
zeripath aa42fb8
Merge branch 'master' into environment-to-ini
zeripath 019e124
Fix Unknwon to unknwon in cmd/environment_to_ini
zeripath f157062
Remove Prefix before passing to DecodeSectionKey
zeripath 8a5a3b8
Merge branch 'master' into environment-to-ini
zeripath bd44265
Merge branch 'master' into environment-to-ini
sapk cbd9404
Merge branch 'master' into environment-to-ini
zeripath File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
// Copyright 2019 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 cmd | ||
|
||
import ( | ||
"os" | ||
"regexp" | ||
"strconv" | ||
"strings" | ||
|
||
"code.gitea.io/gitea/modules/log" | ||
"code.gitea.io/gitea/modules/setting" | ||
"github.com/Unknwon/com" | ||
"github.com/urfave/cli" | ||
ini "gopkg.in/ini.v1" | ||
) | ||
|
||
// EnvironmentPrefix environment variables prefixed with this represent ini values to write | ||
const EnvironmentPrefix = "GITEA__" | ||
|
||
// CmdEnvironmentToIni represents the command to use a provided environment to update the configuration ini | ||
var CmdEnvironmentToIni = cli.Command{ | ||
Name: "environment-to-ini", | ||
Usage: "Use provided environment to update configuration ini", | ||
Action: runEnvironmentToIni, | ||
Flags: []cli.Flag{ | ||
cli.StringFlag{ | ||
Name: "out, o", | ||
Value: "", | ||
Usage: "Destination file to write to", | ||
}, | ||
}, | ||
} | ||
|
||
func runEnvironmentToIni(c *cli.Context) error { | ||
cfg := ini.Empty() | ||
if com.IsFile(setting.CustomConf) { | ||
if err := cfg.Append(setting.CustomConf); err != nil { | ||
log.Fatal("Failed to load custom conf '%s': %v", setting.CustomConf, err) | ||
} | ||
} else { | ||
log.Warn("Custom config '%s' not found, ignore this if you're running first time", setting.CustomConf) | ||
} | ||
cfg.NameMapper = ini.AllCapsUnderscore | ||
|
||
for _, kv := range os.Environ() { | ||
idx := strings.IndexByte(kv, '=') | ||
if idx < 0 { | ||
continue | ||
} | ||
eKey := kv[:idx] | ||
value := kv[idx+1:] | ||
if !strings.HasPrefix(eKey, EnvironmentPrefix) { | ||
continue | ||
} | ||
sectionName, keyName := DecodeSectionKey(eKey) | ||
if len(keyName) == 0 { | ||
continue | ||
} | ||
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 := section.Key(keyName) | ||
if key == nil { | ||
key, err = section.NewKey(keyName, value) | ||
if err != nil { | ||
log.Error("Error creating key: %s in section: %s with value: %s : %v", keyName, sectionName, value, err) | ||
continue | ||
} | ||
} | ||
key.SetValue(value) | ||
} | ||
destination := c.String("out") | ||
if len(destination) == 0 { | ||
destination = setting.CustomConf | ||
} | ||
err := cfg.SaveTo(destination) | ||
return err | ||
} | ||
|
||
const escapeRegexpString = "_0[xX](([0-9a-fA-F][0-9a-fA-F])+)_" | ||
|
||
var escapeRegex = regexp.MustCompile(escapeRegexpString) | ||
|
||
// DecodeSectionKey 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 DecodeSectionKey(encoded string) (string, string) { | ||
section := "" | ||
key := "" | ||
|
||
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] | ||
inKey = true | ||
key += remaining[splitter+2:] | ||
} else { | ||
section += remaining | ||
} | ||
} else { | ||
key += remaining | ||
} | ||
return section, key | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// Copyright 2019 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 cmd | ||
|
||
import "testing" | ||
|
||
func TestDecodeSectionKey(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
encoded string | ||
section string | ||
key string | ||
}{ | ||
{ | ||
name: "Simple", | ||
encoded: "Section__Key", | ||
section: "Section", | ||
key: "Key", | ||
}, | ||
{ | ||
name: "LessSimple", | ||
encoded: "Section_SubSection__Key_SubKey", | ||
section: "Section_SubSection", | ||
key: "Key_SubKey", | ||
}, | ||
{ | ||
name: "OneDotOneDash", | ||
encoded: "Section_0X2E_SubSection__Key_0X2D_SubKey", | ||
section: "Section.SubSection", | ||
key: "Key-SubKey", | ||
}, | ||
{ | ||
name: "OneDotOneEncodedOneDash", | ||
encoded: "Section_0X2E_0X2E_Sub_0X2D_Section__Key_0X2D_SubKey", | ||
section: "Section.0X2E_Sub-Section", | ||
key: "Key-SubKey", | ||
}, | ||
{ | ||
name: "EncodedUnderscore", | ||
encoded: "Section__0X5F_0X2E_Sub_0X2D_Section__Key_0X2D__0X2D_SubKey", | ||
section: "Section__0X2E_Sub-Section", | ||
key: "Key--SubKey", | ||
}, | ||
{ | ||
name: "EncodedUtf8", | ||
encoded: "Section__0XE280A6_Sub_0X2D_Section__Key_0X2D__0X2D_SubKey", | ||
section: "Section_…Sub-Section", | ||
key: "Key--SubKey", | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
gotSection, gotKey := DecodeSectionKey(tt.encoded) | ||
if gotSection != tt.section { | ||
t.Errorf("DecodeSectionKey() gotSection = %v, want %v", gotSection, tt.section) | ||
} | ||
if gotKey != tt.key { | ||
t.Errorf("DecodeSectionKey() gotKey = %v, want %v", gotKey, tt.key) | ||
} | ||
}) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,6 +43,8 @@ if [ ! -f ${GITEA_CUSTOM}/conf/app.ini ]; then | |
SECRET_KEY=${SECRET_KEY:-""} \ | ||
envsubst < /etc/templates/app.ini > ${GITEA_CUSTOM}/conf/app.ini | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can't we drop the template and for example do ?
(maybe they can get in one line) |
||
|
||
/app/gitea/gitea environment-to-ini -c ${GITEA_CUSTOM}/conf/app.ini | ||
|
||
chown ${USER}:git ${GITEA_CUSTOM}/conf/app.ini | ||
fi | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.