Skip to content

Commit 3c9f0d3

Browse files
Fix: DTOSS-8363 App Service certificate bindings (#153)
* automate App Service certificate bindings * fix up hub certificate pfx outputs * fix resource ref * fix resource ref * conditional fix * linting
1 parent b513f69 commit 3c9f0d3

File tree

6 files changed

+47
-13
lines changed

6 files changed

+47
-13
lines changed
Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
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_key_vault_secret_name != 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 = data.azurerm_key_vault_secret.pfx_blob[0].value
10+
}
11+
12+
data "azurerm_key_vault_secret" "pfx_blob" {
13+
count = var.wildcard_ssl_cert_pfx_blob_key_vault_secret_name != null ? 1 : 0
14+
15+
name = var.wildcard_ssl_cert_pfx_blob_key_vault_secret_name
16+
key_vault_id = var.wildcard_ssl_cert_key_vault_id
1117
}

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_key_vault_secret_name != null ? azurerm_app_service_certificate.wildcard[0].id : null
1111
}

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

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,25 +53,24 @@ variable "vnet_integration_subnet_id" {
5353
default = ""
5454
}
5555

56-
variable "wildcard_ssl_cert_key_vault_secret_id" {
56+
variable "wildcard_ssl_cert_name" {
5757
type = string
58-
description = "Wildcard SSL certificate Key Vault secret id, for App Service Custom Domain binding."
58+
description = "Wildcard SSL certificate name as it will appear in the App Service binding, for Custom Domain binding."
5959
default = null
6060
}
6161

62-
variable "wildcard_ssl_cert_key_vault_id" {
62+
variable "wildcard_ssl_cert_pfx_blob_key_vault_secret_name" {
6363
type = string
64-
description = "Wildcard SSL certificate Key Vault id, needed if the Key Vault is in a different subscription."
64+
description = "Wildcard SSL certificate pfx blob Key Vault secret name, for Custom Domain binding."
6565
default = null
6666
}
6767

68-
variable "wildcard_ssl_cert_name" {
68+
variable "wildcard_ssl_cert_key_vault_id" {
6969
type = string
70-
description = "Wildcard SSL certificate name, for Custom Domain binding."
70+
description = "Wildcard SSL certificate Key Vault id, needed if the Key Vault is in a different subscription."
7171
default = null
7272
}
7373

74-
7574
## autoscale rule ##
7675

7776
variable "metric" {

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: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
output "key_vault_certificates" {
33
value = {
44
for k, v in local.letsencrypt_certs_map : k => {
5-
name = v.cert_key
5+
name = data.azurerm_key_vault_certificate.letsencrypt[k].name
6+
naming_key = v.cert_key
67
subject = v.cert_subject
78
location = v.region
89
id = data.azurerm_key_vault_certificate.letsencrypt[k].id
@@ -11,3 +12,16 @@ output "key_vault_certificates" {
1112
}
1213
}
1314
}
15+
16+
output "key_vault_certificate_pfx_blobs" {
17+
value = {
18+
for k, v in local.letsencrypt_certs_map : k => {
19+
name = data.azurerm_key_vault_secret.pfx_blob[k].name
20+
naming_key = v.cert_key
21+
subject = v.cert_subject
22+
location = v.region
23+
id = data.azurerm_key_vault_secret.pfx_blob[k].id
24+
versionless_id = data.azurerm_key_vault_secret.pfx_blob[k].versionless_id
25+
}
26+
}
27+
}

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)