From 95305e7e118350564c70dd4aac8ac159f9e28b81 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torkel=20=C3=96degaard?= Date: Tue, 27 Jan 2015 10:09:54 +0100 Subject: [PATCH] Changed from goconfig to its new counter part go-ini --- conf/grafana.ini | 17 +++-- pkg/api/api.go | 2 +- pkg/api/login.go | 9 +++ pkg/services/sqlstore/sqlstore.go | 16 +++-- pkg/setting/setting.go | 107 ++++++++++++++++++------------ pkg/social/social.go | 13 ++-- 6 files changed, 99 insertions(+), 65 deletions(-) diff --git a/conf/grafana.ini b/conf/grafana.ini index 64a8bb84ded..324ab39a6f7 100644 --- a/conf/grafana.ini +++ b/conf/grafana.ini @@ -1,5 +1,5 @@ app_name = Grafana -app_mode = development +app_mode = production [server] protocol = http @@ -35,19 +35,22 @@ session_id_hashfunc = sha1 ; Session hash key, default is use random string session_id_hashkey = -[admin] +[security] ; default admin user, created on startup -user = admin +admin_user = admin ; default admin password, can be changed before first start of grafana, or in profile settings -password = admin +admin_password = admin +; used for sig +secret_key = !#@FDEWREWR&*( +; Auto-login remember days +login_remember_days = 7 +cookie_username = grafana_user +cookie_remember_name = grafana_remember [auth] anonymous = false anonymous_account_id = -[auth.grafana] -enabled = true - [auth.github] enabled = false client_id = some_id diff --git a/pkg/api/api.go b/pkg/api/api.go index 0142298b38a..2fba7c0656e 100644 --- a/pkg/api/api.go +++ b/pkg/api/api.go @@ -22,7 +22,7 @@ func Register(r *macaron.Macaron) { r.Post("/logout", LogoutPost) r.Post("/login", bind(dtos.LoginCommand{}), LoginPost) r.Get("/login/:name", OAuthLogin) - r.Get("/login", Index) + r.Get("/login", LoginView) // authed views r.Get("/profile/", reqSignedIn, Index) diff --git a/pkg/api/login.go b/pkg/api/login.go index 3b53d1a2d24..6f337283363 100644 --- a/pkg/api/login.go +++ b/pkg/api/login.go @@ -9,6 +9,15 @@ import ( "github.com/torkelo/grafana-pro/pkg/util" ) +func LoginView(c *middleware.Context) { + if err := setIndexViewData(c); err != nil { + c.Handle(500, "Failed to get settings", err) + return + } + + c.HTML(200, "index") +} + func LoginPost(c *middleware.Context, cmd dtos.LoginCommand) { userQuery := m.GetUserByLoginQuery{LoginOrEmail: cmd.User} diff --git a/pkg/services/sqlstore/sqlstore.go b/pkg/services/sqlstore/sqlstore.go index 81c51e43c2b..c47c2a4e90c 100644 --- a/pkg/services/sqlstore/sqlstore.go +++ b/pkg/services/sqlstore/sqlstore.go @@ -136,18 +136,20 @@ func getEngine() (*xorm.Engine, error) { } func LoadConfig() { - DbCfg.Type = setting.Cfg.MustValue("database", "type") + sec := setting.Cfg.Section("database") + + DbCfg.Type = sec.Key("type").String() if DbCfg.Type == "sqlite3" { UseSQLite3 = true } - DbCfg.Host = setting.Cfg.MustValue("database", "host") - DbCfg.Name = setting.Cfg.MustValue("database", "name") - DbCfg.User = setting.Cfg.MustValue("database", "user") + DbCfg.Host = sec.Key("host").String() + DbCfg.Name = sec.Key("name").String() + DbCfg.User = sec.Key("user").String() if len(DbCfg.Pwd) == 0 { - DbCfg.Pwd = setting.Cfg.MustValue("database", "password") + DbCfg.Pwd = sec.Key("password").String() } - DbCfg.SslMode = setting.Cfg.MustValue("database", "ssl_mode") - DbCfg.Path = setting.Cfg.MustValue("database", "path", "data/grafana.db") + DbCfg.SslMode = sec.Key("ssl_mode").String() + DbCfg.Path = sec.Key("path").MustString("data/grafana.db") } type dbTransactionFunc func(sess *xorm.Session) error diff --git a/pkg/setting/setting.go b/pkg/setting/setting.go index 382d67284c5..7f90398bada 100644 --- a/pkg/setting/setting.go +++ b/pkg/setting/setting.go @@ -12,8 +12,8 @@ import ( "strings" "github.com/Unknwon/com" - "github.com/Unknwon/goconfig" "github.com/macaron-contrib/session" + "gopkg.in/ini.v1" "github.com/torkelo/grafana-pro/pkg/log" ) @@ -58,6 +58,12 @@ var ( StaticRootPath string EnableGzip bool + // Security settings. + SecretKey string + LogInRememberDays int + CookieUserName string + CookieRememberName string + // Http auth AdminUser string AdminPassword string @@ -69,7 +75,7 @@ var ( // Global setting objects. WorkDir string - Cfg *goconfig.ConfigFile + Cfg *ini.File ConfRootPath string CustomPath string // Custom directory path. ProdMode bool @@ -118,85 +124,98 @@ func findConfigFiles() []string { return filenames } +func parseAppUrlAndSubUrl(section *ini.Section) (string, string) { + appUrl := section.Key("root_url").MustString("http://localhost:3000/") + if appUrl[len(appUrl)-1] != '/' { + appUrl += "/" + } + + // Check if has app suburl. + url, err := url.Parse(AppUrl) + if err != nil { + log.Fatal(4, "Invalid root_url(%s): %s", appUrl, err) + } + appSubUrl := strings.TrimSuffix(url.Path, "/") + + return appUrl, appSubUrl +} + func NewConfigContext() { configFiles := findConfigFiles() //log.Info("Loading config files: %v", configFiles) var err error - Cfg, err = goconfig.LoadConfigFile(configFiles[0]) - if err != nil { - log.Fatal(4, "Fail to parse config file, error: %v", err) - } + for i, file := range configFiles { + if i == 0 { + Cfg, err = ini.Load(configFiles[i]) + } else { + err = Cfg.Append(configFiles[i]) + } - if len(configFiles) > 1 { - err = Cfg.AppendFiles(configFiles[1:]...) if err != nil { - log.Fatal(4, "Fail to parse config file, error: %v", err) + log.Fatal(4, "Fail to parse config file: %v, error: %v", file, err) } } - AppName = Cfg.MustValue("", "app_name", "Grafana") - AppUrl = Cfg.MustValue("server", "root_url", "http://localhost:3000/") - if AppUrl[len(AppUrl)-1] != '/' { - AppUrl += "/" - } + AppName = Cfg.Section("").Key("app_name").MustString("Grafana") + Env = Cfg.Section("").Key("app_mode").MustString("development") - // Check if has app suburl. - url, err := url.Parse(AppUrl) - if err != nil { - log.Fatal(4, "Invalid root_url(%s): %s", AppUrl, err) - } - - AppSubUrl = strings.TrimSuffix(url.Path, "/") + server := Cfg.Section("server") + AppUrl, AppSubUrl = parseAppUrlAndSubUrl(server) Protocol = HTTP - if Cfg.MustValue("server", "protocol") == "https" { + if server.Key("protocol").MustString("http") == "https" { Protocol = HTTPS - CertFile = Cfg.MustValue("server", "cert_file") - KeyFile = Cfg.MustValue("server", "key_file") + CertFile = server.Key("cert_file").String() + KeyFile = server.Key("cert_file").String() } - Domain = Cfg.MustValue("server", "domain", "localhost") - HttpAddr = Cfg.MustValue("server", "http_addr", "0.0.0.0") - HttpPort = Cfg.MustValue("server", "http_port", "3000") + + Domain = server.Key("domain").MustString("localhost") + HttpAddr = server.Key("http_addr").MustString("0.0.0.0") + HttpPort = server.Key("http_port").MustString("3000") port := os.Getenv("PORT") if port != "" { HttpPort = port } - StaticRootPath = Cfg.MustValue("server", "static_root_path", path.Join(WorkDir, "webapp")) - RouterLogging = Cfg.MustBool("server", "router_logging", false) - EnableGzip = Cfg.MustBool("server", "enable_gzip") + StaticRootPath = server.Key("static_root_path").MustString(path.Join(WorkDir, "webapp")) + RouterLogging = server.Key("router_logging").MustBool(false) + EnableGzip = server.Key("enable_gzip").MustBool(false) + + security := Cfg.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() // Http auth - AdminUser = Cfg.MustValue("admin", "user", "admin") - AdminPassword = Cfg.MustValue("admin", "password", "admin") - Anonymous = Cfg.MustBool("auth", "anonymous", false) - AnonymousAccountId = Cfg.MustInt64("auth", "anonymous_account_id", 0) + AdminUser = security.Key("admin_user").String() + AdminPassword = security.Key("admin_password").String() - if Anonymous && AnonymousAccountId == 0 { - log.Fatal(3, "Must specify account id for anonymous access") - } + // Anonymous = Cfg.MustBool("auth", "anonymous", false) + // AnonymousAccountId = Cfg.MustInt64("auth", "anonymous_account_id", 0) // PhantomJS rendering ImagesDir = "data/png" PhantomDir = "vendor/phantomjs" - LogRootPath = Cfg.MustValue("log", "root_path", path.Join(WorkDir, "/data/log")) + LogRootPath = Cfg.Section("log").Key("root_path").MustString(path.Join(WorkDir, "/data/log")) readSessionConfig() } func readSessionConfig() { + sec := Cfg.Section("session") SessionOptions = session.Options{} - SessionOptions.Provider = Cfg.MustValueRange("session", "provider", "memory", []string{"memory", "file"}) - SessionOptions.ProviderConfig = strings.Trim(Cfg.MustValue("session", "provider_config"), "\" ") - SessionOptions.CookieName = Cfg.MustValue("session", "cookie_name", "grafana_pro_sess") + SessionOptions.Provider = sec.Key("provider").In("memory", []string{"memory", "file", "redis", "mysql"}) + SessionOptions.ProviderConfig = strings.Trim(sec.Key("provider_config").String(), "\" ") + SessionOptions.CookieName = sec.Key("cookie_name").MustString("grafana_sess") SessionOptions.CookiePath = AppSubUrl - SessionOptions.Secure = Cfg.MustBool("session", "cookie_secure") - SessionOptions.Gclifetime = Cfg.MustInt64("session", "gc_interval_time", 86400) - SessionOptions.Maxlifetime = Cfg.MustInt64("session", "session_life_time", 86400) + 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) if SessionOptions.Provider == "file" { os.MkdirAll(path.Dir(SessionOptions.ProviderConfig), os.ModePerm) diff --git a/pkg/social/social.go b/pkg/social/social.go index 83df3a8dbb8..3afa96f76f0 100644 --- a/pkg/social/social.go +++ b/pkg/social/social.go @@ -39,13 +39,14 @@ func NewOAuthService() { allOauthes := []string{"github", "google"} for _, name := range allOauthes { + sec := setting.Cfg.Section("auth." + name) info := &setting.OAuthInfo{ - ClientId: setting.Cfg.MustValue("auth."+name, "client_id"), - ClientSecret: setting.Cfg.MustValue("auth."+name, "client_secret"), - Scopes: setting.Cfg.MustValueArray("auth."+name, "scopes", " "), - AuthUrl: setting.Cfg.MustValue("auth."+name, "auth_url"), - TokenUrl: setting.Cfg.MustValue("auth."+name, "token_url"), - Enabled: setting.Cfg.MustBool("auth."+name, "enabled"), + ClientId: sec.Key("client_id").String(), + ClientSecret: sec.Key("client_secret").String(), + Scopes: sec.Key("scopes").Strings(" "), + AuthUrl: sec.Key("auth_url").String(), + TokenUrl: sec.Key("token_url").String(), + Enabled: sec.Key("enabled").MustBool(), } if !info.Enabled {