|
11 | 11 |
|
12 | 12 | import click
|
13 | 13 | import yaml
|
| 14 | + |
| 15 | +from policy_sentry.querying.services import get_services_data |
14 | 16 | from policy_sentry.util.access_levels import transform_access_level_text
|
15 | 17 | from policy_sentry.querying.all import get_all_service_prefixes
|
16 | 18 | from policy_sentry.querying.arns import (
|
@@ -47,8 +49,17 @@ def print_list(output: Any, fmt: str = "json") -> None:
|
47 | 49 |
|
48 | 50 |
|
49 | 51 | def print_dict(output: Any, fmt: str = "json") -> None:
|
50 |
| - """Common method on how to print a dict, depending on whether the user requests JSON or YAML output""" |
51 |
| - print(yaml.dump(output)) if fmt == "yaml" else [print(json.dumps(output, indent=4))] |
| 52 | + """Common method on how to print a dict, depending on whether the user requests JSON, YAML or CSV output""" |
| 53 | + if fmt == "csv": |
| 54 | + if not output: |
| 55 | + return None |
| 56 | + print(",".join(output[0].keys())) |
| 57 | + for entry in output: |
| 58 | + print(",".join(entry.values())) |
| 59 | + elif fmt == "json": |
| 60 | + print(json.dumps(output, indent=4)) |
| 61 | + elif fmt == "yaml": |
| 62 | + print(yaml.dump(output)) |
52 | 63 |
|
53 | 64 |
|
54 | 65 | @click.group()
|
@@ -97,7 +108,7 @@ def query() -> None:
|
97 | 108 | type=click.Choice(["yaml", "json"]),
|
98 | 109 | default="json",
|
99 | 110 | required=False,
|
100 |
| - help='Format output as YAML or JSON. Defaults to "yaml"', |
| 111 | + help='Format output as YAML or JSON. Defaults to "json"', |
101 | 112 | )
|
102 | 113 | @click.option(
|
103 | 114 | "--verbose",
|
@@ -229,7 +240,7 @@ def query_action_table(
|
229 | 240 | type=click.Choice(["yaml", "json"]),
|
230 | 241 | default="json",
|
231 | 242 | required=False,
|
232 |
| - help='Format output as YAML or JSON. Defaults to "yaml"', |
| 243 | + help='Format output as YAML or JSON. Defaults to "json"', |
233 | 244 | )
|
234 | 245 | @click.option(
|
235 | 246 | "--verbose",
|
@@ -296,7 +307,7 @@ def query_arn_table(
|
296 | 307 | type=click.Choice(["yaml", "json"]),
|
297 | 308 | default="json",
|
298 | 309 | required=False,
|
299 |
| - help='Format output as YAML or JSON. Defaults to "yaml"', |
| 310 | + help='Format output as YAML or JSON. Defaults to "json"', |
300 | 311 | )
|
301 | 312 | @click.option(
|
302 | 313 | "--verbose",
|
@@ -334,3 +345,42 @@ def query_condition_table(
|
334 | 345 | output = get_condition_key_details(service, name)
|
335 | 346 | print_dict(output=output, fmt=fmt)
|
336 | 347 | return output
|
| 348 | + |
| 349 | + |
| 350 | +@query.command(short_help="Query the service table.") |
| 351 | +@click.option( |
| 352 | + "--fmt", |
| 353 | + type=click.Choice(["yaml", "json", "csv"]), |
| 354 | + default="json", |
| 355 | + required=False, |
| 356 | + help='Format output as YAML, JSON or CSV. Defaults to "json"', |
| 357 | +) |
| 358 | +@click.option( |
| 359 | + "--verbose", |
| 360 | + "-v", |
| 361 | + type=click.Choice( |
| 362 | + ["critical", "error", "warning", "info", "debug"], case_sensitive=False |
| 363 | + ), |
| 364 | +) |
| 365 | +def service_table(fmt: str, verbose: str | None) -> None: |
| 366 | + """Query the service table from the Policy Sentry database""" |
| 367 | + if verbose: |
| 368 | + log_level = getattr(logging, verbose.upper()) |
| 369 | + set_stream_logger(level=log_level) |
| 370 | + query_service_table(fmt) |
| 371 | + |
| 372 | + |
| 373 | +def query_service_table(fmt: str = "json") -> list[dict[str, str]]: |
| 374 | + """Query the service table from the Policy Sentry database. |
| 375 | + Use this one when leveraging Policy Sentry as a library.""" |
| 376 | + if os.path.exists(LOCAL_DATASTORE_FILE_PATH): |
| 377 | + logger.info( |
| 378 | + f"Using the Local IAM definition: {LOCAL_DATASTORE_FILE_PATH}. To leverage the bundled definition instead, remove the folder $HOME/.policy_sentry/" |
| 379 | + ) |
| 380 | + else: |
| 381 | + # Otherwise, leverage the datastore inside the python package |
| 382 | + logger.debug("Leveraging the bundled IAM Definition.") |
| 383 | + |
| 384 | + output = get_services_data() |
| 385 | + print_dict(output=output, fmt=fmt) |
| 386 | + return output |
0 commit comments