Skip to content

Commit 61e7ad1

Browse files
committed
Refactor AWS integration to use REST API provider for state token generation
- Replaced shell script resource with REST API provider for creating state tokens. - Updated parameters in CloudFormation stack resources to retrieve tokens from the new REST API resource. - Configured REST API provider with global headers for authentication. - Updated Terraform version requirements to include the new REST API provider.
1 parent 684dcd4 commit 61e7ad1

2 files changed

Lines changed: 42 additions & 53 deletions

File tree

src/integrations/aws_integration_automation/main.tf

Lines changed: 39 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
1+
# Configure the REST API provider with global headers
2+
provider "restapi" {
3+
uri = local.jit_api_endpoint
4+
write_returns_object = true
5+
create_returns_object = true
6+
7+
headers = {
8+
"Accept" = "application/json"
9+
"Content-Type" = "application/json"
10+
"Authorization" = "Bearer ${jsondecode(data.http.jit_auth.response_body).accessToken}"
11+
}
12+
}
13+
114
# Authentication with JIT API to get access token
215
data "http" "jit_auth" {
316
url = "${local.jit_api_endpoint}/authentication/login"
@@ -21,45 +34,21 @@ data "http" "jit_auth" {
2134
}
2235
}
2336

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-
37+
# Create state token using REST API provider
38+
resource "restapi_object" "jit_state_token" {
39+
path = "/oauth/state-token"
40+
create_method = "POST"
41+
read_path = "/oauth/state-token/{id}/echo"
42+
id_attribute = "id"
43+
ignore_changes_to = ["token"]
44+
# Request body with state token parameters
45+
data = jsonencode(local.state_token_request_body)
46+
47+
# Ignore changes to data since read endpoint returns different structure
5748
lifecycle {
58-
ignore_changes = [environment, sensitive_environment]
49+
ignore_changes = [data]
5950
}
6051

61-
interpreter = ["/bin/bash", "-c"]
62-
6352
depends_on = [data.http.jit_auth]
6453
}
6554

@@ -71,20 +60,20 @@ resource "aws_cloudformation_stack" "jit_integration_account" {
7160
template_url = local.cloudformation_template_url
7261
capabilities = var.capabilities
7362

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-
}
63+
parameters = {
64+
"ExternalId" = jsondecode(restapi_object.jit_state_token.create_response)["token"]
65+
"ResourceNamePrefix" = local.resource_name_prefix
66+
"AccountName" = var.account_name
67+
"ShouldIncludeRootAccount" = tostring(var.should_include_root_account)
68+
}
8069

8170
lifecycle {
8271
prevent_destroy = true
8372
}
8473

8574
depends_on = [
8675
data.http.jit_auth,
87-
shell_script.jit_state_token
76+
restapi_object.jit_state_token
8877
]
8978
}
9079

@@ -96,12 +85,12 @@ resource "aws_cloudformation_stack_set" "jit_integration_org" {
9685
template_url = local.cloudformation_template_url
9786
capabilities = var.capabilities
9887

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-
}
88+
parameters = {
89+
"ExternalId" = jsondecode(restapi_object.jit_state_token.create_response)["token"]
90+
"ResourceNamePrefix" = local.resource_name_prefix
91+
"OrganizationRootId" = var.organization_root_id
92+
"ShouldIncludeRootAccount" = tostring(var.should_include_root_account)
93+
}
10594

10695
# Auto deployment configuration for organization
10796
auto_deployment {
@@ -118,7 +107,7 @@ resource "aws_cloudformation_stack_set" "jit_integration_org" {
118107

119108
depends_on = [
120109
data.http.jit_auth,
121-
shell_script.jit_state_token
110+
restapi_object.jit_state_token
122111
]
123112
}
124113

src/integrations/aws_integration_automation/versions.tf

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ terraform {
1717
version = ">= 2.0"
1818
}
1919

20-
shell = {
21-
source = "scottwinkler/shell"
22-
version = ">= 1.7.0"
20+
restapi = {
21+
source = "Mastercard/restapi"
22+
version = ">= 1.19.1"
2323
}
2424
}
2525
}

0 commit comments

Comments
 (0)