Skip to content

Commit 97dbbe2

Browse files
authored
Merge pull request #1 from certkit-io/s3-directory-changes
Update Pathing When Requesting Certs From Storage
2 parents cb13a80 + 947969e commit 97dbbe2

File tree

4 files changed

+40
-29
lines changed

4 files changed

+40
-29
lines changed

README.md

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,18 @@
11
# Ansible Role: certkit_io.sync
22

3-
An Ansible Role for Linux that installs a script which synchronizes SSL certificates managed by [Certkit.io](https://www.certkit.io/).
3+
An Ansible Role for Linux that installs a script which synchronizes SSL certificates managed by [Certkit.io](https://www.certkit.io/). It is published on Ansible Galaxy [here](https://galaxy.ansible.com/ui/standalone/roles/certkit_io/sync/install/).
4+
5+
## Overview
6+
7+
* Installs a simple synchronization script to whichever directory you specify.
8+
* Builds a configuration file from variables you give it.
9+
* Once installed, the script:
10+
* Syncs the latest certificate from CertKit into a local directory using [minio-client](https://docs.min.io/community/minio-object-store/reference/minio-mc.html#quickstart).
11+
* Copies the certificate into place if it is changed or missing.
12+
* Optionally runs a post-update command (e.g. `nginx -s reload`).
13+
* Logs all activity to `certkit.log` (keeping last 2000 log lines)
14+
* Is periodically run on a Cron schedule.
15+
* To sync multiple certificates, call the role multiple times. See the [Syncing Multiple Certificates](#syncing-multiple-certificates) section.
416

517
## Requirements
618

@@ -21,7 +33,7 @@ All variables are listed below. They are all required, unless otherwise specifie
2133
- `certkit_bucket`: The name of your certkit storage bucket. Get this from the Certkit UI.
2234
- `certkit_access_key`: The access key for your certkit storage bucket. Get this from the Certkit UI.
2335
- `certkit_secret_key`: The secret key for your certkit storage bucket. Get this from the Certkit UI.
24-
- `certkit_common_name`: The domain name of the certificate. Prefix with * if it's a wildcard.
36+
- `certkit_certificate_id`: The ID of the certificate to sync. Get this from the Certkit UI.
2537
- `certkit_dir`: The directory where the certkit sync script and config file will be placed. Arbitrary, pick what you'd like. Should be unique if multiple certkit scripts are installed on the same box!
2638
- `certkit_update_cmd`: Certkit sync runs this command whenever the certificates are updated. Use to inform the server of a new certificate.
2739
- `certkit_pem_destination`: File path where Certkit sync will write the certificate PEM file. This is wherever your server software expects the certificate to live.
@@ -45,8 +57,8 @@ None.
4557
certkit_access_key: YOUR_ACCESS_KEY
4658
certkit_secret_key: YOUR_SECRET_KEY
4759

48-
# This is the common name/domain of the certificate. If it's wildcard, prefix with *.
49-
certkit_common_name: "*.yourdomain.com"
60+
# The ID of the certificate to sync. Get this from the Certkit UI.
61+
certkit_certificate_id: ab12
5062

5163
# The directory where the certkit sync script and config file will be placed. Arbitrary, pick what you'd like.
5264
# When syncing multiple certificates, each configuration should use a different directory.
@@ -64,10 +76,11 @@ None.
6476
## Syncing Multiple Certificates
6577
6678
Sync more than one certificate by simply calling the role again. These variables will differ between each certificate:
67-
- `certkit_common_name`
68-
- `certkit_dir`
69-
- `certkit_pem_destination`
70-
- `certkit_key_destination`
79+
80+
- `certkit_certificate_id`
81+
- `certkit_dir`
82+
- `certkit_pem_destination`
83+
- `certkit_key_destination`
7184

7285
## License
7386

files/certkit-sync.sh

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ IFS=$'\n\t'
44

55
# Expecting certkit.conf file to exist within the same directory as this script.
66
# Expected vars:
7-
# CERTKIT_CERTIFICATE_DOMAIN
87
# CERTKIT_S3_ACCESS_KEY
98
# CERTKIT_S3_SECRET_KEY
109
# CERTKIT_S3_BUCKET
10+
# CERTKIT_CERTIFICATE_ID
1111
# DESTINATION_PEM_FILE
1212
# DESTINATION_KEY_FILE
13-
# UPDATE_CERTIFICATE_CMD
13+
# UPDATE_CERTIFICATE_CMD (Optional)
1414

1515
# To add to cron, and have this script sync daily at 2am:
1616
# (crontab -l 2>/dev/null; echo "0 2 * * * /path/to/certkit-sync.sh") | crontab -
@@ -64,20 +64,18 @@ echo "Config: $CONFIG_FILE"
6464
# shellcheck source=/dev/null
6565
source "$CONFIG_FILE"
6666

67-
# Normalize directory and file names based on wildcard or not
68-
if [[ "$CERTKIT_CERTIFICATE_DOMAIN" == \*.* ]]; then
69-
# Strip leading "*." for folder name
70-
S3_FOLDER_NAME="${CERTKIT_CERTIFICATE_DOMAIN#*.}"
71-
CERT_BASENAME="wildcard.${CERTKIT_CERTIFICATE_DOMAIN#*.}"
72-
else
73-
S3_FOLDER_NAME="${CERTKIT_CERTIFICATE_DOMAIN}"
74-
CERT_BASENAME="${CERTKIT_CERTIFICATE_DOMAIN}"
75-
fi
67+
: "${CERTKIT_CERTIFICATE_ID:?CERTKIT_CERTIFICATE_ID must be set in certkit.conf}"
68+
: "${CERTKIT_S3_ACCESS_KEY:?CERTKIT_S3_ACCESS_KEY must be set in certkit.conf}"
69+
: "${CERTKIT_S3_SECRET_KEY:?CERTKIT_S3_SECRET_KEY must be set in certkit.conf}"
70+
: "${CERTKIT_S3_BUCKET:?CERTKIT_S3_BUCKET must be set in certkit.conf}"
71+
: "${DESTINATION_PEM_FILE:?DESTINATION_PEM_FILE must be set in certkit.conf}"
72+
: "${DESTINATION_KEY_FILE:?DESTINATION_KEY_FILE must be set in certkit.conf}"
7673

77-
CERT_DIR="${SCRIPT_DIR}/certs/${CERT_BASENAME}"
78-
CERTKIT_PEM_FILE="${CERT_BASENAME}.pem"
79-
CERTKIT_KEY_FILE="${CERT_BASENAME}.key"
74+
# Base S3 folder is /certificate-{id}/
75+
S3_FOLDER_NAME="certificate-${CERTKIT_CERTIFICATE_ID}"
8076

77+
# Local directory for this certificate
78+
CERT_DIR="${SCRIPT_DIR}/certs/${S3_FOLDER_NAME}"
8179
mkdir -p "$CERT_DIR"
8280

8381
# Ensure MinIO client (mc) is present locally next to the script
@@ -122,8 +120,8 @@ files_differ() {
122120
return 0 # differ
123121
}
124122

125-
SRC_PEM="${CERT_DIR}/${CERTKIT_PEM_FILE}"
126-
SRC_KEY="${CERT_DIR}/${CERTKIT_KEY_FILE}"
123+
SRC_PEM=$(echo "$CERT_DIR"/*.pem)
124+
SRC_KEY=$(echo "$CERT_DIR"/*.key)
127125

128126
NEED_UPDATE=false
129127
REASONS=()
@@ -172,4 +170,4 @@ else
172170
fi
173171

174172
sleep 0.1
175-
echo "== $(date -Is) | Done"
173+
echo "== $(date -Is) | Done"

tasks/main.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
# certkit_bucket - The name of your certkit storage bucket. Get this from the Certkit UI.
1111
# certkit_access_key - The access key for your certkit storage bucket. Get this from the Certkit UI.
1212
# certkit_secret_key - The secret key for your certkit storage bucket. Get this from the Certkit UI.
13-
# certkit_common_name - The domain name of the certificate. Prefix with * if it's a wildcard.
13+
# certkit_certificate_id - The ID of the certificate to sync. Get this from the Certkit UI.
1414
# certkit_dir - The directory where the certkit sync script and config file will be placed. Arbitrary, pick what you'd like. Should be unique if multiple certkit scripts are installed on the same box!
1515
# certkit_update_cmd - Certkit sync runs this command whenever the certificates are updated. Use to inform the server of a new certificate.
1616
# certkit_pem_destination - File path where Certkit sync will write the certificate PEM file. This is wherever your server software expects the certificate to live.
@@ -22,7 +22,7 @@
2222
# certkit_bucket: certkit-1234
2323
# certkit_access_key: YOUR_ACCESS_KEY
2424
# certkit_secret_key: YOUR_SECRET_KEY
25-
# certkit_common_name: "*.yourdomain.com"
25+
# certkit_certificate_id: ab12
2626
# certkit_dir: /opt/certkit-nginx
2727
# certkit_update_cmd: "/usr/sbin/nginx -s reload"
2828
# certkit_pem_destination: "/etc/nginx/yourdomain.pem"

templates/certkit.conf.j2

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ CERTKIT_S3_ACCESS_KEY="{{certkit_access_key}}"
33
CERTKIT_S3_SECRET_KEY="{{certkit_secret_key}}"
44
CERTKIT_S3_BUCKET="{{certkit_bucket}}"
55

6-
# This is the common name/domain of the certificate. If it's wildcard, prefix with *.
7-
CERTKIT_CERTIFICATE_DOMAIN="{{certkit_common_name}}"
6+
# The ID of the certificate to sync. Get this from the Certkit UI.
7+
CERTKIT_CERTIFICATE_ID="{{certkit_certificate_id}}"
88

99
# When certificates are updated, this command will be run.
1010
UPDATE_CERTIFICATE_CMD="{{certkit_update_cmd}}"

0 commit comments

Comments
 (0)