Skip to content

Commit 197bc16

Browse files
Veetahamedwards
andcommitted
Run the builds as a non-root user
Co-authored-by: medwards <[email protected]>
1 parent 72ec415 commit 197bc16

File tree

5 files changed

+47
-26
lines changed

5 files changed

+47
-26
lines changed

Dockerfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ FROM lambci/lambda:build-provided
33
ARG RUST_VERSION=stable
44
RUN yum install -y jq
55
RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs \
6-
| sh -s -- -y --profile minimal --default-toolchain $RUST_VERSION
6+
| CARGO_HOME=/cargo RUSTUP_HOME=/rustup sh -s -- -y --profile minimal --default-toolchain $RUST_VERSION
77
ADD build.sh /usr/local/bin/
88
VOLUME ["/code"]
99
WORKDIR /code

Makefile

+4-3
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@ test: build
1616

1717
debug: build
1818
@docker run --rm -it \
19+
-u $(id -u):$(id -g) \
1920
-v ${PWD}:/code \
20-
-v ${HOME}/.cargo/registry:/root/.cargo/registry \
21-
-v ${HOME}/.cargo/git:/root/.cargo/git \
21+
-v ${HOME}/.cargo/registry:/cargo/registry \
22+
-v ${HOME}/.cargo/git:/cargo/git \
2223
--entrypoint=/bin/bash \
23-
$(REPO)
24+
$(REPO)

README.md

+24-11
Original file line numberDiff line numberDiff line change
@@ -41,22 +41,33 @@ A typical docker run might look like the following.
4141

4242
```sh
4343
$ docker run --rm \
44+
-u $(id -u):$(id -g) \
4445
-v ${PWD}:/code \
45-
-v ${HOME}/.cargo/registry:/root/.cargo/registry \
46-
-v ${HOME}/.cargo/git:/root/.cargo/git \
46+
-v ${HOME}/.cargo/registry:/cargo/registry \
47+
-v ${HOME}/.cargo/git:/cargo/git \
4748
softprops/lambda-rust
4849
```
49-
> 💡 The -v (volume mount) flags for `/root/.cargo/{registry,git}` are optional but when supplied, provides a much faster turn around when doing iterative development
50+
> 💡 The -v (volume mount) flags for `/cargo/{registry,git}` are optional but when supplied, provides a much faster turn around when doing iterative development
51+
52+
Note that `-u $(id -u):$(id -g)` argument is crucial for the container to produce artifacts
53+
owned by the current host user, otherwise you won't be able to `rm -rf target/lambda`
54+
or run `cargo update`, because the container will write artifacts owned by `root` docker user
55+
to `target/lambda` and `./cargo/{registry,git}` dirs which will break your dev and/or ci environment.
56+
57+
You should also ensure that you do have `${HOME}/.cargo/{registry,git}` dirs created
58+
on your host machine, otherwise docker will create them automatically and assign `root` user
59+
as an owner for these dirs which is unfortunate...
5060

5161
If you are using Windows, the command above may need to be modified to include
5262
a `BIN` environment variable set to the name of the binary to be build and packaged
5363

54-
```sh
64+
```diff
5565
$ docker run --rm \
56-
-e BIN={your-binary-name} \
66+
-u $(id -u):$(id -g) \
67+
+ -e BIN={your-binary-name} \
5768
-v ${PWD}:/code \
58-
-v ${HOME}/.cargo/registry:/root/.cargo/registry \
59-
-v ${HOME}/.cargo/git:/root/.cargo/git \
69+
-v ${HOME}/.cargo/registry:/cargo/registry \
70+
-v ${HOME}/.cargo/git:/cargo/git \
6071
softprops/lambda-rust
6172
```
6273

@@ -65,10 +76,11 @@ This can be especially useful when using path dependencies for local crates.
6576

6677
```sh
6778
$ docker run --rm \
79+
-u $(id -u):$(id -g) \
6880
-v ${PWD}/lambdas/mylambda:/code/lambdas/mylambda \
6981
-v ${PWD}/libs/mylib:/code/libs/mylib \
70-
-v ${HOME}/.cargo/registry:/root/.cargo/registry \
71-
-v ${HOME}/.cargo/git:/root/.cargo/git \
82+
-v ${HOME}/.cargo/registry:/cargo/registry \
83+
-v ${HOME}/.cargo/git:/cargo/git \
7284
-w /code/lambdas/mylambda \
7385
softprops/lambda-rust
7486
```
@@ -102,11 +114,12 @@ You can then invoke this bootstap executable with the lambda-ci docker image for
102114
# Build your function skipping the zip creation step
103115
# You may pass `-e PROFILE=dev` to build using dev profile, but here we use `release`
104116
docker run \
117+
-u $(id -u):$(id -g) \
105118
-e PACKAGE=false \
106119
-e BIN={your-binary-name} \
107120
-v ${PWD}:/code \
108-
-v ${HOME}/.cargo/registry:/root/.cargo/registry \
109-
-v ${HOME}/.cargo/git:/root/.cargo/git \
121+
-v ${HOME}/.cargo/registry:/cargo/registry \
122+
-v ${HOME}/.cargo/git:/cargo/git \
110123
softprops/lambda-rust
111124

112125
# start a one-off docker container replicating the "provided" lambda runtime

build.sh

+5-2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ mkdir -p target/lambda
1212
export PROFILE=${PROFILE:-release}
1313
export PACKAGE=${PACKAGE:-true}
1414
export DEBUGINFO=${DEBUGINFO}
15+
export CARGO_HOME="/cargo"
16+
export RUSTUP_HOME="/rustup"
17+
1518
# cargo uses different names for target
1619
# of its build profiles
1720
if [[ "${PROFILE}" == "release" ]]; then
@@ -32,7 +35,7 @@ export CARGO_TARGET_DIR=$PWD/target/lambda
3235
fi
3336

3437
# source cargo
35-
. $HOME/.cargo/env
38+
. $CARGO_HOME/env
3639

3740
CARGO_BIN_ARG="" && [[ -n "$BIN" ]] && CARGO_BIN_ARG="--bin ${BIN}"
3841

@@ -77,7 +80,7 @@ function package() {
7780

7881
cd "${CARGO_TARGET_DIR}/${TARGET_PROFILE}"
7982
(
80-
. $HOME/.cargo/env
83+
. $CARGO_HOME/env
8184
if [ -z "$BIN" ]; then
8285
IFS=$'\n'
8386
for executable in $(cargo metadata --no-deps --format-version=1 | jq -r '.packages[] | .targets[] | select(.kind[] | contains("bin")) | .name'); do

tests/test.sh

+13-9
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@ source "${HERE}"/bashtest.sh
1212
package_bin() {
1313
rm -rf target/lambda/release > /dev/null 2>&1
1414
docker run --rm \
15+
-u $(id -u):$(id -g) \
1516
-e BIN="$1" \
1617
-v "${PWD}":/code \
17-
-v "${HOME}"/.cargo/registry:/root/.cargo/registry \
18-
-v "${HOME}"/.cargo/git:/root/.cargo/git \
18+
-v "${HOME}"/.cargo/registry:/cargo/registry \
19+
-v "${HOME}"/.cargo/git:/cargo/git \
1920
${IMAGE} && \
2021
ls target/lambda/release/"${1}".zip > /dev/null 2>&1 &&
2122
ls target/lambda/release/output/"${1}"/bootstrap 2>&1 &&
@@ -26,9 +27,10 @@ package_bin() {
2627
package_all() {
2728
rm -rf target/lambda/release > /dev/null 2>&1
2829
docker run --rm \
30+
-u $(id -u):$(id -g) \
2931
-v "${PWD}":/code \
30-
-v "${HOME}"/.cargo/registry:/root/.cargo/registry \
31-
-v "${HOME}"/.cargo/git:/root/.cargo/git \
32+
-v "${HOME}"/.cargo/registry:/cargo/registry \
33+
-v "${HOME}"/.cargo/git:/cargo/git \
3234
${IMAGE} && \
3335
ls target/lambda/release/"${1}".zip > /dev/null 2>&1 &&
3436
ls target/lambda/release/output/"${1}"/bootstrap 2>&1 &&
@@ -39,10 +41,11 @@ package_all() {
3941
compile_without_packaging() {
4042
rm -rf target/lambda/release > /dev/null 2>&1
4143
docker run --rm \
44+
-u $(id -u):$(id -g) \
4245
-e PACKAGE=false \
4346
-v "${PWD}":/code \
44-
-v "${HOME}"/.cargo/registry:/root/.cargo/registry \
45-
-v "${HOME}"/.cargo/git:/root/.cargo/git \
47+
-v "${HOME}"/.cargo/registry:/cargo/registry \
48+
-v "${HOME}"/.cargo/git:/cargo/git \
4649
${IMAGE} &&
4750
!(ls target/lambda/release/"${1}".zip > /dev/null 2>&1) &&
4851
ls target/lambda/release/output/"${1}"/bootstrap 2>&1 &&
@@ -53,10 +56,11 @@ compile_without_packaging() {
5356
package_all_dev_profile() {
5457
rm -rf target/lambda/debug > /dev/null 2>&1
5558
docker run --rm \
59+
-u $(id -u):$(id -g) \
5660
-e PROFILE=dev \
5761
-v "${PWD}":/code \
58-
-v "${HOME}"/.cargo/registry:/root/.cargo/registry \
59-
-v "${HOME}"/.cargo/git:/root/.cargo/git \
62+
-v "${HOME}"/.cargo/registry:/cargo/registry \
63+
-v "${HOME}"/.cargo/git:/cargo/git \
6064
${IMAGE} && \
6165
ls target/lambda/debug/"${1}".zip > /dev/null 2>&1 &&
6266
ls target/lambda/release/output/"${1}"/bootstrap 2>&1 &&
@@ -86,7 +90,7 @@ for project in test-func test-multi-func test-func-with-hooks; do
8690
rm -f output.log > /dev/null 2>&1
8791
rm -f test-out.log > /dev/null 2>&1
8892
rm -rf /tmp/lambda > /dev/null 2>&1
89-
unzip -o \
93+
unzip -o \
9094
target/lambda/release/"${bin_name}".zip \
9195
-d /tmp/lambda > /dev/null 2>&1 && \
9296
docker run \

0 commit comments

Comments
 (0)