/
AWSRecreateSG_test.py
134 lines (115 loc) · 5.84 KB
/
AWSRecreateSG_test.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
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
import demistomock as demisto # noqa: F401
import pytest
@pytest.mark.parametrize(
"rule, first_rule_created",
[
({"IpProtocol": "-1", "IpRanges": [{"CidrIp": "10.0.0.0/16", "Description": "allow all traffic from VPC"},
{"CidrIp": "0.0.0.0/0"}], "Ipv6Ranges": [], "PrefixListIds": [], "UserIdGroupPairs": []},
{'IpProtocol': 'tcp', 'IpRanges': [{'CidrIp': '0.0.0.0/0'}], 'Ipv6Ranges': [],
'PrefixListIds': [], 'UserIdGroupPairs': [], 'FromPort': 0, 'ToPort': 21}),
({"IpProtocol": "tcp", "IpRanges": [{"CidrIp": "0.0.0.0/0"}], "Ipv6Ranges": [], "PrefixListIds": [],
"UserIdGroupPairs": [], 'FromPort': 0, 'ToPort': 23},
{'IpProtocol': 'tcp', 'IpRanges': [{'CidrIp': '0.0.0.0/0'}], 'Ipv6Ranges': [],
'PrefixListIds': [], 'UserIdGroupPairs': [], 'FromPort': 0, 'ToPort': 21}),
({"IpProtocol": "tcp", "IpRanges": [{"CidrIp": "0.0.0.0/0"}], "Ipv6Ranges": [], "PrefixListIds": [],
"UserIdGroupPairs": [], 'FromPort': 1, 'ToPort': 22},
{'IpProtocol': 'tcp', 'IpRanges': [{'CidrIp': '0.0.0.0/0'}], 'Ipv6Ranges': [],
'PrefixListIds': [], 'UserIdGroupPairs': [], 'FromPort': 1, 'ToPort': 21}),
({"IpProtocol": "tcp", "IpRanges": [{"CidrIp": "0.0.0.0/0"}], "Ipv6Ranges": [], "PrefixListIds": [],
"UserIdGroupPairs": [], 'FromPort': 22, 'ToPort': 100},
{'IpProtocol': 'tcp', 'IpRanges': [{'CidrIp': '0.0.0.0/0'}], 'Ipv6Ranges': [],
'PrefixListIds': [], 'UserIdGroupPairs': [], 'FromPort': 23, 'ToPort': 100}),
({"IpProtocol": "tcp", "IpRanges": [], "Ipv6Ranges": [{'CidrIpv6': '::/0'}], "PrefixListIds": [],
"UserIdGroupPairs": [], 'FromPort': 22, 'ToPort': 100},
{'IpProtocol': 'tcp', 'IpRanges': [], 'Ipv6Ranges': [{'CidrIpv6': '::/0'}], 'PrefixListIds': [],
'UserIdGroupPairs': [], 'FromPort': 23, 'ToPort': 100}),
]
)
def test_split_rule(rule, first_rule_created):
"""Tests split_rule helper function.
Given:
- Mocked arguments
When:
- Sending args to split_rule helper function.
Then:
- Checks the output of the helper function with the expected output.
"""
from AWSRecreateSG import split_rule
args = {"rule": rule, "port": 22, "protocol": "tcp"}
result = split_rule(**args)
assert result[0] == first_rule_created
def test_instance_info(mocker):
"""Tests instance_info helper function.
Given:
- Mocked arguments
When:
- Sending args to instance_info helper function.
Then:
- Checks the output of the helper function with the expected output.
"""
from AWSRecreateSG import instance_info
from test_data.sample import INSTANCE_INFO
mocker.patch.object(demisto, "executeCommand", return_value=INSTANCE_INFO)
args = {"instance_id": "fake-instance-id", "public_ip": "1.1.1.1", "assume_role": "test_role", "region": "us-east-1"}
result = instance_info(**args)
assert result == ({'eni-00000000000000000': ['sg-00000000000000000']}, 'AWS - EC2')
def test_sg_fix(mocker):
"""Tests sg_fix helper function.
Given:
- Mocked arguments
When:
- Sending args to sg_fix helper function.
Then:
- Checks the output of the helper function with the expected output.
"""
from AWSRecreateSG import sg_fix
from test_data.sample import SG_INFO, NEW_SG
mocker.patch.object(demisto, "executeCommand", return_value=NEW_SG)
args = {"sg_info": SG_INFO, "port": 22, "protocol": "tcp", "assume_role": "test_role", "instance_to_use": "AWS - EC2",
"region": "us-east-1"}
result = sg_fix(**args)
assert result == {'new-sg': 'sg-00000000000000001'}
def test_determine_excessive_access(mocker):
"""Tests determine_excessive_access helper function.
Given:
- Mocked arguments
When:
- Sending args to determine_excessive_access helper function.
Then:
- Checks the output of the helper function with the expected output.
"""
from AWSRecreateSG import determine_excessive_access
from test_data.sample import SG_INFO, NEW_SG
def executeCommand(name, *_):
return {
"aws-ec2-describe-security-groups": SG_INFO,
"aws-ec2-create-security-group": NEW_SG
}.get(name)
mocker.patch.object(demisto, "executeCommand", side_effect=executeCommand)
args = {"int_sg_mapping": {'eni-00000000000000000': ['sg-00000000000000000']}, "port": 22,
"protocol": "tcp", "assume_role": "test_role", "instance_to_use": "AWS - EC2", "region": "us-east-1"}
result = determine_excessive_access(**args)
assert result == [{'int': 'eni-00000000000000000', 'old-sg': 'sg-00000000000000000', 'new-sg': 'sg-00000000000000001'}]
def test_aws_recreate_sg(mocker):
"""Tests aws_recreate_sg function.
Given:
- Mocked arguments
When:
- Sending args to aws_recreate_sg function.
Then:
- Checks the output of the function with the expected output.
"""
from AWSRecreateSG import aws_recreate_sg
from test_data.sample import SG_INFO, INSTANCE_INFO, NEW_SG
def execute_command(command, *_):
return {
"aws-ec2-describe-security-groups": SG_INFO,
"aws-ec2-create-security-group": NEW_SG,
"aws-ec2-describe-instances": INSTANCE_INFO
}.get(command)
mocker.patch.object(demisto, "executeCommand", side_effect=execute_command)
args = {"instance_id": "fake-instance-id", "public_ip": "1.1.1.1", "port": "22", "protocol": "tcp"}
command_results = aws_recreate_sg(args)
readable_output = command_results.readable_output
correct_output = "For interface eni-00000000000000000: \r\nreplaced SG sg-00000000000000000 with sg-00000000000000001 \r\n"
assert readable_output == correct_output