-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_access_graph.py
272 lines (216 loc) · 8.23 KB
/
generate_access_graph.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
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
from collections import defaultdict
from dataclasses import dataclass
import time
from typing import Any, List
import random
import os
import enum
import networkx as nx
import itertools
from iam import extract
from z3 import Solver, String, Bool, StringVal, sat
from policy import transpile
random.seed(1234)
ACCOUNT_ID = int.from_bytes(random.randbytes(8), "little")
S3_BUCKET_ACTIONS = [
"s3:GetObject",
"s3:GetObjectAcl",
"s3:GetObjectRetention",
"s3:InitiateReplication",
"s3:PutObject",
"s3:DeleteObject",
"s3:DeleteObjectVersion",
"s3:PutObject"
]
STS_ACTIONS: list[str] = [
"sts:AssumeRole",
#"sts:*"
]
@dataclass
class Statement:
effect: bool # True for allow, False for deny
actions: list[str] # pattern representing resource
resources: list[str] # pattern representing resource this policy applies to
@dataclass
class Policy:
statements: list[Statement]
def policy_to_dict(p: Policy) -> dict[str, Any]:
return {
'PolicyVersion': {
'Document': {
'Statement': list(map(statement_to_dict, p.statements))
}
}
}
def statement_to_dict(s: Statement):
return {
'Effect': 'Allow' if s.effect else 'Deny',
'Action': s.actions,
'Resource': s.resources
}
class IDKind(enum.Enum):
USER = 0
ROLE = 1
def __str__(self):
match self.value:
case 0: return "user"
case 1: return "role"
case _: raise ValueError("unknown id kind")
def format_arn(account_id, user_name, id_kind = IDKind.USER):
return f"arn:aws:iam::{account_id}:{id_kind}/{user_name}"
def format_user_id(n):
return f"user_{n}"
def generate_user_policies(role_groups, n=10):
user_policies = []
role_to_user = defaultdict(list)
for i in range(1, n+1):
role_group_id = random.choice(range(len(role_groups)))
user_arn = format_arn(ACCOUNT_ID, format_user_id(i), IDKind.USER)
role_arn = format_arn(ACCOUNT_ID, format_role_group(role_group_id), IDKind.ROLE)
policy = generate_user_policy(role_arn)
for role in role_groups[role_group_id]:
role_to_user[role].append(user_arn)
user_policies.append((user_arn, policy))
return (user_policies, role_to_user)
def format_test_object(id):
return f"test_object_{id}"
def next_test_object_id():
c = 1
while 1:
yield format_test_object(c)
c += 1
next_test_object = next_test_object_id()
def generate_test_object():
return next(next_test_object)
def generate_test_objects_for_policy(num_objects):
return [generate_test_object() for _ in range(num_objects)]
def generate_role_policy(n_objects, n_actions, connected):
resources = generate_test_objects_for_policy(n_objects)
actions = random.sample(S3_BUCKET_ACTIONS, k=n_actions)
resource_stmt = Statement(True, actions, resources)
assume_stmt = Statement(True, [random.choice(STS_ACTIONS)], connected)
return Policy([resource_stmt, assume_stmt])
def collapse_to_action_resource_pairs(policy):
pairs = []
for stmt in policy.statements:
for act in stmt.actions:
for res in stmt.resources:
pairs.append((act, res))
return pairs
def generate_role_policies(role_graph, max_n_objects, max_n_actions):
role_policies = []
role_action_resource_pairs = {}
for role in role_graph:
rp = generate_role_policy(random.randint(1, max_n_objects),
random.randint(1, max_n_actions),
role_graph[role])
role_action_resource_pairs[role] = collapse_to_action_resource_pairs(rp)
role_policies.append((role, rp))
return (role_policies, role_action_resource_pairs)
def format_role(group_i, role_i):
return f"role_r{group_i}_{role_i}"
def format_role_group(group_i):
return f"role_r{group_i}"
def generate_roles(num_role_groups=5, max_role_group_size=4):
return [[format_arn(ACCOUNT_ID, format_role(i, j), IDKind.ROLE) for j in range(1, random.randint(1, max_role_group_size)+1)]
for i in range(num_role_groups)]
def generate_role_assumption_graph(roles: list[str], branching_factor=2):
adj = defaultdict(list)
seen = set()
for role in roles:
seen.add(role)
connected_roles = list(filter(lambda r: r not in seen, random.sample(roles, min(len(roles), branching_factor))))
adj[role] = connected_roles
return adj
def flatten(roles):
return [r for group in roles for r in group]
def to_digraph(adj):
flattened = [(r1, r2) for r1 in adj for r2 in adj[r1]]
return nx.DiGraph(flattened)
def generate_user_policy(role_arn):
return Policy([Statement(True, [random.choice(STS_ACTIONS)], [f"{role_arn}*"])])
def remove_unmapped_roles(dg, role_to_user):
nodes = list(dg.nodes)
for node in nodes:
if len(role_to_user[node]) == 0:
dg.remove_node(node)
def rand_walk(dg, k):
v = random.choice(list(dg.nodes))
seen = 0
path = []
while seen < k:
path.append(v)
seen += 1
if len(dg[v]) == 0:
break
else:
v = random.choice(list(dg[v]))
return path
@dataclass
class IAMTestConfiguration:
# the params used for generating this test
params: dict[str, int]
# the compiled test environment
env: dict[str, Any]
# mapping from role to list of users that can assume said role
role_to_user: dict[str, list[str]]
# mapping from role to (action, resource) pairs
role_to_action_resource_pairs: dict[str, list[tuple[str, str]]]
role_graph: nx.DiGraph
def generate_allow_test(self):
dg = self.role_graph.copy()
remove_unmapped_roles(dg, self.role_to_user)
lp = nx.dag_longest_path(dg)
user = random.choice(self.role_to_user[lp[0]])
action, resource = random.choice(self.role_to_action_resource_pairs[lp[-1]])
return (user, action, resource, len(lp))
def generate_random_allow_test(self):
dg = self.role_graph.copy()
remove_unmapped_roles(dg, self.role_to_user)
lp = nx.dag_longest_path(dg)
p = rand_walk(dg, len(lp))
user = random.choice(self.role_to_user[p[0]])
action, resource = random.choice(self.role_to_action_resource_pairs[p[-1]])
return (user, action, resource)
def generate_iam_test(params):
roles = generate_roles(num_role_groups=params['num_role_groups'], max_role_group_size=params['roles_per_group'])
adj = generate_role_assumption_graph(flatten(roles), branching_factor=params['role_branching_factor'])
user_policies, role_to_user = generate_user_policies(roles, n=params['num_users'])
role_policies, role_to_pairs = generate_role_policies(adj, max_n_objects=params['max_n_objects'], max_n_actions=params['max_n_actions'])
policy_dicts = flat_map(extract, map(lambda user_policy: policy_to_dict(user_policy[1]), user_policies))
policy_arns = flat_map(lambda user_policy: [user_policy[0]]*len(user_policy[1].statements), user_policies)
role_dicts = flat_map(extract, map(lambda role_policy: policy_to_dict(role_policy[1]), role_policies))
role_arns = flat_map(lambda role_policy: [role_policy[0]]*len(role_policy[1].statements), role_policies)
all_policies = list(zip(policy_arns, policy_dicts)) + list(zip(role_arns, role_dicts))
#print(all_policies)
env = transpile(all_policies)
return IAMTestConfiguration(params, env, role_to_user, role_to_pairs, nx.DiGraph(adj))
#:print(users)
# dg = to_digraph(adj)
# print(nx.dag_longest_path(dg))
def flat_map(f, xs):
return (y for ys in xs for y in f(ys))
# dg = to_digraph(adj)
# lp = nx.dag_longest_path(dg)
# print(lp)
# #transpile(user_policies)
# env = transpile(all_policies)
# s = Solver()
# e = Bool('e')
# u = String('u')
# a = String('a')
# r = String('r')
# user = random.choice(role_to_user[lp[0]])
# act, res = random.choice(role_to_pairs[lp[-1]])
# #print(role_to_pairs)
# print(f"{act}, {res}")
# s.add(env['Allow'](StringVal(user), StringVal(act), StringVal(res)))
# res, elapsed = time_function(lambda: s.check())
# if res == sat:
# print('sat')
# #print(s.model())
# else:
# print('unsat')
# print(f"elapsed: {elapsed}")
# # print(role_to_user)
# # print(role_to_pairs)