Skip to content

Ministack: Free, open-source local AWS emulator - 60+ services, Terraform compatible, real databases. Free forever. MIT licensed.

STS — a cross-account AssumeRole is refused, and an assumed session runs in the wrong account (#1616)

mm-salesquezecb29b2d52099
diff --git a/ministack/core/responses.py b/ministack/core/responses.py
index e81449e4..beca43d5 100644
--- a/ministack/core/responses.py
+++ b/ministack/core/responses.py
@@ -26,16 +26,49 @@ _request_account_id: contextvars.ContextVar[str] = contextvars.ContextVar(
_12_DIGIT_RE = re.compile(r"^\d{12}$")
+def _account_from_sts_session(access_key_id: str):
+ """The account an STS session belongs to, or None if this is not a session key.
+
+ An assumed-role session's key is ``ASIA...``, not 12 digits, so without this it falls to the
+ default account and the session operates in the WRONG TENANT: it sees none of the resources in
+ the account whose role it assumed, and any write lands in the default account instead. That
+ makes cross-account access — the ordinary way one account reaches another's resources —
+ silently not work, while AssumeRole itself reports success.
+
+ Imported lazily because `services.sts` imports this module.
+ """
+ try:
+ from ministack.services import sts as sts_svc
+
+ session = sts_svc._sessions.get(access_key_id)
+ if not session:
+ return None
+ # arn:aws:sts::<account>:assumed-role/<role>/<session>
+ parts = str(session.get("Arn", "")).split(":")
+ if len(parts) > 4 and _12_DIGIT_RE.match(parts[4]):
+ return parts[4]
+ except Exception:
+ pass
+ return None
+
+
def set_request_account_id(access_key_id: str) -> None:
"""Set the account ID for the current request from the access key.
If the access key is a 12-digit number, use it as the account ID.
+ An STS session key resolves to the account of the role it assumed.
Otherwise fall back to the MINISTACK_ACCOUNT_ID env var or 000000000000."""
if access_key_id and _12_DIGIT_RE.match(access_key_id):
_request_account_id.set(access_key_id)
- else:
- _request_account_id.set(
- os.environ.get("MINISTACK_ACCOUNT_ID", "000000000000")
- )
+ return
+
+ assumed = _account_from_sts_session(access_key_id) if access_key_id else None
+ if assumed:
+ _request_account_id.set(assumed)
+ return
+
+ _request_account_id.set(
+ os.environ.get("MINISTACK_ACCOUNT_ID", "000000000000")
+ )
def get_account_id() -> str:
diff --git a/ministack/services/sts.py b/ministack/services/sts.py
index e963d005..020395d2 100644
--- a/ministack/services/sts.py
+++ b/ministack/services/sts.py
@@ -114,7 +114,20 @@ async def handle_request(method, path, headers, body, query_params):
from ministack.services import iam as iam_svc
# Extract role name from ARN
role_name = role_arn.split("/")[-1] if "/" in role_arn else ""
- role = iam_svc._roles.get(role_name)
+ # Resolve the role in the account the ARN names, not the caller's. `_roles` is
+ # account-scoped, so a plain `.get()` looks in the CALLER's account — and a
+ # cross-account AssumeRole, where the role by definition lives somewhere else, then
+ # finds nothing and is refused with the role-missing message below (which reports an
+ # empty `User:`, since it never got as far as resolving the caller).
+ role_account = role_arn.split(":")[4] if role_arn.count(":") > 4 else ""
+ role = None
+ if role_account and role_account != get_account_id():
+ try:
+ role = iam_svc._roles.get_scoped(role_account, None, role_name)
+ except Exception:
+ role = None
+ if role is None:
+ role = iam_svc._roles.get(role_name)
if role is None:
return _error(403, "AccessDenied",
f"User: is not authorized to perform: sts:AssumeRole on resource: {role_arn}",