Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass previous-image to analyzer #1279

Merged
merged 4 commits into from
Oct 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions internal/build/lifecycle_execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,14 @@ func (l *LifecycleExecution) PlatformAPI() *api.Version {
return l.platformAPI
}

func (l *LifecycleExecution) ImageName() name.Reference {
return l.opts.Image
}

func (l *LifecycleExecution) PrevImageName() string {
return l.opts.PreviousImage
}

func (l *LifecycleExecution) Run(ctx context.Context, phaseFactoryCreator PhaseFactoryCreator) error {
phaseFactory := phaseFactoryCreator(l)
var buildCache Cache
Expand Down Expand Up @@ -352,6 +360,31 @@ func (l *LifecycleExecution) newAnalyze(repoName, networkMode string, publish bo
flagsOpt = WithFlags("-gid", strconv.Itoa(l.opts.GID))
}

if l.opts.PreviousImage != "" {
if l.opts.Image == nil {
return nil, errors.New("image can't be nil")
}

image, err := name.ParseReference(l.opts.Image.Name(), name.WeakValidation)
if err != nil {
return nil, fmt.Errorf("invalid image name: %s", err)
}

prevImage, err := name.ParseReference(l.opts.PreviousImage, name.WeakValidation)
if err != nil {
return nil, fmt.Errorf("invalid previous image name: %s", err)
}
if publish {
if image.Context().RegistryStr() != prevImage.Context().RegistryStr() {
return nil, fmt.Errorf(`when --publish is used, <previous-image> must be in the same image registry as <image>
image registry = %s
previous-image registry = %s`, image.Context().RegistryStr(), prevImage.Context().RegistryStr())
}
}

l.opts.Image = prevImage
}

if publish {
authConfig, err := auth.BuildEnvVar(authn.DefaultKeychain, repoName)
if err != nil {
Expand Down
104 changes: 102 additions & 2 deletions internal/build/lifecycle_execution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -840,7 +840,7 @@ func testLifecycleExecution(t *testing.T, when spec.G, it spec.S) {
})
})

when("-previous-image is used", func() {
when("-previous-image is used and builder is trusted", func() {
when("image is invalid", func() {
it("errors", func() {
var imageName name.Tag
Expand Down Expand Up @@ -872,7 +872,7 @@ func testLifecycleExecution(t *testing.T, when spec.G, it spec.S) {
})

when("--publish is false", func() {
it("passes previous-image to creator", func() {
it("successfully passes previous-image to creator", func() {
var imageName name.Tag
imageName, err := name.NewTag("/some/image", name.WeakValidation)
h.AssertNil(t, err)
Expand Down Expand Up @@ -1399,6 +1399,106 @@ func testLifecycleExecution(t *testing.T, when spec.G, it spec.S) {
})
})
})

when("previous-image is used and builder is untrusted", func() {
when("image is invalid", func() {
it("errors", func() {
var imageName name.Tag
imageName, err := name.NewTag("/x/y/?!z", name.WeakValidation)
h.AssertError(t, err, "repository can only contain the runes `abcdefghijklmnopqrstuvwxyz0123456789_-./`")
lifecycle := newTestLifecycleExec(t, true, func(options *build.LifecycleOptions) {
options.Image = imageName
options.PreviousImage = "previous-image"
})
fakePhaseFactory := fakes.NewFakePhaseFactory()

err = lifecycle.Analyze(context.Background(), "test", "test", false, "", false, fakeCache, fakePhaseFactory)
h.AssertError(t, err, "invalid image name")
})
})

when("previous-image is invalid", func() {
it("errors", func() {
var imageName name.Tag
imageName, err := name.NewTag("/some/image", name.WeakValidation)
h.AssertNil(t, err)
lifecycle := newTestLifecycleExec(t, true, func(options *build.LifecycleOptions) {
options.PreviousImage = "%%%"
options.Image = imageName
})
fakePhaseFactory := fakes.NewFakePhaseFactory()
err = lifecycle.Analyze(context.Background(), "test", "test", false, "", false, fakeCache, fakePhaseFactory)
h.AssertError(t, err, "invalid previous image name")
})
})

when("--publish is false", func() {
it("successfully passes previous-image to analyzer", func() {
var imageName name.Tag
imageName, err := name.NewTag("/some/image", name.WeakValidation)
h.AssertNil(t, err)
lifecycle := newTestLifecycleExec(t, true, func(options *build.LifecycleOptions) {
options.PreviousImage = "previous-image"
options.Image = imageName
})
prevImage, err := name.ParseReference(lifecycle.PrevImageName(), name.WeakValidation)
h.AssertNil(t, err)

fakePhaseFactory := fakes.NewFakePhaseFactory()
err = lifecycle.Analyze(context.Background(), "test", "test", false, "", false, fakeCache, fakePhaseFactory)
h.AssertNil(t, err)

lastCallIndex := len(fakePhaseFactory.NewCalledWithProvider) - 1
h.AssertNotEq(t, lastCallIndex, -1)

configProvider := fakePhaseFactory.NewCalledWithProvider[lastCallIndex]
h.AssertEq(t, configProvider.Name(), "analyzer")
h.AssertEq(t, lifecycle.ImageName().Name(), prevImage.Name())
})
})

when("--publish is true", func() {
when("previous-image and image are in the same registry", func() {
it("successfully passes previous-image to analyzer", func() {
var imageName name.Tag
imageName, err := name.NewTag("/some/image", name.WeakValidation)
h.AssertNil(t, err)
lifecycle := newTestLifecycleExec(t, true, func(options *build.LifecycleOptions) {
options.PreviousImage = "index.docker.io/some/previous:latest"
options.Image = imageName
})
prevImage, err := name.ParseReference(lifecycle.PrevImageName(), name.WeakValidation)
h.AssertNil(t, err)

fakePhaseFactory := fakes.NewFakePhaseFactory()
err = lifecycle.Analyze(context.Background(), "test", "test", true, "", false, fakeCache, fakePhaseFactory)
h.AssertNil(t, err)

lastCallIndex := len(fakePhaseFactory.NewCalledWithProvider) - 1
h.AssertNotEq(t, lastCallIndex, -1)

configProvider := fakePhaseFactory.NewCalledWithProvider[lastCallIndex]
h.AssertEq(t, configProvider.Name(), "analyzer")
h.AssertEq(t, lifecycle.ImageName().Name(), prevImage.Name())
})
})

when("previous-image and image are not in the same registry", func() {
it("errors", func() {
var imageName name.Tag
imageName, err := name.NewTag("/some/image", name.WeakValidation)
h.AssertNil(t, err)
lifecycle := newTestLifecycleExec(t, true, func(options *build.LifecycleOptions) {
options.PreviousImage = "example.io/some/previous:latest"
options.Image = imageName
})
fakePhaseFactory := fakes.NewFakePhaseFactory()
err = lifecycle.Analyze(context.Background(), "test", "test", true, "", false, fakeCache, fakePhaseFactory)
h.AssertError(t, err, fmt.Sprintf("%s", err))
})
})
})
})
})

when("#Restore", func() {
Expand Down
30 changes: 27 additions & 3 deletions internal/commands/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -714,9 +714,33 @@ builder = "my-builder"
})
})

when("previous-image flag is provided", func() {
when.Focus("previous-image flag is provided", func() {
when("image is invalid", func() {
it("error must be thrown", func() {
mockClient.EXPECT().
Build(gomock.Any(), EqBuildOptionsWithPreviousImage("previous-image")).
Return(errors.New(""))

command.SetArgs([]string{"--builder", "my-builder", "/x@/y/?!z", "--previous-image", "previous-image"})
err := command.Execute()
h.AssertError(t, err, "failed to build")
})
})

when("previous-image is invalid", func() {
it("error must be thrown", func() {
mockClient.EXPECT().
Build(gomock.Any(), EqBuildOptionsWithPreviousImage("%%%")).
Return(errors.New(""))

command.SetArgs([]string{"--builder", "my-builder", "image", "--previous-image", "%%%"})
err := command.Execute()
h.AssertError(t, err, "failed to build")
})
})

when("--publish is false", func() {
it("previous-image should be passed to creator", func() {
it("previous-image should be passed to builder", func() {
mockClient.EXPECT().
Build(gomock.Any(), EqBuildOptionsWithPreviousImage("previous-image")).
Return(nil)
Expand All @@ -728,7 +752,7 @@ builder = "my-builder"

when("--publish is true", func() {
when("image and previous-image are in same registry", func() {
it("succeeds", func() {
it("previous-image should be passed to builder", func() {
mockClient.EXPECT().
Build(gomock.Any(), EqBuildOptionsWithPreviousImage("index.docker.io/some/previous:latest")).
Return(nil)
Expand Down