diff --git a/docs/swagger.yaml b/docs/swagger.yaml index f77c615f2..462d9242f 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -1809,6 +1809,18 @@ definitions: type: integer format: int description: The public status of the project. + enable_content_trust: + type: boolean + description: Whether content trust is enabled or not. If it is enabled, user cann't pull unsigned images from this project. + prevent_vulnerable_images_from_running: + type: boolean + description: Whether prevent the vulnerable images from running. + prevent_vulnerable_images_from_running_severity: + type: string + description: If the vulnerability is high than severity defined here, the images cann't be pulled. + automatically_scan_images_on_push: + type: boolean + description: Whether scan images automatically when pushing. Project: type: object properties: @@ -1849,6 +1861,18 @@ definitions: repo_count: type: integer description: The number of the repositories under this project. + enable_content_trust: + type: boolean + description: Whether content trust is enabled or not. If it is enabled, user cann't pull unsigned images from this project. + prevent_vulnerable_images_from_running: + type: boolean + description: Whether prevent the vulnerable images from running. + prevent_vulnerable_images_from_running_severity: + type: string + description: If the vulnerability is high than severity defined here, the images cann't be pulled. + automatically_scan_images_on_push: + type: boolean + description: Whether scan images automatically when pushing. Manifest: type: object properties: diff --git a/src/jobservice/replication/transfer.go b/src/jobservice/replication/transfer.go index 0a438b230..5bd5d9593 100644 --- a/src/jobservice/replication/transfer.go +++ b/src/jobservice/replication/transfer.go @@ -27,7 +27,6 @@ import ( "github.com/docker/distribution" "github.com/docker/distribution/manifest/schema1" "github.com/docker/distribution/manifest/schema2" - "github.com/vmware/harbor/src/common/dao" "github.com/vmware/harbor/src/common/models" comutils "github.com/vmware/harbor/src/common/utils" "github.com/vmware/harbor/src/common/utils/log" @@ -182,13 +181,13 @@ func (c *Checker) Enter() (string, error) { } func (c *Checker) enter() (string, error) { - project, err := dao.GetProjectByName(c.project) + project, err := getProject(c.project) if err != nil { - c.logger.Errorf("an error occurred while getting project %s in DB: %v", c.project, err) - return "", err + c.logger.Infof("failed to get project %s from %s: %v", c.project, c.srcURL, err) + return "", nil } - err = c.createProject(project.Public) + err = c.createProject(project) if err == nil { c.logger.Infof("project %s is created on %s with user %s", c.project, c.dstURL, c.dstUsr) return StatePullManifest, nil @@ -207,16 +206,68 @@ func (c *Checker) enter() (string, error) { return "", err } -func (c *Checker) createProject(public int) error { - project := struct { - ProjectName string `json:"project_name"` - Public int `json:"public"` - }{ - ProjectName: c.project, - Public: public, +func getProject(name string) (*models.Project, error) { + req, err := http.NewRequest(http.MethodGet, buildProjectURL(), nil) + if err != nil { + return nil, err } - data, err := json.Marshal(project) + req.URL.Query().Set("name", name) + req.URL.Query().Encode() + req.AddCookie(&http.Cookie{ + Name: models.UISecretCookie, + Value: config.JobserviceSecret(), + }) + + client := &http.Client{} + resp, err := client.Do(req) + if err != nil { + return nil, err + } + defer resp.Body.Close() + + data, err := ioutil.ReadAll(resp.Body) + if err != nil { + return nil, err + } + + list := []*models.Project{} + if err = json.Unmarshal(data, list); err != nil { + return nil, err + } + + var project *models.Project + for _, p := range list { + if p.Name == name { + project = p + break + } + } + if project == nil { + return nil, fmt.Errorf("project %s not found", name) + } + + return project, nil +} + +func (c *Checker) createProject(project *models.Project) error { + pro := struct { + Name string `json:"project_name"` + Public int `json:"public"` + EnableContentTrust bool `json:"enable_content_trust"` + PreventVulnerableImagesFromRunning bool `json:"prevent_vulnerable_images_from_running"` + PreventVulnerableImagesFromRunningSeverity string `json:"prevent_vulnerable_images_from_running_severity"` + AutomaticallyScanImagesOnPush bool `json:"automatically_scan_images_on_push"` + }{ + Name: project.Name, + Public: project.Public, + EnableContentTrust: project.EnableContentTrust, + PreventVulnerableImagesFromRunning: project.PreventVulnerableImagesFromRunning, + PreventVulnerableImagesFromRunningSeverity: project.PreventVulnerableImagesFromRunningSeverity, + AutomaticallyScanImagesOnPush: project.AutomaticallyScanImagesOnPush, + } + + data, err := json.Marshal(pro) if err != nil { return err } @@ -263,6 +314,10 @@ func (c *Checker) createProject(public int) error { c.project, c.dstURL, c.dstUsr, resp.StatusCode, string(message)) } +func buildProjectURL() string { + return strings.TrimRight(config.LocalUIURL(), "/") + "/api/projects/" +} + // ManifestPuller pulls the manifest of a tag. And if no tag needs to be pulled, // the next state that state machine should enter is "finished". type ManifestPuller struct { diff --git a/src/ui/api/project.go b/src/ui/api/project.go index a06be9e69..bdd29e940 100644 --- a/src/ui/api/project.go +++ b/src/ui/api/project.go @@ -37,8 +37,12 @@ type ProjectAPI struct { } type projectReq struct { - ProjectName string `json:"project_name"` - Public int `json:"public"` + ProjectName string `json:"project_name"` + Public int `json:"public"` + EnableContentTrust bool `json:"enable_content_trust"` + PreventVulnerableImagesFromRunning bool `json:"prevent_vulnerable_images_from_running"` + PreventVulnerableImagesFromRunningSeverity string `json:"prevent_vulnerable_images_from_running_severity"` + AutomaticallyScanImagesOnPush bool `json:"automatically_scan_images_on_push"` } const projectNameMaxLen int = 30 @@ -116,9 +120,13 @@ func (p *ProjectAPI) Post() { } projectID, err := p.ProjectMgr.Create(&models.Project{ - Name: pro.ProjectName, - Public: pro.Public, - OwnerName: p.SecurityCtx.GetUsername(), + Name: pro.ProjectName, + Public: pro.Public, + OwnerName: p.SecurityCtx.GetUsername(), + EnableContentTrust: pro.EnableContentTrust, + PreventVulnerableImagesFromRunning: pro.PreventVulnerableImagesFromRunning, + PreventVulnerableImagesFromRunningSeverity: pro.PreventVulnerableImagesFromRunningSeverity, + AutomaticallyScanImagesOnPush: pro.AutomaticallyScanImagesOnPush, }) if err != nil { log.Errorf("Failed to add project, error: %v", err)