aws-annoying 0.1.0__tar.gz → 0.2.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (69) hide show
  1. aws_annoying-0.2.1/.devcontainer/.env.example +4 -0
  2. aws_annoying-0.2.1/.devcontainer/Dockerfile +3 -0
  3. {aws_annoying-0.1.0 → aws_annoying-0.2.1}/.devcontainer/devcontainer.json +7 -1
  4. aws_annoying-0.2.1/.devcontainer/docker-compose.devcontainer.yaml +30 -0
  5. {aws_annoying-0.1.0 → aws_annoying-0.2.1}/.devcontainer/onCreateCommand.sh +1 -1
  6. aws_annoying-0.2.1/.devcontainer/postAttachCommand.sh +8 -0
  7. {aws_annoying-0.1.0 → aws_annoying-0.2.1}/.pre-commit-config.yaml +4 -1
  8. {aws_annoying-0.1.0 → aws_annoying-0.2.1}/.vscode/settings.json +5 -1
  9. {aws_annoying-0.1.0 → aws_annoying-0.2.1}/Makefile +1 -1
  10. aws_annoying-0.2.1/PKG-INFO +38 -0
  11. {aws_annoying-0.1.0 → aws_annoying-0.2.1}/README.md +1 -1
  12. {aws_annoying-0.1.0 → aws_annoying-0.2.1}/aws_annoying/app.py +1 -0
  13. {aws_annoying-0.1.0 → aws_annoying-0.2.1}/aws_annoying/ecs_task_definition_lifecycle.py +3 -0
  14. aws_annoying-0.2.1/aws_annoying/load_variables.py +254 -0
  15. {aws_annoying-0.1.0 → aws_annoying-0.2.1}/aws_annoying/main.py +1 -0
  16. aws_annoying-0.2.1/aws_annoying/mfa/__init__.py +3 -0
  17. aws_annoying-0.2.1/aws_annoying/mfa/_app.py +9 -0
  18. aws_annoying-0.2.1/aws_annoying/mfa/configure.py +157 -0
  19. aws_annoying-0.2.1/console/ecs-exec/README.md +15 -0
  20. aws_annoying-0.2.1/console/ecs-exec/ecs-console.png +0 -0
  21. aws_annoying-0.2.1/console/ecs-exec/ecs-exec.user.js +186 -0
  22. aws_annoying-0.2.1/console/ecs-exec/session-manager.png +0 -0
  23. {aws_annoying-0.1.0 → aws_annoying-0.2.1}/pyproject.toml +21 -10
  24. aws_annoying-0.2.1/tests/_helpers/__init__.py +8 -0
  25. {aws_annoying-0.1.0 → aws_annoying-0.2.1}/tests/_helpers/string_.py +2 -2
  26. aws_annoying-0.2.1/tests/conftest.py +126 -0
  27. aws_annoying-0.2.1/tests/mfa/snapshots/test_configure/test_basic/persist/aws_config.ini +5 -0
  28. aws_annoying-0.2.1/tests/mfa/snapshots/test_configure/test_basic/persist/stdout.txt +3 -0
  29. aws_annoying-0.2.1/tests/mfa/snapshots/test_configure/test_basic/skip_persist/stdout.txt +3 -0
  30. aws_annoying-0.2.1/tests/mfa/snapshots/test_configure/test_load_existing_config/aws_config.ini +5 -0
  31. aws_annoying-0.2.1/tests/mfa/snapshots/test_configure/test_load_existing_config/stdout.txt +4 -0
  32. aws_annoying-0.2.1/tests/mfa/test_configure.py +116 -0
  33. aws_annoying-0.2.1/tests/snapshots/test_ecs_task_definition_lifecycle/test_basic/stdout.txt +15 -0
  34. aws_annoying-0.2.1/tests/snapshots/test_ecs_task_definition_lifecycle/test_dry_run/stdout.txt +16 -0
  35. aws_annoying-0.2.1/tests/snapshots/test_load_variables/test_basic/stdout.txt +13 -0
  36. aws_annoying-0.2.1/tests/snapshots/test_load_variables/test_dry_run/stdout.txt +17 -0
  37. aws_annoying-0.2.1/tests/snapshots/test_load_variables/test_env_prefix/stdout.txt +16 -0
  38. aws_annoying-0.2.1/tests/snapshots/test_load_variables/test_nothing/stdout.txt +1 -0
  39. aws_annoying-0.2.1/tests/snapshots/test_load_variables/test_overwrite_env/stdout.txt +16 -0
  40. aws_annoying-0.2.1/tests/snapshots/test_load_variables/test_replace_quiet/stdout.txt +4 -0
  41. aws_annoying-0.2.1/tests/snapshots/test_load_variables/test_resource_not_found/ssm/stdout.txt +9 -0
  42. aws_annoying-0.2.1/tests/snapshots/test_load_variables/test_unsupported_resource/stdout.txt +7 -0
  43. aws_annoying-0.2.1/tests/test_app.py +0 -0
  44. {aws_annoying-0.1.0 → aws_annoying-0.2.1}/tests/test_ecs_task_definition_lifecycle.py +28 -51
  45. aws_annoying-0.2.1/tests/test_load_variables.py +293 -0
  46. {aws_annoying-0.1.0 → aws_annoying-0.2.1}/uv.lock +304 -187
  47. aws_annoying-0.1.0/.devcontainer/postAttachCommand.sh +0 -6
  48. aws_annoying-0.1.0/PKG-INFO +0 -21
  49. aws_annoying-0.1.0/aws_annoying/load_variables.py +0 -244
  50. aws_annoying-0.1.0/tests/_helpers/__init__.py +0 -4
  51. aws_annoying-0.1.0/tests/conftest.py +0 -40
  52. aws_annoying-0.1.0/tests/test_load_variables.py +0 -342
  53. {aws_annoying-0.1.0 → aws_annoying-0.2.1}/.editorconfig +0 -0
  54. {aws_annoying-0.1.0 → aws_annoying-0.2.1}/.gitattributes +0 -0
  55. {aws_annoying-0.1.0 → aws_annoying-0.2.1}/.github/dependabot.yaml +0 -0
  56. {aws_annoying-0.1.0 → aws_annoying-0.2.1}/.github/workflows/ci.yaml +0 -0
  57. {aws_annoying-0.1.0 → aws_annoying-0.2.1}/.github/workflows/release.yaml +0 -0
  58. {aws_annoying-0.1.0 → aws_annoying-0.2.1}/.gitignore +0 -0
  59. {aws_annoying-0.1.0 → aws_annoying-0.2.1}/.python-version +0 -0
  60. {aws_annoying-0.1.0 → aws_annoying-0.2.1}/.vscode/extensions.json +0 -0
  61. {aws_annoying-0.1.0 → aws_annoying-0.2.1}/.vscode/launch.json +0 -0
  62. {aws_annoying-0.1.0 → aws_annoying-0.2.1}/LICENSE +0 -0
  63. {aws_annoying-0.1.0 → aws_annoying-0.2.1}/aws_annoying/__init__.py +0 -0
  64. {aws_annoying-0.1.0 → aws_annoying-0.2.1}/aws_annoying/utils/__init__.py +0 -0
  65. {aws_annoying-0.1.0 → aws_annoying-0.2.1}/aws_annoying/utils/debugger.py +0 -0
  66. {aws_annoying-0.1.0 → aws_annoying-0.2.1}/tests/__init__.py +0 -0
  67. {aws_annoying-0.1.0 → aws_annoying-0.2.1}/tests/_helpers/command_builder.py +0 -0
  68. {aws_annoying-0.1.0 → aws_annoying-0.2.1}/tests/_helpers/scripts/printenv.py +0 -0
  69. /aws_annoying-0.1.0/tests/test_app.py → /aws_annoying-0.2.1/tests/mfa/__init__.py +0 -0
@@ -0,0 +1,4 @@
1
+ # https://docs.docker.com/compose/how-tos/environment-variables/envvars/
2
+ COMPOSE_PROFILES=localstack
3
+
4
+ # See Docker Compose files for supported environment variables & profiles.
@@ -0,0 +1,3 @@
1
+ FROM mcr.microsoft.com/devcontainers/python:1-3.9-bookworm
2
+
3
+ RUN pipx install uv
@@ -1,8 +1,14 @@
1
1
  {
2
2
  "name": "lasuillard/aws-annoying",
3
- "image": "mcr.microsoft.com/devcontainers/python:1-3.9-bookworm",
3
+ "dockerComposeFile": [
4
+ "./docker-compose.devcontainer.yaml"
5
+ ],
6
+ "service": "workspace",
7
+ "overrideCommand": true,
8
+ "workspaceFolder": "/workspaces/aws-annoying",
4
9
  "features": {
5
10
  "ghcr.io/devcontainers-contrib/features/pre-commit:2": {},
11
+ "ghcr.io/devcontainers/features/docker-in-docker:2": {},
6
12
  "ghcr.io/devcontainers/features/aws-cli:1": {},
7
13
  "ghcr.io/lasuillard/devcontainer-features/aws-ssm-session-manager-plugin:0.1": {}
8
14
  },
@@ -0,0 +1,30 @@
1
+ services:
2
+ workspace:
3
+ build:
4
+ context: .
5
+ volumes:
6
+ - ..:/workspaces/aws-annoying
7
+ - op:/home/vscode/.config/op
8
+ - aws:/home/vscode/.aws
9
+ - cache:/home/vscode/.cache:delegated
10
+ environment:
11
+ AWS_ENDPOINT_URL: http://localstack:4566
12
+ healthcheck:
13
+ disable: true
14
+
15
+ localstack:
16
+ image: localstack/localstack:4
17
+ profiles:
18
+ - localstack
19
+ ports:
20
+ - ${LOCALSTACK_GATEWAY_HOST:-127.0.0.1}:${LOCALSTACK_GATEWAY_PORT:-4566}:4566
21
+ - ${LOCALSTACK_EXTERNAL_SERVICES_HOST:-127.0.0.1}:${LOCALSTACK_EXTERNAL_SERVICES_PORT_RANGE:-4510-4559}:4510-4559
22
+ volumes:
23
+ - localstack:/var/lib/localstack
24
+ - /var/run/docker.sock:/var/run/docker.sock
25
+
26
+ volumes:
27
+ op:
28
+ aws:
29
+ cache:
30
+ localstack:
@@ -1,3 +1,3 @@
1
1
  #!/usr/bin/env bash
2
2
 
3
- pipx install uv
3
+ make install
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+
3
+ # Shortcuts for development
4
+ echo 'alias aa="uv run aws-annoying"' >> ~/.bashrc
5
+
6
+ sudo chown --recursive "$(id --user):$(id --group)" ~
7
+ sudo chmod --recursive 600 ~/.config/op ~/.aws
8
+ sudo chmod --recursive u=rwX,g=,o= ~/.config/op ~/.aws
@@ -1,5 +1,6 @@
1
1
  default_stages: [pre-commit]
2
2
  fail_fast: true
3
+ exclude: tests/.*/?snapshots/.*
3
4
  repos:
4
5
  - repo: https://github.com/pre-commit/pre-commit-hooks
5
6
  rev: v5.0.0
@@ -19,6 +20,8 @@ repos:
19
20
  - utils/debugger.py
20
21
  - --extend-exclude
21
22
  - main.py
23
+ - --extend-exclude
24
+ - "**/_*.py"
22
25
  - id: test-matching-source
23
26
  args:
24
27
  - --target
@@ -57,4 +60,4 @@ repos:
57
60
  language: system
58
61
  types: [python]
59
62
  pass_filenames: false
60
- entry: uv run pytest
63
+ entry: uv run pytest -m unit
@@ -7,7 +7,10 @@
7
7
  "editor.formatOnSave": true
8
8
  },
9
9
  "autoDocstring.docstringFormat": "google-notypes",
10
- "cSpell.words": [],
10
+ "cSpell.words": [
11
+ "localstack",
12
+ "Tampermonkey"
13
+ ],
11
14
  "editor.formatOnSave": true,
12
15
  "files.eol": "\n",
13
16
  "files.exclude": {
@@ -26,6 +29,7 @@
26
29
  "python.testing.pytestArgs": [
27
30
  "--no-cov", // https://code.visualstudio.com/docs/python/testing#_pytest-configuration-settings
28
31
  "-vv",
32
+ "--snapshot-update"
29
33
  ],
30
34
  "python.testing.pytestEnabled": true,
31
35
  "python.testing.unittestEnabled": false,
@@ -46,7 +46,7 @@ lint: ## Run all linters
46
46
  .PHONY: lint
47
47
 
48
48
  test: ## Run tests
49
- uv run pytest
49
+ uv run pytest --snapshot-update
50
50
  .PHONY: test
51
51
 
52
52
  build: ## Build application
@@ -0,0 +1,38 @@
1
+ Metadata-Version: 2.4
2
+ Name: aws-annoying
3
+ Version: 0.2.1
4
+ Summary: Utils to handle some annoying AWS tasks.
5
+ Project-URL: Homepage, https://github.com/lasuillard/aws-annoying
6
+ Project-URL: Repository, https://github.com/lasuillard/aws-annoying.git
7
+ Project-URL: Issues, https://github.com/lasuillard/aws-annoying/issues
8
+ Author-email: Yuchan Lee <lasuillard@gmail.com>
9
+ License-Expression: MIT
10
+ License-File: LICENSE
11
+ Requires-Python: <4.0,>=3.9
12
+ Requires-Dist: boto3>=1.37.1
13
+ Requires-Dist: pydantic>=2.10.6
14
+ Requires-Dist: typer>=0.15.1
15
+ Provides-Extra: dev
16
+ Requires-Dist: boto3-stubs[ecs,secretsmanager,ssm,sts]>=1.37.1; extra == 'dev'
17
+ Requires-Dist: mypy~=1.15.0; extra == 'dev'
18
+ Requires-Dist: ruff<0.12.0,>=0.9.9; extra == 'dev'
19
+ Provides-Extra: test
20
+ Requires-Dist: coverage<7.9,>=7.6; extra == 'test'
21
+ Requires-Dist: moto[ecs,secretsmanager,server,ssm]~=5.1.1; extra == 'test'
22
+ Requires-Dist: pytest-cov~=6.0.0; extra == 'test'
23
+ Requires-Dist: pytest-env~=1.1.1; extra == 'test'
24
+ Requires-Dist: pytest-snapshot>=0.9.0; extra == 'test'
25
+ Requires-Dist: pytest-sugar~=1.0.0; extra == 'test'
26
+ Requires-Dist: pytest-xdist>=3.6.1; extra == 'test'
27
+ Requires-Dist: pytest~=8.3.2; extra == 'test'
28
+ Requires-Dist: testcontainers[localstack]>=4.9.2; extra == 'test'
29
+ Description-Content-Type: text/markdown
30
+
31
+ # aws-annoying
32
+
33
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
34
+ [![CI](https://github.com/lasuillard/aws-annoying/actions/workflows/ci.yaml/badge.svg)](https://github.com/lasuillard/aws-annoying/actions/workflows/ci.yaml)
35
+ [![codecov](https://codecov.io/gh/lasuillard/aws-annoying/graph/badge.svg?token=gbcHMVVz2k)](https://codecov.io/gh/lasuillard/aws-annoying)
36
+ ![PyPI - Version](https://img.shields.io/pypi/v/aws-annoying)
37
+
38
+ Utils to handle some annoying AWS tasks.
@@ -3,6 +3,6 @@
3
3
  [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
4
4
  [![CI](https://github.com/lasuillard/aws-annoying/actions/workflows/ci.yaml/badge.svg)](https://github.com/lasuillard/aws-annoying/actions/workflows/ci.yaml)
5
5
  [![codecov](https://codecov.io/gh/lasuillard/aws-annoying/graph/badge.svg?token=gbcHMVVz2k)](https://codecov.io/gh/lasuillard/aws-annoying)
6
- ![GitHub Release](https://img.shields.io/github/v/release/lasuillard/aws-annoying)
6
+ ![PyPI - Version](https://img.shields.io/pypi/v/aws-annoying)
7
7
 
8
8
  Utils to handle some annoying AWS tasks.
@@ -6,4 +6,5 @@ app = typer.Typer(
6
6
  pretty_exceptions_short=True,
7
7
  pretty_exceptions_show_locals=False,
8
8
  rich_markup_mode="rich",
9
+ no_args_is_help=True,
9
10
  )
@@ -28,6 +28,9 @@ def ecs_task_definition_lifecycle(
28
28
  ),
29
29
  ) -> None:
30
30
  """Execute ECS task definition lifecycle."""
31
+ if dry_run:
32
+ print("⚠️ Dry run mode enabled. Will not perform any actual changes.")
33
+
31
34
  ecs = boto3.client("ecs")
32
35
 
33
36
  # Get all task definitions for the family
@@ -0,0 +1,254 @@
1
+ # flake8: noqa: B008
2
+ from __future__ import annotations
3
+
4
+ import json
5
+ import os
6
+ import subprocess
7
+ from typing import Any, NoReturn, Optional
8
+
9
+ import boto3
10
+ import typer
11
+ from rich.console import Console
12
+ from rich.table import Table
13
+
14
+ from .app import app
15
+
16
+
17
+ @app.command(
18
+ context_settings={
19
+ # Allow extra arguments for user provided command
20
+ "allow_extra_args": True,
21
+ "ignore_unknown_options": True,
22
+ },
23
+ )
24
+ def load_variables( # noqa: PLR0913
25
+ *,
26
+ ctx: typer.Context,
27
+ arns: list[str] = typer.Option(
28
+ [],
29
+ metavar="ARN",
30
+ help=(
31
+ "ARNs of the secret or parameter to load."
32
+ " The variables are loaded in the order of the ARNs,"
33
+ " overwriting the variables with the same name in the order of the ARNs."
34
+ ),
35
+ ),
36
+ env_prefix: Optional[str] = typer.Option(
37
+ None,
38
+ help="Prefix of the environment variables to load the ARNs from.",
39
+ show_default=False,
40
+ ),
41
+ overwrite_env: bool = typer.Option(
42
+ False, # noqa: FBT003
43
+ help="Overwrite the existing environment variables with the same name.",
44
+ ),
45
+ quiet: bool = typer.Option(
46
+ False, # noqa: FBT003
47
+ help="Suppress all outputs from this command.",
48
+ ),
49
+ dry_run: bool = typer.Option(
50
+ False, # noqa: FBT003
51
+ help="Print the progress only. Neither load variables nor run the command.",
52
+ ),
53
+ replace: bool = typer.Option(
54
+ True, # noqa: FBT003
55
+ help=(
56
+ "Replace the current process (`os.execvpe`) with the command."
57
+ " If disabled, run the command as a `subprocess`."
58
+ ),
59
+ ),
60
+ ) -> NoReturn:
61
+ """Wrapper command to run command with variables from AWS resources injected as environment variables.
62
+
63
+ This script is intended to be used in the ECS environment, where currently AWS does not support
64
+ injecting whole JSON dictionary of secrets or parameters as environment variables directly.
65
+
66
+ It first loads the variables from the AWS sources then runs the command with the variables injected as environment variables.
67
+
68
+ In addition to `--arns` option, you can provide ARNs as the environment variables by providing `--env-prefix`.
69
+ For example, if you have the following environment variables:
70
+
71
+ ```shell
72
+ export LOAD_AWS_CONFIG__001_app_config=arn:aws:secretsmanager:...
73
+ export LOAD_AWS_CONFIG__002_db_config=arn:aws:ssm:...
74
+ ```
75
+
76
+ You can run the following command:
77
+
78
+ ```shell
79
+ aws-annoying load-variables --env-prefix LOAD_AWS_CONFIG__ -- ...
80
+ ```
81
+
82
+ The variables are loaded in the order of option provided, overwriting the variables with the same name in the order of the ARNs.
83
+ Existing environment variables are preserved by default, unless `--overwrite-env` is provided.
84
+ """ # noqa: E501
85
+ console = Console(quiet=quiet, emoji=False)
86
+
87
+ command = ctx.args
88
+ if not command:
89
+ console.print("⚠️ No command provided. Exiting...")
90
+ raise typer.Exit(0)
91
+
92
+ # Mapping of the ARNs by index (index used for ordering)
93
+ map_arns_by_index = {str(idx): arn for idx, arn in enumerate(arns)}
94
+ if env_prefix:
95
+ console.print(f"🔍 Loading ARNs from environment variables with prefix: {env_prefix!r}")
96
+ arns_env = {
97
+ key.removeprefix(env_prefix): value for key, value in os.environ.items() if key.startswith(env_prefix)
98
+ }
99
+ console.print(f"🔍 Found {len(arns_env)} sources from environment variables.")
100
+ map_arns_by_index = arns_env | map_arns_by_index
101
+
102
+ # Briefly show the ARNs
103
+ table = Table("Index", "ARN")
104
+ for idx, arn in sorted(map_arns_by_index.items()):
105
+ table.add_row(idx, arn)
106
+
107
+ console.print(table)
108
+
109
+ # Retrieve the variables
110
+ loader = VariableLoader(dry_run=dry_run, console=console)
111
+ try:
112
+ variables = loader.load(map_arns_by_index)
113
+ except Exception as exc: # noqa: BLE001
114
+ console.print(f"❌ Failed to load the variables: {exc!s}")
115
+ raise typer.Exit(1) from None
116
+
117
+ # Prepare the environment variables
118
+ env = os.environ.copy()
119
+ if overwrite_env:
120
+ env.update(variables)
121
+ else:
122
+ # Update variables, preserving the existing ones
123
+ for key, value in variables.items():
124
+ env.setdefault(key, str(value))
125
+
126
+ # Run the command with the variables injected as environment variables, replacing current process
127
+ console.print(f"🚀 Running the command: [bold orchid]{' '.join(command)}[/bold orchid]")
128
+ if replace: # pragma: no cover (not coverable)
129
+ os.execvpe(command[0], command, env=env) # noqa: S606
130
+ # The above line should never return
131
+
132
+ result = subprocess.run(command, env=env, check=False) # noqa: S603
133
+ raise typer.Exit(result.returncode)
134
+
135
+
136
+ # Type aliases for readability
137
+ _ARN = str
138
+ _Variables = dict[str, Any]
139
+
140
+
141
+ class VariableLoader: # noqa: D101
142
+ def __init__(self, *, console: Console | None = None, dry_run: bool) -> None:
143
+ """Initialize the VariableLoader.
144
+
145
+ Args:
146
+ dry_run: Whether to run in dry-run mode.
147
+ console: Rich console instance.
148
+ """
149
+ self.console = console or Console(quiet=True)
150
+ self.dry_run = dry_run
151
+
152
+ # TODO(lasuillard): Currently not using pagination (do we need more than 10-20 secrets or parameters each?)
153
+ # ; consider adding it if needed
154
+ def load(self, map_arns: dict[str, _ARN]) -> dict[str, Any]:
155
+ """Load the variables from the AWS Secrets Manager and SSM Parameter Store.
156
+
157
+ Each secret or parameter should be a valid dictionary, where the keys are the variable names
158
+ and the values are the variable values.
159
+
160
+ The items are merged in the order of the key of provided mapping, overwriting the variables with the same name
161
+ in the order of the keys.
162
+ """
163
+ self.console.print("🔍 Retrieving variables from AWS resources...")
164
+ if self.dry_run:
165
+ self.console.print("⚠️ Dry run mode enabled. Variables won't be loaded from AWS.")
166
+
167
+ # Split the ARNs by resource types
168
+ secrets_map, parameters_map = {}, {}
169
+ for idx, arn in map_arns.items():
170
+ if arn.startswith("arn:aws:secretsmanager:"):
171
+ secrets_map[idx] = arn
172
+ elif arn.startswith("arn:aws:ssm:"):
173
+ parameters_map[idx] = arn
174
+ else:
175
+ msg = f"Unsupported resource: {arn!r}"
176
+ raise ValueError(msg)
177
+
178
+ # Retrieve variables from AWS resources
179
+ secrets: dict[str, _Variables]
180
+ parameters: dict[str, _Variables]
181
+ if self.dry_run:
182
+ secrets = {idx: {} for idx, _ in secrets_map.items()}
183
+ parameters = {idx: {} for idx, _ in parameters_map.items()}
184
+ else:
185
+ secrets = self._retrieve_secrets(secrets_map)
186
+ parameters = self._retrieve_parameters(parameters_map)
187
+
188
+ self.console.print(f"✅ Retrieved {len(secrets)} secrets and {len(parameters)} parameters.")
189
+
190
+ # Merge the variables in order
191
+ full_variables = secrets | parameters # Keys MUST NOT conflict
192
+ merged_in_order = {}
193
+ for _, variables in sorted(full_variables.items()):
194
+ merged_in_order.update(variables)
195
+
196
+ return merged_in_order
197
+
198
+ def _retrieve_secrets(self, secrets_map: dict[str, _ARN]) -> dict[str, _Variables]:
199
+ """Retrieve the secrets from AWS Secrets Manager."""
200
+ if not secrets_map:
201
+ return {}
202
+
203
+ secretsmanager = boto3.client("secretsmanager")
204
+
205
+ # Retrieve the secrets
206
+ arns = list(secrets_map.values())
207
+ response = secretsmanager.batch_get_secret_value(SecretIdList=arns)
208
+ if errors := response["Errors"]:
209
+ msg = f"Failed to retrieve secrets: {errors!r}"
210
+ raise ValueError(msg)
211
+
212
+ # Parse the secrets
213
+ secrets = response["SecretValues"]
214
+ result = {}
215
+ for secret in secrets:
216
+ arn = secret["ARN"]
217
+ order_key = next(key for key, value in secrets_map.items() if value == arn)
218
+ data = json.loads(secret["SecretString"])
219
+ if not isinstance(data, dict):
220
+ msg = f"Secret data must be a valid dictionary, but got: {type(data)!r}"
221
+ raise TypeError(msg)
222
+
223
+ result[order_key] = data
224
+
225
+ return result
226
+
227
+ def _retrieve_parameters(self, parameters_map: dict[str, _ARN]) -> dict[str, _Variables]:
228
+ """Retrieve the parameters from AWS SSM Parameter Store."""
229
+ if not parameters_map:
230
+ return {}
231
+
232
+ ssm = boto3.client("ssm")
233
+
234
+ # Retrieve the parameters
235
+ parameter_names = list(parameters_map.values())
236
+ response = ssm.get_parameters(Names=parameter_names, WithDecryption=True)
237
+ if errors := response["InvalidParameters"]:
238
+ msg = f"Failed to retrieve parameters: {errors!r}"
239
+ raise ValueError(msg)
240
+
241
+ # Parse the parameters
242
+ parameters = response["Parameters"]
243
+ result = {}
244
+ for parameter in parameters:
245
+ arn = parameter["ARN"]
246
+ order_key = next(key for key, value in parameters_map.items() if value == arn)
247
+ data = json.loads(parameter["Value"])
248
+ if not isinstance(data, dict):
249
+ msg = f"Parameter data must be a valid dictionary, but got: {type(data)!r}"
250
+ raise TypeError(msg)
251
+
252
+ result[order_key] = data
253
+
254
+ return result
@@ -3,6 +3,7 @@ from __future__ import annotations
3
3
 
4
4
  import aws_annoying.ecs_task_definition_lifecycle
5
5
  import aws_annoying.load_variables
6
+ import aws_annoying.mfa
6
7
  from aws_annoying.utils.debugger import input_as_args
7
8
 
8
9
  # App with all commands registered
@@ -0,0 +1,3 @@
1
+ from . import configure
2
+
3
+ __all__ = ("configure",)
@@ -0,0 +1,9 @@
1
+ import typer
2
+
3
+ from aws_annoying.app import app
4
+
5
+ mfa_app = typer.Typer(
6
+ no_args_is_help=True,
7
+ help="Commands to manage MFA authentication.",
8
+ )
9
+ app.add_typer(mfa_app, name="mfa")
@@ -0,0 +1,157 @@
1
+ from __future__ import annotations
2
+
3
+ import configparser
4
+ from pathlib import Path # noqa: TC003
5
+ from typing import Optional
6
+
7
+ import boto3
8
+ import typer
9
+ from pydantic import BaseModel, ConfigDict
10
+ from rich import print # noqa: A004
11
+ from rich.prompt import Prompt
12
+
13
+ from ._app import mfa_app
14
+
15
+ _CONFIG_INI_SECTION = "aws-annoying:mfa"
16
+
17
+
18
+ @mfa_app.command()
19
+ def configure( # noqa: PLR0913
20
+ *,
21
+ mfa_profile: Optional[str] = typer.Option(
22
+ None,
23
+ help="The MFA profile to configure.",
24
+ ),
25
+ mfa_source_profile: Optional[str] = typer.Option(
26
+ None,
27
+ help="The AWS profile to use to retrieve MFA credentials.",
28
+ ),
29
+ mfa_serial_number: Optional[str] = typer.Option(
30
+ None,
31
+ help="The MFA device serial number. It is required if not persisted in configuration.",
32
+ show_default=False,
33
+ ),
34
+ mfa_token_code: Optional[str] = typer.Option(
35
+ None,
36
+ help="The MFA token code.",
37
+ show_default=False,
38
+ ),
39
+ aws_credentials: Path = typer.Option( # noqa: B008
40
+ "~/.aws/credentials",
41
+ help="The path to the AWS credentials file.",
42
+ ),
43
+ aws_config: Path = typer.Option( # noqa: B008
44
+ "~/.aws/config",
45
+ help="The path to the AWS config file. Used to persist the MFA configuration.",
46
+ ),
47
+ persist: bool = typer.Option(
48
+ True, # noqa: FBT003
49
+ help="Persist the MFA configuration.",
50
+ ),
51
+ ) -> None:
52
+ """Configure AWS profile for MFA."""
53
+ # Expand user home directory
54
+ aws_credentials = aws_credentials.expanduser()
55
+ aws_config = aws_config.expanduser()
56
+
57
+ # Load configuration
58
+ mfa_config, exists = _MfaConfig.from_ini_file(aws_config, _CONFIG_INI_SECTION)
59
+ if exists:
60
+ print(f"⚙️ Loaded MFA configuration from AWS config ({aws_config}).")
61
+
62
+ mfa_profile = (
63
+ mfa_profile
64
+ or mfa_config.mfa_profile
65
+ # _
66
+ or Prompt.ask("👤 Enter name of MFA profile to configure", default="mfa")
67
+ )
68
+ mfa_source_profile = (
69
+ mfa_source_profile
70
+ or mfa_config.mfa_source_profile
71
+ or Prompt.ask("👤 Enter AWS profile to use to retrieve MFA credentials", default="default")
72
+ )
73
+ mfa_serial_number = (
74
+ mfa_serial_number
75
+ or mfa_config.mfa_serial_number
76
+ # _
77
+ or Prompt.ask("🔒 Enter MFA serial number")
78
+ )
79
+ mfa_token_code = (
80
+ mfa_token_code
81
+ # _
82
+ or Prompt.ask("🔑 Enter MFA token code")
83
+ )
84
+
85
+ # Get credentials
86
+ print(f"💬 Retrieving MFA credentials using profile [bold]{mfa_source_profile}[/bold]")
87
+ session = boto3.session.Session(profile_name=mfa_source_profile)
88
+ sts = session.client("sts")
89
+ response = sts.get_session_token(
90
+ SerialNumber=mfa_serial_number,
91
+ TokenCode=mfa_token_code,
92
+ )
93
+ credentials = response["Credentials"]
94
+
95
+ # Update MFA profile in AWS credentials
96
+ print(f"✅ Updating MFA profile ([bold]{mfa_profile}[/bold]) to AWS credentials ({aws_credentials})")
97
+ _update_credentials(
98
+ aws_credentials,
99
+ mfa_profile, # type: ignore[arg-type]
100
+ access_key=credentials["AccessKeyId"],
101
+ secret_key=credentials["SecretAccessKey"],
102
+ session_token=credentials["SessionToken"],
103
+ )
104
+
105
+ # Persist MFA configuration
106
+ if persist:
107
+ print(
108
+ f"✅ Persisting MFA configuration in AWS config ({aws_config}),"
109
+ f" in [bold]{_CONFIG_INI_SECTION}[/bold] section.",
110
+ )
111
+ mfa_config.mfa_profile = mfa_profile
112
+ mfa_config.mfa_source_profile = mfa_source_profile
113
+ mfa_config.mfa_serial_number = mfa_serial_number
114
+ mfa_config.save_ini_file(aws_config, _CONFIG_INI_SECTION)
115
+ else:
116
+ print("⚠️ MFA configuration not persisted.")
117
+
118
+
119
+ class _MfaConfig(BaseModel):
120
+ model_config = ConfigDict(extra="ignore")
121
+
122
+ mfa_profile: Optional[str] = None
123
+ mfa_source_profile: Optional[str] = None
124
+ mfa_serial_number: Optional[str] = None
125
+
126
+ def save_ini_file(self, path: Path, section_key: str) -> None:
127
+ """Save configuration to an AWS config file."""
128
+ config_ini = configparser.ConfigParser()
129
+ config_ini.read(path)
130
+ config_ini.setdefault(section_key, {})
131
+ for k, v in self.model_dump(exclude_none=True).items():
132
+ config_ini[section_key][k] = v
133
+
134
+ with path.open("w") as f:
135
+ config_ini.write(f)
136
+
137
+ @classmethod
138
+ def from_ini_file(cls, path: Path, section_key: str) -> tuple[_MfaConfig, bool]:
139
+ """Load configuration from an AWS config file, with boolean indicating if the config already exists."""
140
+ config_ini = configparser.ConfigParser()
141
+ config_ini.read(path)
142
+ if config_ini.has_section(section_key):
143
+ section = dict(config_ini.items(section_key))
144
+ return cls.model_validate(section), True
145
+
146
+ return cls(), False
147
+
148
+
149
+ def _update_credentials(path: Path, profile: str, *, access_key: str, secret_key: str, session_token: str) -> None:
150
+ credentials_ini = configparser.ConfigParser()
151
+ credentials_ini.read(path)
152
+ credentials_ini.setdefault(profile, {})
153
+ credentials_ini[profile]["aws_access_key_id"] = access_key
154
+ credentials_ini[profile]["aws_secret_access_key"] = secret_key
155
+ credentials_ini[profile]["aws_session_token"] = session_token
156
+ with path.open("w") as f:
157
+ credentials_ini.write(f)
@@ -0,0 +1,15 @@
1
+ # ECS Exec
2
+
3
+ This [Tampermonkey](https://www.tampermonkey.net/) user script adds a shortcut to the Session Manager.
4
+
5
+ Currently, AWS does not support accessing containers from the web console like EC2. However, you can access a container via the Session Manager:
6
+
7
+ ![Session Manager](session-manager.png)
8
+
9
+ Because the `aws ssm start-session` command's `--target` parameter [can also take a value in the format `ecs:<cluster-name>_<task-id>_<container-runtime-id>`](https://stackoverflow.com/a/67641633), we can use it to access containers from the web.
10
+
11
+ This script attaches a link to the Session Manager in the **Container name** column.
12
+
13
+ ![ECS Console](ecs-console.png)
14
+
15
+ To access the container, ECS Exec should be configured. If it is not configured, please refer to [the official documentation](https://docs.aws.amazon.com/AmazonECS/latest/developerguide/ecs-exec.html).