forked from nccgroup/PMapper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_admin_identification.py
83 lines (68 loc) · 3.45 KB
/
test_admin_identification.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
# Copyright (c) NCC Group and Erik Steringer 2021. This file is part of Principal Mapper.
#
# Principal Mapper is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Principal Mapper is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with Principal Mapper. If not, see <https://www.gnu.org/licenses/>.
import logging
import unittest
from principalmapper.common import Node, Policy, Group
from principalmapper.graphing.gathering import update_admin_status
class TestAdminIdentification(unittest.TestCase):
def test_admin_verified_by_inline_policies(self):
admin_policy = Policy('arn:aws:iam::000000000000:user/user_1', 'inline_admin', {
'Version': '2012-10-17',
'Statement': [{
'Effect': 'Allow',
'Action': '*',
'Resource': '*'
}]
})
not_admin_policy = Policy('arn:aws:iam::000000000000:user/user_2', 'inline_not_admin', {
'Version': '2012-10-17',
'Statement': [
{
'Effect': 'Allow',
'Action': '*',
'Resource': '*'
},
{
'Effect': 'Deny',
'Action': '*',
'Resource': '*'
}
]
})
new_node_1 = Node('arn:aws:iam::000000000000:user/user_1', 'id1', [admin_policy], [], None, None, 1, False,
False, None, False, None)
new_node_2 = Node('arn:aws:iam::000000000000:user/user_2', 'id2', [not_admin_policy], [], None, None, 1, False,
False, None, False, None)
update_admin_status([new_node_1, new_node_2])
self.assertTrue(new_node_1.is_admin, 'User with admin policy should be marked as an admin')
self.assertFalse(new_node_2.is_admin, 'User with non-admin policy should not be marked as an admin')
def test_admin_verified_for_group_member(self):
admin_policy = Policy('arn:aws:iam::000000000000:group/admins', 'inline_admin', {
'Version': '2012-10-17',
'Statement': [{
'Effect': 'Allow',
'Action': '*',
'Resource': '*'
}]
})
admin_group = Group('arn:aws:iam::000000000000:group/admins', [admin_policy])
not_admin_group = Group('arn:aws:iam::000000000000:group/losers', [])
new_node_1 = Node('arn:aws:iam::000000000000:user/node_1', 'id1', [], [admin_group], None, None, 1, False,
False, None, False, None)
new_node_2 = Node('arn:aws:iam::000000000000:user/node_2', 'id2', [], [not_admin_group], None, None, 1, False,
False, None, False, None)
update_admin_status([new_node_1, new_node_2])
self.assertTrue(new_node_1.is_admin, 'Member of admin group should be marked as an admin')
self.assertFalse(new_node_2.is_admin, 'Member of non-admin group should not be marked as an admin')