-
Notifications
You must be signed in to change notification settings - Fork 5.7k
add --with-env flag to publish command #12482
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,6 +18,7 @@ package compose | |
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "os" | ||
|
|
||
| "github.com/compose-spec/compose-go/v2/types" | ||
|
|
@@ -35,7 +36,11 @@ func (s *composeService) Publish(ctx context.Context, project *types.Project, re | |
| } | ||
|
|
||
| func (s *composeService) publish(ctx context.Context, project *types.Project, repository string, options api.PublishOptions) error { | ||
| err := s.Push(ctx, project, api.PushOptions{IgnoreFailures: true, ImageMandatory: true}) | ||
| err := preChecks(project, options) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| err = s.Push(ctx, project, api.PushOptions{IgnoreFailures: true, ImageMandatory: true}) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
@@ -63,6 +68,10 @@ func (s *composeService) publish(ctx context.Context, project *types.Project, re | |
| }) | ||
| } | ||
|
|
||
| if options.WithEnvironment { | ||
| layers = append(layers, envFileLayers(project)...) | ||
| } | ||
|
|
||
| if options.ResolveImageDigests { | ||
| yaml, err := s.generateImageDigestsOverride(ctx, project) | ||
| if err != nil { | ||
|
|
@@ -120,3 +129,49 @@ func (s *composeService) generateImageDigestsOverride(ctx context.Context, proje | |
| } | ||
| return override.MarshalYAML() | ||
| } | ||
|
|
||
| func preChecks(project *types.Project, options api.PublishOptions) error { | ||
| if !options.WithEnvironment { | ||
| for _, service := range project.Services { | ||
| if len(service.EnvFiles) > 0 { | ||
| return fmt.Errorf("service %q has env_file declared. To avoid leaking sensitive data, "+ | ||
| "you must either explicitly allow the sending of environment variables by using the --with-env flag,"+ | ||
| " or remove sensitive data from your Compose configuration", service.Name) | ||
| } | ||
| if len(service.Environment) > 0 { | ||
| return fmt.Errorf("service %q has environment variable(s) declared. To avoid leaking sensitive data, "+ | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. service.environment may be set with a fixed value, not relying on any interpolation. Typically: those should not prevent compose file to be published, right ?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In fact yes that should prevent from publishing by default because you can also have |
||
| "you must either explicitly allow the sending of environment variables by using the --with-env flag,"+ | ||
| " or remove sensitive data from your Compose configuration", service.Name) | ||
| } | ||
| } | ||
|
|
||
| for _, config := range project.Configs { | ||
| if config.Environment != "" { | ||
| return fmt.Errorf("config %q is declare as an environment variable. To avoid leaking sensitive data, "+ | ||
| "you must either explicitly allow the sending of environment variables by using the --with-env flag,"+ | ||
| " or remove sensitive data from your Compose configuration", config.Name) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func envFileLayers(project *types.Project) []ocipush.Pushable { | ||
| var layers []ocipush.Pushable | ||
| for _, service := range project.Services { | ||
| for _, envFile := range service.EnvFiles { | ||
| f, err := os.ReadFile(envFile.Path) | ||
| if err != nil { | ||
| // if we can't read the file, skip to the next one | ||
| continue | ||
| } | ||
| layerDescriptor := ocipush.DescriptorForEnvFile(envFile.Path, f) | ||
| layers = append(layers, ocipush.Pushable{ | ||
| Descriptor: layerDescriptor, | ||
| Data: f, | ||
| }) | ||
| } | ||
| } | ||
| return layers | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| services: | ||
| serviceA: | ||
| image: "alpine:3.12" | ||
| env_file: | ||
| - publish.env | ||
| serviceB: | ||
| image: "alpine:3.12" |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| services: | ||
| serviceA: | ||
| image: "alpine:3.12" | ||
| environment: | ||
| - "FOO=bar" | ||
| serviceB: | ||
| image: "alpine:3.12" |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| FOO=bar |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| /* | ||
| Copyright 2020 Docker Compose CLI authors | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| package e2e | ||
|
|
||
| import ( | ||
| "strings" | ||
| "testing" | ||
|
|
||
| "gotest.tools/v3/assert" | ||
| "gotest.tools/v3/icmd" | ||
| ) | ||
|
|
||
| func TestPublishChecks(t *testing.T) { | ||
| c := NewParallelCLI(t) | ||
| const projectName = "compose-e2e-explicit-profiles" | ||
|
|
||
| t.Run("publish error environment", func(t *testing.T) { | ||
| res := c.RunDockerComposeCmdNoCheck(t, "-f", "./fixtures/publish/compose-environment.yml", | ||
| "-p", projectName, "alpha", "publish", "test/test") | ||
| res.Assert(t, icmd.Expected{ExitCode: 1, Err: `service "serviceA" has environment variable(s) declared. To avoid leaking sensitive data,`}) | ||
| }) | ||
|
|
||
| t.Run("publish error env_file", func(t *testing.T) { | ||
| res := c.RunDockerComposeCmdNoCheck(t, "-f", "./fixtures/publish/compose-env-file.yml", | ||
| "-p", projectName, "alpha", "publish", "test/test") | ||
| res.Assert(t, icmd.Expected{ExitCode: 1, Err: `service "serviceA" has env_file declared. To avoid leaking sensitive data,`}) | ||
| }) | ||
|
|
||
| t.Run("publish success environment", func(t *testing.T) { | ||
| res := c.RunDockerComposeCmd(t, "-f", "./fixtures/publish/compose-environment.yml", | ||
| "-p", projectName, "alpha", "publish", "test/test", "--with-env", "--dry-run") | ||
| assert.Assert(t, strings.Contains(res.Combined(), "test/test publishing"), res.Combined()) | ||
| assert.Assert(t, strings.Contains(res.Combined(), "test/test published"), res.Combined()) | ||
| }) | ||
|
|
||
| t.Run("publish success env_file", func(t *testing.T) { | ||
| res := c.RunDockerComposeCmd(t, "-f", "./fixtures/publish/compose-env-file.yml", | ||
| "-p", projectName, "alpha", "publish", "test/test", "--with-env", "--dry-run") | ||
| assert.Assert(t, strings.Contains(res.Combined(), "test/test publishing"), res.Combined()) | ||
| assert.Assert(t, strings.Contains(res.Combined(), "test/test published"), res.Combined()) | ||
| }) | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.