Skip to content

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

Enforce RDS Data API endpoint fidelity (#1335)

Ian Oberstec195485f9bb
diff --git a/ministack/services/rds_data.py b/ministack/services/rds_data.py
index 990d1091..f881f837 100644
--- a/ministack/services/rds_data.py
+++ b/ministack/services/rds_data.py
@@ -68,6 +68,13 @@ def _stub_success():
})
+def _stub_unsupported_sql_error(sql):
+ return _error(
+ "BadRequestException",
+ f"Unsupported SQL in RDS Data API stub mode: {sql}",
+ )
+
+
def _cluster_id_from_arn(resource_arn):
"""Return a stable, region-qualified key for per-cluster stub state."""
try:
@@ -218,33 +225,45 @@ def _stub_execute(resource_arn, sql):
"records": records,
})
- # Default stub
- return _stub_success()
+ return _stub_unsupported_sql_error(sql)
-def _resolve_cluster(resource_arn):
- """Find RDS cluster and a member instance from a resourceArn."""
+def _resolve_target(resource_arn):
+ """Find RDS cluster metadata and a SQL target from a resourceArn.
+
+ Standalone DB instance ARNs remain a deliberate local-development
+ divergence from AWS's cluster-only Data API; they have no cluster
+ metadata to return or endpoint setting to enforce.
+ """
from ministack.services import rds
parsed = rds._parse_rds_arn(resource_arn)
if not parsed:
- return None, None
+ return None, None, None
spec, resource_type, resource_id = parsed
if resource_type == "db":
if spec.account_id != get_account_id():
- return None, None
+ return None, None, None
instance = rds._instances.get_scoped(spec.account_id, spec.region, resource_id)
if instance:
- return instance, instance.get("Engine", "postgres")
- return None, None
+ cluster = None
+ cluster_id = instance.get("DBClusterIdentifier")
+ if cluster_id:
+ cluster = rds._clusters.get_scoped(
+ spec.account_id,
+ spec.region,
+ cluster_id,
+ )
+ return instance, instance.get("Engine", "postgres"), cluster
+ return None, None, None
if resource_type != "cluster":
- return None, None
+ return None, None, None
cluster = rds._resolve_cluster(resource_arn)
if not cluster:
- return None, None
+ return None, None, None
engine = cluster.get("Engine", "postgres")
cluster_id = cluster["DBClusterIdentifier"]
@@ -253,7 +272,7 @@ def _resolve_cluster(resource_arn):
# intentional in-memory Data API stub. Provisioned clusters, however, must
# have an available member before SQL can run.
if cluster.get("EngineMode") == "serverless":
- return cluster, engine
+ return cluster, engine, cluster
member_ids = {
member.get("DBInstanceIdentifier")
@@ -261,7 +280,7 @@ def _resolve_cluster(resource_arn):
if member.get("DBInstanceIdentifier")
}
if not member_ids or not cluster.get("_shared_container_ready", True):
- return None, engine
+ return None, engine, cluster
# Find an instance belonging to this cluster
for inst in rds._instances.values_scoped(spec.account_id, spec.region):
@@ -278,10 +297,16 @@ def _resolve_cluster(resource_arn):
inst["_docker_container_id"] = cluster.get("_shared_container_id")
inst["_internal_address"] = cluster.get("_shared_internal_address")
inst["_internal_port"] = cluster.get("_shared_internal_port")
- return inst, engine
+ return inst, engine, cluster
# The cluster exists, but it has no available compute to accept SQL.
- return None, engine
+ return None, engine, cluster
+
+
+def _resolve_cluster(resource_arn):
+ """Find RDS cluster and a member instance from a resourceArn."""
+ instance, engine, _cluster = _resolve_target(resource_arn)
+ return instance, engine
def _cluster_resolution_error(resource_arn, engine):
@@ -295,6 +320,57 @@ def _cluster_resolution_error(resource_arn, engine):
)
+def _http_endpoint_not_enabled_error(resource_arn):
+ return _error(
+ "HttpEndpointNotEnabledException",
+ f"HTTP endpoint is not enabled for cluster ARN: {resource_arn}",
+ )
+
+
+def _transaction_not_found_error(txn_id):
+ return _error(
+ "TransactionNotFoundException",
+ f"Transaction {txn_id} not found",
+ 404,
+ )
+
+
+def _transaction_completion_not_found_error(txn_id):
+ return _error(
+ "NotFoundException",
+ f"Transaction {txn_id} not found",
+ 404,
+ )
+
+
+def _validate_http_endpoint_enabled(cluster, resource_arn):
+ if cluster is not None and not cluster.get("HttpEndpointEnabled", False):
+ return _http_endpoint_not_enabled_error(resource_arn)
+ return None
+
+
+def _is_statement_timeout_error(exc):
+ err_str = str(exc).lower()
+ return any(
+ marker in err_str
+ for marker in (
+ "statement timeout",
+ "statement timed out",
+ "max_statement_time",
+ "maximum statement execution time",
+ )
+ )
+
+
+def _sql_execution_error(exc):
+ if _is_statement_timeout_error(exc):
+ return _error("StatementTimeoutException", f"Statement timed out: {exc}", 400)
+ if _is_connection_error(exc):
+ logger.warning("DB connection failed for real endpoint: %s", exc)
+ return _transient_database_error(f"Database endpoint is not available: {exc}")
+ return _error("BadRequestException", f"Database error: {exc}")
+
+
def _get_secret_credentials(secret_arn):
"""Extract username and password from a Secrets Manager secret.
@@ -306,28 +382,54 @@ def _get_secret_credentials(secret_arn):
_name, secret = secretsmanager._resolve(secret_arn, use_arn_scope=True)
if not secret:
return None, None
+ return _credentials_from_secret(secret)
+
+def _require_secret_credentials(secret_arn):
+ from ministack.services import secretsmanager
+
+ _name, secret = secretsmanager._resolve(secret_arn, use_arn_scope=True)
+ if not secret:
+ return None, None, _error(
+ "SecretsErrorException",
+ f"Secret not found for ARN: {secret_arn}",
+ )
+ if secret.get("DeletedDate"):
+ return None, None, _error(
+ "SecretsErrorException",
+ f"Secret is scheduled for deletion for ARN: {secret_arn}",
+ )
+ user, password = _credentials_from_secret(secret)
+ if not password:
+ return None, None, _error(
+ "InvalidSecretException",
+ f"Secret does not contain a password for ARN: {secret_arn}",
+ )
+ return user, password, None
+
+
+def _credentials_from_secret_string(secret_string):
+ try:
+ parsed = json.loads(secret_string)
+ except (json.JSONDecodeError, TypeError):
+ return None, secret_string
+ if isinstance(parsed, dict):
+ return parsed.get("username"), parsed.get("password")
+ return None, secret_string
+
+
+def _credentials_from_secret(secret):
# Find the AWSCURRENT version
for _vid, ver in secret.get("Versions", {}).items():
if "AWSCURRENT" in ver.get("Stages", []):
secret_string = ver.get("SecretString")
if secret_string:
- try:
- parsed = json.loads(secret_string)
- return (parsed.get("username"),
- parsed.get("password", secret_string))
- except (json.JSONDecodeError, TypeError):
- return None, secret_string
+ return _credentials_from_secret_string(secret_string)
# Fallback to any version
for _vid, ver in secret.get("Versions", {}).items():
secret_string = ver.get("SecretString")
if secret_string:
- try:
- parsed = json.loads(secret_string)
- return (parsed.get("username"),
- parsed.get("password", secret_string))
- except (json.JSONDecodeError, TypeError):
- return None, secret_string
+ return _credentials_from_secret_string(secret_string)
return None, None
@@ -342,7 +444,7 @@ def _connect(instance, engine, database=None, password=None,
port = (instance.get("_internal_port")
or instance.get("Endpoint", {}).get("Port", 5432))
db = database or ""
- pw = password or "password"
+ pw = "password" if password is None else password
if "mysql" in engine or "aurora-mysql" in engine or "mariadb" in engine:
try:
@@ -525,30 +627,41 @@ def _execute_statement(data):
if not sql:
return _error("BadRequestException", "sql is required")
- instance, engine = _resolve_cluster(resource_arn)
+ instance, engine, cluster = _resolve_target(resource_arn)
+ endpoint_error = _validate_http_endpoint_enabled(cluster, resource_arn)
+ if endpoint_error:
+ return endpoint_error
if not instance:
return _cluster_resolution_error(resource_arn, engine)
+ secret_user, password, secret_error = _require_secret_credentials(secret_arn)
+ if secret_error:
+ return secret_error
+
+ conn = None
+ if txn_id:
+ with _lock:
+ txn = _transactions.get(txn_id)
+ if not txn or txn.get("resourceArn") != resource_arn:
+ return _transaction_not_found_error(txn_id)
+ conn = txn["conn"]
# Keep stub mode for intentional mock environments only. Once RDS has a
# real container-backed endpoint, connection failures must surface as
# transient errors instead of acknowledging writes that never reached MySQL.
- if not _has_real_endpoint(instance):
+ # A supplied transactionId is validated first and always uses its bound
+ # connection; stub mode is only available to non-transactional requests.
+ if not txn_id and not _has_real_endpoint(instance):
logger.info("No endpoint for %s, using stub mode", resource_arn)
return _stub_execute(resource_arn, sql)
- secret_user, password = _get_secret_credentials(secret_arn)
-
# Convert :name placeholders to %(name)s for DB-API
params = _convert_parameters(parameters)
exec_sql = _substitute_named_params(sql, params)
own_conn = False
- conn = None
try:
- with _lock:
- if txn_id and txn_id in _transactions:
- conn = _transactions[txn_id]["conn"]
- else:
+ if conn is None:
+ with _lock:
conn = _connect(instance, engine, database, password,
username=secret_user)
own_conn = True
@@ -588,10 +701,7 @@ def _execute_statement(data):
except Exception as e:
if own_conn and conn:
conn.close()
- if _is_connection_error(e):
- logger.warning("DB connection failed for real endpoint: %s", e)
- return _transient_database_error(f"Database endpoint is not available: {e}")
- return _error("BadRequestException", f"Database error: {e}")
+ return _sql_execution_error(e)
def _begin_transaction(data):
@@ -604,11 +714,16 @@ def _begin_transaction(data):
if not secret_arn:
return _error("BadRequestException", "secretArn is required")
- instance, engine = _resolve_cluster(resource_arn)
+ instance, engine, cluster = _resolve_target(resource_arn)
+ endpoint_error = _validate_http_endpoint_enabled(cluster, resource_arn)
+ if endpoint_error:
+ return endpoint_error
if not instance:
return _cluster_resolution_error(resource_arn, engine)
- secret_user, password = _get_secret_credentials(secret_arn)
+ secret_user, password, secret_error = _require_secret_credentials(secret_arn)
+ if secret_error:
+ return secret_error
try:
conn = _connect(instance, engine, database, password,
@@ -620,7 +735,7 @@ def _begin_transaction(data):
except ImportError as e:
return _error("BadRequestException", str(e))
except Exception as e:
- return _error("BadRequestException", f"Database connection error: {e}")
+ return _sql_execution_error(e)
txn_id = str(uuid.uuid4())
with _lock:
@@ -638,12 +753,31 @@ def _commit_transaction(data):
txn_id = data.get("transactionId")
if not txn_id:
return _error("BadRequestException", "transactionId is required")
+ resource_arn = data.get("resourceArn")
+ secret_arn = data.get("secretArn")
+ if not resource_arn:
+ return _error("BadRequestException", "resourceArn is required")
+ if not secret_arn:
+ return _error("BadRequestException", "secretArn is required")
+
+ instance, engine, cluster = _resolve_target(resource_arn)
+ endpoint_error = _validate_http_endpoint_enabled(cluster, resource_arn)
+ if endpoint_error:
+ return endpoint_error
+ if not instance:
+ return _cluster_resolution_error(resource_arn, engine)
+ _secret_user, _password, secret_error = _require_secret_credentials(secret_arn)
+ if secret_error:
+ return secret_error
with _lock:
- txn = _transactions.pop(txn_id, None)
+ txn = _transactions.get(txn_id)
+ if txn and txn.get("resourceArn") != resource_arn:
+ return _transaction_completion_not_found_error(txn_id)
+ if txn:
+ _transactions.pop(txn_id)
if not txn:
- return _error("NotFoundException",
- f"Transaction {txn_id} not found", 404)
+ return _transaction_completion_not_found_error(txn_id)
try:
txn["conn"].commit()
@@ -658,12 +792,31 @@ def _rollback_transaction(data):
txn_id = data.get("transactionId")
if not txn_id:
return _error("BadRequestException", "transactionId is required")
+ resource_arn = data.get("resourceArn")
+ secret_arn = data.get("secretArn")
+ if not resource_arn:
+ return _error("BadRequestException", "resourceArn is required")
+ if not secret_arn:
+ return _error("BadRequestException", "secretArn is required")
+
+ instance, engine, cluster = _resolve_target(resource_arn)
+ endpoint_error = _validate_http_endpoint_enabled(cluster, resource_arn)
+ if endpoint_error:
+ return endpoint_error
+ if not instance:
+ return _cluster_resolution_error(resource_arn, engine)
+ _secret_user, _password, secret_error = _require_secret_credentials(secret_arn)
+ if secret_error:
+ return secret_error
with _lock:
- txn = _transactions.pop(txn_id, None)
+ txn = _transactions.get(txn_id)
+ if txn and txn.get("resourceArn") != resource_arn:
+ return _transaction_completion_not_found_error(txn_id)
+ if txn:
+ _transactions.pop(txn_id)
if not txn:
- return _error("NotFoundException",
- f"Transaction {txn_id} not found", 404)
+ return _transaction_completion_not_found_error(txn_id)
try:
txn["conn"].rollback()
@@ -689,18 +842,26 @@ def _batch_execute_statement(data):
if not sql:
return _error("BadRequestException", "sql is required")
- instance, engine = _resolve_cluster(resource_arn)
+ instance, engine, cluster = _resolve_target(resource_arn)
+ endpoint_error = _validate_http_endpoint_enabled(cluster, resource_arn)
+ if endpoint_error:
+ return endpoint_error
if not instance:
return _cluster_resolution_error(resource_arn, engine)
- secret_user, password = _get_secret_credentials(secret_arn)
+ secret_user, password, secret_error = _require_secret_credentials(secret_arn)
+ if secret_error:
+ return secret_error
own_conn = False
conn = None
try:
with _lock:
- if txn_id and txn_id in _transactions:
- conn = _transactions[txn_id]["conn"]
+ if txn_id:
+ txn = _transactions.get(txn_id)
+ if not txn or txn.get("resourceArn") != resource_arn:
+ return _transaction_not_found_error(txn_id)
+ conn = txn["conn"]
else:
conn = _connect(instance, engine, database, password,
username=secret_user)
@@ -735,7 +896,7 @@ def _batch_execute_statement(data):
except Exception as e:
if own_conn and conn:
conn.close()
- return _error("BadRequestException", f"Database error: {e}")
+ return _sql_execution_error(e)
def reset():
diff --git a/tests/test_rds.py b/tests/test_rds.py
index 6d5eaf52..d30f34ae 100644
--- a/tests/test_rds.py
+++ b/tests/test_rds.py
@@ -6870,6 +6870,11 @@ def test_aurora_delete_member_keeps_shared_data(rds, rds_data):
_reader,
cluster,
):
+ rds.modify_db_cluster(
+ DBClusterIdentifier=cluster_id,
+ EnableHttpEndpoint=True,
+ ApplyImmediately=True,
+ )
with _aurora_connect(writer["Endpoint"]) as conn:
with conn.cursor() as cursor:
cursor.execute("CREATE TABLE durable_rows (id INT PRIMARY KEY)")
diff --git a/tests/test_rds_data.py b/tests/test_rds_data.py
index ca2d914a..d7e158c4 100644
--- a/tests/test_rds_data.py
+++ b/tests/test_rds_data.py
@@ -158,6 +158,7 @@ def test_rds_data_resolves_cluster_arn_by_arn_region(rds, sm):
DBClusterIdentifier=cluster_id,
Engine="aurora-mysql",
EngineMode="serverless",
+ EnableHttpEndpoint=True,
MasterUsername="admin",
MasterUserPassword="testpass123",
)
@@ -165,6 +166,7 @@ def test_rds_data_resolves_cluster_arn_by_arn_region(rds, sm):
DBClusterIdentifier=cluster_id,
Engine="aurora-mysql",
EngineMode="serverless",
+ EnableHttpEndpoint=True,
MasterUsername="admin",
MasterUserPassword="testpass123",
)
@@ -375,16 +377,22 @@ def test_rollback_missing_transaction_id():
assert "transactionId" in body.get("message", body.get("Message", ""))
-def test_commit_nonexistent_transaction():
+def test_commit_nonexistent_transaction(rds, sm):
+ resource_arn, secret_arn = _setup_stub_cluster(rds, sm)
status, body = _raw_post("/CommitTransaction", {
+ "resourceArn": resource_arn,
+ "secretArn": secret_arn,
"transactionId": "nonexistent-txn-id",
})
assert status == 404
assert "not found" in body.get("message", body.get("Message", "")).lower()
-def test_rollback_nonexistent_transaction():
+def test_rollback_nonexistent_transaction(rds, sm):
+ resource_arn, secret_arn = _setup_stub_cluster(rds, sm)
status, body = _raw_post("/RollbackTransaction", {
+ "resourceArn": resource_arn,
+ "secretArn": secret_arn,
"transactionId": "nonexistent-txn-id",
})
assert status == 404
@@ -503,6 +511,7 @@ def _setup_stub_cluster(rds, sm):
DBClusterIdentifier=cluster_id,
Engine="aurora-mysql",
EngineMode="serverless",
+ EnableHttpEndpoint=True,
MasterUsername="admin",
MasterUserPassword="testpass123",
)
@@ -523,6 +532,74 @@ def _exec(cluster_arn, secret_arn, sql):
})
+def _setup_stub_cluster_state():
+ from ministack.services import rds, rds_data, secretsmanager
+
+ cluster_id = f"stub-state-{uuid.uuid4().hex[:8]}"
+ secret_name = f"stub-state-secret-{uuid.uuid4().hex[:8]}"
+ cluster_arn = f"arn:aws:rds:{REGION}:{ACCOUNT_ID}:cluster:{cluster_id}"
+ secret_arn = f"arn:aws:secretsmanager:{REGION}:{ACCOUNT_ID}:secret:{secret_name}"
+ rds_data.reset()
+ rds._clusters[cluster_id] = {
+ "DBClusterIdentifier": cluster_id,
+ "DBClusterArn": cluster_arn,
+ "Engine": "aurora-mysql",
+ "EngineMode": "serverless",
+ "HttpEndpointEnabled": True,
+ }
+ secretsmanager._secrets[secret_name] = {
+ "ARN": secret_arn,
+ "Name": secret_name,
+ "Versions": {
+ "v1": {
+ "Stages": ["AWSCURRENT"],
+ "SecretString": '{"username":"admin","password":"testpass123"}',
+ },
+ },
+ }
+ return cluster_id, cluster_arn, secret_name, secret_arn
+
+
+ "stored_resource_arn",
+ (None, "other-resource"),
+ ids=("bogus", "cross-resource"),
+)
+def test_rds_data_stub_rejects_invalid_transaction_id(stored_resource_arn):
+ from ministack.services import rds, rds_data, secretsmanager
+
+ cluster_id, cluster_arn, secret_name, secret_arn = _setup_stub_cluster_state()
+ txn_id = f"stub-invalid-txn-{uuid.uuid4().hex}"
+ if stored_resource_arn is not None:
+ rds_data._transactions[txn_id] = {
+ "resourceArn": f"{cluster_arn}-{stored_resource_arn}",
+ "conn": object(),
+ }
+
+ try:
+ status, headers, body = rds_data._execute_statement({
+ "resourceArn": cluster_arn,
+ "secretArn": secret_arn,
+ "transactionId": txn_id,
+ "sql": "CREATE DATABASE should_not_write",
+ })
+ payload = json.loads(body)
+
+ assert status == 404
+ assert headers["x-amzn-errortype"] == "TransactionNotFoundException"
+ assert payload["__type"] == "TransactionNotFoundException"
+ assert payload["message"] == f"Transaction {txn_id} not found"
+ assert "should_not_write" not in rds_data._stub_databases.get(
+ rds_data._cluster_id_from_arn(cluster_arn), set()
+ )
+ if stored_resource_arn is not None:
+ assert txn_id in rds_data._transactions
+ finally:
+ rds_data.reset()
+ rds._clusters.pop(cluster_id, None)
+ secretsmanager._secrets.pop(secret_name, None)
+
+
def test_rds_data_stub_create_and_query_databases(rds, sm):
"""CREATE DATABASE via stub, then query information_schema.schemata."""
cluster_arn, secret_arn = _setup_stub_cluster(rds, sm)
@@ -572,6 +649,398 @@ def test_rds_data_stub_grant_and_show_grants(rds, sm):
assert any("GRANT" in g and "grantee" in g for g in grants)
+def test_rds_data_stub_show_grants_accepts_host_qualified_user():
+ """The provider asks for SHOW GRANTS using the MySQL 'user'@'host' shape."""
+ from ministack.services import rds, rds_data, secretsmanager
+
+ cluster_id, cluster_arn, secret_name, secret_arn = _setup_stub_cluster_state()
+ try:
+ rds_data._execute_statement({
+ "resourceArn": cluster_arn,
+ "secretArn": secret_arn,
+ "sql": "CREATE USER 'grantee'@'%' IDENTIFIED BY 'pass'",
+ })
+ rds_data._execute_statement({
+ "resourceArn": cluster_arn,
+ "secretArn": secret_arn,
+ "sql": "GRANT SELECT ON appdb.* TO 'grantee'@'%'",
+ })
+
+ status, _headers, body = rds_data._execute_statement({
+ "resourceArn": cluster_arn,
+ "secretArn": secret_arn,
+ "sql": "SHOW GRANTS FOR 'grantee'@'%'",
+ })
+ payload = json.loads(body)
+
+ assert status == 200
+ grants = [r[0]["stringValue"] for r in payload.get("records", [])]
+ assert grants
+ assert any("GRANT" in g and "grantee" in g for g in grants)
+ finally:
+ rds._clusters.pop(cluster_id, None)
+ secretsmanager._secrets.pop(secret_name, None)
+
+
+def test_rds_data_rejects_disabled_http_endpoint_before_stub():
+ from ministack.services import rds, rds_data
+
+ cluster_id = f"disabled-endpoint-{uuid.uuid4().hex[:8]}"
+ resource_arn = f"arn:aws:rds:{REGION}:{ACCOUNT_ID}:cluster:{cluster_id}"
+ rds._clusters[cluster_id] = {
+ "DBClusterIdentifier": cluster_id,
+ "DBClusterArn": resource_arn,
+ "Engine": "aurora-mysql",
+ "EngineMode": "serverless",
+ "HttpEndpointEnabled": False,
+ }
+
+ try:
+ common = {
+ "resourceArn": resource_arn,
+ "secretArn": FAKE_SECRET_ARN,
+ }
+ cases = (
+ (rds_data._execute_statement, {"sql": "SELECT 1"}),
+ (rds_data._begin_transaction, {}),
+ (rds_data._batch_execute_statement, {"sql": "SELECT 1"}),
+ )
+ for handler, extra in cases:
+ status, headers, body = handler({**common, **extra})
+ payload = json.loads(body)
+ assert status == 400
+ assert headers["x-amzn-errortype"] == "HttpEndpointNotEnabledException"
+ assert payload["__type"] == "HttpEndpointNotEnabledException"
+ finally:
+ rds._clusters.pop(cluster_id, None)
+
+
+def test_rds_data_member_instance_arn_honors_parent_http_endpoint_gate():
+ from ministack.services import rds, rds_data, secretsmanager
+
+ cluster_id, _cluster_arn, secret_name, secret_arn = _setup_stub_cluster_state()
+ instance_id = f"disabled-member-{uuid.uuid4().hex[:8]}"
+ instance_arn = f"arn:aws:rds:{REGION}:{ACCOUNT_ID}:db:{instance_id}"
+ rds._clusters[cluster_id]["HttpEndpointEnabled"] = False
+ rds._instances[instance_id] = {
+ "DBInstanceIdentifier": instance_id,
+ "DBInstanceArn": instance_arn,
+ "DBClusterIdentifier": cluster_id,
+ "DBInstanceStatus": "available",
+ "Engine": "aurora-mysql",
+ }
+
+ try:
+ status, headers, body = rds_data._execute_statement({
+ "resourceArn": instance_arn,
+ "secretArn": secret_arn,
+ "sql": "SHOW DATABASES",
+ })
+ payload = json.loads(body)
+
+ assert status == 400
+ assert headers["x-amzn-errortype"] == "HttpEndpointNotEnabledException"
+ assert payload["__type"] == "HttpEndpointNotEnabledException"
+ finally:
+ rds._instances.pop(instance_id, None)
+ rds._clusters.pop(cluster_id, None)
+ secretsmanager._secrets.pop(secret_name, None)
+
+
+ "handler_name",
+ ("_commit_transaction", "_rollback_transaction"),
+)
+def test_transaction_completion_requires_resource_and_secret_arns(handler_name):
+ from ministack.services import rds_data
+
+ class _Connection:
+ def commit(self):
+ pass
+
+ def rollback(self):
+ pass
+
+ def close(self):
+ pass
+
+ rds_data.reset()
+ txn_id = f"txn-required-arns-{uuid.uuid4().hex}"
+ rds_data._transactions[txn_id] = {"conn": _Connection()}
+ handler = getattr(rds_data, handler_name)
+
+ try:
+ status, _headers, body = handler({"transactionId": txn_id})
+ payload = json.loads(body)
+ assert status == 400
+ assert payload["__type"] == "BadRequestException"
+ assert "resourceArn" in payload["message"]
+ assert txn_id in rds_data._transactions
+
+ status, _headers, body = handler({
+ "transactionId": txn_id,
+ "resourceArn": FAKE_CLUSTER_ARN,
+ })
+ payload = json.loads(body)
+ assert status == 400
+ assert payload["__type"] == "BadRequestException"
+ assert "secretArn" in payload["message"]
+ assert txn_id in rds_data._transactions
+ finally:
+ rds_data._transactions.pop(txn_id, None)
+
+
+ "handler_name",
+ ("_commit_transaction", "_rollback_transaction"),
+)
+def test_transaction_completion_honors_http_endpoint_gate(handler_name):
+ from ministack.services import rds, rds_data, secretsmanager
+
+ class _Connection:
+ def commit(self):
+ pass
+
+ def rollback(self):
+ pass
+
+ def close(self):
+ pass
+
+ cluster_id, cluster_arn, secret_name, secret_arn = _setup_stub_cluster_state()
+ rds._clusters[cluster_id]["HttpEndpointEnabled"] = False
+ txn_id = f"txn-disabled-endpoint-{uuid.uuid4().hex}"
+ rds_data._transactions[txn_id] = {"conn": _Connection()}
+ handler = getattr(rds_data, handler_name)
+
+ try:
+ status, headers, body = handler({
+ "transactionId": txn_id,
+ "resourceArn": cluster_arn,
+ "secretArn": secret_arn,
+ })
+ payload = json.loads(body)
+ assert status == 400
+ assert headers["x-amzn-errortype"] == "HttpEndpointNotEnabledException"
+ assert payload["__type"] == "HttpEndpointNotEnabledException"
+ assert txn_id in rds_data._transactions
+ finally:
+ rds_data._transactions.pop(txn_id, None)
+ rds._clusters.pop(cluster_id, None)
+ secretsmanager._secrets.pop(secret_name, None)
+
+
+ ("handler_name", "extra", "expected_error"),
+ (
+ ("_commit_transaction", {}, "NotFoundException"),
+ ("_rollback_transaction", {}, "NotFoundException"),
+ (
+ "_execute_statement",
+ {"sql": "SHOW DATABASES"},
+ "TransactionNotFoundException",
+ ),
+ (
+ "_batch_execute_statement",
+ {"sql": "SHOW DATABASES"},
+ "TransactionNotFoundException",
+ ),
+ ),
+ ids=("commit", "rollback", "execute", "batch"),
+)
+def test_transaction_is_bound_to_resource_arn(
+ monkeypatch,
+ handler_name,
+ extra,
+ expected_error,
+):
+ from ministack.services import rds_data
+
+ class _Cursor:
+ rowcount = 0
+ description = None
+
+ def execute(self, *_args, **_kwargs):
+ pass
+
+ def close(self):
+ pass
+
+ class _Connection:
+ def __init__(self):
+ self.commits = 0
+ self.rollbacks = 0
+
+ def autocommit(self, _enabled):
+ pass
+
+ def cursor(self):
+ return _Cursor()
+
+ def commit(self):
+ self.commits += 1
+
+ def rollback(self):
+ self.rollbacks += 1
+
+ def close(self):
+ pass
+
+ cluster_a_arn = f"arn:aws:rds:{REGION}:{ACCOUNT_ID}:cluster:txn-cluster-a"
+ cluster_b_arn = f"arn:aws:rds:{REGION}:{ACCOUNT_ID}:cluster:txn-cluster-b"
+ instance = {
+ "Endpoint": {"Address": "127.0.0.1", "Port": 3306},
+ "_docker_container_id": "txn-test-container",
+ "Engine": "aurora-mysql",
+ }
+ clusters = {
+ cluster_a_arn: {"DBClusterIdentifier": "txn-cluster-a", "HttpEndpointEnabled": True},
+ cluster_b_arn: {"DBClusterIdentifier": "txn-cluster-b", "HttpEndpointEnabled": True},
+ }
+ connection = _Connection()
+
+ monkeypatch.setattr(
+ rds_data,
+ "_resolve_target",
+ lambda resource_arn: (instance, "aurora-mysql", clusters[resource_arn]),
+ )
+ monkeypatch.setattr(
+ rds_data,
+ "_require_secret_credentials",
+ lambda _secret_arn: ("admin", "password", None),
+ )
+ monkeypatch.setattr(rds_data, "_connect", lambda *_args, **_kwargs: connection)
+ rds_data.reset()
+
+ try:
+ status, _headers, body = rds_data._begin_transaction({
+ "resourceArn": cluster_a_arn,
+ "secretArn": FAKE_SECRET_ARN,
+ })
+ assert status == 200
+ txn_id = json.loads(body)["transactionId"]
+
+ handler = getattr(rds_data, handler_name)
+ status, headers, body = handler({
+ "resourceArn": cluster_b_arn,
+ "secretArn": FAKE_SECRET_ARN,
+ "transactionId": txn_id,
+ **extra,
+ })
+ payload = json.loads(body)
+
+ assert status == 404
+ assert headers["x-amzn-errortype"] == expected_error
+ assert payload["__type"] == expected_error
+ assert payload["message"] == f"Transaction {txn_id} not found"
+ assert txn_id in rds_data._transactions
+ assert connection.commits == 0
+ assert connection.rollbacks == 0
+
+ if expected_error == "TransactionNotFoundException":
+ rds_data._transactions.pop(txn_id)
+ status, headers, body = handler({
+ "resourceArn": cluster_a_arn,
+ "secretArn": FAKE_SECRET_ARN,
+ "transactionId": txn_id,
+ **extra,
+ })
+ payload = json.loads(body)
+ assert status == 404
+ assert headers["x-amzn-errortype"] == "TransactionNotFoundException"
+ assert payload["__type"] == "TransactionNotFoundException"
+ assert payload["message"] == f"Transaction {txn_id} not found"
+ finally:
+ rds_data.reset()
+
+
+def test_rds_data_rejects_missing_secret_before_password_fallback():
+ from ministack.services import rds, rds_data
+
+ cluster_id, cluster_arn, secret_name, _secret_arn = _setup_stub_cluster_state()
+
+ try:
+ status, headers, body = rds_data._execute_statement({
+ "resourceArn": cluster_arn,
+ "secretArn": FAKE_SECRET_ARN,
+ "sql": "CREATE DATABASE should_not_write",
+ })
+ payload = json.loads(body)
+ assert status == 400
+ assert headers["x-amzn-errortype"] == "SecretsErrorException"
+ assert payload["__type"] == "SecretsErrorException"
+ assert "secret" in payload["message"].lower()
+ finally:
+ rds._clusters.pop(cluster_id, None)
+ from ministack.services import secretsmanager
+ secretsmanager._secrets.pop(secret_name, None)
+
+
+def test_rds_data_rejects_secret_without_password_before_password_fallback():
+ from ministack.services import rds, rds_data, secretsmanager
+
+ cluster_id, cluster_arn, secret_name, secret_arn = _setup_stub_cluster_state()
+ secretsmanager._secrets[secret_name]["Versions"]["v1"]["SecretString"] = '{"username":"admin"}'
+
+ try:
+ status, headers, body = rds_data._execute_statement({
+ "resourceArn": cluster_arn,
+ "secretArn": secret_arn,
+ "sql": "CREATE DATABASE should_not_write",
+ })
+ payload = json.loads(body)
+ assert status == 400
+ assert headers["x-amzn-errortype"] == "InvalidSecretException"
+ assert payload["__type"] == "InvalidSecretException"
+ assert "password" in payload["message"].lower()
+ finally:
+ rds._clusters.pop(cluster_id, None)
+ secretsmanager._secrets.pop(secret_name, None)
+
+
+def test_rds_data_rejects_secret_scheduled_for_deletion():
+ from ministack.services import rds, rds_data, secretsmanager
+
+ cluster_id, cluster_arn, secret_name, secret_arn = _setup_stub_cluster_state()
+ secretsmanager._secrets[secret_name]["DeletedDate"] = "2026-08-10T00:00:00Z"
+
+ try:
+ status, headers, body = rds_data._execute_statement({
+ "resourceArn": cluster_arn,
+ "secretArn": secret_arn,
+ "sql": "SHOW DATABASES",
+ })
+ payload = json.loads(body)
+
+ assert status == 400
+ assert headers["x-amzn-errortype"] == "SecretsErrorException"
+ assert payload["__type"] == "SecretsErrorException"
+ assert "scheduled for deletion" in payload["message"]
+ finally:
+ rds._clusters.pop(cluster_id, None)
+ secretsmanager._secrets.pop(secret_name, None)
+
+
+def test_rds_data_stub_rejects_unrecognized_sql():
+ from ministack.services import rds, rds_data, secretsmanager
+
+ cluster_id, cluster_arn, secret_name, secret_arn = _setup_stub_cluster_state()
+ try:
+ status, _headers, body = rds_data._execute_statement({
+ "resourceArn": cluster_arn,
+ "secretArn": secret_arn,
+ "sql": "SELECT definitely_unmodeled()",
+ })
+ payload = json.loads(body)
+
+ assert status == 400
+ assert payload["__type"] == "BadRequestException"
+ assert "unsupported" in payload["message"].lower()
+ finally:
+ rds._clusters.pop(cluster_id, None)
+ secretsmanager._secrets.pop(secret_name, None)
+
+
def test_rds_data_stub_drop_database(rds, sm):
"""CREATE then DROP DATABASE, verify gone from queries."""
cluster_arn, secret_arn = _setup_stub_cluster(rds, sm)
@@ -617,9 +1086,18 @@ def test_rds_data_real_endpoint_connection_failure_is_transient(monkeypatch):
"_docker_container_id": "container-id",
"Engine": "aurora-mysql",
}
+ cluster = {
+ "DBClusterIdentifier": "real-cluster",
+ "DBClusterArn": resource_arn,
+ "HttpEndpointEnabled": True,
+ }
- monkeypatch.setattr(rds_data, "_resolve_cluster", lambda _arn: (instance, "aurora-mysql"))
- monkeypatch.setattr(rds_data, "_get_secret_credentials", lambda _arn: ("admin", "pw"))
+ monkeypatch.setattr(
+ rds_data,
+ "_resolve_target",
+ lambda _arn: (instance, "aurora-mysql", cluster),
+ )
+ monkeypatch.setattr(rds_data, "_require_secret_credentials", lambda _arn: ("admin", "pw", None))
def _raise_connection_error(*_args, **_kwargs):
raise OSError("Connection refused")
@@ -650,6 +1128,7 @@ def test_rds_data_provisioned_cluster_without_members_is_unavailable():
"DBClusterArn": resource_arn,
"Engine": "aurora-mysql",
"EngineMode": "provisioned",
+ "HttpEndpointEnabled": True,
"Status": "available",
"DBClusterMembers": [],
"_shared_container_ready": False,
@@ -677,6 +1156,46 @@ def test_rds_data_provisioned_cluster_without_members_is_unavailable():
rds._clusters.pop(cluster_id, None)
+ ("handler_name", "extra"),
+ (
+ ("_execute_statement", {"sql": "SHOW DATABASES"}),
+ ("_begin_transaction", {}),
+ ("_batch_execute_statement", {"sql": "SHOW DATABASES"}),
+ ),
+ ids=("execute", "begin", "batch"),
+)
+def test_rds_data_disabled_endpoint_precedes_missing_compute(handler_name, extra):
+ from ministack.services import rds, rds_data
+
+ cluster_id = f"disabled-empty-provisioned-{uuid.uuid4().hex[:8]}"
+ resource_arn = f"arn:aws:rds:{REGION}:{ACCOUNT_ID}:cluster:{cluster_id}"
+ rds._clusters[cluster_id] = {
+ "DBClusterIdentifier": cluster_id,
+ "DBClusterArn": resource_arn,
+ "Engine": "aurora-mysql",
+ "EngineMode": "provisioned",
+ "HttpEndpointEnabled": False,
+ "Status": "available",
+ "DBClusterMembers": [],
+ "_shared_container_ready": False,
+ }
+
+ try:
+ handler = getattr(rds_data, handler_name)
+ status, headers, body = handler({
+ "resourceArn": resource_arn,
+ "secretArn": FAKE_SECRET_ARN,
+ **extra,
+ })
+ payload = json.loads(body)
+ assert status == 400
+ assert headers["x-amzn-errortype"] == "HttpEndpointNotEnabledException"
+ assert payload["__type"] == "HttpEndpointNotEnabledException"
+ finally:
+ rds._clusters.pop(cluster_id, None)
+
+
def test_rds_data_non_container_endpoint_keeps_stub_mode(monkeypatch):
"""Control-plane-only instances still use the lightweight SQL stub."""
from ministack.services import rds_data
@@ -688,7 +1207,17 @@ def test_rds_data_non_container_endpoint_keeps_stub_mode(monkeypatch):
"_docker_container_id": None,
"Engine": "aurora-mysql",
}
- monkeypatch.setattr(rds_data, "_resolve_cluster", lambda _arn: (instance, "aurora-mysql"))
+ cluster = {
+ "DBClusterIdentifier": "stub-cluster",
+ "DBClusterArn": resource_arn,
+ "HttpEndpointEnabled": True,
+ }
+ monkeypatch.setattr(
+ rds_data,
+ "_resolve_target",
+ lambda _arn: (instance, "aurora-mysql", cluster),
+ )
+ monkeypatch.setattr(rds_data, "_require_secret_credentials", lambda _arn: ("admin", "pw", None))
status, _headers, body = rds_data._execute_statement({
"resourceArn": resource_arn,
@@ -709,9 +1238,16 @@ def test_rds_data_lock_wait_timeout_is_not_connection_error():
"""MySQL lock wait timeout is a SQL error, not endpoint unavailability."""
from ministack.services import rds_data
- assert not rds_data._is_connection_error(
- Exception("(1205, 'Lock wait timeout exceeded; try restarting transaction')")
- )
+ error = Exception("(1205, 'Lock wait timeout exceeded; try restarting transaction')")
+
+ assert not rds_data._is_connection_error(error)
+
+ status, headers, body = rds_data._sql_execution_error(error)
+ payload = json.loads(body)
+ assert status == 400
+ assert headers["x-amzn-errortype"] == "BadRequestException"
+ assert payload["__type"] == "BadRequestException"
+ assert payload["message"].startswith("Database error:")
def test_rds_cluster_status_tracks_member_readiness():
diff --git a/tests/test_stepfunctions.py b/tests/test_stepfunctions.py
index a331e979..ae0380f3 100644
--- a/tests/test_stepfunctions.py
+++ b/tests/test_stepfunctions.py
@@ -5037,6 +5037,7 @@ def test_sfn_aws_sdk_rdsdata_execute_statement(sfn, sfn_sync, rds, sm):
EngineMode="serverless",
MasterUsername="admin",
MasterUserPassword="testpass123",
+ EnableHttpEndpoint=True,
)
secret_arn = sm.create_secret(
Name=f"rdsdata-secret-{_uuid.uuid4().hex[:8]}",
@@ -5053,7 +5054,7 @@ def test_sfn_aws_sdk_rdsdata_execute_statement(sfn, sfn_sync, rds, sm):
"Parameters": {
"resourceArn": cluster_arn,
"secretArn": secret_arn,
- "sql": "SELECT 1",
+ "sql": "SHOW DATABASES",
"database": "testdb",
},
"End": True,
@@ -5119,6 +5120,7 @@ def test_sfn_aws_sdk_rdsdata_output_uses_sfn_key_convention(sfn, sfn_sync, rds,
EngineMode="serverless",
MasterUsername="admin",
MasterUserPassword="testpass123",
+ EnableHttpEndpoint=True,
)
secret_arn = sm.create_secret(
Name=f"rdsdata-output-secret-{_uuid.uuid4().hex[:8]}",
@@ -5514,6 +5516,7 @@ def test_sfn_rest_json_pascal_to_camel_conversion(sfn, sfn_sync, rds, sm):
EngineMode="serverless",
MasterUsername="admin",
MasterUserPassword="testpass123",
+ EnableHttpEndpoint=True,
)
secret_arn = sm.create_secret(
Name=f"rdsdata-camel-secret-{_uuid.uuid4().hex[:8]}",
@@ -5531,7 +5534,7 @@ def test_sfn_rest_json_pascal_to_camel_conversion(sfn, sfn_sync, rds, sm):
"Parameters": {
"ResourceArn": cluster_arn,
"SecretArn": secret_arn,
- "Sql": "SELECT 1",
+ "Sql": "SHOW DATABASES",
"Database": "testdb",
},
"End": True,