|
| 1 | +# Authentication with JIT API to get access token |
| 2 | +data "http" "jit_auth" { |
| 3 | + url = "${local.jit_api_endpoint}/authentication/login" |
| 4 | + method = "POST" |
| 5 | + |
| 6 | + request_headers = { |
| 7 | + "Accept" = "application/json" |
| 8 | + "Content-Type" = "application/json" |
| 9 | + } |
| 10 | + |
| 11 | + request_body = jsonencode({ |
| 12 | + clientId = var.jit_client_id |
| 13 | + secret = var.jit_secret |
| 14 | + }) |
| 15 | + |
| 16 | + lifecycle { |
| 17 | + postcondition { |
| 18 | + condition = self.status_code == 200 |
| 19 | + error_message = "JIT authentication failed with status ${self.status_code}" |
| 20 | + } |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +# Create state token using shell script resource |
| 25 | +resource "shell_script" "jit_state_token" { |
| 26 | + triggers = { |
| 27 | + client_id = var.jit_client_id |
| 28 | + integration_type = var.integration_type |
| 29 | + regions = join(",", var.aws_regions_to_monitor) |
| 30 | + org_root_id = var.organization_root_id |
| 31 | + } |
| 32 | + |
| 33 | + environment = { |
| 34 | + JIT_API_ENDPOINT = local.jit_api_endpoint |
| 35 | + STATE_TOKEN_BODY = jsonencode(local.state_token_request_body) |
| 36 | + } |
| 37 | + |
| 38 | + sensitive_environment = { |
| 39 | + JIT_AUTH_RESPONSE = data.http.jit_auth.response_body |
| 40 | + } |
| 41 | + |
| 42 | + lifecycle_commands { |
| 43 | + create = <<-EOT |
| 44 | + ACCESS_TOKEN=$(echo "$JIT_AUTH_RESPONSE" | jq -r '.accessToken') |
| 45 | + TOKEN=$(curl -s -X POST "$JIT_API_ENDPOINT/oauth/state-token" \ |
| 46 | + -H "Authorization: Bearer $ACCESS_TOKEN" \ |
| 47 | + -H "Accept: application/json" \ |
| 48 | + -H "Content-Type: application/json" \ |
| 49 | + -d "$STATE_TOKEN_BODY" \ |
| 50 | + | jq -r '.token') |
| 51 | + echo "{\"token\": \"$TOKEN\"}" |
| 52 | + EOT |
| 53 | + |
| 54 | + delete = "echo 'State token cleanup - no action needed'" |
| 55 | + } |
| 56 | + |
| 57 | + lifecycle { |
| 58 | + ignore_changes = [environment, sensitive_environment] |
| 59 | + } |
| 60 | + |
| 61 | + interpreter = ["/bin/bash", "-c"] |
| 62 | + |
| 63 | + depends_on = [data.http.jit_auth] |
| 64 | +} |
| 65 | + |
| 66 | +# CloudFormation Stack for single account integration |
| 67 | +resource "aws_cloudformation_stack" "jit_integration_account" { |
| 68 | + count = var.integration_type == "account" ? 1 : 0 |
| 69 | + |
| 70 | + name = var.stack_name |
| 71 | + template_url = local.cloudformation_template_url |
| 72 | + capabilities = var.capabilities |
| 73 | + |
| 74 | + parameters = { |
| 75 | + "ExternalId" = shell_script.jit_state_token.output["token"] |
| 76 | + "ResourceNamePrefix" = local.resource_name_prefix |
| 77 | + "AccountName" = var.account_name |
| 78 | + "ShouldIncludeRootAccount" = tostring(var.should_include_root_account) |
| 79 | + } |
| 80 | + |
| 81 | + lifecycle { |
| 82 | + prevent_destroy = true |
| 83 | + } |
| 84 | + |
| 85 | + depends_on = [ |
| 86 | + data.http.jit_auth, |
| 87 | + shell_script.jit_state_token |
| 88 | + ] |
| 89 | +} |
| 90 | + |
| 91 | +# CloudFormation StackSet for organization integration |
| 92 | +resource "aws_cloudformation_stack_set" "jit_integration_org" { |
| 93 | + count = var.integration_type == "org" ? 1 : 0 |
| 94 | + |
| 95 | + name = var.stack_name |
| 96 | + template_url = local.cloudformation_template_url |
| 97 | + capabilities = var.capabilities |
| 98 | + |
| 99 | + parameters = { |
| 100 | + "ExternalId" = shell_script.jit_state_token.output["token"] |
| 101 | + "ResourceNamePrefix" = local.resource_name_prefix |
| 102 | + "OrganizationRootId" = var.organization_root_id |
| 103 | + "ShouldIncludeRootAccount" = tostring(var.should_include_root_account) |
| 104 | + } |
| 105 | + |
| 106 | + # Auto deployment configuration for organization |
| 107 | + auto_deployment { |
| 108 | + enabled = true |
| 109 | + retain_stacks_on_account_removal = false |
| 110 | + } |
| 111 | + |
| 112 | + # Permission model for organization deployment |
| 113 | + permission_model = "SERVICE_MANAGED" |
| 114 | + |
| 115 | + lifecycle { |
| 116 | + prevent_destroy = true |
| 117 | + } |
| 118 | + |
| 119 | + depends_on = [ |
| 120 | + data.http.jit_auth, |
| 121 | + shell_script.jit_state_token |
| 122 | + ] |
| 123 | +} |
| 124 | + |
| 125 | +# StackSet instances for organization integration (deploys to all accounts in org) |
| 126 | +resource "aws_cloudformation_stack_set_instance" "jit_integration_org_instance" { |
| 127 | + count = var.integration_type == "org" ? 1 : 0 |
| 128 | + |
| 129 | + stack_set_name = aws_cloudformation_stack_set.jit_integration_org[0].name |
| 130 | + deployment_targets { |
| 131 | + organizational_unit_ids = [var.organization_root_id] |
| 132 | + } |
| 133 | + |
| 134 | + operation_preferences { |
| 135 | + region_concurrency_type = "PARALLEL" |
| 136 | + max_concurrent_percentage = 100 |
| 137 | + failure_tolerance_percentage = 10 |
| 138 | + } |
| 139 | + |
| 140 | + depends_on = [aws_cloudformation_stack_set.jit_integration_org] |
| 141 | +} |
0 commit comments