mirror of
https://github.com/goharbor/harbor.git
synced 2025-03-11 22:20:00 +01:00
ui config direcotry
This commit is contained in:
parent
0e3cb2e3f4
commit
33da8b230e
135
src/ui/config/config.go
Normal file
135
src/ui/config/config.go
Normal file
@ -0,0 +1,135 @@
|
||||
/*
|
||||
Copyright (c) 2016 VMware, Inc. All Rights Reserved.
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
package config
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
commonConfig "github.com/vmware/harbor/src/common/config"
|
||||
"github.com/vmware/harbor/src/common/utils/log"
|
||||
)
|
||||
|
||||
type LDAPSetting struct {
|
||||
URL string
|
||||
BaseDn string
|
||||
SearchDn string
|
||||
SearchPwd string
|
||||
UID string
|
||||
Filter string
|
||||
Scope string
|
||||
}
|
||||
|
||||
type uiParser struct{}
|
||||
|
||||
// Parse parses the auth settings url settings and other configuration consumed by code under src/ui
|
||||
func (up *uiParser) Parse(raw map[string]string, config map[string]interface{}) error {
|
||||
mode := raw["AUTH_MODE"]
|
||||
if mode == "ldap_auth" {
|
||||
setting := LDAPSetting{
|
||||
URL: raw["LDAP_URL"],
|
||||
BaseDn: raw["LDAP_BASE_DN"],
|
||||
SearchDn: raw["LDAP_SEARCH_DN"],
|
||||
SearchPwd: raw["LDAP_SEARCH_PWD"],
|
||||
UID: raw["LDAP_UID"],
|
||||
Filter: raw["LDAP_FILTER"],
|
||||
Scope: raw["LDAP_SCOPE"],
|
||||
}
|
||||
config["ldap"] = setting
|
||||
}
|
||||
config["auth_mode"] = mode
|
||||
var tokenExpiration int = 30 //minutes
|
||||
if len(raw["TOKEN_EXPIRATION"]) > 0 {
|
||||
i, err := strconv.Atoi(raw["TOKEN_EXPIRATION"])
|
||||
if err != nil {
|
||||
log.Warningf("failed to parse token expiration: %v, using default value %d", err, tokenExpiration)
|
||||
} else if i <= 0 {
|
||||
log.Warningf("invalid token expiration, using default value: %d minutes", tokenExpiration)
|
||||
} else {
|
||||
tokenExpiration = i
|
||||
}
|
||||
}
|
||||
config["token_exp"] = tokenExpiration
|
||||
config["admin_password"] = raw["HARBOR_ADMIN_PASSWORD"]
|
||||
config["ext_reg_url"] = raw["EXT_REG_URL"]
|
||||
config["ui_secret"] = raw["UI_SECRET"]
|
||||
config["secret_key"] = raw["SECRET_KEY"]
|
||||
config["self_registration"] = raw["SELF_REGISTRATION"] != "off"
|
||||
config["admin_create_project"] = strings.ToLower(raw["PROJECT_CREATE_RESTRICTION"]) == "adminonly"
|
||||
registryURL := raw["REGISTRY_URL"]
|
||||
registryURL = strings.TrimRight(registryURL, "/")
|
||||
config["internal_registry_url"] = registryURL
|
||||
jobserviceURL := raw["JOB_SERVICE_URL"]
|
||||
jobserviceURL = strings.TrimRight(jobserviceURL, "/")
|
||||
config["internal_jobservice_url"] = jobserviceURL
|
||||
return nil
|
||||
}
|
||||
|
||||
var uiConfig *commonConfig.Config
|
||||
|
||||
func init() {
|
||||
uiKeys := []string{"AUTH_MODE", "LDAP_URL", "LDAP_BASE_DN", "LDAP_SEARCH_DN", "LDAP_SEARCH_PWD", "LDAP_UID", "LDAP_FILTER", "LDAP_SCOPE", "TOKEN_EXPIRATION", "HARBOR_ADMIN_PASSWORD", "EXT_REG_URL", "UI_SECRET", "SECRET_KEY", "SELF_REGISTRATION", "PROJECT_CREATE_RESTRICTION", "REGISTRY_URL", "JOB_SERVICE_URL"}
|
||||
uiConfig = &commonConfig.Config{
|
||||
Config: make(map[string]interface{}),
|
||||
Loader: &commonConfig.EnvConfigLoader{Keys: uiKeys},
|
||||
Parser: &uiParser{},
|
||||
}
|
||||
if err := uiConfig.Load(); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
func Reload() error {
|
||||
return uiConfig.Load()
|
||||
}
|
||||
|
||||
func AuthMode() string {
|
||||
return uiConfig.Config["auth_mode"].(string)
|
||||
}
|
||||
func LDAP() LDAPSetting {
|
||||
return uiConfig.Config["ldap"].(LDAPSetting)
|
||||
}
|
||||
|
||||
func TokenExpiration() int {
|
||||
return uiConfig.Config["token_exp"].(int)
|
||||
}
|
||||
|
||||
func ExtRegistryURL() string {
|
||||
return uiConfig.Config["ext_reg_url"].(string)
|
||||
}
|
||||
|
||||
func UISecret() string {
|
||||
return uiConfig.Config["ui_secret"].(string)
|
||||
}
|
||||
|
||||
func SecretKey() string {
|
||||
return uiConfig.Config["secret_key"].(string)
|
||||
}
|
||||
|
||||
func SelfRegistration() bool {
|
||||
return uiConfig.Config["self_registration"].(bool)
|
||||
}
|
||||
|
||||
func InternalRegistryURL() string {
|
||||
return uiConfig.Config["internal_registry_url"].(string)
|
||||
}
|
||||
|
||||
func InternalJobServiceURL() string {
|
||||
return uiConfig.Config["internal_jobservice_url"].(string)
|
||||
}
|
||||
|
||||
func OnlyAdminCreateProject() bool {
|
||||
return uiConfig.Config["admin_create_project"].(bool)
|
||||
}
|
138
src/ui/config/config_test.go
Normal file
138
src/ui/config/config_test.go
Normal file
@ -0,0 +1,138 @@
|
||||
/*
|
||||
Copyright (c) 2016 VMware, Inc. All Rights Reserved.
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
package config
|
||||
|
||||
import (
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
|
||||
var (
|
||||
auth = "ldap_auth"
|
||||
ldap = LDAPSetting{
|
||||
"ldap://test.ldap.com",
|
||||
"ou=people",
|
||||
"dc=whatever,dc=org",
|
||||
"1234567",
|
||||
"cn",
|
||||
"uid",
|
||||
"2",
|
||||
}
|
||||
tokenExp = "3"
|
||||
tokenExpRes = 3
|
||||
adminPassword = "password"
|
||||
externalRegURL = "127.0.0.1"
|
||||
uiSecret = "ffadsdfsdf"
|
||||
secretKey = "keykey"
|
||||
selfRegistration = "off"
|
||||
projectCreationRestriction = "everyone"
|
||||
internalRegistryURL = "http://registry:5000"
|
||||
jobServiceURL = "http://jobservice"
|
||||
)
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
|
||||
os.Setenv("AUTH_MODE", auth)
|
||||
os.Setenv("LDAP_URL", ldap.URL)
|
||||
os.Setenv("LDAP_BASE_DN", ldap.BaseDn)
|
||||
os.Setenv("LDAP_SEARCH_DN", ldap.SearchDn)
|
||||
os.Setenv("LDAP_SEARCH_PWD", ldap.SearchPwd)
|
||||
os.Setenv("LDAP_UID", ldap.UID)
|
||||
os.Setenv("LDAP_SCOPE", ldap.Scope)
|
||||
os.Setenv("LDAP_FILTER", ldap.Filter)
|
||||
os.Setenv("TOKEN_EXPIRATION", tokenExp)
|
||||
os.Setenv("HARBOR_ADMIN_PASSWORD", adminPassword)
|
||||
os.Setenv("EXT_REG_URL", externalRegURL)
|
||||
os.Setenv("UI_SECRET", uiSecret)
|
||||
os.Setenv("SECRET_KEY", secretKey)
|
||||
os.Setenv("SELF_REGISTRATION", selfRegistration)
|
||||
os.Setenv("CREATE_PROJECT_RESTRICTION", projectCreationRestriction)
|
||||
os.Setenv("REGISTRY_URL", internalRegistryURL)
|
||||
os.Setenv("JOB_SERVICE_URL", jobServiceURL)
|
||||
|
||||
err := Reload()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
rc := m.Run()
|
||||
|
||||
os.Unsetenv("AUTH_MODE")
|
||||
os.Unsetenv("LDAP_URL")
|
||||
os.Unsetenv("LDAP_BASE_DN")
|
||||
os.Unsetenv("LDAP_SEARCH_DN")
|
||||
os.Unsetenv("LDAP_SEARCH_PWD")
|
||||
os.Unsetenv("LDAP_UID")
|
||||
os.Unsetenv("LDAP_SCOPE")
|
||||
os.Unsetenv("LDAP_FILTER")
|
||||
os.Unsetenv("TOKEN_EXPIRATION")
|
||||
os.Unsetenv("HARBOR_ADMIN_PASSWORD")
|
||||
os.Unsetenv("EXT_REG_URL")
|
||||
os.Unsetenv("UI_SECRET")
|
||||
os.Unsetenv("SECRET_KEY")
|
||||
os.Unsetenv("SELF_REGISTRATION")
|
||||
os.Unsetenv("CREATE_PROJECT_RESTRICTION")
|
||||
os.Unsetenv("REGISTRY_URL")
|
||||
os.Unsetenv("JOB_SERVICE_URL")
|
||||
|
||||
os.Exit(rc)
|
||||
}
|
||||
|
||||
func TestAuth(t *testing.T) {
|
||||
if AuthMode() != auth {
|
||||
t.Errorf("Expected auth mode:%s, in fact: %s", auth, AuthMode())
|
||||
}
|
||||
if LDAP() != ldap {
|
||||
t.Errorf("Expected ldap setting: %+v, in fact: %+v", ldap, LDAP())
|
||||
}
|
||||
}
|
||||
|
||||
func TestTokenExpiration(t *testing.T) {
|
||||
if TokenExpiration() != tokenExpRes {
|
||||
t.Errorf("Expected token expiration: %d, in fact: %d", tokenExpRes, TokenExpiration())
|
||||
}
|
||||
}
|
||||
|
||||
func TestURLs(t *testing.T) {
|
||||
if InternalRegistryURL() != internalRegistryURL {
|
||||
t.Errorf("Expected internal Registry URL: %s, in fact: %s", internalRegistryURL, InternalRegistryURL())
|
||||
}
|
||||
if InternalJobServiceURL() != jobServiceURL {
|
||||
t.Errorf("Expected internal jobservice URL: %s, in fact: %s", jobServiceURL, InternalJobServiceURL())
|
||||
}
|
||||
if ExtRegistryURL() != externalRegURL {
|
||||
t.Errorf("Expected External Registry URL: %s, in fact: %s", externalRegURL, ExtRegistryURL())
|
||||
}
|
||||
}
|
||||
|
||||
func TestSelfRegistration(t *testing.T) {
|
||||
if SelfRegistration() {
|
||||
t.Errorf("Expected Self Registration to be false")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSecrets(t *testing.T) {
|
||||
if SecretKey() != secretKey {
|
||||
t.Errorf("Expected Secrect Key :%s, in fact: %s", secretKey, SecretKey())
|
||||
}
|
||||
if UISecret() != uiSecret {
|
||||
t.Errorf("Expected UI Secret: %s, in fact: %s", uiSecret, UISecret())
|
||||
}
|
||||
}
|
||||
|
||||
func TestProjectCreationRestrict(t *testing.T) {
|
||||
if OnlyAdminCreateProject() {
|
||||
t.Errorf("Expected OnlyAdminCreateProject to be false")
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user