Skip to content

Commit 740b7d6

Browse files
committed
added script tool for exploring AWS SSO cache
1 parent 7127b2d commit 740b7d6

File tree

4 files changed

+1336
-2
lines changed

4 files changed

+1336
-2
lines changed

.pre-commit-config.yaml

-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ repos:
99
# Run the linter.
1010
- id: ruff
1111
types_or: [ python, pyi, jupyter ]
12-
args: [ --fix ]
1312
- repo: https://codeberg.org/frnmst/md-toc
1413
# Release updates (ATOM) https://codeberg.org/frnmst/md-toc/tags.atom
1514
rev: 9.0.0 # set a GIT tag

scripts/aws-sso.py

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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()

scripts/injinja.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# ruff: noqa: E501
2+
# ruff: noqa: I001
23
# /// script
34
# requires-python = ">=3.11"
45
# dependencies = [
@@ -43,11 +44,11 @@
4344
import logging
4445
import pathlib
4546
import sys
47+
import tomllib
4648
from typing import Any
4749

4850
# Third Party
4951
import jinja2
50-
import tomllib
5152
import yaml
5253

5354
log = logging.getLogger(__name__)

0 commit comments

Comments
 (0)