Skip to content

Commit 02c111c

Browse files
automate App Service certificate bindings
1 parent 43a1c11 commit 02c111c

File tree

6 files changed

+35
-15
lines changed

6 files changed

+35
-15
lines changed
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
resource "azurerm_app_service_certificate" "wildcard" {
2-
count = var.wildcard_ssl_cert_key_vault_secret_id != null ? 1 : 0
2+
count = var.wildcard_ssl_cert_pfx_blob != null ? 1 : 0
33

44
name = var.wildcard_ssl_cert_name
55
resource_group_name = var.resource_group_name
66
location = var.location
77

88
app_service_plan_id = azurerm_service_plan.appserviceplan.id
9-
key_vault_secret_id = var.wildcard_ssl_cert_key_vault_secret_id
10-
key_vault_id = var.wildcard_ssl_cert_key_vault_id
9+
pfx_blob = var.wildcard_ssl_cert_pfx_blob
1110
}

infrastructure/modules/app-service-plan/output.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ output "app_service_plan_id" {
77
}
88

99
output "wildcard_ssl_cert_id" {
10-
value = var.wildcard_ssl_cert_key_vault_secret_id != null ? azurerm_app_service_certificate.wildcard[0].id : null
10+
value = var.wildcard_ssl_cert_pfx_blob != null ? azurerm_app_service_certificate.wildcard[0].id : null
1111
}

infrastructure/modules/app-service-plan/variables.tf

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -53,21 +53,15 @@ variable "vnet_integration_subnet_id" {
5353
default = ""
5454
}
5555

56-
variable "wildcard_ssl_cert_key_vault_secret_id" {
57-
type = string
58-
description = "Wildcard SSL certificate Key Vault secret id, for App Service Custom Domain binding."
59-
default = null
60-
}
61-
62-
variable "wildcard_ssl_cert_key_vault_id" {
56+
variable "wildcard_ssl_cert_name" {
6357
type = string
64-
description = "Wildcard SSL certificate Key Vault id, needed if the Key Vault is in a different subscription."
58+
description = "Wildcard SSL certificate name, for Custom Domain binding."
6559
default = null
6660
}
6761

68-
variable "wildcard_ssl_cert_name" {
62+
variable "wildcard_ssl_cert_pfx_blob" {
6963
type = string
70-
description = "Wildcard SSL certificate name, for Custom Domain binding."
64+
description = "Wildcard SSL certificate pfx blob, for Custom Domain binding. Referencing an elliptic curve certificate from Key Vault is not working currently."
7165
default = null
7266
}
7367

infrastructure/modules/lets-encrypt-certificates/data.tf

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,16 @@ data "azurerm_key_vault_certificate" "letsencrypt" {
3434
null_resource.letsencrypt_cert
3535
]
3636
}
37+
38+
# references to the created certificate pfx blobs, for outputs
39+
data "azurerm_key_vault_secret" "pfx_blob" {
40+
for_each = local.letsencrypt_certs_map
41+
42+
name = "pfx-${replace(replace(each.value.cert_subject, "*.", "wildcard-"), ".", "-")}"
43+
44+
key_vault_id = var.key_vaults[each.value.region].key_vault_id
45+
46+
depends_on = [
47+
null_resource.letsencrypt_cert
48+
]
49+
}

infrastructure/modules/lets-encrypt-certificates/output.tf

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,15 @@ output "key_vault_certificates" {
1111
}
1212
}
1313
}
14+
15+
output "key_vault_certificate_pfx_blobs" {
16+
value = {
17+
for k, v in local.letsencrypt_certs_map : k => {
18+
name = v.cert_key
19+
subject = v.cert_subject
20+
location = v.region
21+
id = data.azurerm_key_vault_secret.pfx_blob[k].id
22+
versionless_id = data.azurerm_key_vault_secret.pfx_blob[k].versionless_id
23+
}
24+
}
25+
}

infrastructure/modules/lets-encrypt-certificates/scripts/certbot.sh

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,10 @@ while [[ $# -gt 0 ]]; do
126126
echo "Certificate ${cert_name} with thumbprint ${thumbprint_local} already exists in Key Vault ${kv_name}, skipping import..."
127127
else
128128
echo "Importing certificate ${cert_name} into Key Vault ${kv_name}..."
129-
openssl pkcs12 -export -inkey certbot/config/live/${trimmed_domain}/privkey.pem -in certbot/config/live/${trimmed_domain}/fullchain.pem -out ${trimmed_domain}.pfx -password pass:
129+
openssl pkcs12 -export -inkey certbot/config/live/${trimmed_domain}/privkey.pem -in certbot/config/live/${trimmed_domain}/cert.pem -certfile certbot/config/live/${trimmed_domain}/chain.pem -out ${trimmed_domain}.pfx -password pass:
130130
az keyvault certificate import --vault-name "${kv_name}" --name "${cert_name}" --file "${trimmed_domain}.pfx" --password ""
131+
# Also upload the certificate pfx blob since App Services cannot currently reference elliptic curve certificates as Key Vault Certificate objects
132+
az keyvault secret set --vault-name "${kv_name}" --name "pfx-${cert_name}" --file "${trimmed_domain}.pfx" --encoding base64 --content-type "application/x-pkcs12"
131133
fi
132134
done
133135
[[ -e "${trimmed_domain}.pfx" ]] && rm "${trimmed_domain}.pfx"

0 commit comments

Comments
 (0)