-
Notifications
You must be signed in to change notification settings - Fork 3.7k
/
rule.ts
384 lines (331 loc) · 9.39 KB
/
rule.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
import * as events from '@aws-cdk/aws-events';
import * as iam from '@aws-cdk/aws-iam';
import * as lambda from '@aws-cdk/aws-lambda';
import { Construct, IResource, Lazy, Resource } from '@aws-cdk/core';
import { CfnConfigRule } from './config.generated';
/**
* A config rule.
*/
export interface IRule extends IResource {
/**
* The name of the rule.
*
* @attribute
*/
readonly configRuleName: string;
/**
* Defines a CloudWatch event rule which triggers for rule events. Use
* `rule.addEventPattern(pattern)` to specify a filter.
*/
onEvent(id: string, options?: events.OnEventOptions): events.Rule;
/**
* Defines a CloudWatch event rule which triggers for rule compliance events.
*/
onComplianceChange(id: string, options?: events.OnEventOptions): events.Rule;
/**
* Defines a CloudWatch event rule which triggers for rule re-evaluation status events.
*/
onReEvaluationStatus(id: string, options?: events.OnEventOptions): events.Rule;
}
/**
* A new or imported rule.
*/
abstract class RuleBase extends Resource implements IRule {
public abstract readonly configRuleName: string;
/**
* Defines a CloudWatch event rule which triggers for rule events. Use
* `rule.addEventPattern(pattern)` to specify a filter.
*/
public onEvent(id: string, options: events.OnEventOptions = {}) {
const rule = new events.Rule(this, id, options);
rule.addEventPattern({
source: ['aws.config'],
detail: {
configRuleName: [this.configRuleName],
},
});
rule.addTarget(options.target);
return rule;
}
/**
* Defines a CloudWatch event rule which triggers for rule compliance events.
*/
public onComplianceChange(id: string, options: events.OnEventOptions = {}): events.Rule {
const rule = this.onEvent(id, options);
rule.addEventPattern({
detailType: [ 'Config Rules Compliance Change' ],
});
return rule;
}
/**
* Defines a CloudWatch event rule which triggers for rule re-evaluation status events.
*/
public onReEvaluationStatus(id: string, options: events.OnEventOptions = {}): events.Rule {
const rule = this.onEvent(id, options);
rule.addEventPattern({
detailType: [ 'Config Rules Re-evaluation Status' ],
});
return rule;
}
}
/**
* A new managed or custom rule.
*/
abstract class RuleNew extends RuleBase {
/**
* Imports an existing rule.
*
* @param configRuleName the name of the rule
*/
public static fromConfigRuleName(scope: Construct, id: string, configRuleName: string): IRule {
class Import extends RuleBase {
public readonly configRuleName = configRuleName;
}
return new Import(scope, id);
}
/**
* The arn of the rule.
*/
public abstract readonly configRuleArn: string;
/**
* The id of the rule.
*/
public abstract readonly configRuleId: string;
/**
* The compliance status of the rule.
*/
public abstract readonly configRuleComplianceType: string;
protected scope?: CfnConfigRule.ScopeProperty;
protected isManaged?: boolean;
protected isCustomWithChanges?: boolean;
/**
* Restrict scope of changes to a specific resource.
*
* @see https://docs.aws.amazon.com/config/latest/developerguide/resource-config-reference.html#supported-resources
*
* @param type the resource type
* @param identifier the resource identifier
*/
public scopeToResource(type: string, identifier?: string) {
this.scope = {
complianceResourceId: identifier,
complianceResourceTypes: [type],
};
}
/**
* Restrict scope of changes to specific resource types.
*
* @see https://docs.aws.amazon.com/config/latest/developerguide/resource-config-reference.html#supported-resources
*
* @param types resource types
*/
public scopeToResources(...types: string[]) {
this.scope = {
complianceResourceTypes: types,
};
}
/**
* Restrict scope of changes to a specific tag.
*
* @param key the tag key
* @param value the tag value
*/
public scopeToTag(key: string, value?: string) {
this.scope = {
tagKey: key,
tagValue: value,
};
}
}
/**
* The maximum frequency at which the AWS Config rule runs evaluations.
*/
export enum MaximumExecutionFrequency {
/**
* 1 hour.
*/
ONE_HOUR = 'One_Hour',
/**
* 3 hours.
*/
THREE_HOURS = 'Three_Hours',
/**
* 6 hours.
*/
SIX_HOURS = 'Six_Hours',
/**
* 12 hours.
*/
TWELVE_HOURS = 'Twelve_Hours',
/**
* 24 hours.
*/
TWENTY_FOUR_HOURS = 'TwentyFour_Hours'
}
/**
* Construction properties for a new rule.
*/
export interface RuleProps {
/**
* A name for the AWS Config rule.
*
* @default a CloudFormation generated name
*/
readonly configRuleName?: string;
/**
* A description about this AWS Config rule.
*
* @default no description
*/
readonly description?: string;
/**
* Input parameter values that are passed to the AWS Config rule.
*
* @default no input parameters
*/
readonly inputParameters?: { [key: string]: any };
/**
* The maximum frequency at which the AWS Config rule runs evaluations.
*
* @default 24 hours
*/
readonly maximumExecutionFrequency?: MaximumExecutionFrequency
}
/**
* Construction properties for a ManagedRule.
*/
export interface ManagedRuleProps extends RuleProps {
/**
* The identifier of the AWS managed rule.
*
* @see https://docs.aws.amazon.com/config/latest/developerguide/managed-rules-by-aws-config.html
*/
readonly identifier: string;
}
/**
* A new managed rule.
*
* @resource AWS::Config::ConfigRule
*/
export class ManagedRule extends RuleNew {
/** @attribute */
public readonly configRuleName: string;
/** @attribute */
public readonly configRuleArn: string;
/** @attribute */
public readonly configRuleId: string;
/** @attribute */
public readonly configRuleComplianceType: string;
constructor(scope: Construct, id: string, props: ManagedRuleProps) {
super(scope, id, {
physicalName: props.configRuleName,
});
const rule = new CfnConfigRule(this, 'Resource', {
configRuleName: this.physicalName,
description: props.description,
inputParameters: props.inputParameters,
maximumExecutionFrequency: props.maximumExecutionFrequency,
scope: Lazy.anyValue({ produce: () => this.scope }),
source: {
owner: 'AWS',
sourceIdentifier: props.identifier,
},
});
this.configRuleName = rule.ref;
this.configRuleArn = rule.attrArn;
this.configRuleId = rule.attrConfigRuleId;
this.configRuleComplianceType = rule.attrComplianceType;
this.isManaged = true;
}
}
/**
* Consruction properties for a CustomRule.
*/
export interface CustomRuleProps extends RuleProps {
/**
* The Lambda function to run.
*/
readonly lambdaFunction: lambda.IFunction;
/**
* Whether to run the rule on configuration changes.
*
* @default false
*/
readonly configurationChanges?: boolean;
/**
* Whether to run the rule on a fixed frequency.
*
* @default false
*/
readonly periodic?: boolean;
}
/**
* A new custom rule.
*
* @resource AWS::Config::ConfigRule
*/
export class CustomRule extends RuleNew {
/** @attribute */
public readonly configRuleName: string;
/** @attribute */
public readonly configRuleArn: string;
/** @attribute */
public readonly configRuleId: string;
/** @attribute */
public readonly configRuleComplianceType: string;
constructor(scope: Construct, id: string, props: CustomRuleProps) {
super(scope, id, {
physicalName: props.configRuleName,
});
if (!props.configurationChanges && !props.periodic) {
throw new Error('At least one of `configurationChanges` or `periodic` must be set to true.');
}
const sourceDetails: any[] = [];
if (props.configurationChanges) {
sourceDetails.push({
eventSource: 'aws.config',
messageType: 'ConfigurationItemChangeNotification',
});
sourceDetails.push({
eventSource: 'aws.config',
messageType: 'OversizedConfigurationItemChangeNotification',
});
}
if (props.periodic) {
sourceDetails.push({
eventSource: 'aws.config',
maximumExecutionFrequency: props.maximumExecutionFrequency,
messageType: 'ScheduledNotification',
});
}
props.lambdaFunction.addPermission('Permission', {
principal: new iam.ServicePrincipal('config.amazonaws.com'),
});
if (props.lambdaFunction.role) {
props.lambdaFunction.role.addManagedPolicy(
iam.ManagedPolicy.fromAwsManagedPolicyName('service-role/AWSConfigRulesExecutionRole'),
);
}
// The lambda permission must be created before the rule
this.node.addDependency(props.lambdaFunction);
const rule = new CfnConfigRule(this, 'Resource', {
configRuleName: this.physicalName,
description: props.description,
inputParameters: props.inputParameters,
maximumExecutionFrequency: props.maximumExecutionFrequency,
scope: Lazy.anyValue({ produce: () => this.scope }),
source: {
owner: 'CUSTOM_LAMBDA',
sourceDetails,
sourceIdentifier: props.lambdaFunction.functionArn,
},
});
this.configRuleName = rule.ref;
this.configRuleArn = rule.attrArn;
this.configRuleId = rule.attrConfigRuleId;
this.configRuleComplianceType = rule.attrComplianceType;
if (props.configurationChanges) {
this.isCustomWithChanges = true;
}
}
}