forked from PaloAltoNetworks/Prisma-Enhanced-Remediation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAWS-CFM-003.py
80 lines (58 loc) · 1.83 KB
/
AWS-CFM-003.py
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
"""
Remediate Prisma Policy:
AWS:CFM-003 CFN Stack Termination Protection
Description:
You can prevent a stack from being accidentally deleted by enabling termination protection on the stack.
If a user attempts to delete a stack with termination protection enabled, the deletion fails and the stack
including its status remains unchanged.
Required Permissions:
- cloudformation:DescribeStacks
- cloudformation:UpdateTerminationProtection
Sample IAM Policy:
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "CloudFormationPermissions",
"Action": [
"cloudformation:DescribeStacks",
"cloudformation:UpdateTerminationProtection"
],
"Effect": "Allow",
"Resource": "*"
}
]
}
"""
import boto3
from botocore.exceptions import ClientError
def remediate(session, alert, lambda_context):
"""
Main Function invoked by index_prisma.py
"""
stack_id = alert['resource_id']
region = alert['region']
cfn = session.client('cloudformation', region_name=region)
try:
stack = cfn.describe_stacks(StackName=stack_id)['Stacks']
except ClientError as e:
print(e.response['Error']['Message'])
return
stack_name = stack[0]['StackName']
# Don't check stack with a status of..
if stack[0]['StackStatus'] in ("DELETE_COMPLETE", "DELETE_IN_PROGRESS", "DELETE_FAILED"):
return
if stack[0]['EnableTerminationProtection'] != True:
result = enable_term_protection(cfn, stack_name)
return
def enable_term_protection(cfn, stack_name):
"""
Enable CloudFormation Termination Protection
"""
try:
result = cfn.update_termination_protection(StackName=stack_name, EnableTerminationProtection=True)
except ClientError as e:
print(e.response['Error']['Message'])
else:
print('Enabled termination protection for CloudFormation Stack {}.'.format(stack_name))
return