-
Notifications
You must be signed in to change notification settings - Fork 4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into rds-secret-replication
- Loading branch information
Showing
38 changed files
with
3,063 additions
and
215 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
154 changes: 154 additions & 0 deletions
154
packages/@aws-cdk-containers/ecs-service-extensions/lib/extensions/injecter.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
import * as ecs from '@aws-cdk/aws-ecs'; | ||
import * as sns from '@aws-cdk/aws-sns'; | ||
import { Service } from '../service'; | ||
import { Container } from './container'; | ||
import { ContainerMutatingHook, ServiceExtension } from './extension-interfaces'; | ||
|
||
// Keep this import separate from other imports to reduce chance for merge conflicts with v2-main | ||
// eslint-disable-next-line no-duplicate-imports, import/order | ||
import { Construct } from '@aws-cdk/core'; | ||
|
||
/** | ||
* An interface that will be implemented by all the resources that can be published events or written data to. | ||
*/ | ||
export interface Injectable { | ||
environmentVariables(): { [key: string]: string }; | ||
} | ||
|
||
/** | ||
* An interface that will be implemented by all the injectable resources that need to grant permissions to the task role. | ||
*/ | ||
export interface GrantInjectable extends Injectable { | ||
grant(taskDefinition: ecs.TaskDefinition): void; | ||
} | ||
|
||
/** | ||
* The settings for the `InjectableTopic` class. | ||
*/ | ||
export interface InjectableTopicProps { | ||
/** | ||
* The SNS Topic to publish events to. | ||
*/ | ||
readonly topic: sns.ITopic; | ||
} | ||
|
||
/** | ||
* The `InjectableTopic` class represents SNS Topic resource that can be published events to by the parent service. | ||
*/ | ||
|
||
export class InjectableTopic implements GrantInjectable { | ||
public readonly topic: sns.ITopic; | ||
|
||
constructor(props: InjectableTopicProps) { | ||
this.topic = props.topic; | ||
} | ||
|
||
public grant(taskDefinition: ecs.TaskDefinition) { | ||
this.topic.grantPublish(taskDefinition.taskRole); | ||
} | ||
|
||
public environmentVariables(): { [key: string]: string } { | ||
let environment: { [key: string]: string } = {}; | ||
environment[`${this.topic.node.id.toUpperCase()}_TOPIC_ARN`] = this.topic.topicArn; | ||
return environment; | ||
} | ||
} | ||
|
||
/** | ||
* The settings for the Injecter extension. | ||
*/ | ||
export interface InjecterExtensionProps { | ||
/** | ||
* The list of injectable resources for this service. | ||
*/ | ||
readonly injectables: Injectable[]; | ||
} | ||
|
||
/** | ||
* Settings for the hook which mutates the application container | ||
* to add the injectable resource environment variables. | ||
*/ | ||
interface ContainerMutatingProps { | ||
/** | ||
* The resource environment variables to be added to the container environment. | ||
*/ | ||
readonly environment: { [key: string]: string }; | ||
} | ||
|
||
/** | ||
* This hook modifies the application container's environment to | ||
* add the injectable resource environment variables. | ||
*/ | ||
class InjecterExtensionMutatingHook extends ContainerMutatingHook { | ||
private environment: { [key: string]: string }; | ||
|
||
constructor(props: ContainerMutatingProps) { | ||
super(); | ||
this.environment = props.environment; | ||
} | ||
|
||
public mutateContainerDefinition(props: ecs.ContainerDefinitionOptions): ecs.ContainerDefinitionOptions { | ||
return { | ||
...props, | ||
|
||
environment: { ...(props.environment || {}), ...this.environment }, | ||
} as ecs.ContainerDefinitionOptions; | ||
} | ||
} | ||
|
||
/** | ||
* This extension accepts a list of `Injectable` resources that the parent service can publish events or write data to. | ||
* It sets up the corresponding permissions for the task role of the parent service. | ||
*/ | ||
export class InjecterExtension extends ServiceExtension { | ||
private props: InjecterExtensionProps; | ||
|
||
private environment: { [key: string]: string } = {}; | ||
|
||
constructor(props: InjecterExtensionProps) { | ||
super('injecter'); | ||
|
||
this.props = props; | ||
} | ||
|
||
// @ts-ignore - Ignore unused params that are required for abstract class extend | ||
public prehook(service: Service, scope: Construct) { | ||
this.parentService = service; | ||
|
||
for (const injectable of this.props.injectables) { | ||
for (const [key, val] of Object.entries(injectable.environmentVariables())) { | ||
this.environment[key] = val; | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Add hooks to the main application extension so that it is modified to | ||
* add the injectable resource environment variables to the container environment. | ||
*/ | ||
public addHooks() { | ||
const container = this.parentService.serviceDescription.get('service-container') as Container; | ||
|
||
if (!container) { | ||
throw new Error('Injecter Extension requires an application extension'); | ||
} | ||
|
||
container.addContainerMutatingHook(new InjecterExtensionMutatingHook({ | ||
environment: this.environment, | ||
})); | ||
} | ||
|
||
/** | ||
* After the task definition has been created, this hook grants the required permissions to the task role for the | ||
* parent service. | ||
* | ||
* @param taskDefinition The created task definition | ||
*/ | ||
public useTaskDefinition(taskDefinition: ecs.TaskDefinition) { | ||
for (const injectable of this.props.injectables) { | ||
if ((injectable as GrantInjectable).grant !== undefined) { | ||
(injectable as GrantInjectable).grant(taskDefinition); | ||
} | ||
} | ||
} | ||
} |
114 changes: 114 additions & 0 deletions
114
packages/@aws-cdk-containers/ecs-service-extensions/test/injecter.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
import { countResources, expect, haveResource } from '@aws-cdk/assert-internal'; | ||
import * as ecs from '@aws-cdk/aws-ecs'; | ||
import * as sns from '@aws-cdk/aws-sns'; | ||
import * as cdk from '@aws-cdk/core'; | ||
import { Container, Environment, InjecterExtension, InjectableTopic, Service, ServiceDescription } from '../lib'; | ||
|
||
describe('injecter', () => { | ||
test('correctly sets publish permissions for given topics', () => { | ||
// GIVEN | ||
const stack = new cdk.Stack(); | ||
|
||
const environment = new Environment(stack, 'production'); | ||
const serviceDescription = new ServiceDescription(); | ||
|
||
serviceDescription.add(new Container({ | ||
cpu: 256, | ||
memoryMiB: 512, | ||
trafficPort: 80, | ||
image: ecs.ContainerImage.fromRegistry('nathanpeck/name'), | ||
environment: { | ||
PORT: '80', | ||
}, | ||
})); | ||
|
||
// WHEN | ||
const topic1 = new InjectableTopic({ | ||
topic: new sns.Topic(stack, 'topic1'), | ||
}); | ||
|
||
const topic2 = new InjectableTopic({ | ||
topic: new sns.Topic(stack, 'topic2'), | ||
}); | ||
|
||
serviceDescription.add(new InjecterExtension({ | ||
injectables: [topic1, topic2], | ||
})); | ||
|
||
new Service(stack, 'my-service', { | ||
environment, | ||
serviceDescription, | ||
}); | ||
|
||
// THEN | ||
// Ensure creation of provided topics | ||
expect(stack).to(countResources('AWS::SNS::Topic', 2)); | ||
|
||
// Ensure the task role is given permissions to publish events to topics | ||
expect(stack).to(haveResource('AWS::IAM::Policy', { | ||
PolicyDocument: { | ||
Statement: [ | ||
{ | ||
Action: 'sns:Publish', | ||
Effect: 'Allow', | ||
Resource: { | ||
Ref: 'topic152D84A37', | ||
}, | ||
}, | ||
{ | ||
Action: 'sns:Publish', | ||
Effect: 'Allow', | ||
Resource: { | ||
Ref: 'topic2A4FB547F', | ||
}, | ||
}, | ||
], | ||
Version: '2012-10-17', | ||
}, | ||
})); | ||
|
||
// Ensure that the topic ARNs have been correctly appended to the environment variables | ||
expect(stack).to(haveResource('AWS::ECS::TaskDefinition', { | ||
ContainerDefinitions: [ | ||
{ | ||
Cpu: 256, | ||
Environment: [ | ||
{ | ||
Name: 'PORT', | ||
Value: '80', | ||
}, | ||
{ | ||
Name: 'TOPIC1_TOPIC_ARN', | ||
Value: { | ||
Ref: 'topic152D84A37', | ||
}, | ||
}, | ||
{ | ||
Name: 'TOPIC2_TOPIC_ARN', | ||
Value: { | ||
Ref: 'topic2A4FB547F', | ||
}, | ||
}, | ||
], | ||
Image: 'nathanpeck/name', | ||
Essential: true, | ||
Memory: 512, | ||
Name: 'app', | ||
PortMappings: [ | ||
{ | ||
ContainerPort: 80, | ||
Protocol: 'tcp', | ||
}, | ||
], | ||
Ulimits: [ | ||
{ | ||
HardLimit: 1024000, | ||
Name: 'nofile', | ||
SoftLimit: 1024000, | ||
}, | ||
], | ||
}, | ||
], | ||
})); | ||
}); | ||
}); |
Oops, something went wrong.