Skip to content

Commit

Permalink
Migrate Github SES role to CDK
Browse files Browse the repository at this point in the history
  • Loading branch information
AshCorr committed Aug 2, 2024
1 parent bcd7186 commit e696026
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 30 deletions.
23 changes: 16 additions & 7 deletions cdk/lib/__snapshots__/identity-gateway.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ exports[`The IdentityGateway stack matches the snapshot 1`] = `
},
},
"Metadata": {
"gu:cdk:constructs": [],
"gu:cdk:constructs": [
"GuRole",
],
"gu:cdk:version": "TEST",
},
"Parameters": {
Expand Down Expand Up @@ -819,28 +821,35 @@ exports[`The IdentityGateway stack matches the snapshot 1`] = `
},
"Type": "AWS::CloudWatch::Alarm",
},
"GithubActionsSESSendEmailsRole": {
"Condition": "NotIsProd",
"GithubActionsSESSendEmailsRole3ACDD4F9": {
"Properties": {
"AssumeRolePolicyDocument": {
"Statement": [
{
"Action": "sts:AssumeRoleWithWebIdentity",
"Condition": {
"StringLike": {
"token.actions.githubusercontent.com:sub": {
"Fn::Sub": "repo:guardian/gateway:*",
},
"actions.githubusercontent.com:sub": "repo:guardian/gateway:*",
},
},
"Effect": "Allow",
"Principal": {
"Federated": {
"Fn::Sub": "arn:aws:iam::\${AWS::AccountId}:oidc-provider/token.actions.githubusercontent.com",
"Fn::Join": [
"",
[
"arn:aws:iam::",
{
"Ref": "AWS::AccountId",
},
":oidc-provider/token.actions.githubusercontent.com",
],
],
},
},
},
],
"Version": "2012-10-17",
},
"Policies": [
{
Expand Down
32 changes: 32 additions & 0 deletions cdk/lib/identity-gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@ import type { GuStackProps } from '@guardian/cdk/lib/constructs/core';
import { GuStack } from '@guardian/cdk/lib/constructs/core';
import type { App } from 'aws-cdk-lib';
import { CfnInclude } from 'aws-cdk-lib/cloudformation-include';
import { GuRole } from '@guardian/cdk/lib/constructs/iam';

Check failure on line 5 in cdk/lib/identity-gateway.ts

View workflow job for this annotation

GitHub Actions / Continuous Integration

`@guardian/cdk/lib/constructs/iam` import should occur before type import of `aws-cdk-lib`
import {

Check failure on line 6 in cdk/lib/identity-gateway.ts

View workflow job for this annotation

GitHub Actions / Continuous Integration

`aws-cdk-lib/aws-iam` import should occur before import of `aws-cdk-lib/cloudformation-include`
Effect,
PolicyDocument,
PolicyStatement,
WebIdentityPrincipal,
} from 'aws-cdk-lib/aws-iam';

export class IdentityGateway extends GuStack {
constructor(scope: App, id: string, props: GuStackProps) {
Expand All @@ -10,5 +17,30 @@ export class IdentityGateway extends GuStack {
new CfnInclude(this, 'IdentityGateway', {
templateFile: '../cloudformation.yaml',
});

if (['CODE', 'TEST'].includes(props.stage)) {
new GuRole(this, 'GithubActionsSESSendEmailsRole', {
roleName: 'GithubActionsSESSendEmailsRole',
assumedBy: new WebIdentityPrincipal(
`arn:aws:iam::${this.account}:oidc-provider/token.actions.githubusercontent.com`,
{
StringLike: {
'actions.githubusercontent.com:sub': 'repo:guardian/gateway:*',
},
},
),
inlinePolicies: {
SendEmailSES: new PolicyDocument({
statements: [
new PolicyStatement({
actions: ['ses:SendEmail'],
resources: ['*'],
effect: Effect.ALLOW,
}),
],
}),
},
});
}
}
}
23 changes: 0 additions & 23 deletions cloudformation.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -191,29 +191,6 @@ Resources:
- ssmmessages:OpenControlChannel
- ssmmessages:OpenDataChannel
Resource: '*'
GithubActionsSESSendEmailsRole:
Type: AWS::IAM::Role
Condition: NotIsProd
Properties:
RoleName: GithubActionsSESSendEmailsRole
AssumeRolePolicyDocument:
Statement:
- Effect: Allow
Action: sts:AssumeRoleWithWebIdentity
Principal:
Federated: !Sub arn:aws:iam::${AWS::AccountId}:oidc-provider/token.actions.githubusercontent.com
Condition:
StringLike:
# All GitHub Actions running from the guardian/gateway repository.
token.actions.githubusercontent.com:sub: !Sub repo:guardian/gateway:*
Policies:
- PolicyName: SendEmailSES
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Action: ses:SendEmail
Resource: '*'
AutoScalingGroup:
Type: AWS::AutoScaling::AutoScalingGroup
Properties:
Expand Down

0 comments on commit e696026

Please sign in to comment.