Skip to content

Commit fa2327d

Browse files
authored
chore(scheduler-alpha-targets): raise awareness for default policy risk (#33003)
### Issue # (if applicable) N/A. ### Reason for this change Raise awareness on the `*` used for resources in the default policy in the `Universal` target class. ### Description of changes README updates and added a new warning. ### Describe any new or updated permissions being added None ### Description of how you validated changes Unit tests. ### Checklist - [x] My code adheres to the [CONTRIBUTING GUIDE](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) and [DESIGN GUIDELINES](https://github.com/aws/aws-cdk/blob/main/docs/DESIGN_GUIDELINES.md) ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent d4845ce commit fa2327d

File tree

3 files changed

+16
-6
lines changed

3 files changed

+16
-6
lines changed

packages/@aws-cdk/aws-scheduler-targets-alpha/README.md

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ new Schedule(this, 'Schedule', {
316316

317317
## Invoke a wider set of AWS API
318318

319-
Use the `Universal` target to invoke AWS API.
319+
Use the `Universal` target to invoke AWS API. See https://docs.aws.amazon.com/scheduler/latest/UserGuide/managing-targets-universal.html
320320

321321
The code snippet below creates an event rule with AWS API as the target which is
322322
called at midnight every day by EventBridge Scheduler.
@@ -339,9 +339,9 @@ new Schedule(this, 'Schedule', {
339339

340340
The `service` must be in lowercase and the `action` must be in camelCase.
341341

342-
By default, an IAM policy for the Scheduler is extracted from the API call.
343-
344-
You can control the IAM policy for the Scheduler by specifying the `policyStatements` property.
342+
By default, an IAM policy for the Scheduler is extracted from the API call. The action in the policy is constructed using the `service` and `action` prop.
343+
Re-using the example above, the action will be `rds:stopDBCluster`. Note that not all IAM actions follow the same pattern. In such scenario, please use the
344+
`policyStatements` prop to override the policy:
345345

346346
```ts
347347
new Schedule(this, 'Schedule', {
@@ -362,3 +362,6 @@ new Schedule(this, 'Schedule', {
362362
}),
363363
});
364364
```
365+
366+
> Note: The default policy uses `*` in the resources field as CDK does not have a straight forward way to auto-discover the resources permission required.
367+
> It is recommended that you scope the field down to specific resources to have a better security posture.

packages/@aws-cdk/aws-scheduler-targets-alpha/lib/universal.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { IScheduleTarget } from '@aws-cdk/aws-scheduler-alpha';
2-
import { Aws, Token } from 'aws-cdk-lib';
2+
import { Annotations, Aws, Token } from 'aws-cdk-lib';
33
import { IRole, PolicyStatement } from 'aws-cdk-lib/aws-iam';
44
import { awsSdkToIamAction } from 'aws-cdk-lib/custom-resources/lib/helpers-internal';
55
import { ScheduleTargetBase, ScheduleTargetBaseProps } from './target';
@@ -95,6 +95,8 @@ export class Universal extends ScheduleTargetBase implements IScheduleTarget {
9595

9696
protected addTargetActionToRole(role: IRole): void {
9797
if (!this.props.policyStatements?.length) {
98+
Annotations.of(role).addWarningV2('@aws-cdk/aws-scheduler-alpha:defaultWildcardResourcePolicy',
99+
'Default policy with * for resources is used. Use custom policy for better security posture.');
98100
role.addToPrincipalPolicy(new PolicyStatement({
99101
actions: [awsSdkToIamAction(this.props.service, this.props.action)],
100102
resources: ['*'],

packages/@aws-cdk/aws-scheduler-targets-alpha/test/universal.test.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as scheduler from '@aws-cdk/aws-scheduler-alpha';
22
import { Group } from '@aws-cdk/aws-scheduler-alpha';
33
import { App, Duration, Stack } from 'aws-cdk-lib';
4-
import { Template } from 'aws-cdk-lib/assertions';
4+
import { Annotations, Template } from 'aws-cdk-lib/assertions';
55
import * as iam from 'aws-cdk-lib/aws-iam';
66
import * as sqs from 'aws-cdk-lib/aws-sqs';
77
import { Universal } from '../lib/universal';
@@ -105,6 +105,11 @@ describe('Universal schedule target', () => {
105105
],
106106
},
107107
});
108+
109+
Annotations.fromStack(stack).hasWarning(
110+
'*',
111+
'Default policy with * for resources is used. Use custom policy for better security posture. [ack: @aws-cdk/aws-scheduler-alpha:defaultWildcardResourcePolicy]',
112+
);
108113
});
109114

110115
test('creates IAM policy for provided IAM role', () => {

0 commit comments

Comments
 (0)