-
Notifications
You must be signed in to change notification settings - Fork 0
/
sfn-prowler-task.ts
190 lines (185 loc) · 6.36 KB
/
sfn-prowler-task.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
import * as cdk from "aws-cdk-lib"
import * as constructs from "constructs"
import * as ecs from "aws-cdk-lib/aws-ecs"
import * as iam from "aws-cdk-lib/aws-iam"
import * as sfn from "aws-cdk-lib/aws-stepfunctions"
import * as tasks from "aws-cdk-lib/aws-stepfunctions-tasks"
import * as cr from "aws-cdk-lib/custom-resources"
type Props = {
/**
* The ECS cluster to run the task in.
*/
cluster: ecs.ICluster
/**
* An optional set of task parameter overrides.
*/
taskOverrides?: Partial<tasks.EcsRunTaskProps>
/**
* The regions that Prowler should scan
*
* @remarks
* Security Hub needs to be enabled in each of these regions.
*
* You likely also want to configure one of the regions as
* an aggregation region to have a single pane of glass
* for all findings from Prowler.
*
* @default - Prowler scans the current AWS region
*/
regions?: [string, ...string[]]
/**
* Whether Prowler should return a non-zero exit code
* if any of its findings fails a check (a "rule")
*
* @default false
*/
exitCode?: boolean
}
/**
* Configures a Step Functions task that can be used in a
* state machine to run the open-source security tool Prowler
* as a Fargate task and send the results to AWS Security Hub
* in one or multiple regions.
*/
export class SfnProwlerTask extends constructs.Construct {
public readonly sfnTask: tasks.EcsRunTask
constructor(scope: constructs.Construct, id: string, props: Props) {
super(scope, id)
const account = cdk.Stack.of(this).account
const currentRegion = cdk.Stack.of(this).region
const regions = props.regions || [currentRegion]
regions.forEach((region) => {
if (cdk.Token.isUnresolved(region)) {
throw new Error("Prowler regions can not contain unresolved CDK tokens")
}
new cr.AwsCustomResource(this, `SecurityHubProwlerIntegration${region}`, {
policy: cr.AwsCustomResourcePolicy.fromSdkCalls({
resources: [`arn:aws:securityhub:${region}:${account}:hub/default`],
}),
onCreate: {
region,
service: "SecurityHub",
action: "enableImportFindingsForProduct",
// Continue if the integration already has been enabled
ignoreErrorCodesMatching: "ResourceConflictException",
parameters: {
ProductArn: `arn:aws:securityhub:${region}::product/prowler/prowler`,
},
physicalResourceId: cr.PhysicalResourceId.of(region),
},
})
})
const taskDefinition = new ecs.TaskDefinition(this, "TaskDefinition", {
compatibility: ecs.Compatibility.FARGATE,
cpu: "1024",
memoryMiB: "2048",
})
taskDefinition.addContainer("main", {
logging: new ecs.AwsLogDriver({
streamPrefix: "prefix",
logRetention: 14,
}),
image: ecs.ContainerImage.fromRegistry(
// renovate: datasource=docker depName=toniblyx/prowler
"toniblyx/prowler:4.5.3@sha256:9832b103daf43c40fa843f3a8c6a731f793a750c3cd7585fc35a101bcd3b12f9",
),
command: [
"aws",
...((props.exitCode ?? false) ? [] : ["-z"]),
"--output-modes",
"json-asff",
"--region",
...regions,
// Enable security hub
"--security-hub",
// Only send failed checks
"--status",
"FAIL",
],
})
taskDefinition.taskRole.addManagedPolicy(
iam.ManagedPolicy.fromAwsManagedPolicyName("SecurityAudit"),
)
taskDefinition.taskRole.addManagedPolicy(
iam.ManagedPolicy.fromAwsManagedPolicyName("job-function/ViewOnlyAccess"),
)
taskDefinition.taskRole.attachInlinePolicy(
new iam.Policy(this, "ProwlerAdditionalPolicy", {
statements: [
new iam.PolicyStatement({
effect: iam.Effect.ALLOW,
actions: ["securityhub:BatchImportFindings"],
resources: regions.map(
(region) =>
`arn:aws:securityhub:${region}::product/prowler/prowler`,
),
}),
new iam.PolicyStatement({
effect: iam.Effect.ALLOW,
actions: ["securityhub:GetFindings"],
resources: ["*"],
}),
// Based on https://github.com/prowler-cloud/prowler/blob/master/permissions/prowler-additions-policy.json
new iam.PolicyStatement({
effect: iam.Effect.ALLOW,
actions: [
"account:Get*",
"appstream:Describe*",
"appstream:List*",
"backup:List*",
"cloudtrail:GetInsightSelectors",
"codeartifact:List*",
"codebuild:BatchGet*",
"cognito-idp:GetUserPoolMfaConfig",
"dlm:Get*",
"drs:Describe*",
"ds:Describe*",
"ds:Get*",
"ds:List*",
"dynamodb:GetResourcePolicy",
"ec2:GetEbsEncryptionByDefault",
"ec2:GetInstanceMetadataDefaults",
"ec2:GetSnapshotBlockPublicAccessState",
"ecr:Describe*",
"ecr:GetRegistryScanningConfiguration",
"elasticfilesystem:DescribeBackupPolicy",
"glue:GetConnections",
"glue:GetSecurityConfiguration*",
"glue:SearchTables",
"lambda:GetFunction*",
"lightsail:GetRelationalDatabases",
"logs:FilterLogEvents",
"macie2:GetMacieSession",
"s3:GetAccountPublicAccessBlock",
"shield:DescribeProtection",
"shield:GetSubscriptionState",
"ssm-incidents:List*",
"ssm:GetDocument",
"support:Describe*",
"tag:GetTagKeys",
"wellarchitected:List*",
],
resources: ["*"],
}),
new iam.PolicyStatement({
effect: iam.Effect.ALLOW,
actions: ["apigateway:GET"],
resources: [
"arn:aws:apigateway:*::/restapis/*",
"arn:aws:apigateway:*::/apis/*",
],
}),
],
}),
)
this.sfnTask = new tasks.EcsRunTask(this, "FargateTask", {
stateName: "ProwlerScan",
integrationPattern: sfn.IntegrationPattern.RUN_JOB,
taskDefinition,
cluster: props.cluster,
launchTarget: new tasks.EcsFargateLaunchTarget(),
assignPublicIp: true,
...(props.taskOverrides || {}),
})
}
}