Skip to main content

Google Cloud Function SSL Cert Rotator tool

Project description

Google Cloud Function SSL Rotator Tool

This tool allows you to use Google cloud function to rotate a regional SSL cert for a regional Google target https proxy.

Note: This tool only supports regional google ssl certs and regional google target https proxies.

Overview

You'll upload cert files to the configured GCS bucket, and it'll use the cert files to create a google ssl create and update the target https proxy. For clarity, here are the steps:

  1. Upload your SSL cert files to your GCS Bucket, IE: cert1.key and cert1.crt
  2. The Cloud function listens to the upload event.
  3. Function creates a self-managed Google SSL Certificate with the same name as the uploaded file. IE: cert1
  4. Function updates the target https proxy with the new cert.

Notes

  • The GOOGLE_PROJECT env variable must be set for this script to work.
  • The Google Cloud Function is written in Python and uses purely the Google Cloud SDK. It does not use gcloud. This allows it to run on a Google Cloud Function.
  • The tool comes with the google-ssl deploy command to deploy the function to Google Cloud functions. It creates the necessary resources, like an IAM service account with required permissions. While the Google Cloud Function itself does not require gcloud, some parts of the deploy command do rely on the gcloud cli. Note: There was an attempt to use the pure Google Cloud SDK, but it proved unsatisfactory. The SDK does not document deployment well, and the interface was too complex at the time.
  • The tool also provides the ability to test locally. This helps speed up debugging, development, and testing.
  • The tool provides a message explaining what it will do with a "Are you sure?" prompt. To bypass the message and prompt, use the `-y' option.

Structure

Here's a suggested GCS bucket structure.

gcs://$BUCKET/certs/$DOMAIN/

Here's an example with files uploaded.

gcs://my-bucket/certs/example.com/proxies.txt
gcs://my-bucket/certs/example.com/cert-name-1.key
gcs://my-bucket/certs/example.com/cert-name-1.crt

Considerations:

  • The cert name will be the name of the google ssl certification record that shows up with gcloud compute ssl-certificates list. Google ssl certificates need to be unique per Google project.
  • The .key and .crt files and proxies.txt must exist before the script will create the google ssl certificate and continue on. Otherwise the script exits early with a message in the logs.
  • Only a .key and .crt files will trigger a target https proxy update.

proxies.txt

There needs to be a proxies.txt file in the same GCS folder. The proxies.txt lists target https proxies to be updated. This is because the only useful information passed to the cloud function in the received cloud_event object is the bucket name and filename path. So a proxies.txt contains a list of target proxie. If the proxies.txt does not exist, the script logs a message.

This file should be a list of target https proxies separated by newlines. Here's a gcloud command to help you grab a list to work with. You should remove most entries and only keep the proxies you want to update.

gcloud compute target-https-proxies list --format json | jq -r '.[].name'

Development Setup

For development, it is recommended to use Python virtualenv to set up the requirements. Here's a cheatsheet.

virtualenv -p python3 .venv
source .venv/bin/activate

Install

Install the google-ssl tool.

pip install --editable .

This installs the google-ssl command.

Note: The pip install --editable . creates a shim that points to your local folder of the tool. This means any code edits you make are reflected without having to reinstall unless you move the folder. TLDR: You only have to install once.

Alternatively, if you have poetry installed. You can run

poetry install

See poetry site for detailed install docs: https://python-poetry.org/docs/#installation

CLI Help Intro

CLI help:

google-ssl
google-ssl --help
google-ssl deploy --help
google-ssl rotate --help

You can also call the tool directly with python without installing the shim.

python google_ssl/cli.py
python google_ssl/cli.py --help
python google_ssl/cli.py deploy --help
python google_ssl/cli.py rotate --help

The shim makes the interface more user friendly, though and can be run from any location, not just the google-ssl project folder.

Deploy

Deploy the code to google cloud functions.

google-ssl deploy --bucket my-bucket

This simply uses gcloud functions deploy to package up the code and deploy it to Google Cloud functions. The google function name is called google-ssl-rotator by default. It can be set with the GS_FUNCTION_NAME env var.

Local Testing

This can be useful before deploying code to Google Cloud Functions.

Copy a proxies.txt file with a list of target https proxies you want to be updated and the SSL cert files you want to be used to create the Self-managed Google SSL Cert. Replace CERT_NAME with your own value. The cert name needs to be unique across the entire Google project.

CERT_NAME=cert-name-1
gsutil cp proxies.txt gs://my-bucket/certs/example.com/proxies.txt
gsutil cp $CERT_NAME.key gs://my-bucket/certs/example.com/$CERT_NAME.key
gsutil cp $CERT_NAME.crt gs://my-bucket/certs/example.com/$CERT_NAME.crt

The files must exist on the GCS bucket before running the next command: rotate.

The rotate command "triggers" the ssl cert update logic and downloads the cert files from the GCS bucket. It performs the same logic that the google cloud function performs. You're just manually triggering it for rapid testing and development.

google-ssl rotate --bucket my-bucket --name certs/example.com/name.key

The nice thing about local testing is that you see the logs immediately in the same terminal.

You can also provide a --proxies option to specify which target https proxies to update. In this case, the proxies.txt is not downloaded and used. Example:

google-ssl rotate --bucket my-bucket --name certs/example.com/name.key --proxies demo-target-https-proxy-dev

Remote Testing: Cloud Function

You can build a test payload using the Google Cloud Function console Testing tab. Replace the name and bucket with some test values that exist in the GCS bucket.

{
  "name": "certs/dev.example.com/test1.key",
  "bucket": "certs-bucket-dev",
  "contentType": "application/json",
  "metageneration": "1",
  "timeCreated": "2020-04-23T07:38:57.230Z",
  "updated": "2020-04-23T07:38:57.230Z"
}

It will display a curl command you can use in the Cloud Shell to test. It will return an "OK" http body response. Check the Logs tab to verify that it worked.

Last but not least, use gcloud to check that the google ssl cert was created and target https proxy was updated. Here's a cheatsheet with useful example commands:

gcloud compute ssl-certificates list
# useful to confirm certs are regional
gcloud compute ssl-certificates list --format json | jq '.[].selfLink'
gcloud compute ssl-certificates describe test1 --region us-central1
gcloud compute target-https-proxies describe demo-target-https-proxy-dev --region us-central1 | yq '.sslCertificates'

The tool also shows a hint/tip with similar check commands upon completion.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

google_ssl-0.1.0.tar.gz (11.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

google_ssl-0.1.0-py3-none-any.whl (10.8 kB view details)

Uploaded Python 3

File details

Details for the file google_ssl-0.1.0.tar.gz.

File metadata

  • Download URL: google_ssl-0.1.0.tar.gz
  • Upload date:
  • Size: 11.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.4.0 CPython/3.11.1 Linux/5.19.0-1024-aws

File hashes

Hashes for google_ssl-0.1.0.tar.gz
Algorithm Hash digest
SHA256 6b9886873a65479889ede054b4927c0d4e1aaaacd2d0385c02add94b52a9e7aa
MD5 2962edc5454c3ce760fc8d179d2458d2
BLAKE2b-256 59b68b3f6e5f0426a12f0d0bbd33fd19681ca521746165c071ddbf0748dffb9a

See more details on using hashes here.

File details

Details for the file google_ssl-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: google_ssl-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 10.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.4.0 CPython/3.11.1 Linux/5.19.0-1024-aws

File hashes

Hashes for google_ssl-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 327208c4e790d6ab534e0a41f21b4ed5bf2a86fe4f3b275047db3c6fc64da99f
MD5 475316ac69f2f37f7923a6117caf05ac
BLAKE2b-256 75784772c48b5cd8cae5d4956d5aa8c858dab1565765e1fbe18bb537098e4be9

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page