File: test_security_group.py

package info (click to toggle)
heat 1:11.0.0-6
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 15,964 kB
  • sloc: python: 172,697; sh: 1,527; makefile: 208
file content (564 lines) | stat: -rw-r--r-- 21,719 bytes parent folder | download | duplicates (2)
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

import collections
import copy

import mock
from neutronclient.common import exceptions as neutron_exc
from neutronclient.v2_0 import client as neutronclient

from heat.common import template_format
from heat.engine import resource
from heat.engine import rsrc_defn
from heat.engine import scheduler
from heat.engine import stack as parser
from heat.engine import template
from heat.tests import common
from heat.tests import utils

NovaSG = collections.namedtuple('NovaSG',
                                ' '.join([
                                    'name',
                                    'id',
                                    'rules',
                                    'description',
                                ]))


class SecurityGroupTest(common.HeatTestCase):

    test_template_neutron = '''
HeatTemplateFormatVersion: '2012-12-12'
Resources:
  the_sg:
    Type: AWS::EC2::SecurityGroup
    Properties:
      GroupDescription: HTTP and SSH access
      VpcId: aaaa
      SecurityGroupIngress:
        - IpProtocol: tcp
          FromPort: "22"
          ToPort: "22"
          CidrIp: 0.0.0.0/0
        - IpProtocol: tcp
          FromPort : "80"
          ToPort : "80"
          CidrIp : 0.0.0.0/0
        - IpProtocol: tcp
          SourceSecurityGroupId: wwww
      SecurityGroupEgress:
        - IpProtocol: tcp
          FromPort: "22"
          ToPort: "22"
          CidrIp: 10.0.1.0/24
        - SourceSecurityGroupName: xxxx
'''

    def setUp(self):
        super(SecurityGroupTest, self).setUp()
        self.m_csg = self.patchobject(neutronclient.Client,
                                      'create_security_group')
        self.m_csgr = self.patchobject(
            neutronclient.Client, 'create_security_group_rule')
        self.m_ssg = self.patchobject(neutronclient.Client,
                                      'show_security_group')
        self.m_dsgr = self.patchobject(
            neutronclient.Client, 'delete_security_group_rule')
        self.m_dsg = self.patchobject(
            neutronclient.Client, 'delete_security_group')
        self.m_usg = self.patchobject(
            neutronclient.Client, 'update_security_group')
        self.patchobject(resource.Resource, 'is_using_neutron',
                         return_value=True)
        self.sg_name = utils.PhysName('test_stack', 'the_sg')

    def mock_no_neutron(self):
        self.patchobject(resource.Resource, 'is_using_neutron',
                         return_value=False)

    def create_stack(self, templ):
        self.stack = self.parse_stack(template_format.parse(templ))
        self.assertIsNone(self.stack.create())
        return self.stack

    def parse_stack(self, t):
        stack_name = 'test_stack'
        tmpl = template.Template(t)
        stack = parser.Stack(utils.dummy_context(), stack_name, tmpl)
        stack.store()
        return stack

    def assertResourceState(self, rsrc, ref_id, metadata=None):
        metadata = metadata or {}
        self.assertIsNone(rsrc.validate())
        self.assertEqual((rsrc.CREATE, rsrc.COMPLETE), rsrc.state)
        self.assertEqual(ref_id, rsrc.FnGetRefId())
        self.assertEqual(metadata, dict(rsrc.metadata_get()))

    def validate_create_security_group_rule_calls(self):
        expected = [
            mock.call(
                {'security_group_rule': {
                    'security_group_id': 'aaaa', 'protocol': 'tcp',
                    'port_range_max': 22, 'direction': 'ingress',
                    'remote_group_id': None, 'ethertype': 'IPv4',
                    'remote_ip_prefix': '0.0.0.0/0', 'port_range_min': 22}}
            ),
            mock.call(
                {'security_group_rule': {
                    'security_group_id': 'aaaa', 'protocol': 'tcp',
                    'port_range_max': 80, 'direction': 'ingress',
                    'remote_group_id': None, 'ethertype': 'IPv4',
                    'remote_ip_prefix': '0.0.0.0/0', 'port_range_min': 80}}
            ),
            mock.call(
                {'security_group_rule': {
                    'security_group_id': 'aaaa', 'protocol': 'tcp',
                    'port_range_max': None, 'direction': 'ingress',
                    'remote_group_id': 'wwww', 'ethertype': 'IPv4',
                    'remote_ip_prefix': None, 'port_range_min': None}}
            ),
            mock.call(
                {'security_group_rule': {
                    'security_group_id': 'aaaa', 'protocol': 'tcp',
                    'port_range_max': 22, 'direction': 'egress',
                    'remote_group_id': None, 'ethertype': 'IPv4',
                    'remote_ip_prefix': '10.0.1.0/24', 'port_range_min': 22}}
            ),
            mock.call(
                {'security_group_rule': {
                    'security_group_id': 'aaaa', 'protocol': None,
                    'port_range_max': None, 'direction': 'egress',
                    'remote_group_id': 'xxxx', 'ethertype': 'IPv4',
                    'remote_ip_prefix': None, 'port_range_min': None}})
        ]

        self.assertEqual(expected, self.m_csgr.call_args_list)

    def validate_delete_security_group_rule(self):
        self.assertEqual(
            [mock.call('aaaa-1'),
             mock.call('aaaa-2'),
             mock.call('bbbb'),
             mock.call('cccc'),
             mock.call('dddd'),
             mock.call('eeee'),
             mock.call('ffff'),
             ],
            self.m_dsgr.call_args_list)

    def validate_stubout_neutron_create_security_group(self):
        self.m_csg.assert_called_once_with({
            'security_group': {
                'name': self.sg_name,
                'description': 'HTTP and SSH access'
            }
        })
        self.validate_delete_security_group_rule()
        self.validate_create_security_group_rule_calls()

    def stubout_neutron_create_security_group(self, mock_csgr=True):
        self.m_csg.return_value = {
            'security_group': {
                'tenant_id': 'f18ca530cc05425e8bac0a5ff92f7e88',
                'name': self.sg_name,
                'description': 'HTTP and SSH access',
                'security_group_rules': [{
                    "direction": "egress",
                    "ethertype": "IPv4",
                    "id": "aaaa-1",
                    "port_range_max": None,
                    "port_range_min": None,
                    "protocol": None,
                    "remote_group_id": None,
                    "remote_ip_prefix": None,
                    "security_group_id": "aaaa",
                    "tenant_id": "f18ca530cc05425e8bac0a5ff92f7e88"
                }, {
                    "direction": "egress",
                    "ethertype": "IPv6",
                    "id": "aaaa-2",
                    "port_range_max": None,
                    "port_range_min": None,
                    "protocol": None,
                    "remote_group_id": None,
                    "remote_ip_prefix": None,
                    "security_group_id": "aaaa",
                    "tenant_id": "f18ca530cc05425e8bac0a5ff92f7e88"
                }],
                'id': 'aaaa'
            }
        }
        if mock_csgr:
            self.m_csgr.side_effect = [
                {
                    'security_group_rule': {
                        'direction': 'ingress',
                        'remote_group_id': None,
                        'remote_ip_prefix': '0.0.0.0/0',
                        'port_range_min': 22,
                        'ethertype': 'IPv4',
                        'port_range_max': 22,
                        'protocol': 'tcp',
                        'security_group_id': 'aaaa',
                        'id': 'bbbb'
                    }},
                {
                    'security_group_rule': {
                        'direction': 'ingress',
                        'remote_group_id': None,
                        'remote_ip_prefix': '0.0.0.0/0',
                        'port_range_min': 80,
                        'ethertype': 'IPv4',
                        'port_range_max': 80,
                        'protocol': 'tcp',
                        'security_group_id': 'aaaa',
                        'id': 'cccc'
                    }
                },
                {
                    'security_group_rule': {
                        'direction': 'ingress',
                        'remote_group_id': 'wwww',
                        'remote_ip_prefix': None,
                        'port_range_min': None,
                        'ethertype': 'IPv4',
                        'port_range_max': None,
                        'protocol': 'tcp',
                        'security_group_id': 'aaaa',
                        'id': 'dddd'
                    }
                },
                {
                    'security_group_rule': {
                        'direction': 'egress',
                        'remote_group_id': None,
                        'remote_ip_prefix': '10.0.1.0/24',
                        'port_range_min': 22,
                        'ethertype': 'IPv4',
                        'port_range_max': 22,
                        'protocol': 'tcp',
                        'security_group_id': 'aaaa',
                        'id': 'eeee'
                    }
                },
                {
                    'security_group_rule': {
                        'direction': 'egress',
                        'remote_group_id': 'xxxx',
                        'remote_ip_prefix': None,
                        'port_range_min': None,
                        'ethertype': 'IPv4',
                        'port_range_max': None,
                        'protocol': None,
                        'security_group_id': 'aaaa',
                        'id': 'ffff'
                    }
                }
            ]

    def stubout_neutron_get_security_group(self):
        self.m_ssg.return_value = {
            'security_group': {
                'tenant_id': 'f18ca530cc05425e8bac0a5ff92f7e88',
                'name': 'sc1',
                'description': '',
                'security_group_rules': [{
                    'direction': 'ingress',
                    'protocol': 'tcp',
                    'port_range_max': 22,
                    'id': 'bbbb',
                    'ethertype': 'IPv4',
                    'security_group_id': 'aaaa',
                    'remote_group_id': None,
                    'remote_ip_prefix': '0.0.0.0/0',
                    'tenant_id': 'f18ca530cc05425e8bac0a5ff92f7e88',
                    'port_range_min': 22
                }, {
                    'direction': 'ingress',
                    'protocol': 'tcp',
                    'port_range_max': 80,
                    'id': 'cccc',
                    'ethertype': 'IPv4',
                    'security_group_id': 'aaaa',
                    'remote_group_id': None,
                    'remote_ip_prefix': '0.0.0.0/0',
                    'tenant_id': 'f18ca530cc05425e8bac0a5ff92f7e88',
                    'port_range_min': 80
                }, {
                    'direction': 'ingress',
                    'protocol': 'tcp',
                    'port_range_max': None,
                    'id': 'dddd',
                    'ethertype': 'IPv4',
                    'security_group_id': 'aaaa',
                    'remote_group_id': 'wwww',
                    'remote_ip_prefix': None,
                    'tenant_id': 'f18ca530cc05425e8bac0a5ff92f7e88',
                    'port_range_min': None
                }, {
                    'direction': 'egress',
                    'protocol': 'tcp',
                    'port_range_max': 22,
                    'id': 'eeee',
                    'ethertype': 'IPv4',
                    'security_group_id': 'aaaa',
                    'remote_group_id': None,
                    'remote_ip_prefix': '10.0.1.0/24',
                    'tenant_id': 'f18ca530cc05425e8bac0a5ff92f7e88',
                    'port_range_min': 22
                }, {
                    'direction': 'egress',
                    'protocol': None,
                    'port_range_max': None,
                    'id': 'ffff',
                    'ethertype': 'IPv4',
                    'security_group_id': 'aaaa',
                    'remote_group_id': 'xxxx',
                    'remote_ip_prefix': None,
                    'tenant_id': 'f18ca530cc05425e8bac0a5ff92f7e88',
                    'port_range_min': None
                }],
                'id': 'aaaa'}}

    def test_security_group_neutron(self):
        # create script
        self.stubout_neutron_create_security_group()

        self.stubout_neutron_get_security_group()

        stack = self.create_stack(self.test_template_neutron)

        sg = stack['the_sg']

        self.assertResourceState(sg, 'aaaa')

        stack.delete()
        self.validate_stubout_neutron_create_security_group()
        self.m_ssg.assert_called_once_with('aaaa')
        self.m_dsg.assert_called_once_with('aaaa')

    def test_security_group_neutron_exception(self):
        # create script
        self.m_csg.return_value = {
            'security_group': {
                'tenant_id': 'f18ca530cc05425e8bac0a5ff92f7e88',
                'name': self.sg_name,
                'description': 'HTTP and SSH access',
                'security_group_rules': [],
                'id': 'aaaa'
            }
        }
        self.m_csgr.side_effect = neutron_exc.Conflict

        # delete script
        self.m_dsgr.side_effect = neutron_exc.NeutronClientException(
            status_code=404)
        self.m_ssg.side_effect = [
            {'security_group': {
                'tenant_id': 'f18ca530cc05425e8bac0a5ff92f7e88',
                'name': 'sc1',
                'description': '',
                'security_group_rules': [{
                    'direction': 'ingress',
                    'protocol': 'tcp',
                    'port_range_max': 22,
                    'id': 'bbbb',
                    'ethertype': 'IPv4',
                    'security_group_id': 'aaaa',
                    'remote_group_id': None,
                    'remote_ip_prefix': '0.0.0.0/0',
                    'tenant_id': 'f18ca530cc05425e8bac0a5ff92f7e88',
                    'port_range_min': 22
                }, {
                    'direction': 'ingress',
                    'protocol': 'tcp',
                    'port_range_max': 80,
                    'id': 'cccc',
                    'ethertype': 'IPv4',
                    'security_group_id': 'aaaa',
                    'remote_group_id': None,
                    'remote_ip_prefix': '0.0.0.0/0',
                    'tenant_id': 'f18ca530cc05425e8bac0a5ff92f7e88',
                    'port_range_min': 80
                }, {
                    'direction': 'ingress',
                    'protocol': 'tcp',
                    'port_range_max': None,
                    'id': 'dddd',
                    'ethertype': 'IPv4',
                    'security_group_id': 'aaaa',
                    'remote_group_id': 'wwww',
                    'remote_ip_prefix': None,
                    'tenant_id': 'f18ca530cc05425e8bac0a5ff92f7e88',
                    'port_range_min': None
                }, {
                    'direction': 'egress',
                    'protocol': 'tcp',
                    'port_range_max': 22,
                    'id': 'eeee',
                    'ethertype': 'IPv4',
                    'security_group_id': 'aaaa',
                    'remote_group_id': None,
                    'remote_ip_prefix': '10.0.1.0/24',
                    'tenant_id': 'f18ca530cc05425e8bac0a5ff92f7e88',
                    'port_range_min': 22
                }, {
                    'direction': 'egress',
                    'protocol': None,
                    'port_range_max': None,
                    'id': 'ffff',
                    'ethertype': 'IPv4',
                    'security_group_id': 'aaaa',
                    'remote_group_id': None,
                    'remote_ip_prefix': None,
                    'tenant_id': 'f18ca530cc05425e8bac0a5ff92f7e88',
                    'port_range_min': None
                }],
                'id': 'aaaa'}},
            neutron_exc.NeutronClientException(status_code=404)]

        stack = self.create_stack(self.test_template_neutron)

        sg = stack['the_sg']

        self.assertResourceState(sg, 'aaaa')

        scheduler.TaskRunner(sg.delete)()

        sg.state_set(sg.CREATE, sg.COMPLETE, 'to delete again')
        sg.resource_id = 'aaaa'
        stack.delete()

        self.m_csg.assert_called_once_with({
            'security_group': {
                'name': self.sg_name,
                'description': 'HTTP and SSH access'
            }
        })
        self.validate_create_security_group_rule_calls()
        self.assertEqual(
            [mock.call('aaaa'), mock.call('aaaa')],
            self.m_ssg.call_args_list)
        self.assertEqual(
            [mock.call('bbbb'), mock.call('cccc'), mock.call('dddd'),
             mock.call('eeee'), mock.call('ffff')], self.m_dsgr.call_args_list)

    def test_security_group_neutron_update(self):
        # create script
        self.stubout_neutron_create_security_group(mock_csgr=False)

        # update script
        # delete old not needed rules
        self.stubout_neutron_get_security_group()

        stack = self.create_stack(self.test_template_neutron)
        sg = stack['the_sg']
        self.assertResourceState(sg, 'aaaa')

        # make updated template
        props = copy.deepcopy(sg.properties.data)
        props['SecurityGroupIngress'] = [
            {'IpProtocol': 'tcp',
             'FromPort': '80',
             'ToPort': '80',
             'CidrIp': '0.0.0.0/0'},
            {'IpProtocol': 'tcp',
             'FromPort': '443',
             'ToPort': '443',
             'CidrIp': '0.0.0.0/0'},
            {'IpProtocol': 'tcp',
             'SourceSecurityGroupId': 'zzzz'},
        ]
        props['SecurityGroupEgress'] = [
            {'IpProtocol': 'tcp',
             'FromPort': '22',
             'ToPort': '22',
             'CidrIp': '0.0.0.0/0'},
            {'SourceSecurityGroupName': 'xxxx'},
        ]
        after = rsrc_defn.ResourceDefinition(sg.name, sg.type(), props)

        # create missing rules
        self.m_csgr.side_effect = [
            {
                'security_group_rule': {
                    'direction': 'ingress',
                    'remote_group_id': None,
                    'remote_ip_prefix': '0.0.0.0/0',
                    'port_range_min': 443,
                    'ethertype': 'IPv4',
                    'port_range_max': 443,
                    'protocol': 'tcp',
                    'security_group_id': 'aaaa',
                    'id': 'bbbb'}
            }, {
                'security_group_rule': {
                    'direction': 'ingress',
                    'remote_group_id': 'zzzz',
                    'remote_ip_prefix': None,
                    'port_range_min': None,
                    'ethertype': 'IPv4',
                    'port_range_max': None,
                    'protocol': 'tcp',
                    'security_group_id': 'aaaa',
                    'id': 'dddd'}
            }, {
                'security_group_rule': {
                    'direction': 'egress',
                    'remote_group_id': None,
                    'remote_ip_prefix': '0.0.0.0/0',
                    'port_range_min': 22,
                    'ethertype': 'IPv4',
                    'port_range_max': 22,
                    'protocol': 'tcp',
                    'security_group_id': 'aaaa',
                    'id': 'eeee'
                }
            }
        ]

        scheduler.TaskRunner(sg.update, after)()

        self.assertEqual((sg.UPDATE, sg.COMPLETE), sg.state)
        self.m_dsgr.assert_has_calls(
            [mock.call('aaaa-1'), mock.call('aaaa-2'), mock.call('eeee'),
             mock.call('dddd'), mock.call('bbbb')],
            any_order=True)
        self.m_ssg.assert_called_once_with('aaaa')

    def test_security_group_neutron_update_with_empty_rules(self):
        # create script
        self.stubout_neutron_create_security_group()

        # update script
        # delete old not needed rules
        self.stubout_neutron_get_security_group()

        stack = self.create_stack(self.test_template_neutron)
        sg = stack['the_sg']
        self.assertResourceState(sg, 'aaaa')

        # make updated template
        props = copy.deepcopy(sg.properties.data)
        del props['SecurityGroupEgress']
        after = rsrc_defn.ResourceDefinition(sg.name, sg.type(), props)
        scheduler.TaskRunner(sg.update, after)()

        self.assertEqual((sg.UPDATE, sg.COMPLETE), sg.state)
        self.m_ssg.assert_called_once_with('aaaa')
        self.m_dsgr.assert_has_calls(
            [mock.call('aaaa-1'), mock.call('aaaa-2'), mock.call('eeee'),
             mock.call('ffff')],
            any_order=True)