Skip to content

Commit e0977c2

Browse files
ndeloofglours
authored andcommitted
only check for env_file
Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
1 parent 2d56991 commit e0977c2

2 files changed

Lines changed: 4 additions & 85 deletions

File tree

pkg/compose/publish.go

Lines changed: 4 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,6 @@ func (s *composeService) generateImageDigestsOverride(ctx context.Context, proje
297297
return override.MarshalYAML()
298298
}
299299

300-
//nolint:gocyclo
301300
func (s *composeService) preChecks(project *types.Project, options api.PublishOptions) (bool, error) {
302301
if ok, err := s.checkOnlyBuildSection(project); !ok || err != nil {
303302
return false, err
@@ -340,50 +339,20 @@ func (s *composeService) preChecks(project *types.Project, options api.PublishOp
340339
return false, err
341340
}
342341
}
343-
envVariables, err := s.checkEnvironmentVariables(project, options)
342+
err = s.checkEnvironmentVariables(project, options)
344343
if err != nil {
345344
return false, err
346345
}
347-
if len(envVariables) > 0 {
348-
b := strings.Builder{}
349-
b.WriteString("you are about to publish environment variables within your OCI artifact.\n" +
350-
"please double check that you are not leaking sensitive data\n")
351-
for key, val := range envVariables {
352-
b.WriteString("Service/Config ")
353-
b.WriteString(key)
354-
b.WriteRune('\n')
355-
for k, v := range val {
356-
b.WriteString(fmt.Sprintf("%s=%v\n", k, *v))
357-
}
358-
}
359-
b.WriteString("Are you ok to publish these environment variables?")
360-
confirm, err := s.prompt(b.String(), false)
361-
if err != nil || !confirm {
362-
return false, err
363-
}
364-
}
365346
return true, nil
366347
}
367348

368-
func (s *composeService) checkEnvironmentVariables(project *types.Project, options api.PublishOptions) (map[string]types.MappingWithEquals, error) {
369-
envVarList := map[string]types.MappingWithEquals{}
349+
func (s *composeService) checkEnvironmentVariables(project *types.Project, options api.PublishOptions) error {
370350
errorList := map[string][]string{}
371351

372352
for _, service := range project.Services {
373353
if len(service.EnvFiles) > 0 {
374354
errorList[service.Name] = append(errorList[service.Name], fmt.Sprintf("service %q has env_file declared.", service.Name))
375355
}
376-
if len(service.Environment) > 0 {
377-
errorList[service.Name] = append(errorList[service.Name], fmt.Sprintf("service %q has environment variable(s) declared.", service.Name))
378-
envVarList[service.Name] = service.Environment
379-
}
380-
}
381-
382-
for _, config := range project.Configs {
383-
if config.Environment != "" {
384-
errorList[config.Name] = append(errorList[config.Name], fmt.Sprintf("config %q is declare as an environment variable.", config.Name))
385-
envVarList[config.Name] = types.NewMappingWithEquals([]string{fmt.Sprintf("%s=%s", config.Name, config.Environment)})
386-
}
387356
}
388357

389358
if !options.WithEnvironment && len(errorList) > 0 {
@@ -395,10 +364,10 @@ func (s *composeService) checkEnvironmentVariables(project *types.Project, optio
395364
errorMsg.WriteString(fmt.Sprintf("%s\n", err))
396365
}
397366
}
398-
return nil, fmt.Errorf("%s%s", errorMsg.String(), errorMsgSuffix)
367+
return fmt.Errorf("%s%s", errorMsg.String(), errorMsgSuffix)
399368

400369
}
401-
return envVarList, nil
370+
return nil
402371
}
403372

404373
func envFileLayers(files map[string]string) []v1.Descriptor {

pkg/e2e/publish_test.go

Lines changed: 0 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,10 @@ func TestPublishChecks(t *testing.T) {
2929
c := NewParallelCLI(t)
3030
const projectName = "compose-e2e-explicit-profiles"
3131

32-
t.Run("publish error environment", func(t *testing.T) {
33-
res := c.RunDockerComposeCmdNoCheck(t, "-f", "./fixtures/publish/compose-environment.yml",
34-
"-p", projectName, "publish", "test/test")
35-
res.Assert(t, icmd.Expected{ExitCode: 1, Err: `service "serviceA" has environment variable(s) declared.
36-
To avoid leaking sensitive data,`})
37-
})
38-
3932
t.Run("publish error env_file", func(t *testing.T) {
4033
res := c.RunDockerComposeCmdNoCheck(t, "-f", "./fixtures/publish/compose-env-file.yml",
4134
"-p", projectName, "publish", "test/test")
4235
res.Assert(t, icmd.Expected{ExitCode: 1, Err: `service "serviceA" has env_file declared.
43-
service "serviceA" has environment variable(s) declared.
4436
To avoid leaking sensitive data,`})
4537
})
4638

@@ -49,8 +41,6 @@ To avoid leaking sensitive data,`})
4941
"-p", projectName, "publish", "test/test")
5042
// we don't in which order the services will be loaded, so we can't predict the order of the error messages
5143
assert.Assert(t, strings.Contains(res.Combined(), `service "serviceB" has env_file declared.`), res.Combined())
52-
assert.Assert(t, strings.Contains(res.Combined(), `service "serviceB" has environment variable(s) declared.`), res.Combined())
53-
assert.Assert(t, strings.Contains(res.Combined(), `service "serviceA" has environment variable(s) declared.`), res.Combined())
5444
assert.Assert(t, strings.Contains(res.Combined(), `To avoid leaking sensitive data, you must either explicitly allow the sending of environment variables by using the --with-env flag,
5545
or remove sensitive data from your Compose configuration
5646
`), res.Combined())
@@ -70,52 +60,12 @@ or remove sensitive data from your Compose configuration
7060
assert.Assert(t, strings.Contains(res.Combined(), "test/test published"), res.Combined())
7161
})
7262

73-
t.Run("publish approve validation message", func(t *testing.T) {
74-
cmd := c.NewDockerComposeCmd(t, "-f", "./fixtures/publish/compose-env-file.yml",
75-
"-p", projectName, "publish", "test/test", "--with-env", "--dry-run")
76-
cmd.Stdin = strings.NewReader("y\n")
77-
res := icmd.RunCmd(cmd)
78-
res.Assert(t, icmd.Expected{ExitCode: 0})
79-
assert.Assert(t, strings.Contains(res.Combined(), "Are you ok to publish these environment variables?"), res.Combined())
80-
assert.Assert(t, strings.Contains(res.Combined(), "test/test publishing"), res.Combined())
81-
assert.Assert(t, strings.Contains(res.Combined(), "test/test published"), res.Combined())
82-
})
83-
84-
t.Run("publish refuse validation message", func(t *testing.T) {
85-
cmd := c.NewDockerComposeCmd(t, "-f", "./fixtures/publish/compose-env-file.yml",
86-
"-p", projectName, "publish", "test/test", "--with-env", "--dry-run")
87-
cmd.Stdin = strings.NewReader("n\n")
88-
res := icmd.RunCmd(cmd)
89-
res.Assert(t, icmd.Expected{ExitCode: 0})
90-
assert.Assert(t, strings.Contains(res.Combined(), "Are you ok to publish these environment variables?"), res.Combined())
91-
assert.Assert(t, !strings.Contains(res.Combined(), "test/test publishing"), res.Combined())
92-
assert.Assert(t, !strings.Contains(res.Combined(), "test/test published"), res.Combined())
93-
})
94-
9563
t.Run("publish with extends", func(t *testing.T) {
9664
res := c.RunDockerComposeCmd(t, "-f", "./fixtures/publish/compose-with-extends.yml",
9765
"-p", projectName, "publish", "test/test", "--dry-run")
9866
assert.Assert(t, strings.Contains(res.Combined(), "test/test published"), res.Combined())
9967
})
10068

101-
t.Run("publish list env variables", func(t *testing.T) {
102-
cmd := c.NewDockerComposeCmd(t, "-f", "./fixtures/publish/compose-multi-env-config.yml",
103-
"-p", projectName, "publish", "test/test", "--with-env", "--dry-run")
104-
cmd.Stdin = strings.NewReader("n\n")
105-
res := icmd.RunCmd(cmd)
106-
res.Assert(t, icmd.Expected{ExitCode: 0})
107-
out := res.Combined()
108-
assert.Assert(t, strings.Contains(out, `you are about to publish environment variables within your OCI artifact.
109-
please double check that you are not leaking sensitive data`), out)
110-
assert.Assert(t, strings.Contains(out, `Service/Config serviceA
111-
FOO=bar`), out)
112-
assert.Assert(t, strings.Contains(out, `Service/Config serviceB`), out)
113-
// we don't know in which order the env variables will be loaded
114-
assert.Assert(t, strings.Contains(out, `FOO=bar`), out)
115-
assert.Assert(t, strings.Contains(out, `BAR=baz`), out)
116-
assert.Assert(t, strings.Contains(out, `QUIX=`), out)
117-
})
118-
11969
t.Run("refuse to publish with bind mount", func(t *testing.T) {
12070
cmd := c.NewDockerComposeCmd(t, "-f", "./fixtures/publish/compose-bind-mount.yml",
12171
"-p", projectName, "publish", "test/test", "--dry-run")

0 commit comments

Comments
 (0)