From 6fa880c935bc2e47861d9bc3aa8448c97de192c5 Mon Sep 17 00:00:00 2001 From: pikomonde <32364823+pikomonde@users.noreply.github.com> Date: Wed, 27 Mar 2024 01:12:55 +0700 Subject: [PATCH 1/6] validate config file for deprecated value --- internal/conf/conf.go | 1 + internal/conf/static.go | 47 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) diff --git a/internal/conf/conf.go b/internal/conf/conf.go index f7a6f88666f..61d2b3fef71 100644 --- a/internal/conf/conf.go +++ b/internal/conf/conf.go @@ -350,6 +350,7 @@ func Init(customConf string) error { LFS.ObjectsPath = ensureAbs(LFS.ObjectsPath) handleDeprecated() + warnDeprecated(File) if err = File.Section("cache").MapTo(&Cache); err != nil { return errors.Wrap(err, "mapping [cache] section") diff --git a/internal/conf/static.go b/internal/conf/static.go index 016fd139cb8..38f0f64264c 100644 --- a/internal/conf/static.go +++ b/internal/conf/static.go @@ -10,6 +10,8 @@ import ( "time" "github.com/gogs/go-libravatar" + "gopkg.in/ini.v1" + log "unknwon.dev/clog/v2" ) // ℹ️ README: This file contains static values that should only be set at initialization time. @@ -433,6 +435,51 @@ func handleDeprecated() { // } } +// warnDeprecated warns about deprecated configuration sections and options. +func warnDeprecated(cfg *ini.File) { + deprecatedSections := map[string]string{ + "mailer": "email", + "service": "auth", + } + + type sectionOptionPair struct { + section string + option string + } + + deprecatedOptions := map[sectionOptionPair]sectionOptionPair{ + {"security", "REVERSE_PROXY_AUTHENTICATION_USER"}: {"auth", "REVERSE_PROXY_AUTHENTICATION_HEADER"}, + {"auth", "ACTIVE_CODE_LIVE_MINUTES"}: {"auth", "ACTIVATE_CODE_LIVES"}, + {"auth", "RESET_PASSWD_CODE_LIVE_MINUTES"}: {"auth", "RESET_PASSWORD_CODE_LIVES"}, + {"auth", "ENABLE_CAPTCHA"}: {"auth", "ENABLE_REGISTRATION_CAPTCHA"}, + {"auth", "ENABLE_NOTIFY_MAIL"}: {"user", "ENABLE_EMAIL_NOTIFICATION"}, + {"auth", "REGISTER_EMAIL_CONFIRM"}: {"auth", "REQUIRE_EMAIL_CONFIRMATION"}, + {"session", "GC_INTERVAL_TIME"}: {"session", "GC_INTERVAL"}, + {"session", "SESSION_LIFE_TIME"}: {"session", "MAX_LIFE_TIME"}, + {"server", "ROOT_URL"}: {"server", "EXTERNAL_URL"}, + {"server", "LANDING_PAGE"}: {"server", "LANDING_URL"}, + {"database", "DB_TYPE"}: {"database", "TYPE"}, + {"database", "PASSWD"}: {"database", "PASSWORD"}, + } + + for oldSection, newSection := range deprecatedSections { + if cfg.Section(oldSection).KeyStrings() != nil { + log.Warn("section %s is deprecated, use %s instead", oldSection, newSection) + } + } + + for oldSectionOption, newSectionOption := range deprecatedOptions { + if cfg.Section(oldSectionOption.section).HasKey(oldSectionOption.option) { + log.Warn("option [%s] %s is deprecated, use [%s] %s instead", + oldSectionOption.section, oldSectionOption.option, + newSectionOption.section, newSectionOption.option) + } + } + + // TODO: get list of available section-option pair, give warning if there are + // exisisting (unused) section-option pair +} + // HookMode indicates whether program starts as Git server-side hook callback. // All operations should be done synchronously to prevent program exits before finishing. // From 4867a56d54209c84302d5e4452a6a582fc152a3b Mon Sep 17 00:00:00 2001 From: pikomonde <32364823+pikomonde@users.noreply.github.com> Date: Thu, 28 Mar 2024 21:36:06 +0700 Subject: [PATCH 2/6] add unit test --- internal/conf/static.go | 17 +++++-- internal/conf/static_test.go | 44 +++++++++++++++++ .../testdata/custom_warning_deprecated.ini | 49 +++++++++++++++++++ 3 files changed, 105 insertions(+), 5 deletions(-) create mode 100644 internal/conf/testdata/custom_warning_deprecated.ini diff --git a/internal/conf/static.go b/internal/conf/static.go index 38f0f64264c..0f6ddf9f823 100644 --- a/internal/conf/static.go +++ b/internal/conf/static.go @@ -5,6 +5,7 @@ package conf import ( + "fmt" "net/url" "os" "time" @@ -436,7 +437,8 @@ func handleDeprecated() { } // warnDeprecated warns about deprecated configuration sections and options. -func warnDeprecated(cfg *ini.File) { +func warnDeprecated(cfg *ini.File) []string { + // Deprecated sections and options deprecatedSections := map[string]string{ "mailer": "email", "service": "auth", @@ -462,22 +464,27 @@ func warnDeprecated(cfg *ini.File) { {"database", "PASSWD"}: {"database", "PASSWORD"}, } + var warnings []string + for oldSection, newSection := range deprecatedSections { if cfg.Section(oldSection).KeyStrings() != nil { - log.Warn("section %s is deprecated, use %s instead", oldSection, newSection) + warning := fmt.Sprintf("section %s is deprecated, use %s instead", oldSection, newSection) + log.Warn(warning) + warnings = append(warnings, warning) } } for oldSectionOption, newSectionOption := range deprecatedOptions { if cfg.Section(oldSectionOption.section).HasKey(oldSectionOption.option) { - log.Warn("option [%s] %s is deprecated, use [%s] %s instead", + warning := fmt.Sprintf("option [%s] %s is deprecated, use [%s] %s instead", oldSectionOption.section, oldSectionOption.option, newSectionOption.section, newSectionOption.option) + log.Warn(warning) + warnings = append(warnings, warning) } } - // TODO: get list of available section-option pair, give warning if there are - // exisisting (unused) section-option pair + return warnings } // HookMode indicates whether program starts as Git server-side hook callback. diff --git a/internal/conf/static_test.go b/internal/conf/static_test.go index e9b9cb022e7..07b97e2a685 100644 --- a/internal/conf/static_test.go +++ b/internal/conf/static_test.go @@ -5,9 +5,11 @@ package conf import ( + "sort" "testing" "github.com/stretchr/testify/assert" + "gopkg.in/ini.v1" ) func Test_i18n_DateLang(t *testing.T) { @@ -33,3 +35,45 @@ func Test_i18n_DateLang(t *testing.T) { }) } } + +func TestWarnDeprecated(t *testing.T) { + cfg := ini.Empty() + cfg.Section("mailer").NewKey("ENABLED", "true") + cfg.Section("service").NewKey("START_TYPE", "true") + cfg.Section("security").NewKey("REVERSE_PROXY_AUTHENTICATION_USER", "true") + cfg.Section("auth").NewKey("ACTIVE_CODE_LIVE_MINUTES", "10") + cfg.Section("auth").NewKey("RESET_PASSWD_CODE_LIVE_MINUTES", "10") + cfg.Section("auth").NewKey("ENABLE_CAPTCHA", "true") + cfg.Section("auth").NewKey("ENABLE_NOTIFY_MAIL", "true") + cfg.Section("auth").NewKey("REGISTER_EMAIL_CONFIRM", "true") + cfg.Section("session").NewKey("GC_INTERVAL_TIME", "10") + cfg.Section("session").NewKey("SESSION_LIFE_TIME", "10") + cfg.Section("server").NewKey("ROOT_URL", "true") + cfg.Section("server").NewKey("LANDING_PAGE", "true") + cfg.Section("database").NewKey("DB_TYPE", "true") + cfg.Section("database").NewKey("PASSWD", "true") + cfg.Section("other").NewKey("SHOW_FOOTER_BRANDING", "true") + cfg.Section("other").NewKey("SHOW_FOOTER_TEMPLATE_LOAD_TIME", "true") + + expectedWarning := []string{ + "option [auth] ACTIVE_CODE_LIVE_MINUTES is deprecated, use [auth] ACTIVATE_CODE_LIVES instead", + "option [auth] ENABLE_CAPTCHA is deprecated, use [auth] ENABLE_REGISTRATION_CAPTCHA instead", + "option [auth] ENABLE_NOTIFY_MAIL is deprecated, use [user] ENABLE_EMAIL_NOTIFICATION instead", + "option [auth] REGISTER_EMAIL_CONFIRM is deprecated, use [auth] REQUIRE_EMAIL_CONFIRMATION instead", + "option [auth] RESET_PASSWD_CODE_LIVE_MINUTES is deprecated, use [auth] RESET_PASSWORD_CODE_LIVES instead", + "option [database] DB_TYPE is deprecated, use [database] TYPE instead", + "option [database] PASSWD is deprecated, use [database] PASSWORD instead", + "option [security] REVERSE_PROXY_AUTHENTICATION_USER is deprecated, use [auth] REVERSE_PROXY_AUTHENTICATION_HEADER instead", + "option [session] GC_INTERVAL_TIME is deprecated, use [session] GC_INTERVAL instead", + "option [session] SESSION_LIFE_TIME is deprecated, use [session] MAX_LIFE_TIME instead", + "section mailer is deprecated, use email instead", + "section service is deprecated, use auth instead", + "option [server] ROOT_URL is deprecated, use [server] EXTERNAL_URL instead", + "option [server] LANDING_PAGE is deprecated, use [server] LANDING_URL instead", + } + actualWarning := warnDeprecated(cfg) + sort.Strings(expectedWarning) + sort.Strings(actualWarning) + assert.Equal(t, expectedWarning, actualWarning) + +} diff --git a/internal/conf/testdata/custom_warning_deprecated.ini b/internal/conf/testdata/custom_warning_deprecated.ini new file mode 100644 index 00000000000..279e291cd03 --- /dev/null +++ b/internal/conf/testdata/custom_warning_deprecated.ini @@ -0,0 +1,49 @@ +BRAND_NAME = Testing +RUN_MODE = test + +[server] +ROOT_URL = http://localhost:3080/ +APP_DATA_PATH = /tmp/data +SSH_ROOT_PATH = /tmp +SSH_KEY_TEST_PATH = /tmp/ssh-key-test +MINIMUM_KEY_SIZE_CHECK = true +LANDING_PAGE = /explore + +[repository] +ROOT = /tmp/gogs-repositories + +[repository.upload] +TEMP_PATH = /tmp/uploads + +[database] +DB_TYPE = sqlite +PASSWD = 12345678 +PATH = /tmp/gogs.db + +[mailer] +ENABLED = true +PASSWORD = 87654321 + +[security] +REVERSE_PROXY_AUTHENTICATION_USER=X-FORWARDED-FOR + +[auth] +ACTIVE_CODE_LIVE_MINUTES = 10 +RESET_PASSWD_CODE_LIVE_MINUTES = 10 +REGISTER_EMAIL_CONFIRM = true +ENABLE_CAPTCHA = true +ENABLE_NOTIFY_MAIL = true + +[user] +ENABLE_EMAIL_NOTIFICATION = true + +[session] +GC_INTERVAL_TIME = 10 +SESSION_LIFE_TIME = 10 + +[attachment] +PATH = /tmp/attachments + +[picture] +AVATAR_UPLOAD_PATH = /tmp/avatars +REPOSITORY_AVATAR_UPLOAD_PATH = /tmp/repo-avatars From d8fe50240d50a555c554facc8267066c13869e35 Mon Sep 17 00:00:00 2001 From: pikomonde <32364823+pikomonde@users.noreply.github.com> Date: Fri, 29 Mar 2024 20:59:45 +0700 Subject: [PATCH 3/6] add NOTE to delete warnDeprecated func --- internal/conf/static.go | 1 + 1 file changed, 1 insertion(+) diff --git a/internal/conf/static.go b/internal/conf/static.go index 0f6ddf9f823..8ec1d2f9ad6 100644 --- a/internal/conf/static.go +++ b/internal/conf/static.go @@ -437,6 +437,7 @@ func handleDeprecated() { } // warnDeprecated warns about deprecated configuration sections and options. +// NOTE: Delete this function after 0.14.0 is released func warnDeprecated(cfg *ini.File) []string { // Deprecated sections and options deprecatedSections := map[string]string{ From 86c75a015f7437b39285116daf80986b6d8086ee Mon Sep 17 00:00:00 2001 From: pikomonde <32364823+pikomonde@users.noreply.github.com> Date: Fri, 29 Mar 2024 23:02:18 +0700 Subject: [PATCH 4/6] solve todo --- internal/conf/static.go | 34 +++++++++++++++++++++++++++++++--- internal/conf/static_test.go | 23 ++++++++++++++++++++++- 2 files changed, 53 insertions(+), 4 deletions(-) diff --git a/internal/conf/static.go b/internal/conf/static.go index 8ec1d2f9ad6..4cdb9173fff 100644 --- a/internal/conf/static.go +++ b/internal/conf/static.go @@ -11,6 +11,7 @@ import ( "time" "github.com/gogs/go-libravatar" + "gogs.io/gogs/conf" "gopkg.in/ini.v1" log "unknwon.dev/clog/v2" ) @@ -438,7 +439,7 @@ func handleDeprecated() { // warnDeprecated warns about deprecated configuration sections and options. // NOTE: Delete this function after 0.14.0 is released -func warnDeprecated(cfg *ini.File) []string { +func warnDeprecated(currentConfig *ini.File) []string { // Deprecated sections and options deprecatedSections := map[string]string{ "mailer": "email", @@ -468,7 +469,7 @@ func warnDeprecated(cfg *ini.File) []string { var warnings []string for oldSection, newSection := range deprecatedSections { - if cfg.Section(oldSection).KeyStrings() != nil { + if currentConfig.Section(oldSection).KeyStrings() != nil { warning := fmt.Sprintf("section %s is deprecated, use %s instead", oldSection, newSection) log.Warn(warning) warnings = append(warnings, warning) @@ -476,7 +477,7 @@ func warnDeprecated(cfg *ini.File) []string { } for oldSectionOption, newSectionOption := range deprecatedOptions { - if cfg.Section(oldSectionOption.section).HasKey(oldSectionOption.option) { + if currentConfig.Section(oldSectionOption.section).HasKey(oldSectionOption.option) { warning := fmt.Sprintf("option [%s] %s is deprecated, use [%s] %s instead", oldSectionOption.section, oldSectionOption.option, newSectionOption.section, newSectionOption.option) @@ -485,6 +486,33 @@ func warnDeprecated(cfg *ini.File) []string { } } + // Compare available config with current config + availableConfigFS := conf.Files + availableConfigData, err := availableConfigFS.ReadFile("app.ini") + if err != nil { + log.Error("Failed to open available config: %v", err) + return warnings + } + + availableConfig, err := ini.LoadSources(ini.LoadOptions{ + IgnoreInlineComment: true, + }, availableConfigData) + if err != nil { + log.Error("Failed to parse available config: %v", err) + return warnings + } + + for _, currentSection := range currentConfig.Sections() { + for _, currentOption := range currentSection.Keys() { + if !availableConfig.Section(currentSection.Name()).HasKey(currentOption.Name()) { + warning := fmt.Sprintf("option [%s] %s is not in the available config", + currentSection.Name(), currentOption.Name()) + log.Warn(warning) + warnings = append(warnings, warning) + } + } + } + return warnings } diff --git a/internal/conf/static_test.go b/internal/conf/static_test.go index 07b97e2a685..39f202e276f 100644 --- a/internal/conf/static_test.go +++ b/internal/conf/static_test.go @@ -54,8 +54,10 @@ func TestWarnDeprecated(t *testing.T) { cfg.Section("database").NewKey("PASSWD", "true") cfg.Section("other").NewKey("SHOW_FOOTER_BRANDING", "true") cfg.Section("other").NewKey("SHOW_FOOTER_TEMPLATE_LOAD_TIME", "true") + cfg.Section("email").NewKey("ENABLED", "true") + cfg.Section("email").NewKey("key_not_exist", "true") - expectedWarning := []string{ + expectedDeprecatedWarning := []string{ "option [auth] ACTIVE_CODE_LIVE_MINUTES is deprecated, use [auth] ACTIVATE_CODE_LIVES instead", "option [auth] ENABLE_CAPTCHA is deprecated, use [auth] ENABLE_REGISTRATION_CAPTCHA instead", "option [auth] ENABLE_NOTIFY_MAIL is deprecated, use [user] ENABLE_EMAIL_NOTIFICATION instead", @@ -71,6 +73,25 @@ func TestWarnDeprecated(t *testing.T) { "option [server] ROOT_URL is deprecated, use [server] EXTERNAL_URL instead", "option [server] LANDING_PAGE is deprecated, use [server] LANDING_URL instead", } + expectedUnusedWarning := []string{ + "option [auth] ACTIVE_CODE_LIVE_MINUTES is not in the available config", + "option [auth] ENABLE_CAPTCHA is not in the available config", + "option [auth] ENABLE_NOTIFY_MAIL is not in the available config", + "option [auth] REGISTER_EMAIL_CONFIRM is not in the available config", + "option [auth] RESET_PASSWD_CODE_LIVE_MINUTES is not in the available config", + "option [database] DB_TYPE is not in the available config", + "option [database] PASSWD is not in the available config", + "option [email] key_not_exist is not in the available config", + "option [mailer] ENABLED is not in the available config", + "option [security] REVERSE_PROXY_AUTHENTICATION_USER is not in the available config", + "option [server] LANDING_PAGE is not in the available config", + "option [server] ROOT_URL is not in the available config", + "option [service] START_TYPE is not in the available config", + "option [session] GC_INTERVAL_TIME is not in the available config", + "option [session] SESSION_LIFE_TIME is not in the available config", + } + + expectedWarning := append(expectedDeprecatedWarning, expectedUnusedWarning...) actualWarning := warnDeprecated(cfg) sort.Strings(expectedWarning) sort.Strings(actualWarning) From e360b83802de6cdae86a13f7ef024364bcc31ba4 Mon Sep 17 00:00:00 2001 From: pikomonde <32364823+pikomonde@users.noreply.github.com> Date: Fri, 29 Mar 2024 23:08:42 +0700 Subject: [PATCH 5/6] remove unused config files --- .../testdata/custom_warning_deprecated.ini | 49 ------------------- 1 file changed, 49 deletions(-) delete mode 100644 internal/conf/testdata/custom_warning_deprecated.ini diff --git a/internal/conf/testdata/custom_warning_deprecated.ini b/internal/conf/testdata/custom_warning_deprecated.ini deleted file mode 100644 index 279e291cd03..00000000000 --- a/internal/conf/testdata/custom_warning_deprecated.ini +++ /dev/null @@ -1,49 +0,0 @@ -BRAND_NAME = Testing -RUN_MODE = test - -[server] -ROOT_URL = http://localhost:3080/ -APP_DATA_PATH = /tmp/data -SSH_ROOT_PATH = /tmp -SSH_KEY_TEST_PATH = /tmp/ssh-key-test -MINIMUM_KEY_SIZE_CHECK = true -LANDING_PAGE = /explore - -[repository] -ROOT = /tmp/gogs-repositories - -[repository.upload] -TEMP_PATH = /tmp/uploads - -[database] -DB_TYPE = sqlite -PASSWD = 12345678 -PATH = /tmp/gogs.db - -[mailer] -ENABLED = true -PASSWORD = 87654321 - -[security] -REVERSE_PROXY_AUTHENTICATION_USER=X-FORWARDED-FOR - -[auth] -ACTIVE_CODE_LIVE_MINUTES = 10 -RESET_PASSWD_CODE_LIVE_MINUTES = 10 -REGISTER_EMAIL_CONFIRM = true -ENABLE_CAPTCHA = true -ENABLE_NOTIFY_MAIL = true - -[user] -ENABLE_EMAIL_NOTIFICATION = true - -[session] -GC_INTERVAL_TIME = 10 -SESSION_LIFE_TIME = 10 - -[attachment] -PATH = /tmp/attachments - -[picture] -AVATAR_UPLOAD_PATH = /tmp/avatars -REPOSITORY_AVATAR_UPLOAD_PATH = /tmp/repo-avatars From 4a96c41db44d68e413fc51f92e520694b32fc4ae Mon Sep 17 00:00:00 2001 From: pikomonde <32364823+pikomonde@users.noreply.github.com> Date: Sat, 30 Mar 2024 14:51:30 +0700 Subject: [PATCH 6/6] add docs --- internal/conf/static_test.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/internal/conf/static_test.go b/internal/conf/static_test.go index 39f202e276f..a789028a002 100644 --- a/internal/conf/static_test.go +++ b/internal/conf/static_test.go @@ -36,6 +36,9 @@ func Test_i18n_DateLang(t *testing.T) { } } +// TestWarnDeprecated tests the warnDeprecated function. +// It creates a new ini.File, adds some sections and keys, +// and then checks if the expected warnings are returned by the function. func TestWarnDeprecated(t *testing.T) { cfg := ini.Empty() cfg.Section("mailer").NewKey("ENABLED", "true")
Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.
Alternative Proxies: