Created
February 29, 2024 10:29
-
-
Save lamchau/9a6f2aae949860201c9a6b18b7d2939b to your computer and use it in GitHub Desktop.
Convert AWS credentials into an easier format ot script with.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
from __future__ import annotations | |
import argparse | |
import configparser as ConfigParser | |
import json | |
import re | |
from enum import Enum | |
from pathlib import Path | |
AWS_CREDS_FILE = Path("~/.aws/credentials").expanduser() | |
class OutputMode(Enum): | |
JSON = "json" | |
TABLE = "table" | |
def parse_credentials( | |
filepath: Path, | |
) -> dict[str, int]: | |
""" | |
Parse AWS credentials file and return a dictionary of account names and ids. | |
Args: | |
filepath (Path): path to AWS credentials file (default: ~/.aws/credentials) | |
Returns: | |
dict[str, int]: dictionary of account names and ids | |
""" | |
config = ConfigParser.ConfigParser() | |
config.read(filepath.absolute()) | |
accounts: dict[str, int] = {} | |
for section in config.sections(): | |
# only sections with "credential_process" contain aws account ids | |
if not config.has_option(section, "credential_process"): | |
continue | |
credential_process = config.get(section, "credential_process") | |
account_id_matches = re.findall(r"(\d+)", credential_process) | |
if not account_id_matches: | |
continue | |
account_id = int(account_id_matches[0]) | |
accounts.setdefault(section, account_id) | |
return accounts | |
def render( | |
credentials: dict[str, int], | |
output_mode: OutputMode, | |
) -> None: | |
if not credentials: | |
print("No AWS credentials found.") | |
return | |
match output_mode: | |
case "json": | |
print( | |
json.dumps( | |
credentials, | |
indent=2, | |
sort_keys=True, | |
) | |
) | |
case "table": | |
max_account_len = 0 | |
max_account_id_len = 0 | |
for account, account_id in credentials.items(): | |
max_account_len = max(max_account_len, len(account)) | |
max_account_id_len = max(max_account_id_len, len(str(account_id))) | |
credentials = dict(sorted(credentials.items(), key=lambda item: item[0])) | |
credentials = {"account_id": "account"} | credentials | |
for account, account_id in credentials.items(): | |
print( | |
f"{account_id: >{max_account_id_len}} {account: <{max_account_len}}" | |
) | |
if __name__ == "__main__": | |
argparser = argparse.ArgumentParser( | |
prog="list-aws-creds", | |
description="List AWS account names and ids from credentials file.", | |
formatter_class=lambda prog: argparse.HelpFormatter(prog, max_help_position=30), | |
) | |
argparser.add_argument( | |
"--file", | |
type=Path, | |
nargs="?", | |
const=1, | |
default=AWS_CREDS_FILE, | |
help=f"path to AWS credentials file (default: {AWS_CREDS_FILE})", | |
) | |
argparser.add_argument( | |
"--output", | |
choices=[e.value for e in OutputMode], | |
help="output format (default: table)", | |
default=OutputMode.TABLE.value, | |
) | |
args = argparser.parse_args() | |
credentials = parse_credentials( | |
args.file, | |
) | |
render(credentials, output_mode=args.output) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment