Skip to content

Commit 6d87757

Browse files
committed
[sdlf-cicd] handle crossaccount role trust relationship
1 parent f86197e commit 6d87757

File tree

3 files changed

+29
-4
lines changed

3 files changed

+29
-4
lines changed

sdlf-cicd/deploy-cicd.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,14 @@ aws cloudformation deploy \
182182
${REGION:+--region "$REGION"} \
183183
${PROFILE:+--profile "$PROFILE"} || exit 1
184184

185+
if ! "$dflag"
186+
then
187+
CODEBUILD_ROLE=$(aws codebuild batch-get-projects --names "sdlf-cicd-$1" --query "projects[0].serviceRole" --output text ${REGION:+--region "$REGION"} ${PROFILE:+--profile "$PROFILE"} | cut -d'/' -f2)
188+
CODEBUILD_ROLE_BOOTSTRAP=$(aws codebuild batch-get-projects --names "sdlf-cicd-bootstrap" --query "projects[0].serviceRole" --output text ${REGION:+--region "$REGION"} ${PROFILE:+--profile "$PROFILE"} | cut -d'/' -f2)
189+
echo "Role names to provide to ./deploy-role.sh:"
190+
echo "$CODEBUILD_ROLE $CODEBUILD_ROLE_BOOTSTRAP"
191+
fi
192+
185193
if "$cflag"
186194
then
187195
echo "The list ${CONSTRUCTS[*]} will be used in a future release to restrict CodeBuild permissions to the set of permissions required by the constructs it can deploy."

sdlf-cicd/deploy-role.sh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ Options
2323
-V, --version -- Print the SDLF version
2424
-h, --help -- Show this help message
2525
-p -- Name of the AWS profile to use
26+
-b -- AWS account ID of the CodeBuild project
2627
-c -- Name of the SDLF construct that will be used
2728
<name> -- Name to uniquely identify this deployment
2829
@@ -149,6 +150,15 @@ if ! "$bflag"
149150
then
150151
echo "CodeBuild project is assumed to be in the same AWS account" >&2
151152
CODEBUILD_ACCOUNT_ID=$(aws sts get-caller-identity --query Account --output text ${REGION:+--region "$REGION"} ${PROFILE:+--profile "$PROFILE"})
153+
154+
CODEBUILD_ROLE=$(aws codebuild batch-get-projects --names "sdlf-cicd-$1" --query "projects[0].serviceRole" --output text ${REGION:+--region "$REGION"} ${PROFILE:+--profile "$PROFILE"} | cut -d'/' -f2)
155+
CODEBUILD_ROLE_BOOTSTRAP=$(aws codebuild batch-get-projects --names "sdlf-cicd-bootstrap" --query "projects[0].serviceRole" --output text ${REGION:+--region "$REGION"} ${PROFILE:+--profile "$PROFILE"} | cut -d'/' -f2)
156+
else
157+
if [ -z ${2+x} ]; then die 'ERROR: "./deploy-role.sh" requires a second non-option argument providing the CodeBuild project IAM role name.'; fi
158+
if [ -z ${3+x} ]; then die 'ERROR: "./deploy-role.sh" requires a third non-option argument providing the boostrap CodeBuild project IAM role name.'; fi
159+
160+
CODEBUILD_ROLE=$2
161+
CODEBUILD_ROLE_BOOTSTRAP=$3
152162
fi
153163

154164
STACK_NAME="sdlf-cicd-role-$CODEBUILD_ACCOUNT_ID-$1"
@@ -159,6 +169,8 @@ aws cloudformation deploy \
159169
--parameter-overrides \
160170
pCodeBuildAccountId="$CODEBUILD_ACCOUNT_ID" \
161171
pCodeBuildSuffix="$1" \
172+
pCodeBuildBootstrapRole="$CODEBUILD_ROLE_BOOTSTRAP" \
173+
pCodeBuildUserRepositoryRole="$CODEBUILD_ROLE" \
162174
--tags Framework=sdlf \
163175
--capabilities "CAPABILITY_NAMED_IAM" "CAPABILITY_AUTO_EXPAND" \
164176
${REGION:+--region "$REGION"} \

sdlf-cicd/template-cicd-generic-role.yaml

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ Parameters:
1111
Type: String
1212
AllowedPattern: (\d{12}|^$)
1313
ConstraintDescription: Must be an AWS account ID
14+
pCodeBuildBootstrapRole:
15+
Description: "sdlf-cicd-bootstrap CodeBuild IAM role name"
16+
Type: String
17+
pCodeBuildUserRepositoryRole:
18+
Description: "sdlf-cicd-CodeBuildSuffix CodeBuild IAM role name"
19+
Type: String
1420

1521
Resources:
1622
rSdlfCicdCodeBuildRole:
@@ -25,12 +31,11 @@ Resources:
2531
Statement:
2632
- Effect: Allow
2733
Principal:
28-
Service: codebuild.amazonaws.com
34+
AWS:
35+
- !Sub arn:${AWS::Partition}:iam::${pCodeBuildAccountId}:role/${pCodeBuildBootstrapRole}
36+
- !Sub arn:${AWS::Partition}:iam::${pCodeBuildAccountId}:role/${pCodeBuildUserRepositoryRole}
2937
Action:
3038
- sts:AssumeRole
31-
Condition:
32-
StringEquals:
33-
"aws:SourceAccount": "111111111111"
3439
Policies:
3540
- PolicyName: sdlf-codebuild
3641
PolicyDocument:

0 commit comments

Comments
 (0)