Skip to content

Commit

Permalink
add validation for registry/repo/image being passed
Browse files Browse the repository at this point in the history
Signed-off-by: Harsh Thakur <[email protected]>
  • Loading branch information
RealHarshThakur committed Sep 9, 2023
1 parent 0239690 commit 66fcf39
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
11 changes: 9 additions & 2 deletions pkg/patch/patch.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,15 @@ func patchedImageTarget(image, patchedTag string) (*string, error) {
patchedTag = fmt.Sprintf("%s-%s", tag, defaultPatchedTagSuffix)
}
}
// this implies user has passed a destination image name, not just a tag
if strings.Contains(patchedTag, "/") {

slashCount := strings.Count(patchedTag, "/")
if slashCount > 0 {
if slashCount < 2 {
err := fmt.Errorf("invalid tag %s, must be in the form <registry>/<image>:<tag>", patchedTag)
log.Error(err)
return nil, err
}
// this implies user has passed a destination image name, not just a tag
patchedImageName = patchedTag
} else {
patchedImageName = fmt.Sprintf("%s:%s", imageName.Name(), patchedTag)
Expand Down
17 changes: 13 additions & 4 deletions pkg/patch/patch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,17 @@ func TestPatchedImageTarget(t *testing.T) {
wantErr: false,
},
{
name: "tag passed and contains a slash(indicating registry)",
name: "tag passed but without registry or repo",
image: "docker.io/library/nginx:1.21.3",
patchedTag: "my.registry/nginx:1.21.3-patched",
want: "my.registry/nginx:1.21.3-patched",
want: "",
wantErr: true,
},
{
name: "tag passed containg registry, repo and image",
image: "docker.io/library/nginx:1.21.3",
patchedTag: "my.registry.io/myrepo/nginx:1.21.3-patched",
want: "my.registry.io/myrepo/nginx:1.21.3-patched",
wantErr: false,
},
}
Expand All @@ -89,8 +96,10 @@ func TestPatchedImageTarget(t *testing.T) {
t.Errorf("patchedImageTarget() error = %v, wantErr %v", err, tt.wantErr)
return
}
if *got != tt.want {
t.Errorf("patchedImageTarget() = %v, want %v", *got, tt.want)
if got != nil {
if *got != tt.want {
t.Errorf("patchedImageTarget() = %v, want %v", *got, tt.want)
}
}
})
}
Expand Down

0 comments on commit 66fcf39

Please sign in to comment.