21
21
* For managed policies: You can add up to 10 managed policies to a user, role, or group.
22
22
* The size of each managed policy cannot exceed 6,144 characters.
23
23
"""
24
+ from __future__ import annotations
25
+
24
26
import logging
25
27
import functools
26
28
27
- # from policyuniverse.expander_minimizer import _get_prefixes_for_action
28
-
29
29
logger = logging .getLogger (__name__ )
30
30
31
31
32
32
# Borrowed from policyuniverse to reduce size
33
33
# https://github.com/Netflix-Skunkworks/policyuniverse/blob/master/policyuniverse/expander_minimizer.py#L45
34
34
@functools .lru_cache (maxsize = 1024 )
35
- def _get_prefixes_for_action (action ) :
35
+ def _get_prefixes_for_action (action : str ) -> list [ str ] :
36
36
"""
37
37
:param action: iam:cat
38
38
:return: [ "iam:", "iam:c", "iam:ca", "iam:cat" ]
39
39
"""
40
- (technology , permission ) = action .split (":" )
41
- retval = ["{}:" .format (technology )]
42
- phrase = ""
43
- for char in permission :
44
- newphrase = "{}{}" .format (phrase , char )
45
- retval .append ("{}:{}" .format (technology , newphrase ))
46
- phrase = newphrase
40
+ technology , permission = action .split (":" )
41
+ retval = [f"{ technology } :{ permission [:i ]} " for i in range (len (permission ) + 1 )]
42
+
47
43
return retval
48
44
49
45
50
46
# Adapted version of policyuniverse's _get_denied_prefixes_from_desired, here:
51
47
# https://github.com/Netflix-Skunkworks/policyuniverse/blob/master/policyuniverse/expander_minimizer.py#L101
52
48
def get_denied_prefixes_from_desired (
53
- desired_actions , all_actions
54
- ): # pylint: disable=missing-function-docstring
49
+ desired_actions : list [ str ] , all_actions : set [ str ]
50
+ ) -> set [ str ] : # pylint: disable=missing-function-docstring
55
51
"""
56
52
Adapted version of policyuniverse's _get_denied_prefixes_from_desired, here: https://github.com/Netflix-Skunkworks/policyuniverse/blob/master/policyuniverse/expander_minimizer.py#L101
57
53
"""
58
54
denied_actions = all_actions .difference (desired_actions )
59
- denied_prefixes = set ()
60
- for denied_action in denied_actions :
61
- for denied_prefix in _get_prefixes_for_action (denied_action ):
62
- denied_prefixes .add (denied_prefix )
55
+ denied_prefixes = {
56
+ denied_prefix
57
+ for denied_action in denied_actions
58
+ for denied_prefix in _get_prefixes_for_action (denied_action )
59
+ }
63
60
64
61
return denied_prefixes
65
62
66
63
67
64
# Adapted version of policyuniverse's _check_permission_length. We are commenting out the skipping prefix message
68
65
# https://github.com/Netflix-Skunkworks/policyuniverse/blob/master/policyuniverse/expander_minimizer.py#L111
69
66
def check_min_permission_length (
70
- permission , minchars = None
71
- ): # pylint: disable=missing-function-docstring
67
+ permission : str , minchars : int | None = None
68
+ ) -> bool : # pylint: disable=missing-function-docstring
72
69
"""
73
70
Adapted version of policyuniverse's _check_permission_length. We are commenting out the skipping prefix message
74
71
https://github.com/Netflix-Skunkworks/policyuniverse/blob/master/policyuniverse/expander_minimizer.py#L111
75
72
"""
76
- if minchars and len (permission ) < int (minchars ) and permission != "" :
73
+ if minchars and permission and len (permission ) < int (minchars ):
77
74
# print(
78
75
# "Skipping prefix {} because length of {}".format(
79
76
# permission, len(permission)
@@ -87,8 +84,8 @@ def check_min_permission_length(
87
84
# This is a condensed version of policyuniverse's minimize_statement_actions, changed for our purposes.
88
85
# https://github.com/Netflix-Skunkworks/policyuniverse/blob/master/policyuniverse/expander_minimizer.py#L123
89
86
def minimize_statement_actions (
90
- desired_actions , all_actions , minchars = None
91
- ): # pylint: disable=missing-function-docstring
87
+ desired_actions : list [ str ] , all_actions : set [ str ] , minchars : int | None = None
88
+ ) -> list [ str ] : # pylint: disable=missing-function-docstring
92
89
"""
93
90
This is a condensed version of policyuniverse's minimize_statement_actions, changed for our purposes.
94
91
https://github.com/Netflix-Skunkworks/policyuniverse/blob/master/policyuniverse/expander_minimizer.py#L123
@@ -110,16 +107,16 @@ def minimize_statement_actions(
110
107
continue
111
108
# If the action name is not empty
112
109
if prefix not in denied_prefixes :
113
- if permission != "" :
110
+ if permission :
114
111
if prefix not in desired_actions :
115
- prefix = "{ }*". format ( prefix )
112
+ prefix = f" { prefix } *"
116
113
minimized_actions .add (prefix )
117
114
found_prefix = True
118
115
break
119
116
120
117
if not found_prefix :
121
118
logger .debug (
122
- "Could not suitable prefix. Defaulting to %s" . format ( prefixes [- 1 ])
119
+ f "Could not find suitable prefix. Defaulting to { prefixes [- 1 ]} "
123
120
)
124
121
minimized_actions .add (prefixes [- 1 ])
125
122
# sort the actions
0 commit comments