Skip to content

Commit 132bee2

Browse files
[NDR-71] Redact account ids and invocation urls (#302)
* [NDR-71] Add masking for sensitive information in Terraform plan output * [NDR-71] Add masking for sensitive information in Terraform plan output * [NDR-71] Enhance masking for sensitive URLs in Terraform plan output * [NDR-71] Remove debug outputs * [NDR-71] Refactor Terraform plan output handling to hide sensitive values * [NDR-71] Update GitHub Actions workflow for Terraform to enhance security and streamline processes * [NDR-71] Enhance masking logic for AWS account IDs in Terraform plan output * [NDR-71] Debug * [NDR-71] Add fallback message for missing Lambda URLs in output masking * [NDR-71] Enhance masking logic in Terraform plan output to handle missing API URLs, Lambda URLs, and AWS account IDs gracefully * [NDR-71] Enhance Terraform Plan output logging by redirecting stderr to capture errors in plan and show commands * [NDR-71] Update Terraform CI workflow for improved structure and clarity
1 parent 0682043 commit 132bee2

File tree

1 file changed

+41
-2
lines changed

1 file changed

+41
-2
lines changed

.github/workflows/terraform-dev-to-main-ci.yml

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,40 @@ jobs:
7070
- name: Terraform Plan
7171
id: plan
7272
run: |
73-
terraform plan -input=false -no-color -var-file="${{vars.TF_VARS_FILE}}" -out tf.plan
74-
terraform show -no-color tf.plan > tfplan.txt
73+
terraform plan -input=false -no-color -var-file="${{vars.TF_VARS_FILE}}" -out tf.plan > plan_output.txt 2>&1
74+
terraform show -no-color tf.plan > tfplan.txt 2>&1
75+
76+
# Mask sensitive URLs in the Terraform Plan output
77+
grep -Eo 'https://[a-zA-Z0-9.-]+\.execute-api\.[a-zA-Z0-9.-]+\.amazonaws\.com/[a-zA-Z0-9/._-]*' tfplan.txt | while read -r api_url; do
78+
if [ -n "$api_url" ]; then
79+
echo "::add-mask::$api_url"
80+
fi
81+
done || echo "No api URLs found to mask."
82+
83+
# Mask Lambda invocation URLs
84+
grep -Eo 'https://[a-zA-Z0-9.-]+\.lambda\.amazonaws\.com/[a-zA-Z0-9/._-]+' tfplan.txt | while read -r lambda_url; do
85+
if [ -n "$lambda_url" ]; then
86+
echo "::add-mask::$lambda_url"
87+
fi
88+
done || echo "No Lambda URLs found to mask."
89+
90+
# Mask AWS account IDs (12-digit numbers)
91+
grep -Eo '[0-9]{12}' tfplan.txt | while read -r account_id; do
92+
if [ -n "$account_id" ]; then
93+
echo "::add-mask::$account_id"
94+
fi
95+
done || echo "No Account IDs found to mask."
96+
97+
# Mask GitHub secrets
98+
echo "::add-mask::${{ secrets.AWS_ASSUME_ROLE }}"
99+
echo "::add-mask::${{ secrets.GITHUB_TOKEN }}"
100+
101+
# Mask Terraform variables
102+
echo "::add-mask::${{ vars.TF_VARS_FILE }}"
103+
104+
# Output the sanitized plan to logs
105+
cat plan_output.txt
106+
75107
echo "summary=$(grep -E 'Plan: [0-9]+ to add, [0-9]+ to change, [0-9]+ to destroy\.|No changes\. Your infrastructure matches the configuration\.' tfplan.txt | sed 's/.*No changes\. Your infrastructure matches the configuration/Plan: no changes/g' | sed 's/.*Plan: //g' | sed 's/\..*//g')" >> $GITHUB_OUTPUT
76108
working-directory: ./infrastructure
77109
shell: bash
@@ -87,6 +119,13 @@ jobs:
87119
${{ steps.plan.outputs.stderr }}
88120
EOF
89121
)
122+
123+
# Optionally redact sensitive strings in the PLAN_FULL variable
124+
PLAN_FULL=$(echo "$PLAN_FULL" | sed -E 's/[0-9]{12}/[REDACTED_AWS_ACCOUNT_ID]/g')
125+
PLAN_FULL=$(echo "$PLAN_FULL" | sed -E 's#https://[a-zA-Z0-9.-]+\.lambda\.amazonaws\.com/[a-zA-Z0-9/._-]+#[REDACTED_LAMBDA_URL]#g')
126+
PLAN_FULL=$(echo "$PLAN_FULL" | sed -E 's#https://[a-zA-Z0-9.-]+\.execute-api\.[a-zA-Z0-9.-]+\.amazonaws\.com/[a-zA-Z0-9/._-]*#[REDACTED_API_GATEWAY_URL]#g')
127+
PLAN_FULL=$(echo "$PLAN_FULL" | sed -E 's#arn:aws:iam::[0-9]{12}:role/[a-zA-Z0-9_-]+#[REDACTED_IAM_ROLE_ARN]#g')
128+
90129
echo "PLAN<<EOF" >> $GITHUB_ENV
91130
echo "${PLAN_FULL::$LENGTH}" >> $GITHUB_ENV
92131
[ ${#PLAN_FULL} -gt $LENGTH ] && echo "(truncated - see workflow logs for full output)" >> $GITHUB_ENV

0 commit comments

Comments
 (0)