Skip to content

Commit ba54b86

Browse files
Support Insecure Registries (#2077)
* Support Insecure Registries Signed-off-by: Prashant Rewar <108176843+prashantrewar@users.noreply.github.com> Signed-off-by: Juan Bustamante <bustamantejj@gmail.com> Signed-off-by: Juan Bustamante <juan.bustamante@broadcom.com> Co-authored-by: Juan Bustamante <juan.bustamante@broadcom.com> Co-authored-by: Juan Bustamante <bustamantejj@gmail.com>
1 parent 5cbbf33 commit ba54b86

11 files changed

Lines changed: 157 additions & 29 deletions

File tree

internal/build/lifecycle_execution.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,12 @@ func (l *LifecycleExecution) Create(ctx context.Context, buildCache, launchCache
382382
flags = append(flags, "-uid", strconv.Itoa(l.opts.UID))
383383
}
384384

385+
if l.platformAPI.AtLeast("0.13") {
386+
for _, reg := range l.opts.InsecureRegistries {
387+
flags = append(flags, "-insecure-registry", reg)
388+
}
389+
}
390+
385391
if l.opts.PreviousImage != "" {
386392
if l.opts.Image == nil {
387393
return errors.New("image can't be nil")
@@ -539,6 +545,12 @@ func (l *LifecycleExecution) Restore(ctx context.Context, buildCache Cache, kani
539545
flags = append(flags, "-uid", strconv.Itoa(l.opts.UID))
540546
}
541547

548+
if l.platformAPI.AtLeast("0.13") {
549+
for _, reg := range l.opts.InsecureRegistries {
550+
flags = append(flags, "-insecure-registry", reg)
551+
}
552+
}
553+
542554
// for kaniko
543555
kanikoCacheBindOp := NullOp()
544556
if (l.platformAPI.AtLeast("0.10") && l.hasExtensionsForBuild()) ||
@@ -646,6 +658,12 @@ func (l *LifecycleExecution) Analyze(ctx context.Context, buildCache, launchCach
646658
flags = append(flags, "-uid", strconv.Itoa(l.opts.UID))
647659
}
648660

661+
if l.platformAPI.AtLeast("0.13") {
662+
for _, reg := range l.opts.InsecureRegistries {
663+
flags = append(flags, "-insecure-registry", reg)
664+
}
665+
}
666+
649667
if l.opts.PreviousImage != "" {
650668
if l.opts.Image == nil {
651669
return errors.New("image can't be nil")
@@ -855,6 +873,12 @@ func (l *LifecycleExecution) Export(ctx context.Context, buildCache, launchCache
855873
flags = append(flags, "-uid", strconv.Itoa(l.opts.UID))
856874
}
857875

876+
if l.platformAPI.AtLeast("0.13") {
877+
for _, reg := range l.opts.InsecureRegistries {
878+
flags = append(flags, "-insecure-registry", reg)
879+
}
880+
}
881+
858882
cacheBindOp := NullOp()
859883
switch buildCache.Type() {
860884
case cache.Image:

internal/build/lifecycle_executor.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ type LifecycleOptions struct {
9494
Network string
9595
AdditionalTags []string
9696
Volumes []string
97+
InsecureRegistries []string
9798
DefaultProcessType string
9899
FileFilter func(string) bool
99100
Workspace string

internal/commands/build.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ type BuildFlags struct {
6161
DateTime string
6262
PreBuildpacks []string
6363
PostBuildpacks []string
64+
InsecureRegistries []string
6465
}
6566

6667
// Build an image from source code
@@ -219,6 +220,7 @@ func Build(logger logging.Logger, cfg config.Config, packClient PackClient) *cob
219220
PreviousInputImage: inputPreviousImage,
220221
LayoutRepoDir: cfg.LayoutRepositoryDir,
221222
},
223+
InsecureRegistries: flags.InsecureRegistries,
222224
}); err != nil {
223225
return errors.Wrap(err, "failed to build")
224226
}
@@ -252,6 +254,7 @@ func buildCommandFlags(cmd *cobra.Command, buildFlags *BuildFlags, cfg config.Co
252254
cmd.Flags().StringVarP(&buildFlags.AppPath, "path", "p", "", "Path to app dir or zip-formatted file (defaults to current working directory)")
253255
cmd.Flags().StringSliceVarP(&buildFlags.Buildpacks, "buildpack", "b", nil, "Buildpack to use. One of:\n a buildpack by id and version in the form of '<buildpack>@<version>',\n path to a buildpack directory (not supported on Windows),\n path/URL to a buildpack .tar or .tgz file, or\n a packaged buildpack image name in the form of '<hostname>/<repo>[:<tag>]'"+stringSliceHelp("buildpack"))
254256
cmd.Flags().StringSliceVarP(&buildFlags.Extensions, "extension", "", nil, "Extension to use. One of:\n an extension by id and version in the form of '<extension>@<version>',\n path to an extension directory (not supported on Windows),\n path/URL to an extension .tar or .tgz file, or\n a packaged extension image name in the form of '<hostname>/<repo>[:<tag>]'"+stringSliceHelp("extension"))
257+
cmd.Flags().StringArrayVar(&buildFlags.InsecureRegistries, "insecure-registry", []string{}, "List of insecure registries (only available for API >= 0.13)")
255258
cmd.Flags().StringVarP(&buildFlags.Builder, "builder", "B", cfg.DefaultBuilder, "Builder image")
256259
cmd.Flags().Var(&buildFlags.Cache, "cache",
257260
`Cache options used to define cache techniques for build process.

internal/commands/build_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -982,6 +982,31 @@ builder = "my-builder"
982982
h.AssertError(t, err, "Exporting to OCI layout is currently experimental.")
983983
})
984984
})
985+
986+
when("--insecure-registry is provided", func() {
987+
it("sets one insecure registry", func() {
988+
mockClient.EXPECT().
989+
Build(gomock.Any(), EqBuildOptionsWithInsecureRegistries([]string{
990+
"foo.bar",
991+
})).
992+
Return(nil)
993+
994+
command.SetArgs([]string{"image", "--builder", "my-builder", "--insecure-registry", "foo.bar"})
995+
h.AssertNil(t, command.Execute())
996+
})
997+
998+
it("sets more than one insecure registry", func() {
999+
mockClient.EXPECT().
1000+
Build(gomock.Any(), EqBuildOptionsWithInsecureRegistries([]string{
1001+
"foo.bar",
1002+
"foo.com",
1003+
})).
1004+
Return(nil)
1005+
1006+
command.SetArgs([]string{"image", "--builder", "my-builder", "--insecure-registry", "foo.bar", "--insecure-registry", "foo.com"})
1007+
h.AssertNil(t, command.Execute())
1008+
})
1009+
})
9851010
})
9861011

9871012
when("export to OCI layout is expected", func() {
@@ -1243,6 +1268,18 @@ func EqBuildOptionsWithLayoutConfig(image, previousImage string, sparse bool, la
12431268
}
12441269
}
12451270

1271+
func EqBuildOptionsWithInsecureRegistries(insecureRegistries []string) gomock.Matcher {
1272+
return buildOptionsMatcher{
1273+
description: fmt.Sprintf("Insercure Registries=%s", insecureRegistries),
1274+
equals: func(o client.BuildOptions) bool {
1275+
if len(o.InsecureRegistries) != len(insecureRegistries) {
1276+
return false
1277+
}
1278+
return reflect.DeepEqual(o.InsecureRegistries, insecureRegistries)
1279+
},
1280+
}
1281+
}
1282+
12461283
type buildOptionsMatcher struct {
12471284
equals func(client.BuildOptions) bool
12481285
description string

internal/commands/rebase.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func Rebase(logger logging.Logger, cfg config.Config, pack PackClient) *cobra.Co
5252
cmd.Flags().StringVar(&opts.PreviousImage, "previous-image", "", "Image to rebase. Set to a particular tag reference, digest reference, or (when performing a daemon build) image ID. Use this flag in combination with <image-name> to avoid replacing the original image.")
5353
cmd.Flags().StringVar(&opts.ReportDestinationDir, "report-output-dir", "", "Path to export build report.toml.\nOmitting the flag yield no report file.")
5454
cmd.Flags().BoolVar(&opts.Force, "force", false, "Perform rebase operation without target validation (only available for API >= 0.12)")
55-
55+
cmd.Flags().StringArrayVar(&opts.InsecureRegistries, "insecure-registry", []string{}, "List of insecure registries (only available for API >= 0.13)")
5656
AddHelpFlag(cmd, "rebase")
5757
return cmd
5858
}

internal/commands/rebase_test.go

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ func testRebaseCommand(t *testing.T, when spec.G, it spec.S) {
5050
when("#RebaseCommand", func() {
5151
when("no image is provided", func() {
5252
it("fails to run", func() {
53+
command.SetArgs([]string{})
5354
err := command.Execute()
5455
h.AssertError(t, err, "accepts 1 arg")
5556
})
@@ -80,6 +81,7 @@ func testRebaseCommand(t *testing.T, when spec.G, it spec.S) {
8081
AdditionalMirrors: map[string][]string{
8182
runImage: {testMirror1, testMirror2},
8283
},
84+
InsecureRegistries: []string{},
8385
}
8486
})
8587

@@ -122,6 +124,7 @@ func testRebaseCommand(t *testing.T, when spec.G, it spec.S) {
122124
h.AssertError(t, command.Execute(), "parsing pull policy")
123125
})
124126
})
127+
125128
when("--pull-policy not set", func() {
126129
when("no policy set in config", func() {
127130
it("uses the default policy", func() {
@@ -158,6 +161,7 @@ func testRebaseCommand(t *testing.T, when spec.G, it spec.S) {
158161
})
159162
})
160163
})
164+
161165
when("image name and previous image are provided", func() {
162166
var expectedOpts client.RebaseOptions
163167

@@ -182,7 +186,8 @@ func testRebaseCommand(t *testing.T, when spec.G, it spec.S) {
182186
AdditionalMirrors: map[string][]string{
183187
runImage: {testMirror1, testMirror2},
184188
},
185-
PreviousImage: previousImage,
189+
PreviousImage: previousImage,
190+
InsecureRegistries: []string{},
186191
}
187192
expectedOpts = opts
188193
})
@@ -196,6 +201,35 @@ func testRebaseCommand(t *testing.T, when spec.G, it spec.S) {
196201
h.AssertNil(t, command.Execute())
197202
})
198203
})
204+
205+
when("--insecure-registry is provided", func() {
206+
it("sets one insecure registry", func() {
207+
opts.PullPolicy = image.PullAlways
208+
opts.InsecureRegistries = []string{
209+
"foo.bar",
210+
}
211+
mockClient.EXPECT().
212+
Rebase(gomock.Any(), opts).
213+
Return(nil)
214+
215+
command.SetArgs([]string{repoName, "--insecure-registry", "foo.bar"})
216+
h.AssertNil(t, command.Execute())
217+
})
218+
219+
it("sets more than one insecure registry", func() {
220+
opts.PullPolicy = image.PullAlways
221+
opts.InsecureRegistries = []string{
222+
"foo.bar",
223+
"foo.com",
224+
}
225+
mockClient.EXPECT().
226+
Rebase(gomock.Any(), opts).
227+
Return(nil)
228+
229+
command.SetArgs([]string{repoName, "--insecure-registry", "foo.bar", "--insecure-registry", "foo.com"})
230+
h.AssertNil(t, command.Execute())
231+
})
232+
})
199233
})
200234
})
201235
}

pkg/client/build.go

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,8 @@ type BuildOptions struct {
231231

232232
// Enable user namespace isolation for the build containers
233233
EnableUsernsHost bool
234+
235+
InsecureRegistries []string
234236
}
235237

236238
func (b *BuildOptions) Layout() bool {
@@ -366,9 +368,11 @@ func (c *Client) Build(ctx context.Context, opts BuildOptions) error {
366368
ctx,
367369
builderRef.Name(),
368370
image.FetchOptions{
369-
Daemon: true,
370-
Target: requestedTarget,
371-
PullPolicy: opts.PullPolicy},
371+
Daemon: true,
372+
Target: requestedTarget,
373+
PullPolicy: opts.PullPolicy,
374+
InsecureRegistries: opts.InsecureRegistries,
375+
},
372376
)
373377
if err != nil {
374378
return errors.Wrapf(err, "failed to fetch builder image '%s'", builderRef.Name())
@@ -390,9 +394,10 @@ func (c *Client) Build(ctx context.Context, opts BuildOptions) error {
390394
}
391395

392396
fetchOptions := image.FetchOptions{
393-
Daemon: !opts.Publish,
394-
PullPolicy: opts.PullPolicy,
395-
Target: targetToUse,
397+
Daemon: !opts.Publish,
398+
PullPolicy: opts.PullPolicy,
399+
Target: targetToUse,
400+
InsecureRegistries: opts.InsecureRegistries,
396401
}
397402
runImageName := c.resolveRunImage(opts.RunImage, imgRegistry, builderRef.Context().RegistryStr(), bldr.DefaultRunImage(), opts.AdditionalMirrors, opts.Publish, fetchOptions)
398403

@@ -488,9 +493,10 @@ func (c *Client) Build(ctx context.Context, opts BuildOptions) error {
488493
ctx,
489494
lifecycleImageName,
490495
image.FetchOptions{
491-
Daemon: true,
492-
PullPolicy: opts.PullPolicy,
493-
Target: targetToUse,
496+
Daemon: true,
497+
PullPolicy: opts.PullPolicy,
498+
Target: targetToUse,
499+
InsecureRegistries: opts.InsecureRegistries,
494500
},
495501
)
496502
if err != nil {
@@ -661,6 +667,7 @@ func (c *Client) Build(ctx context.Context, opts BuildOptions) error {
661667
Layout: opts.Layout(),
662668
Keychain: c.keychain,
663669
EnableUsernsHost: opts.EnableUsernsHost,
670+
InsecureRegistries: opts.InsecureRegistries,
664671
}
665672

666673
switch {
@@ -823,7 +830,7 @@ func (c *Client) Build(ctx context.Context, opts BuildOptions) error {
823830
if err = c.lifecycleExecutor.Execute(ctx, lifecycleOpts); err != nil {
824831
return fmt.Errorf("executing lifecycle: %w", err)
825832
}
826-
return c.logImageNameAndSha(ctx, opts.Publish, imageRef)
833+
return c.logImageNameAndSha(ctx, opts.Publish, imageRef, opts.InsecureRegistries)
827834
}
828835

829836
func usesContainerdStorage(docker DockerClient) bool {
@@ -1677,13 +1684,13 @@ func randString(n int) string {
16771684
return string(b)
16781685
}
16791686

1680-
func (c *Client) logImageNameAndSha(ctx context.Context, publish bool, imageRef name.Reference) error {
1687+
func (c *Client) logImageNameAndSha(ctx context.Context, publish bool, imageRef name.Reference, insecureRegistries []string) error {
16811688
// The image name and sha are printed in the lifecycle logs, and there is no need to print it again, unless output is suppressed.
16821689
if !logging.IsQuiet(c.logger) {
16831690
return nil
16841691
}
16851692

1686-
img, err := c.imageFetcher.Fetch(ctx, imageRef.Name(), image.FetchOptions{Daemon: !publish, PullPolicy: image.PullNever})
1693+
img, err := c.imageFetcher.Fetch(ctx, imageRef.Name(), image.FetchOptions{Daemon: !publish, PullPolicy: image.PullNever, InsecureRegistries: insecureRegistries})
16871694
if err != nil {
16881695
return fmt.Errorf("fetching built image: %w", err)
16891696
}

pkg/client/client.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Package client provides all the functionally provided by pack as a library through a go api.
2+
Package client provides all the functionality provided by pack as a library through a go api.
33
44
# Prerequisites
55

pkg/client/rebase.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,16 @@ type RebaseOptions struct {
4646
// validated (will not have any effect if API < 0.12).
4747
Force bool
4848

49+
InsecureRegistries []string
50+
4951
// Image reference to use as the previous image for rebase.
5052
PreviousImage string
5153
}
5254

5355
// Rebase updates the run image layers in an app image.
5456
// This operation mutates the image specified in opts.
5557
func (c *Client) Rebase(ctx context.Context, opts RebaseOptions) error {
58+
var flags = []string{"rebase"}
5659
imageRef, err := c.parseTagReference(opts.RepoName)
5760
if err != nil {
5861
return errors.Wrapf(err, "invalid image name '%s'", opts.RepoName)
@@ -64,7 +67,7 @@ func (c *Client) Rebase(ctx context.Context, opts RebaseOptions) error {
6467
repoName = opts.PreviousImage
6568
}
6669

67-
appImage, err := c.imageFetcher.Fetch(ctx, repoName, image.FetchOptions{Daemon: !opts.Publish, PullPolicy: opts.PullPolicy})
70+
appImage, err := c.imageFetcher.Fetch(ctx, repoName, image.FetchOptions{Daemon: !opts.Publish, PullPolicy: opts.PullPolicy, InsecureRegistries: opts.InsecureRegistries})
6871
if err != nil {
6972
return err
7073
}
@@ -100,9 +103,10 @@ func (c *Client) Rebase(ctx context.Context, opts RebaseOptions) error {
100103

101104
target := &dist.Target{OS: appOS, Arch: appArch}
102105
fetchOptions := image.FetchOptions{
103-
Daemon: !opts.Publish,
104-
PullPolicy: opts.PullPolicy,
105-
Target: target,
106+
Daemon: !opts.Publish,
107+
PullPolicy: opts.PullPolicy,
108+
Target: target,
109+
InsecureRegistries: opts.InsecureRegistries,
106110
}
107111

108112
runImageName := c.resolveRunImage(
@@ -124,6 +128,10 @@ func (c *Client) Rebase(ctx context.Context, opts RebaseOptions) error {
124128
return err
125129
}
126130

131+
for _, reg := range opts.InsecureRegistries {
132+
flags = append(flags, "-insecure-registry", reg)
133+
}
134+
127135
c.logger.Infof("Rebasing %s on run image %s", style.Symbol(appImage.Name()), style.Symbol(baseImage.Name()))
128136
rebaser := &phase.Rebaser{Logger: c.logger, PlatformAPI: build.SupportedPlatformAPIVersions.Latest(), Force: opts.Force}
129137
report, err := rebaser.Rebase(appImage, baseImage, opts.RepoName, nil)

0 commit comments

Comments
 (0)