|
| 1 | +data "aws_caller_identity" "current" {} |
| 2 | + |
| 3 | +resource "aws_iam_user" "buildingblock_route53_alias_record_user" { |
| 4 | + count = var.workload_identity_federation == null ? 1 : 0 |
| 5 | + |
| 6 | + name = "buildingblock-route53-alias-record-user" |
| 7 | +} |
| 8 | + |
| 9 | +data "aws_iam_policy_document" "route53_alias_record_access" { |
| 10 | + # Global Route53 actions that don't support resource-level permissions |
| 11 | + statement { |
| 12 | + effect = "Allow" |
| 13 | + actions = [ |
| 14 | + "route53:GetChange", |
| 15 | + "route53:ListHostedZones" |
| 16 | + ] |
| 17 | + resources = ["*"] |
| 18 | + } |
| 19 | + |
| 20 | + # Zone-specific actions scoped to specific hosted zones |
| 21 | + statement { |
| 22 | + effect = "Allow" |
| 23 | + actions = [ |
| 24 | + "route53:ListTagsForResource", |
| 25 | + "route53:GetHostedZone", |
| 26 | + "route53:ChangeResourceRecordSets", |
| 27 | + "route53:ListResourceRecordSets" |
| 28 | + ] |
| 29 | + resources = [ |
| 30 | + for zone_id in var.hosted_zone_ids : "arn:aws:route53:::hostedzone/${zone_id}" |
| 31 | + ] |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +resource "aws_iam_policy" "buildingblock_route53_alias_record_policy" { |
| 36 | + name = var.workload_identity_federation == null ? "Route53AliasRecordBuildingBlockPolicy" : "Route53AliasRecordBuildingBlockFederatedPolicy" |
| 37 | + description = "Policy for the Route53 DNS Alias Record Building Block" |
| 38 | + policy = data.aws_iam_policy_document.route53_alias_record_access.json |
| 39 | +} |
| 40 | + |
| 41 | +resource "aws_iam_user_policy_attachment" "buildingblock_route53_alias_record_user_policy_attachment" { |
| 42 | + count = var.workload_identity_federation == null ? 1 : 0 |
| 43 | + |
| 44 | + user = aws_iam_user.buildingblock_route53_alias_record_user[0].name |
| 45 | + policy_arn = aws_iam_policy.buildingblock_route53_alias_record_policy.arn |
| 46 | +} |
| 47 | + |
| 48 | +resource "aws_iam_access_key" "buildingblock_route53_alias_record_access_key" { |
| 49 | + count = var.workload_identity_federation == null ? 1 : 0 |
| 50 | + |
| 51 | + user = aws_iam_user.buildingblock_route53_alias_record_user[0].name |
| 52 | +} |
| 53 | + |
| 54 | +# Workload Identity Federation |
| 55 | + |
| 56 | +resource "aws_iam_openid_connect_provider" "buildingblock_oidc_provider" { |
| 57 | + count = var.workload_identity_federation != null ? 1 : 0 |
| 58 | + |
| 59 | + url = var.workload_identity_federation.issuer |
| 60 | + client_id_list = [var.workload_identity_federation.audience] |
| 61 | +} |
| 62 | + |
| 63 | +data "aws_iam_policy_document" "workload_identity_federation" { |
| 64 | + count = var.workload_identity_federation != null ? 1 : 0 |
| 65 | + version = "2012-10-17" |
| 66 | + |
| 67 | + statement { |
| 68 | + effect = "Allow" |
| 69 | + principals { |
| 70 | + type = "Federated" |
| 71 | + identifiers = [aws_iam_openid_connect_provider.buildingblock_oidc_provider[0].arn] |
| 72 | + } |
| 73 | + actions = ["sts:AssumeRoleWithWebIdentity"] |
| 74 | + |
| 75 | + condition { |
| 76 | + test = "StringEquals" |
| 77 | + variable = "${trimprefix(var.workload_identity_federation.issuer, "https://")}:aud" |
| 78 | + |
| 79 | + values = [var.workload_identity_federation.audience] |
| 80 | + } |
| 81 | + |
| 82 | + condition { |
| 83 | + test = "StringLike" |
| 84 | + variable = "${trimprefix(var.workload_identity_federation.issuer, "https://")}:sub" |
| 85 | + |
| 86 | + values = var.workload_identity_federation.subjects |
| 87 | + } |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +resource "aws_iam_role" "assume_federated_role" { |
| 92 | + count = var.workload_identity_federation != null ? 1 : 0 |
| 93 | + |
| 94 | + name = "BuildingBlockRoute53AliasRecordIdentityFederation" |
| 95 | + assume_role_policy = data.aws_iam_policy_document.workload_identity_federation[0].json |
| 96 | +} |
| 97 | + |
| 98 | +resource "aws_iam_role_policy_attachment" "buildingblock_route53_alias_record" { |
| 99 | + count = var.workload_identity_federation != null ? 1 : 0 |
| 100 | + |
| 101 | + role = aws_iam_role.assume_federated_role[0].name |
| 102 | + policy_arn = aws_iam_policy.buildingblock_route53_alias_record_policy.arn |
| 103 | +} |
0 commit comments