Repository navigation
Expand file tree
/
Copy pathtest_rds_data.py
More file actions
1564 lines (1307 loc) · 55.8 KB
/
Copy pathtest_rds_data.py
File metadata and controls
1564 lines (1307 loc) · 55.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
Tests for RDS Data API service emulator.
Since no real DB containers are available in CI, these tests focus on:
- API routing (requests reach the handler, not 404)
- Parameter validation (missing resourceArn, missing sql, etc.)
- Transaction lifecycle error paths
- Invalid resource ARN handling
"""
import json
import os
import urllib.request
import uuid
from types import SimpleNamespace
from unittest.mock import Mock
import boto3
import pytest
from botocore.config import Config
from botocore.exceptions import ClientError
ENDPOINT = os.environ.get("MINISTACK_ENDPOINT", "http://localhost:4566")
REGION = "us-east-1"
ACCOUNT_ID = "000000000000"
FAKE_CLUSTER_ARN = f"arn:aws:rds:{REGION}:{ACCOUNT_ID}:cluster:nonexistent-cluster"
FAKE_SECRET_ARN = f"arn:aws:secretsmanager:{REGION}:{ACCOUNT_ID}:secret:nonexistent-secret"
def _raw_post(path, body):
"""Send a raw POST to the MiniStack endpoint (bypassing boto3 since
rds-data uses REST paths like /Execute)."""
data = json.dumps(body).encode()
req = urllib.request.Request(
f"{ENDPOINT}{path}",
data=data,
headers={"Content-Type": "application/json"},
method="POST",
)
# 30s instead of 10s: under CI xdist contention the server's first
# call into _resolve_cluster triggers a lazy `from ministack.services
# import rds` whose import-time block can exceed 10s on the shared
# 2-core Linux runner. Handler itself is sub-ms once rds is loaded,
# so 30s leaves a wide margin without making real failures slow.
try:
resp = urllib.request.urlopen(req, timeout=30)
return resp.status, json.loads(resp.read())
except urllib.error.HTTPError as e:
return e.code, json.loads(e.read())
def _regional_client(service, region, access_key_id="test"):
return boto3.client(
service,
endpoint_url=ENDPOINT,
aws_access_key_id=access_key_id,
aws_secret_access_key="test",
region_name=region,
config=Config(region_name=region, retries={"mode": "standard"}),
)
# ── Routing tests ──────────────────────────────────────────
def test_execute_route_exists():
"""POST /Execute reaches the rds-data handler (not a 404)."""
status, body = _raw_post("/Execute", {})
# Should get a 400 (missing params), not 404
assert status == 400
assert "BadRequestException" in str(body) or "resourceArn" in str(body)
def test_begin_transaction_route_exists():
"""POST /BeginTransaction reaches the rds-data handler."""
status, body = _raw_post("/BeginTransaction", {})
assert status == 400
def test_commit_transaction_route_exists():
"""POST /CommitTransaction reaches the rds-data handler."""
status, body = _raw_post("/CommitTransaction", {})
assert status == 400
def test_rollback_transaction_route_exists():
"""POST /RollbackTransaction reaches the rds-data handler."""
status, body = _raw_post("/RollbackTransaction", {})
assert status == 400
def test_batch_execute_route_exists():
"""POST /BatchExecute reaches the rds-data handler."""
status, body = _raw_post("/BatchExecute", {})
assert status == 400
# ── Parameter validation ───────────────────────────────────
def test_execute_missing_resource_arn():
status, body = _raw_post("/Execute", {
"secretArn": FAKE_SECRET_ARN,
"sql": "SELECT 1",
})
assert status == 400
assert "resourceArn" in body.get("message", body.get("Message", ""))
def test_execute_missing_secret_arn():
status, body = _raw_post("/Execute", {
"resourceArn": FAKE_CLUSTER_ARN,
"sql": "SELECT 1",
})
assert status == 400
assert "secretArn" in body.get("message", body.get("Message", ""))
def test_execute_missing_sql():
status, body = _raw_post("/Execute", {
"resourceArn": FAKE_CLUSTER_ARN,
"secretArn": FAKE_SECRET_ARN,
})
assert status == 400
assert "sql" in body.get("message", body.get("Message", ""))
def test_batch_execute_missing_sql():
status, body = _raw_post("/BatchExecute", {
"resourceArn": FAKE_CLUSTER_ARN,
"secretArn": FAKE_SECRET_ARN,
})
assert status == 400
assert "sql" in body.get("message", body.get("Message", ""))
# ── Invalid ARN ────────────────────────────────────────────
def test_execute_nonexistent_cluster():
"""ExecuteStatement with a non-existent cluster ARN returns an error."""
status, body = _raw_post("/Execute", {
"resourceArn": FAKE_CLUSTER_ARN,
"secretArn": FAKE_SECRET_ARN,
"sql": "SELECT 1",
})
assert status == 400
assert "not found" in body.get("message", body.get("Message", "")).lower()
def test_rds_data_resolves_cluster_arn_by_arn_region(rds, sm):
east_rds = _regional_client("rds", "us-east-1")
west_rds = _regional_client("rds", "us-west-2")
east_data = _regional_client("rds-data", "us-east-1")
cluster_id = f"rds-data-region-{uuid.uuid4().hex[:8]}"
secret_arn = sm.create_secret(
Name=f"rds-data-region-secret-{uuid.uuid4().hex[:8]}",
SecretString='{"username":"admin","password":"testpass123"}',
)["ARN"]
try:
east_rds.create_db_cluster(
DBClusterIdentifier=cluster_id,
Engine="aurora-mysql",
EngineMode="serverless",
EnableHttpEndpoint=True,
MasterUsername="admin",
MasterUserPassword="testpass123",
)
west_rds.create_db_cluster(
DBClusterIdentifier=cluster_id,
Engine="aurora-mysql",
EngineMode="serverless",
EnableHttpEndpoint=True,
MasterUsername="admin",
MasterUserPassword="testpass123",
)
east_arn = east_rds.describe_db_clusters(DBClusterIdentifier=cluster_id)["DBClusters"][0]["DBClusterArn"]
west_arn = west_rds.describe_db_clusters(DBClusterIdentifier=cluster_id)["DBClusters"][0]["DBClusterArn"]
east_data.execute_statement(
resourceArn=west_arn,
secretArn=secret_arn,
sql="CREATE DATABASE west_only_db",
)
east_resp = east_data.execute_statement(
resourceArn=east_arn,
secretArn=secret_arn,
sql="SHOW DATABASES",
)
west_resp = east_data.execute_statement(
resourceArn=west_arn,
secretArn=secret_arn,
sql="SHOW DATABASES",
)
east_names = [r[0]["stringValue"] for r in east_resp.get("records", [])]
west_names = [r[0]["stringValue"] for r in west_resp.get("records", [])]
assert "west_only_db" not in east_names
assert "west_only_db" in west_names
finally:
for client in (east_rds, west_rds):
try:
client.delete_db_cluster(DBClusterIdentifier=cluster_id, SkipFinalSnapshot=True)
except ClientError:
pass
def test_execute_rejects_foreign_account_cluster_arn():
"""ExecuteStatement should not resolve a cluster ARN from another account."""
owner_account = "111111111111"
owner = _regional_client("rds", REGION, access_key_id=owner_account)
cluster_id = f"rds-data-foreign-account-{uuid.uuid4().hex[:8]}"
try:
cluster = owner.create_db_cluster(
DBClusterIdentifier=cluster_id,
Engine="aurora-mysql",
MasterUsername="admin",
MasterUserPassword="password123",
)["DBCluster"]
assert f":{owner_account}:" in cluster["DBClusterArn"]
status, body = _raw_post("/Execute", {
"resourceArn": cluster["DBClusterArn"],
"secretArn": FAKE_SECRET_ARN,
"sql": "SELECT 1",
})
assert status == 400
assert "not found" in body.get("message", body.get("Message", "")).lower()
finally:
try:
owner.delete_db_cluster(DBClusterIdentifier=cluster_id, SkipFinalSnapshot=True)
except ClientError:
pass
def test_cluster_id_from_arn_includes_account_region_and_resource_tail():
from ministack.services import rds_data
assert (
rds_data._cluster_id_from_arn(
"arn:aws:rds:us-east-1:000000000000:cluster:cluster-id:tail",
)
== "000000000000:us-east-1:cluster:cluster-id:tail"
)
def test_rds_data_member_lookup_uses_resource_arn_region():
from ministack.core.responses import (
get_account_id,
get_region,
set_request_account_id,
set_request_region,
)
from ministack.services import rds, rds_data
original_account = get_account_id()
original_region = get_region()
cluster_id = f"rds-data-member-{uuid.uuid4().hex[:8]}"
east_arn = f"arn:aws:rds:us-east-1:{ACCOUNT_ID}:cluster:{cluster_id}"
west_arn = f"arn:aws:rds:us-west-2:{ACCOUNT_ID}:cluster:{cluster_id}"
east_instance = {
"DBInstanceIdentifier": f"{cluster_id}-east",
"DBClusterIdentifier": cluster_id,
"DBInstanceStatus": "available",
"Engine": "aurora-mysql",
"Endpoint": {"Address": "east.example", "Port": 3306},
}
west_instance = {
"DBInstanceIdentifier": f"{cluster_id}-west",
"DBClusterIdentifier": cluster_id,
"DBInstanceStatus": "available",
"Engine": "aurora-mysql",
}
try:
set_request_account_id(ACCOUNT_ID)
rds.reset()
rds._clusters.set_scoped(
ACCOUNT_ID,
"us-east-1",
cluster_id,
{"DBClusterIdentifier": cluster_id, "DBClusterArn": east_arn, "Engine": "aurora-mysql"},
)
rds._clusters.set_scoped(
ACCOUNT_ID,
"us-west-2",
cluster_id,
{
"DBClusterIdentifier": cluster_id,
"DBClusterArn": west_arn,
"Engine": "aurora-mysql",
"EngineMode": "provisioned",
"DBClusterMembers": [{
"DBInstanceIdentifier": west_instance["DBInstanceIdentifier"],
"IsClusterWriter": True,
}],
"_shared_container_ready": True,
"_shared_endpoint": {"Address": "west.example", "Port": 3306},
"_shared_container_id": "west-shared-container",
"_shared_internal_address": "172.20.0.8",
"_shared_internal_port": 3306,
},
)
rds._instances.set_scoped(ACCOUNT_ID, "us-east-1", east_instance["DBInstanceIdentifier"], east_instance)
rds._instances.set_scoped(ACCOUNT_ID, "us-west-2", west_instance["DBInstanceIdentifier"], west_instance)
set_request_region("us-east-1")
instance, engine = rds_data._resolve_cluster(west_arn)
assert instance is west_instance
assert engine == "aurora-mysql"
assert instance["Endpoint"] == {"Address": "west.example", "Port": 3306}
assert instance["_docker_container_id"] == "west-shared-container"
assert instance["_internal_address"] == "172.20.0.8"
assert instance["_internal_port"] == 3306
finally:
rds.reset()
set_request_account_id(original_account)
set_request_region(original_region)
def test_rds_data_db_arn_lookup_rejects_foreign_account():
from ministack.core.responses import (
get_account_id,
set_request_account_id,
)
from ministack.services import rds, rds_data
original_account = get_account_id()
db_id = f"rds-data-db-{uuid.uuid4().hex[:8]}"
db_arn = f"arn:aws:rds:us-east-1:111111111111:db:{db_id}"
instance = {
"DBInstanceIdentifier": db_id,
"Engine": "aurora-mysql",
"Endpoint": {"Address": "foreign.example", "Port": 3306},
}
try:
rds.reset()
rds._instances.set_scoped("111111111111", "us-east-1", db_id, instance)
set_request_account_id(ACCOUNT_ID)
assert rds_data._resolve_cluster(db_arn) == (None, None)
finally:
rds.reset()
set_request_account_id(original_account)
def test_begin_transaction_nonexistent_cluster():
"""BeginTransaction with a non-existent cluster ARN returns an error."""
status, body = _raw_post("/BeginTransaction", {
"resourceArn": FAKE_CLUSTER_ARN,
"secretArn": FAKE_SECRET_ARN,
})
assert status == 400
assert "not found" in body.get("message", body.get("Message", "")).lower()
def test_batch_execute_nonexistent_cluster():
status, body = _raw_post("/BatchExecute", {
"resourceArn": FAKE_CLUSTER_ARN,
"secretArn": FAKE_SECRET_ARN,
"sql": "INSERT INTO t VALUES (1)",
})
assert status == 400
assert "not found" in body.get("message", body.get("Message", "")).lower()
# ── Transaction lifecycle (error paths) ────────────────────
def test_commit_missing_transaction_id():
status, body = _raw_post("/CommitTransaction", {})
assert status == 400
assert "transactionId" in body.get("message", body.get("Message", ""))
def test_rollback_missing_transaction_id():
status, body = _raw_post("/RollbackTransaction", {})
assert status == 400
assert "transactionId" in body.get("message", body.get("Message", ""))
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(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
assert "not found" in body.get("message", body.get("Message", "")).lower()
# ── Invalid JSON ───────────────────────────────────────────
def test_execute_invalid_json():
"""Malformed JSON body returns BadRequestException."""
req = urllib.request.Request(
f"{ENDPOINT}/Execute",
data=b"not-json{{{",
headers={"Content-Type": "application/json"},
method="POST",
)
try:
resp = urllib.request.urlopen(req, timeout=10)
status = resp.status
body = json.loads(resp.read())
except urllib.error.HTTPError as e:
status = e.code
body = json.loads(e.read())
assert status == 400
assert "Invalid JSON" in body.get("message", body.get("Message", ""))
# ── Parameter conversion (unit tests) ─────────────────────
def test_convert_parameters_all_types():
"""_convert_parameters handles all RDS Data API value types."""
from ministack.services.rds_data import _convert_parameters
params = [
{"name": "s", "value": {"stringValue": "hello"}},
{"name": "n", "value": {"longValue": 42}},
{"name": "d", "value": {"doubleValue": 3.14}},
{"name": "b", "value": {"booleanValue": True}},
{"name": "null_val", "value": {"isNull": True}},
{"name": "blob", "value": {"blobValue": "AQID"}}, # base64 of b'\x01\x02\x03'
]
result = _convert_parameters(params)
assert result["s"] == "hello"
assert result["n"] == 42
assert result["d"] == 3.14
assert result["b"] is True
assert result["null_val"] is None
assert result["blob"] == b"\x01\x02\x03"
def test_convert_parameters_empty():
"""_convert_parameters returns empty dict for empty/None input."""
from ministack.services.rds_data import _convert_parameters
assert _convert_parameters([]) == {}
assert _convert_parameters(None) == {}
def test_convert_parameters_missing_name_skipped():
"""Parameters without a name are skipped."""
from ministack.services.rds_data import _convert_parameters
params = [
{"value": {"stringValue": "no-name"}},
{"name": "valid", "value": {"stringValue": "ok"}},
]
result = _convert_parameters(params)
assert len(result) == 1
assert result["valid"] == "ok"
def test_convert_parameters_empty_value():
"""Parameter with empty value object returns None."""
from ministack.services.rds_data import _convert_parameters
result = _convert_parameters([{"name": "x", "value": {}}])
assert result["x"] is None
def test_substitute_named_params_distinct_numeric_tokens():
""":1 and :10 are distinct placeholders, not substring shadows (#957 bug 2).
A naive substring replace of ":1" first corrupts ":10" into "<val>0". Token
matching keeps them distinct regardless of supply order."""
from ministack.services.rds_data import _substitute_named_params
params = {"1": "ONE", "10": "TEN"}
assert _substitute_named_params("SELECT :1 AS one, :10 AS ten", params) == \
"SELECT %(1)s AS one, %(10)s AS ten"
# Order of the names in the dict must not change the result.
assert _substitute_named_params("SELECT :10 AS ten, :1 AS one", {"10": "TEN", "1": "ONE"}) == \
"SELECT %(10)s AS ten, %(1)s AS one"
def test_substitute_named_params_preserves_cast_and_unknowns():
"""A ::type cast is left intact and a :word that is not a supplied parameter
passes through unchanged instead of becoming a bad placeholder."""
from ministack.services.rds_data import _substitute_named_params
assert _substitute_named_params("SELECT :val::jsonb", {"val": "{}"}) == "SELECT %(val)s::jsonb"
assert _substitute_named_params("SELECT :foo", {"bar": 1}) == "SELECT :foo"
assert _substitute_named_params("SELECT 1", {}) == "SELECT 1"
# A longer unrelated :token must not be partially eaten by a shorter param
# name. Substring replace of ":id" would corrupt ":identity"; token matching
# leaves it whole because it is not a supplied parameter.
assert _substitute_named_params("SELECT :id, :identity", {"id": 1}) == "SELECT %(id)s, :identity"
# ── Stub mode tests ────────────────────────────────────────
def _setup_stub_cluster(rds, sm):
"""Create an Aurora Serverless cluster and secret for stub testing."""
import uuid as _uuid
cluster_id = f"stub-test-{_uuid.uuid4().hex[:8]}"
rds.create_db_cluster(
DBClusterIdentifier=cluster_id,
Engine="aurora-mysql",
EngineMode="serverless",
EnableHttpEndpoint=True,
MasterUsername="admin",
MasterUserPassword="testpass123",
)
secret_arn = sm.create_secret(
Name=f"stub-secret-{_uuid.uuid4().hex[:8]}",
SecretString='{"username":"admin","password":"testpass123"}',
)["ARN"]
cluster_arn = f"arn:aws:rds:{REGION}:{ACCOUNT_ID}:cluster:{cluster_id}"
return cluster_arn, secret_arn
def _exec(cluster_arn, secret_arn, sql):
"""Execute a SQL statement via the stub and return (status, body)."""
return _raw_post("/Execute", {
"resourceArn": cluster_arn,
"secretArn": secret_arn,
"sql": 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
@pytest.mark.parametrize(
"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)
status, _ = _exec(cluster_arn, secret_arn, "CREATE DATABASE myappdb")
assert status == 200
status, body = _exec(
cluster_arn, secret_arn,
"SELECT schema_name FROM information_schema.schemata WHERE schema_name IN ('myappdb')",
)
assert status == 200
names = [r[0]["stringValue"] for r in body.get("records", [])]
assert "myappdb" in names
def test_rds_data_stub_create_and_query_users(rds, sm):
"""CREATE USER via stub, then query mysql.user."""
cluster_arn, secret_arn = _setup_stub_cluster(rds, sm)
status, _ = _exec(cluster_arn, secret_arn, "CREATE USER 'appuser'@'%' IDENTIFIED BY 'pass'")
assert status == 200
status, body = _exec(
cluster_arn, secret_arn,
"SELECT User FROM mysql.user WHERE User='appuser'",
)
assert status == 200
names = [r[0]["stringValue"] for r in body.get("records", [])]
assert "appuser" in names
def test_rds_data_stub_grant_and_show_grants(rds, sm):
"""GRANT privileges, then SHOW GRANTS FOR."""
cluster_arn, secret_arn = _setup_stub_cluster(rds, sm)
_exec(cluster_arn, secret_arn, "CREATE USER 'grantee'@'%' IDENTIFIED BY 'pass'")
status, _ = _exec(
cluster_arn, secret_arn,
"GRANT ALL PRIVILEGES ON mydb.* TO 'grantee'@'%'",
)
assert status == 200
status, body = _exec(cluster_arn, secret_arn, "SHOW GRANTS FOR 'grantee'")
assert status == 200
grants = [r[0]["stringValue"] for r in body.get("records", [])]
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)
@pytest.mark.parametrize(
"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)
@pytest.mark.parametrize(
"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)
@pytest.mark.parametrize(
("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)