ui config refactory

This commit is contained in:
Tan Jiang 2016-11-16 13:25:37 +08:00
parent 31eccec220
commit 0e3cb2e3f4
5 changed files with 30 additions and 28 deletions

View File

@ -3,11 +3,11 @@ MYSQL_PORT=3306
MYSQL_USR=root MYSQL_USR=root
MYSQL_PWD=$db_password MYSQL_PWD=$db_password
REGISTRY_URL=http://registry:5000 REGISTRY_URL=http://registry:5000
JOB_SERVICE_URL=http://jobservice
UI_URL=http://ui UI_URL=http://ui
CONFIG_PATH=/etc/ui/app.conf CONFIG_PATH=/etc/ui/app.conf
HARBOR_REG_URL=$hostname EXT_REG_URL=$hostname
HARBOR_ADMIN_PASSWORD=$harbor_admin_password HARBOR_ADMIN_PASSWORD=$harbor_admin_password
HARBOR_URL=$ui_url
AUTH_MODE=$auth_mode AUTH_MODE=$auth_mode
LDAP_URL=$ldap_url LDAP_URL=$ldap_url
LDAP_SEARCH_DN=$ldap_searchdn LDAP_SEARCH_DN=$ldap_searchdn
@ -26,3 +26,4 @@ EXT_ENDPOINT=$ui_url
TOKEN_ENDPOINT=http://ui TOKEN_ENDPOINT=http://ui
VERIFY_REMOTE_CERT=$verify_remote_cert VERIFY_REMOTE_CERT=$verify_remote_cert
TOKEN_EXPIRATION=$token_expiration TOKEN_EXPIRATION=$token_expiration
CREATE_PROJECT_RESTRICTION=$create_project_restriction

View File

@ -2,11 +2,11 @@
#The IP address or hostname to access admin UI and registry service. #The IP address or hostname to access admin UI and registry service.
#DO NOT use localhost or 127.0.0.1, because Harbor needs to be accessed by external clients. #DO NOT use localhost or 127.0.0.1, because Harbor needs to be accessed by external clients.
hostname = reg.mydomain.com hostname = 10.117.4.174
#The protocol for accessing the UI and token/notification service, by default it is http. #The protocol for accessing the UI and token/notification service, by default it is http.
#It can be set to https if ssl is enabled on nginx. #It can be set to https if ssl is enabled on nginx.
ui_url_protocol = http ui_url_protocol = https
#Email account settings for sending out password resetting emails. #Email account settings for sending out password resetting emails.
email_identity = Mail Config email_identity = Mail Config

View File

@ -29,13 +29,13 @@ type ConfigLoader interface {
// EnvConfigLoader loads the config from env vars. // EnvConfigLoader loads the config from env vars.
type EnvConfigLoader struct { type EnvConfigLoader struct {
keys []string Keys []string
} }
// Load ... // Load ...
func (ec *EnvConfigLoader) Load() (map[string]string, error) { func (ec *EnvConfigLoader) Load() (map[string]string, error) {
m := make(map[string]string) m := make(map[string]string)
for _, k := range ec.keys { for _, k := range ec.Keys {
m[k] = os.Getenv(k) m[k] = os.Getenv(k)
} }
return m, nil return m, nil
@ -48,17 +48,17 @@ type ConfigParser interface {
} }
type Config struct { type Config struct {
config map[string]interface{} Config map[string]interface{}
loader ConfigLoader Loader ConfigLoader
parser ConfigParser Parser ConfigParser
} }
func (conf *Config) load() error { func (conf *Config) Load() error {
rawMap, err := conf.loader.Load() rawMap, err := conf.Loader.Load()
if err != nil { if err != nil {
return err return err
} }
err = conf.parser.Parse(rawMap, conf.config) err = conf.Parser.Parse(rawMap, conf.Config)
return err return err
} }
@ -121,50 +121,50 @@ var commonConfig *Config
func init() { func init() {
commonKeys := []string{"DATABASE", "MYSQL_DATABASE", "MYSQL_USR", "MYSQL_PWD", "MYSQL_HOST", "MYSQL_PORT", "SQLITE_FILE", "VERIFY_REMOTE_CERT", "EXT_ENDPOINT", "TOKEN_ENDPOINT", "LOG_LEVEL"} commonKeys := []string{"DATABASE", "MYSQL_DATABASE", "MYSQL_USR", "MYSQL_PWD", "MYSQL_HOST", "MYSQL_PORT", "SQLITE_FILE", "VERIFY_REMOTE_CERT", "EXT_ENDPOINT", "TOKEN_ENDPOINT", "LOG_LEVEL"}
commonConfig = &Config{ commonConfig = &Config{
config: make(map[string]interface{}), Config: make(map[string]interface{}),
loader: &EnvConfigLoader{keys: commonKeys}, Loader: &EnvConfigLoader{Keys: commonKeys},
parser: &commonParser{}, Parser: &commonParser{},
} }
if err := commonConfig.load(); err != nil { if err := commonConfig.Load(); err != nil {
panic(err) panic(err)
} }
} }
// Reload will reload the configuration. // Reload will reload the configuration.
func Reload() error { func Reload() error {
return commonConfig.load() return commonConfig.Load()
} }
// Database returns the DB type in configuration. // Database returns the DB type in configuration.
func Database() string { func Database() string {
return commonConfig.config["database"].(string) return commonConfig.Config["database"].(string)
} }
// MySQL returns the mysql setting in configuration. // MySQL returns the mysql setting in configuration.
func MySQL() MySQLSetting { func MySQL() MySQLSetting {
return commonConfig.config["mysql"].(MySQLSetting) return commonConfig.Config["mysql"].(MySQLSetting)
} }
// SQLite returns the SQLite setting // SQLite returns the SQLite setting
func SQLite() SQLiteSetting { func SQLite() SQLiteSetting {
return commonConfig.config["sqlite"].(SQLiteSetting) return commonConfig.Config["sqlite"].(SQLiteSetting)
} }
// VerifyRemoteCert returns bool value. // VerifyRemoteCert returns bool value.
func VerifyRemoteCert() bool { func VerifyRemoteCert() bool {
return commonConfig.config["verify_remote_cert"].(bool) return commonConfig.Config["verify_remote_cert"].(bool)
} }
// ExtEndpoint ... // ExtEndpoint ...
func ExtEndpoint() string { func ExtEndpoint() string {
return commonConfig.config["ext_endpoint"].(string) return commonConfig.Config["ext_endpoint"].(string)
} }
// TokenEndpoint returns the endpoint string of token service, which can be accessed by internal service of Harbor. // TokenEndpoint returns the endpoint string of token service, which can be accessed by internal service of Harbor.
func TokenEndpoint() string { func TokenEndpoint() string {
return commonConfig.config["token_endpoint"].(string) return commonConfig.Config["token_endpoint"].(string)
} }
func LogLevel() string { func LogLevel() string {
return commonConfig.config["log_level"].(string) return commonConfig.Config["log_level"].(string)
} }

View File

@ -17,11 +17,11 @@ package auth
import ( import (
"fmt" "fmt"
"github.com/vmware/harbor/src/common/utils/log"
"os"
"time" "time"
"github.com/vmware/harbor/src/common/models" "github.com/vmware/harbor/src/common/models"
"github.com/vmware/harbor/src/common/utils/log"
"github.com/vmware/harbor/src/ui/config"
) )
// 1.5 seconds // 1.5 seconds
@ -50,7 +50,7 @@ func Register(name string, authenticator Authenticator) {
// Login authenticates user credentials based on setting. // Login authenticates user credentials based on setting.
func Login(m models.AuthModel) (*models.User, error) { func Login(m models.AuthModel) (*models.User, error) {
var authMode = os.Getenv("AUTH_MODE") var authMode = config.AuthMode()
if authMode == "" || m.Principal == "admin" { if authMode == "" || m.Principal == "admin" {
authMode = "db_auth" authMode = "db_auth"
} }

View File

@ -8,6 +8,7 @@ import (
"text/template" "text/template"
"github.com/astaxie/beego" "github.com/astaxie/beego"
"github.com/vmware/harbor/src/common/config"
"github.com/vmware/harbor/src/common/dao" "github.com/vmware/harbor/src/common/dao"
"github.com/vmware/harbor/src/common/models" "github.com/vmware/harbor/src/common/models"
"github.com/vmware/harbor/src/common/utils" "github.com/vmware/harbor/src/common/utils"
@ -49,7 +50,7 @@ func (cc *CommonController) SendEmail() {
message := new(bytes.Buffer) message := new(bytes.Buffer)
harborURL := os.Getenv("HARBOR_URL") harborURL := config.ExtEndpoint()
if harborURL == "" { if harborURL == "" {
harborURL = "localhost" harborURL = "localhost"
} }