Skip to content

Commit

Permalink
Update default CAA records to globalsign and digicert
Browse files Browse the repository at this point in the history
Refactor the dns module to handle multiple CAA records
  • Loading branch information
johnake authored and saliceti committed Dec 19, 2023
1 parent f51f10a commit 6a27418
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 10 deletions.
4 changes: 2 additions & 2 deletions dns/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ So we add the following 3 records to indicate we don't send mail from these doma

- CAA record
```
0 issue "amazon.com"
0 issue "globalsign.com"
```

# Records
Expand All @@ -82,7 +82,7 @@ To create (or update) records to an existing zone
- Run the make command with DNS_ENV set to the environment you are adding records too

- make ${zone} dnsrecord-plan DNS_ENV=${env}
- make ${zone} dnsrecord-apply DNS_ENV=$env}
- make ${zone} dnsrecord-apply DNS_ENV=${env}
- e.g. make register dnsrecord-plan DNS_ENV=qa

Note;
Expand Down
33 changes: 32 additions & 1 deletion dns/zones/resources.tf
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ resource "azurerm_dns_zone" "dns_zone" {
# CAA record

locals {
# caa_records is deprecated. Use caa_record_list instead
caa_records = flatten([
for zone_name, zone_cfg in var.hosted_zone : [
for record_name, record_cfg in zone_cfg["caa_records"] : {
for record_name, record_cfg in try(zone_cfg["caa_records"], {}) : {
record_name = record_name
zone_name = zone_name
resource_group_name = zone_cfg["resource_group_name"]
Expand All @@ -26,8 +27,15 @@ locals {
}
]
])

hosted_zone_with_caa_record_list = {
for zone_name, zone_cfg in var.hosted_zone :
zone_name => zone_cfg
if length(try(zone_cfg.caa_record_list, [])) > 0
}
}

# caa_records is deprecated. Use caa_record_list instead
resource "azurerm_dns_caa_record" "caa_records" {
for_each = {
for zone in local.caa_records : "${zone.zone_name}.${zone.record_name}" => zone
Expand All @@ -50,6 +58,29 @@ resource "azurerm_dns_caa_record" "caa_records" {

}

resource "azurerm_dns_caa_record" "caa_record_list" {
for_each = local.hosted_zone_with_caa_record_list

name = "@"
zone_name = each.key
resource_group_name = each.value.resource_group_name
ttl = 300

dynamic "record" {
for_each = toset(each.value.caa_record_list)
content {
flags = 0
tag = "issue"
value = record.value
}
}

depends_on = [
azurerm_dns_zone.dns_zone
]

}

# TXT record

locals {
Expand Down
1 change: 1 addition & 0 deletions dns/zones/tfdocs.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ No modules.

| Name | Type |
|------|------|
| [azurerm_dns_caa_record.caa_record_list](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/dns_caa_record) | resource |
| [azurerm_dns_caa_record.caa_records](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/dns_caa_record) | resource |
| [azurerm_dns_txt_record.txt_records](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/dns_txt_record) | resource |
| [azurerm_dns_zone.dns_zone](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/dns_zone) | resource |
Expand Down
8 changes: 1 addition & 7 deletions domains/infrastructure/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,7 @@ variable "azure_enable_monitoring" {

locals {
default_records = {
"caa_records" = {
"@" = {
"flags" = 0,
"tag" = "issue",
"value" = "digicert.com"
}
}
"caa_record_list" = ["globalsign.com", "digicert.com"],
"txt_records" = {
"@" = {
"value" = "v=spf1 -all"
Expand Down

0 comments on commit 6a27418

Please sign in to comment.