Add SafeCast function and set default value for some sytem configure

Add SafeCastString, SafeCastInt, SafeCastFloat64, SafeCastBool to check
the type matched or not to avoid panic in runtime

Add default value to configure settings to avoid can not save result
issue
This commit is contained in:
stonezdj 2018-07-13 19:19:54 +08:00
parent 218765edf0
commit 5bc3c6bdcd
9 changed files with 341 additions and 171 deletions

View File

@ -31,7 +31,7 @@ func ListCfgs(w http.ResponseWriter, r *http.Request) {
handleInternalServerError(w) handleInternalServerError(w)
return return
} }
systemcfg.AddMissedKey(cfg)
if err = writeJSON(w, cfg); err != nil { if err = writeJSON(w, cfg); err != nil {
log.Errorf("failed to write response: %v", err) log.Errorf("failed to write response: %v", err)
return return
@ -52,7 +52,6 @@ func UpdateCfgs(w http.ResponseWriter, r *http.Request) {
handleBadRequestError(w, err.Error()) handleBadRequestError(w, err.Error())
return return
} }
if err = systemcfg.CfgStore.Write(m); err != nil { if err = systemcfg.CfgStore.Write(m); err != nil {
log.Errorf("failed to update system configurations: %v", err) log.Errorf("failed to update system configurations: %v", err)
handleInternalServerError(w) handleInternalServerError(w)
@ -68,7 +67,6 @@ func ResetCfgs(w http.ResponseWriter, r *http.Request) {
handleInternalServerError(w) handleInternalServerError(w)
return return
} }
if err := systemcfg.CfgStore.Write(cfgs); err != nil { if err := systemcfg.CfgStore.Write(cfgs); err != nil {
log.Errorf("failed to write system configurations to storage: %v", err) log.Errorf("failed to write system configurations to storage: %v", err)
handleInternalServerError(w) handleInternalServerError(w)

View File

@ -30,6 +30,7 @@ import (
comcfg "github.com/vmware/harbor/src/common/config" comcfg "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/log" "github.com/vmware/harbor/src/common/utils/log"
) )
@ -242,31 +243,27 @@ func Init() (err error) {
if err = initCfgStore(); err != nil { if err = initCfgStore(); err != nil {
return err return err
} }
cfgs := map[string]interface{}{}
//Use reload key to avoid reset customed setting after restart //Use reload key to avoid reset customed setting after restart
curCfgs, err := CfgStore.Read() curCfgs, err := CfgStore.Read()
if err != nil { if err != nil {
return err return err
} }
loadAll := isLoadAll(curCfgs[common.ReloadKey]) loadAll := isLoadAll(curCfgs)
if !loadAll { if curCfgs == nil {
cfgs = curCfgs curCfgs = map[string]interface{}{}
if cfgs == nil {
log.Info("configurations read from storage driver are null, will load them from environment variables")
loadAll = true
cfgs = map[string]interface{}{}
} }
} //restart: only repeatload envs will be load
//reload_config: all envs will be reload except the skiped envs
if err = LoadFromEnv(cfgs, loadAll); err != nil { if err = LoadFromEnv(curCfgs, loadAll); err != nil {
return err return err
} }
AddMissedKey(curCfgs)
return CfgStore.Write(cfgs) return CfgStore.Write(curCfgs)
} }
func isLoadAll(curReloadKey interface{}) bool { func isLoadAll(cfg map[string]interface{}) bool {
return strings.EqualFold(os.Getenv("RESET"), "true") && os.Getenv("RELOAD_KEY") != curReloadKey return cfg == nil || strings.EqualFold(os.Getenv("RESET"), "true") && os.Getenv("RELOAD_KEY") != cfg[common.ReloadKey]
} }
func initCfgStore() (err error) { func initCfgStore() (err error) {
@ -316,7 +313,6 @@ func initCfgStore() (err error) {
// only used when migrating harbor release before v1.3 // only used when migrating harbor release before v1.3
// after v1.3 there is always a db configuration before migrate. // after v1.3 there is always a db configuration before migrate.
validLdapScope(jsonconfig, true) validLdapScope(jsonconfig, true)
err = CfgStore.Write(jsonconfig) err = CfgStore.Write(jsonconfig)
if err != nil { if err != nil {
log.Error("Failed to update old configuration to database") log.Error("Failed to update old configuration to database")
@ -403,16 +399,16 @@ func LoadFromEnv(cfgs map[string]interface{}, all bool) error {
// GetDatabaseFromCfg Create database object from config // GetDatabaseFromCfg Create database object from config
func GetDatabaseFromCfg(cfg map[string]interface{}) *models.Database { func GetDatabaseFromCfg(cfg map[string]interface{}) *models.Database {
database := &models.Database{} database := &models.Database{}
database.Type = cfg[common.DatabaseType].(string) database.Type = utils.SafeCastString(cfg[common.DatabaseType])
mysql := &models.MySQL{} mysql := &models.MySQL{}
mysql.Host = cfg[common.MySQLHost].(string) mysql.Host = utils.SafeCastString(cfg[common.MySQLHost])
mysql.Port = int(cfg[common.MySQLPort].(int)) mysql.Port = utils.SafeCastInt(cfg[common.MySQLPort])
mysql.Username = cfg[common.MySQLUsername].(string) mysql.Username = utils.SafeCastString(cfg[common.MySQLUsername])
mysql.Password = cfg[common.MySQLPassword].(string) mysql.Password = utils.SafeCastString(cfg[common.MySQLPassword])
mysql.Database = cfg[common.MySQLDatabase].(string) mysql.Database = utils.SafeCastString(cfg[common.MySQLDatabase])
database.MySQL = mysql database.MySQL = mysql
sqlite := &models.SQLite{} sqlite := &models.SQLite{}
sqlite.File = cfg[common.SQLiteFile].(string) sqlite.File = utils.SafeCastString(cfg[common.SQLiteFile])
database.SQLite = sqlite database.SQLite = sqlite
return database return database
} }
@ -438,3 +434,26 @@ func validLdapScope(cfg map[string]interface{}, isMigrate bool) {
cfg[ldapScopeKey] = ldapScope cfg[ldapScopeKey] = ldapScope
} }
//AddMissedKey ... If the configure key is missing in the cfg map, add default value to it
func AddMissedKey(cfg map[string]interface{}) {
for k, v := range common.HarborStringKeysMap {
if _, exist := cfg[k]; !exist {
cfg[k] = v
}
}
for k, v := range common.HarborNumKeysMap {
if _, exist := cfg[k]; !exist {
cfg[k] = v
}
}
for k, v := range common.HarborBoolKeysMap {
if _, exist := cfg[k]; !exist {
cfg[k] = v
}
}
}

View File

@ -135,8 +135,10 @@ func TestIsLoadAll(t *testing.T) {
if err := os.Setenv("RESET", "True"); err != nil { if err := os.Setenv("RESET", "True"); err != nil {
t.Fatalf("failed to set env: %v", err) t.Fatalf("failed to set env: %v", err)
} }
assert.False(t, isLoadAll("123456")) cfg1 := map[string]interface{}{common.ReloadKey: "123456"}
assert.True(t, isLoadAll("654321")) cfg2 := map[string]interface{}{common.ReloadKey: "654321"}
assert.False(t, isLoadAll(cfg1))
assert.True(t, isLoadAll(cfg2))
} }
func TestLoadFromEnvWithReloadConfigInvalidSkipPattern(t *testing.T) { func TestLoadFromEnvWithReloadConfigInvalidSkipPattern(t *testing.T) {
@ -259,3 +261,31 @@ func TestValidLdapScope(t *testing.T) {
} }
} }
func Test_AddMissingKey(t *testing.T) {
cfg := map[string]interface{}{
common.LDAPURL: "sampleurl",
common.EmailPort: 555,
common.LDAPVerifyCert: true,
}
type args struct {
cfg map[string]interface{}
}
tests := []struct {
name string
args args
}{
{"Add default value", args{cfg}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
AddMissedKey(tt.args.cfg)
})
}
if _, ok := cfg[common.LDAPBaseDN]; !ok {
t.Errorf("Can not found default value for %v", common.LDAPBaseDN)
}
}

View File

@ -109,3 +109,87 @@ const (
LdapGroupType = 1 LdapGroupType = 1
ReloadKey = "reload_key" ReloadKey = "reload_key"
) )
// Shared variable, not allowed to modify
var (
// the keys of configurations which user can modify in PUT method and user can
// get in GET method
HarborValidKeys = []string{
AUTHMode,
SelfRegistration,
LDAPURL,
LDAPSearchDN,
LDAPSearchPwd,
LDAPBaseDN,
LDAPUID,
LDAPFilter,
LDAPScope,
LDAPTimeout,
LDAPVerifyCert,
LDAPGroupAttributeName,
LDAPGroupBaseDN,
LDAPGroupSearchFilter,
LDAPGroupSearchScope,
EmailHost,
EmailPort,
EmailUsername,
EmailPassword,
EmailFrom,
EmailSSL,
EmailIdentity,
EmailInsecure,
ProjectCreationRestriction,
TokenExpiration,
ScanAllPolicy,
UAAClientID,
UAAClientSecret,
UAAEndpoint,
UAAVerifyCert,
ReadOnly,
}
//value is default value
HarborStringKeysMap = map[string]string{
AUTHMode: "db_auth",
LDAPURL: "",
LDAPSearchDN: "",
LDAPSearchPwd: "",
LDAPBaseDN: "",
LDAPUID: "",
LDAPFilter: "",
LDAPGroupAttributeName: "",
LDAPGroupBaseDN: "",
LDAPGroupSearchFilter: "",
EmailHost: "smtp.mydomain.com",
EmailUsername: "sample_admin@mydomain.com",
EmailPassword: "abc",
EmailFrom: "admin <sample_admin@mydomain.com>",
EmailIdentity: "",
ProjectCreationRestriction: ProCrtRestrEveryone,
UAAClientID: "",
UAAEndpoint: "",
}
HarborNumKeysMap = map[string]int{
EmailPort: 25,
LDAPScope: 2,
LDAPTimeout: 5,
LDAPGroupSearchScope: 2,
TokenExpiration: 30,
}
HarborBoolKeysMap = map[string]bool{
EmailSSL: false,
EmailInsecure: false,
SelfRegistration: true,
LDAPVerifyCert: true,
UAAVerifyCert: true,
ReadOnly: false,
}
HarborPasswordKeys = []string{
EmailPassword,
LDAPSearchPwd,
UAAClientSecret,
}
)

View File

@ -167,3 +167,35 @@ func ParseProjectIDOrName(value interface{}) (int64, string, error) {
} }
return id, name, nil return id, name, nil
} }
//SafeCastString -- cast a object to string saftely
func SafeCastString(value interface{}) string {
if result, ok := value.(string); ok {
return result
}
return ""
}
//SafeCastInt --
func SafeCastInt(value interface{}) int {
if result, ok := value.(int); ok {
return result
}
return 0
}
//SafeCastBool --
func SafeCastBool(value interface{}) bool {
if result, ok := value.(bool); ok {
return result
}
return false
}
//SafeCastFloat64 --
func SafeCastFloat64(value interface{}) float64 {
if result, ok := value.(float64); ok {
return result
}
return 0
}

View File

@ -248,3 +248,91 @@ func TestConvertMapToStruct(t *testing.T) {
} }
} }
} }
func TestSafeCastString(t *testing.T) {
type args struct {
value interface{}
}
tests := []struct {
name string
args args
want string
}{
{"nil value", args{nil}, ""},
{"normal string", args{"sample"}, "sample"},
{"wrong type", args{12}, ""},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := SafeCastString(tt.args.value); got != tt.want {
t.Errorf("SafeCastString() = %v, want %v", got, tt.want)
}
})
}
}
func TestSafeCastBool(t *testing.T) {
type args struct {
value interface{}
}
tests := []struct {
name string
args args
want bool
}{
{"nil value", args{nil}, false},
{"normal bool", args{true}, true},
{"wrong type", args{"true"}, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := SafeCastBool(tt.args.value); got != tt.want {
t.Errorf("SafeCastBool() = %v, want %v", got, tt.want)
}
})
}
}
func TestSafeCastInt(t *testing.T) {
type args struct {
value interface{}
}
tests := []struct {
name string
args args
want int
}{
{"nil value", args{nil}, 0},
{"normal int", args{1234}, 1234},
{"wrong type", args{"sample"}, 0},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := SafeCastInt(tt.args.value); got != tt.want {
t.Errorf("SafeCastInt() = %v, want %v", got, tt.want)
}
})
}
}
func TestSafeCastFloat64(t *testing.T) {
type args struct {
value interface{}
}
tests := []struct {
name string
args args
want float64
}{
{"nil value", args{nil}, 0},
{"normal float64", args{12.34}, 12.34},
{"wrong type", args{false}, 0},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := SafeCastFloat64(tt.args.value); got != tt.want {
t.Errorf("SafeCastFloat64() = %v, want %v", got, tt.want)
}
})
}
}

View File

@ -26,88 +26,6 @@ import (
"github.com/vmware/harbor/src/ui/config" "github.com/vmware/harbor/src/ui/config"
) )
var (
// the keys of configurations which user can modify in PUT method and user can
// get in GET method
validKeys = []string{
common.AUTHMode,
common.SelfRegistration,
common.LDAPURL,
common.LDAPSearchDN,
common.LDAPSearchPwd,
common.LDAPBaseDN,
common.LDAPUID,
common.LDAPFilter,
common.LDAPScope,
common.LDAPTimeout,
common.LDAPVerifyCert,
common.LDAPGroupAttributeName,
common.LDAPGroupBaseDN,
common.LDAPGroupSearchFilter,
common.LDAPGroupSearchScope,
common.EmailHost,
common.EmailPort,
common.EmailUsername,
common.EmailPassword,
common.EmailFrom,
common.EmailSSL,
common.EmailIdentity,
common.EmailInsecure,
common.ProjectCreationRestriction,
common.TokenExpiration,
common.ScanAllPolicy,
common.UAAClientID,
common.UAAClientSecret,
common.UAAEndpoint,
common.UAAVerifyCert,
common.ReadOnly,
}
stringKeys = []string{
common.AUTHMode,
common.LDAPURL,
common.LDAPSearchDN,
common.LDAPSearchPwd,
common.LDAPBaseDN,
common.LDAPUID,
common.LDAPFilter,
common.LDAPGroupAttributeName,
common.LDAPGroupBaseDN,
common.LDAPGroupSearchFilter,
common.EmailHost,
common.EmailUsername,
common.EmailPassword,
common.EmailFrom,
common.EmailIdentity,
common.ProjectCreationRestriction,
common.UAAClientID,
common.UAAEndpoint,
}
numKeys = []string{
common.EmailPort,
common.LDAPScope,
common.LDAPTimeout,
common.LDAPGroupSearchScope,
common.TokenExpiration,
}
boolKeys = []string{
common.EmailSSL,
common.EmailInsecure,
common.SelfRegistration,
common.LDAPVerifyCert,
common.UAAVerifyCert,
common.ReadOnly,
}
passwordKeys = []string{
common.EmailPassword,
common.LDAPSearchPwd,
common.UAAClientSecret,
}
)
// ConfigAPI ... // ConfigAPI ...
type ConfigAPI struct { type ConfigAPI struct {
BaseController BaseController
@ -140,7 +58,7 @@ func (c *ConfigAPI) Get() {
} }
cfgs := map[string]interface{}{} cfgs := map[string]interface{}{}
for _, k := range validKeys { for _, k := range common.HarborValidKeys {
if v, ok := configs[k]; ok { if v, ok := configs[k]; ok {
cfgs[k] = v cfgs[k] = v
} }
@ -162,7 +80,7 @@ func (c *ConfigAPI) Put() {
c.DecodeJSONReq(&m) c.DecodeJSONReq(&m)
cfg := map[string]interface{}{} cfg := map[string]interface{}{}
for _, k := range validKeys { for _, k := range common.HarborValidKeys {
if v, ok := m[k]; ok { if v, ok := m[k]; ok {
cfg[k] = v cfg[k] = v
} }
@ -205,7 +123,7 @@ func (c *ConfigAPI) Reset() {
func validateCfg(c map[string]interface{}) (bool, error) { func validateCfg(c map[string]interface{}) (bool, error) {
strMap := map[string]string{} strMap := map[string]string{}
for _, k := range stringKeys { for k := range common.HarborStringKeysMap {
if _, ok := c[k]; !ok { if _, ok := c[k]; !ok {
continue continue
} }
@ -215,7 +133,7 @@ func validateCfg(c map[string]interface{}) (bool, error) {
strMap[k] = c[k].(string) strMap[k] = c[k].(string)
} }
numMap := map[string]int{} numMap := map[string]int{}
for _, k := range numKeys { for k := range common.HarborNumKeysMap {
if _, ok := c[k]; !ok { if _, ok := c[k]; !ok {
continue continue
} }
@ -225,7 +143,7 @@ func validateCfg(c map[string]interface{}) (bool, error) {
numMap[k] = int(c[k].(float64)) numMap[k] = int(c[k].(float64))
} }
boolMap := map[string]bool{} boolMap := map[string]bool{}
for _, k := range boolKeys { for k := range common.HarborBoolKeysMap {
if _, ok := c[k]; !ok { if _, ok := c[k]; !ok {
continue continue
} }
@ -327,7 +245,7 @@ func validateCfg(c map[string]interface{}) (bool, error) {
func convertForGet(cfg map[string]interface{}) (map[string]*value, error) { func convertForGet(cfg map[string]interface{}) (map[string]*value, error) {
result := map[string]*value{} result := map[string]*value{}
for _, k := range passwordKeys { for _, k := range common.HarborPasswordKeys {
if _, ok := cfg[k]; ok { if _, ok := cfg[k]; ok {
delete(cfg, k) delete(cfg, k)
} }

View File

@ -167,17 +167,17 @@ func (sia *SystemInfoAPI) GetGeneralInfo() {
_, caStatErr := os.Stat(defaultRootCert) _, caStatErr := os.Stat(defaultRootCert)
harborVersion := sia.getVersion() harborVersion := sia.getVersion()
info := GeneralInfo{ info := GeneralInfo{
AdmiralEndpoint: cfg[common.AdmiralEndpoint].(string), AdmiralEndpoint: utils.SafeCastString(cfg[common.AdmiralEndpoint]),
WithAdmiral: config.WithAdmiral(), WithAdmiral: config.WithAdmiral(),
WithNotary: config.WithNotary(), WithNotary: config.WithNotary(),
WithClair: config.WithClair(), WithClair: config.WithClair(),
AuthMode: cfg[common.AUTHMode].(string), AuthMode: utils.SafeCastString(cfg[common.AUTHMode]),
ProjectCreationRestrict: cfg[common.ProjectCreationRestriction].(string), ProjectCreationRestrict: utils.SafeCastString(cfg[common.ProjectCreationRestriction]),
SelfRegistration: cfg[common.SelfRegistration].(bool), SelfRegistration: utils.SafeCastBool(cfg[common.SelfRegistration]),
RegistryURL: registryURL, RegistryURL: registryURL,
HasCARoot: caStatErr == nil, HasCARoot: caStatErr == nil,
HarborVersion: harborVersion, HarborVersion: harborVersion,
RegistryStorageProviderName: cfg[common.RegistryStorageProviderName].(string), RegistryStorageProviderName: utils.SafeCastString(cfg[common.RegistryStorageProviderName]),
ReadOnly: config.ReadOnly(), ReadOnly: config.ReadOnly(),
} }
if info.WithClair { if info.WithClair {

View File

@ -30,6 +30,7 @@ import (
comcfg "github.com/vmware/harbor/src/common/config" comcfg "github.com/vmware/harbor/src/common/config"
"github.com/vmware/harbor/src/common/models" "github.com/vmware/harbor/src/common/models"
"github.com/vmware/harbor/src/common/secret" "github.com/vmware/harbor/src/common/secret"
"github.com/vmware/harbor/src/common/utils"
"github.com/vmware/harbor/src/common/utils/log" "github.com/vmware/harbor/src/common/utils/log"
"github.com/vmware/harbor/src/ui/promgr" "github.com/vmware/harbor/src/ui/promgr"
"github.com/vmware/harbor/src/ui/promgr/pmsdriver" "github.com/vmware/harbor/src/ui/promgr/pmsdriver"
@ -186,7 +187,7 @@ func AuthMode() (string, error) {
if err != nil { if err != nil {
return "", err return "", err
} }
return cfg[common.AUTHMode].(string), nil return utils.SafeCastString(cfg[common.AUTHMode]), nil
} }
// LDAPConf returns the setting of ldap server // LDAPConf returns the setting of ldap server
@ -196,14 +197,14 @@ func LDAPConf() (*models.LdapConf, error) {
return nil, err return nil, err
} }
ldapConf := &models.LdapConf{} ldapConf := &models.LdapConf{}
ldapConf.LdapURL = cfg[common.LDAPURL].(string) ldapConf.LdapURL = utils.SafeCastString(cfg[common.LDAPURL])
ldapConf.LdapSearchDn = cfg[common.LDAPSearchDN].(string) ldapConf.LdapSearchDn = utils.SafeCastString(cfg[common.LDAPSearchDN])
ldapConf.LdapSearchPassword = cfg[common.LDAPSearchPwd].(string) ldapConf.LdapSearchPassword = utils.SafeCastString(cfg[common.LDAPSearchPwd])
ldapConf.LdapBaseDn = cfg[common.LDAPBaseDN].(string) ldapConf.LdapBaseDn = utils.SafeCastString(cfg[common.LDAPBaseDN])
ldapConf.LdapUID = cfg[common.LDAPUID].(string) ldapConf.LdapUID = utils.SafeCastString(cfg[common.LDAPUID])
ldapConf.LdapFilter = cfg[common.LDAPFilter].(string) ldapConf.LdapFilter = utils.SafeCastString(cfg[common.LDAPFilter])
ldapConf.LdapScope = int(cfg[common.LDAPScope].(float64)) ldapConf.LdapScope = int(utils.SafeCastFloat64(cfg[common.LDAPScope]))
ldapConf.LdapConnectionTimeout = int(cfg[common.LDAPTimeout].(float64)) ldapConf.LdapConnectionTimeout = int(utils.SafeCastFloat64(cfg[common.LDAPTimeout]))
if cfg[common.LDAPVerifyCert] != nil { if cfg[common.LDAPVerifyCert] != nil {
ldapConf.LdapVerifyCert = cfg[common.LDAPVerifyCert].(bool) ldapConf.LdapVerifyCert = cfg[common.LDAPVerifyCert].(bool)
} else { } else {
@ -223,13 +224,13 @@ func LDAPGroupConf() (*models.LdapGroupConf, error) {
ldapGroupConf := &models.LdapGroupConf{LdapGroupSearchScope: 2} ldapGroupConf := &models.LdapGroupConf{LdapGroupSearchScope: 2}
if _, ok := cfg[common.LDAPGroupBaseDN]; ok { if _, ok := cfg[common.LDAPGroupBaseDN]; ok {
ldapGroupConf.LdapGroupBaseDN = cfg[common.LDAPGroupBaseDN].(string) ldapGroupConf.LdapGroupBaseDN = utils.SafeCastString(cfg[common.LDAPGroupBaseDN])
} }
if _, ok := cfg[common.LDAPGroupSearchFilter]; ok { if _, ok := cfg[common.LDAPGroupSearchFilter]; ok {
ldapGroupConf.LdapGroupFilter = cfg[common.LDAPGroupSearchFilter].(string) ldapGroupConf.LdapGroupFilter = utils.SafeCastString(cfg[common.LDAPGroupSearchFilter])
} }
if _, ok := cfg[common.LDAPGroupAttributeName]; ok { if _, ok := cfg[common.LDAPGroupAttributeName]; ok {
ldapGroupConf.LdapGroupNameAttribute = cfg[common.LDAPGroupAttributeName].(string) ldapGroupConf.LdapGroupNameAttribute = utils.SafeCastString(cfg[common.LDAPGroupAttributeName])
} }
if _, ok := cfg[common.LDAPGroupSearchScope]; ok { if _, ok := cfg[common.LDAPGroupSearchScope]; ok {
if scopeStr, ok := cfg[common.LDAPGroupSearchScope].(string); ok { if scopeStr, ok := cfg[common.LDAPGroupSearchScope].(string); ok {
@ -249,7 +250,7 @@ func TokenExpiration() (int, error) {
return 0, err return 0, err
} }
return int(cfg[common.TokenExpiration].(float64)), nil return int(utils.SafeCastFloat64(cfg[common.TokenExpiration])), nil
} }
// ExtEndpoint returns the external URL of Harbor: protocol://host:port // ExtEndpoint returns the external URL of Harbor: protocol://host:port
@ -258,7 +259,7 @@ func ExtEndpoint() (string, error) {
if err != nil { if err != nil {
return "", err return "", err
} }
return cfg[common.ExtEndpoint].(string), nil return utils.SafeCastString(cfg[common.ExtEndpoint]), nil
} }
// ExtURL returns the external URL: host:port // ExtURL returns the external URL: host:port
@ -285,7 +286,7 @@ func SelfRegistration() (bool, error) {
if err != nil { if err != nil {
return false, err return false, err
} }
return cfg[common.SelfRegistration].(bool), nil return utils.SafeCastBool(cfg[common.SelfRegistration]), nil
} }
// RegistryURL ... // RegistryURL ...
@ -294,7 +295,7 @@ func RegistryURL() (string, error) {
if err != nil { if err != nil {
return "", err return "", err
} }
return cfg[common.RegistryURL].(string), nil return utils.SafeCastString(cfg[common.RegistryURL]), nil
} }
// InternalJobServiceURL returns jobservice URL for internal communication between Harbor containers // InternalJobServiceURL returns jobservice URL for internal communication between Harbor containers
@ -308,7 +309,7 @@ func InternalJobServiceURL() string {
if cfg[common.JobServiceURL] == nil { if cfg[common.JobServiceURL] == nil {
return common.DefaultJobserviceEndpoint return common.DefaultJobserviceEndpoint
} }
return strings.TrimSuffix(cfg[common.JobServiceURL].(string), "/") return strings.TrimSuffix(utils.SafeCastString(cfg[common.JobServiceURL]), "/")
} }
// InternalUIURL returns the local ui url // InternalUIURL returns the local ui url
@ -318,7 +319,7 @@ func InternalUIURL() string {
log.Warningf("Failed to Get job service UI URL from backend, error: %v, will return default value.") log.Warningf("Failed to Get job service UI URL from backend, error: %v, will return default value.")
return common.DefaultUIEndpoint return common.DefaultUIEndpoint
} }
return strings.TrimSuffix(cfg[common.UIURL].(string), "/") return strings.TrimSuffix(utils.SafeCastString(cfg[common.UIURL]), "/")
} }
@ -338,7 +339,7 @@ func InternalNotaryEndpoint() string {
if cfg[common.NotaryURL] == nil { if cfg[common.NotaryURL] == nil {
return common.DefaultNotaryEndpoint return common.DefaultNotaryEndpoint
} }
return cfg[common.NotaryURL].(string) return utils.SafeCastString(cfg[common.NotaryURL])
} }
// InitialAdminPassword returns the initial password for administrator // InitialAdminPassword returns the initial password for administrator
@ -347,7 +348,7 @@ func InitialAdminPassword() (string, error) {
if err != nil { if err != nil {
return "", err return "", err
} }
return cfg[common.AdminInitialPassword].(string), nil return utils.SafeCastString(cfg[common.AdminInitialPassword]), nil
} }
// OnlyAdminCreateProject returns the flag to restrict that only sys admin can create project // OnlyAdminCreateProject returns the flag to restrict that only sys admin can create project
@ -356,7 +357,7 @@ func OnlyAdminCreateProject() (bool, error) {
if err != nil { if err != nil {
return true, err return true, err
} }
return cfg[common.ProjectCreationRestriction].(string) == common.ProCrtRestrAdmOnly, nil return utils.SafeCastString(cfg[common.ProjectCreationRestriction]) == common.ProCrtRestrAdmOnly, nil
} }
// Email returns email server settings // Email returns email server settings
@ -367,14 +368,14 @@ func Email() (*models.Email, error) {
} }
email := &models.Email{} email := &models.Email{}
email.Host = cfg[common.EmailHost].(string) email.Host = utils.SafeCastString(cfg[common.EmailHost])
email.Port = int(cfg[common.EmailPort].(float64)) email.Port = int(utils.SafeCastFloat64(cfg[common.EmailPort]))
email.Username = cfg[common.EmailUsername].(string) email.Username = utils.SafeCastString(cfg[common.EmailUsername])
email.Password = cfg[common.EmailPassword].(string) email.Password = utils.SafeCastString(cfg[common.EmailPassword])
email.SSL = cfg[common.EmailSSL].(bool) email.SSL = utils.SafeCastBool(cfg[common.EmailSSL])
email.From = cfg[common.EmailFrom].(string) email.From = utils.SafeCastString(cfg[common.EmailFrom])
email.Identity = cfg[common.EmailIdentity].(string) email.Identity = utils.SafeCastString(cfg[common.EmailIdentity])
email.Insecure = cfg[common.EmailInsecure].(bool) email.Insecure = utils.SafeCastBool(cfg[common.EmailInsecure])
return email, nil return email, nil
} }
@ -386,16 +387,16 @@ func Database() (*models.Database, error) {
return nil, err return nil, err
} }
database := &models.Database{} database := &models.Database{}
database.Type = cfg[common.DatabaseType].(string) database.Type = utils.SafeCastString(cfg[common.DatabaseType])
mysql := &models.MySQL{} mysql := &models.MySQL{}
mysql.Host = cfg[common.MySQLHost].(string) mysql.Host = utils.SafeCastString(cfg[common.MySQLHost])
mysql.Port = int(cfg[common.MySQLPort].(float64)) mysql.Port = int(utils.SafeCastFloat64(cfg[common.MySQLPort]))
mysql.Username = cfg[common.MySQLUsername].(string) mysql.Username = utils.SafeCastString(cfg[common.MySQLUsername])
mysql.Password = cfg[common.MySQLPassword].(string) mysql.Password = utils.SafeCastString(cfg[common.MySQLPassword])
mysql.Database = cfg[common.MySQLDatabase].(string) mysql.Database = utils.SafeCastString(cfg[common.MySQLDatabase])
database.MySQL = mysql database.MySQL = mysql
sqlite := &models.SQLite{} sqlite := &models.SQLite{}
sqlite.File = cfg[common.SQLiteFile].(string) sqlite.File = utils.SafeCastString(cfg[common.SQLiteFile])
database.SQLite = sqlite database.SQLite = sqlite
return database, nil return database, nil
@ -421,7 +422,7 @@ func WithNotary() bool {
log.Warningf("Failed to get configuration, will return WithNotary == false") log.Warningf("Failed to get configuration, will return WithNotary == false")
return false return false
} }
return cfg[common.WithNotary].(bool) return utils.SafeCastBool(cfg[common.WithNotary])
} }
// WithClair returns a bool value to indicate if Harbor's deployed with Clair // WithClair returns a bool value to indicate if Harbor's deployed with Clair
@ -431,7 +432,7 @@ func WithClair() bool {
log.Errorf("Failed to get configuration, will return WithClair == false") log.Errorf("Failed to get configuration, will return WithClair == false")
return false return false
} }
return cfg[common.WithClair].(bool) return utils.SafeCastBool(cfg[common.WithClair])
} }
// ClairEndpoint returns the end point of clair instance, by default it's the one deployed within Harbor. // ClairEndpoint returns the end point of clair instance, by default it's the one deployed within Harbor.
@ -441,7 +442,7 @@ func ClairEndpoint() string {
log.Errorf("Failed to get configuration, use default clair endpoint") log.Errorf("Failed to get configuration, use default clair endpoint")
return common.DefaultClairEndpoint return common.DefaultClairEndpoint
} }
return cfg[common.ClairURL].(string) return utils.SafeCastString(cfg[common.ClairURL])
} }
// ClairDB return Clair db info // ClairDB return Clair db info
@ -452,11 +453,11 @@ func ClairDB() (*models.PostGreSQL, error) {
return nil, err return nil, err
} }
clairDB := &models.PostGreSQL{} clairDB := &models.PostGreSQL{}
clairDB.Host = cfg[common.ClairDBHost].(string) clairDB.Host = utils.SafeCastString(cfg[common.ClairDBHost])
clairDB.Port = int(cfg[common.ClairDBPort].(float64)) clairDB.Port = int(utils.SafeCastFloat64(cfg[common.ClairDBPort]))
clairDB.Username = cfg[common.ClairDBUsername].(string) clairDB.Username = utils.SafeCastString(cfg[common.ClairDBUsername])
clairDB.Password = cfg[common.ClairDBPassword].(string) clairDB.Password = utils.SafeCastString(cfg[common.ClairDBPassword])
clairDB.Database = cfg[common.ClairDB].(string) clairDB.Database = utils.SafeCastString(cfg[common.ClairDB])
return clairDB, nil return clairDB, nil
} }
@ -471,7 +472,7 @@ func AdmiralEndpoint() string {
if e, ok := cfg[common.AdmiralEndpoint].(string); !ok || e == "NA" { if e, ok := cfg[common.AdmiralEndpoint].(string); !ok || e == "NA" {
return "" return ""
} }
return cfg[common.AdmiralEndpoint].(string) return utils.SafeCastString(cfg[common.AdmiralEndpoint])
} }
// ScanAllPolicy returns the policy which controls the scan all. // ScanAllPolicy returns the policy which controls the scan all.
@ -510,10 +511,10 @@ func UAASettings() (*models.UAASettings, error) {
return nil, err return nil, err
} }
us := &models.UAASettings{ us := &models.UAASettings{
Endpoint: cfg[common.UAAEndpoint].(string), Endpoint: utils.SafeCastString(cfg[common.UAAEndpoint]),
ClientID: cfg[common.UAAClientID].(string), ClientID: utils.SafeCastString(cfg[common.UAAClientID]),
ClientSecret: cfg[common.UAAClientSecret].(string), ClientSecret: utils.SafeCastString(cfg[common.UAAClientSecret]),
VerifyCert: cfg[common.UAAVerifyCert].(bool), VerifyCert: utils.SafeCastBool(cfg[common.UAAVerifyCert]),
} }
return us, nil return us, nil
} }
@ -525,5 +526,5 @@ func ReadOnly() bool {
log.Errorf("Failed to get configuration, will return false as read only, error: %v", err) log.Errorf("Failed to get configuration, will return false as read only, error: %v", err)
return false return false
} }
return cfg[common.ReadOnly].(bool) return utils.SafeCastBool(cfg[common.ReadOnly])
} }