|
| 1 | +# ruff: noqa: E501 |
| 2 | +# /// script |
| 3 | +# requires-python = ">=3.11" |
| 4 | +# dependencies = [ |
| 5 | +# ] |
| 6 | +# /// |
| 7 | +# https://docs.astral.sh/uv/guides/scripts/#creating-a-python-script |
| 8 | +# https://packaging.python.org/en/latest/specifications/inline-script-metadata/#inline-script-metadata |
| 9 | +# Standard Library |
| 10 | +import configparser |
| 11 | +import hashlib |
| 12 | +import logging |
| 13 | +from pathlib import Path |
| 14 | + |
| 15 | +log = logging.getLogger(__name__) |
| 16 | + |
| 17 | +aws_config = Path("~/.aws/").expanduser() |
| 18 | +aws_sso_cache = aws_config / "sso/cache" |
| 19 | +aws_credentials_file = aws_config / "credentials" |
| 20 | +aws_config_file = aws_config / "config" |
| 21 | + |
| 22 | +credentials = aws_credentials_file.read_text() |
| 23 | + |
| 24 | + |
| 25 | +def main(): |
| 26 | + cache_files = [p.stem for p in list(aws_sso_cache.glob("*.json"))] |
| 27 | + log.info(cache_files) |
| 28 | + |
| 29 | + parser = configparser.ConfigParser() |
| 30 | + parser.read(aws_config_file) |
| 31 | + for section in parser.sections(): |
| 32 | + log.info(f"Section: {section}") |
| 33 | + key = None |
| 34 | + hash = None |
| 35 | + |
| 36 | + if section.startswith("profile") and parser.has_option(section, "sso_session"): |
| 37 | + key = parser.get(section, "sso_session") |
| 38 | + |
| 39 | + elif section.startswith("sso-session") and parser.has_option(section, "sso_start_url"): |
| 40 | + key = parser.get(section, "sso_start_url") |
| 41 | + |
| 42 | + if key: |
| 43 | + hash_object = hashlib.sha1(key.encode()) |
| 44 | + hash = hash_object.hexdigest() |
| 45 | + log.info(hash) |
| 46 | + if hash in cache_files: |
| 47 | + log.info("Cache hit") |
| 48 | + log.info(f"{key} = {hash}.json") |
| 49 | + log.info((aws_sso_cache / f"{hash}.json").read_text()) |
| 50 | + else: |
| 51 | + log.info("Cache miss") |
| 52 | + |
| 53 | + # TODO: aws sso get-role-credentials --role-name <role-name> --account-id <account-id> --access-token <access-token> --region <region> |
| 54 | + # aws sso list-accounts --profile <profile> --no-paginate --access-token <access-token> --region <region> |
| 55 | + # aws sso list-account-roles --profile <profile> --no-paginate --access-token <access-token> --region <region> --account-id <account-id> |
| 56 | + |
| 57 | + |
| 58 | +if __name__ == "__main__": |
| 59 | + logging.basicConfig( |
| 60 | + level=logging.INFO, |
| 61 | + format="%(asctime)s::%(name)s::%(levelname)s::%(module)s:%(lineno)d| %(message)s", |
| 62 | + datefmt="%Y-%m-%d %H:%M:%S", |
| 63 | + ) |
| 64 | + main() |
0 commit comments