|
19 | 19 | get_action_data,
|
20 | 20 | get_actions_matching_condition_key,
|
21 | 21 | get_actions_with_arn_type_and_access_level,
|
22 |
| - get_actions_matching_arn_type |
| 22 | + get_actions_matching_arn_type, |
| 23 | + get_actions_that_support_wildcard_arns_only |
23 | 24 | )
|
24 | 25 | from policy_sentry.querying.conditions import (
|
25 | 26 | get_condition_keys_for_service,
|
|
32 | 33 | iam_definition_path = DATASTORE_FILE_PATH
|
33 | 34 |
|
34 | 35 |
|
| 36 | +def print_list(output, fmt="json"): |
| 37 | + """Common method on how to print a list, depending on whether the user requests JSON or YAML output""" |
| 38 | + print(yaml.dump(output)) if fmt == "yaml" else [ |
| 39 | + print(item) for item in output |
| 40 | + ] |
| 41 | + |
| 42 | + |
| 43 | +def print_dict(output, fmt="json"): |
| 44 | + """Common method on how to print a dict, depending on whether the user requests JSON or YAML output""" |
| 45 | + print(yaml.dump(output)) if fmt == "yaml" else [ |
| 46 | + print(json.dumps(output, indent=4)) |
| 47 | + ] |
| 48 | + |
| 49 | + |
35 | 50 | @click.group()
|
36 | 51 | def query():
|
37 | 52 | """Allow users to query the IAM tables from command line"""
|
@@ -113,63 +128,53 @@ def query_action_table(
|
113 | 128 | for serv in all_services:
|
114 | 129 | result = get_actions_with_access_level(serv, level)
|
115 | 130 | output.extend(result)
|
116 |
| - print(yaml.dump(output)) if fmt == "yaml" else [ |
117 |
| - print(result) for result in output |
118 |
| - ] |
| 131 | + print_list(output=output, fmt=fmt) |
119 | 132 | # Get a list of all services in the database
|
| 133 | + elif resource_type == "*": |
| 134 | + print("ALL actions that do not support resource ARN constraints") |
| 135 | + output = get_actions_that_support_wildcard_arns_only(service) |
| 136 | + print_dict(output=output, fmt=fmt) |
120 | 137 | else:
|
121 | 138 | print("All services in the database:\n")
|
122 | 139 | output = all_services
|
123 |
| - print(yaml.dump(output)) if fmt == "yaml" else [ |
124 |
| - print(item) for item in output |
125 |
| - ] |
| 140 | + print_list(output=output, fmt=fmt) |
126 | 141 | elif name is None and access_level and not resource_type:
|
127 | 142 | print(
|
128 | 143 | f"All IAM actions under the {service} service that have the access level {access_level}:"
|
129 | 144 | )
|
130 | 145 | level = transform_access_level_text(access_level)
|
131 | 146 | output = get_actions_with_access_level(service, level)
|
132 |
| - print(yaml.dump(output)) if fmt == "yaml" else [ |
133 |
| - print(json.dumps(output, indent=4)) |
134 |
| - ] |
| 147 | + print_dict(output=output, fmt=fmt) |
135 | 148 | elif name is None and access_level and resource_type:
|
136 | 149 | print(
|
137 | 150 | f"{service} {access_level.upper()} actions that have the resource type {resource_type.upper()}:"
|
138 | 151 | )
|
139 | 152 | access_level = transform_access_level_text(access_level)
|
140 | 153 | output = get_actions_with_arn_type_and_access_level(service, resource_type, access_level)
|
141 |
| - print(yaml.dump(output)) if fmt == "yaml" else [ |
142 |
| - print(json.dumps(output, indent=4)) |
143 |
| - ] |
| 154 | + print_dict(output=output, fmt=fmt) |
144 | 155 | # Get a list of all IAM actions under the service that support the specified condition key.
|
145 | 156 | elif condition:
|
146 | 157 | print(
|
147 | 158 | f"IAM actions under {service} service that support the {condition} condition only:"
|
148 | 159 | )
|
149 | 160 | output = get_actions_matching_condition_key(service, condition)
|
150 |
| - print(yaml.dump(output)) if fmt == "yaml" else [ |
151 |
| - print(json.dumps(output, indent=4)) |
152 |
| - ] |
| 161 | + print_dict(output=output, fmt=fmt) |
153 | 162 | # Get a list of IAM Actions under the service that only support resources = "*"
|
154 | 163 | # (i.e., you cannot restrict it according to ARN)
|
155 | 164 | elif resource_type:
|
156 | 165 | print(
|
157 | 166 | f"IAM actions under {service} service that have the resource type {resource_type}:"
|
158 | 167 | )
|
159 | 168 | output = get_actions_matching_arn_type(service, resource_type)
|
160 |
| - print(yaml.dump(output)) if fmt == "yaml" else [ |
161 |
| - print(json.dumps(output, indent=4)) |
162 |
| - ] |
| 169 | + print_dict(output=output, fmt=fmt) |
163 | 170 | elif name and access_level is None:
|
164 | 171 | output = get_action_data(service, name)
|
165 |
| - print(yaml.dump(output)) if fmt == "yaml" else [ |
166 |
| - print(json.dumps(output, indent=4)) |
167 |
| - ] |
| 172 | + print_dict(output=output, fmt=fmt) |
168 | 173 | else:
|
169 | 174 | # Get a list of all IAM Actions available to the service
|
170 | 175 | output = get_actions_for_service(service)
|
171 | 176 | print(f"ALL {service} actions:")
|
172 |
| - print(yaml.dump(output)) if fmt == "yaml" else [print(item) for item in output] |
| 177 | + print_list(output=output, fmt=fmt) |
173 | 178 | return output
|
174 | 179 |
|
175 | 180 |
|
@@ -225,20 +230,16 @@ def query_arn_table(name, service, list_arn_types, fmt):
|
225 | 230 | # Get a list of all RAW ARN formats available through the service.
|
226 | 231 | if name is None and list_arn_types is False:
|
227 | 232 | output = get_raw_arns_for_service(service)
|
228 |
| - print(yaml.dump(output)) if fmt == "yaml" else [print(item) for item in output] |
| 233 | + print_list(output=output, fmt=fmt) |
229 | 234 | # Get a list of all the ARN types per service, paired with the RAW ARNs
|
230 | 235 | elif name is None and list_arn_types:
|
231 | 236 | output = get_arn_types_for_service(service)
|
232 |
| - print(yaml.dump(output)) if fmt == "yaml" else [ |
233 |
| - print(json.dumps(output, indent=4)) |
234 |
| - ] |
| 237 | + print_dict(output=output, fmt=fmt) |
235 | 238 | # Get the raw ARN format for the `cloud9` service with the short name
|
236 | 239 | # `environment`
|
237 | 240 | else:
|
238 | 241 | output = get_arn_type_details(service, name)
|
239 |
| - print(yaml.dump(output)) if fmt == "yaml" else [ |
240 |
| - print(json.dumps(output, indent=4)) |
241 |
| - ] |
| 242 | + print_dict(output=output, fmt=fmt) |
242 | 243 | return output
|
243 | 244 |
|
244 | 245 |
|
@@ -287,11 +288,9 @@ def query_condition_table(name, service, fmt="json"):
|
287 | 288 | # Get a list of all condition keys available to the service
|
288 | 289 | if name is None:
|
289 | 290 | output = get_condition_keys_for_service(service)
|
290 |
| - print(yaml.dump(output)) if fmt == "yaml" else [print(item) for item in output] |
| 291 | + print_list(output=output, fmt=fmt) |
291 | 292 | # Get details on the specific condition key
|
292 | 293 | else:
|
293 | 294 | output = get_condition_key_details(service, name)
|
294 |
| - print(yaml.dump(output)) if fmt == "yaml" else [ |
295 |
| - print(json.dumps(output, indent=4)) |
296 |
| - ] |
| 295 | + print_dict(output=output, fmt=fmt) |
297 | 296 | return output
|
0 commit comments