Skip to content

Commit 073958f

Browse files
authored
feat(scheduler-targets-alpha): InspectorStartAssessmentRun Target (#27850)
This PR adds InspectorStartAssessmentRun Target for EventBridge Scheduler. In [the issue](#27453), the `inspector.CfnAssessmentTarget` is used in the `InspectorStartAssessmentRun`. But it should be a `CfnAssessmentTemplate` so I fixed. ```ts export class InspectorStartAssessmentRun extends ScheduleTargetBase implements IScheduleTarget { constructor( private readonly target: inspector.CfnAssessmentTarget, // <- here private readonly props: ScheduleTargetBaseProps, ) { ``` Closes #27453. ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 78b34ac commit 073958f

14 files changed

+35748
-2
lines changed

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

+19
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ The following targets are supported:
3030
4. `targets.SqsSendMessage`: [Send a Message to an Amazon SQS Queue](#send-a-message-to-sqs-queue)
3131
5. `targets.SnsPublish`: [Publish messages to an Amazon SNS topic](#publish-messages-to-an-amazon-sns-topic)
3232
6. `targets.EventBridgePutEvents`: [Put Events on EventBridge](#send-events-to-an-eventbridge-event-bus)
33+
7. `targets.InspectorStartAssessmentRun`: [Start an Amazon Inspector assessment run](#start-an-amazon-inspector-assessment-run)
3334

3435
## Invoke a Lambda function
3536

@@ -206,3 +207,21 @@ new Schedule(this, 'Schedule', {
206207
target: new targets.EventBridgePutEvents(eventEntry, {}),
207208
});
208209
```
210+
211+
## Start an Amazon Inspector assessment run
212+
213+
Use the `InspectorStartAssessmentRun` target to start an Inspector assessment run.
214+
215+
The code snippet below creates an event rule with an assessment template as target which is
216+
called every hour by Event Bridge Scheduler.
217+
218+
```ts
219+
import * as inspector from 'aws-cdk-lib/aws-inspector';
220+
221+
declare const assessmentTemplate: inspector.CfnAssessmentTemplate;
222+
223+
new Schedule(this, 'Schedule', {
224+
schedule: ScheduleExpression.rate(Duration.minutes(60)),
225+
target: new targets.InspectorStartAssessmentRun(assessmentTemplate),
226+
});
227+
```
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
export * from './codebuild-start-build';
22
export * from './event-bridge-put-events';
3-
export * from './target';
3+
export * from './inspector-start-assessment-run';
44
export * from './lambda-invoke';
55
export * from './sns-publish';
6+
export * from './sqs-send-message';
67
export * from './stepfunctions-start-execution';
7-
export * from './sqs-send-message';
8+
export * from './target';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { ISchedule, IScheduleTarget } from '@aws-cdk/aws-scheduler-alpha';
2+
import { Names } from 'aws-cdk-lib';
3+
import { IRole, PolicyStatement } from 'aws-cdk-lib/aws-iam';
4+
import { CfnAssessmentTemplate } from 'aws-cdk-lib/aws-inspector';
5+
import { ScheduleTargetBase, ScheduleTargetBaseProps } from './target';
6+
import { sameEnvDimension } from './util';
7+
8+
/**
9+
* Use an Amazon Inspector as a target for AWS EventBridge Scheduler.
10+
*/
11+
export class InspectorStartAssessmentRun extends ScheduleTargetBase implements IScheduleTarget {
12+
constructor(
13+
private readonly template: CfnAssessmentTemplate,
14+
private readonly props: ScheduleTargetBaseProps = {},
15+
) {
16+
super(props, template.attrArn);
17+
}
18+
19+
protected addTargetActionToRole(schedule: ISchedule, role: IRole): void {
20+
if (!sameEnvDimension(this.template.stack.region, schedule.env.region)) {
21+
throw new Error(`Cannot assign assessment template in region ${this.template.stack.region} to the schedule ${Names.nodeUniqueId(schedule.node)} in region ${schedule.env.region}. Both the schedule and the assessment template must be in the same region.`);
22+
}
23+
24+
if (!sameEnvDimension(this.template.stack.account, schedule.env.account)) {
25+
throw new Error(`Cannot assign assessment template in account ${this.template.stack.account} to the schedule ${Names.nodeUniqueId(schedule.node)} in account ${schedule.env.region}. Both the schedule and the assessment template must be in the same account.`);
26+
}
27+
28+
if (this.props.role && !sameEnvDimension(this.props.role.env.account, this.template.stack.account)) {
29+
throw new Error(`Cannot grant permission to execution role in account ${this.props.role.env.account} to invoke target ${Names.nodeUniqueId(this.template.node)} in account ${this.template.stack.account}. Both the target and the execution role must be in the same account.`);
30+
}
31+
32+
role.addToPrincipalPolicy(new PolicyStatement({
33+
actions: ['inspector:StartAssessmentRun'],
34+
resources: ['*'],
35+
}));
36+
}
37+
}

0 commit comments

Comments
 (0)