Skip to content

Commit

Permalink
Skip allowed prefixes before parsing (#340) (#341)
Browse files Browse the repository at this point in the history
  • Loading branch information
kwiatekus authored Nov 19, 2024
1 parent 87d71c7 commit 87514db
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
11 changes: 6 additions & 5 deletions internal/validate/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ func NewImageValidator(sc *ServiceConfig, notaryClientFactory RepoFactory) Image
func (s *notaryService) Validate(ctx context.Context, image string, imagePullCredentials map[string]cliType.AuthConfig) error {
logger := helpers.LoggerFromCtx(ctx).With("image", image)
ctx = helpers.LoggerToContext(ctx, logger)

if allowed := s.isImageAllowed(image); allowed {
logger.Info("image validation skipped, because it's allowed")
return nil
}

split := strings.Split(image, tagDelim)

if len(split) != 2 {
Expand All @@ -73,11 +79,6 @@ func (s *notaryService) Validate(ctx context.Context, image string, imagePullCre
imgRepo := split[0]
imgTag := split[1]

if allowed := s.isImageAllowed(imgRepo); allowed {
logger.Info("image validation skipped, because it's allowed")
return nil
}

expectedShaBytes, err := s.loggedGetNotaryImageDigestHash(ctx, imgRepo, imgTag)
if err != nil {
return err
Expand Down
14 changes: 14 additions & 0 deletions internal/validate/image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,20 @@ func Test_Validate_ImageWhichIsNotInNotaryButIsInAllowedList_ShouldPass(t *testi
"some-registry/allowed-image-3",
},
},
{
name: "image name has no colon delimiters but is allowed anyway",
imageName: "nginx",
allowedRegistries: []string{
"nginx",
},
},
{
name: "image name has two colon delimiters but is allowed anyway",
imageName: "public.ecr.aws/dynatrace/dynatrace-operator:v1.3.2@sha256:f8ecdcd87d7d84b87e645074084dd7f57dd62c76e120bb21e5abde158755be56",
allowedRegistries: []string{
"public.ecr.aws/dynatrace/dynatrace-operator",
},
},
}
f := &mocks.RepoFactory{}
f.On("NewRepoClient", mock.Anything, mock.Anything).Return(nil, errors.New("Should be called"))
Expand Down

0 comments on commit 87514db

Please sign in to comment.