Skip to content

Commit 7e9a43d

Browse files
authored
fix(codedeploy): the Service Principal is wrong in isolated regions (#19729)
Turns out, the Service Principal for CodeDeploy in the isolated regions is not regional like in all other regions, but rather universal (`codedeploy.amazonaws.com`). Fixes #19399 ---- ### All Submissions: * [x] Have you followed the guidelines in our [Contributing guide?](https://github.com/aws/aws-cdk/blob/master/CONTRIBUTING.md) ### Adding new Unconventional Dependencies: * [ ] This PR adds new unconventional dependencies following the process described [here](https://github.com/aws/aws-cdk/blob/master/CONTRIBUTING.md/#adding-new-unconventional-dependencies) ### New Features * [ ] Have you added the new feature to an [integration test](https://github.com/aws/aws-cdk/blob/master/INTEGRATION_TESTS.md)? * [ ] Did you use `cdk-integ` to deploy the infrastructure and generate the snapshot (i.e. `cdk-integ` without `--dry-run`)? *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 7c752e0 commit 7e9a43d

File tree

6 files changed

+38
-29
lines changed

6 files changed

+38
-29
lines changed

packages/@aws-cdk/aws-codedeploy/test/lambda/deployment-group.test.ts

+26-1
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,6 @@ describe('CodeDeploy Lambda DeploymentGroup', () => {
115115
});
116116
});
117117

118-
119118
test('can be created with explicit name', () => {
120119
const stack = new cdk.Stack();
121120
const application = new codedeploy.LambdaApplication(stack, 'MyApp');
@@ -589,6 +588,32 @@ describe('CodeDeploy Lambda DeploymentGroup', () => {
589588
},
590589
});
591590
});
591+
592+
test('uses the correct Service Principal in the us-isob-east-1 region', () => {
593+
const app = new cdk.App();
594+
const stack = new cdk.Stack(app, 'CodeDeployLambdaStack', {
595+
env: { region: 'us-isob-east-1' },
596+
});
597+
const alias = mockAlias(stack);
598+
new codedeploy.LambdaDeploymentGroup(stack, 'MyDG', {
599+
alias,
600+
});
601+
602+
Template.fromStack(stack).hasResourceProperties('AWS::IAM::Role', {
603+
AssumeRolePolicyDocument: {
604+
Statement: [
605+
{
606+
Action: 'sts:AssumeRole',
607+
Effect: 'Allow',
608+
Principal: {
609+
Service: 'codedeploy.amazonaws.com',
610+
},
611+
},
612+
],
613+
Version: '2012-10-17',
614+
},
615+
});
616+
});
592617
});
593618

594619
describe('imported with fromLambdaDeploymentGroupAttributes', () => {

packages/@aws-cdk/aws-iam/lib/principals.ts

+2-8
Original file line numberDiff line numberDiff line change
@@ -767,14 +767,8 @@ class ServicePrincipalToken implements cdk.IResolvable {
767767
public resolve(ctx: cdk.IResolveContext) {
768768
if (this.opts.region) {
769769
// Special case, handle it separately to not break legacy behavior.
770-
return (
771-
RegionInfo.get(this.opts.region).servicePrincipal(this.service) ??
772-
Default.servicePrincipal(
773-
this.service,
774-
this.opts.region,
775-
cdk.Aws.URL_SUFFIX,
776-
)
777-
);
770+
return RegionInfo.get(this.opts.region).servicePrincipal(this.service) ??
771+
Default.servicePrincipal(this.service, this.opts.region, cdk.Aws.URL_SUFFIX);
778772
}
779773

780774
const stack = cdk.Stack.of(ctx.scope);

packages/@aws-cdk/region-info/lib/aws-entities.ts

-11
Original file line numberDiff line numberDiff line change
@@ -133,14 +133,3 @@ export function partitionInformation(region: string): Region {
133133
}
134134
return PARTITION_MAP.default;
135135
}
136-
137-
/**
138-
* Build a lookup map for all regions
139-
*/
140-
export function generateRegionMap(cb: (region: string) => string): Record<string, string> {
141-
const ret: Record<string, string> = {};
142-
for (const region of AWS_REGIONS) {
143-
ret[region] = cb(region);
144-
}
145-
return ret;
146-
}

packages/@aws-cdk/region-info/lib/default.ts

+6-5
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@ export class Default {
3535
}
3636

3737
function determineConfiguration(service: string): (service: string, region: string, urlSuffix: string) => string {
38-
function universal(s: string) { return `${s}.amazonaws.com`; };
39-
function partitional(s: string, _: string, u: string) { return `${s}.${u}`; };
40-
function regional(s: string, r: string) { return `${s}.${r}.amazonaws.com`; };
41-
function regionalPartitional(s: string, r: string, u: string) { return `${s}.${r}.${u}`; };
38+
function universal(s: string) { return `${s}.amazonaws.com`; }
39+
function partitional(s: string, _: string, u: string) { return `${s}.${u}`; }
40+
function regional(s: string, r: string) { return `${s}.${r}.amazonaws.com`; }
41+
function regionalPartitional(s: string, r: string, u: string) { return `${s}.${r}.${u}`; }
4242

4343
// Exceptions for Service Principals in us-iso-*
4444
const US_ISO_EXCEPTIONS = new Set([
@@ -91,7 +91,8 @@ export class Default {
9191
case 'codedeploy':
9292
return region.startsWith('cn-')
9393
? regionalPartitional
94-
: regional;
94+
// ...except in the isolated regions, where it's universal
95+
: (region.startsWith('us-iso') ? universal : regional);
9596

9697
// Services with a regional AND partitional principal
9798
case 'logs':

packages/@aws-cdk/region-info/lib/fact.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ export class FactName {
182182
* The `.amazonaws.com` and `.amazonaws.com.cn` domains are stripped from service names, so they are
183183
* canonicalized in that respect.
184184
*/
185-
public static servicePrincipal(service: string) {
185+
public static servicePrincipal(service: string): string {
186186
return `service-principal:${service.replace(/\.amazonaws\.com(\.cn)?$/, '')}`;
187187
}
188188
}

packages/@aws-cdk/region-info/test/__snapshots__/region-info.test.js.snap

+3-3
Original file line numberDiff line numberDiff line change
@@ -795,7 +795,7 @@ Object {
795795
"servicePrincipals": Object {
796796
"application-autoscaling": "application-autoscaling.amazonaws.com",
797797
"autoscaling": "autoscaling.amazonaws.com",
798-
"codedeploy": "codedeploy.us-iso-east-1.amazonaws.com",
798+
"codedeploy": "codedeploy.amazonaws.com",
799799
"ec2": "ec2.c2s.ic.gov",
800800
"events": "events.amazonaws.com",
801801
"lambda": "lambda.amazonaws.com",
@@ -826,7 +826,7 @@ Object {
826826
"servicePrincipals": Object {
827827
"application-autoscaling": "application-autoscaling.amazonaws.com",
828828
"autoscaling": "autoscaling.amazonaws.com",
829-
"codedeploy": "codedeploy.us-iso-west-1.amazonaws.com",
829+
"codedeploy": "codedeploy.amazonaws.com",
830830
"ec2": "ec2.c2s.ic.gov",
831831
"events": "events.amazonaws.com",
832832
"lambda": "lambda.amazonaws.com",
@@ -857,7 +857,7 @@ Object {
857857
"servicePrincipals": Object {
858858
"application-autoscaling": "application-autoscaling.amazonaws.com",
859859
"autoscaling": "autoscaling.amazonaws.com",
860-
"codedeploy": "codedeploy.us-isob-east-1.amazonaws.com",
860+
"codedeploy": "codedeploy.amazonaws.com",
861861
"ec2": "ec2.sc2s.sgov.gov",
862862
"events": "events.amazonaws.com",
863863
"lambda": "lambda.amazonaws.com",

0 commit comments

Comments
 (0)