|
| 1 | +// Copyright Red Hat |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +mock_provider "aws" { |
| 5 | + alias = "default" |
| 6 | + |
| 7 | + mock_data "aws_partition" { |
| 8 | + defaults = { |
| 9 | + partition = "aws" |
| 10 | + } |
| 11 | + } |
| 12 | + |
| 13 | + mock_data "aws_caller_identity" { |
| 14 | + defaults = { |
| 15 | + account_id = "123456789012" |
| 16 | + } |
| 17 | + } |
| 18 | + |
| 19 | + mock_resource "aws_iam_role" { |
| 20 | + defaults = { |
| 21 | + arn = "arn:aws:iam::123456789012:role/mock" |
| 22 | + id = "mock-role-id" |
| 23 | + name = "mock-role" |
| 24 | + } |
| 25 | + } |
| 26 | + |
| 27 | + mock_resource "aws_iam_role_policy_attachment" { |
| 28 | + defaults = { |
| 29 | + id = "mock-attachment-id" |
| 30 | + } |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +mock_provider "rhcs" { |
| 35 | + alias = "default" |
| 36 | + |
| 37 | + mock_data "rhcs_hcp_policies" { |
| 38 | + defaults = { |
| 39 | + account_role_policies = { |
| 40 | + sts_support_rh_sre_role = "arn:aws:iam::999999999999:role/RH-SRE-Support" |
| 41 | + } |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + mock_data "rhcs_info" { |
| 46 | + defaults = { |
| 47 | + ocm_aws_account_id = "999999999999" |
| 48 | + } |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +mock_provider "time" { |
| 53 | + alias = "default" |
| 54 | + |
| 55 | + mock_resource "time_sleep" { |
| 56 | + defaults = { |
| 57 | + id = "mock-sleep" |
| 58 | + } |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +mock_provider "random" { |
| 63 | + alias = "default" |
| 64 | +} |
| 65 | + |
| 66 | +variables { |
| 67 | + account_role_prefix = "tf-test-acc" |
| 68 | +} |
| 69 | + |
| 70 | +run "null_external_id_omits_condition" { |
| 71 | + command = plan |
| 72 | + |
| 73 | + providers = { |
| 74 | + aws = aws.default |
| 75 | + rhcs = rhcs.default |
| 76 | + time = time.default |
| 77 | + random = random.default |
| 78 | + } |
| 79 | + |
| 80 | + variables { |
| 81 | + trust_policy_external_id = null |
| 82 | + } |
| 83 | + |
| 84 | + assert { |
| 85 | + condition = !strcontains(aws_iam_role.account_role[0].assume_role_policy, "sts:ExternalId") |
| 86 | + error_message = "Installer trust policy must not include sts:ExternalId when trust_policy_external_id is null." |
| 87 | + } |
| 88 | + |
| 89 | + assert { |
| 90 | + condition = !strcontains(aws_iam_role.account_role[1].assume_role_policy, "sts:ExternalId") |
| 91 | + error_message = "Support trust policy must not include sts:ExternalId when trust_policy_external_id is null." |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +run "empty_external_id_rejected" { |
| 96 | + command = plan |
| 97 | + |
| 98 | + providers = { |
| 99 | + aws = aws.default |
| 100 | + rhcs = rhcs.default |
| 101 | + time = time.default |
| 102 | + random = random.default |
| 103 | + } |
| 104 | + |
| 105 | + variables { |
| 106 | + trust_policy_external_id = "" |
| 107 | + } |
| 108 | + |
| 109 | + expect_failures = [ |
| 110 | + var.trust_policy_external_id, |
| 111 | + ] |
| 112 | +} |
| 113 | + |
| 114 | +run "whitespace_external_id_rejected" { |
| 115 | + command = plan |
| 116 | + |
| 117 | + providers = { |
| 118 | + aws = aws.default |
| 119 | + rhcs = rhcs.default |
| 120 | + time = time.default |
| 121 | + random = random.default |
| 122 | + } |
| 123 | + |
| 124 | + variables { |
| 125 | + trust_policy_external_id = " " |
| 126 | + } |
| 127 | + |
| 128 | + expect_failures = [ |
| 129 | + var.trust_policy_external_id, |
| 130 | + ] |
| 131 | +} |
| 132 | + |
| 133 | +run "non_empty_external_id_adds_condition_to_installer_and_support" { |
| 134 | + command = plan |
| 135 | + |
| 136 | + providers = { |
| 137 | + aws = aws.default |
| 138 | + rhcs = rhcs.default |
| 139 | + time = time.default |
| 140 | + random = random.default |
| 141 | + } |
| 142 | + |
| 143 | + variables { |
| 144 | + trust_policy_external_id = "test-external-id-12345" |
| 145 | + } |
| 146 | + |
| 147 | + assert { |
| 148 | + condition = strcontains( |
| 149 | + aws_iam_role.account_role[0].assume_role_policy, |
| 150 | + "test-external-id-12345", |
| 151 | + ) |
| 152 | + error_message = "Installer trust policy must include the configured external ID." |
| 153 | + } |
| 154 | + |
| 155 | + assert { |
| 156 | + condition = strcontains( |
| 157 | + aws_iam_role.account_role[1].assume_role_policy, |
| 158 | + "test-external-id-12345", |
| 159 | + ) |
| 160 | + error_message = "Support trust policy must include the configured external ID." |
| 161 | + } |
| 162 | + |
| 163 | + assert { |
| 164 | + condition = strcontains( |
| 165 | + aws_iam_role.account_role[0].assume_role_policy, |
| 166 | + "sts:ExternalId", |
| 167 | + ) |
| 168 | + error_message = "Installer trust policy must include sts:ExternalId condition key." |
| 169 | + } |
| 170 | + |
| 171 | + assert { |
| 172 | + condition = strcontains( |
| 173 | + aws_iam_role.account_role[1].assume_role_policy, |
| 174 | + "sts:ExternalId", |
| 175 | + ) |
| 176 | + error_message = "Support trust policy must include sts:ExternalId condition key." |
| 177 | + } |
| 178 | +} |
| 179 | + |
| 180 | +run "worker_never_includes_external_id" { |
| 181 | + command = plan |
| 182 | + |
| 183 | + providers = { |
| 184 | + aws = aws.default |
| 185 | + rhcs = rhcs.default |
| 186 | + time = time.default |
| 187 | + random = random.default |
| 188 | + } |
| 189 | + |
| 190 | + variables { |
| 191 | + trust_policy_external_id = "test-external-id-12345" |
| 192 | + } |
| 193 | + |
| 194 | + assert { |
| 195 | + condition = !strcontains(aws_iam_role.account_role[2].assume_role_policy, "sts:ExternalId") |
| 196 | + error_message = "Worker trust policy must never include sts:ExternalId." |
| 197 | + } |
| 198 | + |
| 199 | + assert { |
| 200 | + condition = strcontains(aws_iam_role.account_role[2].assume_role_policy, "ec2.amazonaws.com") |
| 201 | + error_message = "Worker role trust policy must trust ec2.amazonaws.com." |
| 202 | + } |
| 203 | + |
| 204 | + assert { |
| 205 | + condition = length(aws_iam_role.account_role) == 3 |
| 206 | + error_message = "account_iam_resources must create Installer, Support, and Worker roles." |
| 207 | + } |
| 208 | +} |
0 commit comments