diff --git a/.travis.yml b/.travis.yml index 42ff3124e..336c92a06 100644 --- a/.travis.yml +++ b/.travis.yml @@ -89,8 +89,8 @@ script: - sudo mkdir -p ./make/common/config/registry/ - sudo mv ./tests/reg_config.yml ./make/common/config/registry/config.yml - sudo docker-compose -f ./make/docker-compose.test.yml up -d - - go list ./... | grep -v -E 'vendor|tests' | xargs -L1 fgt golint - - go list ./... | grep -v -E 'vendor|tests' | xargs -L1 go vet + - go list ./... | grep -v -E 'vendor|tests|test' | xargs -L1 fgt golint + - go list ./... | grep -v -E 'vendor|tests|test' | xargs -L1 go vet - export MYSQL_HOST=$IP - export REGISTRY_URL=$IP:5000 - echo $REGISTRY_URL diff --git a/docs/swagger.yaml b/docs/swagger.yaml index 231c8cb78..94f4cc70a 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -1391,6 +1391,32 @@ paths: description: User need to login first. '500': description: Unexpected internal errors. + put: + summary: Update status of jobs. Only stop is supported for now. + description: > + The endpoint is used to stop the replication jobs of a policy. + tags: + - Products + parameters: + - name: policyinfo + in: body + description: The policy ID and status. + required: true + schema: + $ref: '#/definitions/UpdateJobs' + responses: + '200': + description: Update the status successfully. + '400': + description: Bad request because of invalid parameters. + '401': + description: User need to login first. + '403': + description: User has no privilege for the operation. + '404': + description: Resource requested does not exist. + '500': + description: Unexpected internal errors. /jobs/replication/{id}: delete: summary: Delete specific ID job. @@ -1511,7 +1537,7 @@ paths: description: Create new policy. required: true schema: - $ref: '#/definitions/RepPolicyPost' + $ref: '#/definitions/RepPolicy' tags: - Products responses: @@ -1566,10 +1592,10 @@ paths: description: policy ID - name: policyupdate in: body - description: 'Update policy name, description, target and enablement.' + description: 'Updated properties of the replication policy.' required: true schema: - $ref: '#/definitions/RepPolicyUpdate' + $ref: '#/definitions/RepPolicy' tags: - Products responses: @@ -1587,35 +1613,27 @@ paths: project and target. '500': description: Unexpected internal errors. - /policies/replication/{id}/enablement: - put: - summary: Put modifies enablement of the policy. + /replications: + post: + summary: Trigger the replication according to the specified policy. description: | - This endpoint let user update policy enablement flag. + This endpoint is used to trigger a replication. parameters: - - name: id - in: path - type: integer - format: int64 - required: true - description: policy ID - - name: enabledflag + - name: policy ID in: body - description: The policy enablement flag. + description: The ID of replication policy. required: true schema: - $ref: '#/definitions/RepPolicyEnablementReq' + $ref: '#/definitions/Replication' tags: - Products responses: '200': - description: Update job policy enablement successfully. - '400': - description: Invalid enabled value. + description: Trigger the replication successfully. '401': description: User need to log in first. '404': - description: The specific repository ID's policy does not exist. + description: The policy does not exist. '500': description: Unexpected internal errors. /targets: @@ -2378,33 +2396,35 @@ definitions: type: integer format: int64 description: The policy ID. - project_id: - type: integer - format: int64 - description: The project ID. - project_name: - type: string - description: The project name. - target_id: - type: integer - format: int64 - description: The target ID. name: type: string description: The policy name. - enabled: - type: integer - format: int - description: The policy's enabled status. description: type: string description: The description of the policy. - cron_str: - type: string - description: The cron string for schedule job. - start_time: - type: string - description: The start time of the policy. + projects: + type: array + description: The project list that the policy applys to. + items: + $ref: '#/definitions/Project' + targets: + type: array + description: The target list. + items: + $ref: '#/definitions/RepTarget' + trigger: + $ref: '#/definitions/RepTrigger' + filters: + type: array + description: The replication policy filter array. + items: + $ref: '#/definitions/RepFilter' + replicate_existing_image_now: + type: boolean + description: Whether to replicate the existing images now. + replicate_deletion: + type: boolean + description: Whether to replicate the deletion operation. creation_time: type: string description: The create time of the policy. @@ -2412,55 +2432,42 @@ definitions: type: string description: The update time of the policy. error_job_count: - format: int + type: integer description: The error job count number for the policy. - deleted: - type: integer - RepPolicyPost: + RepTrigger: type: object properties: - project_id: - type: integer - format: int64 - description: The project ID. - target_id: - type: integer - format: int64 - description: The target ID. - name: + kind: type: string - description: The policy name. - enabled: - type: integer - format: int - description: '1-enable, 0-disable' - RepPolicyUpdate: + description: The replication policy trigger kind. The valid values are manual, immediate and schedule. + schedule_param: + $ref: '#/definitions/ScheduleParam' + ScheduleParam: type: object properties: - target_id: + type: + type: string + description: The schedule type. The valid values are daily and weekly. + weekday: + type: integer + format: int8 + description: Optional, only used when the type is weedly. The valid values are 1-7. + offtime: type: integer format: int64 - description: The target ID. - name: - type: string - description: The policy name. - enabled: - type: integer - format: int - description: The policy's enabled status. - description: - type: string - description: The description of the policy. - cron_str: - type: string - description: The cron string for schedule job. - RepPolicyEnablementReq: + description: The time offset with the UTC 00:00 in seconds. + RepFilter: type: object properties: - enabled: - type: integer - format: int - description: The policy enablement flag. + kind: + type: string + description: The replication policy filter kind. The valid values are project, repository and tag. + pattern: + type: string + description: The replication policy filter pattern. + metadata: + type: object + description: This map object is the replication policy filter metadata. RepTarget: type: object properties: @@ -2947,12 +2954,24 @@ definitions: type: integer description: The offest in seconds of UTC 0 o'clock, only valid when the policy type is "daily" description: The parameters of the policy, the values are dependant on the type of the policy. + Replication: + type: object + properties: + policy_id: + type: integer + description: The ID of replication policy RepositoryDescription: type: object properties: description: type: string description: The description of the repository. - - - + UpdateJobs: + type: object + properties: + policy_id: + type: integer + description: The ID of replication policy + status: + type: string + description: The status of jobs. The only valid value is stop for now. \ No newline at end of file diff --git a/make/common/templates/registry/config.yml b/make/common/templates/registry/config.yml index c49805a04..e30dfb47c 100644 --- a/make/common/templates/registry/config.yml +++ b/make/common/templates/registry/config.yml @@ -6,8 +6,7 @@ log: storage: cache: layerinfo: inmemory - filesystem: - rootdirectory: /storage + $storage_provider_info maintenance: uploadpurging: enabled: false diff --git a/make/common/templates/registry/config_ha.yml b/make/common/templates/registry/config_ha.yml index f3b04fcb1..e4570db2c 100644 --- a/make/common/templates/registry/config_ha.yml +++ b/make/common/templates/registry/config_ha.yml @@ -6,7 +6,7 @@ log: storage: cache: layerinfo: redis - Place_holder_for_Storage_configureation + $storage_provider_info maintenance: uploadpurging: enabled: false diff --git a/make/harbor.cfg b/make/harbor.cfg index 156a6acfa..96032547e 100644 --- a/make/harbor.cfg +++ b/make/harbor.cfg @@ -133,14 +133,21 @@ clair_db_username = postgres #Clair default database clair_db = postgres - - ################### end of HA section ##################### + #************************END INITIAL PROPERTIES************************ + #The following attributes only need to be set when auth mode is uaa_auth uaa_endpoint = uaa.mydomain.org uaa_clientid = id uaa_clientsecret = secret uaa_verify_cert = true uaa_ca_cert = /path/to/ca.pem -############# + + +### Docker Registry setting ### +#registry_storage_provider can be: filesystem, s3, gcs, azure, etc. +registry_storage_provider_name = filesystem +#registry_storage_provider_config is a comma separated "key: value" pairs, e.g. "key1: value, key2: value2". +#Refer to https://docs.docker.com/registry/configuration/#storage for all available configuration. +registry_storage_provider_config = diff --git a/make/photon/db/registry.sql b/make/photon/db/registry.sql index e63ac8ac0..1276a129b 100644 --- a/make/photon/db/registry.sql +++ b/make/photon/db/registry.sql @@ -147,6 +147,8 @@ create table replication_policy ( description text, deleted tinyint (1) DEFAULT 0 NOT NULL, cron_str varchar(256), + filters varchar(1024), + replicate_deletion tinyint (1) DEFAULT 0 NOT NULL, start_time timestamp NULL, creation_time timestamp default CURRENT_TIMESTAMP, update_time timestamp default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP, @@ -184,6 +186,17 @@ create table replication_job ( INDEX policy (policy_id), INDEX poid_uptime (policy_id, update_time) ); + +create table replication_immediate_trigger ( + id int NOT NULL AUTO_INCREMENT, + policy_id int NOT NULL, + namespace varchar(256) NOT NULL, + on_push tinyint(1) NOT NULL DEFAULT 0, + on_deletion tinyint(1) NOT NULL DEFAULT 0, + creation_time timestamp default CURRENT_TIMESTAMP, + update_time timestamp default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP, + PRIMARY KEY (id) + ); create table img_scan_job ( id int NOT NULL AUTO_INCREMENT, diff --git a/make/photon/db/registry_sqlite.sql b/make/photon/db/registry_sqlite.sql index 99ce73fc4..ce63d7d3d 100644 --- a/make/photon/db/registry_sqlite.sql +++ b/make/photon/db/registry_sqlite.sql @@ -142,6 +142,8 @@ create table replication_policy ( description text, deleted tinyint (1) DEFAULT 0 NOT NULL, cron_str varchar(256), + filters varchar(1024), + replicate_deletion tinyint (1) DEFAULT 0 NOT NULL, start_time timestamp NULL, creation_time timestamp default CURRENT_TIMESTAMP, update_time timestamp default CURRENT_TIMESTAMP @@ -175,6 +177,16 @@ create table replication_job ( update_time timestamp default CURRENT_TIMESTAMP ); +create table replication_immediate_trigger ( + id INTEGER PRIMARY KEY, + policy_id int NOT NULL, + namespace varchar(256) NOT NULL, + on_push tinyint(1) NOT NULL DEFAULT 0, + on_deletion tinyint(1) NOT NULL DEFAULT 0, + creation_time timestamp default CURRENT_TIMESTAMP, + update_time timestamp default CURRENT_TIMESTAMP + ); + create table img_scan_job ( id INTEGER PRIMARY KEY, diff --git a/make/prepare b/make/prepare index ba5e7476c..6658de07e 100755 --- a/make/prepare +++ b/make/prepare @@ -23,13 +23,16 @@ def validate(conf, args): if args.ha_mode: db_host = rcp.get("configuration", "db_host") if db_host == "mysql": - raise Exception("Error: In HA mode, db_host in harbor.cfg needs to point to an external DB address") - registry_config_path = os.path.join(templates_dir,"registry","config_ha.yml") - if check_storage_config(registry_config_path): - raise Exception("Error: In HA model shared storage configuration is required registry, refer HA installation guide for detail.") + raise Exception("Error: In HA mode, db_host in harbor.cfg needs to point to an external DB address.") + registry_storage_provider_name = rcp.get("configuration", + "registry_storage_provider_name").strip() + if registry_storage_provider_name == "filesystem" and not args.yes: + msg = 'Is the Harbor Docker Registry configured to use shared storage (e.g. NFS, S3, GCS, etc.)? [yes/no]:' + if raw_input(msg).lower() != "yes": + raise Exception("Error: In HA mode, shared storage configuration for Docker Registry in harbor.cfg is required. Refer to HA installation guide for details.") redis_url = rcp.get("configuration", "redis_url") if redis_url is None or len(redis_url) < 1: - raise Exception("Error: In HA mode redis is required redis_url need to point to an redis cluster") + raise Exception("Error: In HA mode, redis_url in harbor.cfg needs to point to a Redis cluster.") if args.notary_mode: raise Exception("Error: HA mode doesn't support Notary currently") if args.clair_mode: @@ -117,11 +120,6 @@ def prepare_ha(conf, args): if os.path.isfile(secret_key): shutil.copy2(secret_key, shared_secret_key) -def check_storage_config(path): - if 'Place_holder_for_Storage_configureation' in open(path).read(): - return True - return False - def get_secret_key(path): secret_key = _get_secret(path, "secretkey") if len(secret_key) != 16: @@ -180,6 +178,7 @@ parser.add_argument('--conf', dest='cfgfile', default=base_dir+'/harbor.cfg',typ parser.add_argument('--with-notary', dest='notary_mode', default=False, action='store_true', help="the Harbor instance is to be deployed with notary") parser.add_argument('--with-clair', dest='clair_mode', default=False, action='store_true', help="the Harbor instance is to be deployed with clair") parser.add_argument('--ha', dest='ha_mode', default=False, action='store_true', help="the Harbor instance is to be deployed in HA mode") +parser.add_argument('--yes', dest='yes', default=False, action='store_true', help="Answer yes to all questions") args = parser.parse_args() delfile(config_dir) @@ -260,7 +259,6 @@ if rcp.has_option("configuration", "redis_url"): redis_url = rcp.get("configuration", "redis_url") else: redis_url = "" -######## ui_secret = ''.join(random.choice(string.ascii_letters+string.digits) for i in range(16)) jobservice_secret = ''.join(random.choice(string.ascii_letters+string.digits) for i in range(16)) @@ -357,17 +355,23 @@ render(os.path.join(templates_dir, "ui", "env"), jobservice_secret=jobservice_secret, redis_url = redis_url ) -if args.ha_mode: - render(os.path.join(templates_dir, "registry", - "config_ha.yml"), - registry_conf, - ui_url=ui_url, - redis_url=redis_url) -else: - render(os.path.join(templates_dir, "registry", - "config.yml"), - registry_conf, - ui_url=ui_url) + +registry_config_file = "config_ha.yml" if args.ha_mode else "config.yml" +storage_provider_name = rcp.get("configuration", "registry_storage_provider_name").strip() +storage_provider_config = rcp.get("configuration", "registry_storage_provider_config").strip() +if storage_provider_name == "filesystem": + if not storage_provider_config: + storage_provider_config = "rootdirectory: /storage" + elif "rootdirectory:" not in storage_provider_config: + storage_provider_config = "rootdirectory: /storage" + "," + storage_provider_config +# generate storage configuration section in yaml format +storage_provider_info = ('\n' + ' ' * 4).join( + [storage_provider_name + ':'] + map(string.strip, storage_provider_config.split(","))) +render(os.path.join(templates_dir, "registry", registry_config_file), + registry_conf, + storage_provider_info=storage_provider_info, + ui_url=ui_url, + redis_url=redis_url) render(os.path.join(templates_dir, "db", "env"), db_conf_env, diff --git a/src/adminserver/client/client.go b/src/adminserver/client/client.go index 95fbdf0f8..115e903b6 100644 --- a/src/adminserver/client/client.go +++ b/src/adminserver/client/client.go @@ -15,16 +15,11 @@ package client import ( - "bytes" - "encoding/json" - "fmt" - "io" - "io/ioutil" - "net/http" "strings" - "github.com/vmware/harbor/src/adminserver/client/auth" "github.com/vmware/harbor/src/adminserver/systeminfo/imagestorage" + "github.com/vmware/harbor/src/common/http" + "github.com/vmware/harbor/src/common/http/modifier/auth" "github.com/vmware/harbor/src/common/utils" ) @@ -43,38 +38,29 @@ type Client interface { } // NewClient return an instance of Adminserver client -func NewClient(baseURL string, authorizer auth.Authorizer) Client { +func NewClient(baseURL string, cfg *Config) Client { baseURL = strings.TrimRight(baseURL, "/") if !strings.Contains(baseURL, "://") { baseURL = "http://" + baseURL } - return &client{ - baseURL: baseURL, - client: &http.Client{}, - authorizer: authorizer, + client := &client{ + baseURL: baseURL, } + if cfg != nil { + authorizer := auth.NewSecretAuthorizer(cfg.Secret) + client.client = http.NewClient(nil, authorizer) + } + return client } type client struct { - baseURL string - client *http.Client - authorizer auth.Authorizer + baseURL string + client *http.Client } -// do creates request and authorizes it if authorizer is not nil -func (c *client) do(method, relativePath string, body io.Reader) (*http.Response, error) { - url := c.baseURL + relativePath - req, err := http.NewRequest(method, url, body) - if err != nil { - return nil, err - } - - if c.authorizer != nil { - if err := c.authorizer.Authorize(req); err != nil { - return nil, err - } - } - return c.client.Do(req) +// Config contains configurations needed for client +type Config struct { + Secret string } func (c *client) Ping() error { @@ -88,96 +74,32 @@ func (c *client) Ping() error { // GetCfgs ... func (c *client) GetCfgs() (map[string]interface{}, error) { - resp, err := c.do(http.MethodGet, "/api/configurations", nil) - if err != nil { - return nil, err - } - defer resp.Body.Close() - - b, err := ioutil.ReadAll(resp.Body) - if err != nil { - return nil, err - } - - if resp.StatusCode != http.StatusOK { - return nil, fmt.Errorf("failed to get configurations: %d %s", - resp.StatusCode, b) - } - + url := c.baseURL + "/api/configurations" cfgs := map[string]interface{}{} - if err = json.Unmarshal(b, &cfgs); err != nil { + if err := c.client.Get(url, &cfgs); err != nil { return nil, err } - return cfgs, nil } // UpdateCfgs ... func (c *client) UpdateCfgs(cfgs map[string]interface{}) error { - data, err := json.Marshal(cfgs) - if err != nil { - return err - } - - resp, err := c.do(http.MethodPut, "/api/configurations", bytes.NewReader(data)) - if err != nil { - return err - } - defer resp.Body.Close() - - if resp.StatusCode != http.StatusOK { - b, err := ioutil.ReadAll(resp.Body) - if err != nil { - return err - } - return fmt.Errorf("failed to update configurations: %d %s", - resp.StatusCode, b) - } - - return nil + url := c.baseURL + "/api/configurations" + return c.client.Put(url, cfgs) } // ResetCfgs ... func (c *client) ResetCfgs() error { - resp, err := c.do(http.MethodPost, "/api/configurations/reset", nil) - if err != nil { - return err - } - - if resp.StatusCode != http.StatusOK { - b, err := ioutil.ReadAll(resp.Body) - if err != nil { - return err - } - return fmt.Errorf("failed to reset configurations: %d %s", - resp.StatusCode, b) - } - - return nil + url := c.baseURL + "/api/configurations/reset" + return c.client.Post(url) } // Capacity ... func (c *client) Capacity() (*imagestorage.Capacity, error) { - resp, err := c.do(http.MethodGet, "/api/systeminfo/capacity", nil) - if err != nil { - return nil, err - } - defer resp.Body.Close() - - b, err := ioutil.ReadAll(resp.Body) - if err != nil { - return nil, err - } - - if resp.StatusCode != http.StatusOK { - return nil, fmt.Errorf("failed to get capacity: %d %s", - resp.StatusCode, b) - } - + url := c.baseURL + "/api/systeminfo/capacity" capacity := &imagestorage.Capacity{} - if err = json.Unmarshal(b, capacity); err != nil { + if err := c.client.Get(url, capacity); err != nil { return nil, err } - return capacity, nil } diff --git a/src/adminserver/client/client_test.go b/src/adminserver/client/client_test.go index c232288c1..59d407a34 100644 --- a/src/adminserver/client/client_test.go +++ b/src/adminserver/client/client_test.go @@ -34,7 +34,7 @@ func TestMain(m *testing.M) { os.Exit(1) } - c = NewClient(server.URL, nil) + c = NewClient(server.URL, &Config{}) os.Exit(m.Run()) } diff --git a/src/common/dao/dao_test.go b/src/common/dao/dao_test.go index a9cbbe8e4..9be616be1 100644 --- a/src/common/dao/dao_test.go +++ b/src/common/dao/dao_test.go @@ -941,7 +941,6 @@ func TestFilterRepTargets(t *testing.T) { func TestAddRepPolicy(t *testing.T) { policy := models.RepPolicy{ ProjectID: 1, - Enabled: 1, TargetID: targetID, Description: "whatever", Name: "mypolicy", @@ -961,15 +960,10 @@ func TestAddRepPolicy(t *testing.T) { t.Errorf("Unable to find a policy with id: %d", id) } - if p.Name != "mypolicy" || p.TargetID != targetID || p.Enabled != 1 || p.Description != "whatever" { - t.Errorf("The data does not match, expected: Name: mypolicy, TargetID: %d, Enabled: 1, Description: whatever;\n result: Name: %s, TargetID: %d, Enabled: %d, Description: %s", - targetID, p.Name, p.TargetID, p.Enabled, p.Description) + if p.Name != "mypolicy" || p.TargetID != targetID || p.Description != "whatever" { + t.Errorf("The data does not match, expected: Name: mypolicy, TargetID: %d, Description: whatever;\n result: Name: %s, TargetID: %d, Description: %s", + targetID, p.Name, p.TargetID, p.Description) } - var tm = time.Now().AddDate(0, 0, -1) - if !p.StartTime.After(tm) { - t.Errorf("Unexpected start_time: %v", p.StartTime) - } - } func TestGetRepPolicyByTarget(t *testing.T) { @@ -1019,44 +1013,9 @@ func TestGetRepPolicyByName(t *testing.T) { } -func TestDisableRepPolicy(t *testing.T) { - err := DisableRepPolicy(policyID) - if err != nil { - t.Errorf("Failed to disable policy, id: %d", policyID) - } - p, err := GetRepPolicy(policyID) - if err != nil { - t.Errorf("Error occurred in GetPolicy: %v, id: %d", err, policyID) - } - if p == nil { - t.Errorf("Unable to find a policy with id: %d", policyID) - } - if p.Enabled == 1 { - t.Errorf("The Enabled value of replication policy is still 1 after disabled, id: %d", policyID) - } -} - -func TestEnableRepPolicy(t *testing.T) { - err := EnableRepPolicy(policyID) - if err != nil { - t.Errorf("Failed to disable policy, id: %d", policyID) - } - p, err := GetRepPolicy(policyID) - if err != nil { - t.Errorf("Error occurred in GetPolicy: %v, id: %d", err, policyID) - } - if p == nil { - t.Errorf("Unable to find a policy with id: %d", policyID) - } - if p.Enabled == 0 { - t.Errorf("The Enabled value of replication policy is still 0 after disabled, id: %d", policyID) - } -} - func TestAddRepPolicy2(t *testing.T) { policy2 := models.RepPolicy{ ProjectID: 3, - Enabled: 0, TargetID: 3, Description: "whatever", Name: "mypolicy", @@ -1073,10 +1032,6 @@ func TestAddRepPolicy2(t *testing.T) { if p == nil { t.Errorf("Unable to find a policy with id: %d", policyID2) } - var tm time.Time - if p.StartTime.After(tm) { - t.Errorf("Unexpected start_time: %v", p.StartTime) - } } func TestAddRepJob(t *testing.T) { diff --git a/src/common/dao/replication_job.go b/src/common/dao/replication_job.go index b53fcfd08..5a2639bd0 100644 --- a/src/common/dao/replication_job.go +++ b/src/common/dao/replication_job.go @@ -106,34 +106,26 @@ func FilterRepTargets(name string) ([]*models.RepTarget, error) { // AddRepPolicy ... func AddRepPolicy(policy models.RepPolicy) (int64, error) { o := GetOrmer() - sql := `insert into replication_policy (name, project_id, target_id, enabled, description, cron_str, start_time, creation_time, update_time ) values (?, ?, ?, ?, ?, ?, ?, ?, ?)` - p, err := o.Raw(sql).Prepare() - if err != nil { - return 0, err - } - + sql := `insert into replication_policy (name, project_id, target_id, enabled, description, cron_str, creation_time, update_time, filters, replicate_deletion) + values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)` params := []interface{}{} - params = append(params, policy.Name, policy.ProjectID, policy.TargetID, policy.Enabled, policy.Description, policy.CronStr) now := time.Now() - if policy.Enabled == 1 { - params = append(params, now) - } else { - params = append(params, nil) - } - params = append(params, now, now) + params = append(params, policy.Name, policy.ProjectID, policy.TargetID, 1, + policy.Description, policy.Trigger, now, now, policy.Filters, + policy.ReplicateDeletion) - r, err := p.Exec(params...) + result, err := o.Raw(sql, params...).Exec() if err != nil { return 0, err } - id, err := r.LastInsertId() - return id, err + + return result.LastInsertId() } // GetRepPolicy ... func GetRepPolicy(id int64) (*models.RepPolicy, error) { o := GetOrmer() - sql := `select * from replication_policy where id = ?` + sql := `select * from replication_policy where id = ? and deleted = 0` var policy models.RepPolicy @@ -154,8 +146,9 @@ func FilterRepPolicies(name string, projectID int64) ([]*models.RepPolicy, error var args []interface{} sql := `select rp.id, rp.project_id, rp.target_id, - rt.name as target_name, rp.name, rp.enabled, rp.description, - rp.cron_str, rp.start_time, rp.creation_time, rp.update_time, + rt.name as target_name, rp.name, rp.description, + rp.cron_str, rp.filters, rp.replicate_deletion, + rp.creation_time, rp.update_time, count(rj.status) as error_job_count from replication_policy rp left join replication_target rt on rp.target_id=rt.id @@ -181,6 +174,7 @@ func FilterRepPolicies(name string, projectID int64) ([]*models.RepPolicy, error if _, err := o.Raw(sql, args).QueryRows(&policies); err != nil { return nil, err } + return policies, nil } @@ -247,7 +241,8 @@ func GetRepPolicyByProjectAndTarget(projectID, targetID int64) ([]*models.RepPol func UpdateRepPolicy(policy *models.RepPolicy) error { o := GetOrmer() policy.UpdateTime = time.Now() - _, err := o.Update(policy, "TargetID", "Name", "Enabled", "Description", "CronStr", "UpdateTime") + _, err := o.Update(policy, "ProjectID", "TargetID", "Name", "Description", + "Trigger", "Filters", "ReplicateDeletion", "UpdateTime") return err } @@ -263,36 +258,6 @@ func DeleteRepPolicy(id int64) error { return err } -// UpdateRepPolicyEnablement ... -func UpdateRepPolicyEnablement(id int64, enabled int) error { - o := GetOrmer() - p := models.RepPolicy{ - ID: id, - Enabled: enabled, - UpdateTime: time.Now(), - } - - var err error - if enabled == 1 { - p.StartTime = time.Now() - _, err = o.Update(&p, "Enabled", "StartTime") - } else { - _, err = o.Update(&p, "Enabled") - } - - return err -} - -// EnableRepPolicy ... -func EnableRepPolicy(id int64) error { - return UpdateRepPolicyEnablement(id, 1) -} - -// DisableRepPolicy ... -func DisableRepPolicy(id int64) error { - return UpdateRepPolicyEnablement(id, 0) -} - // AddRepJob ... func AddRepJob(job models.RepJob) (int64, error) { o := GetOrmer() diff --git a/src/common/dao/watch_item.go b/src/common/dao/watch_item.go new file mode 100644 index 000000000..dd16ae66b --- /dev/null +++ b/src/common/dao/watch_item.go @@ -0,0 +1,62 @@ +// Copyright (c) 2017 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 dao + +import ( + "time" + + "github.com/vmware/harbor/src/common/models" +) + +// DefaultDatabaseWatchItemDAO is an instance of DatabaseWatchItemDAO +var DefaultDatabaseWatchItemDAO WatchItemDAO = &DatabaseWatchItemDAO{} + +// WatchItemDAO defines operations about WatchItem +type WatchItemDAO interface { + Add(*models.WatchItem) (int64, error) + DeleteByPolicyID(int64) error + Get(namespace, operation string) ([]models.WatchItem, error) +} + +// DatabaseWatchItemDAO implements interface WatchItemDAO for database +type DatabaseWatchItemDAO struct{} + +// Add a WatchItem +func (d *DatabaseWatchItemDAO) Add(item *models.WatchItem) (int64, error) { + now := time.Now() + item.CreationTime = now + item.UpdateTime = now + return GetOrmer().Insert(item) +} + +// DeleteByPolicyID deletes the WatchItem specified by policy ID +func (d *DatabaseWatchItemDAO) DeleteByPolicyID(policyID int64) error { + _, err := GetOrmer().QueryTable(&models.WatchItem{}).Filter("PolicyID", policyID).Delete() + return err +} + +// Get returns WatchItem list according to the namespace and operation +func (d *DatabaseWatchItemDAO) Get(namespace, operation string) ([]models.WatchItem, error) { + qs := GetOrmer().QueryTable(&models.WatchItem{}).Filter("Namespace", namespace) + if operation == "push" { + qs = qs.Filter("OnPush", 1) + } else if operation == "delete" { + qs = qs.Filter("OnDeletion", 1) + } + + items := []models.WatchItem{} + _, err := qs.All(&items) + return items, err +} diff --git a/src/common/dao/watch_item_test.go b/src/common/dao/watch_item_test.go new file mode 100644 index 000000000..b5b9b1b84 --- /dev/null +++ b/src/common/dao/watch_item_test.go @@ -0,0 +1,71 @@ +// Copyright (c) 2017 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 dao + +import ( + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "github.com/vmware/harbor/src/common/models" +) + +func TestMethodsOfWatchItem(t *testing.T) { + targetID, err := AddRepTarget(models.RepTarget{ + Name: "test_target_for_watch_item", + URL: "http://127.0.0.1", + }) + require.Nil(t, err) + defer DeleteRepTarget(targetID) + + policyID, err := AddRepPolicy(models.RepPolicy{ + Name: "test_policy_for_watch_item", + ProjectID: 1, + TargetID: targetID, + }) + require.Nil(t, err) + defer DeleteRepPolicy(policyID) + + item := &models.WatchItem{ + PolicyID: policyID, + Namespace: "library", + OnPush: false, + OnDeletion: true, + } + + // test Add + id, err := DefaultDatabaseWatchItemDAO.Add(item) + require.Nil(t, err) + + // test Get: operation-push + items, err := DefaultDatabaseWatchItemDAO.Get("library", "push") + require.Nil(t, err) + assert.Equal(t, 0, len(items)) + + // test Get: operation-delete + items, err = DefaultDatabaseWatchItemDAO.Get("library", "delete") + require.Nil(t, err) + assert.Equal(t, 1, len(items)) + assert.Equal(t, id, items[0].ID) + assert.Equal(t, "library", items[0].Namespace) + assert.True(t, items[0].OnDeletion) + + // test DeleteByPolicyID + err = DefaultDatabaseWatchItemDAO.DeleteByPolicyID(policyID) + require.Nil(t, err) + items, err = DefaultDatabaseWatchItemDAO.Get("library", "delete") + require.Nil(t, err) + assert.Equal(t, 0, len(items)) +} diff --git a/src/common/http/client.go b/src/common/http/client.go new file mode 100644 index 000000000..a5971cbaf --- /dev/null +++ b/src/common/http/client.go @@ -0,0 +1,162 @@ +// Copyright (c) 2017 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 http + +import ( + "bytes" + "encoding/json" + "io" + "io/ioutil" + "net/http" + + "github.com/vmware/harbor/src/common/http/modifier" +) + +// Client is a util for common HTTP operations, such Get, Head, Post, Put and Delete. +// Use Do instead if those methods can not meet your requirement +type Client struct { + modifiers []modifier.Modifier + client *http.Client +} + +// NewClient creates an instance of Client. +// Use net/http.Client as the default value if c is nil. +// Modifiers modify the request before sending it. +func NewClient(c *http.Client, modifiers ...modifier.Modifier) *Client { + client := &Client{ + client: c, + } + if client.client == nil { + client.client = &http.Client{} + } + if len(modifiers) > 0 { + client.modifiers = modifiers + } + return client +} + +// Do ... +func (c *Client) Do(req *http.Request) (*http.Response, error) { + for _, modifier := range c.modifiers { + if err := modifier.Modify(req); err != nil { + return nil, err + } + } + + return c.client.Do(req) +} + +// Get ... +func (c *Client) Get(url string, v ...interface{}) error { + req, err := http.NewRequest(http.MethodGet, url, nil) + if err != nil { + return err + } + + data, err := c.do(req) + if err != nil { + return err + } + + if len(v) == 0 { + return nil + } + + return json.Unmarshal(data, v[0]) +} + +// Head ... +func (c *Client) Head(url string) error { + req, err := http.NewRequest(http.MethodHead, url, nil) + if err != nil { + return err + } + _, err = c.do(req) + return err +} + +// Post ... +func (c *Client) Post(url string, v ...interface{}) error { + var reader io.Reader + if len(v) > 0 { + data, err := json.Marshal(v[0]) + if err != nil { + return err + } + + reader = bytes.NewReader(data) + } + + req, err := http.NewRequest(http.MethodPost, url, reader) + if err != nil { + return err + } + req.Header.Set("Content-Type", "application/json") + _, err = c.do(req) + return err +} + +// Put ... +func (c *Client) Put(url string, v ...interface{}) error { + var reader io.Reader + if len(v) > 0 { + data := []byte{} + data, err := json.Marshal(v[0]) + if err != nil { + return err + } + reader = bytes.NewReader(data) + } + + req, err := http.NewRequest(http.MethodPut, url, reader) + if err != nil { + return err + } + req.Header.Set("Content-Type", "application/json") + _, err = c.do(req) + return err +} + +// Delete ... +func (c *Client) Delete(url string) error { + req, err := http.NewRequest(http.MethodDelete, url, nil) + if err != nil { + return err + } + _, err = c.do(req) + return err +} + +func (c *Client) do(req *http.Request) ([]byte, error) { + resp, err := c.Do(req) + if err != nil { + return nil, err + } + defer resp.Body.Close() + + data, err := ioutil.ReadAll(resp.Body) + if err != nil { + return nil, err + } + + if resp.StatusCode < 200 || resp.StatusCode > 299 { + return nil, &Error{ + Code: resp.StatusCode, + Message: string(data), + } + } + + return data, nil +} diff --git a/src/common/http/error.go b/src/common/http/error.go new file mode 100644 index 000000000..67e5da8e0 --- /dev/null +++ b/src/common/http/error.go @@ -0,0 +1,30 @@ +// Copyright (c) 2017 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 http + +import ( + "fmt" +) + +// Error wrap HTTP status code and message as an error +type Error struct { + Code int + Message string +} + +// Error ... +func (e *Error) Error() string { + return fmt.Sprintf("http error: code %d, message %s", e.Code, e.Message) +} diff --git a/src/common/http/modifier/auth/auth.go b/src/common/http/modifier/auth/auth.go new file mode 100644 index 000000000..b28253fdf --- /dev/null +++ b/src/common/http/modifier/auth/auth.go @@ -0,0 +1,54 @@ +// Copyright (c) 2017 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 auth + +import ( + "errors" + "net/http" + + "github.com/vmware/harbor/src/common/http/modifier" +) + +const ( + secretCookieName = "secret" +) + +// Authorizer is a kind of Modifier used to authorize the requests +type Authorizer modifier.Modifier + +// SecretAuthorizer authorizes the requests with the specified secret +type SecretAuthorizer struct { + secret string +} + +// NewSecretAuthorizer returns an instance of SecretAuthorizer +func NewSecretAuthorizer(secret string) *SecretAuthorizer { + return &SecretAuthorizer{ + secret: secret, + } +} + +// Modify the request by adding secret authentication information +func (s *SecretAuthorizer) Modify(req *http.Request) error { + if req == nil { + return errors.New("the request is null") + } + + req.AddCookie(&http.Cookie{ + Name: secretCookieName, + Value: s.secret, + }) + return nil +} diff --git a/src/adminserver/client/auth/auth_test.go b/src/common/http/modifier/auth/auth_test.go similarity index 59% rename from src/adminserver/client/auth/auth_test.go rename to src/common/http/modifier/auth/auth_test.go index f5743cccc..c0a68bc65 100644 --- a/src/adminserver/client/auth/auth_test.go +++ b/src/common/http/modifier/auth/auth_test.go @@ -1,4 +1,3 @@ -// Copyright (c) 2017 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. @@ -19,25 +18,22 @@ import ( "testing" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) -func TestAuthorize(t *testing.T) { - cookieName := "secret" +func TestAuthorizeOfSecretAuthorizer(t *testing.T) { secret := "secret" - authorizer := NewSecretAuthorizer(cookieName, secret) + authorizer := NewSecretAuthorizer(secret) + + // nil request + require.NotNil(t, authorizer.Modify(nil)) + + // valid request req, err := http.NewRequest("", "", nil) - if !assert.Nil(t, err, "unexpected error") { - return - } - - err = authorizer.Authorize(req) - if !assert.Nil(t, err, "unexpected error") { - return - } - - cookie, err := req.Cookie(cookieName) - if !assert.Nil(t, err, "unexpected error") { - return - } - assert.Equal(t, secret, cookie.Value, "unexpected cookie") + require.Nil(t, err) + require.Nil(t, authorizer.Modify(req)) + require.Equal(t, 1, len(req.Cookies())) + v, err := req.Cookie(secretCookieName) + require.Nil(t, err) + assert.Equal(t, secret, v.Value) } diff --git a/src/common/utils/registry/modifier.go b/src/common/http/modifier/modifier.go similarity index 97% rename from src/common/utils/registry/modifier.go rename to src/common/http/modifier/modifier.go index a15a9a0fb..ccd34a87c 100644 --- a/src/common/utils/registry/modifier.go +++ b/src/common/http/modifier/modifier.go @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package registry +package modifier import ( "net/http" diff --git a/src/common/models/base.go b/src/common/models/base.go index 1606bbe1e..79ce7483e 100644 --- a/src/common/models/base.go +++ b/src/common/models/base.go @@ -30,6 +30,7 @@ func init() { new(RepoRecord), new(ImgScanOverview), new(ClairVulnTimestamp), + new(WatchItem), new(ProjectMetadata), - new(ConfigEntry)) + new(ConfigEntry)) } diff --git a/src/common/models/replication_job.go b/src/common/models/replication_job.go index 262ff2473..8c14234d9 100644 --- a/src/common/models/replication_job.go +++ b/src/common/models/replication_job.go @@ -38,48 +38,17 @@ const ( // RepPolicy is the model for a replication policy, which associate to a project and a target (destination) type RepPolicy struct { - ID int64 `orm:"pk;auto;column(id)" json:"id"` - ProjectID int64 `orm:"column(project_id)" json:"project_id"` - ProjectName string `json:"project_name,omitempty"` - TargetID int64 `orm:"column(target_id)" json:"target_id"` - TargetName string `json:"target_name,omitempty"` - Name string `orm:"column(name)" json:"name"` - // Target RepTarget `orm:"-" json:"target"` - Enabled int `orm:"column(enabled)" json:"enabled"` - Description string `orm:"column(description)" json:"description"` - CronStr string `orm:"column(cron_str)" json:"cron_str"` - StartTime time.Time `orm:"column(start_time)" json:"start_time"` - CreationTime time.Time `orm:"column(creation_time);auto_now_add" json:"creation_time"` - UpdateTime time.Time `orm:"column(update_time);auto_now" json:"update_time"` - ErrorJobCount int `json:"error_job_count"` - Deleted int `orm:"column(deleted)" json:"deleted"` -} - -// Valid ... -func (r *RepPolicy) Valid(v *validation.Validation) { - if len(r.Name) == 0 { - v.SetError("name", "can not be empty") - } - - if len(r.Name) > 256 { - v.SetError("name", "max length is 256") - } - - if r.ProjectID <= 0 { - v.SetError("project_id", "invalid") - } - - if r.TargetID <= 0 { - v.SetError("target_id", "invalid") - } - - if r.Enabled != 0 && r.Enabled != 1 { - v.SetError("enabled", "must be 0 or 1") - } - - if len(r.CronStr) > 256 { - v.SetError("cron_str", "max length is 256") - } + ID int64 `orm:"pk;auto;column(id)"` + ProjectID int64 `orm:"column(project_id)" ` + TargetID int64 `orm:"column(target_id)"` + Name string `orm:"column(name)"` + Description string `orm:"column(description)"` + Trigger string `orm:"column(cron_str)"` + Filters string `orm:"column(filters)"` + ReplicateDeletion bool `orm:"column(replicate_deletion)"` + CreationTime time.Time `orm:"column(creation_time);auto_now_add"` + UpdateTime time.Time `orm:"column(update_time);auto_now"` + Deleted int `orm:"column(deleted)"` } // RepJob is the model for a replication job, which is the execution unit on job service, currently it is used to transfer/remove diff --git a/src/common/models/watch_item.go b/src/common/models/watch_item.go new file mode 100644 index 000000000..75f22dcbe --- /dev/null +++ b/src/common/models/watch_item.go @@ -0,0 +1,35 @@ +// Copyright (c) 2017 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 models + +import ( + "time" +) + +// WatchItem ... +type WatchItem struct { + ID int64 `orm:"pk;auto;column(id)" json:"id"` + PolicyID int64 `orm:"column(policy_id)" json:"policy_id"` + Namespace string `orm:"column(namespace)" json:"namespace"` + OnDeletion bool `orm:"column(on_deletion)" json:"on_deletion"` + OnPush bool `orm:"column(on_push)" json:"on_push"` + CreationTime time.Time `orm:"column(creation_time)" json:"creation_time"` + UpdateTime time.Time `orm:"column(update_time)" json:"update_time"` +} + +//TableName ... +func (w *WatchItem) TableName() string { + return "replication_immediate_trigger" +} diff --git a/src/common/notifier/notifier_test.go b/src/common/notifier/notifier_test.go index 9f98679ce..8bb93c59d 100644 --- a/src/common/notifier/notifier_test.go +++ b/src/common/notifier/notifier_test.go @@ -39,6 +39,7 @@ func (fsh *fakeStatelessHandler) Handle(v interface{}) error { } func TestSubscribeAndUnSubscribe(t *testing.T) { + count := len(notificationWatcher.handlers) err := Subscribe("topic1", &fakeStatefulHandler{0}) if err != nil { t.Fatal(err) @@ -59,7 +60,7 @@ func TestSubscribeAndUnSubscribe(t *testing.T) { t.Fatal(err) } - if len(notificationWatcher.handlers) != 2 { + if len(notificationWatcher.handlers) != (count + 2) { t.Fail() } @@ -94,7 +95,7 @@ func TestSubscribeAndUnSubscribe(t *testing.T) { t.Fatal(err) } - if len(notificationWatcher.handlers) != 1 { + if len(notificationWatcher.handlers) != (count + 1) { t.Fail() } @@ -103,12 +104,13 @@ func TestSubscribeAndUnSubscribe(t *testing.T) { t.Fatal(err) } - if len(notificationWatcher.handlers) != 0 { + if len(notificationWatcher.handlers) != count { t.Fail() } } func TestPublish(t *testing.T) { + count := len(notificationWatcher.handlers) err := Subscribe("topic1", &fakeStatefulHandler{0}) if err != nil { t.Fatal(err) @@ -119,7 +121,7 @@ func TestPublish(t *testing.T) { t.Fatal(err) } - if len(notificationWatcher.handlers) != 2 { + if len(notificationWatcher.handlers) != (count + 2) { t.Fail() } @@ -149,12 +151,13 @@ func TestPublish(t *testing.T) { } func TestConcurrentPublish(t *testing.T) { + count := len(notificationWatcher.handlers) err := Subscribe("topic1", &fakeStatefulHandler{0}) if err != nil { t.Fatal(err) } - if len(notificationWatcher.handlers) != 1 { + if len(notificationWatcher.handlers) != (count + 1) { t.Fail() } @@ -186,11 +189,12 @@ func TestConcurrentPublishWithScanPolicyHandler(t *testing.T) { t.Fatal("Policy scheduler is not started") } + count := len(notificationWatcher.handlers) if err := Subscribe("testing_topic", &ScanPolicyNotificationHandler{}); err != nil { t.Fatal(err.Error()) } - if len(notificationWatcher.handlers) != 1 { - t.Fatal("Handler is not registered") + if len(notificationWatcher.handlers) != (count + 1) { + t.Fatalf("Handler is not registered") } utcTime := time.Now().UTC().Unix() @@ -209,7 +213,7 @@ func TestConcurrentPublishWithScanPolicyHandler(t *testing.T) { t.Fatal(err.Error()) } - if len(notificationWatcher.handlers) != 0 { + if len(notificationWatcher.handlers) != count { t.Fatal("Handler is not unregistered") } diff --git a/src/common/notifier/scan_policy_notitification_handler.go b/src/common/notifier/scan_policy_notitification_handler.go index 076019f06..e3f7d3530 100644 --- a/src/common/notifier/scan_policy_notitification_handler.go +++ b/src/common/notifier/scan_policy_notitification_handler.go @@ -63,7 +63,7 @@ func (s *ScanPolicyNotificationHandler) Handle(value interface{}) error { //To check and compare if the related parameter is changed. if pl := scheduler.DefaultScheduler.GetPolicy(alternatePolicy); pl != nil { - policyCandidate := policy.NewAlternatePolicy(&policy.AlternatePolicyConfiguration{ + policyCandidate := policy.NewAlternatePolicy(alternatePolicy, &policy.AlternatePolicyConfiguration{ Duration: 24 * time.Hour, OffsetTime: notification.DailyTime, }) @@ -95,7 +95,7 @@ func (s *ScanPolicyNotificationHandler) Handle(value interface{}) error { //Schedule policy. func schedulePolicy(notification ScanPolicyNotification) error { - schedulePolicy := policy.NewAlternatePolicy(&policy.AlternatePolicyConfiguration{ + schedulePolicy := policy.NewAlternatePolicy(alternatePolicy, &policy.AlternatePolicyConfiguration{ Duration: 24 * time.Hour, OffsetTime: notification.DailyTime, }) diff --git a/src/common/scheduler/policy/alternate_policy.go b/src/common/scheduler/policy/alternate_policy.go index 52ca37589..fe72959c8 100644 --- a/src/common/scheduler/policy/alternate_policy.go +++ b/src/common/scheduler/policy/alternate_policy.go @@ -10,11 +10,22 @@ import ( "github.com/vmware/harbor/src/common/utils/log" ) +const ( + oneDay = 24 * 3600 +) + //AlternatePolicyConfiguration store the related configurations for alternate policy. type AlternatePolicyConfiguration struct { //Duration is the interval of executing attached tasks. + //E.g: 24*3600 for daily + // 7*24*3600 for weekly Duration time.Duration + //An integer to indicate the the weekday of the week. Please be noted that Sunday is 7. + //Use default value 0 to indicate weekday is not set. + //To support by weekly function. + Weekday int8 + //OffsetTime is the execution time point of each turn //It's a number to indicate the seconds offset to the 00:00 of UTC time. OffsetTime int64 @@ -42,16 +53,21 @@ type AlternatePolicy struct { //Channel used to receive terminate signal. terminator chan bool + + //Unique name of this policy to support multiple instances + name string } //NewAlternatePolicy is constructor of creating AlternatePolicy. -func NewAlternatePolicy(config *AlternatePolicyConfiguration) *AlternatePolicy { +//Accept name and configuration as parameters. +func NewAlternatePolicy(name string, config *AlternatePolicyConfiguration) *AlternatePolicy { return &AlternatePolicy{ RWMutex: new(sync.RWMutex), tasks: task.NewDefaultStore(), config: config, isEnabled: false, terminator: make(chan bool), + name: name, } } @@ -62,7 +78,7 @@ func (alp *AlternatePolicy) GetConfig() *AlternatePolicyConfiguration { //Name is an implementation of same method in policy interface. func (alp *AlternatePolicy) Name() string { - return "Alternate Policy" + return alp.name } //Tasks is an implementation of same method in policy interface. @@ -110,6 +126,11 @@ func (alp *AlternatePolicy) Evaluate() (<-chan bool, error) { defer alp.Unlock() alp.Lock() + //Check if configuration is valid + if !alp.isValidConfig() { + return nil, errors.New("Policy configuration is not valid") + } + //Check if policy instance is still running if alp.isEnabled { return nil, fmt.Errorf("Instance of policy %s is still running", alp.Name()) @@ -124,19 +145,41 @@ func (alp *AlternatePolicy) Evaluate() (<-chan bool, error) { alp.evaluation = make(chan bool) go func() { + var ( + waitingTime int64 + ) timeNow := time.Now().UTC() //Reach the execution time point? + //Weekday is set + if alp.config.Weekday > 0 { + targetWeekday := (alp.config.Weekday + 7) % 7 + currentWeekday := timeNow.Weekday() + weekdayDiff := (int)(targetWeekday - (int8)(currentWeekday)) + if weekdayDiff < 0 { + weekdayDiff += 7 + } + waitingTime = (int64)(weekdayDiff * oneDay) + } + + //Time utcTime := (int64)(timeNow.Hour()*3600 + timeNow.Minute()*60) diff := alp.config.OffsetTime - utcTime - if diff < 0 { - diff += 24 * 3600 + if waitingTime > 0 { + waitingTime += diff + } else { + waitingTime = diff + if waitingTime < 0 { + waitingTime += oneDay + } } - if diff > 0 { + + //Let's wait for a while + if waitingTime > 0 { //Wait for a while. log.Infof("Waiting for %d seconds after comparing offset %d and utc time %d\n", diff, alp.config.OffsetTime, utcTime) select { - case <-time.After(time.Duration(diff) * time.Second): + case <-time.After(time.Duration(waitingTime) * time.Second): case <-alp.terminator: return } @@ -188,7 +231,10 @@ func (alp *AlternatePolicy) Equal(p Policy) bool { return false } - return cfg == nil || (cfg.Duration == cfg2.Duration && cfg.OffsetTime == cfg2.OffsetTime) + return cfg == nil || + (cfg.Duration == cfg2.Duration && + cfg.OffsetTime == cfg2.OffsetTime && + cfg.Weekday == cfg2.Weekday) } //IsEnabled is an implementation of same method in policy interface. @@ -198,3 +244,8 @@ func (alp *AlternatePolicy) IsEnabled() bool { return alp.isEnabled } + +//Check if the config is valid. At least it should have the configurations for supporting daily policy. +func (alp *AlternatePolicy) isValidConfig() bool { + return alp.config != nil && alp.config.Duration > 0 && alp.config.OffsetTime >= 0 +} diff --git a/src/common/scheduler/policy/alternate_policy_test.go b/src/common/scheduler/policy/alternate_policy_test.go index 777fe59de..5a3eda4e3 100644 --- a/src/common/scheduler/policy/alternate_policy_test.go +++ b/src/common/scheduler/policy/alternate_policy_test.go @@ -6,6 +6,10 @@ import ( "time" ) +const ( + testPolicyName = "TestingPolicy" +) + type fakeTask struct { number int32 } @@ -24,18 +28,18 @@ func (ft *fakeTask) Number() int32 { } func TestBasic(t *testing.T) { - tp := NewAlternatePolicy(&AlternatePolicyConfiguration{}) + tp := NewAlternatePolicy(testPolicyName, &AlternatePolicyConfiguration{}) err := tp.AttachTasks(&fakeTask{number: 100}) if err != nil { t.Fail() } if tp.GetConfig() == nil { - t.Fail() + t.Fatal("nil config") } - if tp.Name() != "Alternate Policy" { - t.Fail() + if tp.Name() != testPolicyName { + t.Fatalf("Wrong name %s", tp.Name()) } tks := tp.Tasks() @@ -48,7 +52,7 @@ func TestBasic(t *testing.T) { func TestEvaluatePolicy(t *testing.T) { now := time.Now().UTC() utcOffset := (int64)(now.Hour()*3600 + now.Minute()*60) - tp := NewAlternatePolicy(&AlternatePolicyConfiguration{ + tp := NewAlternatePolicy(testPolicyName, &AlternatePolicyConfiguration{ Duration: 1 * time.Second, OffsetTime: utcOffset + 1, }) @@ -78,7 +82,7 @@ func TestEvaluatePolicy(t *testing.T) { func TestDisablePolicy(t *testing.T) { now := time.Now().UTC() utcOffset := (int64)(now.Hour()*3600 + now.Minute()*60) - tp := NewAlternatePolicy(&AlternatePolicyConfiguration{ + tp := NewAlternatePolicy(testPolicyName, &AlternatePolicyConfiguration{ Duration: 1 * time.Second, OffsetTime: utcOffset + 1, }) @@ -118,3 +122,28 @@ func TestDisablePolicy(t *testing.T) { t.Fatalf("Policy is still running after calling Disable() %d=%d", atomic.LoadInt32(&copiedCounter), atomic.LoadInt32(&counter)) } } + +func TestPolicyEqual(t *testing.T) { + tp1 := NewAlternatePolicy(testPolicyName, &AlternatePolicyConfiguration{ + Duration: 1 * time.Second, + OffsetTime: 8000, + }) + + tp2 := NewAlternatePolicy(testPolicyName+"2", &AlternatePolicyConfiguration{ + Duration: 100 * time.Second, + OffsetTime: 8000, + }) + + if tp1.Equal(tp2) { + t.Fatal("tp1 should not equal tp2") + } + + tp3 := NewAlternatePolicy(testPolicyName, &AlternatePolicyConfiguration{ + Duration: 1 * time.Second, + OffsetTime: 8000, + }) + + if !tp1.Equal(tp3) { + t.Fatal("tp1 should equal tp3") + } +} diff --git a/src/common/scheduler/policy/policy.go b/src/common/scheduler/policy/policy.go index 732d56cca..4fc72c0d6 100644 --- a/src/common/scheduler/policy/policy.go +++ b/src/common/scheduler/policy/policy.go @@ -15,6 +15,7 @@ import ( // type Policy interface { //Name will return the name of the policy. + //If the policy supports multiple instances, please make sure the name is unique as an UUID. Name() string //Tasks will return the attached tasks with this policy. diff --git a/src/common/scheduler/policy/uuid.go b/src/common/scheduler/policy/uuid.go new file mode 100644 index 000000000..8bd1bd72c --- /dev/null +++ b/src/common/scheduler/policy/uuid.go @@ -0,0 +1,22 @@ +package policy + +import ( + "crypto/rand" + "fmt" + "io" +) + +//NewUUID will generate a new UUID. +//Code copied from https://play.golang.org/p/4FkNSiUDMg +func newUUID() (string, error) { + uuid := make([]byte, 16) + n, err := io.ReadFull(rand.Reader, uuid) + if n != len(uuid) || err != nil { + return "", err + } + + uuid[8] = uuid[8]&^0xc0 | 0x80 + uuid[6] = uuid[6]&^0xf0 | 0x40 + + return fmt.Sprintf("%x-%x-%x-%x-%x", uuid[0:4], uuid[4:6], uuid[6:8], uuid[8:10], uuid[10:]), nil +} diff --git a/src/common/scheduler/task/replication/replication_task.go b/src/common/scheduler/task/replication/replication_task.go new file mode 100644 index 000000000..1d4917420 --- /dev/null +++ b/src/common/scheduler/task/replication/replication_task.go @@ -0,0 +1,31 @@ +package replication + +import ( + "github.com/vmware/harbor/src/common/notifier" + "github.com/vmware/harbor/src/replication/event/notification" + "github.com/vmware/harbor/src/replication/event/topic" +) + +//Task is the task for triggering one replication +type Task struct { + PolicyID int64 +} + +//NewTask is constructor of creating ReplicationTask +func NewTask(policyID int64) *Task { + return &Task{ + PolicyID: policyID, + } +} + +//Name returns the name of this task +func (t *Task) Name() string { + return "replication" +} + +//Run the actions here +func (t *Task) Run() error { + return notifier.Publish(topic.StartReplicationTopic, notification.StartReplicationNotification{ + PolicyID: t.PolicyID, + }) +} diff --git a/src/common/scheduler/task/replication/replication_task_test.go b/src/common/scheduler/task/replication/replication_task_test.go new file mode 100644 index 000000000..a914b46f9 --- /dev/null +++ b/src/common/scheduler/task/replication/replication_task_test.go @@ -0,0 +1,14 @@ +package replication + +import "testing" + +func TestTask(t *testing.T) { + tk := NewTask(1) + if tk == nil { + t.Fail() + } + + if tk.Name() != "replication" { + t.Fail() + } +} diff --git a/src/common/scheduler/task/scan_all_task_test.go b/src/common/scheduler/task/scan_all_task_test.go index 18ac9202b..b7482fbfc 100644 --- a/src/common/scheduler/task/scan_all_task_test.go +++ b/src/common/scheduler/task/scan_all_task_test.go @@ -4,7 +4,7 @@ import ( "testing" ) -func TestTask(t *testing.T) { +func TestScanAllTask(t *testing.T) { tk := NewScanAllTask() if tk == nil { t.Fail() diff --git a/src/common/utils/registry/auth/tokenauthorizer.go b/src/common/utils/registry/auth/tokenauthorizer.go index bbdb69d9f..4e34a9f4d 100644 --- a/src/common/utils/registry/auth/tokenauthorizer.go +++ b/src/common/utils/registry/auth/tokenauthorizer.go @@ -23,9 +23,9 @@ import ( "time" "github.com/docker/distribution/registry/auth/token" + "github.com/vmware/harbor/src/common/http/modifier" "github.com/vmware/harbor/src/common/models" "github.com/vmware/harbor/src/common/utils/log" - "github.com/vmware/harbor/src/common/utils/registry" token_util "github.com/vmware/harbor/src/ui/service/token" ) @@ -254,7 +254,7 @@ func ping(client *http.Client, endpoint string) (string, string, error) { // from token server and add it to the origin request // If customizedTokenService is set, the token request will be sent to it instead of the server get from authorizer func NewStandardTokenAuthorizer(client *http.Client, credential Credential, - customizedTokenService ...string) registry.Modifier { + customizedTokenService ...string) modifier.Modifier { generator := &standardTokenGenerator{ credential: credential, client: client, @@ -309,7 +309,7 @@ func (s *standardTokenGenerator) generate(scopes []*token.ResourceActions, endpo // NewRawTokenAuthorizer returns a token authorizer which calls method to create // token directly -func NewRawTokenAuthorizer(username, service string) registry.Modifier { +func NewRawTokenAuthorizer(username, service string) modifier.Modifier { generator := &rawTokenGenerator{ service: service, username: username, diff --git a/src/common/utils/registry/transport.go b/src/common/utils/registry/transport.go index 3ae8a8d50..d844f9308 100644 --- a/src/common/utils/registry/transport.go +++ b/src/common/utils/registry/transport.go @@ -17,17 +17,18 @@ package registry import ( "net/http" + "github.com/vmware/harbor/src/common/http/modifier" "github.com/vmware/harbor/src/common/utils/log" ) // Transport holds information about base transport and modifiers type Transport struct { transport http.RoundTripper - modifiers []Modifier + modifiers []modifier.Modifier } // NewTransport ... -func NewTransport(transport http.RoundTripper, modifiers ...Modifier) *Transport { +func NewTransport(transport http.RoundTripper, modifiers ...modifier.Modifier) *Transport { return &Transport{ transport: transport, modifiers: modifiers, diff --git a/src/common/utils/test/policy_manager.go b/src/common/utils/test/policy_manager.go new file mode 100644 index 000000000..492c88e00 --- /dev/null +++ b/src/common/utils/test/policy_manager.go @@ -0,0 +1,45 @@ +// Copyright (c) 2017 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 test + +import ( + "github.com/vmware/harbor/src/replication" + "github.com/vmware/harbor/src/replication/models" +) + +type FakePolicyManager struct { +} + +func (f *FakePolicyManager) GetPolicies(query models.QueryParameter) ([]models.ReplicationPolicy, error) { + return []models.ReplicationPolicy{}, nil +} + +func (f *FakePolicyManager) GetPolicy(id int64) (models.ReplicationPolicy, error) { + return models.ReplicationPolicy{ + ID: 1, + Trigger: &models.Trigger{ + Kind: replication.TriggerKindManual, + }, + }, nil +} +func (f *FakePolicyManager) CreatePolicy(policy models.ReplicationPolicy) (int64, error) { + return 1, nil +} +func (f *FakePolicyManager) UpdatePolicy(models.ReplicationPolicy) error { + return nil +} +func (f *FakePolicyManager) RemovePolicy(int64) error { + return nil +} diff --git a/src/common/utils/test/replication_controllter.go b/src/common/utils/test/replication_controllter.go new file mode 100644 index 000000000..adff7ae7a --- /dev/null +++ b/src/common/utils/test/replication_controllter.go @@ -0,0 +1,26 @@ +// Copyright (c) 2017 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 test + +type FakeReplicatoinController struct { + FakePolicyManager +} + +func (f *FakeReplicatoinController) Init() error { + return nil +} +func (f *FakeReplicatoinController) Replicate(policyID int64, metadata ...map[string]interface{}) error { + return nil +} diff --git a/src/common/utils/test/watch_item.go b/src/common/utils/test/watch_item.go new file mode 100644 index 000000000..c801a9761 --- /dev/null +++ b/src/common/utils/test/watch_item.go @@ -0,0 +1,65 @@ +// Copyright (c) 2017 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 test + +import ( + "github.com/vmware/harbor/src/common/models" +) + +// FakeWatchItemDAO is the fake implement for the dao.WatchItemDAO +type FakeWatchItemDAO struct { + items []models.WatchItem +} + +// Add ... +func (f *FakeWatchItemDAO) Add(item *models.WatchItem) (int64, error) { + f.items = append(f.items, *item) + return int64(len(f.items) + 1), nil +} + +// DeleteByPolicyID : delete the WatchItem specified by policy ID +func (f *FakeWatchItemDAO) DeleteByPolicyID(policyID int64) error { + for i, item := range f.items { + if item.PolicyID == policyID { + f.items = append(f.items[:i], f.items[i+1:]...) + break + } + } + return nil +} + +// Get returns WatchItem list according to the namespace and operation +func (f *FakeWatchItemDAO) Get(namespace, operation string) ([]models.WatchItem, error) { + items := []models.WatchItem{} + for _, item := range f.items { + if item.Namespace != namespace { + continue + } + + if operation == "push" { + if item.OnPush { + items = append(items, item) + } + } + + if operation == "delete" { + if item.OnDeletion { + items = append(items, item) + } + } + } + + return items, nil +} diff --git a/src/jobservice/client/client.go b/src/jobservice/client/client.go new file mode 100644 index 000000000..215d018f5 --- /dev/null +++ b/src/jobservice/client/client.go @@ -0,0 +1,76 @@ +// Copyright (c) 2017 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 client + +import ( + "github.com/vmware/harbor/src/common/http" + "github.com/vmware/harbor/src/common/http/modifier/auth" +) + +// Replication holds information for submiting a replication job +type Replication struct { + PolicyID int64 `json:"policy_id"` + Repository string `json:"repository"` + Operation string `json:"operation"` + Tags []string `json:"tags"` +} + +// Client defines the methods that a jobservice client should implement +type Client interface { + SubmitReplicationJob(*Replication) error + StopReplicationJobs(policyID int64) error +} + +// DefaultClient provides a default implement for the interface Client +type DefaultClient struct { + endpoint string + client *http.Client +} + +// Config contains configuration items needed for DefaultClient +type Config struct { + Secret string +} + +// NewDefaultClient returns an instance of DefaultClient +func NewDefaultClient(endpoint string, cfg *Config) *DefaultClient { + c := &DefaultClient{ + endpoint: endpoint, + } + + if cfg != nil { + c.client = http.NewClient(nil, auth.NewSecretAuthorizer(cfg.Secret)) + } + + return c +} + +// SubmitReplicationJob submits a replication job to the jobservice +func (d *DefaultClient) SubmitReplicationJob(replication *Replication) error { + url := d.endpoint + "/api/jobs/replication" + return d.client.Post(url, replication) +} + +// StopReplicationJobs stop replication jobs of the policy specified by the policy ID +func (d *DefaultClient) StopReplicationJobs(policyID int64) error { + url := d.endpoint + "/api/jobs/replication/actions" + return d.client.Post(url, &struct { + PolicyID int64 `json:"policy_id"` + Action string `json:"action"` + }{ + PolicyID: policyID, + Action: "stop", + }) +} diff --git a/src/jobservice/client/client_test.go b/src/jobservice/client/client_test.go new file mode 100644 index 000000000..a48adc8e0 --- /dev/null +++ b/src/jobservice/client/client_test.go @@ -0,0 +1,86 @@ +// Copyright (c) 2017 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 client + +import ( + "encoding/json" + "net/http" + "os" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/vmware/harbor/src/common/utils/test" +) + +var url string + +func TestMain(m *testing.M) { + requestMapping := []*test.RequestHandlerMapping{ + &test.RequestHandlerMapping{ + Method: http.MethodPost, + Pattern: "/api/jobs/replication/actions", + Handler: func(w http.ResponseWriter, r *http.Request) { + action := &struct { + PolicyID int64 `json:"policy_id"` + Action string `json:"action"` + }{} + if err := json.NewDecoder(r.Body).Decode(action); err != nil { + w.WriteHeader(http.StatusInternalServerError) + return + } + + if action.PolicyID != 1 { + w.WriteHeader(http.StatusNotFound) + return + } + + }, + }, + &test.RequestHandlerMapping{ + Method: http.MethodPost, + Pattern: "/api/jobs/replication", + Handler: func(w http.ResponseWriter, r *http.Request) { + replication := &Replication{} + if err := json.NewDecoder(r.Body).Decode(replication); err != nil { + w.WriteHeader(http.StatusInternalServerError) + } + }, + }, + } + server := test.NewServer(requestMapping...) + defer server.Close() + + url = server.URL + + os.Exit(m.Run()) +} + +func TestSubmitReplicationJob(t *testing.T) { + client := NewDefaultClient(url, &Config{}) + err := client.SubmitReplicationJob(&Replication{}) + assert.Nil(t, err) +} + +func TestStopReplicationJobs(t *testing.T) { + client := NewDefaultClient(url, &Config{}) + + // 404 + err := client.StopReplicationJobs(2) + assert.NotNil(t, err) + + // 200 + err = client.StopReplicationJobs(1) + assert.Nil(t, err) +} diff --git a/src/jobservice/config/config.go b/src/jobservice/config/config.go index 674cd058d..4992c2363 100644 --- a/src/jobservice/config/config.go +++ b/src/jobservice/config/config.go @@ -20,7 +20,6 @@ import ( "strings" "github.com/vmware/harbor/src/adminserver/client" - "github.com/vmware/harbor/src/adminserver/client/auth" "github.com/vmware/harbor/src/common" comcfg "github.com/vmware/harbor/src/common/config" "github.com/vmware/harbor/src/common/models" @@ -50,8 +49,10 @@ func Init() error { adminServerURL = "http://adminserver" } log.Infof("initializing client for adminserver %s ...", adminServerURL) - authorizer := auth.NewSecretAuthorizer(secretCookieName, UISecret()) - AdminserverClient = client.NewClient(adminServerURL, authorizer) + cfg := &client.Config{ + Secret: UISecret(), + } + AdminserverClient = client.NewClient(adminServerURL, cfg) if err := AdminserverClient.Ping(); err != nil { return fmt.Errorf("failed to ping adminserver: %v", err) } diff --git a/src/jobservice/job/job_test.go b/src/jobservice/job/job_test.go index ab52c9487..eb29c93cc 100644 --- a/src/jobservice/job/job_test.go +++ b/src/jobservice/job/job_test.go @@ -105,7 +105,6 @@ func TestRepJob(t *testing.T) { assert.Nil(err) j, err := dao.GetRepJob(repJobID) assert.Equal(models.JobRetrying, j.Status) - assert.Equal(1, rj.parm.Enabled) assert.False(rj.parm.Insecure) rj2 := NewRepJob(99999) err = rj2.Init() @@ -163,7 +162,6 @@ func prepareRepJobData() error { } policy := models.RepPolicy{ ProjectID: 1, - Enabled: 1, TargetID: targetID, Description: "whatever", Name: "mypolicy", diff --git a/src/jobservice/job/jobs.go b/src/jobservice/job/jobs.go index 96a3bc821..1752f9164 100644 --- a/src/jobservice/job/jobs.go +++ b/src/jobservice/job/jobs.go @@ -62,7 +62,6 @@ type RepJobParm struct { TargetPassword string Repository string Tags []string - Enabled int Operation string Insecure bool } @@ -124,13 +123,8 @@ func (rj *RepJob) Init() error { LocalRegURL: regURL, Repository: job.Repository, Tags: job.TagList, - Enabled: policy.Enabled, Operation: job.Operation, } - if policy.Enabled == 0 { - //worker will cancel this job - return nil - } target, err := dao.GetRepTarget(policy.TargetID) if err != nil { return fmt.Errorf("Failed to get target, error: %v", err) diff --git a/src/jobservice/job/statemachine.go b/src/jobservice/job/statemachine.go index 3db88a78f..8b41ebdf7 100644 --- a/src/jobservice/job/statemachine.go +++ b/src/jobservice/job/statemachine.go @@ -208,16 +208,6 @@ func (sm *SM) Reset(j Job) error { } func (sm *SM) kickOff() error { - if repJob, ok := sm.CurrentJob.(*RepJob); ok { - if repJob.parm.Enabled == 0 { - log.Debugf("The policy of job:%v is disabled, will cancel the job", repJob) - _, err := sm.EnterState(models.JobCanceled) - if err != nil { - log.Warningf("For job: %v, failed to update state to 'canceled', error: %v", repJob, err) - } - return err - } - } log.Debugf("In kickOff: will start job: %v", sm.CurrentJob) sm.Start(models.JobRunning) return nil diff --git a/src/replication/consts.go b/src/replication/consts.go new file mode 100644 index 000000000..b9a8a36a7 --- /dev/null +++ b/src/replication/consts.go @@ -0,0 +1,25 @@ +package replication + +const ( + //FilterItemKindProject : Kind of filter item is 'project' + FilterItemKindProject = "project" + //FilterItemKindRepository : Kind of filter item is 'repository' + FilterItemKindRepository = "repository" + //FilterItemKindTag : Kind of filter item is 'tag' + FilterItemKindTag = "tag" + + //AdaptorKindHarbor : Kind of adaptor of Harbor + AdaptorKindHarbor = "Harbor" + + //TriggerKindImmediate : Kind of trigger is 'Immediate' + TriggerKindImmediate = "immediate" + //TriggerKindSchedule : Kind of trigger is 'Schedule' + TriggerKindSchedule = "schedule" + //TriggerKindManual : Kind of trigger is 'Manual' + TriggerKindManual = "manual" + + //TriggerScheduleDaily : type of scheduling is 'daily' + TriggerScheduleDaily = "daily" + //TriggerScheduleWeekly : type of scheduling is 'weekly' + TriggerScheduleWeekly = "weekly" +) diff --git a/src/replication/core/controller.go b/src/replication/core/controller.go new file mode 100644 index 000000000..9065e6284 --- /dev/null +++ b/src/replication/core/controller.go @@ -0,0 +1,319 @@ +// Copyright (c) 2017 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 core + +import ( + "fmt" + "strings" + + common_models "github.com/vmware/harbor/src/common/models" + "github.com/vmware/harbor/src/common/utils/log" + "github.com/vmware/harbor/src/jobservice/client" + "github.com/vmware/harbor/src/replication" + "github.com/vmware/harbor/src/replication/models" + "github.com/vmware/harbor/src/replication/policy" + "github.com/vmware/harbor/src/replication/replicator" + "github.com/vmware/harbor/src/replication/source" + "github.com/vmware/harbor/src/replication/target" + "github.com/vmware/harbor/src/replication/trigger" + "github.com/vmware/harbor/src/ui/config" +) + +// Controller defines the methods that a replicatoin controllter should implement +type Controller interface { + policy.Manager + Init() error + Replicate(policyID int64, metadata ...map[string]interface{}) error +} + +//DefaultController is core module to cordinate and control the overall workflow of the +//replication modules. +type DefaultController struct { + //Indicate whether the controller has been initialized or not + initialized bool + + //Manage the policies + policyManager policy.Manager + + //Manage the targets + targetManager target.Manager + + //Handle the things related with source + sourcer *source.Sourcer + + //Manage the triggers of policies + triggerManager *trigger.Manager + + //Handle the replication work + replicator replicator.Replicator +} + +//Keep controller as singleton instance +var ( + GlobalController Controller +) + +//ControllerConfig includes related configurations required by the controller +type ControllerConfig struct { + //The capacity of the cache storing enabled triggers + CacheCapacity int +} + +//NewDefaultController is the constructor of DefaultController. +func NewDefaultController(cfg ControllerConfig) *DefaultController { + //Controller refer the default instances + ctl := &DefaultController{ + policyManager: policy.NewDefaultManager(), + targetManager: target.NewDefaultManager(), + sourcer: source.NewSourcer(), + triggerManager: trigger.NewManager(cfg.CacheCapacity), + } + + ctl.replicator = replicator.NewDefaultReplicator(config.GlobalJobserviceClient) + + return ctl +} + +// Init creates the GlobalController and inits it +func Init() error { + GlobalController = NewDefaultController(ControllerConfig{}) //Use default data + return GlobalController.Init() +} + +//Init will initialize the controller and the sub components +func (ctl *DefaultController) Init() error { + if ctl.initialized { + return nil + } + + //Build query parameters + query := models.QueryParameter{ + TriggerType: replication.TriggerKindSchedule, + } + + policies, err := ctl.policyManager.GetPolicies(query) + if err != nil { + return err + } + if policies != nil && len(policies) > 0 { + for _, policy := range policies { + if err := ctl.triggerManager.SetupTrigger(&policy); err != nil { + log.Errorf("failed to setup trigger for policy %v: %v", policy, err) + } + } + } + + //Initialize sourcer + ctl.sourcer.Init() + + ctl.initialized = true + + return nil +} + +//CreatePolicy is used to create a new policy and enable it if necessary +func (ctl *DefaultController) CreatePolicy(newPolicy models.ReplicationPolicy) (int64, error) { + id, err := ctl.policyManager.CreatePolicy(newPolicy) + if err != nil { + return 0, err + } + + newPolicy.ID = id + if err = ctl.triggerManager.SetupTrigger(&newPolicy); err != nil { + return 0, err + } + + return id, nil +} + +//UpdatePolicy will update the policy with new content. +//Parameter updatedPolicy must have the ID of the updated policy. +func (ctl *DefaultController) UpdatePolicy(updatedPolicy models.ReplicationPolicy) error { + id := updatedPolicy.ID + originPolicy, err := ctl.policyManager.GetPolicy(id) + if err != nil { + return err + } + + if originPolicy.ID == 0 { + return fmt.Errorf("policy %d not found", id) + } + + reset := false + if updatedPolicy.Trigger.Kind != originPolicy.Trigger.Kind { + reset = true + } else { + switch updatedPolicy.Trigger.Kind { + case replication.TriggerKindSchedule: + if !originPolicy.Trigger.ScheduleParam.Equal(updatedPolicy.Trigger.ScheduleParam) { + reset = true + } + case replication.TriggerKindImmediate: + // Always reset immediate trigger as it is relevent with namespaces + reset = true + default: + // manual trigger, no need to reset + } + } + + if err = ctl.policyManager.UpdatePolicy(updatedPolicy); err != nil { + return err + } + + if reset { + if err = ctl.triggerManager.UnsetTrigger(&originPolicy); err != nil { + return err + } + + return ctl.triggerManager.SetupTrigger(&updatedPolicy) + } + + return nil +} + +//RemovePolicy will remove the specified policy and clean the related settings +func (ctl *DefaultController) RemovePolicy(policyID int64) error { + // TODO check pre-conditions + + policy, err := ctl.policyManager.GetPolicy(policyID) + if err != nil { + return err + } + + if policy.ID == 0 { + return fmt.Errorf("policy %d not found", policyID) + } + + if err = ctl.triggerManager.UnsetTrigger(&policy); err != nil { + return err + } + + return ctl.policyManager.RemovePolicy(policyID) +} + +//GetPolicy is delegation of GetPolicy of Policy.Manager +func (ctl *DefaultController) GetPolicy(policyID int64) (models.ReplicationPolicy, error) { + return ctl.policyManager.GetPolicy(policyID) +} + +//GetPolicies is delegation of GetPoliciemodels.ReplicationPolicy{}s of Policy.Manager +func (ctl *DefaultController) GetPolicies(query models.QueryParameter) ([]models.ReplicationPolicy, error) { + return ctl.policyManager.GetPolicies(query) +} + +//Replicate starts one replication defined in the specified policy; +//Can be launched by the API layer and related triggers. +func (ctl *DefaultController) Replicate(policyID int64, metadata ...map[string]interface{}) error { + policy, err := ctl.GetPolicy(policyID) + if err != nil { + return err + } + if policy.ID == 0 { + return fmt.Errorf("policy %d not found", policyID) + } + + // prepare candidates for replication + candidates := getCandidates(&policy, ctl.sourcer, metadata...) + + /* + targets := []*common_models.RepTarget{} + for _, targetID := range policy.TargetIDs { + target, err := ctl.targetManager.GetTarget(targetID) + if err != nil { + return err + } + targets = append(targets, target) + } + */ + + // submit the replication + return replicate(ctl.replicator, policyID, candidates) +} + +func getCandidates(policy *models.ReplicationPolicy, sourcer *source.Sourcer, + metadata ...map[string]interface{}) []models.FilterItem { + candidates := []models.FilterItem{} + if len(metadata) > 0 { + meta := metadata[0]["candidates"] + if meta != nil { + cands, ok := meta.([]models.FilterItem) + if ok { + candidates = append(candidates, cands...) + } + } + } + + if len(candidates) == 0 { + for _, namespace := range policy.Namespaces { + candidates = append(candidates, models.FilterItem{ + Kind: replication.FilterItemKindProject, + Value: namespace, + Operation: common_models.RepOpTransfer, + }) + } + } + + filterChain := buildFilterChain(policy, sourcer) + + return filterChain.DoFilter(candidates) +} + +func buildFilterChain(policy *models.ReplicationPolicy, sourcer *source.Sourcer) source.FilterChain { + filters := []source.Filter{} + + patterns := map[string]string{} + for _, f := range policy.Filters { + patterns[f.Kind] = f.Pattern + } + + registry := sourcer.GetAdaptor(replication.AdaptorKindHarbor) + // only support repository and tag filter for now + filters = append(filters, + source.NewRepositoryFilter(patterns[replication.FilterItemKindRepository], registry)) + filters = append(filters, + source.NewTagFilter(patterns[replication.FilterItemKindTag], registry)) + + return source.NewDefaultFilterChain(filters) +} + +func replicate(replicator replicator.Replicator, policyID int64, candidates []models.FilterItem) error { + if len(candidates) == 0 { + log.Debugf("replicaton candidates are null, no further action needed") + } + + repositories := map[string][]string{} + // TODO the operation of all candidates are same for now. Update it after supporting + // replicate deletion + operation := "" + for _, candidate := range candidates { + strs := strings.SplitN(candidate.Value, ":", 2) + repositories[strs[0]] = append(repositories[strs[0]], strs[1]) + operation = candidate.Operation + } + + for repository, tags := range repositories { + replication := &client.Replication{ + PolicyID: policyID, + Repository: repository, + Operation: operation, + Tags: tags, + } + log.Debugf("submiting replication job to jobservice: %v", replication) + if err := replicator.Replicate(replication); err != nil { + return err + } + } + return nil +} diff --git a/src/replication/core/controller_test.go b/src/replication/core/controller_test.go new file mode 100644 index 000000000..b88e2165b --- /dev/null +++ b/src/replication/core/controller_test.go @@ -0,0 +1,142 @@ +// Copyright (c) 2017 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 core + +import ( + "os" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/vmware/harbor/src/common/utils/test" + "github.com/vmware/harbor/src/replication" + "github.com/vmware/harbor/src/replication/models" + "github.com/vmware/harbor/src/replication/source" +) + +func TestMain(m *testing.M) { + GlobalController = NewDefaultController(ControllerConfig{}) + // set the policy manager used by GlobalController with a fake policy manager + controller := GlobalController.(*DefaultController) + controller.policyManager = &test.FakePolicyManager{} + os.Exit(m.Run()) +} + +func TestNewDefaultController(t *testing.T) { + controller := NewDefaultController(ControllerConfig{}) + assert.NotNil(t, controller) +} + +func TestInit(t *testing.T) { + assert.Nil(t, GlobalController.Init()) +} + +func TestCreatePolicy(t *testing.T) { + _, err := GlobalController.CreatePolicy(models.ReplicationPolicy{ + Trigger: &models.Trigger{ + Kind: replication.TriggerKindManual, + }, + }) + assert.Nil(t, err) +} + +func TestUpdatePolicy(t *testing.T) { + assert.Nil(t, GlobalController.UpdatePolicy(models.ReplicationPolicy{ + ID: 2, + Trigger: &models.Trigger{ + Kind: replication.TriggerKindManual, + }, + })) +} + +func TestRemovePolicy(t *testing.T) { + assert.Nil(t, GlobalController.RemovePolicy(1)) +} + +func TestGetPolicy(t *testing.T) { + _, err := GlobalController.GetPolicy(1) + assert.Nil(t, err) +} + +func TestGetPolicies(t *testing.T) { + _, err := GlobalController.GetPolicies(models.QueryParameter{}) + assert.Nil(t, err) +} + +func TestReplicate(t *testing.T) { + // TODO +} + +func TestGetCandidates(t *testing.T) { + policy := &models.ReplicationPolicy{ + ID: 1, + Filters: []models.Filter{ + models.Filter{ + Kind: replication.FilterItemKindTag, + Pattern: "*", + }, + }, + Trigger: &models.Trigger{ + Kind: replication.TriggerKindImmediate, + }, + } + + sourcer := source.NewSourcer() + + candidates := []models.FilterItem{ + models.FilterItem{ + Kind: replication.FilterItemKindTag, + Value: "library/hello-world:release-1.0", + }, + models.FilterItem{ + Kind: replication.FilterItemKindTag, + Value: "library/hello-world:latest", + }, + } + metadata := map[string]interface{}{ + "candidates": candidates, + } + result := getCandidates(policy, sourcer, metadata) + assert.Equal(t, 2, len(result)) + + policy.Filters = []models.Filter{ + models.Filter{ + Kind: replication.FilterItemKindTag, + Pattern: "release-*", + }, + } + result = getCandidates(policy, sourcer, metadata) + assert.Equal(t, 1, len(result)) +} + +func TestBuildFilterChain(t *testing.T) { + policy := &models.ReplicationPolicy{ + ID: 1, + Filters: []models.Filter{ + models.Filter{ + Kind: replication.FilterItemKindRepository, + Pattern: "*", + }, + models.Filter{ + Kind: replication.FilterItemKindTag, + Pattern: "*", + }, + }, + } + + sourcer := source.NewSourcer() + + chain := buildFilterChain(policy, sourcer) + assert.Equal(t, 2, len(chain.Filters())) +} diff --git a/src/replication/event/init.go b/src/replication/event/init.go new file mode 100644 index 000000000..0f79739e3 --- /dev/null +++ b/src/replication/event/init.go @@ -0,0 +1,39 @@ +// Copyright (c) 2017 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 event + +import ( + "github.com/vmware/harbor/src/common/notifier" + "github.com/vmware/harbor/src/common/utils/log" + "github.com/vmware/harbor/src/replication/event/topic" +) + +//Subscribe related topics +func init() { + //Listen the related event topics + handlers := map[string]notifier.NotificationHandler{ + topic.StartReplicationTopic: &StartReplicationHandler{}, + topic.ReplicationEventTopicOnPush: &OnPushHandler{}, + topic.ReplicationEventTopicOnDeletion: &OnDeletionHandler{}, + } + + for topic, handler := range handlers { + if err := notifier.Subscribe(topic, handler); err != nil { + log.Errorf("failed to subscribe topic %s: %v", topic, err) + continue + } + log.Debugf("topic %s is subscribed", topic) + } +} diff --git a/src/replication/event/notification/notification.go b/src/replication/event/notification/notification.go new file mode 100644 index 000000000..fa2309b78 --- /dev/null +++ b/src/replication/event/notification/notification.go @@ -0,0 +1,34 @@ +// Copyright (c) 2017 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 notification + +//OnPushNotification contains the data required by this handler +type OnPushNotification struct { + //The name of the image that is being pushed + Image string +} + +//OnDeletionNotification contains the data required by this handler +type OnDeletionNotification struct { + //The name of the image that is being deleted + Image string +} + +//StartReplicationNotification contains data required by this handler +type StartReplicationNotification struct { + //ID of the policy + PolicyID int64 + Metadata map[string]interface{} +} diff --git a/src/replication/event/on_deletion_handler.go b/src/replication/event/on_deletion_handler.go new file mode 100644 index 000000000..ac7b9697b --- /dev/null +++ b/src/replication/event/on_deletion_handler.go @@ -0,0 +1,48 @@ +// Copyright (c) 2017 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 event + +import ( + "errors" + "fmt" + "reflect" + + "github.com/vmware/harbor/src/common/models" + "github.com/vmware/harbor/src/replication/event/notification" +) + +//OnDeletionHandler implements the notification handler interface to handle image on push event. +type OnDeletionHandler struct{} + +//Handle implements the same method of notification handler interface +func (oph *OnDeletionHandler) Handle(value interface{}) error { + if value == nil { + return errors.New("OnDeletionHandler can not handle nil value") + } + + vType := reflect.TypeOf(value) + if vType.Kind() != reflect.Struct || vType.String() != "notification.OnDeletionNotification" { + return fmt.Errorf("Mismatch value type of OnDeletionHandler, expect %s but got %s", "notification.OnDeletionNotification", vType.String()) + } + + notification := value.(notification.OnDeletionNotification) + return checkAndTriggerReplication(notification.Image, models.RepOpDelete) +} + +//IsStateful implements the same method of notification handler interface +func (oph *OnDeletionHandler) IsStateful() bool { + //Statless + return false +} diff --git a/src/replication/event/on_deletion_handler_test.go b/src/replication/event/on_deletion_handler_test.go new file mode 100644 index 000000000..e34792f07 --- /dev/null +++ b/src/replication/event/on_deletion_handler_test.go @@ -0,0 +1,43 @@ +// Copyright (c) 2017 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 event + +import ( + "testing" + + "github.com/stretchr/testify/assert" + "github.com/vmware/harbor/src/common/dao" + "github.com/vmware/harbor/src/common/utils/test" + "github.com/vmware/harbor/src/replication/event/notification" +) + +func TestHandleOfOnDeletionHandler(t *testing.T) { + dao.DefaultDatabaseWatchItemDAO = &test.FakeWatchItemDAO{} + + handler := &OnDeletionHandler{} + + assert.NotNil(t, handler.Handle(nil)) + assert.NotNil(t, handler.Handle(map[string]string{})) + assert.NotNil(t, handler.Handle(struct{}{})) + + assert.Nil(t, handler.Handle(notification.OnDeletionNotification{ + Image: "library/hello-world:latest", + })) +} + +func TestIsStatefulOfOnDeletionHandler(t *testing.T) { + handler := &OnDeletionHandler{} + assert.False(t, handler.IsStateful()) +} diff --git a/src/replication/event/on_push_handler.go b/src/replication/event/on_push_handler.go new file mode 100644 index 000000000..f00468ca2 --- /dev/null +++ b/src/replication/event/on_push_handler.go @@ -0,0 +1,91 @@ +// Copyright (c) 2017 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 event + +import ( + "errors" + "fmt" + "reflect" + + common_models "github.com/vmware/harbor/src/common/models" + "github.com/vmware/harbor/src/common/notifier" + "github.com/vmware/harbor/src/common/utils" + "github.com/vmware/harbor/src/common/utils/log" + "github.com/vmware/harbor/src/replication" + "github.com/vmware/harbor/src/replication/event/notification" + "github.com/vmware/harbor/src/replication/event/topic" + "github.com/vmware/harbor/src/replication/models" + "github.com/vmware/harbor/src/replication/trigger" +) + +//OnPushHandler implements the notification handler interface to handle image on push event. +type OnPushHandler struct{} + +//Handle implements the same method of notification handler interface +func (oph *OnPushHandler) Handle(value interface{}) error { + if value == nil { + return errors.New("OnPushHandler can not handle nil value") + } + + vType := reflect.TypeOf(value) + if vType.Kind() != reflect.Struct || vType.String() != "notification.OnPushNotification" { + return fmt.Errorf("Mismatch value type of OnPushHandler, expect %s but got %s", "notification.OnPushNotification", vType.String()) + } + + notification := value.(notification.OnPushNotification) + + return checkAndTriggerReplication(notification.Image, common_models.RepOpTransfer) +} + +//IsStateful implements the same method of notification handler interface +func (oph *OnPushHandler) IsStateful() bool { + //Statless + return false +} + +// checks whether replication policy is set on the resource, if is, trigger the replication +func checkAndTriggerReplication(image, operation string) error { + project, _ := utils.ParseRepository(image) + watchItems, err := trigger.DefaultWatchList.Get(project, operation) + if err != nil { + return fmt.Errorf("failed to get watch list for resource %s, operation %s: %v", + image, operation, err) + } + if len(watchItems) == 0 { + log.Debugf("no replication should be triggered for resource %s, operation %s, skip", image, operation) + return nil + } + + for _, watchItem := range watchItems { + item := models.FilterItem{ + Kind: replication.FilterItemKindTag, + Value: image, + Operation: operation, + } + + if err := notifier.Publish(topic.StartReplicationTopic, notification.StartReplicationNotification{ + PolicyID: watchItem.PolicyID, + Metadata: map[string]interface{}{ + "candidates": []models.FilterItem{item}, + }, + }); err != nil { + return fmt.Errorf("failed to publish replication topic for resource %s, operation %s, policy %d: %v", + image, operation, watchItem.PolicyID, err) + } + log.Infof("replication topic for resource %s, operation %s, policy %d triggered", + image, operation, watchItem.PolicyID) + } + return nil +} diff --git a/src/replication/event/on_push_handler_test.go b/src/replication/event/on_push_handler_test.go new file mode 100644 index 000000000..bcd57605c --- /dev/null +++ b/src/replication/event/on_push_handler_test.go @@ -0,0 +1,43 @@ +// Copyright (c) 2017 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 event + +import ( + "testing" + + "github.com/stretchr/testify/assert" + "github.com/vmware/harbor/src/common/dao" + "github.com/vmware/harbor/src/common/utils/test" + "github.com/vmware/harbor/src/replication/event/notification" +) + +func TestHandleOfOnPushHandler(t *testing.T) { + dao.DefaultDatabaseWatchItemDAO = &test.FakeWatchItemDAO{} + + handler := &OnPushHandler{} + + assert.NotNil(t, handler.Handle(nil)) + assert.NotNil(t, handler.Handle(map[string]string{})) + assert.NotNil(t, handler.Handle(struct{}{})) + + assert.Nil(t, handler.Handle(notification.OnPushNotification{ + Image: "library/hello-world:latest", + })) +} + +func TestIsStatefulOfOnPushHandler(t *testing.T) { + handler := &OnPushHandler{} + assert.False(t, handler.IsStateful()) +} diff --git a/src/replication/event/start_replication_handler.go b/src/replication/event/start_replication_handler.go new file mode 100644 index 000000000..7e29ef542 --- /dev/null +++ b/src/replication/event/start_replication_handler.go @@ -0,0 +1,53 @@ +// Copyright (c) 2017 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 event + +import ( + "errors" + "fmt" + "reflect" + + "github.com/vmware/harbor/src/replication/core" + "github.com/vmware/harbor/src/replication/event/notification" +) + +//StartReplicationHandler implements the notification handler interface to handle start replication requests. +type StartReplicationHandler struct{} + +//Handle implements the same method of notification handler interface +func (srh *StartReplicationHandler) Handle(value interface{}) error { + if value == nil { + return errors.New("StartReplicationHandler can not handle nil value") + } + + vType := reflect.TypeOf(value) + if vType.Kind() != reflect.Struct || vType.String() != "notification.StartReplicationNotification" { + return fmt.Errorf("Mismatch value type of StartReplicationHandler, expect %s but got %s", "notification.StartReplicationNotification", vType.String()) + } + + notification := value.(notification.StartReplicationNotification) + if notification.PolicyID <= 0 { + return errors.New("Invalid policy") + } + + //Start replication + return core.GlobalController.Replicate(notification.PolicyID, notification.Metadata) +} + +//IsStateful implements the same method of notification handler interface +func (srh *StartReplicationHandler) IsStateful() bool { + //Stateless + return false +} diff --git a/src/replication/event/start_replication_handler_test.go b/src/replication/event/start_replication_handler_test.go new file mode 100644 index 000000000..88ba61fbc --- /dev/null +++ b/src/replication/event/start_replication_handler_test.go @@ -0,0 +1,45 @@ +// Copyright (c) 2017 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 event + +import ( + "testing" + + "github.com/stretchr/testify/assert" + "github.com/vmware/harbor/src/common/utils/test" + "github.com/vmware/harbor/src/replication/core" + "github.com/vmware/harbor/src/replication/event/notification" +) + +func TestHandle(t *testing.T) { + core.GlobalController = &test.FakeReplicatoinController{} + + handler := &StartReplicationHandler{} + + assert.NotNil(t, handler.Handle(nil)) + assert.NotNil(t, handler.Handle(map[string]string{})) + assert.NotNil(t, handler.Handle(struct{}{})) + assert.NotNil(t, handler.Handle(notification.StartReplicationNotification{ + PolicyID: -1, + })) + assert.Nil(t, handler.Handle(notification.StartReplicationNotification{ + PolicyID: 1, + })) +} + +func TestIsStateful(t *testing.T) { + handler := &StartReplicationHandler{} + assert.False(t, handler.IsStateful()) +} diff --git a/src/replication/event/topic/topics.go b/src/replication/event/topic/topics.go new file mode 100644 index 000000000..fce3b9c81 --- /dev/null +++ b/src/replication/event/topic/topics.go @@ -0,0 +1,12 @@ +package topic + +const ( + //ReplicationEventTopicOnPush : OnPush event + ReplicationEventTopicOnPush = "OnPush" + + //ReplicationEventTopicOnDeletion : OnDeletion event + ReplicationEventTopicOnDeletion = "OnDeletion" + + //StartReplicationTopic : Start application request + StartReplicationTopic = "StartReplication" +) diff --git a/src/replication/models/filter.go b/src/replication/models/filter.go new file mode 100644 index 000000000..648d6247c --- /dev/null +++ b/src/replication/models/filter.go @@ -0,0 +1,41 @@ +// Copyright (c) 2017 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 models + +import ( + "fmt" + + "github.com/astaxie/beego/validation" + "github.com/vmware/harbor/src/replication" +) + +// Filter is the data model represents the filter defined by user. +type Filter struct { + Kind string `json:"kind"` + Pattern string `json:"pattern"` +} + +// Valid ... +func (f *Filter) Valid(v *validation.Validation) { + if !(f.Kind == replication.FilterItemKindProject || + f.Kind == replication.FilterItemKindRepository || + f.Kind == replication.FilterItemKindTag) { + v.SetError("kind", fmt.Sprintf("invalid filter kind: %s", f.Kind)) + } + + if len(f.Pattern) == 0 { + v.SetError("pattern", "filter pattern can not be empty") + } +} diff --git a/src/replication/models/filter_config.go b/src/replication/models/filter_config.go new file mode 100644 index 000000000..149780eaa --- /dev/null +++ b/src/replication/models/filter_config.go @@ -0,0 +1,7 @@ +package models + +//FilterConfig is data model to provide configurations to the filters. +type FilterConfig struct { + //The pattern for fuzzy matching + Pattern string +} diff --git a/src/replication/models/filter_item.go b/src/replication/models/filter_item.go new file mode 100644 index 000000000..82497dd90 --- /dev/null +++ b/src/replication/models/filter_item.go @@ -0,0 +1,35 @@ +// Copyright (c) 2017 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 models + +//FilterItem is the general data model represents the filtering resources which are used as input and output for the filters. +type FilterItem struct { + + //The kind of the filtering resources. Support 'project','repository' and 'tag' etc. + Kind string `json:"kind"` + + //The key value of resource which can be used to filter out the resource matched with specified pattern. + //E.g: + //kind == 'project', value will be project name; + //kind == 'repository', value will be repository name + //kind == 'tag', value will be tag name. + Value string `json:"value"` + + Operation string `json:"operation"` + + //Extension placeholder. + //To append more additional information if required by the filter. + Metadata map[string]interface{} `json:"metadata"` +} diff --git a/src/replication/models/filter_test.go b/src/replication/models/filter_test.go new file mode 100644 index 000000000..4026f0e20 --- /dev/null +++ b/src/replication/models/filter_test.go @@ -0,0 +1,45 @@ +// Copyright (c) 2017 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 models + +import ( + "testing" + + "github.com/astaxie/beego/validation" + "github.com/stretchr/testify/assert" + "github.com/vmware/harbor/src/replication" +) + +func TestValid(t *testing.T) { + cases := map[*Filter]bool{ + &Filter{}: true, + &Filter{ + Kind: "invalid_kind", + }: true, + &Filter{ + Kind: replication.FilterItemKindRepository, + }: true, + &Filter{ + Kind: replication.FilterItemKindRepository, + Pattern: "*", + }: false, + } + + for filter, hasError := range cases { + v := &validation.Validation{} + filter.Valid(v) + assert.Equal(t, hasError, v.HasErrors()) + } +} diff --git a/src/replication/models/policy.go b/src/replication/models/policy.go new file mode 100644 index 000000000..f6ab0217d --- /dev/null +++ b/src/replication/models/policy.go @@ -0,0 +1,38 @@ +package models + +import ( + "time" +) + +//ReplicationPolicy defines the structure of a replication policy. +type ReplicationPolicy struct { + ID int64 //UUID of the policy + Name string + Description string + Filters []Filter + ReplicateDeletion bool + Trigger *Trigger //The trigger of the replication + ProjectIDs []int64 //Projects attached to this policy + TargetIDs []int64 + Namespaces []string // The namespaces are used to set immediate trigger + CreationTime time.Time + UpdateTime time.Time +} + +//QueryParameter defines the parameters used to do query selection. +type QueryParameter struct { + //Query by page, couple with pageSize + Page int64 + + //Size of each page, couple with page + PageSize int64 + + //Query by the type of trigger + TriggerType string + + //Query by project ID + ProjectID int64 + + //Query by name + Name string +} diff --git a/src/replication/models/registry_models.go b/src/replication/models/registry_models.go new file mode 100644 index 000000000..69be2268c --- /dev/null +++ b/src/replication/models/registry_models.go @@ -0,0 +1,34 @@ +package models + +//Namespace is the resource group/scope like project in Harbor and organization in docker hub. +type Namespace struct { + //Name of the namespace + Name string + + //Extensions to provide flexibility + Metadata map[string]interface{} +} + +//Repository is to keep the info of image repository. +type Repository struct { + //Name of the repository + Name string + + //Project reference of this repository belongs to + Namespace Namespace + + //Extensions to provide flexibility + Metadata map[string]interface{} +} + +//Tag keeps the info of image with specified version +type Tag struct { + //Name of the tag + Name string + + //The repository reference of this tag belongs to + Repository Repository + + //Extensions to provide flexibility + Metadata map[string]interface{} +} diff --git a/src/replication/models/trigger.go b/src/replication/models/trigger.go new file mode 100644 index 000000000..4af3e5329 --- /dev/null +++ b/src/replication/models/trigger.go @@ -0,0 +1,79 @@ +// Copyright (c) 2017 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 models + +import ( + "fmt" + + "github.com/astaxie/beego/validation" + "github.com/vmware/harbor/src/replication" +) + +//Trigger is replication launching approach definition +type Trigger struct { + Kind string `json:"kind"` // the type of the trigger + ScheduleParam *ScheduleParam `json:"schedule_param"` // optional, only used when kind is 'schedule' +} + +// Valid ... +func (t *Trigger) Valid(v *validation.Validation) { + if !(t.Kind == replication.TriggerKindImmediate || + t.Kind == replication.TriggerKindManual || + t.Kind == replication.TriggerKindSchedule) { + v.SetError("kind", fmt.Sprintf("invalid trigger kind: %s", t.Kind)) + } + + if t.Kind == replication.TriggerKindSchedule { + if t.ScheduleParam == nil { + v.SetError("schedule_param", "empty schedule_param") + } else { + t.ScheduleParam.Valid(v) + } + } +} + +// ScheduleParam defines the parameters used by schedule trigger +type ScheduleParam struct { + Type string `json:"type"` //daily or weekly + Weekday int8 `json:"weekday"` //Optional, only used when type is 'weekly' + Offtime int64 `json:"offtime"` //The time offset with the UTC 00:00 in seconds +} + +// Valid ... +func (s *ScheduleParam) Valid(v *validation.Validation) { + if !(s.Type == replication.TriggerScheduleDaily || + s.Type == replication.TriggerScheduleWeekly) { + v.SetError("type", fmt.Sprintf("invalid schedule trigger parameter type: %s", s.Type)) + } + + if s.Type == replication.TriggerScheduleWeekly { + if s.Weekday < 1 || s.Weekday > 7 { + v.SetError("weekday", fmt.Sprintf("invalid schedule trigger parameter weekday: %d", s.Weekday)) + } + } + + if s.Offtime < 0 || s.Offtime > 3600*24 { + v.SetError("offtime", fmt.Sprintf("invalid schedule trigger parameter offtime: %d", s.Offtime)) + } +} + +// Equal ... +func (s *ScheduleParam) Equal(param *ScheduleParam) bool { + if param == nil { + return false + } + + return s.Type == param.Type && s.Weekday == param.Weekday && s.Offtime == param.Offtime +} diff --git a/src/replication/models/trigger_test.go b/src/replication/models/trigger_test.go new file mode 100644 index 000000000..2aba67a44 --- /dev/null +++ b/src/replication/models/trigger_test.go @@ -0,0 +1,77 @@ +// Copyright (c) 2017 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 models + +import ( + "testing" + + "github.com/astaxie/beego/validation" + "github.com/stretchr/testify/assert" + "github.com/vmware/harbor/src/replication" +) + +func TestValidOfTrigger(t *testing.T) { + cases := map[*Trigger]bool{ + &Trigger{}: true, + &Trigger{ + Kind: "invalid_kind", + }: true, + &Trigger{ + Kind: replication.TriggerKindImmediate, + }: false, + &Trigger{ + Kind: replication.TriggerKindSchedule, + }: true, + } + + for filter, hasError := range cases { + v := &validation.Validation{} + filter.Valid(v) + assert.Equal(t, hasError, v.HasErrors()) + } +} + +func TestValidOfScheduleParam(t *testing.T) { + cases := map[*ScheduleParam]bool{ + &ScheduleParam{}: true, + &ScheduleParam{ + Type: "invalid_type", + }: true, + &ScheduleParam{ + Type: replication.TriggerScheduleDaily, + Offtime: 3600*24 + 1, + }: true, + &ScheduleParam{ + Type: replication.TriggerScheduleDaily, + Offtime: 3600 * 2, + }: false, + &ScheduleParam{ + Type: replication.TriggerScheduleWeekly, + Weekday: 0, + Offtime: 3600 * 2, + }: true, + &ScheduleParam{ + Type: replication.TriggerScheduleWeekly, + Weekday: 7, + Offtime: 3600 * 2, + }: false, + } + + for param, hasError := range cases { + v := &validation.Validation{} + param.Valid(v) + assert.Equal(t, hasError, v.HasErrors()) + } +} diff --git a/src/replication/policy/manager.go b/src/replication/policy/manager.go new file mode 100644 index 000000000..a6049dbfe --- /dev/null +++ b/src/replication/policy/manager.go @@ -0,0 +1,188 @@ +// Copyright (c) 2017 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 policy + +import ( + "encoding/json" + "time" + + "github.com/vmware/harbor/src/common/dao" + persist_models "github.com/vmware/harbor/src/common/models" + "github.com/vmware/harbor/src/replication/models" + "github.com/vmware/harbor/src/ui/config" +) + +// Manager defines the method a policy manger should implement +type Manager interface { + GetPolicies(models.QueryParameter) ([]models.ReplicationPolicy, error) + GetPolicy(int64) (models.ReplicationPolicy, error) + CreatePolicy(models.ReplicationPolicy) (int64, error) + UpdatePolicy(models.ReplicationPolicy) error + RemovePolicy(int64) error +} + +//DefaultManager provides replication policy CURD capabilities. +type DefaultManager struct{} + +//NewDefaultManager is the constructor of DefaultManager. +func NewDefaultManager() *DefaultManager { + return &DefaultManager{} +} + +//GetPolicies returns all the policies +func (m *DefaultManager) GetPolicies(query models.QueryParameter) ([]models.ReplicationPolicy, error) { + result := []models.ReplicationPolicy{} + //TODO support more query conditions other than name and project ID + policies, err := dao.FilterRepPolicies(query.Name, query.ProjectID) + if err != nil { + return result, err + } + + for _, policy := range policies { + ply, err := convertFromPersistModel(policy) + if err != nil { + return []models.ReplicationPolicy{}, err + } + + if len(query.TriggerType) > 0 { + if ply.Trigger.Kind != query.TriggerType { + continue + } + } + + result = append(result, ply) + } + + return result, nil +} + +//GetPolicy returns the policy with the specified ID +func (m *DefaultManager) GetPolicy(policyID int64) (models.ReplicationPolicy, error) { + policy, err := dao.GetRepPolicy(policyID) + if err != nil { + return models.ReplicationPolicy{}, err + } + + return convertFromPersistModel(policy) +} + +func convertFromPersistModel(policy *persist_models.RepPolicy) (models.ReplicationPolicy, error) { + if policy == nil { + return models.ReplicationPolicy{}, nil + } + + ply := models.ReplicationPolicy{ + ID: policy.ID, + Name: policy.Name, + Description: policy.Description, + ReplicateDeletion: policy.ReplicateDeletion, + ProjectIDs: []int64{policy.ProjectID}, + TargetIDs: []int64{policy.TargetID}, + CreationTime: policy.CreationTime, + UpdateTime: policy.UpdateTime, + } + + project, err := config.GlobalProjectMgr.Get(policy.ProjectID) + if err != nil { + return models.ReplicationPolicy{}, err + } + ply.Namespaces = []string{project.Name} + + if len(policy.Filters) > 0 { + filters := []models.Filter{} + if err := json.Unmarshal([]byte(policy.Filters), &filters); err != nil { + return models.ReplicationPolicy{}, err + } + ply.Filters = filters + } + + if len(policy.Trigger) > 0 { + trigger := &models.Trigger{} + if err := json.Unmarshal([]byte(policy.Trigger), trigger); err != nil { + return models.ReplicationPolicy{}, err + } + ply.Trigger = trigger + } + + return ply, nil +} + +func convertToPersistModel(policy models.ReplicationPolicy) (*persist_models.RepPolicy, error) { + ply := &persist_models.RepPolicy{ + ID: policy.ID, + Name: policy.Name, + Description: policy.Description, + ReplicateDeletion: policy.ReplicateDeletion, + CreationTime: policy.CreationTime, + UpdateTime: policy.UpdateTime, + } + + if len(policy.ProjectIDs) > 0 { + ply.ProjectID = policy.ProjectIDs[0] + } + + if len(policy.TargetIDs) > 0 { + ply.TargetID = policy.TargetIDs[0] + } + + if policy.Trigger != nil { + trigger, err := json.Marshal(policy.Trigger) + if err != nil { + return nil, err + } + ply.Trigger = string(trigger) + } + + if len(policy.Filters) > 0 { + filters, err := json.Marshal(policy.Filters) + if err != nil { + return nil, err + } + ply.Filters = string(filters) + } + + return ply, nil +} + +//CreatePolicy creates a new policy with the provided data; +//If creating failed, error will be returned; +//If creating succeed, ID of the new created policy will be returned. +func (m *DefaultManager) CreatePolicy(policy models.ReplicationPolicy) (int64, error) { + now := time.Now() + policy.CreationTime = now + policy.UpdateTime = now + ply, err := convertToPersistModel(policy) + if err != nil { + return 0, err + } + return dao.AddRepPolicy(*ply) +} + +//UpdatePolicy updates the policy; +//If updating failed, error will be returned. +func (m *DefaultManager) UpdatePolicy(policy models.ReplicationPolicy) error { + policy.UpdateTime = time.Now() + ply, err := convertToPersistModel(policy) + if err != nil { + return err + } + return dao.UpdateRepPolicy(ply) +} + +//RemovePolicy removes the specified policy; +//If removing failed, error will be returned. +func (m *DefaultManager) RemovePolicy(policyID int64) error { + return dao.DeleteRepPolicy(policyID) +} diff --git a/src/replication/policy/manager_test.go b/src/replication/policy/manager_test.go new file mode 100644 index 000000000..62337aff7 --- /dev/null +++ b/src/replication/policy/manager_test.go @@ -0,0 +1,60 @@ +// Copyright (c) 2017 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 policy + +import ( + "encoding/json" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "github.com/vmware/harbor/src/replication/models" +) + +func TestConvertToPersistModel(t *testing.T) { + var id, projectID, targetID int64 = 1, 1, 1 + name := "policy01" + replicateDeletion := true + trigger := &models.Trigger{ + Kind: "trigger_kind", + } + filters := []models.Filter{ + models.Filter{ + Kind: "filter_kind", + Pattern: "filter_pattern", + }, + } + policy := models.ReplicationPolicy{ + ID: id, + Name: name, + ReplicateDeletion: replicateDeletion, + ProjectIDs: []int64{projectID}, + TargetIDs: []int64{targetID}, + Trigger: trigger, + Filters: filters, + } + + ply, err := convertToPersistModel(policy) + require.Nil(t, err) + assert.Equal(t, id, ply.ID) + assert.Equal(t, name, ply.Name) + assert.Equal(t, replicateDeletion, ply.ReplicateDeletion) + assert.Equal(t, projectID, ply.ProjectID) + assert.Equal(t, targetID, ply.TargetID) + tg, _ := json.Marshal(trigger) + assert.Equal(t, string(tg), ply.Trigger) + ft, _ := json.Marshal(filters) + assert.Equal(t, string(ft), ply.Filters) +} diff --git a/src/replication/registry/adaptor.go b/src/replication/registry/adaptor.go new file mode 100644 index 000000000..29b3cf138 --- /dev/null +++ b/src/replication/registry/adaptor.go @@ -0,0 +1,34 @@ +package registry + +import ( + "github.com/vmware/harbor/src/replication/models" +) + +//Adaptor defines the unified operations for all the supported registries such as Harbor or DockerHub. +//It's used to adapt the different interfaces provied by the different registry providers. +//Use external registry with restful api providing as example, these intrefaces may depends on the +//related restful apis like: +// /api/vx/repositories/{namespace}/{repositoryName}/tags/{name} +// /api/v0/accounts/{namespace} +type Adaptor interface { + //Return the unique kind identifier of the adaptor + Kind() string + + //Get all the namespaces + GetNamespaces() []models.Namespace + + //Get the namespace with the specified name + GetNamespace(name string) models.Namespace + + //Get all the repositories under the specified namespace + GetRepositories(namespace string) []models.Repository + + //Get the repository with the specified name under the specified namespace + GetRepository(name string, namespace string) models.Repository + + //Get all the tags of the specified repository under the namespace + GetTags(repositoryName string, namespace string) []models.Tag + + //Get the tag with the specified name of the repository under the namespace + GetTag(name string, repositoryName string, namespace string) models.Tag +} diff --git a/src/replication/registry/harbor_adaptor.go b/src/replication/registry/harbor_adaptor.go new file mode 100644 index 000000000..e28622f93 --- /dev/null +++ b/src/replication/registry/harbor_adaptor.go @@ -0,0 +1,80 @@ +package registry + +import ( + "github.com/vmware/harbor/src/common/dao" + "github.com/vmware/harbor/src/common/utils/log" + "github.com/vmware/harbor/src/replication" + "github.com/vmware/harbor/src/replication/models" + "github.com/vmware/harbor/src/ui/utils" +) + +// TODO refacotor the methods of HarborAdaptor by caling Harbor's API + +//HarborAdaptor is defined to adapt the Harbor registry +type HarborAdaptor struct{} + +//Kind returns the unique kind identifier of the adaptor +func (ha *HarborAdaptor) Kind() string { + return replication.AdaptorKindHarbor +} + +//GetNamespaces is ued to get all the namespaces +func (ha *HarborAdaptor) GetNamespaces() []models.Namespace { + return nil +} + +//GetNamespace is used to get the namespace with the specified name +func (ha *HarborAdaptor) GetNamespace(name string) models.Namespace { + return models.Namespace{} +} + +//GetRepositories is used to get all the repositories under the specified namespace +func (ha *HarborAdaptor) GetRepositories(namespace string) []models.Repository { + repos, err := dao.GetRepositoryByProjectName(namespace) + if err != nil { + log.Errorf("failed to get repositories under namespace %s: %v", namespace, err) + return nil + } + + repositories := []models.Repository{} + for _, repo := range repos { + repositories = append(repositories, models.Repository{ + Name: repo.Name, + }) + } + return repositories +} + +//GetRepository is used to get the repository with the specified name under the specified namespace +func (ha *HarborAdaptor) GetRepository(name string, namespace string) models.Repository { + return models.Repository{} +} + +//GetTags is used to get all the tags of the specified repository under the namespace +func (ha *HarborAdaptor) GetTags(repositoryName string, namespace string) []models.Tag { + client, err := utils.NewRepositoryClientForUI("harbor-ui", repositoryName) + if err != nil { + log.Errorf("failed to create registry client: %v", err) + return nil + } + + ts, err := client.ListTag() + if err != nil { + log.Errorf("failed to get tags of repository %s: %v", repositoryName, err) + return nil + } + + tags := []models.Tag{} + for _, t := range ts { + tags = append(tags, models.Tag{ + Name: t, + }) + } + + return tags +} + +//GetTag is used to get the tag with the specified name of the repository under the namespace +func (ha *HarborAdaptor) GetTag(name string, repositoryName string, namespace string) models.Tag { + return models.Tag{} +} diff --git a/src/replication/replicator/replicator.go b/src/replication/replicator/replicator.go new file mode 100644 index 000000000..37e387ce8 --- /dev/null +++ b/src/replication/replicator/replicator.go @@ -0,0 +1,41 @@ +// Copyright (c) 2017 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 replicator + +import ( + "github.com/vmware/harbor/src/jobservice/client" +) + +// Replicator submits the replication work to the jobservice +type Replicator interface { + Replicate(*client.Replication) error +} + +// DefaultReplicator provides a default implement for Replicator +type DefaultReplicator struct { + client client.Client +} + +// NewDefaultReplicator returns an instance of DefaultReplicator +func NewDefaultReplicator(client client.Client) *DefaultReplicator { + return &DefaultReplicator{ + client: client, + } +} + +// Replicate ... +func (d *DefaultReplicator) Replicate(replication *client.Replication) error { + return d.client.SubmitReplicationJob(replication) +} diff --git a/src/adminserver/client/auth/auth.go b/src/replication/replicator/replicator_test.go similarity index 53% rename from src/adminserver/client/auth/auth.go rename to src/replication/replicator/replicator_test.go index 038a8c122..934939cd0 100644 --- a/src/adminserver/client/auth/auth.go +++ b/src/replication/replicator/replicator_test.go @@ -12,39 +12,26 @@ // See the License for the specific language governing permissions and // limitations under the License. -package auth +package replicator import ( - "net/http" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/vmware/harbor/src/jobservice/client" ) -// Authorizer authorizes request -type Authorizer interface { - Authorize(*http.Request) error -} - -// NewSecretAuthorizer returns an instance of secretAuthorizer -func NewSecretAuthorizer(cookieName, secret string) Authorizer { - return &secretAuthorizer{ - cookieName: cookieName, - secret: secret, - } -} - -type secretAuthorizer struct { - cookieName string - secret string -} - -func (s *secretAuthorizer) Authorize(req *http.Request) error { - if req == nil { - return nil - } - - req.AddCookie(&http.Cookie{ - Name: s.cookieName, - Value: s.secret, - }) +type fakeJobserviceClient struct{} +func (f *fakeJobserviceClient) SubmitReplicationJob(replication *client.Replication) error { return nil } + +func (f *fakeJobserviceClient) StopReplicationJobs(policyID int64) error { + return nil +} + +func TestReplicate(t *testing.T) { + replicator := NewDefaultReplicator(&fakeJobserviceClient{}) + assert.Nil(t, replicator.Replicate(&client.Replication{})) +} diff --git a/src/replication/source/convertor.go b/src/replication/source/convertor.go new file mode 100644 index 000000000..d07aafd35 --- /dev/null +++ b/src/replication/source/convertor.go @@ -0,0 +1,16 @@ +package source + +import ( + "github.com/vmware/harbor/src/replication/models" +) + +//Convertor is designed to covert the format of output from upstream filter to the input format +//required by the downstream filter if needed. +//Each convertor covers only one specified conversion process between the two filters. +//E.g: +//If project filter connects to repository filter, then one convertor should be defined for this connection; +//If project filter connects to tag filter, then another one should be defined. The above one can not be reused. +type Convertor interface { + //Accept the items from upstream filter as input and then covert them to the required format and returned. + Convert(itemsOfUpstream []models.FilterItem) (itemsOfDownstream []models.FilterItem) +} diff --git a/src/replication/source/default_filter_chain.go b/src/replication/source/default_filter_chain.go new file mode 100644 index 000000000..1acd29bf8 --- /dev/null +++ b/src/replication/source/default_filter_chain.go @@ -0,0 +1,57 @@ +// Copyright (c) 2017 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 source + +import ( + "github.com/vmware/harbor/src/replication/models" +) + +// DefaultFilterChain provides a default implement for interface FilterChain +type DefaultFilterChain struct { + filters []Filter +} + +// NewDefaultFilterChain returns an instance of DefaultFilterChain +func NewDefaultFilterChain(filters []Filter) *DefaultFilterChain { + return &DefaultFilterChain{ + filters: filters, + } +} + +// Build nil implement now +func (d *DefaultFilterChain) Build(filters []Filter) error { + return nil +} + +// Filters returns the filter list +func (d *DefaultFilterChain) Filters() []Filter { + return d.filters +} + +// DoFilter does the filter works for filterItems +func (d *DefaultFilterChain) DoFilter(filterItems []models.FilterItem) []models.FilterItem { + if len(filterItems) == 0 { + return []models.FilterItem{} + } + + for _, filter := range d.filters { + convertor := filter.GetConvertor() + if convertor != nil { + filterItems = convertor.Convert(filterItems) + } + filterItems = filter.DoFilter(filterItems) + } + return filterItems +} diff --git a/src/replication/source/default_filter_chain_test.go b/src/replication/source/default_filter_chain_test.go new file mode 100644 index 000000000..ee1eec79a --- /dev/null +++ b/src/replication/source/default_filter_chain_test.go @@ -0,0 +1,75 @@ +// Copyright (c) 2017 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 source + +import ( + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "github.com/vmware/harbor/src/replication" + "github.com/vmware/harbor/src/replication/models" +) + +func TestBuild(t *testing.T) { + chain := NewDefaultFilterChain(nil) + require.Nil(t, chain.Build(nil)) +} + +func TestFilters(t *testing.T) { + filters := []Filter{NewPatternFilter("project", "*")} + chain := NewDefaultFilterChain(filters) + assert.EqualValues(t, filters, chain.Filters()) +} + +func TestDoFilter(t *testing.T) { + projectFilter := NewPatternFilter(replication.FilterItemKindProject, "library*") + repositoryFilter := NewPatternFilter(replication.FilterItemKindRepository, + "library/ubuntu*", &fakeRepositoryConvertor{}) + filters := []Filter{projectFilter, repositoryFilter} + + items := []models.FilterItem{ + models.FilterItem{ + Kind: replication.FilterItemKindProject, + Value: "library", + }, + models.FilterItem{ + Kind: replication.FilterItemKindProject, + Value: "test", + }, + } + chain := NewDefaultFilterChain(filters) + items = chain.DoFilter(items) + assert.EqualValues(t, []models.FilterItem{ + models.FilterItem{ + Kind: replication.FilterItemKindRepository, + Value: "library/ubuntu", + }, + }, items) + +} + +type fakeRepositoryConvertor struct{} + +func (f *fakeRepositoryConvertor) Convert(items []models.FilterItem) []models.FilterItem { + result := []models.FilterItem{} + for _, item := range items { + result = append(result, models.FilterItem{ + Kind: replication.FilterItemKindRepository, + Value: item.Value + "/ubuntu", + }) + } + return result +} diff --git a/src/replication/source/filter.go b/src/replication/source/filter.go new file mode 100644 index 000000000..eb7b0623c --- /dev/null +++ b/src/replication/source/filter.go @@ -0,0 +1,18 @@ +package source + +import ( + "github.com/vmware/harbor/src/replication/models" +) + +//Filter define the operations of selecting the matched resources from the candidates +//according to the specified pattern. +type Filter interface { + //Initialize the filter + Init() error + + //Return the convertor if existing or nil if never set + GetConvertor() Convertor + + //Filter the items + DoFilter(filterItems []models.FilterItem) []models.FilterItem +} diff --git a/src/replication/source/filter_chain.go b/src/replication/source/filter_chain.go new file mode 100644 index 000000000..156ed6fc6 --- /dev/null +++ b/src/replication/source/filter_chain.go @@ -0,0 +1,21 @@ +package source + +import ( + "github.com/vmware/harbor/src/replication/models" +) + +//FilterChain is the interface to define the operations of coordinating multiple filters +//to work together as a whole pipeline. +//E.g: +//(original resources)---->[project filter]---->[repository filter]---->[tag filter]---->[......]---->(filter resources) +type FilterChain interface { + //Build the filter chain with the filters provided; + //if failed, an error will be returned. + Build(filter []Filter) error + + //Return all the filters in the chain. + Filters() []Filter + + //Filter the items and returned the filtered items via the appended filters in the chain. + DoFilter(filterItems []models.FilterItem) []models.FilterItem +} diff --git a/src/replication/source/match.go b/src/replication/source/match.go new file mode 100644 index 000000000..9e09dcbd2 --- /dev/null +++ b/src/replication/source/match.go @@ -0,0 +1,23 @@ +// Copyright (c) 2017 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 source + +import ( + "path/filepath" +) + +func match(pattern, str string) (bool, error) { + return filepath.Match(pattern, str) +} diff --git a/src/replication/source/match_test.go b/src/replication/source/match_test.go new file mode 100644 index 000000000..b4d581b74 --- /dev/null +++ b/src/replication/source/match_test.go @@ -0,0 +1,43 @@ +// Copyright (c) 2017 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 source + +import ( + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestMatch(t *testing.T) { + cases := []struct { + pattern string + str string + matched bool + }{ + {"", "", true}, + {"*", "library", true}, + {"library/*", "library/mysql", true}, + {"library/*", "library/mysql/5.6", false}, + {"library/mysq?", "library/mysql", true}, + {"library/mysq?", "library/mysqld", false}, + } + + for _, c := range cases { + matched, err := match(c.pattern, c.str) + require.Nil(t, err) + assert.Equal(t, c.matched, matched) + } +} diff --git a/src/replication/source/pattern_filter.go b/src/replication/source/pattern_filter.go new file mode 100644 index 000000000..6c895d0eb --- /dev/null +++ b/src/replication/source/pattern_filter.go @@ -0,0 +1,84 @@ +// Copyright (c) 2017 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 source + +import ( + "regexp" + + "github.com/vmware/harbor/src/common/utils/log" + "github.com/vmware/harbor/src/replication/models" +) + +// PatternFilter implements Filter interface for pattern filter +type PatternFilter struct { + kind string + pattern string + convertor Convertor +} + +// NewPatternFilter returns an instance of PatternFilter +func NewPatternFilter(kind, pattern string, convertor ...Convertor) *PatternFilter { + filer := &PatternFilter{ + kind: kind, + pattern: pattern, + } + + if len(convertor) > 0 { + filer.convertor = convertor[0] + } + + return filer +} + +// Init the filter. nil implement for now +func (p *PatternFilter) Init() error { + return nil +} + +// GetConvertor returns the convertor +func (p *PatternFilter) GetConvertor() Convertor { + return p.convertor +} + +// DoFilter filters resources +func (p *PatternFilter) DoFilter(filterItems []models.FilterItem) []models.FilterItem { + items := []models.FilterItem{} + for _, item := range filterItems { + if item.Kind != p.kind { + log.Warningf("unexpected filter item kind, expected: %s, got: %s, skip", + p.kind, item.Kind) + continue + } + + matched, err := regexp.MatchString(p.pattern, item.Value) + if err != nil { + log.Errorf("failed to match pattern %s, value %s: %v, skip", + p.pattern, item.Value, err) + continue + } + + if !matched { + log.Debugf("%s does not match to the %s filter %s, skip", + item.Value, p.kind, p.pattern) + continue + } + + log.Debugf("add %s to the result of %s filter %s", + item.Value, p.kind, p.pattern) + items = append(items, item) + } + + return items +} diff --git a/src/replication/source/pattern_filter_test.go b/src/replication/source/pattern_filter_test.go new file mode 100644 index 000000000..2f1ea372e --- /dev/null +++ b/src/replication/source/pattern_filter_test.go @@ -0,0 +1,63 @@ +// Copyright (c) 2017 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 source + +import ( + "github.com/stretchr/testify/assert" + "github.com/vmware/harbor/src/replication" + "github.com/vmware/harbor/src/replication/models" + + "testing" +) + +var pfilter = NewPatternFilter(replication.FilterItemKindTag, "library/ubuntu:release-*", nil) + +func TestPatternFilterInit(t *testing.T) { + assert.Nil(t, pfilter.Init()) +} + +func TestPatternFilterGetConvertor(t *testing.T) { + assert.Nil(t, pfilter.GetConvertor()) +} + +func TestPatternFilterDoFilter(t *testing.T) { + items := []models.FilterItem{ + models.FilterItem{ + Kind: replication.FilterItemKindProject, + }, + models.FilterItem{ + Kind: replication.FilterItemKindRepository, + }, + models.FilterItem{ + Kind: replication.FilterItemKindTag, + Value: "library/ubuntu:release-14.04", + }, + models.FilterItem{ + Kind: replication.FilterItemKindTag, + Value: "library/ubuntu:release-16.04", + }, + models.FilterItem{ + Kind: replication.FilterItemKindTag, + Value: "library/ubuntu:test", + }, + } + result := pfilter.DoFilter(items) + assert.Equal(t, 2, len(result)) + assert.Equal(t, replication.FilterItemKindTag, result[0].Kind) + assert.Equal(t, "library/ubuntu:release-14.04", result[0].Value) + assert.Equal(t, replication.FilterItemKindTag, result[1].Kind) + assert.Equal(t, "library/ubuntu:release-16.04", result[1].Value) + +} diff --git a/src/replication/source/repository_convertor.go b/src/replication/source/repository_convertor.go new file mode 100644 index 000000000..c3a8ea0b8 --- /dev/null +++ b/src/replication/source/repository_convertor.go @@ -0,0 +1,55 @@ +// Copyright (c) 2017 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 source + +import ( + "github.com/vmware/harbor/src/replication" + "github.com/vmware/harbor/src/replication/models" + "github.com/vmware/harbor/src/replication/registry" +) + +// RepositoryConvertor implement Convertor interface, convert projects to repositories +type RepositoryConvertor struct { + registry registry.Adaptor +} + +// NewRepositoryConvertor returns an instance of RepositoryConvertor +func NewRepositoryConvertor(registry registry.Adaptor) *RepositoryConvertor { + return &RepositoryConvertor{ + registry: registry, + } +} + +// Convert projects to repositories +func (r *RepositoryConvertor) Convert(items []models.FilterItem) []models.FilterItem { + result := []models.FilterItem{} + for _, item := range items { + // just put it to the result list if the item is not a project + if item.Kind != replication.FilterItemKindProject { + result = append(result, item) + continue + } + + repositories := r.registry.GetRepositories(item.Value) + for _, repository := range repositories { + result = append(result, models.FilterItem{ + Kind: replication.FilterItemKindRepository, + Value: repository.Name, + Operation: item.Operation, + }) + } + } + return result +} diff --git a/src/replication/source/repository_convertor_test.go b/src/replication/source/repository_convertor_test.go new file mode 100644 index 000000000..2ee1f5183 --- /dev/null +++ b/src/replication/source/repository_convertor_test.go @@ -0,0 +1,95 @@ +// Copyright (c) 2017 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 source + +import ( + "testing" + + "github.com/stretchr/testify/assert" + "github.com/vmware/harbor/src/replication" + "github.com/vmware/harbor/src/replication/models" +) + +func TestRepositoryConvert(t *testing.T) { + items := []models.FilterItem{ + models.FilterItem{ + Kind: replication.FilterItemKindProject, + Value: "library", + }, + models.FilterItem{ + Kind: replication.FilterItemKindRepository, + }, + } + expected := []models.FilterItem{ + models.FilterItem{ + Kind: replication.FilterItemKindRepository, + Value: "library/ubuntu", + }, + models.FilterItem{ + Kind: replication.FilterItemKindRepository, + Value: "library/centos", + }, + models.FilterItem{ + Kind: replication.FilterItemKindRepository, + }, + } + + convertor := NewRepositoryConvertor(&fakeRegistryAdaptor{}) + assert.EqualValues(t, expected, convertor.Convert(items)) +} + +type fakeRegistryAdaptor struct{} + +func (f *fakeRegistryAdaptor) Kind() string { + return "fake" +} + +func (f *fakeRegistryAdaptor) GetNamespaces() []models.Namespace { + return nil +} + +func (f *fakeRegistryAdaptor) GetNamespace(name string) models.Namespace { + return models.Namespace{} +} + +func (f *fakeRegistryAdaptor) GetRepositories(namespace string) []models.Repository { + return []models.Repository{ + models.Repository{ + Name: "library/ubuntu", + }, + models.Repository{ + Name: "library/centos", + }, + } +} + +func (f *fakeRegistryAdaptor) GetRepository(name string, namespace string) models.Repository { + return models.Repository{} +} + +func (f *fakeRegistryAdaptor) GetTags(repositoryName string, namespace string) []models.Tag { + return []models.Tag{ + models.Tag{ + Name: "14.04", + }, + models.Tag{ + Name: "16.04", + }, + } +} + +func (f *fakeRegistryAdaptor) GetTag(name string, repositoryName string, namespace string) models.Tag { + return models.Tag{} +} diff --git a/src/replication/source/repository_filter.go b/src/replication/source/repository_filter.go new file mode 100644 index 000000000..7c4dc33fe --- /dev/null +++ b/src/replication/source/repository_filter.go @@ -0,0 +1,89 @@ +// Copyright (c) 2017 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 source + +import ( + "strings" + + "github.com/vmware/harbor/src/common/utils" + "github.com/vmware/harbor/src/common/utils/log" + "github.com/vmware/harbor/src/replication" + "github.com/vmware/harbor/src/replication/models" + "github.com/vmware/harbor/src/replication/registry" +) + +// RepositoryFilter implement Filter interface to filter repository +type RepositoryFilter struct { + pattern string + convertor Convertor +} + +// NewRepositoryFilter returns an instance of RepositoryFilter +func NewRepositoryFilter(pattern string, registry registry.Adaptor) *RepositoryFilter { + return &RepositoryFilter{ + pattern: pattern, + convertor: NewRepositoryConvertor(registry), + } +} + +// Init ... +func (r *RepositoryFilter) Init() error { + return nil +} + +// GetConvertor ... +func (r *RepositoryFilter) GetConvertor() Convertor { + return r.convertor +} + +// DoFilter filters repository and image(according to the repository part) and drops any other resource types +func (r *RepositoryFilter) DoFilter(items []models.FilterItem) []models.FilterItem { + candidates := []string{} + for _, item := range items { + candidates = append(candidates, item.Value) + } + log.Debugf("repository filter candidates: %v", candidates) + + result := []models.FilterItem{} + for _, item := range items { + if item.Kind != replication.FilterItemKindRepository && item.Kind != replication.FilterItemKindTag { + log.Warningf("unsupported type %s for repository filter, drop", item.Kind) + continue + } + + repository := item.Value + if item.Kind == replication.FilterItemKindTag { + repository = strings.SplitN(repository, ":", 2)[0] + } + + if len(r.pattern) == 0 { + log.Debugf("pattern is null, add %s to the repository filter result list", item.Value) + result = append(result, item) + } else { + // trim the project + _, repository = utils.ParseRepository(repository) + matched, err := match(r.pattern, repository) + if err != nil { + log.Errorf("failed to match pattern %s to value %s: %v", r.pattern, repository, err) + break + } + if matched { + log.Debugf("pattern %s matched, add %s to the repository filter result list", r.pattern, item.Value) + result = append(result, item) + } + } + } + return result +} diff --git a/src/replication/source/repository_filter_test.go b/src/replication/source/repository_filter_test.go new file mode 100644 index 000000000..aab5d9462 --- /dev/null +++ b/src/replication/source/repository_filter_test.go @@ -0,0 +1,75 @@ +// Copyright (c) 2017 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 source + +import ( + "testing" + + "github.com/stretchr/testify/assert" + "github.com/vmware/harbor/src/replication" + "github.com/vmware/harbor/src/replication/models" + "github.com/vmware/harbor/src/replication/registry" +) + +func TestInitOfRepositoryFilter(t *testing.T) { + filter := NewRepositoryFilter("", ®istry.HarborAdaptor{}) + assert.Nil(t, filter.Init()) +} + +func TestGetConvertorOfRepositoryFilter(t *testing.T) { + filter := NewRepositoryFilter("", ®istry.HarborAdaptor{}) + assert.NotNil(t, filter.GetConvertor()) +} + +func TestDoFilterOfRepositoryFilter(t *testing.T) { + // invalid filter item type + filter := NewRepositoryFilter("", ®istry.HarborAdaptor{}) + items := filter.DoFilter([]models.FilterItem{ + models.FilterItem{ + Kind: "invalid_type", + }, + }) + assert.Equal(t, 0, len(items)) + + // empty pattern + filter = NewRepositoryFilter("", ®istry.HarborAdaptor{}) + items = filter.DoFilter([]models.FilterItem{ + models.FilterItem{ + Kind: replication.FilterItemKindRepository, + Value: "library/hello-world", + }, + }) + assert.Equal(t, 1, len(items)) + + // non-empty pattern + filter = NewRepositoryFilter("*", ®istry.HarborAdaptor{}) + items = filter.DoFilter([]models.FilterItem{ + models.FilterItem{ + Kind: replication.FilterItemKindTag, + Value: "library/hello-world", + }, + }) + assert.Equal(t, 1, len(items)) + + // non-empty pattern + filter = NewRepositoryFilter("*", ®istry.HarborAdaptor{}) + items = filter.DoFilter([]models.FilterItem{ + models.FilterItem{ + Kind: replication.FilterItemKindTag, + Value: "library/hello-world:latest", + }, + }) + assert.Equal(t, 1, len(items)) +} diff --git a/src/replication/source/sourcer.go b/src/replication/source/sourcer.go new file mode 100644 index 000000000..9322bb50e --- /dev/null +++ b/src/replication/source/sourcer.go @@ -0,0 +1,36 @@ +package source + +import ( + "github.com/vmware/harbor/src/replication" + "github.com/vmware/harbor/src/replication/registry" +) + +//Sourcer is used to manage and/or handle all the artifacts and information related with source registry. +//All the things with replication source should be covered in this object. +type Sourcer struct { + //Keep the adaptors we support now + adaptors map[string]registry.Adaptor +} + +//NewSourcer is the constructor of Sourcer +func NewSourcer() *Sourcer { + return &Sourcer{ + adaptors: make(map[string]registry.Adaptor), + } +} + +//Init will do some initialization work like registrying all the adaptors we support +func (sc *Sourcer) Init() { + //Register Harbor adaptor + sc.adaptors[replication.AdaptorKindHarbor] = ®istry.HarborAdaptor{} +} + +//GetAdaptor returns the required adaptor with the specified kind. +//If no adaptor with the specified kind existing, nil will be returned. +func (sc *Sourcer) GetAdaptor(kind string) registry.Adaptor { + if len(kind) == 0 { + return nil + } + + return sc.adaptors[kind] +} diff --git a/src/replication/source/sourcer_test.go b/src/replication/source/sourcer_test.go new file mode 100644 index 000000000..1cf9b0a90 --- /dev/null +++ b/src/replication/source/sourcer_test.go @@ -0,0 +1,24 @@ +package source + +import ( + "testing" + + "github.com/vmware/harbor/src/replication" +) + +func TestReplicationSourcer(t *testing.T) { + testingSourcer := NewSourcer() + if testingSourcer == nil { + t.Fatal("Failed to create sourcer") + } + + testingSourcer.Init() + + if testingSourcer.GetAdaptor("") != nil { + t.Fatal("Empty kind should not be supported") + } + + if testingSourcer.GetAdaptor(replication.AdaptorKindHarbor) == nil { + t.Fatalf("%s adaptor should be existing", replication.AdaptorKindHarbor) + } +} diff --git a/src/replication/source/tag_combination_filter.go b/src/replication/source/tag_combination_filter.go new file mode 100644 index 000000000..8bdddadd6 --- /dev/null +++ b/src/replication/source/tag_combination_filter.go @@ -0,0 +1,76 @@ +// Copyright (c) 2017 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 source + +import ( + "strings" + + "github.com/vmware/harbor/src/common/utils/log" + "github.com/vmware/harbor/src/replication" + "github.com/vmware/harbor/src/replication/models" +) + +// TagCombinationFilter implements Filter interface for merging tag filter items +// whose repository are same into one repository filter item +type TagCombinationFilter struct{} + +// NewTagCombinationFilter returns an instance of TagCombinationFilter +func NewTagCombinationFilter() *TagCombinationFilter { + return &TagCombinationFilter{} +} + +// Init the filter. nil implement for now +func (t *TagCombinationFilter) Init() error { + return nil +} + +// GetConvertor returns the convertor +func (t *TagCombinationFilter) GetConvertor() Convertor { + return nil +} + +// DoFilter filters resources +func (t *TagCombinationFilter) DoFilter(filterItems []models.FilterItem) []models.FilterItem { + repos := map[string][]string{} + for _, item := range filterItems { + if item.Kind != replication.FilterItemKindTag { + log.Warningf("unexpected filter item kind, expected: %s, got: %s, skip", + replication.FilterItemKindTag, item.Kind) + continue + } + + strs := strings.Split(item.Value, ":") + if len(strs) != 2 { + log.Warningf("unexpected image format: %s, skip", item.Value) + continue + } + + repos[strs[0]] = append(repos[strs[0]], strs[1]) + } + + // TODO append operation + items := []models.FilterItem{} + for repo, tags := range repos { + items = append(items, models.FilterItem{ + Kind: replication.FilterItemKindRepository, + Value: repo, + Metadata: map[string]interface{}{ + "tags": tags, + }, + }) + } + + return items +} diff --git a/src/replication/source/tag_combination_filter_test.go b/src/replication/source/tag_combination_filter_test.go new file mode 100644 index 000000000..8a84218b6 --- /dev/null +++ b/src/replication/source/tag_combination_filter_test.go @@ -0,0 +1,83 @@ +// Copyright (c) 2017 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 source + +import ( + "github.com/stretchr/testify/assert" + "github.com/vmware/harbor/src/replication" + "github.com/vmware/harbor/src/replication/models" + + "testing" +) + +var tcfilter = NewTagCombinationFilter() + +func TestTagCombinationFilterInit(t *testing.T) { + assert.Nil(t, tcfilter.Init()) +} + +func TestTagCombinationFilterGetConvertor(t *testing.T) { + assert.Nil(t, tcfilter.GetConvertor()) +} + +func TestTagCombinationFilterDoFilter(t *testing.T) { + items := []models.FilterItem{ + models.FilterItem{ + Kind: replication.FilterItemKindProject, + }, + models.FilterItem{ + Kind: replication.FilterItemKindRepository, + }, + models.FilterItem{ + Kind: replication.FilterItemKindTag, + Value: "library/ubuntu:invalid_tag:latest", + }, + models.FilterItem{ + Kind: replication.FilterItemKindTag, + Value: "library/ubuntu:14.04", + }, + models.FilterItem{ + Kind: replication.FilterItemKindTag, + Value: "library/ubuntu:16.04", + }, + models.FilterItem{ + Kind: replication.FilterItemKindTag, + Value: "library/centos:7", + }, + } + result := tcfilter.DoFilter(items) + assert.Equal(t, 2, len(result)) + + var ubuntu, centos models.FilterItem + if result[0].Value == "library/ubuntu" { + ubuntu = result[0] + centos = result[1] + } else { + centos = result[0] + ubuntu = result[1] + } + + assert.Equal(t, replication.FilterItemKindRepository, ubuntu.Kind) + assert.Equal(t, "library/ubuntu", ubuntu.Value) + metadata, ok := ubuntu.Metadata["tags"].([]string) + assert.True(t, ok) + assert.EqualValues(t, []string{"14.04", "16.04"}, metadata) + + assert.Equal(t, replication.FilterItemKindRepository, centos.Kind) + assert.Equal(t, "library/centos", centos.Value) + metadata, ok = centos.Metadata["tags"].([]string) + assert.True(t, ok) + assert.EqualValues(t, []string{"7"}, metadata) +} diff --git a/src/replication/source/tag_convertor.go b/src/replication/source/tag_convertor.go new file mode 100644 index 000000000..80e7f29f7 --- /dev/null +++ b/src/replication/source/tag_convertor.go @@ -0,0 +1,55 @@ +// Copyright (c) 2017 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 source + +import ( + "github.com/vmware/harbor/src/replication" + "github.com/vmware/harbor/src/replication/models" + "github.com/vmware/harbor/src/replication/registry" +) + +// TagConvertor implement Convertor interface, convert repositories to tags +type TagConvertor struct { + registry registry.Adaptor +} + +// NewTagConvertor returns an instance of TagConvertor +func NewTagConvertor(registry registry.Adaptor) *TagConvertor { + return &TagConvertor{ + registry: registry, + } +} + +//Convert repositories to tags +func (t *TagConvertor) Convert(items []models.FilterItem) []models.FilterItem { + result := []models.FilterItem{} + for _, item := range items { + if item.Kind != replication.FilterItemKindRepository { + // just put it to the result list if the item is not a repository + result = append(result, item) + continue + } + + tags := t.registry.GetTags(item.Value, "") + for _, tag := range tags { + result = append(result, models.FilterItem{ + Kind: replication.FilterItemKindTag, + Value: item.Value + ":" + tag.Name, + Operation: item.Operation, + }) + } + } + return result +} diff --git a/src/replication/source/tag_convertor_test.go b/src/replication/source/tag_convertor_test.go new file mode 100644 index 000000000..17c244c1d --- /dev/null +++ b/src/replication/source/tag_convertor_test.go @@ -0,0 +1,51 @@ +// Copyright (c) 2017 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 source + +import ( + "testing" + + "github.com/stretchr/testify/assert" + "github.com/vmware/harbor/src/replication" + "github.com/vmware/harbor/src/replication/models" +) + +func TestTagConvert(t *testing.T) { + items := []models.FilterItem{ + models.FilterItem{ + Kind: replication.FilterItemKindRepository, + Value: "library/ubuntu", + }, + models.FilterItem{ + Kind: replication.FilterItemKindProject, + }, + } + expected := []models.FilterItem{ + models.FilterItem{ + Kind: replication.FilterItemKindTag, + Value: "library/ubuntu:14.04", + }, + models.FilterItem{ + Kind: replication.FilterItemKindTag, + Value: "library/ubuntu:16.04", + }, + models.FilterItem{ + Kind: replication.FilterItemKindProject, + }, + } + + convertor := NewTagConvertor(&fakeRegistryAdaptor{}) + assert.EqualValues(t, expected, convertor.Convert(items)) +} diff --git a/src/replication/source/tag_filter.go b/src/replication/source/tag_filter.go new file mode 100644 index 000000000..f6fc5db92 --- /dev/null +++ b/src/replication/source/tag_filter.go @@ -0,0 +1,84 @@ +// Copyright (c) 2017 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 source + +import ( + "strings" + + "github.com/vmware/harbor/src/common/utils/log" + "github.com/vmware/harbor/src/replication" + "github.com/vmware/harbor/src/replication/models" + "github.com/vmware/harbor/src/replication/registry" +) + +// TagFilter implements Filter interface to filter tag +type TagFilter struct { + pattern string + convertor Convertor +} + +// NewTagFilter returns an instance of TagFilter +func NewTagFilter(pattern string, registry registry.Adaptor) *TagFilter { + return &TagFilter{ + pattern: pattern, + convertor: NewTagConvertor(registry), + } +} + +// Init ... +func (t *TagFilter) Init() error { + return nil +} + +// GetConvertor ... +func (t *TagFilter) GetConvertor() Convertor { + return t.convertor +} + +// DoFilter filters tag of the image +func (t *TagFilter) DoFilter(items []models.FilterItem) []models.FilterItem { + candidates := []string{} + for _, item := range items { + candidates = append(candidates, item.Value) + } + log.Debugf("tag filter candidates: %v", candidates) + + result := []models.FilterItem{} + for _, item := range items { + if item.Kind != replication.FilterItemKindTag { + log.Warningf("unsupported type %s for tag filter, dropped", item.Kind) + continue + } + + if len(t.pattern) == 0 { + log.Debugf("pattern is null, add %s to the tag filter result list", item.Value) + result = append(result, item) + continue + } + + tag := strings.SplitN(item.Value, ":", 2)[1] + matched, err := match(t.pattern, tag) + if err != nil { + log.Errorf("failed to match pattern %s to value %s: %v", t.pattern, tag, err) + continue + } + + if matched { + log.Debugf("pattern %s matched, add %s to the tag filter result list", t.pattern, item.Value) + result = append(result, item) + } + } + return result +} diff --git a/src/replication/source/tag_filter_test.go b/src/replication/source/tag_filter_test.go new file mode 100644 index 000000000..8114852ce --- /dev/null +++ b/src/replication/source/tag_filter_test.go @@ -0,0 +1,85 @@ +// Copyright (c) 2017 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 source + +import ( + "testing" + + "github.com/stretchr/testify/assert" + "github.com/vmware/harbor/src/replication" + "github.com/vmware/harbor/src/replication/models" + "github.com/vmware/harbor/src/replication/registry" +) + +func TestInitOfTagFilter(t *testing.T) { + filter := NewTagFilter("", ®istry.HarborAdaptor{}) + assert.Nil(t, filter.Init()) +} + +func TestGetConvertorOfTagFilter(t *testing.T) { + filter := NewTagFilter("", ®istry.HarborAdaptor{}) + assert.NotNil(t, filter.GetConvertor()) +} + +func TestDoFilterOfTagFilter(t *testing.T) { + // invalid filter item type + filter := NewTagFilter("", ®istry.HarborAdaptor{}) + items := filter.DoFilter([]models.FilterItem{ + models.FilterItem{ + Kind: "invalid_type", + }, + }) + assert.Equal(t, 0, len(items)) + + // empty pattern + filter = NewTagFilter("", ®istry.HarborAdaptor{}) + items = filter.DoFilter([]models.FilterItem{ + models.FilterItem{ + Kind: replication.FilterItemKindTag, + Value: "library/hello-world:latest", + }, + }) + assert.Equal(t, 1, len(items)) + + // non-empty pattern + filter = NewTagFilter("l*t", ®istry.HarborAdaptor{}) + items = filter.DoFilter([]models.FilterItem{ + models.FilterItem{ + Kind: replication.FilterItemKindTag, + Value: "library/hello-world:latest", + }, + }) + assert.Equal(t, 1, len(items)) + + // non-empty pattern + filter = NewTagFilter("lates?", ®istry.HarborAdaptor{}) + items = filter.DoFilter([]models.FilterItem{ + models.FilterItem{ + Kind: replication.FilterItemKindTag, + Value: "library/hello-world:latest", + }, + }) + assert.Equal(t, 1, len(items)) + + // non-empty pattern + filter = NewTagFilter("latest?", ®istry.HarborAdaptor{}) + items = filter.DoFilter([]models.FilterItem{ + models.FilterItem{ + Kind: replication.FilterItemKindTag, + Value: "library/hello-world:latest", + }, + }) + assert.Equal(t, 0, len(items)) +} diff --git a/src/replication/target/target.go b/src/replication/target/target.go new file mode 100644 index 000000000..ab8e815e5 --- /dev/null +++ b/src/replication/target/target.go @@ -0,0 +1,38 @@ +// Copyright (c) 2017 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 target + +import ( + "github.com/vmware/harbor/src/common/dao" + "github.com/vmware/harbor/src/common/models" +) + +// Manager defines the methods that a target manager should implement +type Manager interface { + GetTarget(int64) (*models.RepTarget, error) +} + +// DefaultManager implement the Manager interface +type DefaultManager struct{} + +// NewDefaultManager returns an instance of DefaultManger +func NewDefaultManager() *DefaultManager { + return &DefaultManager{} +} + +// GetTarget ... +func (d *DefaultManager) GetTarget(id int64) (*models.RepTarget, error) { + return dao.GetRepTarget(id) +} diff --git a/src/replication/target/target_test.go b/src/replication/target/target_test.go new file mode 100644 index 000000000..42a8f59e7 --- /dev/null +++ b/src/replication/target/target_test.go @@ -0,0 +1,26 @@ +// Copyright (c) 2017 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 target + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestNewDefaultManager(t *testing.T) { + mgr := NewDefaultManager() + assert.NotNil(t, mgr) +} diff --git a/src/replication/trigger/cache.go b/src/replication/trigger/cache.go new file mode 100644 index 000000000..ea694f3eb --- /dev/null +++ b/src/replication/trigger/cache.go @@ -0,0 +1,212 @@ +package trigger + +import ( + "container/heap" + "fmt" + "sync" + "time" +) + +const ( + //The max count of items the cache can keep + defaultCapacity = 1000 +) + +//Item keeps more metadata of the triggers which are stored in the heap. +type Item struct { + //Which policy the trigger belong to + policyID int64 + + //Frequency of cache querying + //First compration factor + frequency int + + //The timestamp of being put into heap + //Second compration factor + timestamp int64 + + //The index in the heap + index int +} + +//MetaQueue implements heap.Interface and holds items which are metadata of trigger +type MetaQueue []*Item + +//Len return the size of the queue +func (mq MetaQueue) Len() int { + return len(mq) +} + +//Less is a comparator of heap +func (mq MetaQueue) Less(i, j int) bool { + return mq[i].frequency < mq[j].frequency || + (mq[i].frequency == mq[j].frequency && + mq[i].timestamp < mq[j].timestamp) +} + +//Swap the items to rebuild heap +func (mq MetaQueue) Swap(i, j int) { + mq[i], mq[j] = mq[j], mq[i] + mq[i].index = i + mq[j].index = j +} + +//Push item into heap +func (mq *MetaQueue) Push(x interface{}) { + item := x.(*Item) + n := len(*mq) + item.index = n + item.timestamp = time.Now().UTC().UnixNano() + *mq = append(*mq, item) +} + +//Pop smallest item from heap +func (mq *MetaQueue) Pop() interface{} { + old := *mq + n := len(old) + item := old[n-1] //Smallest item + item.index = -1 //For safety + *mq = old[:n-1] + return item +} + +//Update the frequency of item +func (mq *MetaQueue) Update(item *Item) { + item.frequency++ + heap.Fix(mq, item.index) +} + +//CacheItem is the data stored in the cache. +//It contains trigger and heap item references. +type CacheItem struct { + //The trigger reference + trigger Interface + + //The heap item reference + item *Item +} + +//Cache is used to cache the enabled triggers with specified capacity. +//If exceed the capacity, cached items will be adjusted with the following rules: +// The item with least usage frequency will be replaced; +// If multiple items with same usage frequency, the oldest one will be replaced. +type Cache struct { + //The max count of items this cache can keep + capacity int + + //Lock to handle concurrent case + lock *sync.RWMutex + + //Hash map for quick locating cached item + hash map[string]CacheItem + + //Heap for quick locating the trigger with least usage + queue *MetaQueue +} + +//NewCache is constructor of cache +func NewCache(capacity int) *Cache { + cap := capacity + if cap <= 0 { + cap = defaultCapacity + } + + //Initialize heap + mq := make(MetaQueue, 0) + heap.Init(&mq) + + return &Cache{ + capacity: cap, + lock: new(sync.RWMutex), + hash: make(map[string]CacheItem), + queue: &mq, + } +} + +//Get the trigger interface with the specified policy ID +func (c *Cache) Get(policyID int64) Interface { + if policyID <= 0 { + return nil + } + + c.lock.RLock() + defer c.lock.RUnlock() + + k := c.key(policyID) + + if cacheItem, ok := c.hash[k]; ok { + //Update frequency + c.queue.Update(cacheItem.item) + return cacheItem.trigger + } + + return nil +} + +//Put the item into cache with ID of ploicy as key +func (c *Cache) Put(policyID int64, trigger Interface) { + if policyID <= 0 || trigger == nil { + return + } + + c.lock.Lock() + defer c.lock.Unlock() + + //Exceed the capacity? + if c.Size() >= c.capacity { + //Pop one for the new one + v := heap.Pop(c.queue) + item := v.(*Item) + //Remove from hash + delete(c.hash, c.key(item.policyID)) + } + + //Add to meta queue + item := &Item{ + policyID: policyID, + frequency: 1, + } + heap.Push(c.queue, item) + + //Cache + cacheItem := CacheItem{ + trigger: trigger, + item: item, + } + + k := c.key(policyID) + c.hash[k] = cacheItem +} + +//Remove the trigger attached to the specified policy +func (c *Cache) Remove(policyID int64) Interface { + if policyID > 0 { + c.lock.Lock() + defer c.lock.Unlock() + + //If existing + k := c.key(policyID) + if cacheItem, ok := c.hash[k]; ok { + //Remove from heap + heap.Remove(c.queue, cacheItem.item.index) + + //Remove from hash + delete(c.hash, k) + + return cacheItem.trigger + } + + } + + return nil +} + +//Size return the count of triggers in the cache +func (c *Cache) Size() int { + return len(c.hash) +} + +//Generate a hash key with the policy ID +func (c *Cache) key(policyID int64) string { + return fmt.Sprintf("trigger-%d", policyID) +} diff --git a/src/replication/trigger/cache_test.go b/src/replication/trigger/cache_test.go new file mode 100644 index 000000000..b7348cf26 --- /dev/null +++ b/src/replication/trigger/cache_test.go @@ -0,0 +1,53 @@ +package trigger + +import "testing" +import "time" + +func TestCache(t *testing.T) { + cache := NewCache(10) + trigger := NewImmediateTrigger(ImmediateParam{}) + + cache.Put(1, trigger) + if cache.Size() != 1 { + t.Fatalf("Invalid size, expect 1 but got %d", cache.Size()) + } + + tr := cache.Get(1) + if tr == nil { + t.Fatal("Should not get nil item") + } + + tri := cache.Remove(1) + if tri == nil || cache.Size() > 0 { + t.Fatal("Failed to remove") + } +} + +func TestCacheChange(t *testing.T) { + cache := NewCache(2) + trigger1 := NewImmediateTrigger(ImmediateParam{}) + trigger2 := NewImmediateTrigger(ImmediateParam{}) + cache.Put(1, trigger1) + cache.Put(2, trigger2) + + if cache.Size() != 2 { + t.Fatalf("Invalid size, expect 2 but got %d", cache.Size()) + } + + if tr := cache.Get(2); tr == nil { + t.Fatal("Should not get nil item") + } + + time.Sleep(100 * time.Microsecond) + + trigger3 := NewImmediateTrigger(ImmediateParam{}) + cache.Put(3, trigger3) + if cache.Size() != 2 { + t.Fatalf("Invalid size, expect 2 but got %d", cache.Size()) + } + + if tr := cache.Get(1); tr != nil { + t.Fatal("item1 should not exist") + } + +} diff --git a/src/replication/trigger/immediate.go b/src/replication/trigger/immediate.go new file mode 100644 index 000000000..f10753a95 --- /dev/null +++ b/src/replication/trigger/immediate.go @@ -0,0 +1,46 @@ +package trigger + +import ( + "github.com/vmware/harbor/src/replication" +) + +//ImmediateTrigger will setup watcher at the image pushing action to fire +//replication event at pushing happening time. +type ImmediateTrigger struct { + params ImmediateParam +} + +//NewImmediateTrigger is constructor of ImmediateTrigger +func NewImmediateTrigger(params ImmediateParam) *ImmediateTrigger { + return &ImmediateTrigger{ + params: params, + } +} + +//Kind is the implementation of same method defined in Trigger interface +func (st *ImmediateTrigger) Kind() string { + return replication.TriggerKindImmediate +} + +//Setup is the implementation of same method defined in Trigger interface +func (st *ImmediateTrigger) Setup() error { + //TODO: Need more complicated logic here to handle partial updates + for _, namespace := range st.params.Namespaces { + wt := WatchItem{ + PolicyID: st.params.PolicyID, + Namespace: namespace, + OnDeletion: st.params.OnDeletion, + OnPush: true, + } + + if err := DefaultWatchList.Add(wt); err != nil { + return err + } + } + return nil +} + +//Unset is the implementation of same method defined in Trigger interface +func (st *ImmediateTrigger) Unset() error { + return DefaultWatchList.Remove(st.params.PolicyID) +} diff --git a/src/replication/trigger/immediate_test.go b/src/replication/trigger/immediate_test.go new file mode 100644 index 000000000..a14dfe252 --- /dev/null +++ b/src/replication/trigger/immediate_test.go @@ -0,0 +1,57 @@ +// Copyright (c) 2017 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 trigger + +import ( + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "github.com/vmware/harbor/src/common/dao" + "github.com/vmware/harbor/src/common/utils/test" + "github.com/vmware/harbor/src/replication" +) + +func TestKindOfImmediateTrigger(t *testing.T) { + trigger := NewImmediateTrigger(ImmediateParam{}) + assert.Equal(t, replication.TriggerKindImmediate, trigger.Kind()) +} + +func TestSetupAndUnsetOfImmediateTrigger(t *testing.T) { + dao.DefaultDatabaseWatchItemDAO = &test.FakeWatchItemDAO{} + + param := ImmediateParam{} + param.PolicyID = 1 + param.OnDeletion = true + param.Namespaces = []string{"library"} + trigger := NewImmediateTrigger(param) + + err := trigger.Setup() + require.Nil(t, err) + + items, err := DefaultWatchList.Get("library", "push") + require.Nil(t, err) + assert.Equal(t, 1, len(items)) + + items, err = DefaultWatchList.Get("library", "delete") + require.Nil(t, err) + assert.Equal(t, 1, len(items)) + + err = trigger.Unset() + require.Nil(t, err) + items, err = DefaultWatchList.Get("library", "delete") + require.Nil(t, err) + assert.Equal(t, 0, len(items)) +} diff --git a/src/replication/trigger/interface.go b/src/replication/trigger/interface.go new file mode 100644 index 000000000..d08e75137 --- /dev/null +++ b/src/replication/trigger/interface.go @@ -0,0 +1,13 @@ +package trigger + +//Interface is certian mechanism to know when fire the replication operation. +type Interface interface { + //Kind indicates what type of the trigger is. + Kind() string + + //Setup/enable the trigger; if failed, an error would be returned. + Setup() error + + //Remove/disable the trigger; if failed, an error would be returned. + Unset() error +} diff --git a/src/replication/trigger/manager.go b/src/replication/trigger/manager.go new file mode 100644 index 000000000..10f6eaf83 --- /dev/null +++ b/src/replication/trigger/manager.go @@ -0,0 +1,124 @@ +package trigger + +import ( + "fmt" + + "github.com/vmware/harbor/src/common/utils/log" + "github.com/vmware/harbor/src/replication" + "github.com/vmware/harbor/src/replication/models" +) + +//Manager provides unified methods to manage the triggers of policies; +//Cache the enabled triggers, setup/unset the trigger based on the parameters +//with json format. +type Manager struct { + //Cache for triggers + //cache *Cache +} + +//NewManager is the constructor of trigger manager. +//capacity is the max number of trigger references manager can keep in memory +func NewManager(capacity int) *Manager { + return &Manager{ + //cache: NewCache(capacity), + } +} + +/* +//GetTrigger returns the enabled trigger reference if existing in the cache. +func (m *Manager) GetTrigger(policyID int64) Interface { + return m.cache.Get(policyID) +} + +//RemoveTrigger will disable the trigger and remove it from the cache if existing. +func (m *Manager) RemoveTrigger(policyID int64) error { + trigger := m.cache.Get(policyID) + if trigger == nil { + return errors.New("Trigger is not cached, please use UnsetTrigger to disable the trigger") + } + + //Unset trigger + if err := trigger.Unset(); err != nil { + return err + } + + //Remove from cache + //No need to check the return of remove because the dirty item cached in the cache + //will be removed out finally after a certain while + m.cache.Remove(policyID) + + return nil +} +*/ + +//SetupTrigger will create the new trigger based on the provided policy. +//If failed, an error will be returned. +func (m *Manager) SetupTrigger(policy *models.ReplicationPolicy) error { + trigger, err := createTrigger(policy) + if err != nil { + return err + } + + // manual trigger, do nothing + if trigger == nil { + return nil + } + + tg := trigger.(Interface) + if err = tg.Setup(); err != nil { + return err + } + + log.Debugf("%s trigger for policy %d is set", tg.Kind(), policy.ID) + return nil +} + +//UnsetTrigger will disable the trigger which is not cached in the trigger cache. +func (m *Manager) UnsetTrigger(policy *models.ReplicationPolicy) error { + trigger, err := createTrigger(policy) + if err != nil { + return err + } + + // manual trigger, do nothing + if trigger == nil { + return nil + } + + tg := trigger.(Interface) + if err = tg.Unset(); err != nil { + return err + } + + log.Debugf("%s trigger for policy %d is unset", tg.Kind(), policy.ID) + return nil +} + +func createTrigger(policy *models.ReplicationPolicy) (interface{}, error) { + if policy == nil || policy.Trigger == nil { + return nil, fmt.Errorf("empty policy or trigger") + } + + trigger := policy.Trigger + switch trigger.Kind { + case replication.TriggerKindSchedule: + param := ScheduleParam{} + param.PolicyID = policy.ID + param.Type = trigger.ScheduleParam.Type + param.Weekday = trigger.ScheduleParam.Weekday + param.Offtime = trigger.ScheduleParam.Offtime + + return NewScheduleTrigger(param), nil + case replication.TriggerKindImmediate: + param := ImmediateParam{} + param.PolicyID = policy.ID + param.OnDeletion = policy.ReplicateDeletion + param.Namespaces = policy.Namespaces + + return NewImmediateTrigger(param), nil + case replication.TriggerKindManual: + return nil, nil + default: + return nil, fmt.Errorf("invalid trigger type: %s", trigger.Kind) + } +} diff --git a/src/replication/trigger/manager_test.go b/src/replication/trigger/manager_test.go new file mode 100644 index 000000000..d02d9ee28 --- /dev/null +++ b/src/replication/trigger/manager_test.go @@ -0,0 +1,96 @@ +// Copyright (c) 2017 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 trigger + +import ( + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "github.com/vmware/harbor/src/replication" + "github.com/vmware/harbor/src/replication/models" +) + +func TestCreateTrigger(t *testing.T) { + // nil policy + _, err := createTrigger(nil) + require.NotNil(t, err) + + // nil trigger + _, err = createTrigger(&models.ReplicationPolicy{}) + require.NotNil(t, err) + + // schedule trigger + trigger, err := createTrigger(&models.ReplicationPolicy{ + Trigger: &models.Trigger{ + Kind: replication.TriggerKindSchedule, + ScheduleParam: &models.ScheduleParam{ + Type: replication.TriggerScheduleWeekly, + Weekday: 1, + Offtime: 1, + }, + }, + }) + require.Nil(t, err) + assert.NotNil(t, trigger) + + // immediate trigger + trigger, err = createTrigger(&models.ReplicationPolicy{ + Trigger: &models.Trigger{ + Kind: replication.TriggerKindImmediate, + }, + }) + require.Nil(t, err) + assert.NotNil(t, trigger) + + // manual trigger + trigger, err = createTrigger(&models.ReplicationPolicy{ + Trigger: &models.Trigger{ + Kind: replication.TriggerKindManual, + }, + }) + require.Nil(t, err) + assert.Nil(t, trigger) +} + +func TestSetupTrigger(t *testing.T) { + mgr := NewManager(1) + + err := mgr.SetupTrigger(&models.ReplicationPolicy{ + Trigger: &models.Trigger{ + Kind: replication.TriggerKindSchedule, + ScheduleParam: &models.ScheduleParam{ + Type: replication.TriggerScheduleDaily, + Offtime: 1, + }, + }, + }) + assert.Nil(t, err) +} + +func TestUnsetTrigger(t *testing.T) { + mgr := NewManager(1) + + err := mgr.UnsetTrigger(&models.ReplicationPolicy{ + Trigger: &models.Trigger{ + Kind: replication.TriggerKindSchedule, + ScheduleParam: &models.ScheduleParam{ + Type: replication.TriggerScheduleDaily, + Offtime: 1, + }, + }, + }) + assert.Nil(t, err) +} diff --git a/src/replication/trigger/param_immediate.go b/src/replication/trigger/param_immediate.go new file mode 100644 index 000000000..bb9b248a5 --- /dev/null +++ b/src/replication/trigger/param_immediate.go @@ -0,0 +1,22 @@ +package trigger + +//NOTES: Whether replicate the existing images when the type of trigger is +//'Immediate' is a once-effective setting which will not be persisted +// and kept as one parameter of 'Immediate' trigger. It will only be +//covered by the UI logic. + +//ImmediateParam defines the parameter of immediate trigger +type ImmediateParam struct { + //Basic parameters + BasicParam + + //Namepaces + Namespaces []string +} + +//Parse is the implementation of same method in TriggerParam interface +//NOTES: No need to implement this method for 'Immediate' trigger as +//it does not have any parameters with json format. +func (ip ImmediateParam) Parse(param string) error { + return nil +} diff --git a/src/replication/trigger/param_schedule.go b/src/replication/trigger/param_schedule.go new file mode 100644 index 000000000..84ca46f44 --- /dev/null +++ b/src/replication/trigger/param_schedule.go @@ -0,0 +1,30 @@ +package trigger + +import ( + "encoding/json" + "errors" +) + +//ScheduleParam defines the parameter of schedule trigger +type ScheduleParam struct { + //Basic parameters + BasicParam + + //Daily or weekly + Type string + + //Optional, only used when type is 'weekly' + Weekday int8 + + //The time offset with the UTC 00:00 in seconds + Offtime int64 +} + +//Parse is the implementation of same method in TriggerParam interface +func (stp ScheduleParam) Parse(param string) error { + if len(param) == 0 { + return errors.New("Parameter of schedule trigger should not be empty") + } + + return json.Unmarshal([]byte(param), &stp) +} diff --git a/src/replication/trigger/schedule.go b/src/replication/trigger/schedule.go new file mode 100644 index 000000000..5d5b24aad --- /dev/null +++ b/src/replication/trigger/schedule.go @@ -0,0 +1,58 @@ +package trigger + +import ( + "fmt" + "time" + + "github.com/vmware/harbor/src/common/scheduler" + "github.com/vmware/harbor/src/common/scheduler/policy" + replication_task "github.com/vmware/harbor/src/common/scheduler/task/replication" + "github.com/vmware/harbor/src/replication" +) + +//ScheduleTrigger will schedule a alternate policy to provide 'daily' and 'weekly' trigger ways. +type ScheduleTrigger struct { + params ScheduleParam +} + +//NewScheduleTrigger is constructor of ScheduleTrigger +func NewScheduleTrigger(params ScheduleParam) *ScheduleTrigger { + return &ScheduleTrigger{ + params: params, + } +} + +//Kind is the implementation of same method defined in Trigger interface +func (st *ScheduleTrigger) Kind() string { + return replication.TriggerKindSchedule +} + +//Setup is the implementation of same method defined in Trigger interface +func (st *ScheduleTrigger) Setup() error { + config := &policy.AlternatePolicyConfiguration{} + switch st.params.Type { + case replication.TriggerScheduleDaily: + config.Duration = 24 * 3600 * time.Second + config.OffsetTime = st.params.Offtime + case replication.TriggerScheduleWeekly: + config.Duration = 7 * 24 * 3600 * time.Second + config.OffsetTime = st.params.Offtime + config.Weekday = st.params.Weekday + default: + return fmt.Errorf("unsupported schedual trigger type: %s", st.params.Type) + } + + schedulePolicy := policy.NewAlternatePolicy(assembleName(st.params.PolicyID), config) + attachTask := replication_task.NewTask(st.params.PolicyID) + schedulePolicy.AttachTasks(attachTask) + return scheduler.DefaultScheduler.Schedule(schedulePolicy) +} + +//Unset is the implementation of same method defined in Trigger interface +func (st *ScheduleTrigger) Unset() error { + return scheduler.DefaultScheduler.UnSchedule(assembleName(st.params.PolicyID)) +} + +func assembleName(policyID int64) string { + return fmt.Sprintf("replication_policy_%d", policyID) +} diff --git a/src/replication/trigger/schedule_test.go b/src/replication/trigger/schedule_test.go new file mode 100644 index 000000000..5fecd934b --- /dev/null +++ b/src/replication/trigger/schedule_test.go @@ -0,0 +1,63 @@ +// Copyright (c) 2017 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 trigger + +import ( + "testing" + "time" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "github.com/vmware/harbor/src/common/scheduler" + "github.com/vmware/harbor/src/replication" +) + +func TestAssembleName(t *testing.T) { + assert.Equal(t, "replication_policy_1", assembleName(1)) +} + +func TestKindOfScheduleTrigger(t *testing.T) { + trigger := NewScheduleTrigger(ScheduleParam{}) + assert.Equal(t, replication.TriggerKindSchedule, trigger.Kind()) +} + +func TestSetupAndUnSetOfScheduleTrigger(t *testing.T) { + // invalid schedule param + trigger := NewScheduleTrigger(ScheduleParam{}) + assert.NotNil(t, trigger.Setup()) + + // valid schedule param + var policyID int64 = 1 + trigger = NewScheduleTrigger(ScheduleParam{ + BasicParam: BasicParam{ + PolicyID: policyID, + }, + Type: replication.TriggerScheduleWeekly, + Weekday: (int8(time.Now().Weekday()) + 1) % 7, + Offtime: 0, + }) + + count := scheduler.DefaultScheduler.PolicyCount() + require.Nil(t, scheduler.DefaultScheduler.GetPolicy(assembleName(policyID))) + + require.Nil(t, trigger.Setup()) + + assert.Equal(t, count+1, scheduler.DefaultScheduler.PolicyCount()) + assert.NotNil(t, scheduler.DefaultScheduler.GetPolicy(assembleName(policyID))) + + require.Nil(t, trigger.Unset()) + assert.Equal(t, count, scheduler.DefaultScheduler.PolicyCount()) + assert.Nil(t, scheduler.DefaultScheduler.GetPolicy(assembleName(policyID))) +} diff --git a/src/replication/trigger/trigger_param.go b/src/replication/trigger/trigger_param.go new file mode 100644 index 000000000..cccf3fca3 --- /dev/null +++ b/src/replication/trigger/trigger_param.go @@ -0,0 +1,17 @@ +package trigger + +//BasicParam contains the general parameters for all triggers +type BasicParam struct { + //ID of the related policy + PolicyID int64 + + //Whether delete remote replicated images if local ones are deleted + OnDeletion bool +} + +//Parameter defines operation of doing initialization from parameter json text +type Parameter interface { + //Decode parameter with json style to the owner struct + //If failed, an error will be returned + Parse(param string) error +} diff --git a/src/replication/trigger/watch_list.go b/src/replication/trigger/watch_list.go new file mode 100644 index 000000000..95f902179 --- /dev/null +++ b/src/replication/trigger/watch_list.go @@ -0,0 +1,65 @@ +package trigger + +import ( + "github.com/vmware/harbor/src/common/dao" + "github.com/vmware/harbor/src/common/models" +) + +//DefaultWatchList is the default instance of WatchList +var DefaultWatchList = &WatchList{} + +//WatchList contains the items which should be evaluated for replication +//when image pushing or deleting happens. +type WatchList struct{} + +//WatchItem keeps the related data for evaluation in WatchList. +type WatchItem struct { + //ID of policy + PolicyID int64 + + //Corresponding namespace + Namespace string + + //For deletion event + OnDeletion bool + + //For pushing event + OnPush bool +} + +//Add item to the list and persist into DB +func (wl *WatchList) Add(item WatchItem) error { + _, err := dao.DefaultDatabaseWatchItemDAO.Add( + &models.WatchItem{ + PolicyID: item.PolicyID, + Namespace: item.Namespace, + OnPush: item.OnPush, + OnDeletion: item.OnDeletion, + }) + return err +} + +//Remove the specified watch item from list +func (wl *WatchList) Remove(policyID int64) error { + return dao.DefaultDatabaseWatchItemDAO.DeleteByPolicyID(policyID) +} + +//Get the watch items according to the namespace and operation +func (wl *WatchList) Get(namespace, operation string) ([]WatchItem, error) { + items, err := dao.DefaultDatabaseWatchItemDAO.Get(namespace, operation) + if err != nil { + return nil, err + } + + watchItems := []WatchItem{} + for _, item := range items { + watchItems = append(watchItems, WatchItem{ + PolicyID: item.PolicyID, + Namespace: item.Namespace, + OnPush: item.OnPush, + OnDeletion: item.OnDeletion, + }) + } + + return watchItems, nil +} diff --git a/src/replication/trigger/watch_list_test.go b/src/replication/trigger/watch_list_test.go new file mode 100644 index 000000000..e8f1b9aed --- /dev/null +++ b/src/replication/trigger/watch_list_test.go @@ -0,0 +1,64 @@ +// Copyright (c) 2017 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 trigger + +import ( + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "github.com/vmware/harbor/src/common/dao" + "github.com/vmware/harbor/src/common/utils/test" +) + +func TestMethodsOfWatchList(t *testing.T) { + dao.DefaultDatabaseWatchItemDAO = &test.FakeWatchItemDAO{} + + var policyID int64 = 1 + + // test Add + item := WatchItem{ + PolicyID: policyID, + Namespace: "library", + OnDeletion: true, + OnPush: false, + } + + err := DefaultWatchList.Add(item) + require.Nil(t, err) + + // test Get: non-exist namespace + items, err := DefaultWatchList.Get("non-exist-namespace", "delete") + require.Nil(t, err) + assert.Equal(t, 0, len(items)) + + // test Get: non-exist operation + items, err = DefaultWatchList.Get("library", "non-exist-operation") + require.Nil(t, err) + assert.Equal(t, 0, len(items)) + + // test Get: valid params + items, err = DefaultWatchList.Get("library", "delete") + require.Nil(t, err) + assert.Equal(t, 1, len(items)) + assert.Equal(t, policyID, items[0].PolicyID) + + // test Remove + err = DefaultWatchList.Remove(policyID) + require.Nil(t, err) + items, err = DefaultWatchList.Get("library", "delete") + require.Nil(t, err) + assert.Equal(t, 0, len(items)) +} diff --git a/src/ui/api/api_test.go b/src/ui/api/api_test.go new file mode 100644 index 000000000..de9956954 --- /dev/null +++ b/src/ui/api/api_test.go @@ -0,0 +1,195 @@ +// Copyright (c) 2017 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 api + +import ( + "encoding/json" + "fmt" + "net/http" + "net/http/httptest" + "os" + "strconv" + "strings" + "testing" + + "github.com/astaxie/beego" + "github.com/dghubble/sling" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "github.com/vmware/harbor/src/common/dao" + "github.com/vmware/harbor/src/common/models" +) + +var ( + nonSysAdminID int64 + sysAdmin = &usrInfo{ + Name: "admin", + Passwd: "Harbor12345", + } + nonSysAdmin = &usrInfo{ + Name: "non_admin", + Passwd: "Harbor12345", + } +) + +type testingRequest struct { + method string + url string + header http.Header + queryStruct interface{} + bodyJSON interface{} + credential *usrInfo +} + +type codeCheckingCase struct { + request *testingRequest + code int + postFunc func(*httptest.ResponseRecorder) error +} + +func newRequest(r *testingRequest) (*http.Request, error) { + if r == nil { + return nil, nil + } + + reqBuilder := sling.New() + switch strings.ToUpper(r.method) { + case "", http.MethodGet: + reqBuilder = reqBuilder.Get(r.url) + case http.MethodPost: + reqBuilder = reqBuilder.Post(r.url) + case http.MethodPut: + reqBuilder = reqBuilder.Put(r.url) + case http.MethodDelete: + reqBuilder = reqBuilder.Delete(r.url) + case http.MethodHead: + reqBuilder = reqBuilder.Head(r.url) + case http.MethodPatch: + reqBuilder = reqBuilder.Patch(r.url) + default: + return nil, fmt.Errorf("unsupported method %s", r.method) + } + + for key, values := range r.header { + for _, value := range values { + reqBuilder = reqBuilder.Add(key, value) + } + } + + if r.queryStruct != nil { + reqBuilder = reqBuilder.QueryStruct(r.queryStruct) + } + + if r.bodyJSON != nil { + reqBuilder = reqBuilder.BodyJSON(r.bodyJSON) + } + + if r.credential != nil { + reqBuilder = reqBuilder.SetBasicAuth(r.credential.Name, r.credential.Passwd) + } + + return reqBuilder.Request() +} + +func handle(r *testingRequest) (*httptest.ResponseRecorder, error) { + req, err := newRequest(r) + if err != nil { + return nil, err + } + + resp := httptest.NewRecorder() + beego.BeeApp.Handlers.ServeHTTP(resp, req) + return resp, nil +} + +func handleAndParse(r *testingRequest, v interface{}) (*httptest.ResponseRecorder, error) { + req, err := newRequest(r) + if err != nil { + return nil, err + } + + resp := httptest.NewRecorder() + beego.BeeApp.Handlers.ServeHTTP(resp, req) + + if resp.Code >= 200 && resp.Code <= 299 { + if err := json.NewDecoder(resp.Body).Decode(v); err != nil { + return nil, err + } + } + + return resp, nil +} + +func runCodeCheckingCases(t *testing.T, cases ...*codeCheckingCase) { + for _, c := range cases { + resp, err := handle(c.request) + require.Nil(t, err) + equal := assert.Equal(t, c.code, resp.Code) + if !equal { + if resp.Body.Len() > 0 { + t.Log(resp.Body.String()) + } + continue + } + + if c.postFunc != nil { + if err := c.postFunc(resp); err != nil { + t.Logf("error in running post function: %v", err) + } + } + } +} + +func parseResourceID(resp *httptest.ResponseRecorder) (int64, error) { + location := resp.Header().Get(http.CanonicalHeaderKey("location")) + if len(location) == 0 { + return 0, fmt.Errorf("empty location header") + } + index := strings.LastIndex(location, "/") + if index == -1 { + return 0, fmt.Errorf("location header %s contains no /", location) + } + + id := strings.TrimPrefix(location, location[:index+1]) + if len(id) == 0 { + return 0, fmt.Errorf("location header %s contains no resource ID", location) + } + + return strconv.ParseInt(id, 10, 64) +} + +func TestMain(m *testing.M) { + if err := prepare(); err != nil { + panic(err) + } + defer clean() + + os.Exit(m.Run()) +} + +func prepare() error { + id, err := dao.Register(models.User{ + Username: nonSysAdmin.Name, + Password: nonSysAdmin.Passwd, + }) + if err != nil { + return err + } + nonSysAdminID = id + return nil +} + +func clean() error { + return dao.DeleteUser(int(nonSysAdminID)) +} diff --git a/src/ui/api/dataprepare_test.go b/src/ui/api/dataprepare_test.go index 67e41c9c9..711a74bed 100644 --- a/src/ui/api/dataprepare_test.go +++ b/src/ui/api/dataprepare_test.go @@ -118,10 +118,6 @@ func CommonDelTarget() { _ = dao.DeleteRepTarget(target.ID) } -func CommonPolicyEabled(policyID int, enabled int) { - _ = dao.UpdateRepPolicyEnablement(int64(policyID), enabled) -} - func CommonAddRepository() { commonRepository := &models.RepoRecord{ RepositoryID: 1, diff --git a/src/ui/api/harborapi_test.go b/src/ui/api/harborapi_test.go index 86f4784cb..127824a81 100644 --- a/src/ui/api/harborapi_test.go +++ b/src/ui/api/harborapi_test.go @@ -40,6 +40,8 @@ import ( "github.com/dghubble/sling" //for test env prepare + "github.com/vmware/harbor/src/replication/core" + _ "github.com/vmware/harbor/src/replication/event" _ "github.com/vmware/harbor/src/ui/auth/db" _ "github.com/vmware/harbor/src/ui/auth/ldap" ) @@ -121,7 +123,6 @@ func init() { beego.Router("/api/policies/replication/:id([0-9]+)", &RepPolicyAPI{}) beego.Router("/api/policies/replication", &RepPolicyAPI{}, "get:List") beego.Router("/api/policies/replication", &RepPolicyAPI{}, "post:Post;delete:Delete") - beego.Router("/api/policies/replication/:id([0-9]+)/enablement", &RepPolicyAPI{}, "put:UpdateEnablement") beego.Router("/api/systeminfo", &SystemInfoAPI{}, "get:GetGeneralInfo") beego.Router("/api/systeminfo/volumes", &SystemInfoAPI{}, "get:GetVolumeInfo") beego.Router("/api/systeminfo/getcert", &SystemInfoAPI{}, "get:GetCert") @@ -129,9 +130,14 @@ func init() { beego.Router("/api/configurations", &ConfigAPI{}) beego.Router("/api/configurations/reset", &ConfigAPI{}, "post:Reset") beego.Router("/api/email/ping", &EmailAPI{}, "post:Ping") + beego.Router("/api/replications", &ReplicationAPI{}) _ = updateInitPassword(1, "Harbor12345") + if err := core.Init(); err != nil { + log.Fatalf("failed to initialize GlobalController: %v", err) + } + //syncRegistry if err := SyncRegistry(config.GlobalProjectMgr); err != nil { log.Fatalf("failed to sync repositories from registry: %v", err) @@ -698,7 +704,10 @@ func (a testapi) AddPolicy(authInfo usrInfo, repPolicy apilib.RepPolicyPost) (in _sling = _sling.Path(path) _sling = _sling.BodyJSON(repPolicy) - httpStatusCode, _, err := request(_sling, jsonAcceptHeader, authInfo) + httpStatusCode, body, err := request(_sling, jsonAcceptHeader, authInfo) + if httpStatusCode != http.StatusCreated { + log.Println(string(body)) + } return httpStatusCode, err } diff --git a/src/ui/api/models/replication.go b/src/ui/api/models/replication.go new file mode 100644 index 000000000..c19584ea2 --- /dev/null +++ b/src/ui/api/models/replication.go @@ -0,0 +1,31 @@ +// Copyright (c) 2017 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 models + +import ( + "github.com/astaxie/beego/validation" +) + +// Replication defines the properties of model used in replication API +type Replication struct { + PolicyID int64 `json:"policy_id"` +} + +// Valid ... +func (r *Replication) Valid(v *validation.Validation) { + if r.PolicyID <= 0 { + v.SetError("policy_id", "invalid value") + } +} diff --git a/src/ui/api/models/replication_job.go b/src/ui/api/models/replication_job.go new file mode 100644 index 000000000..f3dfc2402 --- /dev/null +++ b/src/ui/api/models/replication_job.go @@ -0,0 +1,35 @@ +// Copyright (c) 2017 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 models + +import ( + "github.com/astaxie/beego/validation" +) + +// StopJobsReq holds information needed to stop the jobs for a replication rule +type StopJobsReq struct { + PolicyID int64 `json:"policy_id"` + Status string `json:"status"` +} + +// Valid ... +func (s *StopJobsReq) Valid(v *validation.Validation) { + if s.PolicyID <= 0 { + v.SetError("policy_id", "invalid value") + } + if s.Status != "stop" { + v.SetError("status", "invalid status, valid values: [stop]") + } +} diff --git a/src/ui/api/models/replication_policy.go b/src/ui/api/models/replication_policy.go new file mode 100644 index 000000000..c8da8a03a --- /dev/null +++ b/src/ui/api/models/replication_policy.go @@ -0,0 +1,66 @@ +// Copyright (c) 2017 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 models + +import ( + "time" + + "github.com/astaxie/beego/validation" + common_models "github.com/vmware/harbor/src/common/models" + rep_models "github.com/vmware/harbor/src/replication/models" +) + +// ReplicationPolicy defines the data model used in API level +type ReplicationPolicy struct { + ID int64 `json:"id"` + Name string `json:"name"` + Description string `json:"description"` + Filters []rep_models.Filter `json:"filters"` + ReplicateDeletion bool `json:"replicate_deletion"` + Trigger *rep_models.Trigger `json:"trigger"` + Projects []*common_models.Project `json:"projects"` + Targets []*common_models.RepTarget `json:"targets"` + CreationTime time.Time `json:"creation_time"` + UpdateTime time.Time `json:"update_time"` + ReplicateExistingImageNow bool `json:"replicate_existing_image_now"` + ErrorJobCount int64 `json:"error_job_count"` +} + +// Valid ... +func (r *ReplicationPolicy) Valid(v *validation.Validation) { + if len(r.Name) == 0 { + v.SetError("name", "can not be empty") + } + + if len(r.Name) > 256 { + v.SetError("name", "max length is 256") + } + + if len(r.Projects) == 0 { + v.SetError("projects", "can not be empty") + } + + if len(r.Targets) == 0 { + v.SetError("targets", "can not be empty") + } + + for _, filter := range r.Filters { + filter.Valid(v) + } + + if r.Trigger != nil { + r.Trigger.Valid(v) + } +} diff --git a/src/ui/api/replication.go b/src/ui/api/replication.go new file mode 100644 index 000000000..ffe9165d7 --- /dev/null +++ b/src/ui/api/replication.go @@ -0,0 +1,75 @@ +// Copyright (c) 2017 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 api + +import ( + "fmt" + + "github.com/vmware/harbor/src/common/notifier" + "github.com/vmware/harbor/src/common/utils/log" + "github.com/vmware/harbor/src/replication/core" + "github.com/vmware/harbor/src/replication/event/notification" + "github.com/vmware/harbor/src/replication/event/topic" + "github.com/vmware/harbor/src/ui/api/models" +) + +// ReplicationAPI handles API calls for replication +type ReplicationAPI struct { + BaseController +} + +// Prepare does authentication and authorization works +func (r *ReplicationAPI) Prepare() { + r.BaseController.Prepare() + if !r.SecurityCtx.IsAuthenticated() { + r.HandleUnauthorized() + return + } + + if !r.SecurityCtx.IsSysAdmin() { + r.HandleForbidden(r.SecurityCtx.GetUsername()) + return + } +} + +// Post trigger a replication according to the specified policy +func (r *ReplicationAPI) Post() { + replication := &models.Replication{} + r.DecodeJSONReqAndValidate(replication) + + policy, err := core.GlobalController.GetPolicy(replication.PolicyID) + if err != nil { + r.HandleInternalServerError(fmt.Sprintf("failed to get replication policy %d: %v", replication.PolicyID, err)) + return + } + + if policy.ID == 0 { + r.HandleNotFound(fmt.Sprintf("replication policy %d not found", replication.PolicyID)) + return + } + + if err = startReplication(replication.PolicyID); err != nil { + r.HandleInternalServerError(fmt.Sprintf("failed to publish replication topic for policy %d: %v", replication.PolicyID, err)) + return + } + log.Infof("replication signal for policy %d sent", replication.PolicyID) +} + +func startReplication(policyID int64) error { + return notifier.Publish(topic.StartReplicationTopic, + notification.StartReplicationNotification{ + PolicyID: policyID, + }) +} diff --git a/src/ui/api/replication_job.go b/src/ui/api/replication_job.go index e032eb0d2..670ebacda 100644 --- a/src/ui/api/replication_job.go +++ b/src/ui/api/replication_job.go @@ -23,6 +23,9 @@ import ( "github.com/vmware/harbor/src/common/dao" "github.com/vmware/harbor/src/common/models" "github.com/vmware/harbor/src/common/utils/log" + "github.com/vmware/harbor/src/replication/core" + api_models "github.com/vmware/harbor/src/ui/api/models" + "github.com/vmware/harbor/src/ui/config" "github.com/vmware/harbor/src/ui/utils" ) @@ -40,7 +43,7 @@ func (ra *RepJobAPI) Prepare() { return } - if !ra.SecurityCtx.IsSysAdmin() { + if !(ra.Ctx.Request.Method == http.MethodGet || ra.SecurityCtx.IsSysAdmin()) { ra.HandleForbidden(ra.SecurityCtx.GetUsername()) return } @@ -63,16 +66,21 @@ func (ra *RepJobAPI) List() { ra.CustomAbort(http.StatusBadRequest, "invalid policy_id") } - policy, err := dao.GetRepPolicy(policyID) + policy, err := core.GlobalController.GetPolicy(policyID) if err != nil { log.Errorf("failed to get policy %d: %v", policyID, err) ra.CustomAbort(http.StatusInternalServerError, "") } - if policy == nil { + if policy.ID == 0 { ra.CustomAbort(http.StatusNotFound, fmt.Sprintf("policy %d not found", policyID)) } + if !ra.SecurityCtx.HasAllPerm(policy.ProjectIDs[0]) { + ra.HandleForbidden(ra.SecurityCtx.GetUsername()) + return + } + repository := ra.GetString("repository") status := ra.GetString("status") @@ -145,12 +153,56 @@ func (ra *RepJobAPI) GetLog() { if ra.jobID == 0 { ra.CustomAbort(http.StatusBadRequest, "id is nil") } + + job, err := dao.GetRepJob(ra.jobID) + if err != nil { + ra.HandleInternalServerError(fmt.Sprintf("failed to get replication job %d: %v", ra.jobID, err)) + return + } + + if job == nil { + ra.HandleNotFound(fmt.Sprintf("replication job %d not found", ra.jobID)) + return + } + + policy, err := core.GlobalController.GetPolicy(job.PolicyID) + if err != nil { + ra.HandleInternalServerError(fmt.Sprintf("failed to get policy %d: %v", job.PolicyID, err)) + return + } + + if !ra.SecurityCtx.HasAllPerm(policy.ProjectIDs[0]) { + ra.HandleForbidden(ra.SecurityCtx.GetUsername()) + return + } + url := buildJobLogURL(strconv.FormatInt(ra.jobID, 10), ReplicationJobType) - err := utils.RequestAsUI(http.MethodGet, url, nil, utils.NewJobLogRespHandler(&ra.BaseAPI)) + err = utils.RequestAsUI(http.MethodGet, url, nil, utils.NewJobLogRespHandler(&ra.BaseAPI)) if err != nil { ra.RenderError(http.StatusInternalServerError, err.Error()) return } } +// StopJobs stop replication jobs for the policy +func (ra *RepJobAPI) StopJobs() { + req := &api_models.StopJobsReq{} + ra.DecodeJSONReqAndValidate(req) + + policy, err := core.GlobalController.GetPolicy(req.PolicyID) + if err != nil { + ra.HandleInternalServerError(fmt.Sprintf("failed to get policy %d: %v", req.PolicyID, err)) + return + } + + if policy.ID == 0 { + ra.CustomAbort(http.StatusNotFound, fmt.Sprintf("policy %d not found", req.PolicyID)) + } + + if err = config.GlobalJobserviceClient.StopReplicationJobs(req.PolicyID); err != nil { + ra.HandleInternalServerError(fmt.Sprintf("failed to stop replication jobs of policy %d: %v", req.PolicyID, err)) + return + } +} + //TODO:add Post handler to call job service API to submit jobs by policy diff --git a/src/ui/api/replication_policy.go b/src/ui/api/replication_policy.go index ebde92340..647fe6e58 100644 --- a/src/ui/api/replication_policy.go +++ b/src/ui/api/replication_policy.go @@ -23,6 +23,10 @@ import ( "github.com/vmware/harbor/src/common/dao" "github.com/vmware/harbor/src/common/models" "github.com/vmware/harbor/src/common/utils/log" + "github.com/vmware/harbor/src/replication/core" + rep_models "github.com/vmware/harbor/src/replication/models" + api_models "github.com/vmware/harbor/src/ui/api/models" + "github.com/vmware/harbor/src/ui/promgr" ) // RepPolicyAPI handles /api/replicationPolicies /api/replicationPolicies/:id/enablement @@ -38,7 +42,7 @@ func (pa *RepPolicyAPI) Prepare() { return } - if !pa.SecurityCtx.IsSysAdmin() { + if !(pa.Ctx.Request.Method == http.MethodGet || pa.SecurityCtx.IsSysAdmin()) { pa.HandleForbidden(pa.SecurityCtx.GetUsername()) return } @@ -47,344 +51,199 @@ func (pa *RepPolicyAPI) Prepare() { // Get ... func (pa *RepPolicyAPI) Get() { id := pa.GetIDFromURL() - policy, err := dao.GetRepPolicy(id) + policy, err := core.GlobalController.GetPolicy(id) if err != nil { log.Errorf("failed to get policy %d: %v", id, err) pa.CustomAbort(http.StatusInternalServerError, http.StatusText(http.StatusInternalServerError)) } - if policy == nil { + if policy.ID == 0 { pa.CustomAbort(http.StatusNotFound, http.StatusText(http.StatusNotFound)) } - pa.Data["json"] = policy + if !pa.SecurityCtx.HasAllPerm(policy.ProjectIDs[0]) { + pa.HandleForbidden(pa.SecurityCtx.GetUsername()) + return + } + + ply, err := convertFromRepPolicy(pa.ProjectMgr, policy) + if err != nil { + pa.ParseAndHandleError(fmt.Sprintf("failed to convert from replication policy"), err) + return + } + + pa.Data["json"] = ply pa.ServeJSON() } -// List filters policies by name and project_id, if name and project_id -// are nil, List returns all policies +// List ... func (pa *RepPolicyAPI) List() { - name := pa.GetString("name") + queryParam := rep_models.QueryParameter{ + Name: pa.GetString("name"), + } projectIDStr := pa.GetString("project_id") - - var projectID int64 - var err error - - if len(projectIDStr) != 0 { - projectID, err = strconv.ParseInt(projectIDStr, 10, 64) + if len(projectIDStr) > 0 { + projectID, err := strconv.ParseInt(projectIDStr, 10, 64) if err != nil || projectID <= 0 { pa.CustomAbort(http.StatusBadRequest, "invalid project ID") } + queryParam.ProjectID = projectID } - policies, err := dao.FilterRepPolicies(name, projectID) + result := []*api_models.ReplicationPolicy{} + + policies, err := core.GlobalController.GetPolicies(queryParam) if err != nil { - log.Errorf("failed to filter policies %s project ID %d: %v", name, projectID, err) + log.Errorf("failed to get policies: %v, query parameters: %v", err, queryParam) pa.CustomAbort(http.StatusInternalServerError, http.StatusText(http.StatusInternalServerError)) } for _, policy := range policies { - project, err := pa.ProjectMgr.Get(policy.ProjectID) + if !pa.SecurityCtx.HasAllPerm(policy.ProjectIDs[0]) { + continue + } + ply, err := convertFromRepPolicy(pa.ProjectMgr, policy) if err != nil { - pa.ParseAndHandleError(fmt.Sprintf( - "failed to get project %d", policy.ProjectID), err) + pa.ParseAndHandleError(fmt.Sprintf("failed to convert from replication policy"), err) return } - if project != nil { - policy.ProjectName = project.Name - } + result = append(result, ply) } - pa.Data["json"] = policies + pa.Data["json"] = result pa.ServeJSON() } -// Post creates a policy, and if it is enbled, the replication will be triggered right now. +// Post creates a replicartion policy func (pa *RepPolicyAPI) Post() { - policy := &models.RepPolicy{} + policy := &api_models.ReplicationPolicy{} pa.DecodeJSONReqAndValidate(policy) - /* - po, err := dao.GetRepPolicyByName(policy.Name) + // check the existence of projects + for _, project := range policy.Projects { + pro, err := pa.ProjectMgr.Get(project.ProjectID) if err != nil { - log.Errorf("failed to get policy %s: %v", policy.Name, err) - pa.CustomAbort(http.StatusInternalServerError, http.StatusText(http.StatusInternalServerError)) + pa.ParseAndHandleError(fmt.Sprintf("failed to check the existence of project %d", project.ProjectID), err) + return + } + if pro == nil { + pa.HandleNotFound(fmt.Sprintf("project %d not found", project.ProjectID)) + return + } + project.Name = pro.Name + } + + // check the existence of targets + for _, target := range policy.Targets { + t, err := dao.GetRepTarget(target.ID) + if err != nil { + pa.HandleInternalServerError(fmt.Sprintf("failed to get target %d: %v", target.ID, err)) + return } - if po != nil { - pa.CustomAbort(http.StatusConflict, "name is already used") + if t == nil { + pa.HandleNotFound(fmt.Sprintf("target %d not found", target.ID)) + return } - */ + } - project, err := pa.ProjectMgr.Get(policy.ProjectID) + id, err := core.GlobalController.CreatePolicy(convertToRepPolicy(policy)) if err != nil { - pa.ParseAndHandleError(fmt.Sprintf("failed to get project %d", policy.ProjectID), err) + pa.HandleInternalServerError(fmt.Sprintf("failed to create policy: %v", err)) return } - if project == nil { - pa.CustomAbort(http.StatusBadRequest, fmt.Sprintf("project %d does not exist", policy.ProjectID)) - } - - target, err := dao.GetRepTarget(policy.TargetID) - if err != nil { - log.Errorf("failed to get target %d: %v", policy.TargetID, err) - pa.CustomAbort(http.StatusInternalServerError, http.StatusText(http.StatusInternalServerError)) - } - - if target == nil { - pa.CustomAbort(http.StatusBadRequest, fmt.Sprintf("target %d does not exist", policy.TargetID)) - } - - policies, err := dao.GetRepPolicyByProjectAndTarget(policy.ProjectID, policy.TargetID) - if err != nil { - log.Errorf("failed to get policy [project ID: %d,targetID: %d]: %v", policy.ProjectID, policy.TargetID, err) - pa.CustomAbort(http.StatusInternalServerError, http.StatusText(http.StatusInternalServerError)) - } - - if len(policies) > 0 { - pa.CustomAbort(http.StatusConflict, "policy already exists with the same project and target") - } - - pid, err := dao.AddRepPolicy(*policy) - if err != nil { - log.Errorf("Failed to add policy to DB, error: %v", err) - pa.RenderError(http.StatusInternalServerError, "Internal Error") - return - } - - if policy.Enabled == 1 { + if policy.ReplicateExistingImageNow { go func() { - if err := TriggerReplication(pid, "", nil, models.RepOpTransfer); err != nil { - log.Errorf("failed to trigger replication of %d: %v", pid, err) - } else { - log.Infof("replication of %d triggered", pid) + if err = startReplication(id); err != nil { + log.Errorf("failed to send replication signal for policy %d: %v", id, err) + return } + log.Infof("replication signal for policy %d sent", id) }() } - pa.Redirect(http.StatusCreated, strconv.FormatInt(pid, 10)) + pa.Redirect(http.StatusCreated, strconv.FormatInt(id, 10)) } -// Put modifies name, description, target and enablement of policy +// Put updates the replication policy func (pa *RepPolicyAPI) Put() { id := pa.GetIDFromURL() - originalPolicy, err := dao.GetRepPolicy(id) + + originalPolicy, err := core.GlobalController.GetPolicy(id) if err != nil { log.Errorf("failed to get policy %d: %v", id, err) pa.CustomAbort(http.StatusInternalServerError, http.StatusText(http.StatusInternalServerError)) } - if originalPolicy == nil { + if originalPolicy.ID == 0 { pa.CustomAbort(http.StatusNotFound, http.StatusText(http.StatusNotFound)) } - policy := &models.RepPolicy{} - pa.DecodeJSONReq(policy) - policy.ProjectID = originalPolicy.ProjectID - pa.Validate(policy) - - /* - // check duplicate name - if policy.Name != originalPolicy.Name { - po, err := dao.GetRepPolicyByName(policy.Name) - if err != nil { - log.Errorf("failed to get policy %s: %v", policy.Name, err) - pa.CustomAbort(http.StatusInternalServerError, http.StatusText(http.StatusInternalServerError)) - } - - if po != nil { - pa.CustomAbort(http.StatusConflict, "name is already used") - } - } - */ - - if policy.TargetID != originalPolicy.TargetID { - //target of policy can not be modified when the policy is enabled - if originalPolicy.Enabled == 1 { - pa.CustomAbort(http.StatusBadRequest, "target of policy can not be modified when the policy is enabled") - } - - // check the existance of target - target, err := dao.GetRepTarget(policy.TargetID) - if err != nil { - log.Errorf("failed to get target %d: %v", policy.TargetID, err) - pa.CustomAbort(http.StatusInternalServerError, http.StatusText(http.StatusInternalServerError)) - } - - if target == nil { - pa.CustomAbort(http.StatusBadRequest, fmt.Sprintf("target %d does not exist", policy.TargetID)) - } - - // check duplicate policy with the same project and target - policies, err := dao.GetRepPolicyByProjectAndTarget(policy.ProjectID, policy.TargetID) - if err != nil { - log.Errorf("failed to get policy [project ID: %d,targetID: %d]: %v", policy.ProjectID, policy.TargetID, err) - pa.CustomAbort(http.StatusInternalServerError, http.StatusText(http.StatusInternalServerError)) - } - - if len(policies) > 0 { - pa.CustomAbort(http.StatusConflict, "policy already exists with the same project and target") - } - } + policy := &api_models.ReplicationPolicy{} + pa.DecodeJSONReqAndValidate(policy) policy.ID = id - /* - isTargetChanged := !(policy.TargetID == originalPolicy.TargetID) - isEnablementChanged := !(policy.Enabled == policy.Enabled) - - var shouldStop, shouldTrigger bool - - // if target and enablement are not changed, do nothing - if !isTargetChanged && !isEnablementChanged { - shouldStop = false - shouldTrigger = false - } else if !isTargetChanged && isEnablementChanged { - // target is not changed, but enablement is changed - if policy.Enabled == 0 { - shouldStop = true - shouldTrigger = false - } else { - shouldStop = false - shouldTrigger = true - } - } else if isTargetChanged && !isEnablementChanged { - // target is changed, but enablement is not changed - if policy.Enabled == 0 { - // enablement is 0, do nothing - shouldStop = false - shouldTrigger = false - } else { - // enablement is 1, so stop original target's jobs - // and trigger new target's jobs - shouldStop = true - shouldTrigger = true - } - } else { - // both target and enablement are changed - - // enablement: 1 -> 0 - if policy.Enabled == 0 { - shouldStop = true - shouldTrigger = false - } else { - shouldStop = false - shouldTrigger = true - } + // check the existence of projects + for _, project := range policy.Projects { + pro, err := pa.ProjectMgr.Get(project.ProjectID) + if err != nil { + pa.ParseAndHandleError(fmt.Sprintf("failed to check the existence of project %d", project.ProjectID), err) + return } - - if shouldStop { - if err := postReplicationAction(id, "stop"); err != nil { - log.Errorf("failed to stop replication of %d: %v", id, err) - pa.CustomAbort(http.StatusInternalServerError, http.StatusText(http.StatusInternalServerError)) - } - log.Infof("replication of %d has been stopped", id) + if pro == nil { + pa.HandleNotFound(fmt.Sprintf("project %d not found", project.ProjectID)) + return } - - if err = dao.UpdateRepPolicy(policy); err != nil { - log.Errorf("failed to update policy %d: %v", id, err) - pa.CustomAbort(http.StatusInternalServerError, http.StatusText(http.StatusInternalServerError)) - } - - if shouldTrigger { - go func() { - if err := TriggerReplication(id, "", nil, models.RepOpTransfer); err != nil { - log.Errorf("failed to trigger replication of %d: %v", id, err) - } else { - log.Infof("replication of %d triggered", id) - } - }() - } - */ - - if err = dao.UpdateRepPolicy(policy); err != nil { - log.Errorf("failed to update policy %d: %v", id, err) - pa.CustomAbort(http.StatusInternalServerError, http.StatusText(http.StatusInternalServerError)) + project.Name = pro.Name } - if policy.Enabled != originalPolicy.Enabled && policy.Enabled == 1 { + // check the existence of targets + for _, target := range policy.Targets { + t, err := dao.GetRepTarget(target.ID) + if err != nil { + pa.HandleInternalServerError(fmt.Sprintf("failed to get target %d: %v", target.ID, err)) + return + } + + if t == nil { + pa.HandleNotFound(fmt.Sprintf("target %d not found", target.ID)) + return + } + } + + if err = core.GlobalController.UpdatePolicy(convertToRepPolicy(policy)); err != nil { + pa.HandleInternalServerError(fmt.Sprintf("failed to update policy %d: %v", id, err)) + return + } + + if policy.ReplicateExistingImageNow { go func() { - if err := TriggerReplication(id, "", nil, models.RepOpTransfer); err != nil { - log.Errorf("failed to trigger replication of %d: %v", id, err) - } else { - log.Infof("replication of %d triggered", id) + if err = startReplication(id); err != nil { + log.Errorf("failed to send replication signal for policy %d: %v", id, err) + return } + log.Infof("replication signal for policy %d sent", id) }() } } -type enablementReq struct { - Enabled int `json:"enabled"` -} - -// UpdateEnablement changes the enablement of the policy -func (pa *RepPolicyAPI) UpdateEnablement() { +// Delete the replication policy +func (pa *RepPolicyAPI) Delete() { id := pa.GetIDFromURL() - policy, err := dao.GetRepPolicy(id) + + policy, err := core.GlobalController.GetPolicy(id) if err != nil { log.Errorf("failed to get policy %d: %v", id, err) pa.CustomAbort(http.StatusInternalServerError, http.StatusText(http.StatusInternalServerError)) } - if policy == nil { + if policy.ID == 0 { pa.CustomAbort(http.StatusNotFound, http.StatusText(http.StatusNotFound)) } - e := enablementReq{} - pa.DecodeJSONReq(&e) - if e.Enabled != 0 && e.Enabled != 1 { - pa.RenderError(http.StatusBadRequest, "invalid enabled value") - return - } - - if policy.Enabled == e.Enabled { - return - } - - if err := dao.UpdateRepPolicyEnablement(id, e.Enabled); err != nil { - log.Errorf("Failed to update policy enablement in DB, error: %v", err) - pa.RenderError(http.StatusInternalServerError, "Internal Error") - return - } - - if e.Enabled == 1 { - go func() { - if err := TriggerReplication(id, "", nil, models.RepOpTransfer); err != nil { - log.Errorf("failed to trigger replication of %d: %v", id, err) - } else { - log.Infof("replication of %d triggered", id) - } - }() - } else { - go func() { - if err := postReplicationAction(id, "stop"); err != nil { - log.Errorf("failed to stop replication of %d: %v", id, err) - } else { - log.Infof("try to stop replication of %d", id) - } - }() - } -} - -// Delete : policies which are disabled and have no running jobs -// can be deleted -func (pa *RepPolicyAPI) Delete() { - id := pa.GetIDFromURL() - policy, err := dao.GetRepPolicy(id) - if err != nil { - log.Errorf("failed to get policy %d: %v", id, err) - pa.CustomAbort(http.StatusInternalServerError, "") - } - - if policy == nil || policy.Deleted == 1 { - pa.CustomAbort(http.StatusNotFound, "") - } - - if policy.Enabled == 1 { - pa.CustomAbort(http.StatusPreconditionFailed, "plicy is enabled, can not be deleted") - } - + // TODO jobs, err := dao.GetRepJobByPolicy(id) if err != nil { log.Errorf("failed to get jobs of policy %d: %v", id, err) @@ -399,8 +258,83 @@ func (pa *RepPolicyAPI) Delete() { } } - if err = dao.DeleteRepPolicy(id); err != nil { + if err = core.GlobalController.RemovePolicy(id); err != nil { log.Errorf("failed to delete policy %d: %v", id, err) pa.CustomAbort(http.StatusInternalServerError, "") } } + +func convertFromRepPolicy(projectMgr promgr.ProjectManager, policy rep_models.ReplicationPolicy) (*api_models.ReplicationPolicy, error) { + if policy.ID == 0 { + return nil, nil + } + + // populate simple properties + ply := &api_models.ReplicationPolicy{ + ID: policy.ID, + Name: policy.Name, + Description: policy.Description, + Filters: policy.Filters, + ReplicateDeletion: policy.ReplicateDeletion, + Trigger: policy.Trigger, + CreationTime: policy.CreationTime, + UpdateTime: policy.UpdateTime, + } + + // populate projects + for _, projectID := range policy.ProjectIDs { + project, err := projectMgr.Get(projectID) + if err != nil { + return nil, err + } + + ply.Projects = append(ply.Projects, project) + } + + // populate targets + for _, targetID := range policy.TargetIDs { + target, err := dao.GetRepTarget(targetID) + if err != nil { + return nil, err + } + target.Password = "" + ply.Targets = append(ply.Targets, target) + } + + // TODO call the method from replication controller + _, errJobCount, err := dao.FilterRepJobs(policy.ID, "", "error", nil, nil, 0, 0) + if err != nil { + return nil, err + } + ply.ErrorJobCount = errJobCount + + return ply, nil +} + +func convertToRepPolicy(policy *api_models.ReplicationPolicy) rep_models.ReplicationPolicy { + if policy == nil { + return rep_models.ReplicationPolicy{} + } + + ply := rep_models.ReplicationPolicy{ + ID: policy.ID, + Name: policy.Name, + Description: policy.Description, + Filters: policy.Filters, + ReplicateDeletion: policy.ReplicateDeletion, + Trigger: policy.Trigger, + CreationTime: policy.CreationTime, + UpdateTime: policy.UpdateTime, + } + + for _, project := range policy.Projects { + ply.ProjectIDs = append(ply.ProjectIDs, project.ProjectID) + ply.Namespaces = append(ply.Namespaces, project.Name) + } + + for _, target := range policy.Targets { + ply.TargetIDs = append(ply.TargetIDs, target.ID) + } + + return ply +} diff --git a/src/ui/api/replication_policy_test.go b/src/ui/api/replication_policy_test.go index f518537a2..25738298b 100644 --- a/src/ui/api/replication_policy_test.go +++ b/src/ui/api/replication_policy_test.go @@ -15,266 +15,591 @@ package api import ( "fmt" - "github.com/stretchr/testify/assert" - "github.com/vmware/harbor/tests/apitests/apilib" - "strconv" + "net/http" + "net/http/httptest" "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "github.com/vmware/harbor/src/common/dao" + "github.com/vmware/harbor/src/common/models" + "github.com/vmware/harbor/src/replication" + rep_models "github.com/vmware/harbor/src/replication/models" + api_models "github.com/vmware/harbor/src/ui/api/models" ) -const ( - addPolicyName = "testPolicy" +var ( + repPolicyAPIBasePath = "/api/policies/replication" + policyName = "testPolicy" + projectID int64 = 1 + targetID int64 + policyID int64 ) -var addPolicyID int +func TestRepPolicyAPIPost(t *testing.T) { + postFunc := func(resp *httptest.ResponseRecorder) error { + id, err := parseResourceID(resp) + if err != nil { + return err + } + policyID = id + return nil + } -func TestPoliciesPost(t *testing.T) { - var httpStatusCode int - var err error - - assert := assert.New(t) - apiTest := newHarborAPI() - - //add target CommonAddTarget() - targetID := int64(CommonGetTarget()) - repPolicy := &apilib.RepPolicyPost{int64(1), targetID, addPolicyName} + targetID = int64(CommonGetTarget()) - fmt.Println("Testing Policies Post API") + cases := []*codeCheckingCase{ + // 401 + &codeCheckingCase{ + request: &testingRequest{ + method: http.MethodPost, + url: repPolicyAPIBasePath, + }, + code: http.StatusUnauthorized, + }, + // 403 + &codeCheckingCase{ + request: &testingRequest{ + method: http.MethodPost, + url: repPolicyAPIBasePath, + credential: nonSysAdmin, + }, + code: http.StatusForbidden, + }, - //-------------------case 1 : response code = 201------------------------// - fmt.Println("case 1 : response code = 201") - httpStatusCode, err = apiTest.AddPolicy(*admin, *repPolicy) - if err != nil { - t.Error("Error while add policy", err.Error()) - t.Log(err) - } else { - assert.Equal(int(201), httpStatusCode, "httpStatusCode should be 201") - } - - //-------------------case 2 : response code = 409------------------------// - fmt.Println("case 1 : response code = 409:policy already exists") - httpStatusCode, err = apiTest.AddPolicy(*admin, *repPolicy) - if err != nil { - t.Error("Error while add policy", err.Error()) - t.Log(err) - } else { - assert.Equal(int(409), httpStatusCode, "httpStatusCode should be 409") - } - - //-------------------case 3 : response code = 401------------------------// - fmt.Println("case 3 : response code = 401: User need to log in first.") - httpStatusCode, err = apiTest.AddPolicy(*unknownUsr, *repPolicy) - if err != nil { - t.Error("Error while add policy", err.Error()) - t.Log(err) - } else { - assert.Equal(int(401), httpStatusCode, "httpStatusCode should be 401") - } - - //-------------------case 4 : response code = 400------------------------// - fmt.Println("case 4 : response code = 400:project_id invalid.") - - repPolicy = &apilib.RepPolicyPost{TargetId: targetID, Name: addPolicyName} - httpStatusCode, err = apiTest.AddPolicy(*admin, *repPolicy) - if err != nil { - t.Error("Error while add policy", err.Error()) - t.Log(err) - } else { - assert.Equal(int(400), httpStatusCode, "httpStatusCode should be 400") - } - - //-------------------case 5 : response code = 400------------------------// - fmt.Println("case 5 : response code = 400:project_id does not exist.") - - repPolicy.ProjectId = int64(1111) - httpStatusCode, err = apiTest.AddPolicy(*admin, *repPolicy) - if err != nil { - t.Error("Error while add policy", err.Error()) - t.Log(err) - } else { - assert.Equal(int(400), httpStatusCode, "httpStatusCode should be 400") - } - - //-------------------case 6 : response code = 400------------------------// - fmt.Println("case 6 : response code = 400:target_id invalid.") - - repPolicy = &apilib.RepPolicyPost{ProjectId: int64(1), Name: addPolicyName} - httpStatusCode, err = apiTest.AddPolicy(*admin, *repPolicy) - if err != nil { - t.Error("Error while add policy", err.Error()) - t.Log(err) - } else { - assert.Equal(int(400), httpStatusCode, "httpStatusCode should be 400") - } - - //-------------------case 7 : response code = 400------------------------// - fmt.Println("case 6 : response code = 400:target_id does not exist.") - - repPolicy.TargetId = int64(1111) - httpStatusCode, err = apiTest.AddPolicy(*admin, *repPolicy) - if err != nil { - t.Error("Error while add policy", err.Error()) - t.Log(err) - } else { - assert.Equal(int(400), httpStatusCode, "httpStatusCode should be 400") + // 400, invalid name + &codeCheckingCase{ + request: &testingRequest{ + method: http.MethodPost, + url: repPolicyAPIBasePath, + bodyJSON: &api_models.ReplicationPolicy{}, + credential: sysAdmin, + }, + code: http.StatusBadRequest, + }, + // 400, invalid projects + &codeCheckingCase{ + request: &testingRequest{ + method: http.MethodPost, + url: repPolicyAPIBasePath, + bodyJSON: &api_models.ReplicationPolicy{ + Name: policyName, + }, + credential: sysAdmin, + }, + code: http.StatusBadRequest, + }, + // 400, invalid targets + &codeCheckingCase{ + request: &testingRequest{ + method: http.MethodPost, + url: repPolicyAPIBasePath, + bodyJSON: &api_models.ReplicationPolicy{ + Name: policyName, + Projects: []*models.Project{ + &models.Project{ + ProjectID: projectID, + }, + }, + }, + credential: sysAdmin, + }, + code: http.StatusBadRequest, + }, + // 400, invalid filters + &codeCheckingCase{ + request: &testingRequest{ + method: http.MethodPost, + url: repPolicyAPIBasePath, + bodyJSON: &api_models.ReplicationPolicy{ + Name: policyName, + Projects: []*models.Project{ + &models.Project{ + ProjectID: projectID, + }, + }, + Targets: []*models.RepTarget{ + &models.RepTarget{ + ID: targetID, + }, + }, + Filters: []rep_models.Filter{ + rep_models.Filter{ + Kind: "invalid_filter_kind", + Pattern: "", + }, + }, + }, + credential: sysAdmin, + }, + code: http.StatusBadRequest, + }, + // 400, invalid trigger + &codeCheckingCase{ + request: &testingRequest{ + method: http.MethodPost, + url: repPolicyAPIBasePath, + bodyJSON: &api_models.ReplicationPolicy{ + Name: policyName, + Projects: []*models.Project{ + &models.Project{ + ProjectID: projectID, + }, + }, + Targets: []*models.RepTarget{ + &models.RepTarget{ + ID: targetID, + }, + }, + Filters: []rep_models.Filter{ + rep_models.Filter{ + Kind: replication.FilterItemKindRepository, + Pattern: "*", + }, + }, + Trigger: &rep_models.Trigger{ + Kind: "invalid_trigger_kind", + }, + }, + credential: sysAdmin, + }, + code: http.StatusBadRequest, + }, + // 404, project not found + &codeCheckingCase{ + request: &testingRequest{ + method: http.MethodPost, + url: repPolicyAPIBasePath, + bodyJSON: &api_models.ReplicationPolicy{ + Name: policyName, + Projects: []*models.Project{ + &models.Project{ + ProjectID: 10000, + }, + }, + Targets: []*models.RepTarget{ + &models.RepTarget{ + ID: targetID, + }, + }, + Filters: []rep_models.Filter{ + rep_models.Filter{ + Kind: replication.FilterItemKindRepository, + Pattern: "*", + }, + }, + Trigger: &rep_models.Trigger{ + Kind: replication.TriggerKindManual, + }, + }, + credential: sysAdmin, + }, + code: http.StatusNotFound, + }, + // 404, target not found + &codeCheckingCase{ + request: &testingRequest{ + method: http.MethodPost, + url: repPolicyAPIBasePath, + bodyJSON: &api_models.ReplicationPolicy{ + Name: policyName, + Projects: []*models.Project{ + &models.Project{ + ProjectID: projectID, + }, + }, + Targets: []*models.RepTarget{ + &models.RepTarget{ + ID: 10000, + }, + }, + Filters: []rep_models.Filter{ + rep_models.Filter{ + Kind: replication.FilterItemKindRepository, + Pattern: "*", + }, + }, + Trigger: &rep_models.Trigger{ + Kind: replication.TriggerKindManual, + }, + }, + credential: sysAdmin, + }, + code: http.StatusNotFound, + }, + // 201 + &codeCheckingCase{ + request: &testingRequest{ + method: http.MethodPost, + url: repPolicyAPIBasePath, + bodyJSON: &api_models.ReplicationPolicy{ + Name: policyName, + Projects: []*models.Project{ + &models.Project{ + ProjectID: projectID, + }, + }, + Targets: []*models.RepTarget{ + &models.RepTarget{ + ID: targetID, + }, + }, + Filters: []rep_models.Filter{ + rep_models.Filter{ + Kind: replication.FilterItemKindRepository, + Pattern: "*", + }, + }, + Trigger: &rep_models.Trigger{ + Kind: replication.TriggerKindManual, + }, + }, + credential: sysAdmin, + }, + code: http.StatusCreated, + postFunc: postFunc, + }, } + runCodeCheckingCases(t, cases...) } -func TestPoliciesList(t *testing.T) { - var httpStatusCode int - var err error - var reslut []apilib.RepPolicy +func TestRepPolicyAPIGet(t *testing.T) { - assert := assert.New(t) - apiTest := newHarborAPI() - - fmt.Println("Testing Policies Get/List API") - - //-------------------case 1 : response code = 200------------------------// - fmt.Println("case 1 : response code = 200") - projectID := "1" - httpStatusCode, reslut, err = apiTest.ListPolicies(*admin, addPolicyName, projectID) - if err != nil { - t.Error("Error while get policies", err.Error()) - t.Log(err) - } else { - assert.Equal(int(200), httpStatusCode, "httpStatusCode should be 200") - addPolicyID = int(reslut[0].Id) + cases := []*codeCheckingCase{ + // 404 + &codeCheckingCase{ + request: &testingRequest{ + method: http.MethodGet, + url: fmt.Sprintf("%s/%d", repPolicyAPIBasePath, 10000), + credential: sysAdmin, + }, + code: http.StatusNotFound, + }, + // 401 + &codeCheckingCase{ + request: &testingRequest{ + method: http.MethodGet, + url: fmt.Sprintf("%s/%d", repPolicyAPIBasePath, policyID), + }, + code: http.StatusUnauthorized, + }, } - //-------------------case 2 : response code = 400------------------------// - fmt.Println("case 2 : response code = 400:invalid projectID") - projectID = "cc" - httpStatusCode, reslut, err = apiTest.ListPolicies(*admin, addPolicyName, projectID) - if err != nil { - t.Error("Error while get policies", err.Error()) - t.Log(err) - } else { - assert.Equal(int(400), httpStatusCode, "httpStatusCode should be 400") - } + runCodeCheckingCases(t, cases...) + // 200 + policy := &api_models.ReplicationPolicy{} + resp, err := handleAndParse( + &testingRequest{ + method: http.MethodGet, + url: fmt.Sprintf("%s/%d", repPolicyAPIBasePath, policyID), + credential: sysAdmin, + }, policy) + require.Nil(t, err) + assert.Equal(t, http.StatusOK, resp.Code) + assert.Equal(t, policyID, policy.ID) + assert.Equal(t, policyName, policy.Name) } -func TestPolicyGet(t *testing.T) { - var httpStatusCode int - var err error - - assert := assert.New(t) - apiTest := newHarborAPI() - - fmt.Println("Testing Policy Get API by PolicyID") - - //-------------------case 1 : response code = 200------------------------// - fmt.Println("case 1 : response code = 200") - - policyID := strconv.Itoa(addPolicyID) - httpStatusCode, err = apiTest.GetPolicyByID(*admin, policyID) - if err != nil { - t.Error("Error while get policy", err.Error()) - t.Log(err) - } else { - assert.Equal(int(200), httpStatusCode, "httpStatusCode should be 200") +func TestRepPolicyAPIList(t *testing.T) { + projectAdmin := models.User{ + Username: "project_admin", + Password: "ProjectAdmin", + Email: "project_admin@test.com", } + projectDev := models.User{ + Username: "project_dev", + Password: "ProjectDev", + Email: "project_dev@test.com", + } + + proAdminID, err := dao.Register(projectAdmin) + if err != nil { + panic(err) + } + defer dao.DeleteUser(int(proAdminID)) + + if err = dao.AddProjectMember(1, int(proAdminID), models.PROJECTADMIN); err != nil { + panic(err) + } + defer dao.DeleteProjectMember(1, int(proAdminID)) + + proDevID, err := dao.Register(projectDev) + if err != nil { + panic(err) + } + defer dao.DeleteUser(int(proDevID)) + + if err = dao.AddProjectMember(1, int(proDevID), models.DEVELOPER); err != nil { + panic(err) + } + defer dao.DeleteProjectMember(1, int(proDevID)) + + // 400: invalid project ID + runCodeCheckingCases(t, &codeCheckingCase{ + request: &testingRequest{ + method: http.MethodGet, + url: repPolicyAPIBasePath, + queryStruct: struct { + ProjectID int64 `url:"project_id"` + }{ + ProjectID: -1, + }, + credential: sysAdmin, + }, + code: http.StatusBadRequest, + }) + + // 200 system admin + policies := []*api_models.ReplicationPolicy{} + resp, err := handleAndParse( + &testingRequest{ + method: http.MethodGet, + url: repPolicyAPIBasePath, + queryStruct: struct { + ProjectID int64 `url:"project_id"` + Name string `url:"name"` + }{ + ProjectID: projectID, + Name: policyName, + }, + credential: sysAdmin, + }, &policies) + require.Nil(t, err) + assert.Equal(t, http.StatusOK, resp.Code) + require.Equal(t, 1, len(policies)) + assert.Equal(t, policyID, policies[0].ID) + assert.Equal(t, policyName, policies[0].Name) + + // 200 project admin + policies = []*api_models.ReplicationPolicy{} + resp, err = handleAndParse( + &testingRequest{ + method: http.MethodGet, + url: repPolicyAPIBasePath, + queryStruct: struct { + ProjectID int64 `url:"project_id"` + Name string `url:"name"` + }{ + ProjectID: projectID, + Name: policyName, + }, + credential: &usrInfo{ + Name: projectAdmin.Username, + Passwd: projectAdmin.Password, + }, + }, &policies) + require.Nil(t, err) + assert.Equal(t, http.StatusOK, resp.Code) + require.Equal(t, 1, len(policies)) + assert.Equal(t, policyID, policies[0].ID) + assert.Equal(t, policyName, policies[0].Name) + + // 200 project developer + policies = []*api_models.ReplicationPolicy{} + resp, err = handleAndParse( + &testingRequest{ + method: http.MethodGet, + url: repPolicyAPIBasePath, + queryStruct: struct { + ProjectID int64 `url:"project_id"` + Name string `url:"name"` + }{ + ProjectID: projectID, + Name: policyName, + }, + credential: &usrInfo{ + Name: projectDev.Username, + Passwd: projectDev.Password, + }, + }, &policies) + require.Nil(t, err) + assert.Equal(t, http.StatusOK, resp.Code) + require.Equal(t, 0, len(policies)) + + // 200 + policies = []*api_models.ReplicationPolicy{} + resp, err = handleAndParse( + &testingRequest{ + method: http.MethodGet, + url: repPolicyAPIBasePath, + queryStruct: struct { + ProjectID int64 `url:"project_id"` + Name string `url:"name"` + }{ + ProjectID: projectID, + Name: "non_exist_policy", + }, + credential: sysAdmin, + }, &policies) + require.Nil(t, err) + assert.Equal(t, http.StatusOK, resp.Code) + require.Equal(t, 0, len(policies)) } -func TestPolicyUpdateInfo(t *testing.T) { - var httpStatusCode int - var err error - - targetID := int64(CommonGetTarget()) - policyInfo := &apilib.RepPolicyUpdate{TargetId: targetID, Name: "testNewName"} - - assert := assert.New(t) - apiTest := newHarborAPI() - - fmt.Println("Testing Policy PUT API to update policyInfo") - - //-------------------case 1 : response code = 200------------------------// - fmt.Println("case 1 : response code = 200") - - policyID := strconv.Itoa(addPolicyID) - httpStatusCode, err = apiTest.PutPolicyInfoByID(*admin, policyID, *policyInfo) - if err != nil { - t.Error("Error while update policyInfo", err.Error()) - t.Log(err) - } else { - assert.Equal(int(200), httpStatusCode, "httpStatusCode should be 200") +func TestRepPolicyAPIPut(t *testing.T) { + cases := []*codeCheckingCase{ + // 404 + &codeCheckingCase{ + request: &testingRequest{ + method: http.MethodPut, + url: fmt.Sprintf("%s/%d", repPolicyAPIBasePath, 10000), + credential: sysAdmin, + }, + code: http.StatusNotFound, + }, + // 400, invalid trigger + &codeCheckingCase{ + request: &testingRequest{ + method: http.MethodPut, + url: fmt.Sprintf("%s/%d", repPolicyAPIBasePath, policyID), + bodyJSON: &api_models.ReplicationPolicy{ + Name: policyName, + Projects: []*models.Project{ + &models.Project{ + ProjectID: projectID, + }, + }, + Targets: []*models.RepTarget{ + &models.RepTarget{ + ID: targetID, + }, + }, + Filters: []rep_models.Filter{ + rep_models.Filter{ + Kind: replication.FilterItemKindRepository, + Pattern: "*", + }, + }, + Trigger: &rep_models.Trigger{ + Kind: "invalid_trigger_kind", + }, + }, + credential: sysAdmin, + }, + code: http.StatusBadRequest, + }, + // 200 + &codeCheckingCase{ + request: &testingRequest{ + method: http.MethodPut, + url: fmt.Sprintf("%s/%d", repPolicyAPIBasePath, policyID), + bodyJSON: &api_models.ReplicationPolicy{ + Name: policyName, + Projects: []*models.Project{ + &models.Project{ + ProjectID: projectID, + }, + }, + Targets: []*models.RepTarget{ + &models.RepTarget{ + ID: targetID, + }, + }, + Filters: []rep_models.Filter{ + rep_models.Filter{ + Kind: replication.FilterItemKindRepository, + Pattern: "*", + }, + }, + Trigger: &rep_models.Trigger{ + Kind: replication.TriggerKindImmediate, + }, + }, + credential: sysAdmin, + }, + code: http.StatusOK, + }, } + + runCodeCheckingCases(t, cases...) } -func TestPolicyUpdateEnablement(t *testing.T) { - var httpStatusCode int - var err error - - enablement := &apilib.RepPolicyEnablementReq{int32(0)} - - assert := assert.New(t) - apiTest := newHarborAPI() - - fmt.Println("Testing Policy PUT API to update policy enablement") - - //-------------------case 1 : response code = 200------------------------// - fmt.Println("case 1 : response code = 200") - - policyID := strconv.Itoa(addPolicyID) - httpStatusCode, err = apiTest.PutPolicyEnableByID(*admin, policyID, *enablement) - if err != nil { - t.Error("Error while put policy enablement", err.Error()) - t.Log(err) - } else { - assert.Equal(int(200), httpStatusCode, "httpStatusCode should be 200") - } - //-------------------case 2 : response code = 404------------------------// - fmt.Println("case 2 : response code = 404,Not Found") - - policyID = "111" - httpStatusCode, err = apiTest.PutPolicyEnableByID(*admin, policyID, *enablement) - if err != nil { - t.Error("Error while put policy enablement", err.Error()) - t.Log(err) - } else { - assert.Equal(int(404), httpStatusCode, "httpStatusCode should be 404") +func TestRepPolicyAPIDelete(t *testing.T) { + cases := []*codeCheckingCase{ + // 404 + &codeCheckingCase{ + request: &testingRequest{ + method: http.MethodDelete, + url: fmt.Sprintf("%s/%d", repPolicyAPIBasePath, 10000), + credential: sysAdmin, + }, + code: http.StatusNotFound, + }, + // 200 + &codeCheckingCase{ + request: &testingRequest{ + method: http.MethodDelete, + url: fmt.Sprintf("%s/%d", repPolicyAPIBasePath, policyID), + credential: sysAdmin, + }, + code: http.StatusOK, + }, } + runCodeCheckingCases(t, cases...) } -func TestPolicyDelete(t *testing.T) { - var httpStatusCode int - var err error - - assert := assert.New(t) - apiTest := newHarborAPI() - - fmt.Println("Testing Policy Delete API") - - //-------------------case 1 : response code = 412------------------------// - fmt.Println("case 1 : response code = 412:policy is enabled, can not be deleted") - - CommonPolicyEabled(addPolicyID, 1) - policyID := strconv.Itoa(addPolicyID) - - httpStatusCode, err = apiTest.DeletePolicyByID(*admin, policyID) - if err != nil { - t.Error("Error while delete policy", err.Error()) - t.Log(err) - } else { - assert.Equal(int(412), httpStatusCode, "httpStatusCode should be 412") +func TestConvertToRepPolicy(t *testing.T) { + cases := []struct { + input *api_models.ReplicationPolicy + expected rep_models.ReplicationPolicy + }{ + { + input: nil, + expected: rep_models.ReplicationPolicy{}, + }, + { + input: &api_models.ReplicationPolicy{ + ID: 1, + Name: "policy", + Description: "description", + Filters: []rep_models.Filter{ + rep_models.Filter{ + Kind: "filter_kind_01", + Pattern: "*", + }, + }, + ReplicateDeletion: true, + Trigger: &rep_models.Trigger{ + Kind: "trigger_kind_01", + }, + Projects: []*models.Project{ + &models.Project{ + ProjectID: 1, + Name: "library", + }, + }, + Targets: []*models.RepTarget{ + &models.RepTarget{ + ID: 1, + }, + }, + }, + expected: rep_models.ReplicationPolicy{ + ID: 1, + Name: "policy", + Description: "description", + Filters: []rep_models.Filter{ + rep_models.Filter{ + Kind: "filter_kind_01", + Pattern: "*", + }, + }, + ReplicateDeletion: true, + Trigger: &rep_models.Trigger{ + Kind: "trigger_kind_01", + }, + ProjectIDs: []int64{1}, + Namespaces: []string{"library"}, + TargetIDs: []int64{1}, + }, + }, } - //-------------------case 2 : response code = 200------------------------// - fmt.Println("case 2 : response code = 200") - - CommonPolicyEabled(addPolicyID, 0) - policyID = strconv.Itoa(addPolicyID) - - httpStatusCode, err = apiTest.DeletePolicyByID(*admin, policyID) - if err != nil { - t.Error("Error while delete policy", err.Error()) - t.Log(err) - } else { - assert.Equal(int(200), httpStatusCode, "httpStatusCode should be 200") + for _, c := range cases { + assert.EqualValues(t, c.expected, convertToRepPolicy(c.input)) } - - CommonDelTarget() } diff --git a/src/ui/api/replication_test.go b/src/ui/api/replication_test.go new file mode 100644 index 000000000..ce9330f44 --- /dev/null +++ b/src/ui/api/replication_test.go @@ -0,0 +1,92 @@ +// Copyright (c) 2017 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 api + +import ( + "fmt" + "net/http" + "testing" + + "github.com/stretchr/testify/require" + "github.com/vmware/harbor/src/common/dao" + "github.com/vmware/harbor/src/common/models" + "github.com/vmware/harbor/src/replication" + api_models "github.com/vmware/harbor/src/ui/api/models" +) + +const ( + replicationAPIBaseURL = "/api/replications" +) + +func TestReplicationAPIPost(t *testing.T) { + targetID, err := dao.AddRepTarget( + models.RepTarget{ + Name: "test_replication_target", + URL: "127.0.0.1", + Username: "username", + Password: "password", + }) + require.Nil(t, err) + defer dao.DeleteRepTarget(targetID) + + policyID, err := dao.AddRepPolicy( + models.RepPolicy{ + Name: "test_replication_policy", + ProjectID: 1, + TargetID: targetID, + Trigger: fmt.Sprintf("{\"kind\":\"%s\"}", replication.TriggerKindManual), + }) + require.Nil(t, err) + defer dao.DeleteRepPolicy(policyID) + + cases := []*codeCheckingCase{ + // 401 + &codeCheckingCase{ + request: &testingRequest{ + method: http.MethodPost, + url: replicationAPIBaseURL, + bodyJSON: &api_models.Replication{ + PolicyID: policyID, + }, + }, + code: http.StatusUnauthorized, + }, + // 404 + &codeCheckingCase{ + request: &testingRequest{ + method: http.MethodPost, + url: replicationAPIBaseURL, + bodyJSON: &api_models.Replication{ + PolicyID: 10000, + }, + credential: admin, + }, + code: http.StatusNotFound, + }, + // 200 + &codeCheckingCase{ + request: &testingRequest{ + method: http.MethodPost, + url: replicationAPIBaseURL, + bodyJSON: &api_models.Replication{ + PolicyID: policyID, + }, + credential: admin, + }, + code: http.StatusOK, + }, + } + + runCodeCheckingCases(t, cases...) +} diff --git a/src/ui/api/repository.go b/src/ui/api/repository.go index bc12785f5..84551effd 100644 --- a/src/ui/api/repository.go +++ b/src/ui/api/repository.go @@ -27,12 +27,15 @@ import ( "github.com/docker/distribution/manifest/schema2" "github.com/vmware/harbor/src/common/dao" "github.com/vmware/harbor/src/common/models" + "github.com/vmware/harbor/src/common/notifier" "github.com/vmware/harbor/src/common/utils" "github.com/vmware/harbor/src/common/utils/clair" registry_error "github.com/vmware/harbor/src/common/utils/error" "github.com/vmware/harbor/src/common/utils/log" "github.com/vmware/harbor/src/common/utils/notary" "github.com/vmware/harbor/src/common/utils/registry" + "github.com/vmware/harbor/src/replication/event/notification" + "github.com/vmware/harbor/src/replication/event/topic" "github.com/vmware/harbor/src/ui/config" uiutils "github.com/vmware/harbor/src/ui/utils" ) @@ -261,7 +264,17 @@ func (ra *RepositoryAPI) Delete() { } log.Infof("delete tag: %s:%s", repoName, t) - go TriggerReplicationByRepository(project.ProjectID, repoName, []string{t}, models.RepOpDelete) + go func() { + image := repoName + ":" + t + err := notifier.Publish(topic.ReplicationEventTopicOnDeletion, notification.OnDeletionNotification{ + Image: image, + }) + if err != nil { + log.Errorf("failed to publish on deletion topic for resource %s: %v", image, err) + return + } + log.Debugf("the on deletion topic for resource %s published", image) + }() go func(tag string) { if err := dao.AddAccessLog(models.AccessLog{ diff --git a/src/ui/api/target.go b/src/ui/api/target.go index 22d68fdf0..603341bf0 100644 --- a/src/ui/api/target.go +++ b/src/ui/api/target.go @@ -218,23 +218,6 @@ func (t *TargetAPI) Put() { t.CustomAbort(http.StatusNotFound, http.StatusText(http.StatusNotFound)) } - policies, err := dao.GetRepPolicyByTarget(id) - if err != nil { - log.Errorf("failed to get policies according target %d: %v", id, err) - t.CustomAbort(http.StatusInternalServerError, http.StatusText(http.StatusInternalServerError)) - } - - hasEnabledPolicy := false - for _, policy := range policies { - if policy.Enabled == 1 { - hasEnabledPolicy = true - break - } - } - - if hasEnabledPolicy { - t.CustomAbort(http.StatusBadRequest, "the target is associated with policy which is enabled") - } if len(target.Password) != 0 { target.Password, err = utils.ReversibleDecrypt(target.Password, t.secretKey) if err != nil { diff --git a/src/ui/api/utils.go b/src/ui/api/utils.go index a0bb4fb39..7f883109b 100644 --- a/src/ui/api/utils.go +++ b/src/ui/api/utils.go @@ -15,10 +15,7 @@ package api import ( - "bytes" - "encoding/json" "fmt" - "io/ioutil" "net/http" "sort" "strings" @@ -77,94 +74,6 @@ func checkUserExists(name string) int { return 0 } -// TriggerReplication triggers the replication according to the policy -func TriggerReplication(policyID int64, repository string, - tags []string, operation string) error { - data := struct { - PolicyID int64 `json:"policy_id"` - Repo string `json:"repository"` - Operation string `json:"operation"` - TagList []string `json:"tags"` - }{ - PolicyID: policyID, - Repo: repository, - TagList: tags, - Operation: operation, - } - - b, err := json.Marshal(&data) - if err != nil { - return err - } - url := buildReplicationURL() - - return uiutils.RequestAsUI("POST", url, bytes.NewBuffer(b), uiutils.NewStatusRespHandler(http.StatusOK)) -} - -// TriggerReplicationByRepository triggers the replication according to the repository -func TriggerReplicationByRepository(projectID int64, repository string, tags []string, operation string) { - policies, err := dao.GetRepPolicyByProject(projectID) - if err != nil { - log.Errorf("failed to get policies for repository %s: %v", repository, err) - return - } - - for _, policy := range policies { - if policy.Enabled == 0 { - continue - } - if err := TriggerReplication(policy.ID, repository, tags, operation); err != nil { - log.Errorf("failed to trigger replication of policy %d for %s: %v", policy.ID, repository, err) - } else { - log.Infof("replication of policy %d for %s triggered", policy.ID, repository) - } - } -} - -func postReplicationAction(policyID int64, acton string) error { - data := struct { - PolicyID int64 `json:"policy_id"` - Action string `json:"action"` - }{ - PolicyID: policyID, - Action: acton, - } - - b, err := json.Marshal(&data) - if err != nil { - return err - } - - url := buildReplicationActionURL() - - req, err := http.NewRequest("POST", url, bytes.NewBuffer(b)) - if err != nil { - return err - } - - uiutils.AddUISecret(req) - - client := &http.Client{} - - resp, err := client.Do(req) - if err != nil { - return err - } - - defer resp.Body.Close() - - if resp.StatusCode == http.StatusOK { - return nil - } - - b, err = ioutil.ReadAll(resp.Body) - if err != nil { - return err - } - - return fmt.Errorf("%d %s", resp.StatusCode, string(b)) -} - // SyncRegistry syncs the repositories of registry with database. func SyncRegistry(pm promgr.ProjectManager) error { diff --git a/src/ui/config/config.go b/src/ui/config/config.go index 6809eb568..d651aa35c 100644 --- a/src/ui/config/config.go +++ b/src/ui/config/config.go @@ -23,12 +23,12 @@ import ( "strings" "github.com/vmware/harbor/src/adminserver/client" - "github.com/vmware/harbor/src/adminserver/client/auth" "github.com/vmware/harbor/src/common" comcfg "github.com/vmware/harbor/src/common/config" "github.com/vmware/harbor/src/common/models" "github.com/vmware/harbor/src/common/secret" "github.com/vmware/harbor/src/common/utils/log" + jobservice_client "github.com/vmware/harbor/src/jobservice/client" "github.com/vmware/harbor/src/ui/promgr" "github.com/vmware/harbor/src/ui/promgr/pmsdriver" "github.com/vmware/harbor/src/ui/promgr/pmsdriver/admiral" @@ -55,6 +55,8 @@ var ( AdmiralClient *http.Client // TokenReader is used in integration mode to read token TokenReader admiral.TokenReader + // GlobalJobserviceClient is a global client for jobservice + GlobalJobserviceClient jobservice_client.Client ) // Init configurations @@ -73,8 +75,10 @@ func Init() error { // InitByURL Init configurations with given url func InitByURL(adminServerURL string) error { log.Infof("initializing client for adminserver %s ...", adminServerURL) - authorizer := auth.NewSecretAuthorizer(secretCookieName, UISecret()) - AdminserverClient = client.NewClient(adminServerURL, authorizer) + cfg := &client.Config{ + Secret: UISecret(), + } + AdminserverClient = client.NewClient(adminServerURL, cfg) if err := AdminserverClient.Ping(); err != nil { return fmt.Errorf("failed to ping adminserver: %v", err) } @@ -91,6 +95,11 @@ func InitByURL(adminServerURL string) error { // init project manager based on deploy mode initProjectManager() + GlobalJobserviceClient = jobservice_client.NewDefaultClient(InternalJobServiceURL(), + &jobservice_client.Config{ + Secret: UISecret(), + }) + return nil } @@ -259,6 +268,10 @@ func InternalJobServiceURL() string { return "http://jobservice" } + + if cfg[common.JobServiceURL] == nil { + return "http://jobservice" + } return strings.TrimSuffix(cfg[common.JobServiceURL].(string), "/") } diff --git a/src/ui/main.go b/src/ui/main.go index ede844b02..f322db446 100644 --- a/src/ui/main.go +++ b/src/ui/main.go @@ -29,6 +29,8 @@ import ( "github.com/vmware/harbor/src/common/models" "github.com/vmware/harbor/src/common/notifier" "github.com/vmware/harbor/src/common/scheduler" + "github.com/vmware/harbor/src/replication/core" + _ "github.com/vmware/harbor/src/replication/event" "github.com/vmware/harbor/src/ui/api" _ "github.com/vmware/harbor/src/ui/auth/db" _ "github.com/vmware/harbor/src/ui/auth/ldap" @@ -130,6 +132,10 @@ func main() { notifier.Publish(notifier.ScanAllPolicyTopic, notifier.ScanPolicyNotification{Type: scanAllPolicy.Type, DailyTime: (int64)(dailyTime)}) } + if err := core.Init(); err != nil { + log.Errorf("failed to initialize the replication controller: %v", err) + } + filter.Init() beego.InsertFilter("/*", beego.BeforeRouter, filter.SecurityFilter) beego.InsertFilter("/api/*", beego.BeforeRouter, filter.MediaTypeFilter("application/json")) diff --git a/src/ui/router.go b/src/ui/router.go index 51c031653..fd6d34c78 100644 --- a/src/ui/router.go +++ b/src/ui/router.go @@ -97,14 +97,13 @@ func initRouters() { beego.Router("/api/repositories/*/tags/:tag/manifest", &api.RepositoryAPI{}, "get:GetManifests") beego.Router("/api/repositories/*/signatures", &api.RepositoryAPI{}, "get:GetSignatures") beego.Router("/api/repositories/top", &api.RepositoryAPI{}, "get:GetTopRepos") - beego.Router("/api/jobs/replication/", &api.RepJobAPI{}, "get:List") + beego.Router("/api/jobs/replication/", &api.RepJobAPI{}, "get:List;put:StopJobs") beego.Router("/api/jobs/replication/:id([0-9]+)", &api.RepJobAPI{}) beego.Router("/api/jobs/replication/:id([0-9]+)/log", &api.RepJobAPI{}, "get:GetLog") beego.Router("/api/jobs/scan/:id([0-9]+)/log", &api.ScanJobAPI{}, "get:GetLog") beego.Router("/api/policies/replication/:id([0-9]+)", &api.RepPolicyAPI{}) beego.Router("/api/policies/replication", &api.RepPolicyAPI{}, "get:List") beego.Router("/api/policies/replication", &api.RepPolicyAPI{}, "post:Post") - beego.Router("/api/policies/replication/:id([0-9]+)/enablement", &api.RepPolicyAPI{}, "put:UpdateEnablement") beego.Router("/api/targets/", &api.TargetAPI{}, "get:List") beego.Router("/api/targets/", &api.TargetAPI{}, "post:Post") beego.Router("/api/targets/:id([0-9]+)", &api.TargetAPI{}) @@ -114,6 +113,7 @@ func initRouters() { beego.Router("/api/configurations", &api.ConfigAPI{}) beego.Router("/api/configurations/reset", &api.ConfigAPI{}, "post:Reset") beego.Router("/api/statistics", &api.StatisticAPI{}) + beego.Router("/api/replications", &api.ReplicationAPI{}) beego.Router("/api/systeminfo", &api.SystemInfoAPI{}, "get:GetGeneralInfo") beego.Router("/api/systeminfo/volumes", &api.SystemInfoAPI{}, "get:GetVolumeInfo") diff --git a/src/ui/service/notifications/registry/handler.go b/src/ui/service/notifications/registry/handler.go index 4e532242c..750bbf47b 100644 --- a/src/ui/service/notifications/registry/handler.go +++ b/src/ui/service/notifications/registry/handler.go @@ -23,8 +23,11 @@ import ( "github.com/vmware/harbor/src/common/dao" clairdao "github.com/vmware/harbor/src/common/dao/clair" "github.com/vmware/harbor/src/common/models" + "github.com/vmware/harbor/src/common/notifier" "github.com/vmware/harbor/src/common/utils" "github.com/vmware/harbor/src/common/utils/log" + rep_notification "github.com/vmware/harbor/src/replication/event/notification" + "github.com/vmware/harbor/src/replication/event/topic" "github.com/vmware/harbor/src/ui/api" "github.com/vmware/harbor/src/ui/config" uiutils "github.com/vmware/harbor/src/ui/utils" @@ -104,7 +107,17 @@ func (n *NotificationHandler) Post() { } }() - go api.TriggerReplicationByRepository(pro.ProjectID, repository, []string{tag}, models.RepOpTransfer) + go func() { + image := repository + ":" + tag + err := notifier.Publish(topic.ReplicationEventTopicOnPush, rep_notification.OnPushNotification{ + Image: image, + }) + if err != nil { + log.Errorf("failed to publish on push topic for resource %s: %v", image, err) + return + } + log.Debugf("the on push topic for resource %s published", image) + }() if autoScanEnabled(pro) { last, err := clairdao.GetLastUpdate() diff --git a/src/ui_ng/lib/src/confirmation-dialog/confirmation-dialog.component.html.ts b/src/ui_ng/lib/src/confirmation-dialog/confirmation-dialog.component.html.ts index e882ba5c6..086d008e0 100644 --- a/src/ui_ng/lib/src/confirmation-dialog/confirmation-dialog.component.html.ts +++ b/src/ui_ng/lib/src/confirmation-dialog/confirmation-dialog.component.html.ts @@ -23,7 +23,6 @@ export const CONFIRMATION_DIALOG_TEMPLATE: string = ` - @@ -31,7 +30,7 @@ export const CONFIRMATION_DIALOG_TEMPLATE: string = ` - + diff --git a/src/ui_ng/lib/src/confirmation-dialog/confirmation-dialog.component.ts b/src/ui_ng/lib/src/confirmation-dialog/confirmation-dialog.component.ts index 92d549a7c..9216d6356 100644 --- a/src/ui_ng/lib/src/confirmation-dialog/confirmation-dialog.component.ts +++ b/src/ui_ng/lib/src/confirmation-dialog/confirmation-dialog.component.ts @@ -98,8 +98,8 @@ export class ConfirmationDialogComponent { this.close(); } - confirm(): void { - if(!this.message){//Inproper condition + delete(): void { + if (!this.message){//Inproper condition this.close(); return; } @@ -118,4 +118,21 @@ export class ConfirmationDialogComponent { ); this.confirmAction.emit(message); } + + confirm(): void { + if (!this.message){//Inproper condition + this.close(); + return; + } + + let data: any = this.message.data ? this.message.data : {}; + let target = this.message.targetId ? this.message.targetId : ConfirmationTargets.EMPTY; + let message = new ConfirmationAcknowledgement( + ConfirmationState.CONFIRMED, + data, + target + ); + this.confirmAction.emit(message); + this.close(); + } } diff --git a/src/ui_ng/lib/src/list-replication-rule/list-replication-rule.component.html.ts b/src/ui_ng/lib/src/list-replication-rule/list-replication-rule.component.html.ts index aab9138df..f62870012 100644 --- a/src/ui_ng/lib/src/list-replication-rule/list-replication-rule.component.html.ts +++ b/src/ui_ng/lib/src/list-replication-rule/list-replication-rule.component.html.ts @@ -2,39 +2,27 @@ export const LIST_REPLICATION_RULE_TEMPLATE: string = `
-
- - - +
+ + + +
{{'REPLICATION.NAME' | translate}} - {{'REPLICATION.PROJECT' | translate}} + {{'REPLICATION.PROJECT' | translate}} {{'REPLICATION.DESCRIPTION' | translate}} - {{'REPLICATION.DESTINATION_NAME' | translate}} - {{'REPLICATION.LAST_START_TIME' | translate}} - {{'REPLICATION.ACTIVATION' | translate}} + {{'REPLICATION.DESTINATION_NAME' | translate}} + {{'REPLICATION.SCHEDULE' | translate}} {{'REPLICATION.PLACEHOLDER' | translate }} - - - - {{p.name}} - - - {{p.name}} - + {{p.name}} + + {{p.projects?.length>0 ? p.projects[0].name : ''}} - {{p.project_name}} {{p.description ? p.description : '-'}} - {{p.target_name}} - - - - {{p.start_time | date: 'short'}} - - - {{ (p.enabled === 1 ? 'REPLICATION.ENABLED' : 'REPLICATION.DISABLED') | translate}} - + {{p.targets?.length>0 ? p.targets[0].name : ''}} + {{p.trigger ? p.trigger.kind : ''}} {{pagination.firstItem + 1}} - {{pagination.lastItem +1 }} {{'REPLICATION.OF' | translate}} {{pagination.totalItems }} {{'REPLICATION.ITEMS' | translate}} diff --git a/src/ui_ng/lib/src/list-replication-rule/list-replication-rule.component.ts b/src/ui_ng/lib/src/list-replication-rule/list-replication-rule.component.ts index b76bc3982..96bc4f98d 100644 --- a/src/ui_ng/lib/src/list-replication-rule/list-replication-rule.component.ts +++ b/src/ui_ng/lib/src/list-replication-rule/list-replication-rule.component.ts @@ -67,6 +67,7 @@ export class ListReplicationRuleComponent implements OnInit, OnChanges { @Output() toggleOne = new EventEmitter(); @Output() redirect = new EventEmitter(); @Output() openNewRule = new EventEmitter(); + @Output() replicateManual = new EventEmitter(); projectScope: boolean = false; @@ -95,8 +96,8 @@ export class ListReplicationRuleComponent implements OnInit, OnChanges { setInterval(() => ref.markForCheck(), 500); } - public get creationAvailable(): boolean { - return !this.readonly && this.projectId ? true : false; + public get opereateAvailable(): boolean { + return !this.readonly && !this.projectId ? true : false; } @@ -221,6 +222,10 @@ export class ListReplicationRuleComponent implements OnInit, OnChanges { this.editOne.emit(rules); } + replicateRule(rule: ReplicationRule) { + this.replicateManual.emit(rule); + } + toggleRule(rule: ReplicationRule) { let toggleConfirmMessage: ConfirmationMessage = new ConfirmationMessage( rule.enabled === 1 ? 'REPLICATION.TOGGLE_DISABLE_TITLE' : 'REPLICATION.TOGGLE_ENABLE_TITLE', diff --git a/src/ui_ng/lib/src/replication/replication.component.css.ts b/src/ui_ng/lib/src/replication/replication.component.css.ts index d1f9c7907..aa73d9af3 100644 --- a/src/ui_ng/lib/src/replication/replication.component.css.ts +++ b/src/ui_ng/lib/src/replication/replication.component.css.ts @@ -22,10 +22,10 @@ export const REPLICATION_STYLE: string = ` padding-right: 16px; margin-top: 24px; } -.rightPos{ + .rightPos{ position: absolute; - z-index: 100; right: 35px; - margin-top: 4px; -} -`; \ No newline at end of file + margin-top: 5px; + z-index: 100; + height: 32px; +}`; diff --git a/src/ui_ng/lib/src/replication/replication.component.html.ts b/src/ui_ng/lib/src/replication/replication.component.html.ts index 4225a8596..81994aa06 100644 --- a/src/ui_ng/lib/src/replication/replication.component.html.ts +++ b/src/ui_ng/lib/src/replication/replication.component.html.ts @@ -3,11 +3,6 @@ export const REPLICATION_TEMPLATE: string = `
-
- -
@@ -16,8 +11,9 @@ export const REPLICATION_TEMPLATE: string = `
- +
+

{{'REPLICATION.REPLICATION_JOBS' | translate}}
@@ -72,5 +68,4 @@ export const REPLICATION_TEMPLATE: string = `
-
`; \ No newline at end of file diff --git a/src/ui_ng/lib/src/replication/replication.component.spec.ts b/src/ui_ng/lib/src/replication/replication.component.spec.ts index ca875e0ac..7b720ec15 100644 --- a/src/ui_ng/lib/src/replication/replication.component.spec.ts +++ b/src/ui_ng/lib/src/replication/replication.component.spec.ts @@ -260,19 +260,6 @@ describe('Replication Component (inline template)', ()=>{ }); })); - it('Should filter replication rules by status', async(()=>{ - fixture.detectChanges(); - fixture.whenStable().then(()=>{ - fixture.detectChanges(); - comp.doFilterRuleStatus('1' /*Enabled*/); - fixture.detectChanges(); - let el: HTMLElement = deRules.nativeElement; - fixture.detectChanges(); - expect(el).toBeTruthy(); - expect(el.textContent.trim()).toEqual('sync_02'); - }); - })); - it('Should filter replication jobs by keywords', async(()=>{ fixture.detectChanges(); fixture.whenStable().then(()=>{ diff --git a/src/ui_ng/lib/src/replication/replication.component.ts b/src/ui_ng/lib/src/replication/replication.component.ts index 9e6ce673f..96a52d7e3 100644 --- a/src/ui_ng/lib/src/replication/replication.component.ts +++ b/src/ui_ng/lib/src/replication/replication.component.ts @@ -88,6 +88,8 @@ export class ReplicationComponent implements OnInit, OnDestroy { @Input() readonly: boolean; @Output() redirect = new EventEmitter(); + @Output() openCreateRule = new EventEmitter(); + @Output() openEdit = new EventEmitter(); search: SearchOption = new SearchOption(); @@ -111,8 +113,8 @@ export class ReplicationComponent implements OnInit, OnDestroy { @ViewChild(ListReplicationRuleComponent) listReplicationRule: ListReplicationRuleComponent; - @ViewChild(CreateEditRuleComponent) - createEditPolicyComponent: CreateEditRuleComponent; +/* @ViewChild(CreateEditRuleComponent) + createEditPolicyComponent: CreateEditRuleComponent;*/ @ViewChild("replicationLogViewer") replicationLogViewer: JobLogViewerComponent; @@ -134,9 +136,6 @@ export class ReplicationComponent implements OnInit, OnDestroy { private translateService: TranslateService) { } - public get creationAvailable(): boolean { - return !this.readonly && this.projectId ? true : false; - } public get showPaginationIndex(): boolean { return this.totalCount > 0; @@ -155,7 +154,7 @@ export class ReplicationComponent implements OnInit, OnDestroy { } openModal(): void { - this.createEditPolicyComponent.openCreateEditRule(true); + this.openCreateRule.emit(); } openEditRule(rule: ReplicationRule) { @@ -164,7 +163,7 @@ export class ReplicationComponent implements OnInit, OnDestroy { if (rule.enabled === 1) { editable = false; } - this.createEditPolicyComponent.openCreateEditRule(editable, rule.id); + this.openEdit.emit(rule.id); } } @@ -260,6 +259,14 @@ export class ReplicationComponent implements OnInit, OnDestroy { } } + replicateManualRule(rule: ReplicationRule): void { + toPromise(this.replicationService.replicateRule(rule.id)) + .then(response => { + this.refreshJobs(); + }) + .catch(error => this.errorHandler.error(error)); + } + customRedirect(rule: ReplicationRule) { this.redirect.emit(rule); } @@ -269,14 +276,6 @@ export class ReplicationComponent implements OnInit, OnDestroy { this.listReplicationRule.retrieveRules(ruleName); } - doFilterRuleStatus($event: any) { - if ($event && $event.target && $event.target["value"]) { - let status = $event.target["value"]; - this.currentRuleStatus = this.ruleStatus.find((r: any) => r.key === status); - this.listReplicationRule.filterRuleStatus(this.currentRuleStatus.key); - } - } - doFilterJobStatus($event: any) { if ($event && $event.target && $event.target["value"]) { let status = $event.target["value"]; diff --git a/src/ui_ng/lib/src/service/replication.service.ts b/src/ui_ng/lib/src/service/replication.service.ts index 9d722798c..c0f3bce2a 100644 --- a/src/ui_ng/lib/src/service/replication.service.ts +++ b/src/ui_ng/lib/src/service/replication.service.ts @@ -97,6 +97,9 @@ export abstract class ReplicationService { */ abstract disableReplicationRule(ruleId: number | string): Observable | Promise | any; + + abstract replicateRule(ruleId: number | string): Observable | Promise | any; + /** * Get the jobs for the specified replication rule. * Set query parameters through 'queryParams', support: @@ -137,6 +140,7 @@ export abstract class ReplicationService { export class ReplicationDefaultService extends ReplicationService { _ruleBaseUrl: string; _jobBaseUrl: string; + _replicateUrl: string; constructor( private http: Http, @@ -147,6 +151,7 @@ export class ReplicationDefaultService extends ReplicationService { config.replicationRuleEndpoint : '/api/policies/replication'; this._jobBaseUrl = config.replicationJobEndpoint ? config.replicationJobEndpoint : '/api/jobs/replication'; + this._replicateUrl = '/api/replications'; } //Private methods @@ -216,6 +221,17 @@ export class ReplicationDefaultService extends ReplicationService { .catch(error => Promise.reject(error)); } + public replicateRule(ruleId: number | string): Observable | Promise | any { + if (!ruleId) { + return Promise.reject("Bad argument"); + } + + let url: string = `${this._replicateUrl}`; + return this.http.post(url, {policy_id: ruleId}, HTTP_JSON_OPTIONS).toPromise() + .then(response => response) + .catch(error => Promise.reject(error)); + } + public enableReplicationRule(ruleId: number | string, enablement: number): Observable | Promise | any { if (!ruleId || ruleId <= 0) { return Promise.reject('Bad argument'); diff --git a/src/ui_ng/package.json b/src/ui_ng/package.json index 66b983129..7ee2c24de 100644 --- a/src/ui_ng/package.json +++ b/src/ui_ng/package.json @@ -31,7 +31,7 @@ "clarity-icons": "^0.10.17", "clarity-ui": "^0.10.17", "core-js": "^2.4.1", - "harbor-ui": "0.6.8", + "harbor-ui": "0.6.9", "intl": "^1.2.5", "mutationobserver-shim": "^0.3.2", "ngx-cookie": "^1.0.0", diff --git a/src/ui_ng/src/app/harbor-routing.module.ts b/src/ui_ng/src/app/harbor-routing.module.ts index 934e914ca..6ff812f29 100644 --- a/src/ui_ng/src/app/harbor-routing.module.ts +++ b/src/ui_ng/src/app/harbor-routing.module.ts @@ -50,6 +50,8 @@ import { LeavingConfigRouteDeactivate } from './shared/route/leaving-config-deac import { MemberGuard } from './shared/route/member-guard-activate.service'; import { TagDetailPageComponent } from './repository/tag-detail/tag-detail-page.component'; +import { ReplicationRuleComponent} from "./replication/replication-rule/replication-rule.component"; +import {LeavingNewRuleRouteDeactivate} from "./shared/route/leaving-new-rule-deactivate.service"; const harborRoutes: Routes = [ { path: '', redirectTo: 'harbor', pathMatch: 'full' }, @@ -87,6 +89,21 @@ const harborRoutes: Routes = [ path: 'replications', component: TotalReplicationPageComponent, canActivate: [SystemAdminGuard], + canActivateChild: [SystemAdminGuard], + }, + { + path: 'replications/:id/rule', + component: ReplicationRuleComponent, + canActivate: [SystemAdminGuard], + canActivateChild: [SystemAdminGuard], + canDeactivate: [LeavingNewRuleRouteDeactivate] + }, + { + path: 'replications/new-rule', + component: ReplicationRuleComponent, + canActivate: [SystemAdminGuard], + canActivateChild: [SystemAdminGuard], + canDeactivate: [LeavingNewRuleRouteDeactivate] }, { path: 'tags/:id/:repo', @@ -127,7 +144,6 @@ const harborRoutes: Routes = [ { path: 'replications', component: ReplicationPageComponent, - canActivate: [SystemAdminGuard] }, { path: 'members', @@ -148,6 +164,12 @@ const harborRoutes: Routes = [ component: ConfigurationComponent, canActivate: [SystemAdminGuard], canDeactivate: [LeavingConfigRouteDeactivate] + }, + { + path: 'registry', + component: DestinationPageComponent, + canActivate: [SystemAdminGuard], + canActivateChild: [SystemAdminGuard], } ] }, diff --git a/src/ui_ng/src/app/project/project-detail/project-detail.component.html b/src/ui_ng/src/app/project/project-detail/project-detail.component.html index 3feaa5d1c..9f6cf0939 100644 --- a/src/ui_ng/src/app/project/project-detail/project-detail.component.html +++ b/src/ui_ng/src/app/project/project-detail/project-detail.component.html @@ -13,7 +13,7 @@ -