Skip to content

Commit ca682e0

Browse files
authored
Merge pull request #11 from balena-os/kyle/addusers
Create users and run ssh server
2 parents d9bf5d3 + 22eef76 commit ca682e0

File tree

14 files changed

+81
-22
lines changed

14 files changed

+81
-22
lines changed

docker-compose.yml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,17 @@ services:
44
yocto-build-env:
55
build: yocto-build-env
66
privileged: true
7+
ports:
8+
- 22:22
79
tmpfs:
810
- /tmp
911
- /run
1012
volumes:
11-
- work:/work
13+
- home:/home
1214
- docker:/var/lib/docker
15+
- ssh:/etc/ssh
1316

1417
volumes:
15-
work: {}
18+
home: {}
1619
docker: {}
20+
ssh: {}

yocto-build-env/Dockerfile

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ ENV LANG en_US.UTF-8
2222
# Additional host packages required by balena
2323
# hadolint ignore=DL3008
2424
RUN apt-get update \
25-
&& apt-get install -y --no-install-recommends apt-transport-https ca-certificates curl git gnupg lsb-release sudo uidmap \
25+
&& apt-get install -y --no-install-recommends apt-transport-https ca-certificates curl git gnupg lsb-release openssh-server sudo uidmap \
2626
&& rm -rf /var/lib/apt/lists/*
2727

2828
SHELL ["/bin/bash", "-o", "pipefail", "-c"]
@@ -84,21 +84,17 @@ RUN tar -C / -Jxpf /tmp/s6-overlay-x86_64.tar.xz
8484

8585
ENV S6_KEEP_ENV 1
8686
ENV S6_READ_ONLY_ROOT 1
87+
ENV S6_CMD_WAIT_FOR_SERVICES_MAXTIME 0
8788

8889
# install s6-overlay
8990
COPY s6-overlay /etc/s6-overlay
91+
RUN chmod +x /etc/s6-overlay/scripts/*
9092

91-
RUN adduser --disabled-password --gecos "" --uid 1000 nonroot \
92-
&& usermod -aG sudo nonroot \
93-
&& usermod -aG docker nonroot \
94-
&& echo "%sudo ALL=(ALL:ALL) NOPASSWD:ALL" > /etc/sudoers \
93+
# allow sudo without password
94+
RUN echo "%sudo ALL=(ALL:ALL) NOPASSWD:ALL" > /etc/sudoers \
9595
&& echo "Defaults env_keep += \"DEBIAN_FRONTEND\"" >> /etc/sudoers
9696

97-
WORKDIR /work
98-
99-
RUN chown -R nonroot:nonroot /work
100-
101-
# do not switch to nonroot for s6-overlay
102-
# see https://github.com/just-containers/s6-overlay#user-directive
97+
VOLUME /home
98+
VOLUME /etc/ssh
10399

104100
ENTRYPOINT [ "/init" ]

yocto-build-env/s6-overlay/s6-rc.d/addusers/dependencies.d/base

Whitespace-only changes.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
oneshot
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/etc/s6-overlay/scripts/addusers
Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
11
#!/usr/bin/env bash
22

33
rm -f /var/run/docker.pid
4-
5-
# # halt the container if dockerd stops
6-
# if [ "$1" -ne 0 ]; then
7-
# echo "$1" >/run/s6-linux-init-container-results/exitcode
8-
# /run/s6/basedir/bin/halt
9-
# fi

yocto-build-env/s6-overlay/s6-rc.d/dockerd/run

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#!/usr/bin/env bash
22

3-
set -ae
3+
set -euo pipefail
44

5-
[[ ${VERBOSE,,} =~ true|yes|on|1 ]] && set -x
5+
[[ ${VERBOSE:-,,} =~ true|yes|on|1 ]] && set -x
66

77
DOCKER_REGISTRY_MIRROR_INTERNAL=${DOCKER_REGISTRY_MIRROR_INTERNAL:-""}
88
DOCKER_REGISTRY_MIRROR=${DOCKER_REGISTRY_MIRROR:-""}
@@ -25,4 +25,4 @@ if [ -n "${DOCKER_REGISTRY_MIRROR}" ]; then
2525
fi
2626

2727
# shellcheck disable=SC2086
28-
exec dockerd "${dockerd_args[@]}" ${EXTRA_DOCKERD_ARGS} 2>&1
28+
exec dockerd "${dockerd_args[@]}" ${EXTRA_DOCKERD_ARGS:-} 2>&1

yocto-build-env/s6-overlay/s6-rc.d/sshd/dependencies.d/base

Whitespace-only changes.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/usr/bin/env bash
2+
3+
rm -f /var/run/sshd.pid
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/usr/bin/env bash
2+
3+
set -euo pipefail
4+
5+
[[ ${VERBOSE:-,,} =~ true|yes|on|1 ]] && set -x
6+
7+
mkdir -p /run/sshd
8+
9+
ssh-keygen -A
10+
11+
# run sshd in the foreground
12+
/usr/sbin/sshd -De \
13+
-o "LogLevel=${SSHD_LOG_LEVEL:-INFO}" \
14+
-o PermitRootLogin=no \
15+
-o PasswordAuthentication=no \
16+
-o PubkeyAuthentication=yes \
17+
-o UsePAM=yes \
18+
-o AcceptEnv="LANG LC_*" \
19+
-o PrintMotd=no \
20+
-o Banner=none
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
longrun

yocto-build-env/s6-overlay/s6-rc.d/user/contents.d/addusers

Whitespace-only changes.

yocto-build-env/s6-overlay/s6-rc.d/user/contents.d/sshd

Whitespace-only changes.
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#!/usr/bin/env bash
2+
3+
set -euo pipefail
4+
5+
[[ ${VERBOSE:-,,} =~ true|yes|on|1 ]] && set -x
6+
7+
GH_HANDLES="${GH_HANDLES:-alexgg klutchell jakogut mtoman floion acostach majorz lmbarros}"
8+
9+
fetch_ssh_keys() {
10+
local _username="${1}"
11+
local _home="${2}"
12+
(
13+
cd "${_home}" || exit 1
14+
mkdir -p .ssh
15+
curl -fsSL "https://github.com/${_username}.keys" >>.ssh/authorized_keys
16+
chown -R "${_username}:${_username}" .ssh
17+
chmod -R 700 .ssh
18+
)
19+
}
20+
21+
for username in ${GH_HANDLES:-}; do
22+
home="$(eval echo ~"${username}")"
23+
24+
if [ -d "${home}" ]; then
25+
# create the user with the same uid as the existing home directory
26+
uid="$(stat -c "%u" "${home}")"
27+
adduser --disabled-password --gecos "${username}" "${username}" --uid "${uid}"
28+
else
29+
# create a new user and home directory
30+
adduser --disabled-password --gecos "${username}" "${username}"
31+
fi
32+
33+
# add the user to the sudo and docker groups
34+
usermod -aG sudo "${username}" || true
35+
usermod -aG docker "${username}" || true
36+
37+
# fetch the user's ssh keys from github
38+
fetch_ssh_keys "${username}" "${home}" || true
39+
done

0 commit comments

Comments
 (0)