|
2 | 2 | # cfn-test-create-inputs.sh |
3 | 3 | # |
4 | 4 | # This tool generates json files in the inputs/ for `cfn test`. |
| 5 | +# It creates all required AWS resources (S3 bucket, IAM role, Cloud Provider Access role) |
5 | 6 | # |
6 | 7 |
|
7 | 8 | set -o errexit |
8 | 9 | set -o nounset |
9 | 10 | set -o pipefail |
10 | 11 |
|
11 | 12 | function usage { |
12 | | - echo "usage:$0 <project_name> <bucket_name> <iam_role_id>" |
13 | | - echo "Generates test input files for log integration" |
| 13 | + echo "usage: $0 <project_name>" |
| 14 | + echo "Creates S3 bucket, Cloud Provider Access role, IAM role, and generates test input files for log integration" |
14 | 15 | exit 0 |
15 | 16 | } |
16 | 17 |
|
17 | | -if [ "$#" -ne 3 ]; then usage; fi |
| 18 | +if [ "$#" -ne 1 ]; then usage; fi |
18 | 19 | if [[ "$*" == help ]]; then usage; fi |
19 | 20 |
|
20 | | -rm -rf inputs |
21 | | -mkdir inputs |
| 21 | +region=$AWS_DEFAULT_REGION |
| 22 | +awsRegion=$AWS_DEFAULT_REGION |
| 23 | +if [ -z "$region" ]; then |
| 24 | + region=$(aws configure get region) |
| 25 | + awsRegion=$region |
| 26 | +fi |
| 27 | + |
| 28 | +regionFormatted=$(echo "$region" | sed -e "s/-/_/g" | tr '[:lower:]' '[:upper:]') |
| 29 | +echo "Using region: $region (formatted: $regionFormatted)" |
| 30 | + |
| 31 | +roleName="mongodb-atlas-logs-role-${regionFormatted}" |
| 32 | +policyName="atlas-logs-s3-policy-${regionFormatted}" |
| 33 | +bucketTag="${CFN_TEST_TAG:-$(date +%Y%m%d%H%M%S)}" |
| 34 | +bucketName="mongodb-atlas-cfn-test-logs-${bucketTag}" |
22 | 35 |
|
23 | | -#set profile - relevant for contract tests which define a custom profile |
| 36 | +echo "Bucket name: ${bucketName}" |
24 | 37 | profile="default" |
25 | 38 | if [ ${MONGODB_ATLAS_PROFILE+x} ]; then |
26 | 39 | echo "profile set to ${MONGODB_ATLAS_PROFILE}" |
27 | 40 | profile=${MONGODB_ATLAS_PROFILE} |
28 | 41 | fi |
29 | 42 |
|
30 | 43 | projectName="${1}" |
31 | | -bucketName="${2}" |
32 | | -iamRoleId="${3}" |
33 | | - |
34 | 44 | projectId=$(atlas projects list --output json | jq --arg NAME "${projectName}" -r '.results[] | select(.name==$NAME) | .id') |
35 | 45 | if [ -z "$projectId" ]; then |
36 | 46 | projectId=$(atlas projects create "${projectName}" --output=json | jq -r '.id') |
|
39 | 49 | echo -e "FOUND project \"${projectName}\" with id: ${projectId}\n" |
40 | 50 | fi |
41 | 51 |
|
42 | | -echo "bucketName: $bucketName" |
43 | | -echo "iamRoleId: $iamRoleId" |
| 52 | +echo "--------------------------------Creating Cloud Provider Access Role----------------------------" |
| 53 | +roleID=$(atlas cloudProviders accessRoles aws create --projectId "${projectId}" --output json | jq -r '.roleId') |
| 54 | +echo "--------------------------------Mongo CLI Role creation ends----------------------------" |
| 55 | + |
| 56 | +atlasAWSAccountArn=$(atlas cloudProviders accessRoles list --projectId "${projectId}" --output json | jq --arg roleID "${roleID}" -r '.awsIamRoles[] | select(.roleId == $roleID) | .atlasAWSAccountArn') |
| 57 | +atlasAssumedRoleExternalId=$(atlas cloudProviders accessRoles list --projectId "${projectId}" --output json | jq --arg roleID "${roleID}" -r '.awsIamRoles[] | select(.roleId == $roleID) | .atlasAssumedRoleExternalId') |
| 58 | + |
| 59 | +jq --arg atlasAssumedRoleExternalId "$atlasAssumedRoleExternalId" \ |
| 60 | + --arg atlasAWSAccountArn "$atlasAWSAccountArn" \ |
| 61 | + '.Statement[0].Principal.AWS = $atlasAWSAccountArn | .Statement[0].Condition.StringEquals["sts:ExternalId"] = $atlasAssumedRoleExternalId' \ |
| 62 | + "$(dirname "$0")/role-policy-template.json" >"$(dirname "$0")/trust-policy.json" |
| 63 | +echo "--------------------------------AWS Role creation starts----------------------------" |
| 64 | +awsRoleID=$(aws iam get-role --role-name "${roleName}" 2>/dev/null | jq -r '.Role.RoleId' || echo "") |
| 65 | +if [ -z "$awsRoleID" ]; then |
| 66 | + awsRoleID=$(aws iam create-role \ |
| 67 | + --role-name "${roleName}" \ |
| 68 | + --assume-role-policy-document "file://$(dirname "$0")/trust-policy.json" | jq -r '.Role.RoleId') |
| 69 | + echo -e "No role found, hence creating the role. Created id: ${awsRoleID}\n" |
| 70 | +else |
| 71 | + aws iam delete-role-policy --role-name "${roleName}" --policy-name "${policyName}" 2>/dev/null || true |
| 72 | + aws iam delete-role --role-name "${roleName}" |
| 73 | + awsRoleID=$(aws iam create-role \ |
| 74 | + --role-name "${roleName}" \ |
| 75 | + --assume-role-policy-document "file://$(dirname "$0")/trust-policy.json" | jq -r '.Role.RoleId') |
| 76 | + echo -e "FOUND role id, deleted and recreated with new trust policy. Created id: ${awsRoleID}\n" |
| 77 | +fi |
| 78 | +echo "--------------------------------AWS Role creation ends----------------------------" |
| 79 | + |
| 80 | +awsRoleArn=$(aws iam get-role --role-name "${roleName}" | jq -r '.Role.Arn') |
| 81 | + |
| 82 | +echo "--------------------------------Creating S3 Bucket----------------------------" |
| 83 | +if aws s3 ls "s3://${bucketName}" 2>/dev/null; then |
| 84 | + aws s3 rb "s3://${bucketName}" --force |
| 85 | +fi |
| 86 | +aws s3 mb "s3://${bucketName}" --region "${awsRegion}" |
| 87 | +echo "Created S3 bucket: ${bucketName}" |
| 88 | +echo "--------------------------------Attaching S3 policy to IAM role----------------------------" |
| 89 | +bucketArn="arn:aws:s3:::${bucketName}" |
| 90 | +jq --arg bucketArn "$bucketArn" \ |
| 91 | + --arg bucketArnWildcard "${bucketArn}/*" \ |
| 92 | + '.Statement[0].Resource[0] = $bucketArn | .Statement[0].Resource[1] = $bucketArnWildcard' \ |
| 93 | + "$(dirname "$0")/s3-policy-template.json" >"$(dirname "$0")/s3-policy.json" |
| 94 | + |
| 95 | +aws iam put-role-policy \ |
| 96 | + --role-name "${roleName}" \ |
| 97 | + --policy-name "${policyName}" \ |
| 98 | + --policy-document "file://$(dirname "$0")/s3-policy.json" |
| 99 | +echo "--------------------------------attach mongodb Role to AWS Role ends----------------------------" |
| 100 | + |
| 101 | +# shellcheck disable=SC2086 |
| 102 | +sleep 30 |
| 103 | + |
| 104 | +atlas cloudProviders accessRoles aws authorize "${roleID}" \ |
| 105 | + --projectId "${projectId}" \ |
| 106 | + --iamAssumedRoleArn "${awsRoleArn}" |
| 107 | +echo "--------------------------------authorize mongodb Role ends----------------------------" |
| 108 | +rm -rf inputs |
| 109 | +mkdir inputs |
44 | 110 |
|
45 | 111 | WORDTOREMOVE="template." |
46 | 112 | cd "$(dirname "$0")" || exit |
47 | 113 | for inputFile in inputs_*; do |
48 | 114 | outputFile=${inputFile//$WORDTOREMOVE/} |
49 | 115 | jq --arg projectId "$projectId" \ |
50 | 116 | --arg bucketName "$bucketName" \ |
51 | | - --arg iamRoleId "$iamRoleId" \ |
| 117 | + --arg iamRoleId "$roleID" \ |
52 | 118 | --arg profile "$profile" \ |
53 | 119 | '.Profile?|=$profile | .ProjectId?|=$projectId | .BucketName?|=$bucketName | .IamRoleId?|=$iamRoleId' \ |
54 | 120 | "$inputFile" >"../inputs/$outputFile" |
|
0 commit comments