Skip to content

Commit 4869f9c

Browse files
authored
Revert: auto generate INTERNAL_TOKEN (#21608) (#21609)
Backport #21608 Follow #19663 Some users do cluster deployment, they still depend on this auto-generating behavior.
1 parent 79275d9 commit 4869f9c

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

modules/setting/setting.go

+19
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"time"
2323

2424
"code.gitea.io/gitea/modules/container"
25+
"code.gitea.io/gitea/modules/generate"
2526
"code.gitea.io/gitea/modules/json"
2627
"code.gitea.io/gitea/modules/log"
2728
"code.gitea.io/gitea/modules/user"
@@ -962,6 +963,11 @@ func loadFromConf(allowEmpty bool, extraConfig string) {
962963
SuccessfulTokensCacheSize = sec.Key("SUCCESSFUL_TOKENS_CACHE_SIZE").MustInt(20)
963964

964965
InternalToken = loadSecret(sec, "INTERNAL_TOKEN_URI", "INTERNAL_TOKEN")
966+
if InstallLock && InternalToken == "" {
967+
// if Gitea has been installed but the InternalToken hasn't been generated (upgrade from an old release), we should generate
968+
// some users do cluster deployment, they still depend on this auto-generating behavior.
969+
generateSaveInternalToken()
970+
}
965971

966972
cfgdata := sec.Key("PASSWORD_COMPLEXITY").Strings(",")
967973
if len(cfgdata) == 0 {
@@ -1182,6 +1188,19 @@ func loadSecret(sec *ini.Section, uriKey, verbatimKey string) string {
11821188
}
11831189
}
11841190

1191+
// generateSaveInternalToken generates and saves the internal token to app.ini
1192+
func generateSaveInternalToken() {
1193+
token, err := generate.NewInternalToken()
1194+
if err != nil {
1195+
log.Fatal("Error generate internal token: %v", err)
1196+
}
1197+
1198+
InternalToken = token
1199+
CreateOrAppendToCustomConf("security.INTERNAL_TOKEN", func(cfg *ini.File) {
1200+
cfg.Section("security").Key("INTERNAL_TOKEN").SetValue(token)
1201+
})
1202+
}
1203+
11851204
// MakeAbsoluteAssetURL returns the absolute asset url prefix without a trailing slash
11861205
func MakeAbsoluteAssetURL(appURL, staticURLPrefix string) string {
11871206
parsedPrefix, err := url.Parse(strings.TrimSuffix(staticURLPrefix, "/"))

0 commit comments

Comments
 (0)