Skip to content

Commit 1d28cee

Browse files
committed
add automation to rotate cert on terraform apply
reduce app certificate expiration significantly
1 parent f07f18e commit 1d28cee

File tree

5 files changed

+33
-7
lines changed

5 files changed

+33
-7
lines changed

.gitignore

+2-4
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,5 @@
66
*.secrets
77
*.pfx
88
*.pem
9-
*.tfvars
10-
!example.tfvars
11-
**/tenants/*
12-
!**/tenants/myorg.onmicrosoft.com.yaml
9+
**/env/*
10+
!**/env/example

m365/terraform/main.tf

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ module "app" {
3434
create_app = var.create_app
3535
contact_email = var.contact_email
3636
allowed_access_ips = var.vnet.allowed_access_ip_list
37+
certificate_rotation_period_days = var.certificate_rotation_period_days
3738
}
3839

3940
module "networking" {

m365/terraform/modules/app/main.tf

+10-3
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,15 @@ resource "azurerm_key_vault" "vault" {
5757
}
5858
}
5959

60+
# note this requires terraform to be run regularly
61+
resource "time_rotating" "cert_rotation" {
62+
rotation_days = var.certificate_rotation_period_days
63+
}
64+
6065
# Generate the app registration certificate
6166
resource "azurerm_key_vault_certificate" "cert" {
62-
name = "${var.app_name}-app-cert"
67+
# Name change forces recreating certificate
68+
name = "${var.resource_prefix}-app-cert-${formatdate("YYYY-MM-DD", time_rotating.cert_rotation.rfc3339)}"
6369
key_vault_id = azurerm_key_vault.vault.id
6470

6571
certificate_policy {
@@ -80,7 +86,7 @@ resource "azurerm_key_vault_certificate" "cert" {
8086
}
8187

8288
trigger {
83-
days_before_expiry = 30
89+
days_before_expiry = max(3, ceil(var.certificate_rotation_period_days / 6))
8490
}
8591
}
8692

@@ -98,7 +104,8 @@ resource "azurerm_key_vault_certificate" "cert" {
98104
]
99105

100106
subject = "CN=${var.app_name}"
101-
validity_in_months = 24
107+
# min 1 month; approx. twice length of rotation period
108+
validity_in_months = max(1, ceil(var.certificate_rotation_period_days * 2 / 30))
102109
}
103110
}
104111
}

m365/terraform/modules/app/variables.tf

+10
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,16 @@ variable "contact_email" {
2323
type = string
2424
}
2525

26+
variable "certificate_rotation_period_days" {
27+
type = number
28+
description = "How many days between when the certificate key should be rotated. Note: rotation requires running terraform"
29+
default = 30
30+
validation {
31+
condition = var.certificate_rotation_period_days <= 60 && var.certificate_rotation_period_days >= 3
32+
error_message = "Rotation period must be between 3 and 60 days"
33+
}
34+
}
35+
2636
variable "image_path" {
2737
type = string
2838
description = "Path to image used for app logo. Displayed in Azure console on installed tenants. Only needed when create_app=true"

m365/terraform/variables.tf

+10
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,16 @@ variable "contact_email" {
2727
type = string
2828
}
2929

30+
variable "certificate_rotation_period_days" {
31+
type = number
32+
description = "How many days between when the certificate key should be rotated. Note: rotation requires running terraform"
33+
default = 30
34+
validation {
35+
condition = var.certificate_rotation_period_days <= 60 && var.certificate_rotation_period_days >= 3
36+
error_message = "Rotation period must be between 3 and 60 days"
37+
}
38+
}
39+
3040
variable "resource_group_name" {
3141
type = string
3242
description = "Resource group to create and build resources in"

0 commit comments

Comments
 (0)