apply uid/gid when creating secret from environment

Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
This commit is contained in:
Nicolas De Loof 2022-12-15 14:10:02 +01:00 committed by Guillaume Lours
parent 0fedddb008
commit 5c1484ece6
3 changed files with 32 additions and 2 deletions

View File

@ -21,6 +21,7 @@ import (
"bytes" "bytes"
"context" "context"
"fmt" "fmt"
"strconv"
"time" "time"
"github.com/compose-spec/compose-go/types" "github.com/compose-spec/compose-go/types"
@ -69,11 +70,29 @@ func createTar(env string, config types.ServiceSecretConfig) (bytes.Buffer, erro
target = "/run/secrets/" + config.Target target = "/run/secrets/" + config.Target
} }
var uid, gid int
if config.UID != "" {
v, err := strconv.Atoi(config.UID)
if err != nil {
return b, err
}
uid = v
}
if config.GID != "" {
v, err := strconv.Atoi(config.GID)
if err != nil {
return b, err
}
gid = v
}
header := &tar.Header{ header := &tar.Header{
Name: target, Name: target,
Size: int64(len(value)), Size: int64(len(value)),
Mode: int64(mode), Mode: int64(mode),
ModTime: time.Now(), ModTime: time.Now(),
Uid: uid,
Gid: gid,
} }
err := tarWriter.WriteHeader(header) err := tarWriter.WriteHeader(header)
if err != nil { if err != nil {

View File

@ -2,10 +2,14 @@ services:
foo: foo:
image: alpine image: alpine
secrets: secrets:
- bar - source: secret
target: bar
uid: "1005"
gid: "1005"
mode: 0440
command: cat /run/secrets/bar command: cat /run/secrets/bar
secrets: secrets:
bar: secret:
environment: SECRET environment: SECRET

View File

@ -32,4 +32,11 @@ func TestSecretFromEnv(t *testing.T) {
}) })
res.Assert(t, icmd.Expected{Out: "BAR"}) res.Assert(t, icmd.Expected{Out: "BAR"})
}) })
t.Run("secret uid", func(t *testing.T) {
res := icmd.RunCmd(c.NewDockerComposeCmd(t, "-f", "./fixtures/env-secret/compose.yaml", "run", "foo", "ls", "-al", "/var/run/secrets/bar"),
func(cmd *icmd.Cmd) {
cmd.Env = append(cmd.Env, "SECRET=BAR")
})
res.Assert(t, icmd.Expected{Out: "-r--r----- 1 1005 1005"})
})
} }