Merge pull request #10837 from heww/scan-api

feat(scan): add scan API in v2.0
This commit is contained in:
Wenkai Yin(尹文开) 2020-02-25 14:45:19 +08:00 committed by GitHub
commit 5f9c976e95
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 57 additions and 0 deletions

View File

@ -330,6 +330,29 @@ paths:
$ref: '#/responses/404'
'500':
$ref: '#/responses/500'
/projects/{project_name}/repositories/{repository_name}/artifacts/{reference}/scan:
post:
summary: Scan the artifact
description: Scan the specified artifact
tags:
- artifact
operationId: scanArtifact
parameters:
- $ref: '#/parameters/requestId'
- $ref: '#/parameters/projectName'
- $ref: '#/parameters/repositoryName'
- $ref: '#/parameters/reference'
responses:
'202':
$ref: '#/responses/202'
'401':
$ref: '#/responses/401'
'403':
$ref: '#/responses/403'
'404':
$ref: '#/responses/404'
'500':
$ref: '#/responses/500'
/projects/{project_name}/repositories/{repository_name}/artifacts/{reference}/tags:
post:
summary: Create tag
@ -543,6 +566,12 @@ responses:
X-Request-Id:
description: The ID of the corresponding request for the response
type: string
'202':
description: Accepted
headers:
X-Request-Id:
description: The ID of the corresponding request for the response
type: string
'400':
description: Bad request
headers:

View File

@ -27,11 +27,13 @@ import (
"github.com/goharbor/harbor/src/api/artifact"
"github.com/goharbor/harbor/src/api/artifact/abstractor/resolver"
"github.com/goharbor/harbor/src/api/repository"
"github.com/goharbor/harbor/src/api/scan"
"github.com/goharbor/harbor/src/common/rbac"
"github.com/goharbor/harbor/src/common/utils"
ierror "github.com/goharbor/harbor/src/internal/error"
"github.com/goharbor/harbor/src/pkg/project"
"github.com/goharbor/harbor/src/pkg/q"
v1 "github.com/goharbor/harbor/src/pkg/scan/rest/v1"
"github.com/goharbor/harbor/src/server/v2.0/handler/assembler"
"github.com/goharbor/harbor/src/server/v2.0/handler/model"
operation "github.com/goharbor/harbor/src/server/v2.0/restapi/operations/artifact"
@ -47,6 +49,7 @@ func newArtifactAPI() *artifactAPI {
artCtl: artifact.Ctl,
proMgr: project.Mgr,
repoCtl: repository.Ctl,
scanCtl: scan.DefaultController,
}
}
@ -55,6 +58,7 @@ type artifactAPI struct {
artCtl artifact.Controller
proMgr project.Manager
repoCtl repository.Controller
scanCtl scan.Controller
}
func (a *artifactAPI) ListArtifacts(ctx context.Context, params operation.ListArtifactsParams) middleware.Responder {
@ -176,6 +180,30 @@ func (a *artifactAPI) CopyArtifact(ctx context.Context, params operation.CopyArt
return operation.NewCopyArtifactCreated()
}
func (a *artifactAPI) ScanArtifact(ctx context.Context, params operation.ScanArtifactParams) middleware.Responder {
if err := a.RequireProjectAccess(ctx, params.ProjectName, rbac.ActionCreate, rbac.ResourceScan); err != nil {
return a.SendError(ctx, err)
}
repository := fmt.Sprintf("%s/%s", params.ProjectName, params.RepositoryName)
artifact, err := a.artCtl.GetByReference(ctx, repository, params.Reference, nil)
if err != nil {
return a.SendError(ctx, err)
}
art := &v1.Artifact{
NamespaceID: artifact.ProjectID,
Repository: repository,
Digest: artifact.Digest,
MimeType: artifact.ManifestMediaType,
}
if err := a.scanCtl.Scan(art); err != nil {
return a.SendError(ctx, err)
}
return operation.NewScanArtifactAccepted()
}
// parse "repository:tag" or "repository@digest" into repository and reference parts
func parse(s string) (string, string, error) {
matches := reference.ReferenceRegexp.FindStringSubmatch(s)