Skip to content

Commit dfb133c

Browse files
committed
fix(options): read build secrets the same way that we read other options
1 parent 51cd4d4 commit dfb133c

File tree

7 files changed

+21
-231
lines changed

7 files changed

+21
-231
lines changed

docs/build-secrets.md

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Envbuilder supports [build secrets](https://docs.docker.com/reference/dockerfile
44
* the secrets should not be present in the built image.
55
* the secrets should not be accessible in the container after its build has concluded.
66

7-
If your Dockerfile contains directives of the form `RUN --mount=type=secret,...`, Envbuilder will attempt to mount build secrets as specified in the directive. Unlike the `docker build` command, Envbuilder does not support the `--secret` flag. Instead, Envbuilder collects build secrets from environment variables prefixed with `Envbuilder_BUILD_SECRET_`. These build secrets will not be present in any cached layers or images that are pushed to an image repository. Nor will they be available at run time.
7+
If your Dockerfile contains directives of the form `RUN --mount=type=secret,...`, Envbuilder will attempt to mount build secrets as specified in the directive. Unlike the `docker build` command, Envbuilder does not support the `--secret` flag. Instead, Envbuilder collects build secrets from the `ENVBUILDER_BUILD_SECRETS` environment variable. These build secrets will not be present in any cached layers or images that are pushed to an image repository. Nor will they be available at run time.
88

99
## Example
1010

@@ -26,11 +26,10 @@ RUN --mount=type=secret,id=BAR,dst=/tmp/bar.secret cat /tmp/bar.secret > /bar_se
2626
using this command:
2727
```bash
2828
docker run -it --rm \
29-
-e Envbuilder_BUILD_SECRET_FOO='envbuilder-test-secret-foo' \
30-
-e Envbuilder_BUILD_SECRET_BAR='envbuilder-test-secret-bar' \
31-
-e Envbuilder_INIT_SCRIPT='/bin/sh' \
32-
-e Envbuilder_CACHE_REPO=$(docker inspect Envbuilder-registry | jq -r '.[].NetworkSettings.IPAddress'):5000/test-container \
33-
-e Envbuilder_PUSH_IMAGE=1 \
29+
-e ENVBUILDER_BUILD_SECRETS='FOO=envbuilder-test-secret-foo,BAR=envbuilder-test-secret-bar' \
30+
-e ENVBUILDER_INIT_SCRIPT='/bin/sh' \
31+
-e ENVBUILDER_CACHE_REPO=$(docker inspect Envbuilder-registry | jq -r '.[].NetworkSettings.IPAddress'):5000/test-container \
32+
-e ENVBUILDER_PUSH_IMAGE=1 \
3433
-v $PWD:/workspaces/empty \
3534
ghcr.io/coder/Envbuilder:latest
3635
```

envbuilder.go

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -531,10 +531,6 @@ func run(ctx context.Context, opts options.Options, execArgs *execArgsInfo) erro
531531
destinations = append(destinations, opts.CacheRepo)
532532
}
533533

534-
buildSecrets := options.GetBuildSecrets(os.Environ())
535-
// Ensure that build secrets do not make it into the runtime environment or the setup script:
536-
options.ClearBuildSecretsFromProcessEnvironment()
537-
538534
kOpts := &config.KanikoOptions{
539535
// Boilerplate!
540536
CustomPlatform: platforms.Format(platforms.Normalize(platforms.DefaultSpec())),
@@ -559,7 +555,7 @@ func run(ctx context.Context, opts options.Options, execArgs *execArgsInfo) erro
559555
},
560556
ForceUnpack: true,
561557
BuildArgs: buildParams.BuildArgs,
562-
BuildSecrets: buildSecrets,
558+
BuildSecrets: opts.BuildSecrets,
563559
CacheRepo: opts.CacheRepo,
564560
Cache: opts.CacheRepo != "" || opts.BaseImageCacheDir != "",
565561
DockerfilePath: buildParams.DockerfilePath,
@@ -1268,10 +1264,6 @@ func RunCacheProbe(ctx context.Context, opts options.Options) (v1.Image, error)
12681264
destinations = append(destinations, opts.CacheRepo)
12691265
}
12701266

1271-
buildSecrets := options.GetBuildSecrets(os.Environ())
1272-
// Ensure that build secrets do not make it into the runtime environment or the setup script:
1273-
options.ClearBuildSecretsFromProcessEnvironment()
1274-
12751267
kOpts := &config.KanikoOptions{
12761268
// Boilerplate!
12771269
CustomPlatform: platforms.Format(platforms.Normalize(platforms.DefaultSpec())),
@@ -1296,7 +1288,7 @@ func RunCacheProbe(ctx context.Context, opts options.Options) (v1.Image, error)
12961288
},
12971289
ForceUnpack: true,
12981290
BuildArgs: buildParams.BuildArgs,
1299-
BuildSecrets: buildSecrets,
1291+
BuildSecrets: opts.BuildSecrets,
13001292
CacheRepo: opts.CacheRepo,
13011293
Cache: opts.CacheRepo != "" || opts.BaseImageCacheDir != "",
13021294
DockerfilePath: buildParams.DockerfilePath,

integration/integration_test.go

Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1113,36 +1113,6 @@ func TestUnsetOptionsEnv(t *testing.T) {
11131113
}
11141114
}
11151115

1116-
func TestUnsetSecretEnvs(t *testing.T) {
1117-
t.Parallel()
1118-
1119-
// Ensures that a Git repository with a devcontainer.json is cloned and built.
1120-
srv := gittest.CreateGitServer(t, gittest.Options{
1121-
Files: map[string]string{
1122-
".devcontainer/devcontainer.json": `{
1123-
"name": "Test",
1124-
"build": {
1125-
"dockerfile": "Dockerfile"
1126-
},
1127-
}`,
1128-
".devcontainer/Dockerfile": "FROM " + testImageAlpine + "\nENV FROM_DOCKERFILE=foo",
1129-
},
1130-
})
1131-
ctr, err := runEnvbuilder(t, runOpts{env: []string{
1132-
envbuilderEnv("GIT_URL", srv.URL),
1133-
envbuilderEnv("GIT_PASSWORD", "supersecret"),
1134-
options.EnvWithBuildSecretPrefix("FOO", "foo"),
1135-
envbuilderEnv("INIT_SCRIPT", "env > /root/env.txt && sleep infinity"),
1136-
}})
1137-
require.NoError(t, err)
1138-
1139-
output := execContainer(t, ctr, "cat /root/env.txt")
1140-
envsAvailableToInitScript := strings.Split(strings.TrimSpace(output), "\n")
1141-
1142-
leftoverBuildSecrets := options.GetBuildSecrets(envsAvailableToInitScript)
1143-
require.Empty(t, leftoverBuildSecrets, "build secrets should not be available to init script")
1144-
}
1145-
11461116
func TestBuildSecrets(t *testing.T) {
11471117
t.Parallel()
11481118

@@ -1172,7 +1142,7 @@ func TestBuildSecrets(t *testing.T) {
11721142
ctr, err := runEnvbuilder(t, runOpts{env: []string{
11731143
envbuilderEnv("GIT_URL", srv.URL),
11741144
envbuilderEnv("GIT_PASSWORD", "supersecret"),
1175-
options.EnvWithBuildSecretPrefix("FOO", buildSecretVal),
1145+
envbuilderEnv("BUILD_SECRETS", fmt.Sprintf("FOO=%s", buildSecretVal)),
11761146
}})
11771147
require.NoError(t, err)
11781148

options/options.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,9 @@ type Options struct {
8888
// IgnorePaths is the comma separated list of paths to ignore when building
8989
// the workspace.
9090
IgnorePaths []string
91+
// BuildSecrets is the list of secret environment variables to use when
92+
// building the image.
93+
BuildSecrets []string
9194
// SkipRebuild skips building if the MagicFile exists. This is used to skip
9295
// building when a container is restarting. e.g. docker stop -> docker start
9396
// This value can always be set to true - even if the container is being
@@ -323,6 +326,12 @@ func (o *Options) CLI() serpent.OptionSet {
323326
Description: "The comma separated list of paths to ignore when " +
324327
"building the workspace.",
325328
},
329+
{
330+
Flag: "build-secrets",
331+
Env: WithEnvPrefix("BUILD_SECRETS"),
332+
Value: serpent.StringArrayOf(&o.BuildSecrets),
333+
Description: "The list of secret environment variables to use " + "when building the image.",
334+
},
326335
{
327336
Flag: "skip-rebuild",
328337
Env: WithEnvPrefix("SKIP_REBUILD"),

options/secrets.go

Lines changed: 0 additions & 46 deletions
This file was deleted.

options/secrets_test.go

Lines changed: 0 additions & 138 deletions
This file was deleted.

options/testdata/options.golden

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ OPTIONS:
1212
WorkspaceFolder. This path MUST be relative to the WorkspaceFolder
1313
path into which the repo is cloned.
1414

15+
--build-secrets string-array, $ENVBUILDER_BUILD_SECRETS
16+
The list of secret environment variables to use when building the
17+
image.
18+
1519
--cache-repo string, $ENVBUILDER_CACHE_REPO
1620
The name of the container registry to push the cache image to. If this
1721
is empty, the cache will not be pushed.

0 commit comments

Comments
 (0)