Skip to content
This repository was archived by the owner on Feb 5, 2018. It is now read-only.

Commit b43c37f

Browse files
authored
Merge pull request #1 from reload/next-version
Next version
2 parents 22d5a14 + 6c27e64 commit b43c37f

File tree

4 files changed

+40
-19
lines changed

4 files changed

+40
-19
lines changed

Dockerfile

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@ MAINTAINER Arne Jørgensen
33

44
RUN set -x && \
55
apt-get update && \
6-
DEBIAN_FRONTEND=noninteractive apt-get install -y -q golang-go git php-cli php-curl ruby && \
6+
DEBIAN_FRONTEND=noninteractive apt-get install -y -q golang-go git php-cli php-curl ruby python-pip && \
77
GOPATH=/usr/local go get -u github.com/xenolf/lego && \
88
curl -sS https://platform.sh/cli/installer | php && \
9-
curl -sS -o /opt/yamledit.rb https://raw.githubusercontent.com/dbrandenburg/yamledit/master/yamledit.rb && \
10-
DEBIAN_FRONTEND=noninteractive apt-get purge -y -q golang-go && \
9+
curl -sS -o /opt/yamledit.rb https://raw.githubusercontent.com/dbrandenburg/yamledit/e277715d71ed5bac17e97267577dd612fcc7ee2c/yamledit.rb && \
10+
pip install shyaml && \
11+
DEBIAN_FRONTEND=noninteractive apt-get purge -y -q golang-go python-pip && \
1112
apt-get clean -y -q && \
1213
rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
1314

README.md

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
1-
# Let's Encrypt with DNS challenge on platform.sh
1+
# ACME (Let's Encrypt) with DNS challenge on platform.sh
22

33
[Platform.sh](https://platform.sh) currently doesn't support using
4-
Let's Encrypt certificates (at least not with domain verification and
5-
automatic renewal).
4+
ACME/Let's Encrypt certificates (at least not with domain verification
5+
and automatic renewal).
66

7-
This image uses [lego](https://github.com/xenolf/lego) to obtain a
8-
certificate via Let's Encrypts DNS challenge and uploads the
9-
certificate to platform.sh using their commmand line client.
7+
This Docker image provides scripting for obtaining certificates via
8+
ACME/Let's Encrypt and uploading them to Platform.sh using their API.
109

11-
Experimental. YMMV.
10+
This Docker image is based on [lego](https://github.com/xenolf/lego)
11+
to obtain a certificate via ACME DNS challenge and uploads the
12+
certificate to platform.sh using their commmand line client.
1213

1314
Necessary configuration via environment variables, .i.e.:
1415

15-
* `EMAIL=me@example.com` (used for registering with Let's Encrypt)
16+
* `ACME_EMAIL=me@example.com` (used for registering with Let's Encrypt)
1617
* `DOMAINS="example.com www.example.com"` (space separated list --
1718
must already be added to the project at Platform.sh)
1819
* `DNS_PROVIDER=dnsimple` (your DNS provider, see below for supported
@@ -39,8 +40,10 @@ You also need to provide environment variables required by the DNS provider chal
3940

4041
Optional configuration via environment variables:
4142

42-
```
43-
SERVER=https://acme-staging.api.letsencrypt.org/directory
43+
* `ACME_SERVER=https://acme-staging.api.letsencrypt.org/directory`
44+
(optional ACME server -- defaults to Let's Encrypts production server)
45+
* `ACME_DAYS=30` (the number of days left on a certificate to renew
46+
it. Defaults to 30)
4447
```
4548
4649
The container will store the certificates in `/data` so you should

etc/my_init.d/100-platformsh-setup-token

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,8 @@
22

33
TOKEN_FILE=$(mktemp -p /opt)
44
echo "${PLATFORMSH_API_TOKEN}" > "${TOKEN_FILE}"
5+
6+
# Empty platform cache so a new token will be used.
7+
rm -rf /root/.platformsh/cache
8+
59
/usr/bin/ruby /opt/yamledit.rb -f /root/.platformsh/config.yaml -n -k api,token_file -v "${TOKEN_FILE}" --force

usr/local/bin/lego-platform.sh

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,19 @@ trap cleanup EXIT
1515
IFS=' ' read -r -a domains <<< "${DOMAINS}"
1616
LEGOPATH=/data
1717
export HOME=/root
18-
SERVER=${SERVER:-https://acme-v01.api.letsencrypt.org/directory}
18+
ACME_SERVER=${ACME_SERVER:-https://acme-v01.api.letsencrypt.org/directory}
19+
ACME_DAYS=${ACME_DAYS:-30}
1920

2021
verify_preconditions () {
21-
[ ! -z "${SERVER}" ] && [ ! -z "${EMAIL}" ] && [ ! -z "${DNS_PROVIDER}" ] && verify_domains_in_platformsh
22+
[ ! -z "${ACME_SERVER}" ] && [ ! -z "${ACME_EMAIL}" ] && [ ! -z "${DNS_PROVIDER}" ] && verify_domains_in_platformsh
2223
}
2324

2425
verify_domains_in_platformsh () {
2526
local status=0
2627

2728
for i in "${!domains[@]}"
2829
do
29-
platform domain:get --yes --project="${PLATFORMSH_PROJECT_ID}" "${domains[i]}"
30+
platform domain:get --no --project="${PLATFORMSH_PROJECT_ID}" "${domains[i]}"
3031
local err=$?
3132
status=$((${err}|${status}))
3233
done
@@ -53,12 +54,12 @@ domain_exists () {
5354

5455
create_domain () {
5556
local domain=$1
56-
lego --domains=${domain} --server=${SERVER} --email=${EMAIL} --accept-tos --path=${LEGOPATH} --dns=${DNS_PROVIDER} run
57+
lego --domains=${domain} --server=${ACME_SERVER} --email=${ACME_EMAIL} --accept-tos --path=${LEGOPATH} --dns=${DNS_PROVIDER} run
5758
}
5859

5960
renew_domain () {
6061
local domain=$1
61-
lego --domains=${domain} --server=${SERVER} --email=${EMAIL} --accept-tos --path=${LEGOPATH} --dns=${DNS_PROVIDER} renew --days=60
62+
lego --domains=${domain} --server=${ACME_SERVER} --email=${ACME_EMAIL} --accept-tos --path=${LEGOPATH} --dns=${DNS_PROVIDER} renew --days=${ACME_DAYS}
6263
}
6364

6465
upload_certificates () {
@@ -76,7 +77,19 @@ upload_certificate () {
7677
local cert=${TMPDIR}/cert-01
7778
local key=${LEGOPATH}/certificates/${domain}.key
7879
local chain=${TMPDIR}/cert-02
79-
platform domain:update --yes --cert=${cert} --key=${key} --chain=${chain} --project="${PLATFORMSH_PROJECT_ID}" "${domain}"
80+
local current=${TMPDIR}/current
81+
82+
# Compare certificate to current certificate at platform.sh. Only upload if they are different.
83+
84+
# We allow the following commands to fail because there might not be a current certificate.
85+
set -x
86+
platform domain:get --no --project="${PLATFORMSH_PROJECT_ID}" --property=ssl "${domain}" | shyaml get-value certificate > "${current}"
87+
88+
if [ "$(openssl x509 -in "${cert}" -noout -fingerprint)" != "$(openssl x509 -in "${current}" -noout -fingerprint)" ]; then
89+
platform domain:update --no --cert=${cert} --key=${key} --chain=${chain} --project="${PLATFORMSH_PROJECT_ID}" "${domain}"
90+
fi
91+
92+
set +x
8093
}
8194

8295
verify_preconditions && create_or_renew_domains && upload_certificates

0 commit comments

Comments
 (0)