avatartarun bhatt

Summary

This web page provides instructions on how to set an expiration date for Azure Key Vault secrets using Terraform, with the expiration date being calculated dynamically as six months from the current date and time.

Abstract

The Azure Key Vault is a cloud solution by Microsoft to store secrets and certificates for enterprise applications. This article discusses the author's experience with implementing an expiration date for secrets in Azure Key Vault using Terraform, an infrastructure as code (IaC) tool. The author faced the challenge of setting a dynamic expiration date, as the same expiration date would be set each time the pipeline ran. To overcome this, the author used three built-in methods in Terraform: timestamp(), formatdate(), and timeadd() to calculate the expiration date as six months from the current date and time. This approach ensures that the expiration date is updated at each run, and the article includes code snippets for reference.

Opinions

  • The author emphasizes the importance of setting expiration dates for secrets to maintain secure systems and as a reminder for mandatory updates.
  • The author's company has a policy of setting a maximum expiration of six months for secrets.
  • The author prefers to use Terraform IaC pipelines to provision Azure resources.
  • The author finds value in using dynamic expressions to compute the expiration date using the execution date.
  • The author has previously written about IaC and best practices for Azure Key Vault management using Terraform and Azure DevOps.
  • The author highlights that Terraform uses state files to deploy changes, which allows for differential deployments.
  • The author recommends trying out a cost-effective AI service that provides the same performance and functions as ChatGPT Plus (GPT-4) at a lower cost.

Azure Key Vault — Update Expiry using Terraform

timeadd(formatdate(“YYYY-MM-DD’T’HH:mm:ssZ”, timestamp()), “4320h”)

Photo by Folco Masi on Unsplash

Azure Key Vault is Microsoft’s cloud solution to store secrets and certificates. Enterprise applications can fetch them using Key Vault URLs. It enables easy and secure management of passwords and connection strings. Every secret has an expiry attached to it.

In my current company, it’s mandatory to keep the expiry for secrets. It serves as a mandatory reminder and helps in maintaining secure systems. The cyber policy mandates a most expiry of six months. We provision Azure resources via terraform IaC pipelines. Hence, I needed a way to keep an expiry of six months from the creation of secrets in my IaC pipeline.

To understand more about IaC (Infrastructure as Code), read my article below:

To follow through, please read my older stories on Azure Key Vault & Terraform. Below are the links:

azurerm_key_vault_secret

Terraform assists in the management of key vault secrets using the resource — azurerm_key_vault_secret

Code Snippet using Snappify

We are keeping the expiry of the secret to be 1st April 2024. The expiration date is in UTC

What’s the problem?

There is a problem with this approach. The same expiration date will be set every time the pipeline runs. I wanted something dynamic to compute the expiry date using the execution date.

A dynamic expression for the expiration_date eliminates the need for frequent code changes. Every time the pipeline runs, it will change the expiration_date.

I chose to use three in-built methods to form a date six months from the time of execution. Let’s look at these three methods and how can they help us:

Code Snippet using Snappify

1. timestamp()

In the Terraform, RFC 3339 “Date and Time format” syntax represents timestamps. The method returns the current timestamp as a string. To learn more about this in-built method, read the online documentation here.

2. formatdate(“YYYY-MM-DD’T’HH:mm:ssZ”))

Key vault expects an expiration date in a particular format. The formatdate() method helps in sticking to this format.

  • YYYY — Defines year
  • MM — Month
  • DD — Date
  • HH — Hour
  • mm — Minutes
  • ss — Seconds
  • Z — UTC format

There are many variations of the formatdate() method. Read about all these variations on the official documentation site below:

3. Timeadd

We need an expiry of six months from today onwards. 4320 hours is 180 days which is approximately six months. According to the Terraform documentation, they don’t have a denomination for months. Read more about the timeadd method below:

Final code snippet

The final code snippet looks like below:

Code Snippet using Snappify

Terraform uses the concept of state files to deploy changes. The biggest advantage of terraform is that it always deploys the differential. Timestamp() will create a new string for the expiration_date every time. It means Terraform will detect a change from the existing state file. Hence, the code snippet will update the secret at each run. I have written about this concept in my older articles. See links below:

Let me know if you have any questions about this approach.

Regards

Tarun

Programming
Terraform
Terraform Modules
Keyvault
Cybersecurity
Recommended from ReadMedium
avatarBrian Veldman
Pester Framework & Azure Bicep!

3 min read