From 039aef53565bead529275bf562db0e6217f34450 Mon Sep 17 00:00:00 2001 From: He Weiwei Date: Thu, 9 Jul 2020 15:26:14 +0000 Subject: [PATCH] refactor: remove initialization of clair db To fetch vulnerability database updated time of the Clair had moved to the Clair adapter so removes the initialization of clair db in the core. Signed-off-by: He Weiwei --- make/photon/prepare/templates/core/env.jinja | 6 ------ src/common/config/manager_test.go | 21 ++++++++++++++++---- src/common/config/metadata/metadatalist.go | 6 ------ src/common/const.go | 7 ------- src/common/dao/base.go | 19 ------------------ src/common/utils/test/config.go | 5 ----- src/common/utils/test/test.go | 1 - src/core/config/config.go | 13 ------------ src/core/config/config_test.go | 9 --------- src/core/main.go | 8 -------- 10 files changed, 17 insertions(+), 78 deletions(-) diff --git a/make/photon/prepare/templates/core/env.jinja b/make/photon/prepare/templates/core/env.jinja index eadbb9071..1d4767359 100644 --- a/make/photon/prepare/templates/core/env.jinja +++ b/make/photon/prepare/templates/core/env.jinja @@ -26,12 +26,6 @@ JOBSERVICE_SECRET={{jobservice_secret}} WITH_NOTARY={{with_notary}} WITH_CLAIR={{with_clair}} WITH_TRIVY={{with_trivy}} -CLAIR_DB_PASSWORD={{clair_db_password}} -CLAIR_DB_HOST={{clair_db_host}} -CLAIR_DB_PORT={{clair_db_port}} -CLAIR_DB_USERNAME={{clair_db_username}} -CLAIR_DB={{clair_db_name}} -CLAIR_DB_SSLMODE={{clair_db_sslmode}} CORE_URL={{core_url}} CORE_LOCAL_URL={{core_local_url}} JOBSERVICE_URL={{jobservice_url}} diff --git a/src/common/config/manager_test.go b/src/common/config/manager_test.go index 1d65bfc79..00f9fa15e 100644 --- a/src/common/config/manager_test.go +++ b/src/common/config/manager_test.go @@ -1,3 +1,17 @@ +// Copyright Project Harbor Authors +// +// 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 ( @@ -76,14 +90,13 @@ func TestCfgManager_loadDefaultValues(t *testing.T) { } func TestCfgManger_loadSystemValues(t *testing.T) { - // os.Setenv("CLAIR_DB", "mysql") configManager.loadDefault() configManager.loadSystemConfigFromEnv() configManager.UpdateConfig(map[string]interface{}{ - "clair_db": "mysql", + "postgresql_host": "127.0.0.1", }) - if configManager.Get("clair_db").GetString() != "mysql" { - t.Errorf("Failed to set system value clair_db, expected %v, actual %v", "mysql", configManager.Get("clair_db").GetString()) + if configManager.Get("postgresql_host").GetString() != "127.0.0.1" { + t.Errorf("Failed to set system value postgresql_host, expected %v, actual %v", "127.0.0.1", configManager.Get("postgresql_host").GetString()) } } func TestCfgManager_GetDatabaseCfg(t *testing.T) { diff --git a/src/common/config/metadata/metadatalist.go b/src/common/config/metadata/metadatalist.go index 08d6fc5f0..dfc635b43 100644 --- a/src/common/config/metadata/metadatalist.go +++ b/src/common/config/metadata/metadatalist.go @@ -65,12 +65,6 @@ var ( {Name: common.AUTHMode, Scope: UserScope, Group: BasicGroup, EnvKey: "AUTH_MODE", DefaultValue: "db_auth", ItemType: &AuthModeType{}, Editable: false}, {Name: common.ChartRepoURL, Scope: SystemScope, Group: BasicGroup, EnvKey: "CHART_REPOSITORY_URL", DefaultValue: "http://chartmuseum:9999", ItemType: &StringType{}, Editable: false}, - {Name: common.ClairDB, Scope: SystemScope, Group: ClairGroup, EnvKey: "CLAIR_DB", DefaultValue: "postgres", ItemType: &StringType{}, Editable: false}, - {Name: common.ClairDBHost, Scope: SystemScope, Group: ClairGroup, EnvKey: "CLAIR_DB_HOST", DefaultValue: "postgresql", ItemType: &StringType{}, Editable: false}, - {Name: common.ClairDBPassword, Scope: SystemScope, Group: ClairGroup, EnvKey: "CLAIR_DB_PASSWORD", DefaultValue: "root123", ItemType: &PasswordType{}, Editable: false}, - {Name: common.ClairDBPort, Scope: SystemScope, Group: ClairGroup, EnvKey: "CLAIR_DB_PORT", DefaultValue: "5432", ItemType: &PortType{}, Editable: false}, - {Name: common.ClairDBSSLMode, Scope: SystemScope, Group: ClairGroup, EnvKey: "CLAIR_DB_SSLMODE", DefaultValue: "disable", ItemType: &StringType{}, Editable: false}, - {Name: common.ClairDBUsername, Scope: SystemScope, Group: ClairGroup, EnvKey: "CLAIR_DB_USERNAME", DefaultValue: "postgres", ItemType: &StringType{}, Editable: false}, {Name: common.ClairAdapterURL, Scope: SystemScope, Group: ClairGroup, EnvKey: "CLAIR_ADAPTER_URL", DefaultValue: "http://clair-adapter:8080", ItemType: &StringType{}, Editable: false}, {Name: common.TrivyAdapterURL, Scope: SystemScope, Group: ClairGroup, EnvKey: "TRIVY_ADAPTER_URL", DefaultValue: "http://trivy-adapter:8080", ItemType: &StringType{}, Editable: false}, diff --git a/src/common/const.go b/src/common/const.go index 1d24e8c00..229cd7136 100755 --- a/src/common/const.go +++ b/src/common/const.go @@ -91,12 +91,6 @@ const ( WithClair = "with_clair" WithTrivy = "with_trivy" ScanAllPolicy = "scan_all_policy" - ClairDBPassword = "clair_db_password" - ClairDBHost = "clair_db_host" - ClairDBPort = "clair_db_port" - ClairDB = "clair_db" - ClairDBUsername = "clair_db_username" - ClairDBSSLMode = "clair_db_sslmode" UAAEndpoint = "uaa_endpoint" UAAClientID = "uaa_client_id" UAAClientSecret = "uaa_client_secret" @@ -114,7 +108,6 @@ const ( OIDCGroupsClaim = "oidc_groups_claim" OIDCScope = "oidc_scope" - DefaultClairEndpoint = "http://clair:6060" CfgDriverDB = "db" NewHarborAdminName = "admin@harbor.local" RegistryStorageProviderName = "registry_storage_provider_name" diff --git a/src/common/dao/base.go b/src/common/dao/base.go index d9f2632cb..64212dd9b 100644 --- a/src/common/dao/base.go +++ b/src/common/dao/base.go @@ -29,8 +29,6 @@ import ( const ( // NonExistUserID : if a user does not exist, the ID of the user will be 0. NonExistUserID = 0 - // ClairDBAlias ... - ClairDBAlias = "clair-db" ) // ErrDupRows is returned by DAO when inserting failed with error "duplicate key value violates unique constraint" @@ -48,23 +46,6 @@ type Database interface { UpgradeSchema() error } -// InitClairDB ... -func InitClairDB(clairDB *models.PostGreSQL) error { - p := &pgsql{ - host: clairDB.Host, - port: strconv.Itoa(clairDB.Port), - usr: clairDB.Username, - pwd: clairDB.Password, - database: clairDB.Database, - sslmode: clairDB.SSLMode, - } - if err := p.Register(ClairDBAlias); err != nil { - return err - } - log.Info("initialized clair database") - return nil -} - // UpgradeSchema will call the internal migrator to upgrade schema based on the setting of database. func UpgradeSchema(database *models.Database) error { db, err := getDatabase(database) diff --git a/src/common/utils/test/config.go b/src/common/utils/test/config.go index 6569b1045..294d119a1 100644 --- a/src/common/utils/test/config.go +++ b/src/common/utils/test/config.go @@ -56,11 +56,6 @@ var defaultConfig = map[string]interface{}{ common.AdminInitialPassword: "password", common.WithNotary: false, common.WithClair: false, - common.ClairDBUsername: "postgres", - common.ClairDBHost: "postgresql", - common.ClairDB: "postgres", - common.ClairDBPort: 5432, - common.ClairDBPassword: "root123", common.UAAClientID: "testid", common.UAAClientSecret: "testsecret", common.UAAEndpoint: "10.192.168.5", diff --git a/src/common/utils/test/test.go b/src/common/utils/test/test.go index 12527c736..950475cc0 100644 --- a/src/common/utils/test/test.go +++ b/src/common/utils/test/test.go @@ -115,7 +115,6 @@ func GetUnitTestConfig() map[string]interface{} { common.LDAPTimeout: 30, common.LDAPVerifyCert: true, common.UAAVerifyCert: true, - common.ClairDBHost: "postgresql", common.AdminInitialPassword: "Harbor12345", common.LDAPGroupSearchFilter: "objectclass=groupOfNames", common.LDAPGroupBaseDN: "dc=example,dc=com", diff --git a/src/core/config/config.go b/src/core/config/config.go index a7b5f9a48..4a24659eb 100755 --- a/src/core/config/config.go +++ b/src/core/config/config.go @@ -335,19 +335,6 @@ func WithClair() bool { return cfgMgr.Get(common.WithClair).GetBool() } -// ClairDB return Clair db info -func ClairDB() (*models.PostGreSQL, error) { - clairDB := &models.PostGreSQL{ - Host: cfgMgr.Get(common.ClairDBHost).GetString(), - Port: cfgMgr.Get(common.ClairDBPort).GetInt(), - Username: cfgMgr.Get(common.ClairDBUsername).GetString(), - Password: cfgMgr.Get(common.ClairDBPassword).GetString(), - Database: cfgMgr.Get(common.ClairDB).GetString(), - SSLMode: cfgMgr.Get(common.ClairDBSSLMode).GetString(), - } - return clairDB, nil -} - // ClairAdapterEndpoint returns the endpoint of clair adapter instance, by default it's the one deployed within Harbor. func ClairAdapterEndpoint() string { return cfgMgr.Get(common.ClairAdapterURL).GetString() diff --git a/src/core/config/config_test.go b/src/core/config/config_test.go index 005d6e7ea..0993453e7 100644 --- a/src/core/config/config_test.go +++ b/src/core/config/config_test.go @@ -137,17 +137,8 @@ func TestConfig(t *testing.T) { t.Fatalf("failed to get database: %v", err) } - clairDB, err := ClairDB() - if err != nil { - t.Fatalf("failed to get clair DB %v", err) - } defaultConfig := test.GetDefaultConfigMap() Upload(defaultConfig) - assert.Equal(defaultConfig[common.ClairDB], clairDB.Database) - assert.Equal(defaultConfig[common.ClairDBUsername], clairDB.Username) - assert.Equal(defaultConfig[common.ClairDBPassword], clairDB.Password) - assert.Equal(defaultConfig[common.ClairDBHost], clairDB.Host) - assert.Equal(defaultConfig[common.ClairDBPort], clairDB.Port) if InternalNotaryEndpoint() != "http://notary-server:4443" { t.Errorf("Unexpected notary endpoint: %s", InternalNotaryEndpoint()) diff --git a/src/core/main.go b/src/core/main.go index 07ff85ed0..36c49698b 100755 --- a/src/core/main.go +++ b/src/core/main.go @@ -197,14 +197,6 @@ func registerScanners() { } if config.WithClair() { - clairDB, err := config.ClairDB() - if err != nil { - log.Fatalf("failed to load clair database information: %v", err) - } - if err := dao.InitClairDB(clairDB); err != nil { - log.Fatalf("failed to initialize clair database: %v", err) - } - log.Info("Registering Clair scanner") wantedScanners = append(wantedScanners, scanner.Registration{ Name: clairScanner,