Skip to content

Commit 2fcd340

Browse files
committed
fix(docs): update docs on build secrets for legibility
1 parent c922138 commit 2fcd340

File tree

1 file changed

+59
-15
lines changed

1 file changed

+59
-15
lines changed

docs/build-secrets.md

Lines changed: 59 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,22 @@ Then, prepare the files to build our container.
1919
```bash
2020
mkdir test-build-secrets
2121
cd test-build-secrets
22-
printf 'FROM alpine:latest\n\nRUN --mount=type=secret,id=TEST_BUILD_SECRET_A,env=TEST_BUILD_SECRET_A echo -n $TEST_BUILD_SECRET_A | sha256sum > /foo_secret_hash.txt\nRUN --mount=type=secret,id=TEST_BUILD_SECRET_B,dst=/tmp/bar.secret cat /tmp/bar.secret | sha256sum > /bar_secret_hash.txt\n' > Dockerfile
23-
printf '{"build": { "dockerfile": "Dockerfile"}}\n' > devcontainer.json
22+
cat << EOF > Dockerfile
23+
FROM alpine:latest
24+
25+
RUN --mount=type=secret,id=TEST_BUILD_SECRET_A,env=TEST_BUILD_SECRET_A echo -n \$TEST_BUILD_SECRET_A | sha256sum > /foo_secret_hash.txt
26+
RUN --mount=type=secret,id=TEST_BUILD_SECRET_B,dst=/tmp/bar.secret cat /tmp/bar.secret | sha256sum > /bar_secret_hash.txt
27+
EOF
28+
cat << EOF > devcontainer.json
29+
{
30+
"build": {
31+
"dockerfile": "Dockerfile"
32+
}
33+
}
34+
EOF
2435
```
2536

26-
Inspect the Dockerfile and devcontainer.json files in the new directory.
27-
```bash
28-
cat devcontainer.json
29-
cat Dockerfile
30-
```
31-
32-
Note that the Dockerfile requires two secrets: `TEST_BUILD_SECRET_A` and `TEST_BUILD_SECRET_B`. Their values are arbitrarily set to `secret-foo` and `secret-bar` by the command below. Building the container image writes the checksums for these secrets to disk. This illustrates that the secrets can be used in the build to enact side effects without exposing the secrets themselves.
37+
The Dockerfile requires two secrets: `TEST_BUILD_SECRET_A` and `TEST_BUILD_SECRET_B`. Their values are arbitrarily set to `secret-foo` and `secret-bar` by the command below. Building the container image writes the checksums for these secrets to disk. This illustrates that the secrets can be used in the build to enact side effects without exposing the secrets themselves.
3338

3439
Execute the build using this command:
3540
```bash
@@ -43,21 +48,60 @@ docker run -it --rm \
4348
```
4449

4550
This will result in a shell session inside the built container.
46-
You can now verify three things:
47-
* The secrets provided to build are not available once the container is running. They are no longer on disk, nor are they in the process environment, or in `/proc/self/environ`.
51+
You can now verify two things:
52+
53+
Firstly, the secrets provided to build are not available once the container is running. They are no longer on disk, nor are they in the process environment, or in `/proc/self/environ`:
4854
```bash
4955
cat /proc/self/environ | tr '\0' '\n'
5056
printenv
5157
```
52-
* The secrets were still useful during the build. The following commands show that the secrets had side effects inside the build, without remaining in the image:
58+
Expected output:
59+
```bash
60+
/workspaces/empty # cat /proc/self/environ | tr '\0' '\n'
61+
HOSTNAME=c0b0ee3d5564
62+
SHLVL=2
63+
HOME=/root
64+
TERM=xterm
65+
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
66+
DEVCONTAINER_CONFIG=/workspaces/empty/devcontainer.json
67+
ENVBUILDER=true
68+
TS_DEBUG_TRIM_WIREGUARD=false
69+
PWD=/workspaces/empty
70+
DEVCONTAINER=true
71+
/workspaces/empty # printenv
72+
HOSTNAME=c0b0ee3d5564
73+
SHLVL=2
74+
HOME=/root
75+
TERM=xterm
76+
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
77+
DEVCONTAINER_CONFIG=/workspaces/empty/devcontainer.json
78+
ENVBUILDER=true
79+
TS_DEBUG_TRIM_WIREGUARD=false
80+
PWD=/workspaces/empty
81+
DEVCONTAINER=true
82+
/workspaces/empty #
83+
```
84+
85+
Finally, the secrets were still useful during the build. The following commands show that the secrets had side effects inside the build, without remaining in the image:
5386
```bash
5487
echo -n "secret-foo" | sha256sum
5588
cat /foo_secret_hash.txt
5689
echo -n "secret-bar" | sha256sum
5790
cat /bar_secret_hash.txt
5891
```
5992

60-
Notice that the first two checksums match and that the last two checksums match.
93+
Notice that the first two checksums match and that the last two checksums match. Expected output:
94+
```
95+
/workspaces/empty # echo -n "secret-foo" | sha256sum
96+
9a888f08a057159d2ea8fb69d38c9a25e367d7ca3128035b7f6dee2ca988c3d8 -
97+
/workspaces/empty # cat /foo_secret_hash.txt
98+
9a888f08a057159d2ea8fb69d38c9a25e367d7ca3128035b7f6dee2ca988c3d8 -
99+
/workspaces/empty # echo -n "secret-bar" | sha256sum
100+
fb1c9d1220e429b30c60d028b882f735b5af72d7b5496d9202737fe9f1d38289 -
101+
/workspaces/empty # cat /bar_secret_hash.txt
102+
fb1c9d1220e429b30c60d028b882f735b5af72d7b5496d9202737fe9f1d38289 -
103+
/workspaces/empty #
104+
```
61105

62106
Finally, exit the container:
63107
```bash
@@ -103,10 +147,10 @@ Build secrets are meant for use cases where the secret should not be accessible
103147
Build secrets are only protected if they are not copied or moved from their location as designated in the `RUN` directive. If a build secret is used, care should be taken to ensure that it is not copied or otherwise persisted into an image layer beyond the control of Envbuilder.
104148

105149
### Who should be able to access build secrets, when and where?
106-
The secure way to use build secrets with Envbuilder is to deny users access to the platform that hosts Envbuilder. Only grant access to the Envbuilder container once it has concluded its build, using a trusted non-platform channel like ssh or the coder agent running inside the container. Once control has been handed to such a runtime container process, Envbuilder will have cleared all secrets that it set from the container.
107-
108150
Anyone with sufficient access to attach directly to the container (eg. using `kubectl`), will be able to read build secrets if they attach to the container before it has concluded its build. Anyone with sufficient access to the platform that hosts the Envbuilder container will also be able to read these build secrets from where the platform stores them. This is true for other build systems, and containerised software in general.
109151

152+
The secure way to use build secrets with Envbuilder is to deny users access to the platform that hosts Envbuilder. Only grant access to the Envbuilder container once it has concluded its build, using a trusted non-platform channel like ssh or the coder agent running inside the container. Once control has been handed to such a runtime container process, Envbuilder will have cleared all secrets that it set from the container.
153+
110154
If secrets should be accessible at runtime, do not use build secrets. Rather, mount the secret data using a volume or environment variable. Envbuilder will not include mounted volumes in the image that it pushes to any cache repositories, but they will still be available to users that connect to the container.
111155

112156
### Container Management beyond Envbuilder's control

0 commit comments

Comments
 (0)