Skip to content

Commit 7eb5ade

Browse files
ndeloofglours
authored andcommitted
introduce --insecure-registry, reserved for testing purpose
Signed-off-by: Nicolas De Loof <[email protected]>
1 parent 0793ad7 commit 7eb5ade

File tree

11 files changed

+97
-43
lines changed

11 files changed

+97
-43
lines changed

cmd/compose/compose.go

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -131,16 +131,17 @@ func Adapt(fn Command) func(cmd *cobra.Command, args []string) error {
131131
}
132132

133133
type ProjectOptions struct {
134-
ProjectName string
135-
Profiles []string
136-
ConfigPaths []string
137-
WorkDir string
138-
ProjectDir string
139-
EnvFiles []string
140-
Compatibility bool
141-
Progress string
142-
Offline bool
143-
All bool
134+
ProjectName string
135+
Profiles []string
136+
ConfigPaths []string
137+
WorkDir string
138+
ProjectDir string
139+
EnvFiles []string
140+
Compatibility bool
141+
Progress string
142+
Offline bool
143+
All bool
144+
insecureRegistries []string
144145
}
145146

146147
// ProjectFunc does stuff within a types.Project
@@ -216,6 +217,8 @@ func (o *ProjectOptions) addProjectFlags(f *pflag.FlagSet) {
216217
f.StringArrayVar(&o.Profiles, "profile", []string{}, "Specify a profile to enable")
217218
f.StringVarP(&o.ProjectName, "project-name", "p", "", "Project name")
218219
f.StringArrayVarP(&o.ConfigPaths, "file", "f", []string{}, "Compose configuration files")
220+
f.StringArrayVar(&o.insecureRegistries, "insecure-registry", []string{}, "Use insecure registry to pull Compose OCI artifacts. Doesn't apply to images")
221+
_ = f.MarkHidden("insecure-registry")
219222
f.StringArrayVar(&o.EnvFiles, "env-file", defaultStringArrayVar(ComposeEnvFiles), "Specify an alternate environment file")
220223
f.StringVar(&o.ProjectDir, "project-directory", "", "Specify an alternate working directory\n(default: the path of the, first specified, Compose file)")
221224
f.StringVar(&o.WorkDir, "workdir", "", "DEPRECATED! USE --project-directory INSTEAD.\nSpecify an alternate working directory\n(default: the path of the, first specified, Compose file)")
@@ -337,6 +340,9 @@ func (o *ProjectOptions) ToProject(ctx context.Context, dockerCli command.Cli, b
337340
Compatibility: o.Compatibility,
338341
ProjectOptionsFns: po,
339342
LoadListeners: []api.LoadListener{metricsListener},
343+
OCI: api.OCIOptions{
344+
InsecureRegistries: o.insecureRegistries,
345+
},
340346
}
341347

342348
project, err := backend.LoadProject(ctx, loadOpts)
@@ -352,7 +358,7 @@ func (o *ProjectOptions) remoteLoaders(dockerCli command.Cli) []loader.ResourceL
352358
return nil
353359
}
354360
git := remote.NewGitRemoteLoader(dockerCli, o.Offline)
355-
oci := remote.NewOCIRemoteLoader(dockerCli, o.Offline)
361+
oci := remote.NewOCIRemoteLoader(dockerCli, o.Offline, api.OCIOptions{})
356362
return []loader.ResourceLoader{git, oci}
357363
}
358364

cmd/compose/publish.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ type publishOptions struct {
3636
withEnvironment bool
3737
assumeYes bool
3838
app bool
39+
insecureRegistry bool
3940
}
4041

4142
func publishCommand(p *ProjectOptions, dockerCli command.Cli, backendOptions *BackendOptions) *cobra.Command {
@@ -56,6 +57,7 @@ func publishCommand(p *ProjectOptions, dockerCli command.Cli, backendOptions *Ba
5657
flags.BoolVar(&opts.withEnvironment, "with-env", false, "Include environment variables in the published OCI artifact")
5758
flags.BoolVarP(&opts.assumeYes, "yes", "y", false, `Assume "yes" as answer to all prompts`)
5859
flags.BoolVar(&opts.app, "app", false, "Published compose application (includes referenced images)")
60+
flags.BoolVar(&opts.insecureRegistry, "insecure-registry", false, "Use insecure registry")
5961
flags.SetNormalizeFunc(func(f *pflag.FlagSet, name string) pflag.NormalizedName {
6062
// assumeYes was introduced by mistake as `--y`
6163
if name == "y" {
@@ -64,6 +66,8 @@ func publishCommand(p *ProjectOptions, dockerCli command.Cli, backendOptions *Ba
6466
}
6567
return pflag.NormalizedName(name)
6668
})
69+
// Should **only** be used for testing purpose, we don't want to promote use of insecure registries
70+
_ = flags.MarkHidden("insecure-registry")
6771

6872
return cmd
6973
}
@@ -92,5 +96,6 @@ func runPublish(ctx context.Context, dockerCli command.Cli, backendOptions *Back
9296
Application: opts.app,
9397
OCIVersion: api.OCIVersion(opts.ociVersion),
9498
WithEnvironment: opts.withEnvironment,
99+
InsecureRegistry: opts.insecureRegistry,
95100
})
96101
}

docs/reference/docker_compose.yaml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,17 @@ options:
139139
experimentalcli: false
140140
kubernetes: false
141141
swarm: false
142+
- option: insecure-registry
143+
value_type: stringArray
144+
default_value: '[]'
145+
description: |
146+
Use insecure registry to pull Compose OCI artifacts. Doesn't apply to images
147+
deprecated: false
148+
hidden: true
149+
experimental: false
150+
experimentalcli: false
151+
kubernetes: false
152+
swarm: false
142153
- option: no-ansi
143154
value_type: bool
144155
default_value: "false"

docs/reference/docker_compose_alpha_publish.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,16 @@ options:
1515
experimentalcli: false
1616
kubernetes: false
1717
swarm: false
18+
- option: insecure-registry
19+
value_type: bool
20+
default_value: "false"
21+
description: Use insecure registry
22+
deprecated: false
23+
hidden: true
24+
experimental: false
25+
experimentalcli: false
26+
kubernetes: false
27+
swarm: false
1828
- option: oci-version
1929
value_type: string
2030
description: |

docs/reference/docker_compose_publish.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,16 @@ options:
1515
experimentalcli: false
1616
kubernetes: false
1717
swarm: false
18+
- option: insecure-registry
19+
value_type: bool
20+
default_value: "false"
21+
description: Use insecure registry
22+
deprecated: false
23+
hidden: true
24+
experimental: false
25+
experimentalcli: false
26+
kubernetes: false
27+
swarm: false
1828
- option: oci-version
1929
value_type: string
2030
description: |

internal/oci/resolver.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import (
2020
"context"
2121
"io"
2222
"net/url"
23-
"os"
23+
"slices"
2424
"strings"
2525

2626
"github.com/containerd/containerd/v2/core/remotes"
@@ -35,7 +35,7 @@ import (
3535
)
3636

3737
// NewResolver setup an OCI Resolver based on docker/cli config to provide registry credentials
38-
func NewResolver(config *configfile.ConfigFile) remotes.Resolver {
38+
func NewResolver(config *configfile.ConfigFile, insecureRegistries ...string) remotes.Resolver {
3939
return docker.NewResolver(docker.ResolverOptions{
4040
Hosts: docker.ConfigureDefaultRegistries(
4141
docker.WithAuthorizer(docker.NewDockerAuthorizer(
@@ -51,10 +51,9 @@ func NewResolver(config *configfile.ConfigFile) remotes.Resolver {
5151
return auth.Username, auth.Password, nil
5252
}),
5353
)),
54-
docker.WithPlainHTTP(func(s string) (bool, error) {
55-
// Used for testing **only**
56-
_, b := os.LookupEnv("__TEST__INSECURE__REGISTRY__")
57-
return b, nil
54+
docker.WithPlainHTTP(func(domain string) (bool, error) {
55+
// Should be used for testing **only**
56+
return slices.Contains(insecureRegistries, domain), nil
5857
}),
5958
),
6059
})

pkg/api/api.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,12 @@ type ProjectLoadOptions struct {
6969
// All registered listeners will be notified of events.
7070
// This is optional - pass nil or empty slice if not needed.
7171
LoadListeners []LoadListener
72+
73+
OCI OCIOptions
74+
}
75+
76+
type OCIOptions struct {
77+
InsecureRegistries []string
7278
}
7379

7480
// Compose is the API interface one can use to programmatically use docker/compose in a third-party software
@@ -484,8 +490,9 @@ type PublishOptions struct {
484490
ResolveImageDigests bool
485491
Application bool
486492
WithEnvironment bool
487-
488-
OCIVersion OCIVersion
493+
OCIVersion OCIVersion
494+
// Use plain HTTP to access registry. Should only be used for testing purpose
495+
InsecureRegistry bool
489496
}
490497

491498
func (e Event) String() string {

pkg/compose/loader.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import (
3333
// It loads and validates a Compose project from configuration files.
3434
func (s *composeService) LoadProject(ctx context.Context, options api.ProjectLoadOptions) (*types.Project, error) {
3535
// Setup remote loaders (Git, OCI)
36-
remoteLoaders := s.createRemoteLoaders(options.Offline)
36+
remoteLoaders := s.createRemoteLoaders(options)
3737

3838
projectOptions, err := s.buildProjectOptions(options, remoteLoaders)
3939
if err != nil {
@@ -66,12 +66,12 @@ func (s *composeService) LoadProject(ctx context.Context, options api.ProjectLoa
6666
}
6767

6868
// createRemoteLoaders creates Git and OCI remote loaders if not in offline mode
69-
func (s *composeService) createRemoteLoaders(offline bool) []loader.ResourceLoader {
70-
if offline {
69+
func (s *composeService) createRemoteLoaders(options api.ProjectLoadOptions) []loader.ResourceLoader {
70+
if options.Offline {
7171
return nil
7272
}
73-
git := remote.NewGitRemoteLoader(s.dockerCli, offline)
74-
oci := remote.NewOCIRemoteLoader(s.dockerCli, offline)
73+
git := remote.NewGitRemoteLoader(s.dockerCli, options.Offline)
74+
oci := remote.NewOCIRemoteLoader(s.dockerCli, options.Offline, options.OCI)
7575
return []loader.ResourceLoader{git, oci}
7676
}
7777

pkg/compose/publish.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,12 @@ func (s *composeService) publish(ctx context.Context, project *types.Project, re
8989
return err
9090
}
9191

92-
resolver := oci.NewResolver(s.configFile())
92+
var insecureRegistries []string
93+
if options.InsecureRegistry {
94+
insecureRegistries = append(insecureRegistries, reference.Domain(named))
95+
}
96+
97+
resolver := oci.NewResolver(s.configFile(), insecureRegistries...)
9398

9499
descriptor, err := oci.PushManifest(ctx, resolver, named, layers, options.OCIVersion)
95100
if err != nil {

pkg/e2e/publish_test.go

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -186,19 +186,17 @@ func TestPublish(t *testing.T) {
186186
c.RunDockerCmd(t, "rm", "--force", registryName)
187187
})
188188

189-
cmd := c.NewDockerComposeCmd(t, "-f", "./fixtures/publish/oci/compose.yaml", "-f", "./fixtures/publish/oci/compose-override.yaml",
190-
"-p", projectName, "publish", "--with-env", "--yes", registry+"/test:test")
191-
icmd.RunCmd(cmd, func(cmd *icmd.Cmd) {
192-
cmd.Env = append(cmd.Env, "__TEST__INSECURE__REGISTRY__=true")
193-
}).Assert(t, icmd.Expected{ExitCode: 0})
189+
res := c.RunDockerComposeCmd(t, "-f", "./fixtures/publish/oci/compose.yaml", "-f", "./fixtures/publish/oci/compose-override.yaml",
190+
"-p", projectName, "publish", "--with-env", "--yes", "--insecure-registry", registry+"/test:test")
191+
res.Assert(t, icmd.Expected{ExitCode: 0})
194192

195193
// docker exec -it compose-e2e-publish-registry tree /var/lib/registry/docker/registry/v2/
196194

197-
cmd = c.NewDockerComposeCmd(t, "--verbose", "--project-name=oci", "-f", fmt.Sprintf("oci://%s/test:test", registry), "config")
198-
res := icmd.RunCmd(cmd, func(cmd *icmd.Cmd) {
199-
cmd.Env = append(cmd.Env,
200-
"XDG_CACHE_HOME="+t.TempDir(),
201-
"__TEST__INSECURE__REGISTRY__=true")
195+
cmd := c.NewDockerComposeCmd(t, "--verbose", "--project-name=oci",
196+
"--insecure-registry", registry,
197+
"-f", fmt.Sprintf("oci://%s/test:test", registry), "config")
198+
res = icmd.RunCmd(cmd, func(cmd *icmd.Cmd) {
199+
cmd.Env = append(cmd.Env, "XDG_CACHE_HOME="+t.TempDir())
202200
})
203201
res.Assert(t, icmd.Expected{ExitCode: 0})
204202
assert.Equal(t, res.Stdout(), `name: oci

0 commit comments

Comments
 (0)