Skip to content

Commit 30b30d7

Browse files
committed
Add example of nginx+step-ca
1 parent e0877a0 commit 30b30d7

16 files changed

+248
-0
lines changed

examples/docker/Makefile

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
all: binaries build deploy
2+
3+
binaries:
4+
GOOS=linux go build -o ca/step-ca github.com/smallstep/certificates/cmd/step-ca
5+
GOOS=linux go build -o renewer/step github.com/smallstep/cli/cmd/step
6+
7+
build: build-nginx build-ca build-renewer
8+
9+
build-nginx:
10+
docker build -t nginx-test:latest nginx
11+
build-ca:
12+
docker build -t step-ca-test:latest ca
13+
build-renewer:
14+
docker build -t step-renewer-test:latest renewer
15+
16+
deploy:
17+
docker stack deploy -c docker-compose.yml steplab
18+
19+
clean:
20+
docker service rm steplab_ca steplab_nginx steplab_renewer
21+
sleep 20
22+
docker volume rm -f steplab_certificates
23+
24+
ls:
25+
docker service ls
26+
27+
ps:
28+
docker ps
29+
30+
logs:
31+
docker service ls | grep steplab | awk '{print $1}' | xargs -n 1 docker service logs
32+
33+
inspect:
34+
step certificate inspect https://localhost:4443 --insecure

examples/docker/ca/Dockerfile

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
FROM alpine
2+
3+
ADD step-ca /usr/local/bin/step-ca
4+
COPY pki /run
5+
6+
# Cron && Nginx
7+
CMD ["step-ca", "/run/config/ca.json"]

examples/docker/ca/pki/config/ca.json

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
{
2+
"root": "/run/secrets/root_ca.crt",
3+
"crt": "/run/secrets/intermediate_ca.crt",
4+
"key": "/run/secrets/intermediate_ca_key",
5+
"password": "password",
6+
"address": ":443",
7+
"dnsNames": [
8+
"ca"
9+
],
10+
"logger": {
11+
"format": "text"
12+
},
13+
"authority": {
14+
"provisioners": [
15+
{
16+
"name": "mariano@smallstep.com",
17+
"type": "jwk",
18+
"key": {
19+
"use": "sig",
20+
"kty": "EC",
21+
"kid": "DmAtZt2EhmZr_iTJJ387fr4Md2NbzMXGdXQNW1UWPXk",
22+
"crv": "P-256",
23+
"alg": "ES256",
24+
"x": "jXoO1j4CXxoTC32pNzkVC8l6k2LfP0k5ndhJZmcdVbk",
25+
"y": "c3JDL4GTFxJWHa8EaHdMh4QgwMh64P2_AGWrD0ADXcI"
26+
},
27+
"encryptedKey": "eyJhbGciOiJQQkVTMi1IUzI1NitBMTI4S1ciLCJjdHkiOiJqd2sranNvbiIsImVuYyI6IkEyNTZHQ00iLCJwMmMiOjEwMDAwMCwicDJzIjoiOTFVWjdzRGw3RlNXcldfX1I1NUh3USJ9.FcWtrBDNgrkA33G9Ll9sXh1cPF-3jVXeYe1FLmSDc_Q2PmfLOPvJOA.0ZoN32ayaRWnufJb.WrkffMmDLWiq1-2kn-w7-kVBGW12gjNCBHNHB1hyEdED0rWH1YWpKd8FjoOACdJyLhSn4kAS3Lw5AH7fvO27A48zzvoxZU5EgSm5HG9IjkIH-LBJ-v79ShkpmPylchgjkFhxa5epD11OIK4rFmI7s-0BCjmJokLR_DZBhDMw2khGnsr_MEOfAz9UnqXaQ4MIy8eT52xUpx68gpWFlz2YP3EqiYyNEv0PpjMtyP5lO2i8-p8BqvuJdus9H3fO5Dg-1KVto1wuqh4BQ2JKTauv60QAnM_4sdxRHku3F_nV64SCrZfDvnN2ve21raFROtyXaqHZhN6lyoPxDncy8v4.biaOblEe0N-gMpJyFZ-3-A"
28+
},
29+
{
30+
"name": "mike@smallstep.com",
31+
"type": "jwk",
32+
"key": {
33+
"use": "sig",
34+
"kty": "EC",
35+
"kid": "YYNxZ0rq0WsT2MlqLCWvgme3jszkmt99KjoGEJJwAKs",
36+
"crv": "P-256",
37+
"alg": "ES256",
38+
"x": "LsI8nHBflc-mrCbRqhl8d3hSl5sYuSM1AbXBmRfznyg",
39+
"y": "F99LoOvi7z-ZkumsgoHIhodP8q9brXe4bhF3szK-c_w"
40+
},
41+
"encryptedKey": "eyJhbGciOiJQQkVTMi1IUzI1NitBMTI4S1ciLCJjdHkiOiJqd2sranNvbiIsImVuYyI6IkEyNTZHQ00iLCJwMmMiOjEwMDAwMCwicDJzIjoiVERQS2dzcEItTUR4ZDJxTGo0VlpwdyJ9.2_j0cZgTm2eFkZ-hrtr1hBIvLxN0w3TZhbX0Jrrq7vBMaywhgFcGTA.mCasZCbZJ-JT7vjA.bW052WDKSf_ueEXq1dyxLq0n3qXWRO-LXr7OzBLdUKWKSBGQrzqS5KJWqdUCPoMIHTqpwYvm-iD6uFlcxKBYxnsAG_hoq_V3icvvwNQQSd_q7Thxr2_KtPIDJWNuX1t5qXp11hkgb-8d5HO93CmN7xNDG89pzSUepT6RYXOZ483mP5fre9qzkfnrjx3oPROCnf3SnIVUvqk7fwfXuniNsg3NrNqncHYUQNReiq3e9I1R60w0ZQTvIReY7-zfiq7iPgVqmu5I7XGgFK4iBv0L7UOEora65b4hRWeLxg5t7OCfUqrS9yxAk8FdjFb9sEfjopWViPRepB0dYPH8dVI.fb6-7XWqp0j6CR9Li0NI-Q",
42+
"claims": {
43+
"minTLSCertDuration": "60s",
44+
"defaultTLSCertDuration": "120s"
45+
}
46+
}
47+
]
48+
},
49+
"tls": {
50+
"cipherSuites": [
51+
"TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305",
52+
"TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256"
53+
],
54+
"minVersion": 1.2,
55+
"maxVersion": 1.2,
56+
"renegotiation": false
57+
}
58+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
-----BEGIN CERTIFICATE-----
2+
MIIBxjCCAWugAwIBAgIQAYoOWhdChUmmKzlc0DWcWDAKBggqhkjOPQQDAjAcMRow
3+
GAYDVQQDExFTbWFsbHN0ZXAgUm9vdCBDQTAeFw0xODExMDIyMzU0MTNaFw0yODEw
4+
MzAyMzU0MTNaMCQxIjAgBgNVBAMTGVNtYWxsc3RlcCBJbnRlcm1lZGlhdGUgQ0Ew
5+
WTATBgcqhkjOPQIBBggqhkjOPQMBBwNCAASxvIWme8/yDAxkR63KgSYkpN7mHKBH
6+
k5c8S+uzba4xWbaxZtEZ9NNhEIAgYFZ9/3ThrzLOsuGwRCvPTaD5iycQo4GGMIGD
7+
MA4GA1UdDwEB/wQEAwIBpjAdBgNVHSUEFjAUBggrBgEFBQcDAQYIKwYBBQUHAwIw
8+
EgYDVR0TAQH/BAgwBgEB/wIBADAdBgNVHQ4EFgQU8dKIy5ZLH2h3ihWgqjcpoo5e
9+
q3YwHwYDVR0jBBgwFoAU0IpOvAyBnn9UhDqOQzXnfEU3aYMwCgYIKoZIzj0EAwID
10+
SQAwRgIhANXlcktuaEvORhgRvzQ6vVNgvpqCEXW3CcCHjUl1xSdaAiEAmakkpfFq
11+
VsT5PqPnTRgOWlFESRhQ9btl6nQ+2Lt/S5A=
12+
-----END CERTIFICATE-----
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
-----BEGIN EC PRIVATE KEY-----
2+
Proc-Type: 4,ENCRYPTED
3+
DEK-Info: AES-256-CBC,4c7758e66df1884f6560839de64d4dd3
4+
5+
S8Ha8uA+bA3IGPurYODwd9VaJZ6FHI2tlznHXCOxT1MlGqyEAc4aWS11QBUz0Ucp
6+
excwlqM8kfh5BcN5a+vvInHnv74ZiNPdpt/apzz2LIx52pApzASiKVXRsAUmR4Pv
7+
3MsO1/cVHkilpee1uC+axL32d5YmyP0URpSNJK9BhZo=
8+
-----END EC PRIVATE KEY-----
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
-----BEGIN CERTIFICATE-----
2+
MIIBfDCCASGgAwIBAgIQY0CXerxuM+EhTbpVxxLRKjAKBggqhkjOPQQDAjAcMRow
3+
GAYDVQQDExFTbWFsbHN0ZXAgUm9vdCBDQTAeFw0xODExMDIyMzU0MTNaFw0yODEw
4+
MzAyMzU0MTNaMBwxGjAYBgNVBAMTEVNtYWxsc3RlcCBSb290IENBMFkwEwYHKoZI
5+
zj0CAQYIKoZIzj0DAQcDQgAEEGa7ZeL4WVIfPFDS7glJkIVsITVQgjfyz+AhcYaS
6+
rkJZlWOGZ60br9uE/wEfUcX1zavrX1Wz+bSJzTvT0AVBNqNFMEMwDgYDVR0PAQH/
7+
BAQDAgGmMBIGA1UdEwEB/wQIMAYBAf8CAQEwHQYDVR0OBBYEFNCKTrwMgZ5/VIQ6
8+
jkM153xFN2mDMAoGCCqGSM49BAMCA0kAMEYCIQCRA4EdlTTMhs2Zd1cT75ZgxeGa
9+
mjVPl1vqBxLkHqEO+QIhAPKVm7E452ZBe2o5rQRxGwa94MI+CyuEIH9md3nTgWWX
10+
-----END CERTIFICATE-----
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
-----BEGIN EC PRIVATE KEY-----
2+
Proc-Type: 4,ENCRYPTED
3+
DEK-Info: AES-256-CBC,98fdc560ba714aebb9fd4b714395d8ce
4+
5+
2bFn8yRb8lMvDR6oh22PocfhXdaoVNt4QwHCJNy0K0fG8CMokwDfEec//LseP6rA
6+
7/EV11+ZgoN9xyTNe1kB6zFv7/kzCpRm23sqtyio+8xXWnLZNYKBRYYEeJWBUqqd
7+
GAfazg4ZFzoIH5TEPWCEAp7M9CVvtiw1SeA/zjewp2k=
8+
-----END EC PRIVATE KEY-----

examples/docker/docker-compose.yml

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
version: '3.3'
2+
3+
services:
4+
ca:
5+
image: step-ca-test:latest
6+
ports:
7+
- "8443:443"
8+
restart: always
9+
10+
renewer:
11+
depends_on:
12+
- ca
13+
image: step-renewer-test:latest
14+
volumes:
15+
- certificates:/var/local/step
16+
secrets:
17+
- password
18+
environment:
19+
STEPPATH: /home/step
20+
STEP_CA_URL: https://ca
21+
STEP_FINGERPRINT: 84a033e84196f73bd593fad7a63e509e57fd982f02084359c4e8c5c864efc27d
22+
STEP_ROOT: /var/local/step/root_ca.crt
23+
STEP_KID: DmAtZt2EhmZr_iTJJ387fr4Md2NbzMXGdXQNW1UWPXk
24+
STEP_PASSWORD_FILE: /run/secrets/password
25+
COMMON_NAME: nginx
26+
27+
nginx:
28+
depends_on:
29+
- renewer
30+
image: nginx-test:latest
31+
ports:
32+
- "4443:443"
33+
volumes:
34+
- certificates:/var/local/step:ro
35+
restart: always
36+
37+
volumes:
38+
certificates:
39+
40+
secrets:
41+
password:
42+
file: ./password.txt

examples/docker/nginx/Dockerfile

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
FROM nginx:alpine
2+
3+
RUN apk add inotify-tools
4+
RUN mkdir -p /var/local/step
5+
COPY site.conf /etc/nginx/conf.d/
6+
COPY certwatch.sh /
7+
COPY entrypoint.sh /
8+
9+
# Cron && Nginx
10+
ENTRYPOINT ["/entrypoint.sh"]
11+
CMD ["nginx", "-g", "daemon off;"]

examples/docker/nginx/certwatch.sh

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/bin/sh
2+
3+
while true; do
4+
inotifywait -e modify /var/local/step/site.crt
5+
nginx -s reload
6+
done

examples/docker/nginx/entrypoint.sh

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/bin/sh
2+
3+
# Wait for renewer
4+
sleep 10
5+
6+
# watch for the update of the cert and reload nginx
7+
/certwatch.sh &
8+
9+
# Run docker CMD
10+
exec "$@"

examples/docker/nginx/site.conf

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
server {
2+
listen 443 ssl;
3+
server_name localhost;
4+
ssl_certificate /var/local/step/site.crt;
5+
ssl_certificate_key /var/local/step/site.key;
6+
7+
location / {
8+
root /usr/share/nginx/html;
9+
index index.html index.htm;
10+
}
11+
}

examples/docker/password.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
password

examples/docker/renewer/Dockerfile

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
FROM alpine:latest
2+
3+
RUN mkdir -p /var/local/step
4+
ADD step /usr/local/bin/step
5+
ADD crontab /var/spool/cron/crontabs/root
6+
RUN chmod 0644 /var/spool/cron/crontabs/root
7+
8+
COPY entrypoint.sh /
9+
ENTRYPOINT ["/entrypoint.sh"]
10+
CMD ["/usr/sbin/crond", "-l", "2", "-f"]

examples/docker/renewer/crontab

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# min hour day month weekday command
2+
* * * * * rm -f /var/local/step/site-new.crt && step ca renew --out /var/local/step/site-new.crt /var/local/step/site.crt /var/local/step/site.key && mv /var/local/step/site-new.crt /var/local/step/site.crt

examples/docker/renewer/entrypoint.sh

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/bin/sh
2+
3+
# Wait for CA
4+
sleep 5
5+
6+
if [ ! -f /var/local/step/root_ca.crt ]; then
7+
# Donwload the root certificate
8+
step ca root /var/local/step/root_ca.crt
9+
fi
10+
11+
if [ ! -f /var/local/step/site.crt ]; then
12+
# Get token
13+
STEP_TOKEN=$(step ca token $COMMON_NAME)
14+
# Donwload the root certificate
15+
step ca certificate --token $STEP_TOKEN $COMMON_NAME /var/local/step/site.crt /var/local/step/site.key
16+
fi
17+
18+
exec "$@"

0 commit comments

Comments
 (0)