Skip to content

Commit 1a4013b

Browse files
test
1 parent 1df1eb8 commit 1a4013b

2 files changed

Lines changed: 45 additions & 19 deletions

File tree

cfn-resources/log-integration/test/cfn-test-create-inputs.sh

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ fi
2828
regionFormatted=$(echo "$region" | sed -e "s/-/_/g" | tr '[:lower:]' '[:upper:]')
2929
echo "Using region: $region (formatted: $regionFormatted)"
3030

31-
roleName="mongodb-atlas-logs-role-${regionFormatted}"
31+
# Use dynamic role name to avoid conflicts in CI (matches test-folder pattern)
32+
roleName="mongodb-atlas-logs-role-${regionFormatted}-$(date +%s)-${RANDOM}"
3233
policyName="atlas-logs-s3-policy-${regionFormatted}"
3334
bucketTag="${CFN_TEST_TAG:-$(date +%Y%m%d%H%M%S)}"
3435
bucketName="mongodb-atlas-cfn-test-logs-${bucketTag}"
@@ -115,8 +116,9 @@ for inputFile in inputs_*; do
115116
jq --arg projectId "$projectId" \
116117
--arg bucketName "$bucketName" \
117118
--arg iamRoleId "$roleID" \
119+
--arg awsRoleArn "$awsRoleArn" \
118120
--arg profile "$profile" \
119-
'.Profile?|=$profile | .ProjectId?|=$projectId | .BucketName?|=$bucketName | .IamRoleId?|=$iamRoleId' \
121+
'.Profile?|=$profile | .ProjectId?|=$projectId | .BucketName?|=$bucketName | .IamRoleId?|=$iamRoleId | .AwsRoleArn?|=$awsRoleArn' \
120122
"$inputFile" >"../inputs/$outputFile"
121123
done
122124
cd ..

cfn-resources/log-integration/test/cfn-test-delete-inputs.sh

Lines changed: 41 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,31 +18,55 @@ projectId=$(jq -r '.ProjectId' ./inputs/inputs_1_create.json)
1818
echo "Check if a project is created $projectId"
1919
export MCLI_PROJECT_ID=$projectId
2020

21-
region=$AWS_DEFAULT_REGION
22-
if [ -z "$region" ]; then
23-
region=$(aws configure get region)
21+
# Extract role ARN from input file (dynamically generated during create)
22+
roleArn=$(jq -r '.AwsRoleArn // empty' ./inputs/inputs_1_create.json)
23+
if [ -z "$roleArn" ] || [ "$roleArn" == "null" ]; then
24+
echo "Warning: AwsRoleArn not found in inputs file, skipping IAM role cleanup"
25+
roleName=""
26+
policyName=""
27+
else
28+
# Extract role name from ARN (everything after the last '/')
29+
roleName=$(echo "${roleArn}" | awk -F'/' '{print $NF}')
30+
region=$AWS_DEFAULT_REGION
31+
if [ -z "$region" ]; then
32+
region=$(aws configure get region)
33+
fi
34+
# shellcheck disable=SC2001
35+
region=$(echo "$region" | sed -e "s/-/_/g")
36+
region=$(echo "$region" | tr '[:lower:]' '[:upper:]')
37+
policyName="atlas-logs-s3-policy-${region}"
38+
echo "Found IAM role to delete: ${roleName}"
2439
fi
25-
# shellcheck disable=SC2001
26-
region=$(echo "$region" | sed -e "s/-/_/g")
27-
region=$(echo "$region" | tr '[:lower:]' '[:upper:]')
28-
29-
roleName="mongodb-atlas-logs-role-${region}"
30-
policyName="atlas-logs-s3-policy-${region}"
3140

32-
trustPolicy=$(jq '.Statement[0].Condition.StringEquals["sts:ExternalId"]' "$(dirname "$0")/trust-policy.json")
33-
# shellcheck disable=SC2001
34-
atlasAssumedRoleExternalID=$(echo "${trustPolicy}" | sed 's/"//g')
41+
# Deauthorize role from Atlas if trust policy exists
42+
if [ -f "$(dirname "$0")/trust-policy.json" ]; then
43+
trustPolicy=$(jq '.Statement[0].Condition.StringEquals["sts:ExternalId"]' "$(dirname "$0")/trust-policy.json")
44+
# shellcheck disable=SC2001
45+
atlasAssumedRoleExternalID=$(echo "${trustPolicy}" | sed 's/"//g')
3546

36-
roleId=$(atlas cloudProviders accessRoles list --projectId "${projectId}" --output json | jq --arg roleID "${atlasAssumedRoleExternalID}" -r '.awsIamRoles[] | select(.atlasAssumedRoleExternalId | test($roleID)) | .roleId')
47+
roleId=$(atlas cloudProviders accessRoles list --projectId "${projectId}" --output json | jq --arg roleID "${atlasAssumedRoleExternalID}" -r '.awsIamRoles[] | select(.atlasAssumedRoleExternalId | test($roleID)) | .roleId')
3748

38-
atlas cloudProviders accessRoles aws deauthorize "${roleId}" --projectId "${projectId}" --force
39-
echo "--------------------------------deauthorize role ends----------------------------"
49+
if [ -n "${roleId}" ] && [ "${roleId}" != "null" ]; then
50+
echo "Deauthorizing role from Atlas: ${roleId}"
51+
atlas cloudProviders accessRoles aws deauthorize "${roleId}" --projectId "${projectId}" --force
52+
echo "--------------------------------deauthorize role ends----------------------------"
53+
else
54+
echo "Warning: Could not find Atlas role ID to deauthorize"
55+
fi
56+
else
57+
echo "Warning: trust-policy.json not found, skipping Atlas role deauthorization"
58+
fi
4059
bucketName=$(jq -r '.BucketName' "./inputs/inputs_1_create.json")
4160
aws s3 rb "s3://${bucketName}" --force
4261

4362
echo "--------------------------------delete IAM role starts----------------------------"
44-
aws iam delete-role-policy --role-name "$roleName" --policy-name "$policyName"
45-
aws iam delete-role --role-name "$roleName"
63+
if [ -n "$roleName" ]; then
64+
aws iam delete-role-policy --role-name "$roleName" --policy-name "$policyName" || echo "Failed to delete role policy (may not exist)"
65+
aws iam delete-role --role-name "$roleName" || echo "Failed to delete role (may not exist)"
66+
echo "Deleted IAM role: ${roleName}"
67+
else
68+
echo "No IAM role to delete (not found in inputs)"
69+
fi
4670
echo "--------------------------------delete IAM role ends----------------------------"
4771

4872
#delete project

0 commit comments

Comments
 (0)