Skip to content

Commit

Permalink
fix: check if base image is nil
Browse files Browse the repository at this point in the history
Signed-off-by: felipecruz91 <[email protected]>
  • Loading branch information
felipecruz91 committed Feb 16, 2024
1 parent 21141f3 commit 6f7be4c
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 1 deletion.
2 changes: 1 addition & 1 deletion policy/policy_handler/mocks/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func BuildLocalEvalMocks(ctx context.Context, req skill.RequestContext, sb *type
}

// Base image
if sb.Source.Provenance == nil {
if sb.Source.Provenance == nil || sb.Source.Provenance.BaseImage == nil {
req.Log.Info("Skipping base image mock, no provenance in SBOM")
} else {
baseImageMock := MockBaseImage(sb)
Expand Down
101 changes: 101 additions & 0 deletions policy/policy_handler/mocks/image_details_by_digest.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package mocks

import (
"context"

"github.com/atomist-skills/go-skill"
"github.com/atomist-skills/go-skill/policy/types"
)

const (
ImageDetailsQueryName = "image-details-by-digest"

ImageDetailsQuery = `
query ($context: Context!, $digest: String!, $platform: ImagePlatform!) {
imageDetailsByDigest(
context: $context
digest: $digest
platform: $platform
) {
digest
baseImage {
digest
repository {
hostName
repoName
}
tags {
name
current
}
}
baseImageTag {
name
current
}
}
}
`
)

type (
GqlImagePlatform struct {
Architecture string `json:"architecture"`
Os string `json:"os"`
Variant string `json:"variant,omitempty"`
}

Repository struct {
HostName string `json:"hostName" edn:"hostName"`
RepoName string `json:"repoName" edn:"repoName"`
}

Tag struct {
Name string `json:"name" edn:"name"`
Current bool `json:"current" edn:"current"`
}

BaseImage struct {
Digest string `json:"digest" edn:"digest"`
Repository Repository `json:"repository" edn:"repository"`
Tags []Tag `json:"tags" edn:"tags"`
}

ImageDetailsByDigest struct {
Digest string `json:"digest" edn:"digest"`
BaseImage *BaseImage `json:"baseImage" edn:"baseImage"`
BaseImageTag *Tag `json:"baseImageTag" edn:"baseImageTag"`
}

ImageDetailsByDigestResponse struct {
ImageDetailsByDigest *ImageDetailsByDigest `json:"imageDetailsByDigest" edn:"imageDetailsByDigest"`
}
)

func MockImageDetailsByDigest(ctx context.Context, req skill.RequestContext, sb *types.SBOM) (ImageDetailsByDigestResponse, error) {
req.Log.Info("Building local evaluation mocks for image details by digest")

var tags []Tag
for _, t := range sb.Source.Image.Details.Tags {
tags = append(tags, Tag{
Name: t.Name,
Current: t.Current,
})
}

repo := parseFromReference(sb.Source.Provenance.BaseImage.Name)

return ImageDetailsByDigestResponse{
ImageDetailsByDigest: &ImageDetailsByDigest{
Digest: sb.Source.Image.Digest,
BaseImage: &BaseImage{
Digest: sb.Source.Provenance.BaseImage.Digest,
Repository: Repository{
HostName: repo.Host,
RepoName: repo.Repository,
},
Tags: tags,
},
},
}, nil
}

0 comments on commit 6f7be4c

Please sign in to comment.