Skip to content

Commit 5b6c62a

Browse files
committed
fix(docs): fix example in the build secret docs
1 parent dfb133c commit 5b6c62a

File tree

1 file changed

+46
-20
lines changed

1 file changed

+46
-20
lines changed

docs/build-secrets.md

Lines changed: 46 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,39 +12,60 @@ To illustrate build secrets in Envbuilder, let's build, push and run a container
1212

1313
First, start a local docker registry, so that we can push and inspect the built image:
1414
```bash
15-
docker run --rm -d -p 5000:5000 --name Envbuilder-registry registry:2
15+
docker run --rm -d -p 5000:5000 --name envbuilder-registry registry:2
1616
```
1717

18-
Then, build an image based on this Dockerfile:
19-
20-
```Dockerfile
21-
FROM alpine:latest
18+
Then, prepare the files to build our container.
19+
```bash
20+
mkdir test-build-secrets
21+
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
24+
```
2225

23-
RUN --mount=type=secret,id=FOO,env cat $FOO > /foo_secret_hash.txt
24-
RUN --mount=type=secret,id=BAR,dst=/tmp/bar.secret cat /tmp/bar.secret > /bar_secret_hash.txt
26+
Inspect the Dockerfile and devcontainer.json files in the new directory.
27+
```bash
28+
cat devcontainer.json
29+
cat Dockerfile
2530
```
26-
using this command:
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.
33+
34+
Execute the build using this command:
2735
```bash
2836
docker run -it --rm \
29-
-e ENVBUILDER_BUILD_SECRETS='FOO=envbuilder-test-secret-foo,BAR=envbuilder-test-secret-bar' \
37+
-e ENVBUILDER_BUILD_SECRETS='TEST_BUILD_SECRET_A=secret-foo,TEST_BUILD_SECRET_B=secret-bar' \
3038
-e ENVBUILDER_INIT_SCRIPT='/bin/sh' \
31-
-e ENVBUILDER_CACHE_REPO=$(docker inspect Envbuilder-registry | jq -r '.[].NetworkSettings.IPAddress'):5000/test-container \
39+
-e ENVBUILDER_CACHE_REPO=$(docker inspect envbuilder-registry | jq -r '.[].NetworkSettings.IPAddress'):5000/test-container \
3240
-e ENVBUILDER_PUSH_IMAGE=1 \
3341
-v $PWD:/workspaces/empty \
34-
ghcr.io/coder/Envbuilder:latest
42+
ghcr.io/coder/envbuilder:latest
3543
```
3644

3745
This will result in a shell session inside the built container.
38-
You can now verify two things:
46+
You can now verify three things:
3947
* 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`.
40-
* The secrets were still useful during the build. The following comnmands show that the secrets had side effects inside the build, without remaining in the image:
4148
```bash
49+
cat /proc/self/environ | tr '\0' '\n'
50+
printenv
51+
```
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:
53+
```bash
54+
echo -n "secret-foo" | sha256sum
4255
cat /foo_secret_hash.txt
56+
echo -n "secret-bar" | sha256sum
4357
cat /bar_secret_hash.txt
4458
```
4559

60+
Notice that the first two checksums match and that the last two checksums match.
61+
62+
Finally, exit the container:
63+
```bash
64+
exit
65+
```
66+
4667
### Verifying that images are secret free
47-
To verify that the build image doesn't contain build secrets, run the following:
68+
To verify that the built image doesn't contain build secrets, run the following:
4869

4970
```bash
5071
docker pull localhost:5000/test-container:latest
@@ -53,19 +74,24 @@ mkdir -p test-container
5374
tar -xf test-container.tar -C test-container/
5475
cd test-container
5576
# Scan image layers for secrets:
56-
find . -type f | xargs tar -xOf 2>/dev/null | strings | grep -rin "envbuilder-test-secret"
77+
find . -type f | xargs tar -xOf 2>/dev/null | strings | grep -rn "secret-foo"
78+
find . -type f | xargs tar -xOf 2>/dev/null | strings | grep -rn "secret-bar"
5779
# Scan image manifests for secrets:
58-
find . -type f | xargs -n1 grep -rinI 'envbuilder-test-secret'
80+
find . -type f | xargs -n1 grep -rnI 'secret-foo'
81+
find . -type f | xargs -n1 grep -rnI 'secret-bar'
5982
cd ../
6083
```
6184

6285
The output of both find/grep commands should be empty.
63-
To verify that it scans correctly, replace "envbuilder-test-secret" with "Envbuilder" and rerun the commands. It should find strings related to Envbuilder that are not secrets.
86+
To verify that it scans correctly, replace "secret-foo" with "envbuilder" and rerun the commands. It should find strings related to Envbuilder that are not secrets.
87+
88+
### Cleanup
6489

65-
Having verified that no secrets were included in the image, we can now delete the artifacts that we saved to disk.
90+
Having verified that no secrets were included in the image, we can now delete the artifacts that we saved to disk and remove the containers.
6691
```bash
67-
rm -r test-container
68-
rm -r test-container.tar
92+
cd ../
93+
rm -r test-build-secrets
94+
docker stop envbuilder-registry
6995
```
7096

7197
## Security and Production Use

0 commit comments

Comments
 (0)