Skip to content

Commit a391468

Browse files
authored
fix(aws-logs): include new policy.ts exports in index.ts exports (#17403)
## Summary This PR modifies the aws-logs `index.ts` file to also forward the exports from `policy.ts` ([a newly created file](#17015) that implements the `ResourcePolicy` class). Fixes: #17402 ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 6937296 commit a391468

File tree

3 files changed

+61
-4
lines changed

3 files changed

+61
-4
lines changed

packages/@aws-cdk/aws-logs/lib/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export * from './metric-filter';
55
export * from './pattern';
66
export * from './subscription-filter';
77
export * from './log-retention';
8+
export * from './policy';
89

910
// AWS::Logs CloudFormation Resources:
1011
export * from './logs.generated';

packages/@aws-cdk/aws-logs/lib/policy.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export interface ResourcePolicyProps {
1111
* Name of the log group resource policy
1212
* @default - Uses a unique id based on the construct path
1313
*/
14-
readonly policyName?: string;
14+
readonly resourcePolicyName?: string;
1515

1616
/**
1717
* Initial statements to add to the resource policy
@@ -31,15 +31,19 @@ export class ResourcePolicy extends Resource {
3131
public readonly document = new PolicyDocument();
3232

3333
constructor(scope: Construct, id: string, props?: ResourcePolicyProps) {
34-
super(scope, id);
35-
new CfnResourcePolicy(this, 'Resource', {
34+
super(scope, id, {
35+
physicalName: props?.resourcePolicyName,
36+
});
37+
38+
new CfnResourcePolicy(this, 'ResourcePolicy', {
3639
policyName: Lazy.string({
37-
produce: () => props?.policyName ?? Names.uniqueId(this),
40+
produce: () => props?.resourcePolicyName ?? Names.uniqueId(this),
3841
}),
3942
policyDocument: Lazy.string({
4043
produce: () => JSON.stringify(this.document),
4144
}),
4245
});
46+
4347
if (props?.policyStatements) {
4448
this.document.addStatements(...props.policyStatements);
4549
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import '@aws-cdk/assert-internal/jest';
2+
import { PolicyStatement, ServicePrincipal } from '@aws-cdk/aws-iam';
3+
import { Stack } from '@aws-cdk/core';
4+
import { LogGroup, ResourcePolicy } from '../lib';
5+
6+
describe('resource policy', () => {
7+
test('ResourcePolicy is added to stack, when .addToResourcePolicy() is provided a valid Statement', () => {
8+
// GIVEN
9+
const stack = new Stack();
10+
const logGroup = new LogGroup(stack, 'LogGroup');
11+
12+
// WHEN
13+
logGroup.addToResourcePolicy(new PolicyStatement({
14+
actions: ['logs:CreateLogStream'],
15+
resources: ['*'],
16+
}));
17+
18+
// THEN
19+
expect(stack).toHaveResource('AWS::Logs::ResourcePolicy', {
20+
PolicyName: 'LogGroupPolicy643B329C',
21+
PolicyDocument: JSON.stringify({
22+
Statement: [
23+
{
24+
Action: 'logs:CreateLogStream',
25+
Effect: 'Allow',
26+
Resource: '*',
27+
},
28+
],
29+
Version: '2012-10-17',
30+
}),
31+
});
32+
});
33+
34+
test('ResourcePolicy is added to stack, when created manually/directly', () => {
35+
// GIVEN
36+
const stack = new Stack();
37+
const logGroup = new LogGroup(stack, 'LogGroup');
38+
39+
// WHEN
40+
const resourcePolicy = new ResourcePolicy(stack, 'ResourcePolicy');
41+
resourcePolicy.document.addStatements(new PolicyStatement({
42+
actions: ['logs:CreateLogStream', 'logs:PutLogEvents'],
43+
principals: [new ServicePrincipal('es.amazonaws.com')],
44+
resources: [logGroup.logGroupArn],
45+
}));
46+
47+
// THEN
48+
expect(stack).toHaveResource('AWS::Logs::ResourcePolicy', {
49+
PolicyName: 'ResourcePolicy',
50+
});
51+
});
52+
});

0 commit comments

Comments
 (0)