Skip to content

Commit 88689aa

Browse files
committed
Use container secrets securely
* increases the default password length from 40 to 256 characters * adds a new ENV variable DOCKER_STEPCA_PASSWORD_FILE so the password file location can be changed * adds set_password_files() to entrypoint.sh so /home/step/secrets/password becomes a symlink in containers pointing to DOCKER_STEPCA_INIT_PASSWORD_FILE & also DOCKER_STEPCA_PASSWORD_FILE so file permissions are retained * adds podman example quadlet / run command with a 378,000 character secret * small update to README.md for new podman examples / docker examples Fixes #2270
1 parent 30e79a2 commit 88689aa

File tree

4 files changed

+106
-3
lines changed

4 files changed

+106
-3
lines changed

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,9 @@ See our installation docs [here](https://smallstep.com/docs/step-ca/installation
136136
[on smallstep.com](https://smallstep.com/docs/step-cli/reference/),
137137
or by running `step help --http=:8080` from the command line
138138
and visiting http://localhost:8080.
139+
* [Examples](https://github.com/smallstep/certificates/tree/master/examples) including
140+
[podman](https://github.com/smallstep/certificates/tree/master/examples/podman) &
141+
[docker](https://github.com/smallstep/certificates/tree/master/examples/docker)
139142

140143
## Feedback?
141144

docker/entrypoint.sh

+11-3
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,16 @@ function init_if_possible () {
2727

2828
function generate_password () {
2929
set +o pipefail
30-
< /dev/urandom tr -dc A-Za-z0-9 | head -c40
30+
< /dev/urandom tr -dc A-Za-z0-9 | head -c256
3131
echo
3232
set -o pipefail
3333
}
3434

35+
function set_password_files () {
36+
ln -sf "${DOCKER_STEPCA_INIT_PASSWORD_FILE}" "${STEPPATH}/password"
37+
ln -sf "${DOCKER_STEPCA_INIT_PASSWORD_FILE}" "${STEPPATH}/provisioner_password"
38+
}
39+
3540
# Initialize a CA if not already initialized
3641
function step_ca_init () {
3742
DOCKER_STEPCA_INIT_PROVISIONER_NAME="${DOCKER_STEPCA_INIT_PROVISIONER_NAME:-admin}"
@@ -47,8 +52,7 @@ function step_ca_init () {
4752
--address "${DOCKER_STEPCA_INIT_ADDRESS}"
4853
)
4954
if [ -n "${DOCKER_STEPCA_INIT_PASSWORD_FILE}" ]; then
50-
cat < "${DOCKER_STEPCA_INIT_PASSWORD_FILE}" > "${STEPPATH}/password"
51-
cat < "${DOCKER_STEPCA_INIT_PASSWORD_FILE}" > "${STEPPATH}/provisioner_password"
55+
set_password_files
5256
elif [ -n "${DOCKER_STEPCA_INIT_PASSWORD}" ]; then
5357
echo "${DOCKER_STEPCA_INIT_PASSWORD}" > "${STEPPATH}/password"
5458
echo "${DOCKER_STEPCA_INIT_PASSWORD}" > "${STEPPATH}/provisioner_password"
@@ -86,4 +90,8 @@ if [ ! -f "${STEPPATH}/config/ca.json" ]; then
8690
init_if_possible
8791
fi
8892

93+
if [ -n "${DOCKER_STEPCA_PASSWORD_FILE}" ]; then
94+
set_password_files
95+
fi
96+
8997
exec "${@}"

examples/podman/stepca.container.md

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
## Example [Podman Quadlet container](https://docs.podman.io/en/latest/markdown/podman-systemd.unit.5.html#container-units-container) file
2+
3+
* `~/.config/containers/systemd/stepca.container` (rootless)
4+
* `/etc/containers/systemd/stepca.container` (rootful)
5+
6+
```
7+
[Unit]
8+
Description=Smallstep Certificate Authority
9+
After=network-online.target
10+
11+
[Container]
12+
PodmanArgs=--memory 25m --cpus 0.20
13+
PidsLimit=50
14+
DropCapability=ALL
15+
AutoUpdate=registry
16+
ContainerName=stepca
17+
DropCapability=ALL
18+
Environment=TZ="Europe/London"
19+
Environment="DOCKER_STEPCA_INIT_NAME=Example CA"
20+
Environment=DOCKER_STEPCA_INIT_DNS_NAMES=ca.custom.domain,10.89.0.10,localhost,127.0.0.1
21+
Environment=DOCKER_STEPCA_INIT_PROVISIONER_NAME=admin@custom.domain
22+
Environment=DOCKER_STEPCA_INIT_SSH=true
23+
Environment=DOCKER_STEPCA_INIT_ACME=true
24+
Environment=DOCKER_STEPCA_INIT_PASSWORD_FILE=/run/secrets/stepca
25+
HostName=stepca
26+
PodmanArgs=--privileged
27+
# Alpine image
28+
Image=docker.io/smallstep/step-ca
29+
# Debian image with TPM support
30+
#Image=docker.io/smallstep/step-ca:hsm
31+
PublishPort=10.89.0.10:9000:9000/tcp
32+
PublishPort=127.0.0.1:9000:9000/tcp
33+
Secret=source=stepca,type=mount,uid=1000,gid=1000,mode=400
34+
Volume=/path/to/volumes/stepca/config:/home/step:Z
35+
DNS=10.89.0.1
36+
DNSOption=~custom.domain
37+
38+
[Service]
39+
Restart=always
40+
41+
[Install]
42+
WantedBy=default.target
43+
```
44+
45+
* Contributed by: [Stuart Cardall](https://github.com/itoffshore)

examples/podman/stepca.run.md

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
## Example creation of a Podman container & secret
2+
3+
* Using a [cryptographically strong secret of 378,000 characters](https://docs.podman.io/en/latest/markdown/podman-secret-create.1.html#examples)
4+
5+
see also:
6+
7+
- [Create a "quadlet"](https://github.com/containers/podlet)
8+
- [Netbird VPN](https://github.com/netbirdio/netbird)
9+
- [examples/podman/stepca.container.md](https://github.com/smallstep/certificates/tree/master/examples/podman/stepca.container.md)
10+
11+
```
12+
iface=wt0 # running over netbird
13+
ctr=stepca
14+
ip=$(ip -f inet addr show $iface | sed -En -e 's/.*inet ([0-9.]+).*/\1/p')
15+
repo=docker.io/smallstep/step-ca
16+
# TPM supported image
17+
# repo=docker.io/smallstep/step-ca:hsm
18+
dns="ca.custom.domain,$ip,localhost,127.0.0.1"
19+
20+
ca="My CA"
21+
22+
####################
23+
# auto config #
24+
####################
25+
26+
bytes=378000
27+
openssl rand -base64 $bytes | podman secret create --replace $ctr -
28+
29+
podman run -d --replace \
30+
--name $ctr \
31+
--hostname $ctr \
32+
--secret source=$ctr,type=mount,uid=1000,gid=1000,mode=400 \
33+
--env "DOCKER_STEPCA_INIT_NAME=$ca" \
34+
--env "DOCKER_STEPCA_INIT_DNS_NAMES=$dns" \
35+
--env "DOCKER_STEPCA_INIT_PROVISIONER_NAME=$email" \
36+
--env "DOCKER_STEPCA_INIT_SSH=true" \
37+
--env "DOCKER_STEPCA_INIT_ACME=true" \
38+
--env "DOCKER_STEPCA_PASSWORD_FILE=/run/secrets/$ctr" \
39+
--cap-drop ALL \
40+
--restart always \
41+
--privileged \
42+
--label "io.containers.autoupdate=registry" \
43+
-v ${HOME}/volumes/$ctr/config:/home/step:Z \
44+
$repo
45+
```
46+
47+
* Contributed by: [Stuart Cardall](https://github.com/itoffshore)

0 commit comments

Comments
 (0)