Skip to content

Commit

Permalink
modified scan images
Browse files Browse the repository at this point in the history
  • Loading branch information
nvnyale committed May 2, 2023
1 parent 4333cc6 commit e7fde42
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 34 deletions.
26 changes: 18 additions & 8 deletions api/handlers_repositories.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,21 +345,21 @@ func (s *server) ScanRepositoriesHandler(w http.ResponseWriter, r *http.Request)
handleError(w, err)
return
}
scannedImageIds := make(map[string][]string)
scanCount := 0

for _, repository := range repositories {
images, err := service.ListImages(r.Context(), repository)

images, err := service.GetImages(r.Context(), repository)
if err != nil {
handleError(w, err)
return
}

for _, image := range images {
imageScanFindings, err := service.GetImageScanFindingsByImageDigest(r.Context(), repository, *image.ImageDigest)
if err != nil {
handleError(w, err)
return
}
if imageScanFindings != nil && time.Now().UTC().Sub(*imageScanFindings.ImageScanCompletedAt) > 24*time.Hour {
if image.ImageScanFindingsSummary != nil && time.Now().UTC().Sub(*image.ImageScanFindingsSummary.ImageScanCompletedAt) > 24*time.Hour {
scanCount++
scannedImageIds[repository] = append(scannedImageIds[repository], aws.StringValue(image.ImageDigest))
err = service.ScanImage(r.Context(), image, repository)
if err != nil {
handleError(w, err)
Expand All @@ -369,7 +369,17 @@ func (s *server) ScanRepositoriesHandler(w http.ResponseWriter, r *http.Request)
}

}
message := "All images already scanned in the past 24 hours"
if scanCount != 0 {
message = fmt.Sprintf(
"Scan initiated for %d images", scanCount,
)
}

w.WriteHeader(http.StatusOK)
w.Write([]byte("scan initiated"))
data, _ := json.Marshal(map[string]any{
"message": message,
"repositories": scannedImageIds,
})
w.Write(data)
}
24 changes: 0 additions & 24 deletions ecr/images.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,30 +52,6 @@ func (e *ECR) GetImages(ctx context.Context, repoName string, imageIds ...*ecr.I
return out.ImageDetails, nil
}

// GetImageScanFindingsByImageDigest gets the scan findings for an image digest
func (e *ECR) GetImageScanFindingsByImageDigest(ctx context.Context, repoName, digest string) (*ecr.ImageScanFindings, error) {
if repoName == "" || digest == "" {
return nil, apierror.New(apierror.ErrBadRequest, "invalid input", nil)
}

log.Infof("getting image scan findings for %s:%s", repoName, digest)

out, err := e.Service.DescribeImageScanFindingsWithContext(ctx, &ecr.DescribeImageScanFindingsInput{
ImageId: &ecr.ImageIdentifier{
ImageDigest: aws.String(digest),
},
MaxResults: aws.Int64(1000),
RepositoryName: aws.String(repoName),
})

if err != nil {
return nil, ErrCode("failed to get image scan findings", err)
}

log.Debugf("got output from image scan findings %+v", out)

return out.ImageScanFindings, nil
}

// GetImageScanFindings gets the scan findings for an image tag
func (e *ECR) GetImageScanFindings(ctx context.Context, repoName, tag string) (*ecr.ImageScanFindings, error) {
Expand Down
6 changes: 4 additions & 2 deletions ecr/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,9 +219,11 @@ func (e *ECR) GetRepositoryPolicy(ctx context.Context, repoName string) (string,
return aws.StringValue(out.PolicyText), nil
}

func (e *ECR) ScanImage(ctx context.Context, imageDetails *ecr.ImageIdentifier, repository string) error {
func (e *ECR) ScanImage(ctx context.Context, imageDetails *ecr.ImageDetail, repository string) error {
_, err := e.Service.StartImageScanWithContext(ctx, &ecr.StartImageScanInput{
ImageId: imageDetails,
ImageId: &ecr.ImageIdentifier{
ImageDigest: imageDetails.ImageDigest,
},
RepositoryName: &repository,
})
if err != nil {
Expand Down

0 comments on commit e7fde42

Please sign in to comment.