Ministack: Free, open-source local AWS emulator - 60+ services, Terraform compatible, real databases. Free forever. MIT licensed.
Python awsaws-emulatoraws-localaws-sdkdevtoolsdockerdynamodbec2emulatorlambdalocalstacklocalstack-alternativeministackmock-awsopen-sourcepythons3sqsterraform
feat(rds): add resource-bound IAM authorization (#1751)
Ian Oberst85f734cbff53
diff --git a/ministack/core/rds_iam_authorization.py b/ministack/core/rds_iam_authorization.pynew file mode 100644index 00000000..e75c291d--- /dev/null+++ b/ministack/core/rds_iam_authorization.py@@ -0,0 +1,134 @@+# Copyright (c) 2026 MiniStack Contributors. SPDX-License-Identifier: MIT+# Copies or substantial portions, including AI-assisted ports or rewrites, must retain this notice (see LICENSE).+"""Stage 5 of #1744: strict resource-bound RDS IAM authorization.++No runtime callers or AUTH lookup. Future integration must define and test its+AUTH gate separately from IAMDatabaseAuthenticationEnabled before using this+helper. This is not a TLS check or permission to enable MySQL login.+"""++import re+from dataclasses import dataclass, replace++from ministack.core.iam_evaluator import AuthError, EvalContext, evaluate, resolve_principal+from ministack.core.rds_iam import RdsIamTokenError, VerifiedRdsToken, verify_rds_iam_token+from ministack.core.responses import request_scope+++@dataclass(frozen=True)+class RdsIamAuthorizationError:+ """A bounded failure code, with no client token or credential material."""++ code: str+++@dataclass(frozen=True)+class AuthorizedRdsConnection:+ identity: VerifiedRdsToken+ resource_arn: str+++def _resource_policies(policies, resource_arn):+ # The shared evaluator currently case-folds Resource as well as Action.+ # Database usernames are case-sensitive. Match Resource/NotResource here,+ # then leave action, conditions and deny precedence to the shared engine.+ # Do not change policy behavior for unrelated services in this stage.+ def matches(pattern):+ regex = re.escape(pattern).replace(r"\*", ".*").replace(r"\?", ".")+ return re.fullmatch(regex, resource_arn, re.DOTALL) is not None++ return [[replace(stmt, resources=["*"], not_resources=[]) for stmt in policy+ if (any(matches(p) for p in stmt.resources) if stmt.resources+ else bool(stmt.not_resources) and not any(matches(p) for p in stmt.not_resources))]+ for policy in policies]+++def authorize_rds_iam_token(+ token: str,+ *,+ account_id: str,+ region: str,+ resource_kind: str,+ resource_identifier: str,+ db_user: str,+ reader_endpoint: bool = False,+) -> AuthorizedRdsConnection | RdsIamAuthorizationError | RdsIamTokenError:+ """Verify and authorize against a trusted instance or cluster identifier.++ The future broker must bind these arguments to its resource capability,+ not accept a target/account claimed by the client. Endpoint/port, enablement,+ and the stable dbuser resource ID are read from that resource's scoped state.+ Aurora members use the parent cluster's enablement and resource ID, but the+ member's endpoint. Cluster callers may explicitly select the reader endpoint.+ Cross-account credentials must first assume a role in the resource account.+ A success is a point-in-time decision, not a reusable authorization cache.+ """+ from ministack.services import rds++ if (+ not isinstance(account_id, str) or not re.fullmatch(r"[0-9]{12}", account_id)+ or not isinstance(region, str) or not re.fullmatch(r"[a-z0-9-]+", region)+ or resource_kind not in ("instance", "cluster")+ or not isinstance(resource_identifier, str) or not resource_identifier+ or type(reader_endpoint) is not bool+ or (reader_endpoint and resource_kind != "cluster")+ ):+ return RdsIamAuthorizationError("InvalidTarget")++ store = rds._instances if resource_kind == "instance" else rds._clusters+ target = store.get_scoped(account_id, region, resource_identifier)+ if target is None:+ return RdsIamAuthorizationError("ResourceNotFound")+ engines = ("mysql", "aurora", "aurora-mysql") if resource_kind == "instance" else ("aurora", "aurora-mysql")+ if target.get("Engine") not in engines:+ return RdsIamAuthorizationError("UnsupportedEngine")+ if resource_kind == "instance" and target.get("Engine") != "mysql" and not target.get("DBClusterIdentifier"):+ return RdsIamAuthorizationError("InvalidTarget")++ owner = target+ resource_id_key = "DbiResourceId" if resource_kind == "instance" else "DbClusterResourceId"+ if resource_kind == "instance" and target.get("DBClusterIdentifier"):+ owner = rds._clusters.get_scoped(account_id, region, target["DBClusterIdentifier"])+ if owner is None:+ return RdsIamAuthorizationError("ResourceNotFound")+ if owner.get("Engine") not in ("aurora", "aurora-mysql"):+ return RdsIamAuthorizationError("UnsupportedEngine")+ resource_id_key = "DbClusterResourceId"+ if owner.get("IAMDatabaseAuthenticationEnabled") is not True:+ return RdsIamAuthorizationError("IAMDatabaseAuthenticationDisabled")+ resource_id = owner.get(resource_id_key)+ prefix = "db-" if resource_id_key == "DbiResourceId" else "cluster-"+ if not isinstance(resource_id, str) or not re.fullmatch(prefix + r"[A-Za-z0-9]+", resource_id):+ return RdsIamAuthorizationError("InvalidTarget")++ endpoint = target.get("ReaderEndpoint" if reader_endpoint else "Endpoint")+ if isinstance(endpoint, dict):+ hostname, port = endpoint.get("Address"), endpoint.get("Port")+ else:+ hostname, port = endpoint, target.get("Port")+ verified = verify_rds_iam_token(+ token, hostname=hostname, port=port, db_user=db_user, region=region, account_id=account_id,+ )+ if isinstance(verified, RdsIamTokenError):+ return verified++ # MiniStack's credential and RDS stores currently issue arn:aws identities.+ resource_arn = f"arn:aws:rds-db:{region}:{account_id}:dbuser:{resource_id}/{db_user}"+ # Policy gathering includes ambient-scoped user/group/managed-policy stores.+ # Pin the explicit resource account, restoring both contextvars on all exits.+ with request_scope(account_id, region):+ principal = resolve_principal(verified.access_key_id, account_id)+ if (+ isinstance(principal, AuthError) or principal.account != account_id+ or principal.arn != verified.principal_arn or principal.type != verified.principal_type+ ):+ return RdsIamAuthorizationError("InvalidCredentials")+ if principal.policies is not None:+ result = evaluate(EvalContext(+ principal_arn=principal.arn, principal_type=principal.type,+ principal_account=account_id, action="rds-db:connect",+ resource_arn=resource_arn, region=region,+ ), _resource_policies(principal.policies, resource_arn))+ if result.decision != "Allow":+ return RdsIamAuthorizationError(result.decision)+ return AuthorizedRdsConnection(verified, resource_arn)diff --git a/tests/test_rds_iam_authorization.py b/tests/test_rds_iam_authorization.pynew file mode 100644index 00000000..e9468471--- /dev/null+++ b/tests/test_rds_iam_authorization.py@@ -0,0 +1,249 @@+# Copyright (c) 2026 MiniStack Contributors. SPDX-License-Identifier: MIT+# Copies or substantial portions, including AI-assisted ports or rewrites, must retain this notice (see LICENSE).+"""Offline stage-5 authorization using SDK tokens and real scoped policy stores."""++import time++import boto3+import pytest++from ministack.core.rds_iam_authorization import AuthorizedRdsConnection, authorize_rds_iam_token+from ministack.core.responses import (+ AccountRegionScopedDict,+ AccountScopedDict,+ get_account_id,+ get_region,+ request_scope,+)+from ministack.services import iam, rds, sts++ACCOUNT = "123456789012"+OTHER = "210987654321"+REGION = "us-east-1"+KEY = "test-rds-authorization-key"+SECRET = "authorization-test-secret"+USER = "AppUser"+HOST = "db.example.com"+ARN = f"arn:aws:rds-db:{REGION}:{ACCOUNT}:dbuser:db-TEST/{USER}"+ARGS = dict(account_id=ACCOUNT, region=REGION, resource_kind="instance",+ resource_identifier="database", db_user=USER)+++def policy(resource=ARN, effect="Allow", **extra):+ return {"Statement": [{"Effect": effect, "Action": "rds-db:connect", "Resource": resource, **extra}]}+++def token(*, host=HOST, port=3306, user=USER, key=KEY, secret=SECRET, session_token=None):+ return boto3.client("rds", region_name=REGION, aws_access_key_id=key,+ aws_secret_access_key=secret, aws_session_token=session_token).generate_db_auth_token(+ DBHostname=host, Port=port, DBUsername=user,+ )++[email protected](autouse=True)+def state(monkeypatch):+ for name in ("_users", "_access_keys", "_user_inline_policies", "_roles", "_groups",+ "_group_inline_policies", "_policies"):+ monkeypatch.setattr(iam, name, AccountScopedDict())+ monkeypatch.setattr(rds, "_instances", AccountRegionScopedDict())+ monkeypatch.setattr(rds, "_clusters", AccountRegionScopedDict())+ monkeypatch.setattr(sts, "_sessions", {})+ monkeypatch.setenv("AWS_ACCESS_KEY_ID", "test")+ monkeypatch.setenv("AWS_SECRET_ACCESS_KEY", "root-secret")+ monkeypatch.delenv("AWS_SESSION_TOKEN", raising=False)+ iam._users.set_scoped(ACCOUNT, None, "alice", {+ "UserName": "alice", "Arn": f"arn:aws:iam::{ACCOUNT}:user/alice",+ })+ iam._access_keys.set_scoped(ACCOUNT, None, KEY, {+ "UserName": "alice", "Status": "Active", "SecretAccessKey": SECRET,+ })+ iam._user_inline_policies.set_scoped(ACCOUNT, None, "alice", {"connect": policy()})+ instance = dict(Engine="mysql", DbiResourceId="db-TEST", IAMDatabaseAuthenticationEnabled=True,+ Endpoint={"Address": HOST, "Port": 3306})+ rds._instances.set_scoped(ACCOUNT, REGION, "database", instance)+ return instance++[email protected]("auth", [None, "false", "true"])[email protected]("enabled", [False, True])+def test_strict_primitive_independent_of_auth(state, monkeypatch, auth, enabled):+ if auth is None:+ monkeypatch.delenv("AUTH", raising=False)+ else:+ monkeypatch.setenv("AUTH", auth)+ state["IAMDatabaseAuthenticationEnabled"] = enabled+ with request_scope(OTHER, "eu-west-1"):+ result = authorize_rds_iam_token(token(), **ARGS)+ assert (get_account_id(), get_region()) == (OTHER, "eu-west-1")+ if enabled:+ assert isinstance(result, AuthorizedRdsConnection)+ assert result.resource_arn == ARN+ assert SECRET not in repr(result)+ else:+ assert result.code == "IAMDatabaseAuthenticationDisabled"++[email protected]("resource", [ARN, ARN.replace(USER, "*"), "*"])+def test_exact_and_wildcard_grants(resource):+ iam._user_inline_policies.set_scoped(ACCOUNT, None, "alice", {"grant": policy(resource)})+ assert isinstance(authorize_rds_iam_token(token(), **ARGS), AuthorizedRdsConnection)++[email protected]("resource", [ARN.replace(USER, USER.lower()), ARN.replace("db-TEST", "db-OTHER"),+ ARN.replace(ACCOUNT, OTHER), ARN.replace(REGION, "eu-west-1"),+ ARN.replace("db-TEST", "database")])+def test_wrong_policy_resource_is_denied(resource):+ iam._user_inline_policies.set_scoped(ACCOUNT, None, "alice", {"grant": policy(resource)})+ assert authorize_rds_iam_token(token(), **ARGS).code == "ImplicitDeny"+++def test_explicit_deny_overrides_allow():+ iam._user_inline_policies.set_scoped(ACCOUNT, None, "alice", {"allow": policy("*"), "deny": policy(effect="Deny")})+ assert authorize_rds_iam_token(token(), **ARGS).code == "Deny"++[email protected]("exception,allowed", [(ARN, True), (ARN.replace(USER, USER.lower()), False)])+def test_not_resource_respects_case(exception, allowed):+ deny = {"Statement": [{"Effect": "Deny", "Action": "rds-db:connect", "NotResource": exception}]}+ iam._user_inline_policies.set_scoped(ACCOUNT, None, "alice", {"allow": policy("*"), "deny": deny})+ result = authorize_rds_iam_token(token(), **ARGS)+ assert isinstance(result, AuthorizedRdsConnection) is allowed+++def test_missing_policy_and_wrong_action_deny():+ iam._user_inline_policies.set_scoped(ACCOUNT, None, "alice", {})+ assert authorize_rds_iam_token(token(), **ARGS).code == "ImplicitDeny"+ iam._user_inline_policies.set_scoped(ACCOUNT, None, "alice", {"rds": policy(Action="rds:*")})+ assert authorize_rds_iam_token(token(), **ARGS).code == "ImplicitDeny"++[email protected]("kind", ["group", "managed"])+def test_policies_use_explicit_account_and_restore_context(kind):+ iam._user_inline_policies.set_scoped(ACCOUNT, None, "alice", {})+ iam._user_inline_policies.set_scoped(OTHER, None, "alice", {"deny": policy("*", "Deny")})+ if kind == "group":+ iam._groups.set_scoped(ACCOUNT, None, "team", {"Users": ["alice"]})+ iam._group_inline_policies.set_scoped(ACCOUNT, None, "team", {"grant": policy()})+ else:+ arn = f"arn:aws:iam::{ACCOUNT}:policy/connect"+ iam._users.get_scoped(ACCOUNT, None, "alice")["AttachedPolicies"] = [arn]+ iam._policies.set_scoped(ACCOUNT, None, "connect", {+ "Arn": arn, "DefaultVersionId": "v1", "Versions": {"v1": {"Document": policy()}},+ })+ with request_scope(OTHER, "eu-west-1"):+ assert isinstance(authorize_rds_iam_token(token(), **ARGS), AuthorizedRdsConnection)+ assert (get_account_id(), get_region()) == (OTHER, "eu-west-1")++[email protected]("override", [dict(host="other.example.com"), dict(port=3307), dict(user="appuser"),+ dict(secret="wrong"), dict(key="unknown")])+def test_invalid_token_never_reaches_allow(override):+ assert not isinstance(authorize_rds_iam_token(token(**override), **ARGS), AuthorizedRdsConnection)+++def test_inactive_key():+ iam._access_keys.get_scoped(ACCOUNT, None, KEY)["Status"] = "Inactive"+ assert authorize_rds_iam_token(token(), **ARGS).code == "InvalidCredentials"++[email protected]("field,value", [("account_id", OTHER), ("region", "eu-west-1"),+ ("resource_identifier", "missing")])+def test_resource_lookup_does_not_fall_back_to_ambient_tenant(field, value):+ with request_scope(ACCOUNT, REGION):+ assert authorize_rds_iam_token(token(), **{**ARGS, field: value}).code == "ResourceNotFound"++[email protected]("field,value", [("DbiResourceId", ""), ("DbiResourceId", "db-TEST/*"),+ ("Endpoint", {}), ("Endpoint", {"Address": HOST})])+def test_incomplete_resource_fails_closed(state, field, value):+ state[field] = value+ assert not isinstance(authorize_rds_iam_token(token(), **ARGS), AuthorizedRdsConnection)++[email protected]("kind", ["instance", "cluster"])+def test_unsupported_engine(state, kind):+ state["Engine"] = "postgres"+ rds._clusters.set_scoped(ACCOUNT, REGION, "database", state)+ assert authorize_rds_iam_token(token(), **{**ARGS, "resource_kind": kind}).code == "UnsupportedEngine"++[email protected]("kind,reader", [("instance", False), ("cluster", False), ("cluster", True)])[email protected]("enabled", [False, True])+def test_aurora_uses_cluster_resource_and_flag(state, kind, reader, enabled):+ state.update(Engine="aurora-mysql", DBClusterIdentifier="database", IAMDatabaseAuthenticationEnabled=not enabled)+ cluster_arn = ARN.replace("db-TEST", "cluster-TEST")+ rds._clusters.set_scoped(ACCOUNT, REGION, "database", {+ "Engine": "aurora-mysql", "DbClusterResourceId": "cluster-TEST",+ "IAMDatabaseAuthenticationEnabled": enabled, "Endpoint": HOST,+ "ReaderEndpoint": "reader.example.com", "Port": 3306,+ })+ iam._user_inline_policies.set_scoped(ACCOUNT, None, "alice", {"grant": policy(cluster_arn)})+ result = authorize_rds_iam_token(token(host="reader.example.com" if reader else HOST),+ **{**ARGS, "resource_kind": kind}, reader_endpoint=reader)+ if enabled:+ assert result.resource_arn == cluster_arn+ else:+ assert result.code == "IAMDatabaseAuthenticationDisabled"+++def test_missing_parent_cluster_is_not_standalone(state):+ state.update(Engine="aurora-mysql", DBClusterIdentifier="missing")+ assert authorize_rds_iam_token(token(), **ARGS).code == "ResourceNotFound"++[email protected]("session_account", [ACCOUNT, OTHER])+def test_role_session_must_belong_to_resource_account(session_account):+ key = "test-rds-authorization-session-key"+ sts._sessions[key] = dict(Arn=f"arn:aws:sts::{session_account}:assumed-role/db-role/session",+ AccountId=session_account, SecretAccessKey=SECRET, SessionToken="session-token",+ Expiration=time.time() + 600, PrincipalType="AssumedRole")+ iam._roles.set_scoped(session_account, None, "db-role", {"InlinePolicies": {"grant": policy()}})+ result = authorize_rds_iam_token(token(key=key, session_token="session-token"), **ARGS)+ assert isinstance(result, AuthorizedRdsConnection) is (session_account == ACCOUNT)+++def test_root_still_requires_enabled_resource(state):+ signed = token(key="test", secret="root-secret")+ assert isinstance(authorize_rds_iam_token(signed, **ARGS), AuthorizedRdsConnection)+ state["IAMDatabaseAuthenticationEnabled"] = False+ assert authorize_rds_iam_token(signed, **ARGS).code == "IAMDatabaseAuthenticationDisabled"+++def test_ambient_policy_cannot_grant_access():+ iam._user_inline_policies.set_scoped(ACCOUNT, None, "alice", {})+ iam._user_inline_policies.set_scoped(OTHER, None, "alice", {"grant": policy("*")})+ with request_scope(OTHER, "eu-west-1"):+ assert authorize_rds_iam_token(token(), **ARGS).code == "ImplicitDeny"+ assert (get_account_id(), get_region()) == (OTHER, "eu-west-1")++[email protected]("region,allowed", [(REGION, True), ("eu-west-1", False)])+def test_policy_conditions_use_target_region(region, allowed):+ iam._user_inline_policies.set_scoped(ACCOUNT, None, "alice", {+ "grant": policy(Condition={"StringEquals": {"aws:RequestedRegion": region}}),+ })+ assert isinstance(authorize_rds_iam_token(token(), **ARGS), AuthorizedRdsConnection) is allowed+++def test_cluster_dictionary_endpoint_uses_advertised_port():+ rds._clusters.set_scoped(ACCOUNT, REGION, "database", {+ "Engine": "aurora-mysql", "DbClusterResourceId": "cluster-TEST",+ "IAMDatabaseAuthenticationEnabled": True, "Endpoint": {"Address": HOST, "Port": 13306},+ "Port": 3306,+ })+ iam._user_inline_policies.set_scoped(ACCOUNT, None, "alice", {"grant": policy(ARN.replace("db-", "cluster-"))})+ args = {**ARGS, "resource_kind": "cluster"}+ assert isinstance(authorize_rds_iam_token(token(port=13306), **args), AuthorizedRdsConnection)+ assert not isinstance(authorize_rds_iam_token(token(), **args), AuthorizedRdsConnection)+++def test_aurora_without_parent_cannot_use_instance_arn(state):+ state["Engine"] = "aurora-mysql"+ assert authorize_rds_iam_token(token(), **ARGS).code == "InvalidTarget"++[email protected]("override", [dict(account_id=""), dict(region=""), dict(resource_kind="proxy"),+ dict(resource_identifier=""), dict(reader_endpoint=True),+ dict(reader_endpoint="false")])+def test_invalid_target(override):+ assert authorize_rds_iam_token(token(), **{**ARGS, **override}).code == "InvalidTarget"