Skip to content

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

fix(sts): GetCallerIdentity resolves IAM-user callers instead of root (#1830)

Adrian-Ryan Acala6209d7ac1338
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 537251bc..77fcdc5f 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -10,6 +10,7 @@ Versioning follows [Semantic Versioning](https://semver.org/).
### Fixed
- **DynamoDB — `DescribeTable` accepts a table ARN** — `TableName` given as the table's ARN answered `ResourceNotFoundException`, where AWS accepts either form. It now resolves through the same ARN normalization the other table operations use, and an ARN from another account or region still answers `ResourceNotFoundException`. This unblocks OpenSearch Data Prepper's DynamoDB source, which always describes the table by ARN.
+- **STS — `GetCallerIdentity` resolves IAM-user callers** — keys created with `CreateAccessKey` returned the `root` ARN; they now return the user's ARN and ID in both XML and JSON protocols. Under `AUTH=true`, unknown and inactive keys are rejected instead of reported as root.
## [1.5.17] — 2026-09-25
diff --git a/ministack/services/sts.py b/ministack/services/sts.py
index 35ead80e..613368ce 100644
--- a/ministack/services/sts.py
+++ b/ministack/services/sts.py
@@ -151,29 +151,24 @@ async def handle_request(method, path, headers, body, query_params):
use_json = "amz-json" in content_type
if action == "GetCallerIdentity":
- auth = headers.get("authorization", "")
- caller_arn = f"arn:aws:iam::{get_account_id()}:root"
- caller_user_id = get_account_id()
- if "Credential=" in auth:
- try:
- access_key = auth.split("Credential=")[1].split("/")[0]
- if access_key in _sessions:
- session = _sessions[access_key]
- if _session_expired(session):
- return _error(403, "ExpiredToken",
- "The security token included in the request is expired",
- ns="sts")
- caller_arn = session["Arn"]
- caller_user_id = session["UserId"]
- except Exception:
- pass
+ access_key = extract_access_key_id(headers, query_params)
+ if access_key in _sessions and _session_expired(_sessions[access_key]):
+ return _error(403, "ExpiredToken",
+ "The security token included in the request is expired",
+ ns="sts")
+ caller = _caller_identity(headers, query_params)
+ if isinstance(caller, CredentialResolutionError):
+ return _credential_error_response(caller)
+ caller_arn = caller["userArn"]
+ caller_user_id = caller["userId"]
+ caller_account = caller["accountId"]
if use_json:
- return json_response({"Account": get_account_id(), "Arn": caller_arn, "UserId": caller_user_id})
+ return json_response({"Account": caller_account, "Arn": caller_arn, "UserId": caller_user_id})
return _xml(200, "GetCallerIdentityResponse",
f"<GetCallerIdentityResult>"
f"<Arn>{caller_arn}</Arn>"
f"<UserId>{caller_user_id}</UserId>"
- f"<Account>{get_account_id()}</Account>"
+ f"<Account>{caller_account}</Account>"
f"</GetCallerIdentityResult>",
ns="sts")
diff --git a/tests/test_sts.py b/tests/test_sts.py
index dd23eeab..77ee1e3e 100644
--- a/tests/test_sts.py
+++ b/tests/test_sts.py
@@ -1,3 +1,4 @@
+import contextlib
import json
import os
@@ -614,3 +615,156 @@ def test_sts_assume_role_foreign_arn_never_falls_back_to_caller(monkeypatch):
assert status == 403
assert b"AccessDenied" in payload
iam_svc._roles.pop_scoped("111111111111", None, "Trap", None)
+
+
+def _iam_user_key(iam, user_name):
+ """Create a user + access key, delete both on exit."""
+ user = iam.create_user(UserName=user_name)["User"]
+ key = iam.create_access_key(UserName=user_name)["AccessKey"]
+ try:
+ yield user, key
+ finally:
+ iam.delete_access_key(UserName=user_name, AccessKeyId=key["AccessKeyId"])
+ iam.delete_user(UserName=user_name)
+
+
+def test_sts_get_caller_identity_resolves_iam_user(iam):
+ """GetCallerIdentity with an IAM-user key returns the user ARN, not root."""
+ import boto3
+
+ endpoint = os.environ.get("MINISTACK_ENDPOINT", "http://localhost:4566")
+ with _iam_user_key(iam, "caller-identity-user") as (created, source):
+ user_sts = boto3.client(
+ "sts",
+ endpoint_url=endpoint,
+ region_name="us-east-1",
+ aws_access_key_id=source["AccessKeyId"],
+ aws_secret_access_key=source["SecretAccessKey"],
+ )
+ identity = user_sts.get_caller_identity()
+
+ assert identity["Arn"] == "arn:aws:iam::000000000000:user/caller-identity-user"
+ assert identity["UserId"] == created["UserId"]
+ assert identity["Account"] == "000000000000"
+
+
+def test_sts_get_caller_identity_json_protocol_resolves_iam_user(iam):
+ """JSON protocol renders the resolved IAM-user identity, not root."""
+ import urllib.request
+
+ endpoint = os.environ.get("MINISTACK_ENDPOINT", "http://localhost:4566")
+ with _iam_user_key(iam, "caller-identity-json-user") as (_, source):
+ req = urllib.request.Request(
+ endpoint,
+ data=json.dumps({}).encode(),
+ method="POST",
+ headers={
+ "Content-Type": "application/x-amz-json-1.1",
+ "X-Amz-Target": "AWSSecurityTokenServiceV20110615.GetCallerIdentity",
+ "Authorization": (
+ "AWS4-HMAC-SHA256 "
+ f"Credential={source['AccessKeyId']}/20240101/us-east-1/sts/aws4_request, "
+ "SignedHeaders=host, Signature=fake"
+ ),
+ },
+ )
+ with urllib.request.urlopen(req) as r:
+ assert r.status == 200
+ data = json.loads(r.read())
+
+ assert data["Arn"] == "arn:aws:iam::000000000000:user/caller-identity-json-user"
+ assert data["Account"] == "000000000000"
+ assert data["UserId"]
+
+
[email protected]("auth_enabled", [False, True])
[email protected]("credential_kind", ["unknown", "inactive"])
+def test_sts_get_caller_identity_credential_rejections_require_auth(
+ monkeypatch, auth_enabled, credential_kind
+):
+ import asyncio
+
+ from ministack import app as app_mod
+ from ministack.core.responses import request_scope
+ from ministack.services import iam as iam_svc
+ from ministack.services import sts as sts_svc
+
+ key = "test-gci-mode-key"
+ account = "000000000000"
+ monkeypatch.setattr(app_mod, "AUTH", auth_enabled)
+ if credential_kind == "inactive":
+ iam_svc._access_keys.set_scoped(account, None, key, {
+ "UserName": "alice", "Status": "Inactive", "SecretAccessKey": "secret",
+ })
+ try:
+ with request_scope(account, "us-east-1"):
+ status, _, payload = asyncio.run(sts_svc.handle_request(
+ "GET", "/", {
+ "authorization": f"AWS4-HMAC-SHA256 Credential={key}/20260911/us-east-1/sts/aws4_request",
+ }, b"", {"Action": ["GetCallerIdentity"]},
+ ))
+ assert status == (403 if auth_enabled else 200)
+ if not auth_enabled and credential_kind == "unknown":
+ assert b":root</Arn>" in payload
+ if auth_enabled:
+ expected = (
+ b"UnrecognizedClientException"
+ if credential_kind == "unknown"
+ else b"InvalidClientTokenId"
+ )
+ assert expected in payload
+ finally:
+ iam_svc._access_keys.pop_scoped(account, None, key, None)
+
+
[email protected]("signed_via", ["header", "query"])
+def test_sts_get_caller_identity_resolves_auth_iam_user(monkeypatch, signed_via):
+ """Under AUTH, an active IAM key resolves via header or query credentials."""
+ import asyncio
+
+ from ministack import app as app_mod
+ from ministack.core.responses import request_scope
+ from ministack.services import iam as iam_svc
+ from ministack.services import sts as sts_svc
+
+ account = "000000000000"
+ user_name = "gci-auth-user"
+ key = "test-gci-auth-key"
+ monkeypatch.setattr(app_mod, "AUTH", True)
+ iam_svc._users.set_scoped(account, None, user_name, {
+ "UserName": user_name,
+ "UserId": "test-gci-auth-user-id",
+ "Arn": f"arn:aws:iam::{account}:user/{user_name}",
+ "Path": "/",
+ "AttachedPolicies": [],
+ })
+ iam_svc._access_keys.set_scoped(account, None, key, {
+ "AccessKeyId": key,
+ "SecretAccessKey": "test-gci-auth-secret",
+ "Status": "Active",
+ "UserName": user_name,
+ })
+ if signed_via == "header":
+ headers = {
+ "authorization": f"AWS4-HMAC-SHA256 Credential={key}/20260911/us-east-1/sts/aws4_request",
+ }
+ query = {"Action": ["GetCallerIdentity"]}
+ else:
+ headers = {}
+ query = {
+ "Action": ["GetCallerIdentity"],
+ "X-Amz-Credential": [f"{key}/20260911/us-east-1/sts/aws4_request"],
+ }
+ try:
+ with request_scope(account, "us-east-1"):
+ status, _, payload = asyncio.run(
+ sts_svc.handle_request("GET", "/", headers, b"", query)
+ )
+
+ assert status == 200
+ assert f"arn:aws:iam::{account}:user/{user_name}".encode() in payload
+ assert b"test-gci-auth-user-id" in payload
+ finally:
+ iam_svc._access_keys.pop_scoped(account, None, key, None)
+ iam_svc._users.pop_scoped(account, None, user_name, None)