-
Notifications
You must be signed in to change notification settings - Fork 197
Expand file tree
/
Copy pathlocal_policy_simulation.py
More file actions
943 lines (817 loc) · 43 KB
/
local_policy_simulation.py
File metadata and controls
943 lines (817 loc) · 43 KB
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
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
"""Utility code for the querying module: creates functions for simulating the authorization of principals making
API calls to AWS."""
# Copyright (c) NCC Group and Erik Steringer 2019. 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 ast
import datetime as dt
import functools
import dateutil.parser as dup
from enum import Enum
import ipaddress
import logging
from typing import List, Dict, Optional, Union, Pattern
import re
from principalmapper.common import Node, Policy
from principalmapper.util import arns
from principalmapper.util.case_insensitive_dict import CaseInsensitiveDict
logger = logging.getLogger(__name__)
def has_matching_statement(principal: Node, effect_value: str, action_to_check: str, resource_to_check: str,
condition_keys_to_check: CaseInsensitiveDict) -> bool:
"""Locally determine if a node's attached policies (and group's policies if applicable) has at least one matching
statement with the given effect. This is the meat of the local policy evaluation.
"""
for policy in principal.attached_policies:
if policy_has_matching_statement(policy, effect_value, action_to_check, resource_to_check,
condition_keys_to_check):
return True
for group in principal.group_memberships:
for policy in group.attached_policies:
if policy_has_matching_statement(policy, effect_value, action_to_check, resource_to_check,
condition_keys_to_check):
return True
return False
def policy_has_matching_statement(policy: Union[Policy, dict], effect_value: str, action_to_check: str,
resource_to_check: str,
condition_keys_to_check: CaseInsensitiveDict) -> bool:
"""Searches a specific Policy/dict object for a statement with a matching Effect/Action/Resource/Condition"""
if isinstance(policy, Policy):
base_obj = policy.policy_doc
else:
base_obj = policy
# go through each policy_doc
for statement in _listify_dictionary(base_obj['Statement']):
if statement['Effect'] != effect_value:
continue # skip if effect doesn't match
matches_action, matches_resource, matches_condition = False, False, False
# start by checking the action
if 'Action' in statement:
for action in _listify_string(statement['Action']):
if _matches_after_expansion(action_to_check, action):
matches_action = True
break
else: # 'NotAction' in statement
matches_action = True
for notaction in _listify_string(statement['NotAction']):
if _matches_after_expansion(action_to_check, notaction):
matches_action = False
break # finish looping
if not matches_action:
continue # cut early
# if action is good, check resource
if 'Resource' in statement:
for resource in _listify_string(statement['Resource']):
if _matches_after_expansion(resource_to_check, resource, condition_keys_to_check):
matches_resource = True
break
elif 'NotResource' in statement: # 'NotResource' in statement
matches_resource = True
for notresource in _listify_string(statement['NotResource']):
if _matches_after_expansion(resource_to_check, notresource, condition_keys_to_check):
matches_resource = False
break
else:
matches_resource = True # TODO: examine validity of not using a Resource/NotResource field (trust docs)
if not matches_resource:
continue # cut early
# if resource is good, check condition
if 'Condition' in statement:
matches_condition = _get_condition_match(statement['Condition'], condition_keys_to_check)
else:
matches_condition = True
if matches_action and matches_resource and matches_condition:
return True
return False
def _get_condition_match(condition: Dict[str, Dict[str, Union[str, List]]], context: CaseInsensitiveDict) -> bool:
"""
Internal method. It digs through Null, Bool, DateX, NumericX, StringX conditions and returns false if any of
them don't match what the context has.
Also handles ForAnyValue and ForAllValues
See: https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_policies_elements_condition_operators.html
"""
for block in condition.keys():
# String operators
if 'String' in block:
# string comparison after expansion
if block.startswith('ForAllValues:'):
# fail to match unless all of the provided context values match
for policy_key in condition[block]:
if policy_key in context.keys():
for context_value in _listify_string(context[policy_key]):
if context_value != '':
if not _get_str_match(
block,
policy_key,
condition[block][policy_key],
CaseInsensitiveDict({policy_key: context_value})):
return False
elif block.startswith('ForAnyValue:'):
# fail to match unless at least one of the provided context values match
no_match = True
for policy_key in condition[block]:
if policy_key in context.keys():
for context_value in _listify_string(context[policy_key]):
if context_value != '':
if _get_str_match(block, policy_key, condition[block][policy_key], context):
no_match = False
if no_match:
return False
else:
for policy_context_key in condition[block]:
if not _get_str_match(block, policy_context_key, condition[block][policy_context_key], context):
return False
if 'Numeric' in block:
# convert string to int and compare (floats allowed? how to handle?)
if block.startswith('ForAllValues:'):
# fail to match unless all of the provided context values match
for policy_key in condition[block]:
if policy_key in context.keys():
for context_value in _listify_string(context[policy_key]):
if context_value != '':
if not _get_num_match(block, policy_key, condition[block][policy_key], context):
return False
elif block.startswith('ForAnyValue:'):
# fail to match unless at least one of the provided context values match
no_match = True
for policy_key in condition[block]:
if policy_key in context.keys():
for context_value in _listify_string(context[policy_key]):
if context_value != '':
if _get_num_match(block, policy_key, condition[block][policy_key], context):
no_match = False
if no_match:
return False
else:
for policy_context_key in condition[block]:
if not _get_num_match(block, policy_context_key, condition[block][policy_context_key], context):
return False
if 'Date' in block:
# need the datetime and dateutil module to do this, do everything in UTC where undefined
if block.startswith('ForAllValues:'):
# fail to match unless all of the provided context values match
for policy_key in condition[block]:
if policy_key in context.keys():
for context_value in _listify_string(context[policy_key]):
if context_value != '':
if not _get_date_match(block, policy_key, condition[block][policy_key], context):
return False
elif block.startswith('ForAnyValue:'):
# fail to match unless at least one of the provided context values match
no_match = True
for policy_key in condition[block]:
if policy_key in context.keys():
for context_value in _listify_string(context[policy_key]):
if context_value != '':
if _get_date_match(block, policy_key, condition[block][policy_key], context):
no_match = False
if no_match:
return False
else:
for policy_context_key in condition[block]:
if not _get_date_match(block, policy_context_key, condition[block][policy_context_key], context):
return False
if 'Bool' in block:
# boolean comparison
if block.startswith('ForAllValues:'):
# fail to match unless all of the provided context values match
for policy_key in condition[block]:
if policy_key in context.keys():
for context_value in _listify_string(context[policy_key]):
if context_value != '':
if not _get_bool_match(block, policy_key, condition[block][policy_key], context):
return False
elif block.startswith('ForAnyValue:'):
# fail to match unless at least one of the provided context values match
no_match = True
for policy_key in condition[block]:
if policy_key in context.keys():
for context_value in _listify_string(context[policy_key]):
if context_value != '':
if _get_bool_match(block, policy_key, condition[block][policy_key], context):
no_match = False
if no_match:
return False
else:
for policy_context_key in condition[block]:
if not _get_bool_match(block, policy_context_key, condition[block][policy_context_key], context):
return False
if 'BinaryEquals' in block:
# straight string comparison
if block.startswith('ForAllValues:'):
# fail to match unless all of the provided context values match
for policy_key in condition[block]:
if policy_key in context.keys():
for context_value in _listify_string(context[policy_key]):
if context_value != '':
if not _get_straight_str_match(block, policy_key, condition[block][policy_key],
context):
return False
elif block.startswith('ForAnyValue:'):
# fail to match unless at least one of the provided context values match
no_match = True
for policy_key in condition[block]:
if policy_key in context.keys():
for context_value in _listify_string(context[policy_key]):
if context_value != '':
if _get_straight_str_match(block, policy_key, condition[block][policy_key], context):
no_match = False
if no_match:
return False
else:
for policy_context_key in condition[block]:
if not _get_straight_str_match(block, policy_context_key, condition[block][policy_context_key],
context):
return False
if 'IpAddress' in block:
# need ipaddress module, use ipaddress.ip_address in <ipaddress.ip_network obj>
if block.startswith('ForAllValues:'):
# fail to match unless all of the provided context values match
for policy_key in condition[block]:
if policy_key in context.keys():
for context_value in _listify_string(context[policy_key]):
if context_value != '':
if not _get_ipaddress_match(block, policy_key, condition[block][policy_key], context):
return False
elif block.startswith('ForAnyValue:'):
# fail to match unless at least one of the provided context values match
no_match = True
for policy_key in condition[block]:
if policy_key in context.keys():
for context_value in _listify_string(context[policy_key]):
if context_value != '':
if _get_ipaddress_match(block, policy_key, condition[block][policy_key], context):
no_match = False
if no_match:
return False
else:
for policy_context_key in condition[block]:
if not _get_ipaddress_match(block, policy_context_key, condition[block][policy_context_key],
context):
return False
if 'Arn' in block:
# string comparison after expansion
if block.startswith('ForAllValues:'):
# fail to match unless all of the provided context values match
for policy_key in condition[block]:
if policy_key in context.keys():
for context_value in _listify_string(context[policy_key]):
if context_value != '':
if not _get_arn_match(block, policy_key, condition[block][policy_key], context):
return False
elif block.startswith('ForAnyValue:'):
# fail to match if none of the provided context values match
no_match = True
for policy_key in condition[block]:
if policy_key in context.keys():
for context_value in _listify_string(context[policy_key]):
if context_value != '':
if _get_arn_match(block, policy_key, condition[block][policy_key], context):
no_match = False
if no_match:
return False
else:
for policy_context_key in condition[block]:
if not _get_arn_match(block, policy_context_key, condition[block][policy_context_key], context):
return False
# handle Null, ForAllValues:Null, ForAnyValue:Null
if 'Null' in block:
if block.startswith('ForAllValues:'):
# fail to match unless all of the provided context values match
for policy_key in condition[block]:
if policy_key in context.keys():
for context_value in _listify_string(context[policy_key]):
if context_value != '':
if not _get_null_match(policy_key, condition[block][policy_key], context):
return False
elif block.startswith('ForAnyValue:'):
# fail to match unless at least one of the provided context values match
no_match = True
for policy_key in condition[block]:
if policy_key in context.keys():
for context_value in _listify_string(context[policy_key]):
if context_value != '':
if _get_null_match(policy_key, condition[block][policy_key], context):
no_match = False
if no_match:
return False
else:
for policy_context_key in condition[block]:
if not _get_null_match(policy_context_key, condition[block][policy_context_key], context):
return False
return True
def _get_str_match(block: str, policy_key: str, policy_value: Union[str, List[str]], context: CaseInsensitiveDict) -> bool:
"""Helper method for dealing with String* conditions, including: StringEquals, StringNotEquals,
StringEqualsIgnoreCase, StringNotEqualsIgnoreCase, StringLike, StringNotLike
Observed policy simulator behavior for Unicode: if I compare the following, it returns denied:
* ê <- 'LATIN SMALL LETTER E WITH CIRCUMFLEX'
* ê <- 'LATIN SMALL LETTER E' + 'COMBINING CIRCUMFLEX ACCENT'
So even though they're the "same" they end up not matching.
Observed policy simulator behavior for capitalization: if I compare the following, it returns denied:
* ß
* ss
So even though they're the "same" after casefolding (str casefold method) they end up not matching.
Just using lower() on the strings is enough to match the policy simulator behavior without having to dip into the
insanity of unicode.
Many thanks to https://stackoverflow.com/a/45745761 for the extra information.
"""
if_exists_op = 'IfExists' in block
if 'StringEquals' in block:
if policy_key not in context:
return if_exists_op
for value in _listify_string(policy_value):
for context_value in _listify_string(context[policy_key]):
if 'IgnoreCase' in block:
if value.lower() == context_value.lower():
return True
else:
if value == context_value:
return True
return False
elif 'StringLike' in block:
if policy_key not in context:
return if_exists_op
for value in _listify_string(policy_value):
for context_value in _listify_string(context[policy_key]):
if _expand_str_and_compare(value, context_value):
return True
return False
elif 'StringNotEquals' in block:
if policy_key not in context:
return True
for value in _listify_string(policy_value):
for context_value in _listify_string(context[policy_key]):
if 'IgnoreCase' in block:
if value.lower() == context_value.lower():
return False
else:
if value == context_value:
return False
return True
elif 'StringNotLike' in block:
if policy_key not in context:
return if_exists_op
for value in _listify_string(policy_value):
for context_value in _listify_string(context[policy_key]):
if _expand_str_and_compare(value, context_value):
return False
return True
def _expand_str_and_compare(pattern: str, input_value: str) -> bool:
"""Helper method for string comparison for *Like string conditions. Takes a Unicode pattern string,
replaces the asterisk and question mark with regex-equivalents, then test if input_value if found in that pattern.
Returns result.
Note: Docs say that *Like conditions are case-sensitive
"""
pattern_string = pattern \
.replace(".", "\\.") \
.replace("*", ".*") \
.replace("?", ".") \
.replace("$", "\\$") \
.replace("^", "\\^")
pattern_string = "^{}$".format(pattern_string)
return re.match(pattern_string, input_value, flags=re.UNICODE) is not None
def _get_num_match(block: str, policy_key: str, policy_value: Union[str, List[str]], context: CaseInsensitiveDict) -> bool:
"""Helper method for dealing with Numeric* conditions, including: NumericEquals, NumericNotEquals,
NumericLessThan, NumericLessThanEquals, NumericGreaterThan, NumericGreaterThanEquals
Parses the string inputs into numbers before doing comparisons.
"""
if_exists_op = 'IfExists' in block
if block == 'NumericEquals':
if policy_key not in context:
return if_exists_op
for value in _listify_string(policy_value):
value_num = ast.literal_eval(value)
for context_value in _listify_string(context[policy_key]):
context_value_num = ast.literal_eval(context_value)
if value_num == context_value_num:
return True
return False
elif block == 'NumericNotEquals':
if policy_key not in context:
return True
for value in _listify_string(policy_value):
value_num = ast.literal_eval(value)
for context_value in _listify_string(context[policy_key]):
context_value_num = ast.literal_eval(context_value)
if value_num == context_value_num:
return False
return True
else:
if policy_key not in context:
return if_exists_op
for value in _listify_string(policy_value):
value_num = ast.literal_eval(value)
for context_value in _listify_string(context[policy_key]):
context_value_num = ast.literal_eval(context_value)
if block == 'NumericLessThan':
if context_value_num < value_num:
return True
elif block == 'NumericLessThanEquals':
if context_value_num <= value_num:
return True
elif block == 'NumericGreaterThan':
if context_value_num > value_num:
return True
elif block == 'NumericGreaterThanEquals':
if context_value_num >= value_num:
return True
return False
def _get_bool_match(block: str, policy_key: str, policy_value: Union[str, List[str]], context: CaseInsensitiveDict) -> bool:
"""Helper method for dealing with Bool. For 'true' policy values, returns True if context has 'true' as a value. For
'false' policy values, returns True if context has value that's not 'true'. Returns False if no context value.
"""
if_exists_op = 'IfExists' in block
if policy_key not in context:
return if_exists_op
for value in _listify_string(policy_value):
for context_value in _listify_string(context[policy_key]):
if value == 'true' and context_value.lower() == 'true':
return True
if value == 'false' and context_value.lower() != 'true':
return True
return False
def _get_straight_str_match(block: str, policy_key: str, policy_value: Union[str, List[str]], context: CaseInsensitiveDict) -> bool:
"""Helper method for dealing with BinaryEquals
Does a straight string comparison to search for a match.
"""
# can knock this out up here
if policy_key not in context:
return False
for value in _listify_string(policy_value):
for context_value in _listify_string(context[policy_key]):
if value == context_value:
return True
return False
def _get_ipaddress_match(block: str, policy_key: str, policy_value: Union[str, List[str]], context: CaseInsensitiveDict) -> bool:
"""Helper method for dealing with *IpAddress conditions: IpAddress, NotIpAddress
Parses the policy value as an IPvXNetwork, then the context value as an IPvXAddress, then uses
the `in` operator to determine a match.
"""
if_exists_op = 'IfExists' in block
for value in _listify_string(policy_value):
value_net = ipaddress.ip_network(value)
if block == 'IpAddress':
if policy_key not in context:
return if_exists_op
for context_value in _listify_string(context[policy_key]):
context_value_addr = ipaddress.ip_address(context_value)
if context_value_addr in value_net:
return True
else:
if policy_key not in context:
return True # simulator behavior: treat absence as approval
for context_value in _listify_string(context[policy_key]):
context_value_addr = ipaddress.ip_address(context_value)
if context_value_addr in value_net:
return False
# Finished loops without an answer, give defaults
if block == 'IpAddress':
return False
else:
return True
def _get_date_match(block: str, policy_key: str, policy_value: Union[str, List[str]], context: CaseInsensitiveDict) -> bool:
"""Helper method for dealing with Date* conditions: DateEquals, DateNotEquals, DateGreaterThan,
DateGreaterThanEquals, DateLessThan, DateLessThanEquals.
Parses values by distinguishing between epoch values and ISO 8601/RFC 3339 datetimestamps. Assumes
the timezone is UTC when not specified.
"""
if_exists_op = 'IfExists' in block
for value in _listify_string(policy_value):
value_dt = _convert_timestamp_to_datetime_obj(value)
if block == 'DateEquals':
if policy_key not in context:
return if_exists_op
for context_value in _listify_string(context[policy_key]):
context_value_dt = _convert_timestamp_to_datetime_obj(context_value)
if value_dt == context_value_dt:
return True
elif block == 'DateNotEquals':
if policy_key not in context:
return True
for context_value in _listify_string(context[policy_key]):
context_value_dt = _convert_timestamp_to_datetime_obj(context_value)
if value_dt == context_value_dt:
return False
else: # block == 'DateGreaterThan' or 'DateGreaterThanEquals' or 'DateLessThan' or 'DateLessThanEquals'
if policy_key not in context:
return if_exists_op
for context_value in _listify_string(context[policy_key]):
context_value_dt = _convert_timestamp_to_datetime_obj(context_value)
if block == 'DateGreaterThan':
if context_value_dt > value_dt:
return True
elif block == 'DateGreaterThanEquals':
if context_value_dt >= value_dt:
return True
elif block == 'DateLessThan':
if context_value_dt < value_dt:
return True
elif block == 'DateLessThanEquals':
if context_value_dt <= value_dt:
return True
return False
# Finished loops, give default answers
if block == 'DateEquals':
return False
elif block == 'DateNotEquals':
return True
else: # DateGreaterThan, DateGreaterThanEquals, DateLessThan, DateLessThanEquals
return False
def _convert_timestamp_to_datetime_obj(timestamp: str):
"""Helper method for the helper method: converts string to datetime object"""
if '-' in timestamp: # policy simulator behavior: datetimestamps need dashes, even though ISO 8601 doesn't (?)
# parse as ISO 8601/RFC 3339
result = dup.parse(timestamp)
if result.tzinfo is None:
result.replace(tzinfo=dt.timezone.utc)
return result
else:
# parse as epoch timestamp
return dt.datetime.fromtimestamp(float(timestamp), dt.timezone.utc) # TODO: concern around float imprecision
def _get_arn_match(block: str, policy_key: str, policy_value: Union[str, List[str]], context: CaseInsensitiveDict) -> bool:
"""Helper method for dealing with Arn* conditions: ArnEquals, ArnLike, ArnNotEquals, ArnNotLike"""
if_exists_op = 'IfExists' in block
for value in _listify_string(policy_value):
if 'Not' in block:
if policy_key not in context:
return True # policy simulator behavior: returns Allowed when context is null for given key in ArnNot*
for context_value in _listify_string(context[policy_key]):
if not arns.validate_arn(context_value):
return False # policy simulator behavior: reject if provided value isn't a legit ARN
if _matches_after_expansion(context_value, value):
return False
else:
if policy_key not in context:
return if_exists_op
for context_value in _listify_string(context[policy_key]):
if not arns.validate_arn(context_value):
continue # skip invalid arns
if _matches_after_expansion(context_value, value):
return True
# Made it through the loops without an answer, give default response
if 'Not' in block:
return True
else:
return False
def _get_null_match(policy_key: str, policy_value: Union[str, List[str]], context: CaseInsensitiveDict) -> bool:
"""Helper method for dealing with Null conditions"""
for value in _listify_string(policy_value):
if value == 'true': # key is expected not to be in context, or empty
if policy_key not in context or context[policy_key] == '':
return True
else: # key is expected to be in the context with a non-empty value
if policy_key in context and context[policy_key] != '':
return True
return False
def resource_policy_matching_statements(node_or_service: Union[Node, str], resource_policy: dict,
action_to_check: str, resource_to_check: str,
condition_keys_to_check: CaseInsensitiveDict) -> list:
"""Returns if a resource policy has a matching statement for a given service (ec2.amazonaws.com for example)."""
results = []
for statement in _listify_dictionary(resource_policy['Statement']):
matches_principal, matches_action, matches_resource, matches_condition = False, False, False, False
if 'Principal' in statement: # should be a dictionary
if statement['Principal'] == '*':
matches_principal = True
if isinstance(node_or_service, Node):
if 'AWS' in statement['Principal']:
if _principal_matches_in_statement(node_or_service, _listify_string(statement['Principal']['AWS'])):
matches_principal = True
else:
if 'Service' in statement['Principal']:
if node_or_service in _listify_string(statement['Principal']['Service']):
matches_principal = True
else: # 'NotPrincipal' in statement:
matches_principal = True
if isinstance(node_or_service, Node):
if 'AWS' in statement['NotPrincipal']:
if _principal_matches_in_statement(node_or_service, _listify_string(statement['NotPrincipal']['AWS'])):
matches_principal = False
else:
if 'Service' in statement['NotPrincipal']:
if node_or_service in _listify_string(statement['NotPrincipal']['Service']):
matches_principal = False
if not matches_principal:
continue
# if principal is good, proceed to check the Action, process depending on prefix
matches_action = _statement_matches_action(statement, action_to_check, condition_keys_to_check, True)
if not matches_action:
continue
# if action is good, proceed to check resource
matches_resource = _statement_matches_resource(statement, resource_to_check, condition_keys_to_check)
if not matches_resource:
continue
# if resource is good, check condition
if 'Condition' in statement:
matches_condition = _get_condition_match(statement['Condition'], condition_keys_to_check)
else:
matches_condition = True
if matches_principal and matches_action and matches_resource and matches_condition:
results.append(statement)
return results
class ResourcePolicyEvalResult(Enum):
"""For resource policy evaluation, we want to return a result for a few different potential set of results:
1. No match - depends on caller's permissions depending on the service
2. Deny statement match - node or root as Principal (explicit deny)
3. Caller in same account as resource: root Principal only
4. Caller in any account: node Principal matched
5. Caller in different account as resource: root Principal matched
6. Service matches
"""
NO_MATCH = 1
DENY_MATCH = 2
ROOT_MATCH = 3
NODE_MATCH = 4
DIFF_ACCOUNT_MATCH = 5
SERVICE_MATCH = 6
def resource_policy_authorization(node_or_service: Union[Node, str], resource_owner: str, resource_policy: dict,
action_to_check: str, resource_to_check: str,
condition_keys_to_check: Union[dict, CaseInsensitiveDict]) -> ResourcePolicyEvalResult:
"""Returns a ResourcePolicyEvalResult for a given request, based on the resource policy."""
if isinstance(condition_keys_to_check, dict):
prepped_condition_keys = CaseInsensitiveDict(condition_keys_to_check)
else:
prepped_condition_keys = condition_keys_to_check
matching_statements = resource_policy_matching_statements(node_or_service, resource_policy, action_to_check,
resource_to_check, prepped_condition_keys)
if len(matching_statements) == 0:
return ResourcePolicyEvalResult.NO_MATCH
# handle denies outright
for statement in matching_statements:
if statement['Effect'] == 'Deny':
return ResourcePolicyEvalResult.DENY_MATCH
# handle nodes (IAM Users or Roles)
if isinstance(node_or_service, Node):
# if in a different account, check for denies and wrap it up
if arns.get_account_id(node_or_service.arn) != resource_owner:
return ResourcePolicyEvalResult.DIFF_ACCOUNT_MATCH
else:
node_match = False
for statement in matching_statements:
if 'NotPrincipal' in statement:
# NotPrincipal means a node match (tested with S3)
node_match = True
elif isinstance(statement['Principal'], str) and statement['Principal'] == '*':
# Case: "Principal": "*"
node_match = True
else:
# dig through 'AWS' element of Principal for node-matching
if 'AWS' in statement['Principal']:
for aws_principal in _listify_string(statement['Principal']['AWS']):
if node_or_service.arn == aws_principal:
node_match = True
if node_match:
return ResourcePolicyEvalResult.NODE_MATCH
else:
return ResourcePolicyEvalResult.ROOT_MATCH
else:
return ResourcePolicyEvalResult.SERVICE_MATCH
def _principal_matches_in_statement(principal: Node, aws_principal_field: list):
"""Helper function for locally determining a principal matches a resource policy's statement"""
for value in aws_principal_field:
if principal.arn == value:
return True
elif principal.id_value == value:
return True
elif arns.get_account_id(principal.arn) == value:
return True
elif value == '*':
return True
else:
principal_root_str = 'arn:{}:iam::{}:root'.format(arns.get_partition(principal.arn),
arns.get_account_id(principal.arn))
if principal_root_str == value:
return True
return False
def policies_include_matching_allow_action(principal: Node, action_to_check: str) -> bool:
"""Helper function for online-testing. Does a 'light' scan of a principal's policies to determine if any of
their statements have an Allow statement with a matching action. Helps reduce unecessary API calls to
iam:SimulatePrincipalPolicy.
**NOTE:** Not something being used right now, given that all policy evaluation is local
"""
for policy in principal.attached_policies:
for statement in _listify_dictionary(policy.policy_doc['Statement']):
if statement['Effect'] != 'Allow':
continue
if 'Action' in statement:
for action in _listify_string(statement['Action']):
if _matches_after_expansion(action_to_check, action):
return True
else: # 'NotAction' in statement
return True # so broad that we'd need to simulate to make sure
return False
def _statement_matches_action(statement: dict, action: str, condition_keys: Optional[CaseInsensitiveDict] = None,
is_resource_policy_check: bool = False) -> bool:
"""Helper function, returns True if the given action is in the given policy statement"""
exempted_services = ('sns', 'sqs')
if 'Action' in statement:
for item in _listify_string(statement['Action']):
if not is_resource_policy_check:
if _matches_after_expansion(action, item, condition_keys):
return True
else:
if action.startswith('SNS') or action.startswith('SQS') or item.startswith('SNS') or item.startswith(
'SQS'):
action_parts = action.split(':')
item_parts = action.split(':')
new_action = '{}:{}'.format(action_parts[0].lower(), action_parts[1])
new_item = '{}:{}'.format(item_parts[0].lower(), item_parts[1])
if _matches_after_expansion(new_action, new_item, condition_keys):
return True
else:
if _matches_after_expansion(action, item, condition_keys):
return True
return False
elif 'NotAction' in statement:
result = True
for item in _listify_string(statement['NotAction']):
if not is_resource_policy_check:
if _matches_after_expansion(action, item, condition_keys):
result = False
break
else:
if action.startswith('SNS') or action.startswith('SQS') or item.startswith('SNS') or item.startswith(
'SQS'):
action_parts = action.split(':')
item_parts = action.split(':')
new_action = '{}:{}'.format(action_parts[0].lower(), action_parts[1])
new_item = '{}:{}'.format(item_parts[0].lower(), item_parts[1])
if _matches_after_expansion(new_action, new_item, condition_keys):
result = False
break
else:
if _matches_after_expansion(action, item, condition_keys):
result = False
break
return result
else:
return True
def _statement_matches_resource(statement: dict, resource: str, condition_keys: Optional[CaseInsensitiveDict] = None) -> bool:
"""Helper function, returns True if the given resource is in the given policy statement"""
if 'Resource' in statement:
for item in _listify_string(statement['Resource']):
if _matches_after_expansion(resource, item, condition_keys):
return True
return False
elif 'NotResource' in statement:
result = True
for item in _listify_string(statement['NotResource']):
if _matches_after_expansion(resource, item, condition_keys):
result = False
break
return result
else:
return True
@functools.lru_cache(maxsize=2048*64, typed=True)
def _compose_pattern(string_to_transform) -> Pattern:
"""Helper function that transforms a string with potential wildcards (* or ?) into a regular expression.
Uses the functools.lru_cache decorator to reduce re-compiling the same value multiple times."""
return re.compile(
"^{}$".format(
string_to_transform \
.replace(".", "\\.") \
.replace("*", ".*") \
.replace("?", ".") \
.replace("$", "\\$") \
.replace("^", "\\^")
),
re.IGNORECASE
)
def _matches_after_expansion(string_to_check: str, string_to_check_against: str,
condition_keys: Optional[CaseInsensitiveDict] = None) -> bool:
"""Helper function that checks the string_to_check against string_to_check_against.
Handles matching with respect to wildcards, variables.
"""
# regexify string_to_check_against
# handles use of ${} var substitution, wildcards (*), and periods (.)
copy_string = string_to_check_against
if condition_keys is not None:
for k, v in condition_keys.items():
if isinstance(v, list):
v = str(v) # TODO: how would a multi-valued context value be handled in resource fields?
full_key = '${' + k + '}'
copy_string = copy_string.replace(full_key, v)
pattern = _compose_pattern(copy_string)
return pattern.match(string_to_check) is not None
def _listify_dictionary(target_object: Union[List[Dict], Dict]) -> List[Dict]:
"""Helper function that takes a dictionary and returns it wrapped in a list"""
if isinstance(target_object, list):
return target_object
else:
return [target_object]
def _listify_string(target_object: Union[List[str], str]) -> List[str]:
"""Helper function that takes a string and returns it wrapped in a list"""
if isinstance(target_object, list):
return target_object
else:
return [target_object]