Skip to content
This repository has been archived by the owner on Mar 16, 2024. It is now read-only.

Commit

Permalink
Fix: Add image tag validation prior to building the image (#1875)
Browse files Browse the repository at this point in the history
Signed-off-by: pratikjagrut <26519653+pratikjagrut@users.noreply.github.com>
  • Loading branch information
pratikjagrut committed Jul 10, 2023
1 parent 05d349b commit d61ef24
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 0 deletions.
14 changes: 14 additions & 0 deletions pkg/cli/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cli

import (
"fmt"
"strings"

cli "github.com/acorn-io/runtime/pkg/cli/builder"
"github.com/acorn-io/runtime/pkg/client"
Expand Down Expand Up @@ -39,12 +40,25 @@ func (s *Build) Run(cmd *cobra.Command, args []string) error {
if s.Push && (len(s.Tag) == 0 || s.Tag[0] == "") {
return fmt.Errorf("--push must be used with --tag")
}

c, err := s.client.CreateDefault()
if err != nil {
return err
}

// Check if we can parse the name/tag
for _, tag := range s.Tag {
if strings.HasPrefix(tag, "-") {
return fmt.Errorf("invalid image tag: %v", tag)
}

if _, err := name.ParseReference(tag); err != nil {
return err
}
}

helper := imagesource.NewImageSource(s.File, args, s.Profile, s.Platform, false)

image, _, err := helper.GetImageAndDeployArgs(cmd.Context(), c)
if err != nil {
return err
Expand Down
48 changes: 48 additions & 0 deletions pkg/cli/build_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package cli

import (
"os"
"strings"
"testing"

"github.com/acorn-io/runtime/pkg/cli/testdata"
"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"
)

func TestBuildBadTag(t *testing.T) {
type args struct {
cmd *cobra.Command
args []string
client *testdata.MockClient
}
var _, w, _ = os.Pipe()

test := struct {
name string
args args
wantErr bool
wantOut string
commandContext CommandContext
}{
name: "acorn build --tag -bad-tag",
commandContext: CommandContext{
ClientFactory: &testdata.MockClientFactory{},
StdOut: w,
StdErr: w,
StdIn: strings.NewReader("y\n"),
},
args: args{
args: []string{"--tag", "-bad-tag"},
client: &testdata.MockClient{},
},
wantErr: true,
wantOut: "invalid image tag: -bad-tag",
}
t.Run(test.name, func(t *testing.T) {
test.args.cmd = NewBuild(test.commandContext)
test.args.cmd.SetArgs(test.args.args)
err := test.args.cmd.Execute()
assert.Equal(t, test.wantOut, err.Error())
})
}
4 changes: 4 additions & 0 deletions pkg/server/registry/apigroups/acorn/images/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ func (t *TagStrategy) ImageTag(ctx context.Context, namespace, imageName string,
}
set := sets.NewString(res...)

if strings.HasPrefix(tagToAdd, "-") {
return nil, fmt.Errorf("invalid image tag: %v", tagToAdd)
}

imageRef, err := name.ParseReference(tagToAdd, name.WithDefaultRegistry(""))
if err != nil {
return nil, err
Expand Down

0 comments on commit d61ef24

Please sign in to comment.