Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aws
GitHub Repository: aws/aws-cli
Path: blob/develop/tests/functional/ec2/test_security_group_operations.py
1567 views
1
#!/usr/bin/env python
2
# Copyright 2013-2014 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3
#
4
# Licensed under the Apache License, Version 2.0 (the "License"). You
5
# may not use this file except in compliance with the License. A copy of
6
# the License is located at
7
#
8
# http://aws.amazon.com/apache2.0/
9
#
10
# or in the "license" file accompanying this file. This file is
11
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
12
# ANY KIND, either express or implied. See the License for the specific
13
# language governing permissions and limitations under the License.
14
from awscli.testutils import BaseAWSCommandParamsTest
15
16
17
class TestAuthorizeSecurityGroupIngress(BaseAWSCommandParamsTest):
18
19
prefix = 'ec2 authorize-security-group-ingress '
20
21
def test_simple_cidr(self):
22
args = self.prefix + (
23
'--group-name foobar --protocol tcp --port 22-25 --cidr 0.0.0.0/0')
24
result = {'GroupName': 'foobar',
25
'IpPermissions': [{'FromPort': 22, 'IpProtocol': 'tcp',
26
'IpRanges': [{'CidrIp': '0.0.0.0/0'}],
27
'ToPort': 25}]}
28
self.assert_params_for_cmd(args, result)
29
30
def test_all_port(self):
31
args = self.prefix + (
32
'--group-name foobar --protocol tcp --port all --cidr 0.0.0.0/0')
33
result = {'GroupName': 'foobar',
34
'IpPermissions': [{'FromPort': -1, 'IpProtocol': 'tcp',
35
'IpRanges': [{'CidrIp': '0.0.0.0/0'}],
36
'ToPort': -1}]}
37
self.assert_params_for_cmd(args, result)
38
39
def test_icmp_echo_request(self):
40
# This corresponds to a from port of 8 and a to port of -1, i.e
41
# --port 8--1.
42
args = self.prefix + (
43
'--group-name foobar --protocol tcp --port 8--1 --cidr 0.0.0.0/0')
44
result = {'GroupName': 'foobar',
45
'IpPermissions': [{'FromPort': 8, 'IpProtocol': 'tcp',
46
'IpRanges': [{'CidrIp': '0.0.0.0/0'}],
47
'ToPort': -1}]}
48
self.assert_params_for_cmd(args, result)
49
50
def test_all_protocol(self):
51
args = self.prefix + (
52
'--group-name foobar --protocol all --port all --cidr 0.0.0.0/0')
53
result = {'GroupName': 'foobar',
54
# This is correct, the expected value is the *string*
55
# '-1'. This is because the IpProtocol is modeled
56
# as a string.
57
'IpPermissions': [{'FromPort': -1, 'IpProtocol': '-1',
58
'IpRanges': [{'CidrIp': '0.0.0.0/0'}],
59
'ToPort': -1}]}
60
61
self.assert_params_for_cmd(args, result)
62
63
def test_numeric_protocol(self):
64
args = self.prefix + (
65
'--group-name foobar --protocol 200 --cidr 0.0.0.0/0')
66
result = {'GroupName': 'foobar',
67
'IpPermissions': [{'IpProtocol': '200', 'IpRanges':
68
[{'CidrIp': '0.0.0.0/0'}]}]}
69
self.assert_params_for_cmd(args, result)
70
71
def test_negative_one_protocol(self):
72
args = self.prefix + (
73
'--group-name foobar --protocol -1 --cidr 0.0.0.0/0')
74
result = {'GroupName': 'foobar',
75
'IpPermissions': [{'IpProtocol': '-1', 'IpRanges':
76
[{'CidrIp': '0.0.0.0/0'}]}]}
77
self.assert_params_for_cmd(args, result)
78
79
def test_classic_group(self):
80
args = self.prefix + (
81
'--group-name foobar --protocol udp '
82
'--source-group fiebaz --group-owner 11111111')
83
result = {'GroupName': 'foobar',
84
'IpPermissions': [{'IpProtocol': 'udp', 'UserIdGroupPairs':
85
[{'GroupName': 'fiebaz', 'UserId':
86
'11111111'}]}]}
87
self.assert_params_for_cmd(args, result)
88
89
def test_vpc_group(self):
90
args = self.prefix + (
91
'--group-name foobar --protocol icmp --source-group sg-12345678')
92
result = {'GroupName': 'foobar',
93
'IpPermissions': [{'IpProtocol': 'icmp', 'UserIdGroupPairs':
94
[{'GroupId': 'sg-12345678'}]}]}
95
self.assert_params_for_cmd(args, result)
96
97
def test_IpPermissions(self):
98
json = (
99
'[{"FromPort":8000,"ToPort":9000,'
100
'"IpProtocol":"tcp","IpRanges":[{"CidrIp":"192.168.100.0/24"}]}]')
101
args = self.prefix + '--group-name foobar --ip-permissions %s' % json
102
result = {'GroupName': 'foobar',
103
'IpPermissions': [{'FromPort': 8000, 'ToPort': 9000,
104
'IpProtocol': 'tcp', 'IpRanges':
105
[{'CidrIp': '192.168.100.0/24'}]}]}
106
self.assert_params_for_cmd(args, result)
107
108
def test_IpPermissions_with_group_id(self):
109
json = (
110
'[{"FromPort":8000,"ToPort":9000,"IpProtocol":"tcp",'
111
'"IpRanges":[{"CidrIp":"192.168.100.0/24"}]}]')
112
args = self.prefix + '--group-id sg-12345678 --ip-permissions %s' % json
113
result = {'GroupId': 'sg-12345678',
114
'IpPermissions': [{'FromPort': 8000, 'ToPort': 9000,
115
'IpProtocol': 'tcp', 'IpRanges':
116
[{'CidrIp': '192.168.100.0/24'}]}]}
117
self.assert_params_for_cmd(args, result)
118
119
def test_both(self):
120
json = (
121
'[{"FromPort":8000,"ToPort":9000,"IpProtocol":"tcp",'
122
'"IpRanges":[{"CidrIp":"192.168.100.0/24"}]}]')
123
args = self.prefix + '--group-name foobar --port 100 --ip-permissions %s' % json
124
self.assert_params_for_cmd(args, expected_rc=255)
125
126
127
128
class TestRevokeSecurityGroupIngress(TestAuthorizeSecurityGroupIngress):
129
130
prefix = 'ec2 revoke-security-group-ingress '
131
132