Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/mattlqx/cookbook-letsencryptaws

Chef cookbook for Let's Encrypt certificate request/retrieval with Route 53-hosted domains using S3
https://github.com/mattlqx/cookbook-letsencryptaws

chef cookbook letsencrypt route53

Last synced: about 1 month ago
JSON representation

Chef cookbook for Let's Encrypt certificate request/retrieval with Route 53-hosted domains using S3

Awesome Lists containing this project

README

        

# letsencryptaws Cookbook

This cookbook is for an implementation of SSL certificate generation and fetching via the Let's Encrypt certificate authority. Certificates are synced from local storage to S3, which is then used by nodes to retrieve the generated certificate. Authentication is done via DNS challenges and the offical Certbot plugin for Route 53.

Nodes do not need to be EC2 instances to retrieve or request certificates. All that is required is AWS credentials or profile to perform Route 53 and S3 operations.

## Requirements

- Python 3
- certbot ACME client
- Domain(s) hosted by AWS Route 53
- S3 bucket for storing/retrieving certificate files
- Properly stored AWS credentials (see https://github.com/aws/aws-sdk-ruby#configuration)

### Platforms

Certificate generation:
- Ubuntu

The goal for certificate retrieval is to support Windows but for now, Ubuntu only.

### Cookbooks

- `remote_file_s3` - To grab certificates from S3
- `pyenv` - For grabbing Python environment that certbot recipe uses to install `awscli` and `certbot`.

## Usage

### letsencryptaws::default

Set the `certs` attribute as described below and then include this recipe in your cookbook or run_list.

**NOTE** You should manually generate a default certificate (self-signed/fake CA) and place the key, certificate and CA certificate at the `"/#{node['letsencryptaws']['sync_path']}/default-ssl"` path in your S3 sync bucket. These will act as stand-ins for the real certificates/key until they are generated by `letsencryptaws::certbot` after they are first "requested" by a node. So in theory, your real certificates will take up to (chef interval + splay * 3) until they land on the requesting node.

The flow looks like this:
- First Chef run on requesting node. Attribute (`['letsencryptaws']['certs']['example.com'] = []`) gets saved to server (probably created dynamically by a cookbook). Default cert/key gets saved to node from S3.
- Chef runs on host with 'letsencryptaws::certbot' in its run_list. Requests certificate and uploads to S3.
- Second Chef run on requesting node overwrites the previously saved default cert/key with real cert/key from S3.

Any service that uses a certificate provided by this recipe should subscribe to one of the certificate file resources so that it can be reloaded when the certificate is renewed. For example:

```
service 'nginx' do
action %i[start enable]
subscribes :restart, "file[#{::File.join(node['letsencryptaws']['ssl_cert_dir'], 'example.com.crt')}]", :delayed
end
```

**FOR WILDCARD CERTIFICATES**, you should specify the CN as you'd like as `certs` key (e.g. `*.example.com`) however any `*` will be substituted with `star` in the filenames to prevent the need for escaping. So the filename you'd reference for the certificate would be `star.example.com.crt`.

### letsencryptaws::certbot

This is meant to be run by a single host that manages fetching certificates based on a Chef server `search`. Make sure the instance profile or AWS access keys in the data bag is granted the following permissions on the domains in which you allow certificates to be requested by nodes:

- `route53:ChangeResourceRecordSets`
- `route53:ListHostedZonesByName`
- `route53:GetChange`

The credentials will also require write access to the S3 bucket and path that you choose to sync to.

If you desire persistent storage on an EBS volume, use the `['letsencryptaws']['ebs_device']` to specify the path to the device. This will device will have an ext4 filesystem created on it if one does not already exist and be mounted at `['letsencryptaws']['config_dir']`. This is where certbot will store its configs and certificates. All operations take place locally at this path and at the end of the recipe gets synced to S3.

Certbot operations use the `--expand` and `--cert-name` arguments to keep the certificates up-to-date with the requested names. This means the certificate will be renewed appropriately as nodes desire for the certificate name changes.

### letsencryptaws::import_keystore

This recipe takes certificates and imports them into a Java keystore.

## Attributes

### letsencryptaws::default

For certificate retrieval, just specify what certificates you would like by common name
of the certificate and an array of Subject Alternative Names for the cert. The certificate
may have additional SANs if other nodes request them for the same common name.


Key
Type
Description
Default


['letsencryptaws']['certs']
Hash
keys are the common name, values are an array of strings that are the SANs for the cert. These all get merged together in the final certificate.
{}


['letsencryptaws']['ssl_cert_dir']
string
path where ssl certs will be downloaded to
/etc/ssl/certs


['letsencryptaws']['ssl_key_dir']
string
path where ssl private keys will be downloaded to
/etc/ssl/private


['letsencryptaws']['ssl_ca_dir']
string
path where ssl CA certificates will be downloaded to
/etc/ssl/certs

### letsencryptaws::import_keystore

This recipe is automatically included if the `import_keystore` hash is not empty.


Key
Type
Description
Default


['letsencryptaws']['import_keystore']
Hash
keys are full paths to Java keystores, values are an array of primary names of certificates to add to the keystore
{}

### letsencryptaws::certbot


Key
Type
Description
Default


['letsencryptaws']['config_dir']
string
dir where all certbot configuration will be stored, including certs
/mnt/letsencrypt


['letsencryptaws']['certbot_version']
string
version to enforce for certbot
1.18.0


['letsencryptaws']['certbot_dns_version']
string
version to enforce for certbot-dns-route53
['letsencryptaws']['certbot_version']


['letsencryptaws']['data_bag']
string
Name of data bag used for credentials storage
nil


['letsencryptaws']['data_bag_item']
string
Name of item within data bag for credentials storage
nil


['letsencryptaws']['email']
string
Email addressed used for certbot during generation
[email protected]


['letsencryptaws']['ebs_device']
string
device of the ebs volume to mount on `config_dir` (only applies on ec2 instances)
/dev/xvdf


['letsencryptaws']['test_certs']
boolean
request certs from staging (signed by fake CA, subject to less rate limiting)
false


['letsencryptaws']['remove_unused_certs']
boolean
remove certificates that are no longer requested by any node
true


['letsencryptaws']['sync_bucket']
string
s3 bucket to sync local certificate directory to
nil


['letsencryptaws']['sync_path']
string
path on the `sync_bucket` to sync certificate directory to
letsencrypt


['letsencryptaws']['kms_key_id']
string
UUID of the kms key to use for server-side encryption (optional)
nil


['letsencryptaws']['blocklist']
array of strings
Exact matches of primary certificate name to prevent generation
[]


['letsencryptaws']['link_pybins']
boolean
Link aws and certbot in /usr/local/bin to pyenv paths if true
true

## Data Bags

A data bag is used to store sensitive credential information for AWS and Java keystores. You can arbitrarily specify the name and item name with `node['letsencryptaws']['data_bag']` and `node['letsencryptaws']['data_bag_item']` attributes. If you do not wish to use a data bag, you can place the credentials following the same hash structure at `node.run_state['letsencryptaws_creds']`.

The keys inside the data bag item can be:


Key
Type
Description


aws_access_key_id
string
AWS_ACCESS_KEY_ID for storing/fetching certificates from S3


aws_secret_access_key
string
AWS_SECRET_ACCESS_KEY for storing/fetching certificates from S3


keystore_passwords
hash
Keys are paths to Java keystore files, values are the passwords to them. One special key is `default` which will be used as a catch-all password if a keystore does not have a specific entry.


p12_password
string
Password to use when generating pkcs12 keyring files.

## License and Authors

Authors: Matt Kulka