Skip to content

Commit 005fc25

Browse files
committed
add support for setting secret from env variable
Signed-off-by: Nicolas De Loof <[email protected]>
1 parent a48f1e8 commit 005fc25

File tree

5 files changed

+140
-0
lines changed

5 files changed

+140
-0
lines changed

pkg/compose/convergence.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -523,6 +523,8 @@ func (s *composeService) createMobyContainer(ctx context.Context, project *types
523523
return created, err
524524
}
525525
}
526+
527+
err = s.injectSecrets(ctx, project, service, created.ID)
526528
return created, err
527529
}
528530

pkg/compose/create.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -889,6 +889,10 @@ func buildContainerSecretMounts(p types.Project, s types.ServiceConfig) ([]mount
889889
return nil, fmt.Errorf("unsupported external secret %s", definedSecret.Name)
890890
}
891891

892+
if definedSecret.Environment != "" {
893+
continue
894+
}
895+
892896
mount, err := buildMount(p, types.ServiceVolumeConfig{
893897
Type: types.VolumeTypeBind,
894898
Source: definedSecret.File,

pkg/compose/secrets.go

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
Copyright 2020 Docker Compose CLI authors
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package compose
18+
19+
import (
20+
"archive/tar"
21+
"bytes"
22+
"context"
23+
"fmt"
24+
"time"
25+
26+
"github.com/compose-spec/compose-go/types"
27+
moby "github.com/docker/docker/api/types"
28+
)
29+
30+
func (s *composeService) injectSecrets(ctx context.Context, project *types.Project, service types.ServiceConfig, id string) error {
31+
for _, config := range service.Secrets {
32+
secret := project.Secrets[config.Source]
33+
if secret.Environment == "" {
34+
continue
35+
}
36+
37+
env, ok := project.Environment[secret.Environment]
38+
if !ok {
39+
return fmt.Errorf("environment variable %q required by secret %q is not set", secret.Environment, secret.Name)
40+
}
41+
b, err := createTar(env, config)
42+
if err != nil {
43+
return err
44+
}
45+
46+
err = s.apiClient().CopyToContainer(ctx, id, "/", &b, moby.CopyToContainerOptions{
47+
CopyUIDGID: true,
48+
})
49+
if err != nil {
50+
return err
51+
}
52+
}
53+
return nil
54+
}
55+
56+
func createTar(env string, config types.ServiceSecretConfig) (bytes.Buffer, error) {
57+
value := []byte(env)
58+
b := bytes.Buffer{}
59+
tarWriter := tar.NewWriter(&b)
60+
mode := uint32(0400)
61+
if config.Mode != nil {
62+
mode = *config.Mode
63+
}
64+
65+
target := config.Target
66+
if config.Target == "" {
67+
target = "/run/secrets/" + config.Source
68+
} else if !isUnixAbs(config.Target) {
69+
target = "/run/secrets/" + config.Target
70+
}
71+
72+
header := &tar.Header{
73+
Name: target,
74+
Size: int64(len(value)),
75+
Mode: int64(mode),
76+
ModTime: time.Now(),
77+
}
78+
err := tarWriter.WriteHeader(header)
79+
if err != nil {
80+
return bytes.Buffer{}, err
81+
}
82+
_, err = tarWriter.Write(value)
83+
if err != nil {
84+
return bytes.Buffer{}, err
85+
}
86+
err = tarWriter.Close()
87+
return b, err
88+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
services:
2+
foo:
3+
image: alpine
4+
secrets:
5+
- bar
6+
command: cat /run/secrets/bar
7+
8+
secrets:
9+
bar:
10+
environment: SECRET
11+

pkg/e2e/secrets_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
Copyright 2020 Docker Compose CLI authors
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package e2e
18+
19+
import (
20+
"testing"
21+
22+
"gotest.tools/v3/icmd"
23+
)
24+
25+
func TestSecretFromEnv(t *testing.T) {
26+
c := NewParallelE2eCLI(t, binDir)
27+
28+
t.Run("compose run", func(t *testing.T) {
29+
res := icmd.RunCmd(c.NewDockerCmd("compose", "-f", "./fixtures/env-secret/compose.yaml", "run", "foo"),
30+
func(cmd *icmd.Cmd) {
31+
cmd.Env = append(cmd.Env, "SECRET=BAR")
32+
})
33+
res.Assert(t, icmd.Expected{Out: "BAR"})
34+
})
35+
}

0 commit comments

Comments
 (0)