A simple Terraform module to generate self-signed TLS certificates for private use
This repository contains a Terraform module that can be used to generate self-signed TLS certificate. To be more accurate, the module generates the following:
This TLS certificate is meant to be used with private services, such as a web service used only within your company. For publicly-accessible services, especially services you access through a web browser, you should NOT use this module, and instead get certificates from a commercial Certificate Authority, such as Let's Encrypt.
If you're unfamiliar with how TLS certificates work, check out the Background section.
Note: This Terraform module uses bash commands in a local-exec
provisioner to copy the generate certificates to
files, so currently, it will only work on Linux, Unix, and OS X.
git clone
this repo to your computer and go into the modules/generate-cert
folder.
Open vars.tf
and fill in the variables that do not have a default.
DO NOT configure Terraform remote state storage for this code. You do NOT want to store the state files as they will contain the private keys for the certificates.
Run terraform init
.
Run terraform apply
. The output will show you the paths to the generated files:
Outputs:
ca_public_key_file_path = ca.key.pem
private_key_file_path = my-app.key.pem
public_key_file_path = my-app.crt.pem
Delete your local Terraform state:
rm -rf terraform.tfstate*
The Terraform state will contain the private keys for the certificates, so it's important to clean it up!
You can now use the TLS certs with your applications! To inspect a certificate, you can use OpenSSL:
openssl x509 -inform pem -noout -text -in my-app.crt.pem
The industry-standard way to add encryption for data in motion is to use TLS (the successor to SSL). There are many examples online explaining how TLS works, but here are the basics:
Some entity decides to be a "Certificate Authority" ("CA") meaning it will issue TLS certificates to websites or other services
An entity becomes a Certificate Authority by creating a public/private key pair and publishing the public portion (typically known as the "CA Cert"). The private key is kept under the tightest possible security since anyone who possesses it could issue TLS certificates as if they were this Certificate Authority!
In fact, the consequences of a CA's private key being compromised are so disastrous that CA's typically create an "intermediate" CA keypair with their "root" CA key, and only issue TLS certificates with the intermediate key.
Your client (e.g. a web browser) can decide to trust this newly created Certificate Authority by including its CA Cert (the CA's public key) when making an outbound request to a service that uses the TLS certificate.
When CAs issue a TLS certificate ("TLS cert") to a service, they again create a public/private keypair, but this time the public key is "signed" by the CA. That public key is what you view when you click on the lock icon in a web browser and what a service "advertises" to any clients such as web browsers to declare who it is. When we say that the CA signed a public key, we mean that, cryptographically, any possessor of the CA Cert can validate that this same CA issued this particular public key.
The public key is more generally known as the TLS cert.
The private key created by the CA must be kept secret by the service since the possessor of the private key can "prove" they are whoever the TLS cert (public key) claims to be as part of the TLS protocol.
How does that "proof" work? Well, your web browser will attempt to validate the TLS cert in two ways:
Now your client/browser has:
For public services like banks, healthcare, and the like, it makes sense to use a "Commercial CA" like Verisign, Thawte, or Digicert, or better yet a widely trusted but free service like Let's Encrypt. That's because every web browser comes pre-configured with a set of CA's that it trusts. This means the client connecting to the bank doesn't have to know anything about CA's at all. Instead, their web browser is configured to trust the CA that happened to issue the bank's certificate.
Connecting securely to private services is similar to connecting to your bank's website over TLS, with one primary difference: We want total control over the CA.
Imagine if we used a commercial CA to issue our private TLS certificate and that commercial or public CA--which we don't control--were compromised. Now the attackers of that commercial or public CA could impersonate our private server. And indeed, it has happened multiple times.
One option is to be very selective about choosing a commercial CA, but to what benefit? What we want instead is assurance that our private service really was launched by people we trust. Those same people--let's call them our "operators"--can become their own CA and generate their own TLS certificate for the private service.
Sure, no one else in the world will trust this CA, but we don't care because we only need our organization to trust this CA.
So here's our strategy for issuing a TLS Cert for a private service:
Create our own CA.
Using our CA, issue a TLS Certificate for our private service.
Freely advertise our CA's public key to all internal services.
Throw away the CA private key.
This code is released under the MIT License. See LICENSE.txt.