Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(sns): enable passing PolicyDocument to TopicPolicy #10559

Merged
merged 13 commits into from
Mar 17, 2021
Merged
8 changes: 8 additions & 0 deletions packages/@aws-cdk/aws-sns/lib/policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ export interface TopicPolicyProps {
* The set of topics this policy applies to.
*/
readonly topics: ITopic[];
/**
* IAM policy document to apply to topic(s).
* @default empty policy document
*/
readonly policyDocument?: PolicyDocument;

}

/**
Expand All @@ -32,6 +38,8 @@ export class TopicPolicy extends Resource {
constructor(scope: Construct, id: string, props: TopicPolicyProps) {
super(scope, id);

this.document = props.policyDocument ?? this.document;

new CfnTopicPolicy(this, 'Resource', {
policyDocument: this.document,
topics: props.topics.map(t => t.topicArn),
Expand Down
91 changes: 91 additions & 0 deletions packages/@aws-cdk/aws-sns/test/test.sns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,97 @@ export = {
test.done();
},

'TopicPolicy passed document'(test: Test) {
// GIVEN
const stack = new cdk.Stack();
const topic = new sns.Topic(stack, 'MyTopic');
const ps = new iam.PolicyStatement({
actions: ['service:statement0'],
principals: [new iam.ArnPrincipal('arn')],
});

// WHEN
new sns.TopicPolicy(stack, 'topicpolicy', { topics: [topic], policyDocument: new iam.PolicyDocument({ assignSids: true, statements: [ps] }) });

// THEN
expect(stack).toMatch({
'Resources': {
'MyTopic86869434': {
'Type': 'AWS::SNS::Topic',
},
'topicpolicyF8CF12FD': {
'Type': 'AWS::SNS::TopicPolicy',
'Properties': {
'PolicyDocument': {
'Statement': [
{
'Action': 'service:statement0',
'Effect': 'Allow',
'Principal': { 'AWS': 'arn' },
'Sid': '0',
},
],
'Version': '2012-10-17',
},
'Topics': [
{
'Ref': 'MyTopic86869434',
},
],
},
},
},
});

test.done();
},

'Add statements to policy'(test: Test) {
// GIVEN
const stack = new cdk.Stack();
const topic = new sns.Topic(stack, 'MyTopic');

// WHEN
const topicPolicy = new sns.TopicPolicy(stack, 'TopicPolicy', {
topics: [topic],
});
topicPolicy.document.addStatements(new iam.PolicyStatement({
actions: ['service:statement0'],
principals: [new iam.ArnPrincipal('arn')],
}));

// THEN
expect(stack).toMatch({
'Resources': {
'MyTopic86869434': {
'Type': 'AWS::SNS::Topic',
},
'TopicPolicyA24B096F': {
'Type': 'AWS::SNS::TopicPolicy',
'Properties': {
'PolicyDocument': {
'Statement': [
{
'Action': 'service:statement0',
'Effect': 'Allow',
'Principal': { 'AWS': 'arn' },
'Sid': '0',
},
],
'Version': '2012-10-17',
},
'Topics': [
{
'Ref': 'MyTopic86869434',
},
],
},
},
},
});
test.done();
},

'topic resource policy includes unique SIDs'(test: Test) {
const stack = new cdk.Stack();

Expand Down