Skip to content

Commit 4ea6a9b

Browse files
fix: fix dependency in reader_profile
1 parent 0e4d09f commit 4ea6a9b

File tree

5 files changed

+66
-54
lines changed

5 files changed

+66
-54
lines changed

modules/integrations/github_webhook_relay_destination/outputs.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ output "role_arn" {
44
}
55

66
output "webhook" {
7-
value = try(jsondecode(data.aws_secretsmanager_secret_version.target[0].secret_string), null)
7+
value = try(data.external.fetch_secret_value[0].result.secret_value, null)
88
sensitive = true
99
description = "Webhook relay and secret fetched from source account."
1010
}

modules/integrations/github_webhook_relay_destination/scripts/create_assume_profile.sh

Lines changed: 0 additions & 28 deletions
This file was deleted.
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#!/usr/bin/env bash
2+
set -euo pipefail
3+
4+
READER_ROLE_ARN="$1"
5+
SOURCE_ROLE_ARN="$2"
6+
SOURCE_SECRET_ARN="$3"
7+
AWS_PROFILE="$4"
8+
AWS_REGION="$5"
9+
10+
############################################
11+
# 1. Assume the reader role (first hop)
12+
############################################
13+
aws sts assume-role \
14+
--role-arn "$READER_ROLE_ARN" \
15+
--role-session-name reader-temp \
16+
--profile "$AWS_PROFILE" \
17+
--region "$AWS_REGION" \
18+
--query 'Credentials' \
19+
--output json >/tmp/reader-creds.json
20+
21+
aws configure set aws_access_key_id "$(jq -r .AccessKeyId /tmp/reader-creds.json)" --profile reader-temp
22+
aws configure set aws_secret_access_key "$(jq -r .SecretAccessKey /tmp/reader-creds.json)" --profile reader-temp
23+
aws configure set aws_session_token "$(jq -r .SessionToken /tmp/reader-creds.json)" --profile reader-temp
24+
25+
############################################
26+
# 2. Assume the external secret source role (second hop)
27+
############################################
28+
aws sts assume-role \
29+
--role-arn "$SOURCE_ROLE_ARN" \
30+
--role-session-name source-temp \
31+
--profile reader-temp \
32+
--region "$AWS_REGION" \
33+
--query 'Credentials' \
34+
--output json >/tmp/source-creds.json
35+
36+
aws configure set aws_access_key_id "$(jq -r .AccessKeyId /tmp/source-creds.json)" --profile source-temp
37+
aws configure set aws_secret_access_key "$(jq -r .SecretAccessKey /tmp/source-creds.json)" --profile source-temp
38+
aws configure set aws_session_token "$(jq -r .SessionToken /tmp/source-creds.json)" --profile source-temp
39+
40+
############################################
41+
# 3. Use the final profile for AWS calls
42+
############################################
43+
SECRET_VALUE=$(aws secretsmanager get-secret-value \
44+
--secret-id "$SOURCE_SECRET_ARN" \
45+
--region "$AWS_REGION" \
46+
--query 'SecretString' \
47+
--profile source-temp \
48+
--output text)
49+
50+
# 4. Return as JSON to Terraform
51+
jq -n --arg secret_value "$SECRET_VALUE" '{"secret_value":$secret_value}'

modules/integrations/github_webhook_relay_destination/webhook_relay_source.tf

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -34,36 +34,25 @@ resource "aws_iam_role_policy" "allow_assume_external_inline" {
3434
policy = data.aws_iam_policy_document.allow_assume_external[0].json
3535
}
3636

37+
# Use an external script that:
38+
# 1. Assumes the reader role (first hop)
39+
# 2. From that session, assumes the external source_secret_role_arn (second hop)
40+
# 3. Fetches the secret from source_secret_arn
41+
# 4. Returns the secret value to Terraform as JSON
42+
data "external" "fetch_secret_value" {
43+
count = var.reader_config.enable_secret_fetch ? 1 : 0
3744

38-
data "external" "reader_profile" {
3945
program = [
4046
"bash",
41-
"${path.module}/scripts/create_assume_profile.sh",
47+
"${path.module}/scripts/fetch_secret_value.sh",
4248
aws_iam_role.reader.arn,
49+
var.reader_config.source_secret_role_arn,
50+
var.reader_config.source_secret_arn,
4351
var.aws_profile,
44-
"reader-temp",
4552
var.aws_region
4653
]
4754

48-
depends_on = [aws_iam_role.reader]
49-
}
50-
51-
52-
provider "aws" {
53-
alias = "external_secret"
54-
profile = data.external.reader_profile.result.profile
55-
region = var.reader_config.source_secret_region
56-
57-
dynamic "assume_role" {
58-
for_each = var.reader_config.enable_secret_fetch ? [1] : []
59-
content {
60-
role_arn = var.reader_config.source_secret_role_arn
61-
}
62-
}
63-
}
64-
65-
data "aws_secretsmanager_secret_version" "target" {
66-
count = var.reader_config.enable_secret_fetch ? 1 : 0
67-
provider = aws.external_secret
68-
secret_id = var.reader_config.source_secret_arn
55+
depends_on = [
56+
aws_iam_role_policy.allow_assume_external_inline
57+
]
6958
}

modules/platform/forge_runners/outputs.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ output "forge_webhook_relay" {
2525
description = "Webhook relay integration outputs."
2626
value = {
2727
source_secret_arn = try(aws_secretsmanager_secret.github_webhook_relay[0].arn, null)
28-
source_secret_role_arn = try(aws_iam_role.secret_reader[0].id, null)
28+
source_secret_role_arn = try(aws_iam_role.secret_reader[0].arn, null)
2929
}
3030
}
3131

0 commit comments

Comments
 (0)