Initial Baby Step to refactoring settings from global vars to instance (#11777)

* wip: start on refactoring settings

* settings: progress on settings refactor

* refactor: progress on settings refactoring

* fix: fixed failing test

* settings: moved smtp settings from global to instance
This commit is contained in:
Torkel Ödegaard
2018-04-30 16:21:04 +02:00
committed by GitHub
parent fc718b8a9a
commit fa7d7ed5df
28 changed files with 263 additions and 203 deletions

View File

@@ -137,7 +137,7 @@ var (
SessionConnMaxLifetime int64
// Global setting objects.
Cfg *ini.File
Raw *ini.File
ConfRootPath string
IsWindows bool
@@ -160,9 +160,6 @@ var (
LdapConfigFile string
LdapAllowSignup = true
// SMTP email settings
Smtp SmtpSettings
// QUOTA
Quota QuotaSettings
@@ -187,6 +184,16 @@ var (
ImageUploadProvider string
)
type Cfg struct {
Raw *ini.File
// SMTP email settings
Smtp SmtpSettings
ImagesDir string
DisableBruteForceLoginProtection bool
}
type CommandLineArgs struct {
Config string
HomePath string
@@ -228,9 +235,9 @@ func shouldRedactURLKey(s string) bool {
return strings.Contains(uppercased, "DATABASE_URL")
}
func applyEnvVariableOverrides() error {
func applyEnvVariableOverrides(file *ini.File) error {
appliedEnvOverrides = make([]string, 0)
for _, section := range Cfg.Sections() {
for _, section := range file.Sections() {
for _, key := range section.Keys() {
sectionName := strings.ToUpper(strings.Replace(section.Name(), ".", "_", -1))
keyName := strings.ToUpper(strings.Replace(key.Name(), ".", "_", -1))
@@ -264,9 +271,9 @@ func applyEnvVariableOverrides() error {
return nil
}
func applyCommandLineDefaultProperties(props map[string]string) {
func applyCommandLineDefaultProperties(props map[string]string, file *ini.File) {
appliedCommandLineProperties = make([]string, 0)
for _, section := range Cfg.Sections() {
for _, section := range file.Sections() {
for _, key := range section.Keys() {
keyString := fmt.Sprintf("default.%s.%s", section.Name(), key.Name())
value, exists := props[keyString]
@@ -281,8 +288,8 @@ func applyCommandLineDefaultProperties(props map[string]string) {
}
}
func applyCommandLineProperties(props map[string]string) {
for _, section := range Cfg.Sections() {
func applyCommandLineProperties(props map[string]string, file *ini.File) {
for _, section := range file.Sections() {
sectionName := section.Name() + "."
if section.Name() == ini.DEFAULT_SECTION {
sectionName = ""
@@ -341,15 +348,15 @@ func evalEnvVarExpression(value string) string {
})
}
func evalConfigValues() {
for _, section := range Cfg.Sections() {
func evalConfigValues(file *ini.File) {
for _, section := range file.Sections() {
for _, key := range section.Keys() {
key.SetValue(evalEnvVarExpression(key.Value()))
}
}
}
func loadSpecifedConfigFile(configFile string) error {
func loadSpecifedConfigFile(configFile string, masterFile *ini.File) error {
if configFile == "" {
configFile = filepath.Join(HomePath, CustomInitPath)
// return without error if custom file does not exist
@@ -371,9 +378,9 @@ func loadSpecifedConfigFile(configFile string) error {
continue
}
defaultSec, err := Cfg.GetSection(section.Name())
defaultSec, err := masterFile.GetSection(section.Name())
if err != nil {
defaultSec, _ = Cfg.NewSection(section.Name())
defaultSec, _ = masterFile.NewSection(section.Name())
}
defaultKey, err := defaultSec.GetKey(key.Name())
if err != nil {
@@ -387,7 +394,7 @@ func loadSpecifedConfigFile(configFile string) error {
return nil
}
func loadConfiguration(args *CommandLineArgs) error {
func loadConfiguration(args *CommandLineArgs) (*ini.File, error) {
var err error
// load config defaults
@@ -401,44 +408,44 @@ func loadConfiguration(args *CommandLineArgs) error {
}
// load defaults
Cfg, err = ini.Load(defaultConfigFile)
parsedFile, err := ini.Load(defaultConfigFile)
if err != nil {
fmt.Println(fmt.Sprintf("Failed to parse defaults.ini, %v", err))
os.Exit(1)
return err
return nil, err
}
Cfg.BlockMode = false
parsedFile.BlockMode = false
// command line props
commandLineProps := getCommandLineProperties(args.Args)
// load default overrides
applyCommandLineDefaultProperties(commandLineProps)
applyCommandLineDefaultProperties(commandLineProps, parsedFile)
// load specified config file
err = loadSpecifedConfigFile(args.Config)
err = loadSpecifedConfigFile(args.Config, parsedFile)
if err != nil {
initLogging()
initLogging(parsedFile)
log.Fatal(3, err.Error())
}
// apply environment overrides
err = applyEnvVariableOverrides()
err = applyEnvVariableOverrides(parsedFile)
if err != nil {
return err
return nil, err
}
// apply command line overrides
applyCommandLineProperties(commandLineProps)
applyCommandLineProperties(commandLineProps, parsedFile)
// evaluate config values containing environment variables
evalConfigValues()
evalConfigValues(parsedFile)
// update data path and logging config
DataPath = makeAbsolute(Cfg.Section("paths").Key("data").String(), HomePath)
initLogging()
DataPath = makeAbsolute(parsedFile.Section("paths").Key("data").String(), HomePath)
initLogging(parsedFile)
return err
return parsedFile, err
}
func pathExists(path string) bool {
@@ -484,23 +491,33 @@ func validateStaticRootPath() error {
return nil
}
func NewConfigContext(args *CommandLineArgs) error {
func NewCfg() *Cfg {
return &Cfg{}
}
func (cfg *Cfg) Load(args *CommandLineArgs) error {
setHomePath(args)
err := loadConfiguration(args)
iniFile, err := loadConfiguration(args)
if err != nil {
return err
}
cfg.Raw = iniFile
// Temporary keep global, to make refactor in steps
Raw = cfg.Raw
ApplicationName = "Grafana"
if Enterprise {
ApplicationName += " Enterprise"
}
Env = Cfg.Section("").Key("app_mode").MustString("development")
InstanceName = Cfg.Section("").Key("instance_name").MustString("unknown_instance_name")
PluginsPath = makeAbsolute(Cfg.Section("paths").Key("plugins").String(), HomePath)
ProvisioningPath = makeAbsolute(Cfg.Section("paths").Key("provisioning").String(), HomePath)
server := Cfg.Section("server")
Env = iniFile.Section("").Key("app_mode").MustString("development")
InstanceName = iniFile.Section("").Key("instance_name").MustString("unknown_instance_name")
PluginsPath = makeAbsolute(iniFile.Section("paths").Key("plugins").String(), HomePath)
ProvisioningPath = makeAbsolute(iniFile.Section("paths").Key("provisioning").String(), HomePath)
server := iniFile.Section("server")
AppUrl, AppSubUrl = parseAppUrlAndSubUrl(server)
Protocol = HTTP
@@ -528,27 +545,28 @@ func NewConfigContext(args *CommandLineArgs) error {
}
// read data proxy settings
dataproxy := Cfg.Section("dataproxy")
dataproxy := iniFile.Section("dataproxy")
DataProxyLogging = dataproxy.Key("logging").MustBool(false)
// read security settings
security := Cfg.Section("security")
security := iniFile.Section("security")
SecretKey = security.Key("secret_key").String()
LogInRememberDays = security.Key("login_remember_days").MustInt()
CookieUserName = security.Key("cookie_username").String()
CookieRememberName = security.Key("cookie_remember_name").String()
DisableGravatar = security.Key("disable_gravatar").MustBool(true)
DisableBruteForceLoginProtection = security.Key("disable_brute_force_login_protection").MustBool(false)
cfg.DisableBruteForceLoginProtection = security.Key("disable_brute_force_login_protection").MustBool(false)
DisableBruteForceLoginProtection = cfg.DisableBruteForceLoginProtection
// read snapshots settings
snapshots := Cfg.Section("snapshots")
snapshots := iniFile.Section("snapshots")
ExternalSnapshotUrl = snapshots.Key("external_snapshot_url").String()
ExternalSnapshotName = snapshots.Key("external_snapshot_name").String()
ExternalEnabled = snapshots.Key("external_enabled").MustBool(true)
SnapShotRemoveExpired = snapshots.Key("snapshot_remove_expired").MustBool(true)
// read dashboard settings
dashboards := Cfg.Section("dashboards")
dashboards := iniFile.Section("dashboards")
DashboardVersionsToKeep = dashboards.Key("versions_to_keep").MustInt(20)
// read data source proxy white list
@@ -561,7 +579,7 @@ func NewConfigContext(args *CommandLineArgs) error {
AdminUser = security.Key("admin_user").String()
AdminPassword = security.Key("admin_password").String()
users := Cfg.Section("users")
users := iniFile.Section("users")
AllowUserSignUp = users.Key("allow_sign_up").MustBool(true)
AllowUserOrgCreate = users.Key("allow_org_create").MustBool(true)
AutoAssignOrg = users.Key("auto_assign_org").MustBool(true)
@@ -575,17 +593,17 @@ func NewConfigContext(args *CommandLineArgs) error {
ViewersCanEdit = users.Key("viewers_can_edit").MustBool(false)
// auth
auth := Cfg.Section("auth")
auth := iniFile.Section("auth")
DisableLoginForm = auth.Key("disable_login_form").MustBool(false)
DisableSignoutMenu = auth.Key("disable_signout_menu").MustBool(false)
// anonymous access
AnonymousEnabled = Cfg.Section("auth.anonymous").Key("enabled").MustBool(false)
AnonymousOrgName = Cfg.Section("auth.anonymous").Key("org_name").String()
AnonymousOrgRole = Cfg.Section("auth.anonymous").Key("org_role").String()
AnonymousEnabled = iniFile.Section("auth.anonymous").Key("enabled").MustBool(false)
AnonymousOrgName = iniFile.Section("auth.anonymous").Key("org_name").String()
AnonymousOrgRole = iniFile.Section("auth.anonymous").Key("org_role").String()
// auth proxy
authProxy := Cfg.Section("auth.proxy")
authProxy := iniFile.Section("auth.proxy")
AuthProxyEnabled = authProxy.Key("enabled").MustBool(false)
AuthProxyHeaderName = authProxy.Key("header_name").String()
AuthProxyHeaderProperty = authProxy.Key("header_property").String()
@@ -594,63 +612,64 @@ func NewConfigContext(args *CommandLineArgs) error {
AuthProxyWhitelist = authProxy.Key("whitelist").String()
// basic auth
authBasic := Cfg.Section("auth.basic")
authBasic := iniFile.Section("auth.basic")
BasicAuthEnabled = authBasic.Key("enabled").MustBool(true)
// global plugin settings
PluginAppsSkipVerifyTLS = Cfg.Section("plugins").Key("app_tls_skip_verify_insecure").MustBool(false)
PluginAppsSkipVerifyTLS = iniFile.Section("plugins").Key("app_tls_skip_verify_insecure").MustBool(false)
// PhantomJS rendering
ImagesDir = filepath.Join(DataPath, "png")
cfg.ImagesDir = filepath.Join(DataPath, "png")
ImagesDir = cfg.ImagesDir
PhantomDir = filepath.Join(HomePath, "tools/phantomjs")
analytics := Cfg.Section("analytics")
analytics := iniFile.Section("analytics")
ReportingEnabled = analytics.Key("reporting_enabled").MustBool(true)
CheckForUpdates = analytics.Key("check_for_updates").MustBool(true)
GoogleAnalyticsId = analytics.Key("google_analytics_ua_id").String()
GoogleTagManagerId = analytics.Key("google_tag_manager_id").String()
ldapSec := Cfg.Section("auth.ldap")
ldapSec := iniFile.Section("auth.ldap")
LdapEnabled = ldapSec.Key("enabled").MustBool(false)
LdapConfigFile = ldapSec.Key("config_file").String()
LdapAllowSignup = ldapSec.Key("allow_sign_up").MustBool(true)
alerting := Cfg.Section("alerting")
alerting := iniFile.Section("alerting")
AlertingEnabled = alerting.Key("enabled").MustBool(true)
ExecuteAlerts = alerting.Key("execute_alerts").MustBool(true)
explore := Cfg.Section("explore")
explore := iniFile.Section("explore")
ExploreEnabled = explore.Key("enabled").MustBool(false)
readSessionConfig()
readSmtpSettings()
readQuotaSettings()
cfg.readSessionConfig()
cfg.readSmtpSettings()
cfg.readQuotaSettings()
if VerifyEmailEnabled && !Smtp.Enabled {
if VerifyEmailEnabled && !cfg.Smtp.Enabled {
log.Warn("require_email_validation is enabled but smtp is disabled")
}
// check old key name
GrafanaComUrl = Cfg.Section("grafana_net").Key("url").MustString("")
GrafanaComUrl = iniFile.Section("grafana_net").Key("url").MustString("")
if GrafanaComUrl == "" {
GrafanaComUrl = Cfg.Section("grafana_com").Key("url").MustString("https://grafana.com")
GrafanaComUrl = iniFile.Section("grafana_com").Key("url").MustString("https://grafana.com")
}
imageUploadingSection := Cfg.Section("external_image_storage")
imageUploadingSection := iniFile.Section("external_image_storage")
ImageUploadProvider = imageUploadingSection.Key("provider").MustString("")
return nil
}
func readSessionConfig() {
sec := Cfg.Section("session")
func (cfg *Cfg) readSessionConfig() {
sec := cfg.Raw.Section("session")
SessionOptions = session.Options{}
SessionOptions.Provider = sec.Key("provider").In("memory", []string{"memory", "file", "redis", "mysql", "postgres", "memcache"})
SessionOptions.ProviderConfig = strings.Trim(sec.Key("provider_config").String(), "\" ")
SessionOptions.CookieName = sec.Key("cookie_name").MustString("grafana_sess")
SessionOptions.CookiePath = AppSubUrl
SessionOptions.Secure = sec.Key("cookie_secure").MustBool()
SessionOptions.Gclifetime = Cfg.Section("session").Key("gc_interval_time").MustInt64(86400)
SessionOptions.Maxlifetime = Cfg.Section("session").Key("session_life_time").MustInt64(86400)
SessionOptions.Gclifetime = cfg.Raw.Section("session").Key("gc_interval_time").MustInt64(86400)
SessionOptions.Maxlifetime = cfg.Raw.Section("session").Key("session_life_time").MustInt64(86400)
SessionOptions.IDLength = 16
if SessionOptions.Provider == "file" {
@@ -662,21 +681,21 @@ func readSessionConfig() {
SessionOptions.CookiePath = "/"
}
SessionConnMaxLifetime = Cfg.Section("session").Key("conn_max_lifetime").MustInt64(14400)
SessionConnMaxLifetime = cfg.Raw.Section("session").Key("conn_max_lifetime").MustInt64(14400)
}
func initLogging() {
func initLogging(file *ini.File) {
// split on comma
LogModes = strings.Split(Cfg.Section("log").Key("mode").MustString("console"), ",")
LogModes = strings.Split(file.Section("log").Key("mode").MustString("console"), ",")
// also try space
if len(LogModes) == 1 {
LogModes = strings.Split(Cfg.Section("log").Key("mode").MustString("console"), " ")
LogModes = strings.Split(file.Section("log").Key("mode").MustString("console"), " ")
}
LogsPath = makeAbsolute(Cfg.Section("paths").Key("logs").String(), HomePath)
log.ReadLoggingConfig(LogModes, LogsPath, Cfg)
LogsPath = makeAbsolute(file.Section("paths").Key("logs").String(), HomePath)
log.ReadLoggingConfig(LogModes, LogsPath, file)
}
func LogConfigurationInfo() {
func (cfg *Cfg) LogConfigSources() {
var text bytes.Buffer
for _, file := range configFiles {