1
|
|
|
# -*- coding: utf-8 -*- |
2
|
|
|
import json, unittest |
3
|
|
|
from mock import MagicMock |
4
|
|
|
from awslambdahelper import AWSConfigRule |
5
|
|
|
from awslambdahelper import CompliantEvaluation |
6
|
|
|
|
7
|
|
|
|
8
|
|
|
class TestConfigChangeRuleTests(unittest.TestCase): |
9
|
|
|
def setUp(self): |
10
|
|
|
self.parameters = [ |
11
|
|
|
( |
12
|
|
|
# AWS Lambda event payload. Request from AWS Config |
13
|
|
|
{ |
14
|
|
|
"invokingEvent": json.dumps({ |
15
|
|
|
"configurationItem": { |
16
|
|
|
"configurationItemCaptureTime": "2016-02-17T01:36:34.043Z", |
17
|
|
|
"awsAccountId": "123456789012", |
18
|
|
|
"configurationItemStatus": "OK", |
19
|
|
|
"resourceId": "i-00000000", |
20
|
|
|
"ARN": "arn:aws:ec2:us-east-1:123456789012:instance/i-00000000", |
21
|
|
|
"awsRegion": "us-east-1", |
22
|
|
|
"availabilityZone": "us-east-1a", |
23
|
|
|
"resourceType": "AWS::EC2::Instance", |
24
|
|
|
"tags": {"Foo": "Bar"}, |
25
|
|
|
"relationships": [{ |
26
|
|
|
"resourceId": "eipalloc-00000000", |
27
|
|
|
"resourceType": "AWS::EC2::EIP", |
28
|
|
|
"name": "Is attached to ElasticIp" |
29
|
|
|
}], |
30
|
|
|
"configuration": { |
31
|
|
|
"foo": "bar" |
32
|
|
|
} |
33
|
|
|
}, |
34
|
|
|
"messageType": "ConfigurationItemChangeNotification" |
35
|
|
|
}), |
36
|
|
|
"ruleParameters": json.dumps({ |
37
|
|
|
"myParameterKey": "myParameterValue" |
38
|
|
|
}), |
39
|
|
|
"resultToken": "myResultToken", |
40
|
|
|
"eventLeftScope": False, |
41
|
|
|
"executionRoleArn": "arn:aws:iam::123456789012:role/config-role", |
42
|
|
|
"configRuleArn": "arn:aws:config:us-east-1:123456789012:config-rule/config-rule-0123456", |
43
|
|
|
"configRuleName": "change-triggered-config-rule", |
44
|
|
|
"configRuleId": "config-rule-0123456", |
45
|
|
|
"accountId": "123456789012", |
46
|
|
|
"version": "1.0" |
47
|
|
|
}, |
48
|
|
|
# Response |
49
|
|
|
{ |
50
|
|
|
"Evaluations": [{ |
51
|
|
|
"OrderingTimestamp": "2016-02-17T01:36:34.043Z", |
52
|
|
|
"ComplianceResourceId": "i-00000000", |
53
|
|
|
"ComplianceResourceType": "AWS::EC2::Instance", |
54
|
|
|
"Annotation": "This resource is compliant with the rule.", |
55
|
|
|
"ComplianceType": "COMPLIANT" |
56
|
|
|
}], |
57
|
|
|
"ResultToken": "myResultToken" |
58
|
|
|
} |
59
|
|
|
), ( |
60
|
|
|
# AWS Lambda event payload. Request from AWS Config |
61
|
|
|
{ |
62
|
|
|
"invokingEvent": json.dumps({ |
63
|
|
|
"configurationItem": { |
64
|
|
|
"configurationItemCaptureTime": "2016-02-17T01:36:34.043Z", |
65
|
|
|
"awsAccountId": "123456789012", |
66
|
|
|
"configurationItemStatus": "OK", |
67
|
|
|
"resourceId": "sg-00000000", |
68
|
|
|
"ARN": "arn:aws:ec2:us-east-1:123456789012:security-group/sg-00000000", |
69
|
|
|
"awsRegion": "us-east-1", |
70
|
|
|
"availabilityZone": "us-east-1a", |
71
|
|
|
"resourceType": "AWS::EC2::SecurityGroup", |
72
|
|
|
"tags": {"Foo": "Bar"}, |
73
|
|
|
"configuration": { |
74
|
|
|
"foo": "bar" |
75
|
|
|
} |
76
|
|
|
}, |
77
|
|
|
"messageType": "ConfigurationItemChangeNotification" |
78
|
|
|
}), |
79
|
|
|
"ruleParameters": json.dumps({ |
80
|
|
|
"myParameterKey": "myParameterValue" |
81
|
|
|
}), |
82
|
|
|
"resultToken": "myResultToken", |
83
|
|
|
"eventLeftScope": False, |
84
|
|
|
"executionRoleArn": "arn:aws:iam::123456789012:role/config-role", |
85
|
|
|
"configRuleArn": "arn:aws:config:us-east-1:123456789012:config-rule/config-rule-0123456", |
86
|
|
|
"configRuleName": "change-triggered-config-rule", |
87
|
|
|
"configRuleId": "config-rule-0123456", |
88
|
|
|
"accountId": "123456789012", |
89
|
|
|
"version": "1.0" |
90
|
|
|
}, |
91
|
|
|
# Response |
92
|
|
|
{ |
93
|
|
|
"Evaluations": [{ |
94
|
|
|
"OrderingTimestamp": "2016-02-17T01:36:34.043Z", |
95
|
|
|
"ComplianceResourceId": "sg-00000000", |
96
|
|
|
"ComplianceResourceType": "AWS::EC2::SecurityGroup", |
97
|
|
|
"Annotation": "The rule doesn't apply to resources of type AWS::EC2::SecurityGroup.", |
98
|
|
|
"ComplianceType": "NOT_APPLICABLE" |
99
|
|
|
}], |
100
|
|
|
"ResultToken": "myResultToken" |
101
|
|
|
} |
102
|
|
|
) |
103
|
|
|
] |
104
|
|
|
|
105
|
|
|
def test_configchangeevent(self): |
106
|
|
|
class MockConfigRule(AWSConfigRule): |
107
|
|
|
APPLICABLE_RESOURCES = ["AWS::EC2::Instance"] |
108
|
|
|
|
109
|
|
|
def find_violation_config_change(self, config, rule_parameters): |
110
|
|
|
return [CompliantEvaluation()] |
111
|
|
|
|
112
|
|
|
mock_rule = MockConfigRule() |
113
|
|
|
|
114
|
|
|
for lambda_event, put_evaluations_response in self.parameters: |
115
|
|
|
mock_rule.put_evaluations = MagicMock() |
116
|
|
|
|
117
|
|
|
mock_rule.lambda_handler( |
118
|
|
|
event=lambda_event, |
119
|
|
|
context=None |
120
|
|
|
) |
121
|
|
|
|
122
|
|
|
mock_rule.put_evaluations.assert_called_once_with(**put_evaluations_response) |
123
|
|
|
|