diff --git a/make/common/templates/jobservice/env b/make/common/templates/jobservice/env index 51ce015a1..222eeb813 100644 --- a/make/common/templates/jobservice/env +++ b/make/common/templates/jobservice/env @@ -1,4 +1,4 @@ CORE_SECRET=$core_secret JOBSERVICE_SECRET=$jobservice_secret -ADMINSERVER_URL=$adminserver_url +CORE_URL=$core_url GODEBUG=netdns=cgo diff --git a/make/prepare b/make/prepare index f82c2eedc..9aa2a89e4 100755 --- a/make/prepare +++ b/make/prepare @@ -428,7 +428,7 @@ render(os.path.join(templates_dir, "jobservice", "env"), job_conf_env, core_secret=core_secret, jobservice_secret=jobservice_secret, - adminserver_url=adminserver_url) + core_url=core_url) render(os.path.join(templates_dir, "jobservice", "config.yml"), jobservice_conf, diff --git a/src/adminserver/client/client.go b/src/adminserver/client/client.go index ea88c2deb..a8ddec450 100644 --- a/src/adminserver/client/client.go +++ b/src/adminserver/client/client.go @@ -74,7 +74,7 @@ func (c *client) Ping() error { // GetCfgs ... func (c *client) GetCfgs() (map[string]interface{}, error) { - url := c.baseURL + "/api/configurations" + url := c.baseURL + "/api/configs" cfgs := map[string]interface{}{} if err := c.client.Get(url, &cfgs); err != nil { return nil, err diff --git a/src/adminserver/handlers/router.go b/src/adminserver/handlers/router.go index 900a83488..7eb28c147 100644 --- a/src/adminserver/handlers/router.go +++ b/src/adminserver/handlers/router.go @@ -23,8 +23,8 @@ import ( func newRouter() http.Handler { r := mux.NewRouter() - r.HandleFunc("/api/configurations", api.ListCfgs).Methods("GET") r.HandleFunc("/api/configurations", api.UpdateCfgs).Methods("PUT") + r.HandleFunc("/api/configs", api.ListCfgs).Methods("GET") r.HandleFunc("/api/configurations/reset", api.ResetCfgs).Methods("POST") r.HandleFunc("/api/systeminfo/capacity", api.Capacity).Methods("GET") r.HandleFunc("/api/ping", api.Ping).Methods("GET") diff --git a/src/common/utils/test/adminserver.go b/src/common/utils/test/adminserver.go index 0d7a9052d..b58bf880d 100644 --- a/src/common/utils/test/adminserver.go +++ b/src/common/utils/test/adminserver.go @@ -102,7 +102,7 @@ func NewAdminserver(config map[string]interface{}) (*httptest.Server, error) { m = append(m, &RequestHandlerMapping{ Method: "GET", - Pattern: "/api/configurations", + Pattern: "/api/configs", Handler: Handler(resp), }) diff --git a/src/core/api/config.go b/src/core/api/config.go index 59cce1e37..c48b36a7d 100644 --- a/src/core/api/config.go +++ b/src/core/api/config.go @@ -74,6 +74,17 @@ func (c *ConfigAPI) Get() { c.ServeJSON() } +// GetInternalConfig returns internal configurations +func (c *ConfigAPI) GetInternalConfig() { + configs, err := config.GetSystemCfg() + if err != nil { + log.Errorf("failed to get configurations: %v", err) + c.CustomAbort(http.StatusInternalServerError, http.StatusText(http.StatusInternalServerError)) + } + c.Data["json"] = configs + c.ServeJSON() +} + // Put updates configurations func (c *ConfigAPI) Put() { m := map[string]interface{}{} diff --git a/src/core/api/config_test.go b/src/core/api/config_test.go index f5d25f60a..6a647bcdf 100644 --- a/src/core/api/config_test.go +++ b/src/core/api/config_test.go @@ -55,6 +55,30 @@ func TestGetConfig(t *testing.T) { t.Logf("%v", ccc) } +func TestInternalConfig(t *testing.T) { + fmt.Println("Testing internal configurations") + assert := assert.New(t) + apiTest := newHarborAPI() + + // case 1: get configurations without admin role + code, _, err := apiTest.GetInternalConfig(*testUser) + if err != nil { + t.Fatalf("failed to get configurations: %v", err) + } + + assert.Equal(401, code, "the status code of getting configurations with non-admin user should be 401") + + // case 2: get configurations with admin role + code, _, err = apiTest.GetInternalConfig(*admin) + if err != nil { + t.Fatalf("failed to get configurations: %v", err) + } + + if !assert.Equal(200, code, "the status code of getting configurations with admin user should be 200") { + return + } +} + func TestPutConfig(t *testing.T) { fmt.Println("Testing modifying configurations") assert := assert.New(t) diff --git a/src/core/api/harborapi_test.go b/src/core/api/harborapi_test.go index 4dace6345..71bd87a58 100644 --- a/src/core/api/harborapi_test.go +++ b/src/core/api/harborapi_test.go @@ -161,6 +161,7 @@ func init() { beego.Router("/api/ldap/users/import", &LdapAPI{ldapConfig: ldapTestConfig, useTestConfig: true}, "post:ImportUser") beego.Router("/api/configurations", &ConfigAPI{}) beego.Router("/api/configurations/reset", &ConfigAPI{}, "post:Reset") + beego.Router("/api/configs", &ConfigAPI{}, "get:GetInternalConfig") beego.Router("/api/email/ping", &EmailAPI{}, "post:Ping") beego.Router("/api/replications", &ReplicationAPI{}) beego.Router("/api/labels", &LabelAPI{}, "post:Post;get:List") @@ -1090,6 +1091,18 @@ func (a testapi) GetConfig(authInfo usrInfo) (int, map[string]*value, error) { return code, cfg, err } +func (a testapi) GetInternalConfig(authInfo usrInfo) (int, map[string]interface{}, error) { + _sling := sling.New().Base(a.basePath).Get("/api/configs") + + cfg := map[string]interface{}{} + + code, body, err := request(_sling, jsonAcceptHeader, authInfo) + if err == nil && code == 200 { + err = json.Unmarshal(body, &cfg) + } + return code, cfg, err +} + func (a testapi) PutConfig(authInfo usrInfo, cfg map[string]interface{}) (int, error) { _sling := sling.New().Base(a.basePath).Put("/api/configurations").BodyJSON(cfg) diff --git a/src/core/router.go b/src/core/router.go index a12f49364..f1b259911 100644 --- a/src/core/router.go +++ b/src/core/router.go @@ -97,6 +97,7 @@ func initRouters() { beego.Router("/api/targets/:id([0-9]+)/policies/", &api.TargetAPI{}, "get:ListPolicies") beego.Router("/api/targets/ping", &api.TargetAPI{}, "post:Ping") beego.Router("/api/logs", &api.LogAPI{}) + beego.Router("/api/configs", &api.ConfigAPI{}, "get:GetInternalConfig") beego.Router("/api/configurations", &api.ConfigAPI{}) beego.Router("/api/configurations/reset", &api.ConfigAPI{}, "post:Reset") beego.Router("/api/statistics", &api.StatisticAPI{}) diff --git a/src/jobservice/config/config.go b/src/jobservice/config/config.go index 9a1e61bb1..9f38ce27f 100644 --- a/src/jobservice/config/config.go +++ b/src/jobservice/config/config.go @@ -27,7 +27,7 @@ const ( jobServiceLoggerBasePath = "JOB_SERVICE_LOGGER_BASE_PATH" jobServiceLoggerLevel = "JOB_SERVICE_LOGGER_LEVEL" jobServiceLoggerArchivePeriod = "JOB_SERVICE_LOGGER_ARCHIVE_PERIOD" - jobServiceAdminServerEndpoint = "ADMINSERVER_URL" + jobServiceCoreServerEndpoint = "CORE_URL" jobServiceAuthSecret = "JOBSERVICE_SECRET" // JobServiceProtocolHTTPS points to the 'https' protocol @@ -282,8 +282,8 @@ func (c *Configuration) loadEnvs() { } // admin server - if adminServer := utils.ReadEnv(jobServiceAdminServerEndpoint); !utils.IsEmptyStr(adminServer) { - c.AdminServer = adminServer + if coreServer := utils.ReadEnv(jobServiceCoreServerEndpoint); !utils.IsEmptyStr(coreServer) { + c.AdminServer = coreServer } }