Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

feat(rds-data): add RDS Data API support (#1262)

devansh-jain-18e063ac18ee46
diff --git a/README.md b/README.md
index 8f0abec3b3..e4e18fb303 100644
--- a/README.md
+++ b/README.md
@@ -177,7 +177,7 @@ LocalStack's community edition [sunset in March 2026](https://blog.localstack.cl
| CodeBuild | Real Docker execution | No |
| Native binary | ~40 MB | No |
-**53 AWS services. Broad coverage. Free forever.**
+**54 AWS services. Broad coverage. Free forever.**
## Architecture Overview
@@ -222,8 +222,8 @@ Floci supports local emulation for application services, data services, eventing
| Events and workflows | EventBridge, EventBridge Pipes, EventBridge Scheduler, Step Functions, CloudWatch Logs, CloudWatch Metrics |
| API and identity | API Gateway REST, API Gateway v2, AppSync, Cognito, ACM, Route53, Cloud Map |
| Containers and compute | ECS, EC2, EKS, CodeBuild, CodeDeploy, Auto Scaling, ELB v2 |
-| Graph database | Neptune |
| Data and analytics | Athena, Glue, Firehose, OpenSearch, Textract, Transcribe |
+| Databases | RDS, RDS Data API, Neptune |
| Messaging and transfer | SES, SES v2, Kinesis, Transfer Family |
| Cost and billing | Pricing, Cost Explorer, Cost and Usage Reports, BCM Data Exports |
| Backup and config | AWS Backup, AWS Config, AppConfig, AppConfigData, CloudFormation |
@@ -261,6 +261,7 @@ For operation-level compatibility, see the [Services Overview](https://floci.io/
| CloudWatch Metrics | In-process | Custom metrics, statistics, alarms |
| ElastiCache | Real Docker | Redis / Valkey protocol, IAM auth, SigV4 validation |
| RDS | Real Docker | PostgreSQL, MySQL, MariaDB, IAM auth, JDBC-compatible engines |
+| RDS Data API | REST JSON over real RDS containers | Raw SQL execution and transactions for local MySQL / MariaDB RDS resources |
| Neptune | Real Docker | Graph DB via TinkerPop Gremlin Server; RDS-shaped control plane; Gremlin WebSocket on port 8182 with SigV4 proxy |
| MSK | Real Docker | Kafka-compatible broker via Redpanda |
| Athena | In-process with DuckDB sidecar | Real SQL execution over S3 and Glue-backed views |
@@ -658,14 +659,14 @@ The [`compatibility-tests`](./compatibility-tests/) directory validates Floci ac
| `sdk-test-java` | Java 17 | AWS SDK for Java v2 | 899 |
| `sdk-test-node` | Node.js | AWS SDK for JavaScript v3 | 366 |
| `sdk-test-python` | Python 3 | boto3 | 272 |
-| `sdk-test-go` | Go | AWS SDK for Go v2 | 144 |
+| `sdk-test-go` | Go | AWS SDK for Go v2 + RDS Data API SDK v1 | 145 |
| `sdk-test-awscli` | Bash | AWS CLI v2 | 152 |
| `sdk-test-rust` | Rust | AWS SDK for Rust | 90 |
| `compat-terraform` | Terraform | v1.10+ | 14 |
| `compat-opentofu` | OpenTofu | v1.9+ | 14 |
| `compat-cdk` | AWS CDK | v2+ | 17 |
-**1,968 automated compatibility tests across 6 SDKs and 3 IaC tools.**
+**1,969 automated compatibility tests across 6 SDKs and 3 IaC tools.**
## Migrating from LocalStack
diff --git a/compatibility-tests/sdk-test-go/README.md b/compatibility-tests/sdk-test-go/README.md
index 79a99ab52e..963734f864 100644
--- a/compatibility-tests/sdk-test-go/README.md
+++ b/compatibility-tests/sdk-test-go/README.md
@@ -20,6 +20,7 @@ Compatibility tests for [Floci](https://github.com/hectorvent/floci) using the *
| `kms` | Keys, aliases, encrypt/decrypt, data keys, sign/verify |
| `kinesis` | Streams, shards, PutRecord/GetRecords |
| `cloudwatch` | PutMetricData, ListMetrics, GetMetricStatistics, alarms |
+| `rds-data` | RDS Data API execute statements, transactions, result fields with SDK v1 and v2 |
## Requirements
diff --git a/compatibility-tests/sdk-test-go/tests/rds_data_test.go b/compatibility-tests/sdk-test-go/tests/rds_data_test.go
new file mode 100644
index 0000000000..ef1487136e
--- /dev/null
+++ b/compatibility-tests/sdk-test-go/tests/rds_data_test.go
@@ -0,0 +1,636 @@
+package tests
+
+import (
+ "context"
+ "fmt"
+ "strconv"
+ "testing"
+ "time"
+
+ "floci-sdk-test-go/internal/testutil"
+
+ awsV2 "github.com/aws/aws-sdk-go-v2/aws"
+ "github.com/aws/aws-sdk-go-v2/service/rds"
+ "github.com/aws/aws-sdk-go-v2/service/rds/types"
+ "github.com/aws/aws-sdk-go-v2/service/rdsdata"
+ rdsdatatypes "github.com/aws/aws-sdk-go-v2/service/rdsdata/types"
+ "github.com/aws/aws-sdk-go-v2/service/secretsmanager"
+ awsV1 "github.com/aws/aws-sdk-go/aws"
+ "github.com/aws/aws-sdk-go/aws/awserr"
+ "github.com/aws/aws-sdk-go/aws/credentials"
+ "github.com/aws/aws-sdk-go/aws/session"
+ "github.com/aws/aws-sdk-go/service/rdsdataservice"
+ "github.com/aws/smithy-go"
+ "github.com/stretchr/testify/assert"
+ "github.com/stretchr/testify/require"
+)
+
+func TestRdsDataApiGoSdkV1(t *testing.T) {
+ ctx := context.Background()
+ rdsSvc := testutil.RDSClient()
+ secretsSvc := testutil.SecretsManagerClient()
+ dataSvc := rdsDataV1Client(t)
+
+ clusterID := "go-rds-data-mysql-" + strconv.FormatInt(time.Now().UnixNano(), 36)
+ database := "app"
+ username := "admin"
+ password := "secret123"
+ secretName := "go-rds-data/" + clusterID
+ var secretArn string
+ var resourceArn string
+
+ t.Cleanup(func() {
+ if secretArn != "" {
+ _, _ = secretsSvc.DeleteSecret(ctx, &secretsmanager.DeleteSecretInput{
+ SecretId: awsV2.String(secretArn),
+ ForceDeleteWithoutRecovery: awsV2.Bool(true),
+ })
+ }
+ if resourceArn != "" {
+ _, _ = rdsSvc.DeleteDBCluster(ctx, &rds.DeleteDBClusterInput{
+ DBClusterIdentifier: awsV2.String(clusterID),
+ SkipFinalSnapshot: awsV2.Bool(true),
+ })
+ }
+ })
+
+ create, err := rdsSvc.CreateDBCluster(ctx, &rds.CreateDBClusterInput{
+ DBClusterIdentifier: awsV2.String(clusterID),
+ Engine: awsV2.String("aurora-mysql"),
+ EngineVersion: awsV2.String("8.0.mysql_aurora.3.08.0"),
+ MasterUsername: awsV2.String(username),
+ MasterUserPassword: awsV2.String(password),
+ DatabaseName: awsV2.String(database),
+ EngineMode: awsV2.String("provisioned"),
+ Tags: []types.Tag{{Key: awsV2.String("test"), Value: awsV2.String("rds-data")}},
+ })
+ if err != nil {
+ t.Skipf("RDS MySQL cluster unavailable in this environment: %v", err)
+ }
+ require.NotNil(t, create.DBCluster)
+ resourceArn = awsV2.ToString(create.DBCluster.DBClusterArn)
+ require.NotEmpty(t, resourceArn)
+
+ secret, err := secretsSvc.CreateSecret(ctx, &secretsmanager.CreateSecretInput{
+ Name: awsV2.String(secretName),
+ SecretString: awsV2.String(fmt.Sprintf(`{"username":%q,"password":%q}`, username, password)),
+ })
+ require.NoError(t, err)
+ secretArn = awsV2.ToString(secret.ARN)
+ require.NotEmpty(t, secretArn)
+
+ executeEventually(t, dataSvc, &rdsdataservice.ExecuteStatementInput{
+ ResourceArn: awsV1.String(resourceArn),
+ SecretArn: awsV1.String(secretArn),
+ Database: awsV1.String(database),
+ Sql: awsV1.String(`create table if not exists data_api_items (
+ id varchar(64) primary key,
+ title varchar(255),
+ score bigint,
+ payload blob null,
+ active boolean null,
+ ratio double null,
+ json_text text null,
+ rfc3339_text varchar(64) null,
+ observed_at datetime(6) null,
+ stamped_at timestamp(6) null,
+ due_date date null,
+ due_time time null
+ )`),
+ })
+
+ tx, err := dataSvc.BeginTransaction(&rdsdataservice.BeginTransactionInput{
+ ResourceArn: awsV1.String(resourceArn),
+ SecretArn: awsV1.String(secretArn),
+ Database: awsV1.String(database),
+ })
+ require.NoError(t, err)
+ require.NotEmpty(t, awsV1.StringValue(tx.TransactionId))
+
+ insertOut, err := dataSvc.ExecuteStatement(&rdsdataservice.ExecuteStatementInput{
+ ResourceArn: awsV1.String(resourceArn),
+ SecretArn: awsV1.String(secretArn),
+ Database: awsV1.String(database),
+ TransactionId: tx.TransactionId,
+ Sql: awsV1.String(`insert into data_api_items
+ (id, title, score, payload, active, ratio, json_text, rfc3339_text, observed_at, stamped_at, due_date, due_time)
+ values (
+ 'commit-1',
+ 'first item',
+ 7,
+ UNHEX('010203'),
+ true,
+ 1.5,
+ '{"ok":true}',
+ '2021-03-04T05:06:07Z',
+ '2021-03-04 05:06:07.891000',
+ '2021-03-04 05:06:07.891000',
+ '2021-03-04',
+ '05:06:07'
+ )`),
+ })
+ require.NoError(t, err)
+ assert.Equal(t, int64(1), awsV1.Int64Value(insertOut.NumberOfRecordsUpdated))
+
+ inTxRead, err := dataSvc.ExecuteStatement(&rdsdataservice.ExecuteStatementInput{
+ ResourceArn: awsV1.String(resourceArn),
+ SecretArn: awsV1.String(secretArn),
+ Database: awsV1.String(database),
+ TransactionId: tx.TransactionId,
+ Sql: awsV1.String("select count(*) as count from data_api_items where id = 'commit-1'"),
+ })
+ require.NoError(t, err)
+ assert.Equal(t, int64(1), awsV1.Int64Value(inTxRead.Records[0][0].LongValue))
+
+ _, err = dataSvc.CommitTransaction(&rdsdataservice.CommitTransactionInput{
+ ResourceArn: awsV1.String(resourceArn),
+ SecretArn: awsV1.String(secretArn),
+ TransactionId: tx.TransactionId,
+ })
+ require.NoError(t, err)
+
+ _, err = dataSvc.ExecuteStatement(&rdsdataservice.ExecuteStatementInput{
+ ResourceArn: awsV1.String(resourceArn),
+ SecretArn: awsV1.String(secretArn),
+ Database: awsV1.String(database),
+ TransactionId: tx.TransactionId,
+ Sql: awsV1.String("select 1"),
+ })
+ assertAwsErrorCode(t, err, "TransactionNotFoundException")
+
+ selectOut, err := dataSvc.ExecuteStatement(&rdsdataservice.ExecuteStatementInput{
+ ResourceArn: awsV1.String(resourceArn),
+ SecretArn: awsV1.String(secretArn),
+ Database: awsV1.String(database),
+ Sql: awsV1.String("select title, score, payload, null as nothing, observed_at, stamped_at, due_date, due_time, active, ratio, json_text, rfc3339_text from data_api_items where id = 'commit-1'"),
+ IncludeResultMetadata: awsV1.Bool(true),
+ })
+ require.NoError(t, err)
+ require.Len(t, selectOut.ColumnMetadata, 12)
+ assert.Equal(t, "title", awsV1.StringValue(selectOut.ColumnMetadata[0].Name))
+ require.Len(t, selectOut.Records, 1)
+ require.Len(t, selectOut.Records[0], 12)
+ assert.Equal(t, "first item", awsV1.StringValue(selectOut.Records[0][0].StringValue))
+ assert.Equal(t, int64(7), awsV1.Int64Value(selectOut.Records[0][1].LongValue))
+ assert.Equal(t, []byte{1, 2, 3}, selectOut.Records[0][2].BlobValue)
+ assert.True(t, awsV1.BoolValue(selectOut.Records[0][3].IsNull))
+ assert.Equal(t, "2021-03-04 05:06:07.891", awsV1.StringValue(selectOut.Records[0][4].StringValue))
+ assert.Equal(t, "2021-03-04 05:06:07.891", awsV1.StringValue(selectOut.Records[0][5].StringValue))
+ assert.Equal(t, "2021-03-04", awsV1.StringValue(selectOut.Records[0][6].StringValue))
+ assert.Equal(t, "05:06:07", awsV1.StringValue(selectOut.Records[0][7].StringValue))
+ assert.True(t, awsV1.BoolValue(selectOut.Records[0][8].BooleanValue))
+ assert.Equal(t, 1.5, awsV1.Float64Value(selectOut.Records[0][9].DoubleValue))
+ assert.Equal(t, `{"ok":true}`, awsV1.StringValue(selectOut.Records[0][10].StringValue))
+ assert.Equal(t, "2021-03-04T05:06:07Z", awsV1.StringValue(selectOut.Records[0][11].StringValue))
+
+ updateOut, err := dataSvc.ExecuteStatement(&rdsdataservice.ExecuteStatementInput{
+ ResourceArn: awsV1.String(resourceArn),
+ SecretArn: awsV1.String(secretArn),
+ Database: awsV1.String(database),
+ Sql: awsV1.String("update data_api_items set score = 8 where id = 'commit-1' and score = 7"),
+ })
+ require.NoError(t, err)
+ assert.Equal(t, int64(1), awsV1.Int64Value(updateOut.NumberOfRecordsUpdated))
+
+ staleUpdateOut, err := dataSvc.ExecuteStatement(&rdsdataservice.ExecuteStatementInput{
+ ResourceArn: awsV1.String(resourceArn),
+ SecretArn: awsV1.String(secretArn),
+ Database: awsV1.String(database),
+ Sql: awsV1.String("update data_api_items set score = 9 where id = 'commit-1' and score = 7"),
+ })
+ require.NoError(t, err)
+ assert.Equal(t, int64(0), awsV1.Int64Value(staleUpdateOut.NumberOfRecordsUpdated))
+
+ upsertOut, err := dataSvc.ExecuteStatement(&rdsdataservice.ExecuteStatementInput{
+ ResourceArn: awsV1.String(resourceArn),
+ SecretArn: awsV1.String(secretArn),
+ Database: awsV1.String(database),
+ Sql: awsV1.String("insert into data_api_items (id, title, score, payload) values ('commit-1', 'upserted', 11, UNHEX('0A0B')) on duplicate key update title = values(title), score = values(score), payload = values(payload)"),
+ })
+ require.NoError(t, err)
+ assert.GreaterOrEqual(t, awsV1.Int64Value(upsertOut.NumberOfRecordsUpdated), int64(1))
+
+ _, err = dataSvc.ExecuteStatement(&rdsdataservice.ExecuteStatementInput{
+ ResourceArn: awsV1.String(resourceArn),
+ SecretArn: awsV1.String(secretArn),
+ Database: awsV1.String(database),
+ Sql: awsV1.String("insert into data_api_items (id, title, score) values ('page-1', 'alpha', 1), ('page-2', 'beta', 2), ('page-3', 'alphabet', 3)"),
+ })
+ require.NoError(t, err)
+
+ predicateOut, err := dataSvc.ExecuteStatement(&rdsdataservice.ExecuteStatementInput{
+ ResourceArn: awsV1.String(resourceArn),
+ SecretArn: awsV1.String(secretArn),
+ Database: awsV1.String(database),
+ Sql: awsV1.String("select id from data_api_items where (title like 'alpha%' or id in (select id from data_api_items where score = 11)) order by score desc limit 2 offset 0"),
+ IncludeResultMetadata: awsV1.Bool(true),
+ })
+ require.NoError(t, err)
+ require.Len(t, predicateOut.Records, 2)
+ assert.Equal(t, "commit-1", awsV1.StringValue(predicateOut.Records[0][0].StringValue))
+ assert.Equal(t, "page-3", awsV1.StringValue(predicateOut.Records[1][0].StringValue))
+
+ lessThanAndNotInOut, err := dataSvc.ExecuteStatement(&rdsdataservice.ExecuteStatementInput{
+ ResourceArn: awsV1.String(resourceArn),
+ SecretArn: awsV1.String(secretArn),
+ Database: awsV1.String(database),
+ Sql: awsV1.String("select count(*) as count from data_api_items where score < 3 and id not in ('commit-1')"),
+ IncludeResultMetadata: awsV1.Bool(true),
+ })
+ require.NoError(t, err)
+ assert.Equal(t, "count", awsV1.StringValue(lessThanAndNotInOut.ColumnMetadata[0].Name))
+ assert.Equal(t, int64(2), awsV1.Int64Value(lessThanAndNotInOut.Records[0][0].LongValue))
+
+ emptyOut, err := dataSvc.ExecuteStatement(&rdsdataservice.ExecuteStatementInput{
+ ResourceArn: awsV1.String(resourceArn),
+ SecretArn: awsV1.String(secretArn),
+ Database: awsV1.String(database),
+ Sql: awsV1.String("select id from data_api_items where id = 'missing'"),
+ IncludeResultMetadata: awsV1.Bool(true),
+ })
+ require.NoError(t, err)
+ assert.Equal(t, "id", awsV1.StringValue(emptyOut.ColumnMetadata[0].Name))
+ assert.Empty(t, emptyOut.Records)
+ assert.Equal(t, int64(0), awsV1.Int64Value(emptyOut.NumberOfRecordsUpdated))
+
+ replaceTx, err := dataSvc.BeginTransaction(&rdsdataservice.BeginTransactionInput{
+ ResourceArn: awsV1.String(resourceArn),
+ SecretArn: awsV1.String(secretArn),
+ Database: awsV1.String(database),
+ })
+ require.NoError(t, err)
+ deleteOut, err := dataSvc.ExecuteStatement(&rdsdataservice.ExecuteStatementInput{
+ ResourceArn: awsV1.String(resourceArn),
+ SecretArn: awsV1.String(secretArn),
+ Database: awsV1.String(database),
+ TransactionId: replaceTx.TransactionId,
+ Sql: awsV1.String("delete from data_api_items where id = 'page-2'"),
+ })
+ require.NoError(t, err)
+ assert.Equal(t, int64(1), awsV1.Int64Value(deleteOut.NumberOfRecordsUpdated))
+ replaceInsertOut, err := dataSvc.ExecuteStatement(&rdsdataservice.ExecuteStatementInput{
+ ResourceArn: awsV1.String(resourceArn),
+ SecretArn: awsV1.String(secretArn),
+ Database: awsV1.String(database),
+ TransactionId: replaceTx.TransactionId,
+ Sql: awsV1.String("insert into data_api_items (id, title, score) values ('page-4', 'replacement', 4)"),
+ })
+ require.NoError(t, err)
+ assert.Equal(t, int64(1), awsV1.Int64Value(replaceInsertOut.NumberOfRecordsUpdated))
+ _, err = dataSvc.CommitTransaction(&rdsdataservice.CommitTransactionInput{
+ ResourceArn: awsV1.String(resourceArn),
+ SecretArn: awsV1.String(secretArn),
+ TransactionId: replaceTx.TransactionId,
+ })
+ require.NoError(t, err)
+ replaceCountOut, err := dataSvc.ExecuteStatement(&rdsdataservice.ExecuteStatementInput{
+ ResourceArn: awsV1.String(resourceArn),
+ SecretArn: awsV1.String(secretArn),
+ Database: awsV1.String(database),
+ Sql: awsV1.String("select count(*) as count from data_api_items where id in ('page-2', 'page-4')"),
+ })
+ require.NoError(t, err)
+ assert.Equal(t, int64(1), awsV1.Int64Value(replaceCountOut.Records[0][0].LongValue))
+
+ rollbackTx, err := dataSvc.BeginTransaction(&rdsdataservice.BeginTransactionInput{
+ ResourceArn: awsV1.String(resourceArn),
+ SecretArn: awsV1.String(secretArn),
+ Database: awsV1.String(database),
+ })
+ require.NoError(t, err)
+ rollbackInsertOut, err := dataSvc.ExecuteStatement(&rdsdataservice.ExecuteStatementInput{
+ ResourceArn: awsV1.String(resourceArn),
+ SecretArn: awsV1.String(secretArn),
+ Database: awsV1.String(database),
+ TransactionId: rollbackTx.TransactionId,
+ Sql: awsV1.String("insert into data_api_items (id, title, score) values ('rollback-1', 'rolled back', 9)"),
+ })
+ require.NoError(t, err)
+ assert.Equal(t, int64(1), awsV1.Int64Value(rollbackInsertOut.NumberOfRecordsUpdated))
+ _, err = dataSvc.RollbackTransaction(&rdsdataservice.RollbackTransactionInput{
+ ResourceArn: awsV1.String(resourceArn),
+ SecretArn: awsV1.String(secretArn),
+ TransactionId: rollbackTx.TransactionId,
+ })
+ require.NoError(t, err)
+
+ _, err = dataSvc.ExecuteStatement(&rdsdataservice.ExecuteStatementInput{
+ ResourceArn: awsV1.String(resourceArn),
+ SecretArn: awsV1.String(secretArn),
+ Database: awsV1.String(database),
+ TransactionId: rollbackTx.TransactionId,
+ Sql: awsV1.String("select 1"),
+ })
+ assertAwsErrorCode(t, err, "TransactionNotFoundException")
+
+ countOut, err := dataSvc.ExecuteStatement(&rdsdataservice.ExecuteStatementInput{
+ ResourceArn: awsV1.String(resourceArn),
+ SecretArn: awsV1.String(secretArn),
+ Database: awsV1.String(database),
+ Sql: awsV1.String("select count(*) as count from data_api_items where id in ('commit-1', 'rollback-1')"),
+ IncludeResultMetadata: awsV1.Bool(true),
+ })
+ require.NoError(t, err)
+ assert.Equal(t, "count", awsV1.StringValue(countOut.ColumnMetadata[0].Name))
+ assert.Equal(t, int64(1), awsV1.Int64Value(countOut.Records[0][0].LongValue))
+
+ _, err = dataSvc.BatchExecuteStatement(&rdsdataservice.BatchExecuteStatementInput{
+ ResourceArn: awsV1.String(resourceArn),
+ SecretArn: awsV1.String(secretArn),
+ Database: awsV1.String(database),
+ Sql: awsV1.String("insert into data_api_items (id, title, score) values ('batch-1', 'batch', 1)"),
+ })
+ require.Error(t, err)
+ assertAwsErrorCode(t, err, "BadRequestException")
+
+ _, err = dataSvc.ExecuteSql(&rdsdataservice.ExecuteSqlInput{
+ DbClusterOrInstanceArn: awsV1.String(resourceArn),
+ AwsSecretStoreArn: awsV1.String(secretArn),
+ Database: awsV1.String(database),
+ SqlStatements: awsV1.String("select 1"),
+ })
+ assertAwsErrorCode(t, err, "BadRequestException")
+
+ _, err = dataSvc.ExecuteStatement(&rdsdataservice.ExecuteStatementInput{
+ ResourceArn: awsV1.String(resourceArn),
+ SecretArn: awsV1.String(secretArn),
+ Database: awsV1.String(database),
+ Sql: awsV1.String("select :id"),
+ Parameters: []*rdsdataservice.SqlParameter{{
+ Name: awsV1.String("id"),
+ Value: &rdsdataservice.Field{LongValue: awsV1.Int64(1)},
+ }},
+ })
+ assertAwsErrorCode(t, err, "BadRequestException")
+
+ _, err = dataSvc.ExecuteStatement(&rdsdataservice.ExecuteStatementInput{
+ ResourceArn: awsV1.String(resourceArn),
+ SecretArn: awsV1.String(secretArn),
+ Database: awsV1.String(database),
+ Sql: awsV1.String("insert into data_api_items (id, title, score) values ('commit-1', 'duplicate', 1)"),
+ })
+ assertAwsErrorCode(t, err, "DatabaseErrorException")
+
+ _, err = dataSvc.ExecuteStatement(&rdsdataservice.ExecuteStatementInput{
+ ResourceArn: awsV1.String(resourceArn),
+ SecretArn: awsV1.String(secretArn),
+ Database: awsV1.String(database),
+ Sql: awsV1.String("select from data_api_items"),
+ })
+ assertAwsErrorCode(t, err, "DatabaseErrorException")
+}
+
+func TestRdsDataApiGoSdkV2(t *testing.T) {
+ ctx := context.Background()
+ rdsSvc := testutil.RDSClient()
+ secretsSvc := testutil.SecretsManagerClient()
+ dataSvc := rdsDataV2Client()
+
+ clusterID := "go-rds-data-v2-mysql-" + strconv.FormatInt(time.Now().UnixNano(), 36)
+ database := "app"
+ username := "admin"
+ password := "secret123"
+ secretName := "go-rds-data-v2/" + clusterID
+ var secretArn string
+ var resourceArn string
+
+ t.Cleanup(func() {
+ if secretArn != "" {
+ _, _ = secretsSvc.DeleteSecret(ctx, &secretsmanager.DeleteSecretInput{
+ SecretId: awsV2.String(secretArn),
+ ForceDeleteWithoutRecovery: awsV2.Bool(true),
+ })
+ }
+ if resourceArn != "" {
+ _, _ = rdsSvc.DeleteDBCluster(ctx, &rds.DeleteDBClusterInput{
+ DBClusterIdentifier: awsV2.String(clusterID),
+ SkipFinalSnapshot: awsV2.Bool(true),
+ })
+ }
+ })
+
+ create, err := rdsSvc.CreateDBCluster(ctx, &rds.CreateDBClusterInput{
+ DBClusterIdentifier: awsV2.String(clusterID),
+ Engine: awsV2.String("aurora-mysql"),
+ EngineVersion: awsV2.String("8.0.mysql_aurora.3.08.0"),
+ MasterUsername: awsV2.String(username),
+ MasterUserPassword: awsV2.String(password),
+ DatabaseName: awsV2.String(database),
+ EngineMode: awsV2.String("provisioned"),
+ Tags: []types.Tag{{Key: awsV2.String("test"), Value: awsV2.String("rds-data-v2")}},
+ })
+ if err != nil {
+ t.Skipf("RDS MySQL cluster unavailable in this environment: %v", err)
+ }
+ require.NotNil(t, create.DBCluster)
+ resourceArn = awsV2.ToString(create.DBCluster.DBClusterArn)
+ require.NotEmpty(t, resourceArn)
+
+ secret, err := secretsSvc.CreateSecret(ctx, &secretsmanager.CreateSecretInput{
+ Name: awsV2.String(secretName),
+ SecretString: awsV2.String(fmt.Sprintf(`{"username":%q,"password":%q}`, username, password)),
+ })
+ require.NoError(t, err)
+ secretArn = awsV2.ToString(secret.ARN)
+ require.NotEmpty(t, secretArn)
+
+ executeEventuallyV2(t, ctx, dataSvc, &rdsdata.ExecuteStatementInput{
+ ResourceArn: awsV2.String(resourceArn),
+ SecretArn: awsV2.String(secretArn),
+ Database: awsV2.String(database),
+ Sql: awsV2.String(`create table if not exists data_api_v2_items (
+ id varchar(64) primary key,
+ name varchar(255),
+ qty bigint,
+ payload blob null,
+ active boolean null,
+ ratio double null
+ )`),
+ })
+
+ tx, err := dataSvc.BeginTransaction(ctx, &rdsdata.BeginTransactionInput{
+ ResourceArn: awsV2.String(resourceArn),
+ SecretArn: awsV2.String(secretArn),
+ Database: awsV2.String(database),
+ })
+ require.NoError(t, err)
+ require.NotEmpty(t, awsV2.ToString(tx.TransactionId))
+
+ insertOut, err := dataSvc.ExecuteStatement(ctx, &rdsdata.ExecuteStatementInput{
+ ResourceArn: awsV2.String(resourceArn),
+ SecretArn: awsV2.String(secretArn),
+ Database: awsV2.String(database),
+ TransactionId: tx.TransactionId,
+ Sql: awsV2.String("insert into data_api_v2_items (id, name, qty, payload, active, ratio) values ('v2-1', 'second client', 12, UNHEX('0A0B'), true, 2.5)"),
+ })
+ require.NoError(t, err)
+ assert.Equal(t, int64(1), insertOut.NumberOfRecordsUpdated)
+
+ readInTx, err := dataSvc.ExecuteStatement(ctx, &rdsdata.ExecuteStatementInput{
+ ResourceArn: awsV2.String(resourceArn),
+ SecretArn: awsV2.String(secretArn),
+ Database: awsV2.String(database),
+ TransactionId: tx.TransactionId,
+ Sql: awsV2.String("select count(*) as count from data_api_v2_items where id = 'v2-1'"),
+ })
+ require.NoError(t, err)
+ assert.Equal(t, int64(1), fieldLongValue(t, readInTx.Records[0][0]))
+
+ commitOut, err := dataSvc.CommitTransaction(ctx, &rdsdata.CommitTransactionInput{
+ ResourceArn: awsV2.String(resourceArn),
+ SecretArn: awsV2.String(secretArn),
+ TransactionId: tx.TransactionId,
+ })
+ require.NoError(t, err)
+ assert.NotEmpty(t, awsV2.ToString(commitOut.TransactionStatus))
+
+ selectOut, err := dataSvc.ExecuteStatement(ctx, &rdsdata.ExecuteStatementInput{
+ ResourceArn: awsV2.String(resourceArn),
+ SecretArn: awsV2.String(secretArn),
+ Database: awsV2.String(database),
+ Sql: awsV2.String("select name, qty, payload, active, ratio from data_api_v2_items where id = 'v2-1'"),
+ IncludeResultMetadata: true,
+ })
+ require.NoError(t, err)
+ require.Len(t, selectOut.ColumnMetadata, 5)
+ assert.Equal(t, "name", awsV2.ToString(selectOut.ColumnMetadata[0].Name))
+ require.Len(t, selectOut.Records, 1)
+ assert.Equal(t, "second client", fieldStringValue(t, selectOut.Records[0][0]))
+ assert.Equal(t, int64(12), fieldLongValue(t, selectOut.Records[0][1]))
+ assert.Equal(t, []byte{10, 11}, fieldBlobValue(t, selectOut.Records[0][2]))
+ assert.True(t, fieldBooleanValue(t, selectOut.Records[0][3]))
+ assert.Equal(t, 2.5, fieldDoubleValue(t, selectOut.Records[0][4]))
+
+ rollbackTx, err := dataSvc.BeginTransaction(ctx, &rdsdata.BeginTransactionInput{
+ ResourceArn: awsV2.String(resourceArn),
+ SecretArn: awsV2.String(secretArn),
+ Database: awsV2.String(database),
+ })
+ require.NoError(t, err)
+ _, err = dataSvc.ExecuteStatement(ctx, &rdsdata.ExecuteStatementInput{
+ ResourceArn: awsV2.String(resourceArn),
+ SecretArn: awsV2.String(secretArn),
+ Database: awsV2.String(database),
+ TransactionId: rollbackTx.TransactionId,
+ Sql: awsV2.String("insert into data_api_v2_items (id, name, qty) values ('v2-rollback', 'gone', 1)"),
+ })
+ require.NoError(t, err)
+ _, err = dataSvc.RollbackTransaction(ctx, &rdsdata.RollbackTransactionInput{
+ ResourceArn: awsV2.String(resourceArn),
+ SecretArn: awsV2.String(secretArn),
+ TransactionId: rollbackTx.TransactionId,
+ })
+ require.NoError(t, err)
+
+ _, err = dataSvc.ExecuteStatement(ctx, &rdsdata.ExecuteStatementInput{
+ ResourceArn: awsV2.String(resourceArn),
+ SecretArn: awsV2.String(secretArn),
+ Database: awsV2.String(database),
+ TransactionId: rollbackTx.TransactionId,
+ Sql: awsV2.String("select 1"),
+ })
+ assertSmithyErrorCode(t, err, "TransactionNotFoundException")
+
+ _, err = dataSvc.ExecuteStatement(ctx, &rdsdata.ExecuteStatementInput{
+ ResourceArn: awsV2.String(resourceArn),
+ SecretArn: awsV2.String(secretArn),
+ Database: awsV2.String(database),
+ Sql: awsV2.String("select :id"),
+ Parameters: []rdsdatatypes.SqlParameter{{
+ Name: awsV2.String("id"),
+ Value: &rdsdatatypes.FieldMemberLongValue{Value: 1},
+ }},
+ })
+ assertSmithyErrorCode(t, err, "BadRequestException")
+
+ _, err = dataSvc.ExecuteSql(ctx, &rdsdata.ExecuteSqlInput{
+ DbClusterOrInstanceArn: awsV2.String(resourceArn),
+ AwsSecretStoreArn: awsV2.String(secretArn),
+ Database: awsV2.String(database),
+ SqlStatements: awsV2.String("select 1"),
+ })
+ assertSmithyErrorCode(t, err, "BadRequestException")
+}
+
+func rdsDataV1Client(t *testing.T) *rdsdataservice.RDSDataService {
+ t.Helper()
+ sess, err := session.NewSession(&awsV1.Config{
+ Region: awsV1.String("us-east-1"),
+ Endpoint: awsV1.String(testutil.Endpoint()),
+ DisableSSL: awsV1.Bool(true),
+ Credentials: credentials.NewStaticCredentials("test", "test", ""),
+ })
+ require.NoError(t, err)
+ return rdsdataservice.New(sess)
+}
+
+func rdsDataV2Client() *rdsdata.Client {
+ return rdsdata.NewFromConfig(testutil.Config())
+}
+
+func executeEventually(t *testing.T, svc *rdsdataservice.RDSDataService, input *rdsdataservice.ExecuteStatementInput) {
+ t.Helper()
+ require.Eventually(t, func() bool {
+ _, err := svc.ExecuteStatement(input)
+ return err == nil
+ }, 90*time.Second, time.Second)
+}
+
+func executeEventuallyV2(t *testing.T, ctx context.Context, svc *rdsdata.Client, input *rdsdata.ExecuteStatementInput) {
+ t.Helper()
+ require.Eventually(t, func() bool {
+ _, err := svc.ExecuteStatement(ctx, input)
+ return err == nil
+ }, 90*time.Second, time.Second)
+}
+
+func assertAwsErrorCode(t *testing.T, err error, code string) {
+ t.Helper()
+ require.Error(t, err)
+ var awsErr awserr.Error
+ require.ErrorAs(t, err, &awsErr)
+ assert.Equal(t, code, awsErr.Code())
+}
+
+func assertSmithyErrorCode(t *testing.T, err error, code string) {
+ t.Helper()
+ require.Error(t, err)
+ var apiErr smithy.APIError
+ require.ErrorAs(t, err, &apiErr)
+ assert.Equal(t, code, apiErr.ErrorCode())
+}
+
+func fieldStringValue(t *testing.T, field rdsdatatypes.Field) string {
+ t.Helper()
+ value, ok := field.(*rdsdatatypes.FieldMemberStringValue)
+ require.Truef(t, ok, "expected stringValue field, got %T", field)
+ return value.Value
+}
+
+func fieldLongValue(t *testing.T, field rdsdatatypes.Field) int64 {
+ t.Helper()
+ value, ok := field.(*rdsdatatypes.FieldMemberLongValue)
+ require.Truef(t, ok, "expected longValue field, got %T", field)
+ return value.Value
+}
+
+func fieldBlobValue(t *testing.T, field rdsdatatypes.Field) []byte {
+ t.Helper()
+ value, ok := field.(*rdsdatatypes.FieldMemberBlobValue)
+ require.Truef(t, ok, "expected blobValue field, got %T", field)
+ return value.Value
+}
+
+func fieldBooleanValue(t *testing.T, field rdsdatatypes.Field) bool {
+ t.Helper()
+ value, ok := field.(*rdsdatatypes.FieldMemberBooleanValue)
+ require.Truef(t, ok, "expected booleanValue field, got %T", field)
+ return value.Value
+}
+
+func fieldDoubleValue(t *testing.T, field rdsdatatypes.Field) float64 {
+ t.Helper()
+ value, ok := field.(*rdsdatatypes.FieldMemberDoubleValue)
+ require.Truef(t, ok, "expected doubleValue field, got %T", field)
+ return value.Value
+}
diff --git a/docs/configuration/advanced/application-yml.md b/docs/configuration/advanced/application-yml.md
index ebb2b7c8aa..1f95225c86 100644
--- a/docs/configuration/advanced/application-yml.md
+++ b/docs/configuration/advanced/application-yml.md
@@ -156,6 +156,10 @@ floci:
default-mysql-image: "mysql:8.0"
default-mariadb-image: "mariadb:11"
+ rds-data:
+ enabled: true
+ transaction-ttl-seconds: 180
+
eventbridge:
enabled: true
@@ -259,6 +263,8 @@ All keys in this table are declared on `EmulatorConfig` and accept environment v
| `FLOCI_SERVICES_SQS_CLEAR_FIFO_DEDUPLICATION_CACHE_ON_PURGE` | `false` | When `true`, `PurgeQueue` clears the FIFO 5-minute deduplication cache for the target queue and matching SNS FIFO topic dedup entries |
| `FLOCI_SERVICES_S3_DEFAULT_PRESIGN_EXPIRY_SECONDS` | `3600` | Pre-signed URL expiry |
| `FLOCI_SERVICES_DOCKER_NETWORK` | *(unset)* | Shared Docker network for Lambda, RDS, ElastiCache containers |
+| `FLOCI_SERVICES_RDS_DATA_ENABLED` | `true` | Enable the RDS Data API service |
+| `FLOCI_SERVICES_RDS_DATA_TRANSACTION_TTL_SECONDS` | `180` | Idle timeout, in seconds, before leaked RDS Data API transactions expire |
| `FLOCI_SERVICES_ECS_MOCK` | `false` | Skip Docker; tasks go straight to RUNNING (useful for CI) |
| `FLOCI_SERVICES_ECS_DOCKER_NETWORK` | *(unset)* | Docker network for ECS task containers |
| `FLOCI_SERVICES_ECS_DEFAULT_MEMORY_MB` | `512` | Default memory (MB) when task definition omits it |
diff --git a/docs/configuration/environment-variables.md b/docs/configuration/environment-variables.md
index 94c7db6870..b9a2eabbe6 100644
--- a/docs/configuration/environment-variables.md
+++ b/docs/configuration/environment-variables.md
@@ -322,6 +322,8 @@ These services spawn Docker containers. They require access to the Docker socket
| `FLOCI_SERVICES_RDS_DEFAULT_MYSQL_IMAGE` | `mysql:8.0` | Default MySQL Docker image |
| `FLOCI_SERVICES_RDS_DEFAULT_MARIADB_IMAGE` | `mariadb:11` | Default MariaDB Docker image |
| `FLOCI_SERVICES_RDS_DOCKER_NETWORK` | _(none)_ | Docker network for RDS containers (overrides `FLOCI_SERVICES_DOCKER_NETWORK`) |
+| `FLOCI_SERVICES_RDS_DATA_ENABLED` | `true` | Enable the RDS Data API service. Requires `FLOCI_SERVICES_RDS_ENABLED=true` |
+| `FLOCI_SERVICES_RDS_DATA_TRANSACTION_TTL_SECONDS` | `180` | Idle timeout, in seconds, before leaked RDS Data API transactions expire |
### OpenSearch
diff --git a/docs/index.md b/docs/index.md
index 90b1bf25f8..b56974c8d5 100644
--- a/docs/index.md
+++ b/docs/index.md
@@ -12,7 +12,7 @@ Floci is a fast, free, and open-source local AWS service emulator built for deve
## Supported Services
-Floci emulates 53 AWS services. See the [Services Overview](services/index.md) for per-service operation counts, endpoints, and full protocol details.
+Floci emulates 54 AWS services. See the [Services Overview](services/index.md) for per-service operation counts, endpoints, and full protocol details.
| Service | Protocol |
|---|---|
@@ -36,6 +36,7 @@ Floci emulates 53 AWS services. See the [Services Overview](services/index.md) f
| STS | Query |
| ElastiCache (Redis / Valkey) | Query + RESP proxy |
| RDS (PostgreSQL / MySQL) | Query + wire proxy |
+| RDS Data API | REST JSON |
| Neptune (graph DB / Gremlin) | Query + WebSocket proxy |
| MSK (Kafka / Redpanda) | REST JSON + Kafka |
| Athena | JSON 1.1 |
@@ -105,7 +106,7 @@ docker compose up -d
aws --endpoint-url http://localhost:4566 s3 mb s3://my-bucket
```
-All 51 AWS services are immediately available at `http://localhost:4566`.
+All 54 AWS services are immediately available at `http://localhost:4566`.
[Get started →](getting-started/quick-start.md){ .md-button .md-button--primary }
[View services →](services/index.md){ .md-button }
diff --git a/docs/services/index.md b/docs/services/index.md
index 5ce510e906..f8d450a484 100644
--- a/docs/services/index.md
+++ b/docs/services/index.md
@@ -1,6 +1,6 @@
# Services Overview
-Floci emulates 53 AWS services on a single port (`4566`). All services use the real AWS wire protocol, your existing AWS CLI commands and SDK clients work without modification.
+Floci emulates 54 AWS services on a single port (`4566`). All services use the real AWS wire protocol, your existing AWS CLI commands and SDK clients work without modification.
This page is the canonical reference for supported service and operation counts. Some services expose separate control-plane and data-plane rows below. Other docs (and the README) should link here rather than duplicating the table.
@@ -34,6 +34,7 @@ Operation counts are exact. For dispatch-table services (Query and JSON 1.1) eac
| [CloudWatch Metrics](cloudwatch.md#metrics) | `POST /` with `Action=` or JSON 1.1 | Query / JSON | 11 |
| [ElastiCache](elasticache.md) | `POST /` with `Action=` param + TCP proxy | Query + RESP | 8 |
| [RDS](rds.md) | `POST /` with `Action=` param + TCP proxy | Query + wire | 14 |
+| [RDS Data API](rds-data.md) | `/Execute`, `/BeginTransaction`, `/CommitTransaction`, `/RollbackTransaction` | REST JSON | 4 |
| [MSK](msk.md) | `/v1/clusters/...`, `/api/v2/clusters/...` + Redpanda broker | REST JSON + Kafka | 8 |
| [Athena](athena.md) | `POST /` + `X-Amz-Target: AmazonAthena.*` | JSON 1.1 | 4 |
| [Glue](glue.md) | `POST /` + `X-Amz-Target: AWSGlue.*` | JSON 1.1 | 38 |
@@ -69,7 +70,7 @@ Operation counts are exact. For dispatch-table services (Query and JSON 1.1) eac
| [BCM Data Exports](bcm-data-exports.md) | `POST /` + `X-Amz-Target: AWSBillingAndCostManagementDataExports.*` | JSON 1.1 | 7 |
| [Transfer Family](transfer.md) | `POST /` + `X-Amz-Target: TransferService.*` | JSON 1.1 | 17 |
-**Lambda, ElastiCache, RDS, MSK, ECS, EKS, and OpenSearch** spin up real Docker containers and support IAM authentication and SigV4 request signing, the same auth flow as production AWS.
+**Lambda, ElastiCache, RDS, MSK, ECS, EKS, and OpenSearch** spin up real Docker containers and support IAM authentication and SigV4 request signing, the same auth flow as production AWS. **RDS Data API** executes SQL against the local RDS containers through AWS-compatible REST JSON routes.
**ECR** runs a shared `registry:2` container so the stock `docker` client can push and pull image bytes against repositories returned by the AWS-shaped control plane. **EKS** (real mode) starts a k3s container per cluster and exposes the Kubernetes API server on a host port. **OpenSearch** (real mode) starts an `opensearchproject/opensearch` container per domain and exposes the data-plane REST API on a host port.
diff --git a/docs/services/rds-data.md b/docs/services/rds-data.md
new file mode 100644
index 0000000000..73ed6ff6fb
--- /dev/null
+++ b/docs/services/rds-data.md
@@ -0,0 +1,85 @@
+# RDS Data API
+
+**Protocol:** REST JSON
+**Endpoint:** `POST http://localhost:4566/{operation}`
+**Backing data plane:** Local RDS MySQL / MariaDB containers
+
+Floci implements the AWS RDS Data API routes used by AWS SDK clients and executes raw SQL against local RDS resources created through the RDS emulator. The first implementation focuses on MySQL / MariaDB compatibility for local development workflows that already use `ExecuteStatement` and transactions.
+
+For the upstream API shape, see the AWS RDS Data API documentation:
+
+- [Using the Data API for Aurora DB clusters](https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/data-api.html)
+- [Data API operations](https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/data-api-operations.html)
+- [`ExecuteStatement`](https://docs.aws.amazon.com/rdsdataservice/latest/APIReference/API_ExecuteStatement.html)
+- [`BeginTransaction`](https://docs.aws.amazon.com/rdsdataservice/latest/APIReference/API_BeginTransaction.html)
+- [`CommitTransaction`](https://docs.aws.amazon.com/rdsdataservice/latest/APIReference/API_CommitTransaction.html)
+- [`RollbackTransaction`](https://docs.aws.amazon.com/rdsdataservice/latest/APIReference/API_RollbackTransaction.html)
+- [`BatchExecuteStatement`](https://docs.aws.amazon.com/rdsdataservice/latest/APIReference/API_BatchExecuteStatement.html)
+
+## Supported Actions
+
+| Action | Route | Required request fields | Description |
+|---|---|---|---|
+| `ExecuteStatement` | `POST /Execute` | `resourceArn`, `secretArn`, `sql` | Execute raw SQL against a local RDS cluster or instance |
+| `BeginTransaction` | `POST /BeginTransaction` | `resourceArn`, `secretArn` | Open a JDBC transaction and return a transaction ID |
+| `CommitTransaction` | `POST /CommitTransaction` | `resourceArn`, `secretArn`, `transactionId` | Commit an open transaction |
+| `RollbackTransaction` | `POST /RollbackTransaction` | `resourceArn`, `secretArn`, `transactionId` | Roll back an open transaction |
+
+`BatchExecuteStatement` is recognized at `POST /BatchExecute` and returns an AWS-style `BadRequestException` because batch execution is not implemented yet. The deprecated `ExecuteSql` operation is also recognized at `POST /ExecuteSql` and returns an AWS-style `BadRequestException`.
+
+## Compatibility Notes
+
+- `resourceArn` and `secretArn` are required on Data API requests. `resourceArn` must identify an existing local RDS cluster or instance.
+- `database` is optional when the resolved RDS resource has a database name; otherwise it must be provided. Transactional `ExecuteStatement` requests must use the same database as the active transaction when `database` is present.
+- Transaction requests validate `resourceArn` against the active transaction resource. Floci resolves accepted ARN aliases to the local resource before comparing transaction identity.
+- MySQL and MariaDB resources are supported. PostgreSQL Data API execution is not implemented yet.
+- SQL is sent directly to the local database engine through JDBC. `SqlParameter` binding is not implemented yet; send raw SQL strings. Non-empty or malformed `parameters` requests return `BadRequestException`.
+- Result records include Data API field variants such as `stringValue`, `longValue`, `blobValue`, `booleanValue`, `doubleValue`, and `isNull`.
+- SQL errors are returned as `DatabaseErrorException` so AWS SDK callers can handle database failures with normal AWS error decoding.
+- If `secretArn` points to a local Secrets Manager secret with JSON credentials (`username` or `user`, plus `password`), those credentials are used. If the secret is missing or cannot be parsed, Floci falls back to the resolved RDS resource's master credentials for local development convenience.
+- `formatRecordsAs=JSON`, `formattedRecords`, `generatedFields`, and `resultSetOptions` are not implemented yet. Requests that require those unsupported result modes return `BadRequestException`.
+- RDS `HttpEndpointEnabled` control-plane gating is not modeled locally; availability is controlled by `FLOCI_SERVICES_RDS_DATA_ENABLED` and whether the target local RDS resource is running.
+
+## Configuration
+
+| Variable | Default | Description |
+|---|---|---|
+| `FLOCI_SERVICES_RDS_DATA_ENABLED` | `true` | Enable or disable the RDS Data API service |
+| `FLOCI_SERVICES_RDS_DATA_TRANSACTION_TTL_SECONDS` | `180` | Idle timeout, in seconds, before leaked Data API transactions expire |
+
+The RDS Data API also requires the RDS service itself to be enabled because it resolves `resourceArn` values to local RDS containers.
+
+## Example
+
+```bash
+export AWS_ENDPOINT_URL=http://localhost:4566
+
+aws rds create-db-cluster \
+ --db-cluster-identifier appdb \
+ --engine aurora-mysql \
+ --master-username admin \
+ --master-user-password secret123 \
+ --database-name app \
+ --endpoint-url "$AWS_ENDPOINT_URL"
+
+RESOURCE_ARN=$(aws rds describe-db-clusters \
+ --db-cluster-identifier appdb \
+ --query 'DBClusters[0].DBClusterArn' \
+ --output text \
+ --endpoint-url "$AWS_ENDPOINT_URL")
+
+SECRET_ARN=$(aws secretsmanager create-secret \
+ --name appdb/data-api \
+ --secret-string '{"username":"admin","password":"secret123"}' \
+ --query ARN \
+ --output text \
+ --endpoint-url "$AWS_ENDPOINT_URL")
+
+aws rds-data execute-statement \
+ --resource-arn "$RESOURCE_ARN" \
+ --secret-arn "$SECRET_ARN" \
+ --database app \
+ --sql "select 1 as count" \
+ --include-result-metadata \
+ --endpoint-url "$AWS_ENDPOINT_URL"
+```
diff --git a/docs/services/rds.md b/docs/services/rds.md
index 4b233d23cc..6f655b5e5c 100644
--- a/docs/services/rds.md
+++ b/docs/services/rds.md
@@ -6,6 +6,8 @@
Floci manages real PostgreSQL, MySQL, and MariaDB Docker containers and proxies TCP connections to them, including IAM authentication support.
+RDS Data API (`rds-data`) is documented separately because it uses REST JSON routes instead of the RDS Query protocol. See [RDS Data API](rds-data.md).
+
## Supported Management Actions
| Action | Description |
diff --git a/mkdocs.yml b/mkdocs.yml
index 197b13204c..10edc52341 100644
--- a/mkdocs.yml
+++ b/mkdocs.yml
@@ -92,6 +92,7 @@ nav:
- STS: services/sts.md
- ElastiCache: services/elasticache.md
- RDS: services/rds.md
+ - RDS Data API: services/rds-data.md
- MSK (Kafka): services/msk.md
- Glue: services/glue.md
- Neptune: services/neptune.md
diff --git a/pom.xml b/pom.xml
index 870a685bac..cc01a0f90a 100644
--- a/pom.xml
+++ b/pom.xml
@@ -229,6 +229,17 @@
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
</dependency>
+ <dependency>
+ <groupId>com.mysql</groupId>
+ <artifactId>mysql-connector-j</artifactId>
+ <version>8.3.0</version>
+ </dependency>
+ <dependency>
+ <groupId>com.h2database</groupId>
+ <artifactId>h2</artifactId>
+ <version>2.3.232</version>
+ <scope>test</scope>
+ </dependency>
</dependencies>
<build>
diff --git a/src/main/java/io/github/hectorvent/floci/config/EmulatorConfig.java b/src/main/java/io/github/hectorvent/floci/config/EmulatorConfig.java
index 545620e240..39c5605022 100644
--- a/src/main/java/io/github/hectorvent/floci/config/EmulatorConfig.java
+++ b/src/main/java/io/github/hectorvent/floci/config/EmulatorConfig.java
@@ -321,6 +321,7 @@ public interface EmulatorConfig {
MskServiceConfig msk();
ElastiCacheServiceConfig elasticache();
RdsServiceConfig rds();
+ RdsDataServiceConfig rdsData();
EventBridgeServiceConfig eventbridge();
CloudMapServiceConfig cloudmap();
SchedulerServiceConfig scheduler();
@@ -533,6 +534,14 @@ public interface EmulatorConfig {
Optional<String> dockerNetwork();
}
+ interface RdsDataServiceConfig {
+ @WithDefault("true")
+ boolean enabled();
+
+ @WithDefault("180")
+ long transactionTtlSeconds();
+ }
+
interface NeptuneServiceConfig {
@WithDefault("true")
boolean enabled();
diff --git a/src/main/java/io/github/hectorvent/floci/core/common/ResolvedServiceCatalog.java b/src/main/java/io/github/hectorvent/floci/core/common/ResolvedServiceCatalog.java
index 76d911ba1f..84de0aa4d2 100644
--- a/src/main/java/io/github/hectorvent/floci/core/common/ResolvedServiceCatalog.java
+++ b/src/main/java/io/github/hectorvent/floci/core/common/ResolvedServiceCatalog.java
@@ -15,6 +15,7 @@ import io.github.hectorvent.floci.services.cloudfront.CloudFrontController;
import io.github.hectorvent.floci.services.route53.Route53Controller;
import io.github.hectorvent.floci.services.ses.SesController;
import io.github.hectorvent.floci.services.appsync.AppSyncController;
+import io.github.hectorvent.floci.services.rdsdata.RdsDataController;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
@@ -89,6 +90,11 @@ public class ResolvedServiceCatalog {
5000L, AwsNamespaces.RDS, ServiceProtocol.QUERY,
protocols(ServiceProtocol.QUERY),
Set.of(), Set.of("rds"), Set.of(), Set.of()),
+ descriptor("rds-data", "rds-data",
+ config.services().rds().enabled() && config.services().rdsData().enabled(), true,
+ null, null, 5000L, null, ServiceProtocol.REST_JSON,
+ protocols(ServiceProtocol.REST_JSON),
+ Set.of(), Set.of("rds-data"), Set.of(), Set.of(RdsDataController.class)),
descriptor("neptune", "neptune", config.services().neptune().enabled(), true,
"neptune", storageMode(config.storage().services().neptune().mode(), config.storage().mode()),
5000L, AwsNamespaces.RDS, ServiceProtocol.QUERY,
diff --git a/src/main/java/io/github/hectorvent/floci/core/common/ServiceEnabledFilter.java b/src/main/java/io/github/hectorvent/floci/core/common/ServiceEnabledFilter.java
index 6e55bb5c76..de7159ef7f 100644
--- a/src/main/java/io/github/hectorvent/floci/core/common/ServiceEnabledFilter.java
+++ b/src/main/java/io/github/hectorvent/floci/core/common/ServiceEnabledFilter.java
@@ -116,6 +116,8 @@ public class ServiceEnabledFilter implements ContainerRequestFilter {
if (request.protocol() == ServiceProtocol.JSON || request.protocol() == ServiceProtocol.REST_JSON) {
return Response.status(400)
.type(MediaType.APPLICATION_JSON)
+ .header("X-Amzn-Errortype", "ServiceNotAvailableException")
+ .header("x-amzn-query-error", "ServiceNotAvailableException;Sender")
.entity(new AwsErrorResponse("ServiceNotAvailableException", message))
.build();
}
diff --git a/src/main/java/io/github/hectorvent/floci/services/iam/IamActionRegistry.java b/src/main/java/io/github/hectorvent/floci/services/iam/IamActionRegistry.java
index 502a68c9a5..ad0f1f45b5 100644
--- a/src/main/java/io/github/hectorvent/floci/services/iam/IamActionRegistry.java
+++ b/src/main/java/io/github/hectorvent/floci/services/iam/IamActionRegistry.java
@@ -69,6 +69,14 @@ public class IamActionRegistry {
// ── DynamoDB (JSON 1.1, action from X-Amz-Target handled separately) ──
// Handled via Query-style action extraction in the filter
+ // ── RDS Data API ───────────────────────────────────────────────────────
+ rule("rds-data", "POST", "^/Execute/?$", "rds-data:ExecuteStatement"),
+ rule("rds-data", "POST", "^/ExecuteSql/?$", "rds-data:ExecuteSql"),
+ rule("rds-data", "POST", "^/BatchExecute/?$", "rds-data:BatchExecuteStatement"),
+ rule("rds-data", "POST", "^/BeginTransaction/?$", "rds-data:BeginTransaction"),
+ rule("rds-data", "POST", "^/CommitTransaction/?$", "rds-data:CommitTransaction"),
+ rule("rds-data", "POST", "^/RollbackTransaction/?$", "rds-data:RollbackTransaction"),
+
// ── API Gateway ────────────────────────────────────────────────────────
rule("apigateway", "GET", ".*/account$", "apigateway:GET"),
rule("apigateway", "PATCH", ".*/account$", "apigateway:PATCH"),
diff --git a/src/main/java/io/github/hectorvent/floci/services/rdsdata/RdsDataConnectionFactory.java b/src/main/java/io/github/hectorvent/floci/services/rdsdata/RdsDataConnectionFactory.java
new file mode 100644
index 0000000000..a231ab04c7
--- /dev/null
+++ b/src/main/java/io/github/hectorvent/floci/services/rdsdata/RdsDataConnectionFactory.java
@@ -0,0 +1,31 @@
+package io.github.hectorvent.floci.services.rdsdata;
+
+import io.github.hectorvent.floci.core.common.AwsException;
+import io.github.hectorvent.floci.services.rds.model.DatabaseEngine;
+import jakarta.enterprise.context.ApplicationScoped;
+
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.SQLException;
+import java.util.Properties;
+
+@ApplicationScoped
+class RdsDataConnectionFactory {
+
+ Connection open(RdsDataResourceResolver.DatabaseTarget target,
+ String username,
+ String password,
+ String database) throws SQLException {
+ if (target.engine() != DatabaseEngine.MYSQL && target.engine() != DatabaseEngine.MARIADB) {
+ throw new AwsException("BadRequestException",
+ "RDS Data API currently supports local MySQL and MariaDB resources only.", 400);
+ }
+ String url = "jdbc:mysql://" + target.host() + ":" + target.port() + "/" + database
+ + "?useSSL=false&allowPublicKeyRetrieval=true";
+ Properties props = new Properties();
+ props.setProperty("user", username);
+ props.setProperty("password", password);
+ props.setProperty("connectTimeout", "5000");
+ return DriverManager.getConnection(url, props);
+ }
+}
diff --git a/src/main/java/io/github/hectorvent/floci/services/rdsdata/RdsDataController.java b/src/main/java/io/github/hectorvent/floci/services/rdsdata/RdsDataController.java
new file mode 100644
index 0000000000..1cd8691347
--- /dev/null
+++ b/src/main/java/io/github/hectorvent/floci/services/rdsdata/RdsDataController.java
@@ -0,0 +1,105 @@
+package io.github.hectorvent.floci.services.rdsdata;
+
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import io.github.hectorvent.floci.core.common.AwsErrorResponse;
+import io.github.hectorvent.floci.core.common.AwsException;
+import io.github.hectorvent.floci.core.common.RegionResolver;
+import jakarta.inject.Inject;
+import jakarta.ws.rs.Consumes;
+import jakarta.ws.rs.POST;
+import jakarta.ws.rs.Path;
+import jakarta.ws.rs.Produces;
+import jakarta.ws.rs.core.Context;
+import jakarta.ws.rs.core.HttpHeaders;
+import jakarta.ws.rs.core.MediaType;
+import jakarta.ws.rs.core.Response;
+import org.jboss.logging.Logger;
+
+@Path("/")
+@Produces(MediaType.APPLICATION_JSON)
+@Consumes(MediaType.WILDCARD)
+public class RdsDataController {
+
+ private static final Logger LOG = Logger.getLogger(RdsDataController.class);
+
+ private final RdsDataService service;
+ private final RegionResolver regionResolver;
+ private final ObjectMapper objectMapper;
+
+ @Inject
+ public RdsDataController(RdsDataService service, RegionResolver regionResolver, ObjectMapper objectMapper) {
+ this.service = service;
+ this.regionResolver = regionResolver;
+ this.objectMapper = objectMapper;
+ }
+
+ @POST
+ @Path("/Execute")
+ public Response executeStatement(@Context HttpHeaders headers, String body) {
+ return handle(headers, body, (request, region) -> Response.ok(service.executeStatement(request, region)).build());
+ }
+
+ @POST
+ @Path("/ExecuteSql")
+ public Response executeSql() {
+ return error(400, "BadRequestException",
+ "ExecuteSql is not supported by this local RDS Data API implementation.");
+ }
+
+ @POST
+ @Path("/BeginTransaction")
+ public Response beginTransaction(@Context HttpHeaders headers, String body) {
+ return handle(headers, body, (request, region) -> Response.ok(service.beginTransaction(request, region)).build());
+ }
+
+ @POST
+ @Path("/BatchExecute")
+ public Response batchExecuteStatement() {
+ return error(400, "BadRequestException",
+ "BatchExecuteStatement is not supported by this local RDS Data API implementation.");
+ }
+
+ @POST
+ @Path("/CommitTransaction")
+ public Response commitTransaction(@Context HttpHeaders headers, String body) {
+ return handle(headers, body, (request, region) -> Response.ok(service.commitTransaction(request)).build());
+ }
+
+ @POST
+ @Path("/RollbackTransaction")
+ public Response rollbackTransaction(@Context HttpHeaders headers, String body) {
+ return handle(headers, body, (request, region) -> Response.ok(service.rollbackTransaction(request)).build());
+ }
+
+ private Response handle(HttpHeaders headers, String body, Handler handler) {
+ try {
+ JsonNode request = body == null || body.isBlank()
+ ? objectMapper.createObjectNode()
+ : objectMapper.readTree(body);
+ return handler.handle(request, regionResolver.resolveRegion(headers));
+ } catch (AwsException e) {
+ return error(e.getHttpStatus(), e.getErrorCode(), e.getMessage());
+ } catch (JsonProcessingException e) {
+ return error(400, "BadRequestException", "Malformed JSON request body.");
+ } catch (Exception e) {
+ LOG.error("Error processing RDS Data API request", e);
+ return error(500, "InternalFailure", e.getMessage());
+ }
+ }
+
+ private static Response error(int status, String code, String message) {
+ return Response.status(status)
+ .type(MediaType.APPLICATION_JSON)
+ .header("X-Amzn-Errortype", code)
+ .header("x-amzn-query-error", code + ";" + (status < 500 ? "Sender" : "Receiver"))
+ .entity(new AwsErrorResponse(code, message))
+ .build();
+ }
+
+ @FunctionalInterface
+ private interface Handler {
+ Response handle(JsonNode request, String region);
+ }
+}
diff --git a/src/main/java/io/github/hectorvent/floci/services/rdsdata/RdsDataFieldMapper.java b/src/main/java/io/github/hectorvent/floci/services/rdsdata/RdsDataFieldMapper.java
new file mode 100644
index 0000000000..0bce95628d
--- /dev/null
+++ b/src/main/java/io/github/hectorvent/floci/services/rdsdata/RdsDataFieldMapper.java
@@ -0,0 +1,126 @@
+package io.github.hectorvent.floci.services.rdsdata;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+
+import java.math.BigDecimal;
+import java.sql.Blob;
+import java.sql.Date;
+import java.sql.SQLException;
+import java.sql.Time;
+import java.sql.Timestamp;
+import java.sql.Types;
+import java.time.LocalDate;
+import java.time.LocalDateTime;
+import java.time.LocalTime;
+import java.time.format.DateTimeFormatter;
+
+final class RdsDataFieldMapper {
+
+ private static final DateTimeFormatter TIMESTAMP_BASE_FORMATTER =
+ DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
+
+ private RdsDataFieldMapper() {
+ }
+
+ static ObjectNode toField(ObjectMapper objectMapper, Object value, int sqlType) throws SQLException {
+ ObjectNode field = objectMapper.createObjectNode();
+ if (value == null) {
+ field.put("isNull", true);
+ return field;
+ }
+
+ switch (sqlType) {
+ case Types.BIGINT, Types.INTEGER, Types.SMALLINT, Types.TINYINT -> {
+ if (value instanceof Number number) {
+ field.put("longValue", number.longValue());
+ } else {
+ field.put("longValue", Long.parseLong(value.toString()));
+ }
+ }
+ case Types.BINARY, Types.VARBINARY, Types.LONGVARBINARY, Types.BLOB -> {
+ if (value instanceof byte[] bytes) {
+ field.put("blobValue", bytes);
+ } else if (value instanceof Blob blob) {
+ field.put("blobValue", blob.getBytes(1, Math.toIntExact(blob.length())));
+ } else {
+ field.put("blobValue", value.toString().getBytes(java.nio.charset.StandardCharsets.UTF_8));
+ }
+ }
+ case Types.BOOLEAN, Types.BIT -> {
+ if (value instanceof Boolean bool) {
+ field.put("booleanValue", bool);
+ } else if (value instanceof Number number) {
+ field.put("booleanValue", number.longValue() != 0);
+ } else {
+ field.put("booleanValue", Boolean.parseBoolean(value.toString()));
+ }
+ }
+ case Types.FLOAT, Types.REAL, Types.DOUBLE -> {
+ if (value instanceof Number number) {
+ field.put("doubleValue", number.doubleValue());
+ } else {
+ field.put("doubleValue", Double.parseDouble(value.toString()));
+ }
+ }
+ case Types.NUMERIC, Types.DECIMAL -> mapDecimal(field, value);
+ case Types.DATE -> field.put("stringValue", formatDate(value));
+ case Types.TIME -> field.put("stringValue", formatTime(value));
+ case Types.TIMESTAMP, Types.TIMESTAMP_WITH_TIMEZONE -> field.put("stringValue", formatTimestamp(value));
+ default -> field.put("stringValue", value.toString());
+ }
+ return field;
+ }
+
+ private static String formatDate(Object value) {
+ if (value instanceof Date date) {
+ return date.toLocalDate().toString();
+ }
+ if (value instanceof LocalDate localDate) {
+ return localDate.toString();
+ }
+ return value.toString();
+ }
+
+ private static String formatTime(Object value) {
+ if (value instanceof Time time) {
+ return time.toLocalTime().toString();
+ }
+ if (value instanceof LocalTime localTime) {
+ return localTime.toString();
+ }
+ return value.toString();
+ }
+
+ private static String formatTimestamp(Object value) {
+ if (value instanceof Timestamp timestamp) {
+ return formatLocalDateTime(timestamp.toLocalDateTime());
+ }
+ if (value instanceof LocalDateTime localDateTime) {
+ return formatLocalDateTime(localDateTime);
+ }
+ return value.toString();
+ }
+
+ private static String formatLocalDateTime(LocalDateTime localDateTime) {
+ String value = localDateTime.format(TIMESTAMP_BASE_FORMATTER);
+ int nanos = localDateTime.getNano();
+ if (nanos == 0) {
+ return value;
+ }
+ String fraction = String.format("%09d", nanos);
+ int end = fraction.length();
+ while (end > 0 && fraction.charAt(end - 1) == '0') {
+ end--;
+ }
+ return value + "." + fraction.substring(0, end);
+ }
+
+ private static void mapDecimal(ObjectNode field, Object value) {
+ if (value instanceof BigDecimal decimal) {
+ field.put("stringValue", decimal.toPlainString());
+ return;
+ }
+ field.put("stringValue", value.toString());
+ }
+}
diff --git a/src/main/java/io/github/hectorvent/floci/services/rdsdata/RdsDataResourceResolver.java b/src/main/java/io/github/hectorvent/floci/services/rdsdata/RdsDataResourceResolver.java
new file mode 100644
index 0000000000..b005574e0f
--- /dev/null
+++ b/src/main/java/io/github/hectorvent/floci/services/rdsdata/RdsDataResourceResolver.java
@@ -0,0 +1,89 @@
+package io.github.hectorvent.floci.services.rdsdata;
+
+import io.github.hectorvent.floci.core.common.AwsException;
+import io.github.hectorvent.floci.services.rds.RdsService;
+import io.github.hectorvent.floci.services.rds.model.DatabaseEngine;
+import io.github.hectorvent.floci.services.rds.model.DbCluster;
+import io.github.hectorvent.floci.services.rds.model.DbInstance;
+import jakarta.enterprise.context.ApplicationScoped;
+import jakarta.inject.Inject;
+
+@ApplicationScoped
+class RdsDataResourceResolver {
+
+ private final RdsService rdsService;
+
+ @Inject
+ RdsDataResourceResolver(RdsService rdsService) {
+ this.rdsService = rdsService;
+ }
+
+ DatabaseTarget resolve(String resourceArn) {
+ if (resourceArn == null || resourceArn.isBlank()) {
+ throw new AwsException("BadRequestException", "resourceArn is required.", 400);
+ }
+
+ for (DbCluster cluster : rdsService.listDbClusters(null)) {
+ if (resourceArn.equals(cluster.getDbClusterArn())) {
+ return fromCluster(cluster);
+ }
+ }
+ for (DbInstance instance : rdsService.listDbInstances(null)) {
+ if (resourceArn.equals(instance.getDbInstanceArn())) {
+ return fromInstance(instance);
+ }
+ }
+
+ String id = identifierFromArn(resourceArn);
+ if (id != null) {
+ try {
+ return fromCluster(rdsService.getDbCluster(id));
+ } catch (AwsException ignored) {
+ // Try instances below.
+ }
+ try {
+ return fromInstance(rdsService.getDbInstance(id));
+ } catch (AwsException ignored) {
+ // Fall through to the Data API error shape.
+ }
+ }
+
+ throw new AwsException("BadRequestException",
+ "resourceArn does not resolve to a local RDS resource: " + resourceArn, 400);
+ }
+
+ private static DatabaseTarget fromCluster(DbCluster cluster) {
+ return target(cluster.getDbClusterArn(), cluster.getEngine(), cluster.getContainerHost(), cluster.getContainerPort(),
+ cluster.getMasterUsername(), cluster.getMasterPassword(), cluster.getDatabaseName());
+ }
+
+ private static DatabaseTarget fromInstance(DbInstance instance) {
+ return target(instance.getDbInstanceArn(), instance.getEngine(), instance.getContainerHost(), instance.getContainerPort(),
+ instance.getMasterUsername(), instance.getMasterPassword(), instance.getDbName());
+ }
+
+ private static DatabaseTarget target(String arn, DatabaseEngine engine, String host, int port,
+ String username, String password, String databaseName) {
+ if (host == null || host.isBlank() || port <= 0) {
+ throw new AwsException("BadRequestException",
+ "RDS resource runtime is not available for Data API execution.", 400);
+ }
+ return new DatabaseTarget(arn, engine, host, port, username, password, databaseName);
+ }
+
+ private static String identifierFromArn(String arn) {
+ int marker = arn.lastIndexOf(":cluster:");
+ if (marker >= 0) {
+ return arn.substring(marker + ":cluster:".length());
+ }
+ marker = arn.lastIndexOf(":db:");
+ if (marker >= 0) {
+ return arn.substring(marker + ":db:".length());
+ }
+ return null;
+ }
+
+ record DatabaseTarget(String arn, DatabaseEngine engine, String host, int port,
+ String username, String password, String databaseName) {
+ }
+}
diff --git a/src/main/java/io/github/hectorvent/floci/services/rdsdata/RdsDataService.java b/src/main/java/io/github/hectorvent/floci/services/rdsdata/RdsDataService.java
new file mode 100644
index 0000000000..927fc16c84
--- /dev/null
+++ b/src/main/java/io/github/hectorvent/floci/services/rdsdata/RdsDataService.java
@@ -0,0 +1,447 @@
+package io.github.hectorvent.floci.services.rdsdata;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.node.ArrayNode;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import io.github.hectorvent.floci.config.EmulatorConfig;
+import io.github.hectorvent.floci.core.common.AwsException;
+import io.github.hectorvent.floci.services.secretsmanager.SecretsManagerService;
+import io.github.hectorvent.floci.services.secretsmanager.model.SecretVersion;
+import jakarta.annotation.PostConstruct;
+import jakarta.annotation.PreDestroy;
+import jakarta.enterprise.context.ApplicationScoped;
+import jakarta.inject.Inject;
+import org.jboss.logging.Logger;
+
+import java.sql.Connection;
+import java.sql.ResultSet;
+import java.sql.ResultSetMetaData;
+import java.sql.SQLException;
+import java.sql.Statement;
+import java.time.Duration;
+import java.time.Instant;
+import java.util.UUID;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentMap;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.TimeUnit;
+
+@ApplicationScoped
+public class RdsDataService {
+
+ private static final Logger LOG = Logger.getLogger(RdsDataService.class);
+
+ private final RdsDataResourceResolver resourceResolver;
+ private final SecretsManagerService secretsManagerService;
+ private final ObjectMapper objectMapper;
+ private final RdsDataConnectionFactory connectionFactory;
+ private final Duration transactionTtl;
+ private final ConcurrentMap<String, TransactionContext> transactions = new ConcurrentHashMap<>();
+ private final ScheduledExecutorService transactionCleanupExecutor;
+
+ @Inject
+ public RdsDataService(RdsDataResourceResolver resourceResolver,
+ SecretsManagerService secretsManagerService,
+ ObjectMapper objectMapper,
+ RdsDataConnectionFactory connectionFactory,
+ EmulatorConfig config) {
+ this(resourceResolver, secretsManagerService, objectMapper, connectionFactory,
+ Duration.ofSeconds(config.services().rdsData().transactionTtlSeconds()));
+ }
+
+ RdsDataService(RdsDataResourceResolver resourceResolver,
+ SecretsManagerService secretsManagerService,
+ ObjectMapper objectMapper,
+ RdsDataConnectionFactory connectionFactory,
+ Duration transactionTtl) {
+ this.resourceResolver = resourceResolver;
+ this.secretsManagerService = secretsManagerService;
+ this.objectMapper = objectMapper;
+ this.connectionFactory = connectionFactory;
+ this.transactionTtl = transactionTtl;
+ this.transactionCleanupExecutor = Executors.newSingleThreadScheduledExecutor(r -> {
+ Thread thread = new Thread(r, "rds-data-transaction-cleanup");
+ thread.setDaemon(true);
+ return thread;
+ });
+ }
+
+ @PostConstruct
+ void startTransactionCleanup() {
+ long intervalSeconds = Math.max(1, Math.min(60, transactionTtl.toSeconds()));
+ transactionCleanupExecutor.scheduleAtFixedRate(this::cleanupExpiredTransactionsSafely,
+ intervalSeconds, intervalSeconds, TimeUnit.SECONDS);
+ }
+
+ @PreDestroy
+ void shutdown() {
+ transactionCleanupExecutor.shutdownNow();
+ transactions.forEach((id, tx) -> {
+ synchronized (tx) {
+ if (transactions.remove(id, tx)) {
+ rollbackQuietly(tx.connection);
+ closeQuietly(tx.connection);
+ }
+ }
+ });
+ }
+
+ public ObjectNode executeStatement(JsonNode request, String region) {
+ rejectUnsupportedOptions(request);
+
+ String sql = requiredText(request, "sql");
+ String resourceArn = requiredText(request, "resourceArn");
+ requiredText(request, "secretArn");
+ String transactionId = textOrNull(request, "transactionId");
+ boolean includeMetadata = request.path("includeResultMetadata").asBoolean(false);
+
+ try {
+ if (transactionId != null && !transactionId.isBlank()) {
+ TransactionContext tx = transaction(transactionId);
+ synchronized (tx) {
+ requireActiveTransaction(transactionId, tx);
+ validateTransactionIdentity(tx, request);
+ tx.refresh(transactionTtl);
+ return executeOnConnection(tx.connection, sql, includeMetadata);
+ }
+ }
+
+ RdsDataResourceResolver.DatabaseTarget target = resourceResolver.resolve(resourceArn);
+ Credentials credentials = credentials(request, target, region);
+ String database = databaseName(request, target);
+ try (Connection connection = connectionFactory.open(target, credentials.username(), credentials.password(), database)) {
+ return executeOnConnection(connection, sql, includeMetadata);
+ }
+ } catch (SQLException e) {
+ throw databaseError(e);
+ }
+ }
+
+ public ObjectNode beginTransaction(JsonNode request, String region) {
+ cleanupExpiredTransactions();
+ String resourceArn = requiredText(request, "resourceArn");
+ requiredText(request, "secretArn");
+ RdsDataResourceResolver.DatabaseTarget target = resourceResolver.resolve(resourceArn);
+ Credentials credentials = credentials(request, target, region);
+ String database = databaseName(request, target);
+
+ Connection connection = null;
+ try {
+ connection = connectionFactory.open(target, credentials.username(), credentials.password(), database);
+ connection.setAutoCommit(false);
+ String transactionId = UUID.randomUUID().toString();
+ transactions.put(transactionId, new TransactionContext(transactionId, connection, target.arn(), database, transactionTtl));
+
+ ObjectNode response = objectMapper.createObjectNode();
+ response.put("transactionId", transactionId);
+ return response;
+ } catch (SQLException e) {
+ if (connection != null) {
+ closeQuietly(connection);
+ }
+ throw databaseError(e);
+ }
+ }
+
+ public ObjectNode commitTransaction(JsonNode request) {
+ String transactionId = requiredText(request, "transactionId");
+ String resourceArn = requiredText(request, "resourceArn");
+ requiredText(request, "secretArn");
+ TransactionContext tx = transaction(transactionId);
+ synchronized (tx) {
+ requireActiveTransaction(transactionId, tx);
+ validateTransactionResource(tx, resourceArn);
+ if (!transactions.remove(transactionId, tx)) {
+ throw transactionNotFound(transactionId);
+ }
+ try {
+ tx.connection.commit();
+ } catch (SQLException e) {
+ throw databaseError(e);
+ } finally {
+ closeQuietly(tx.connection);
+ }
+ }
+ ObjectNode response = objectMapper.createObjectNode();
+ response.put("transactionStatus", "Transaction Committed");
+ return response;
+ }
+
+ public ObjectNode rollbackTransaction(JsonNode request) {
+ String transactionId = requiredText(request, "transactionId");
+ String resourceArn = requiredText(request, "resourceArn");
+ requiredText(request, "secretArn");
+ TransactionContext tx = transaction(transactionId);
+ synchronized (tx) {
+ requireActiveTransaction(transactionId, tx);
+ validateTransactionResource(tx, resourceArn);
+ if (!transactions.remove(transactionId, tx)) {
+ throw transactionNotFound(transactionId);
+ }
+ try {
+ tx.connection.rollback();
+ } catch (SQLException e) {
+ throw databaseError(e);
+ } finally {
+ closeQuietly(tx.connection);
+ }
+ }
+ ObjectNode response = objectMapper.createObjectNode();
+ response.put("transactionStatus", "Rollback Complete");
+ return response;
+ }
+
+ private ObjectNode executeOnConnection(Connection connection, String sql, boolean includeMetadata) throws SQLException {
+ try (Statement statement = connection.createStatement()) {
+ boolean hasResultSet = statement.execute(sql);
+ ObjectNode response = objectMapper.createObjectNode();
+ if (hasResultSet) {
+ try (ResultSet rs = statement.getResultSet()) {
+ ResultSetMetaData meta = rs.getMetaData();
+ if (includeMetadata) {
+ response.set("columnMetadata", columnMetadata(meta));
+ }
+ response.set("records", records(rs, meta));
+ }
+ response.put("numberOfRecordsUpdated", 0L);
+ } else {
+ int updateCount = statement.getUpdateCount();
+ response.set("records", objectMapper.createArrayNode());
+ response.put("numberOfRecordsUpdated", Math.max(updateCount, 0));
+ }
+ return response;
+ }
+ }
+
+ private ArrayNode columnMetadata(ResultSetMetaData meta) throws SQLException {
+ ArrayNode columns = objectMapper.createArrayNode();
+ for (int i = 1; i <= meta.getColumnCount(); i++) {
+ ObjectNode column = objectMapper.createObjectNode();
+ String name = meta.getColumnLabel(i);
+ if (name == null || name.isBlank()) {
+ name = meta.getColumnName(i);
+ }
+ column.put("name", name);
+ columns.add(column);
+ }
+ return columns;
+ }
+
+ private ArrayNode records(ResultSet rs, ResultSetMetaData meta) throws SQLException {
+ ArrayNode records = objectMapper.createArrayNode();
+ int columnCount = meta.getColumnCount();
+ while (rs.next()) {
+ ArrayNode row = objectMapper.createArrayNode();
+ for (int i = 1; i <= columnCount; i++) {
+ row.add(RdsDataFieldMapper.toField(objectMapper, rs.getObject(i), meta.getColumnType(i)));
+ }
+ records.add(row);
+ }
+ return records;
+ }
+
+ private Credentials credentials(JsonNode request, RdsDataResourceResolver.DatabaseTarget target, String region) {
+ String secretArn = textOrNull(request, "secretArn");
+ if (secretArn != null && !secretArn.isBlank()) {
+ try {
+ SecretVersion secret = secretsManagerService.getSecretValue(secretArn, null, null, region);
+ Credentials fromSecret = parseSecretCredentials(secret.getSecretString());
+ if (fromSecret != null) {
+ return fromSecret;
+ }
+ } catch (AwsException e) {
+ LOG.debugv("Falling back to RDS master credentials for Data API secret {0}: {1}",
+ secretArn, e.getMessage());
+ }
+ }
+ String username = target.username() != null && !target.username().isBlank() ? target.username() : "root";
+ return new Credentials(username, target.password());
+ }
+
+ private Credentials parseSecretCredentials(String secretString) {
+ if (secretString == null || secretString.isBlank()) {
+ return null;
+ }
+ try {
+ JsonNode secret = objectMapper.readTree(secretString);
+ String username = textOrNull(secret, "username");
+ if (username == null) {
+ username = textOrNull(secret, "user");
+ }
+ String password = textOrNull(secret, "password");
+ if (username != null && password != null) {
+ return new Credentials(username, password);
+ }
+ } catch (Exception e) {
+ LOG.debugv("Could not parse RDS Data API secret credentials: {0}", e.getMessage());
+ }
+ return null;
+ }
+
+ private String databaseName(JsonNode request, RdsDataResourceResolver.DatabaseTarget target) {
+ String database = textOrNull(request, "database");
+ if (database != null && !database.isBlank()) {
+ return database;
+ }
+ if (target.databaseName() != null && !target.databaseName().isBlank()) {
+ return target.databaseName();
+ }
+ throw new AwsException("BadRequestException", "database is required.", 400);
+ }
+
+ private TransactionContext transaction(String transactionId) {
+ cleanupExpiredTransactions();
+ TransactionContext tx = transactions.get(transactionId);
+ if (tx == null) {
+ throw transactionNotFound(transactionId);
+ }
+ return tx;
+ }
+
+ private static AwsException transactionNotFound(String transactionId) {
+ return new AwsException("TransactionNotFoundException",
+ "Transaction " + transactionId + " was not found.", 404);
+ }
+
+ private void requireActiveTransaction(String transactionId, TransactionContext tx) {
+ if (transactions.get(transactionId) != tx) {
+ throw transactionNotFound(transactionId);
+ }
+ }
+
+ private void cleanupExpiredTransactions() {
+ Instant now = Instant.now();
+ transactions.forEach((id, tx) -> {
+ if (tx.expiresAt.isBefore(now)) {
+ synchronized (tx) {
+ if (tx.expiresAt.isBefore(now) && transactions.remove(id, tx)) {
+ rollbackQuietly(tx.connection);
+ closeQuietly(tx.connection);
+ }
+ }
+ }
+ });
+ }
+
+ private void cleanupExpiredTransactionsSafely() {
+ try {
+ cleanupExpiredTransactions();
+ } catch (Exception e) {
+ LOG.warn("Failed to clean up expired RDS Data API transactions", e);
+ }
+ }
+
+ private void validateTransactionIdentity(TransactionContext tx, JsonNode request) {
+ validateTransactionResource(tx, requiredText(request, "resourceArn"));
+ String database = textOrNull(request, "database");
+ if (database != null && !database.isBlank() && !database.equals(tx.database)) {
+ throw transactionNotFound(tx.id);
+ }
+ }
+
+ private void validateTransactionResource(TransactionContext tx, String resourceArn) {
+ if (resourceArn.equals(tx.resourceArn)) {
+ return;
+ }
+ RdsDataResourceResolver.DatabaseTarget target;
+ try {
+ target = resourceResolver.resolve(resourceArn);
+ } catch (AwsException e) {
+ throw transactionNotFound(tx.id);
+ }
+ if (target == null || !target.arn().equals(tx.resourceArn)) {
+ throw transactionNotFound(tx.id);
+ }
+ }
+
+ private static void rollbackQuietly(Connection connection) {
+ try {
+ connection.rollback();
+ } catch (SQLException ignored) {
+ }
+ }
+
+ private static void closeQuietly(Connection connection) {
+ try {
+ connection.close();
+ } catch (SQLException ignored) {
+ }
+ }
+
+ private static void rejectUnsupportedOptions(JsonNode request) {
+ rejectSqlParameters(request);
+ rejectFormattedRecords(request);
+ rejectResultSetOptions(request);
+ }
+
+ private static void rejectSqlParameters(JsonNode request) {
+ JsonNode parameters = request.get("parameters");
+ if (parameters != null && (!parameters.isArray() || !parameters.isEmpty())) {
+ throw new AwsException("BadRequestException",
+ "SqlParameter binding is not supported by this local RDS Data API implementation.", 400);
+ }
+ }
+
+ private static void rejectFormattedRecords(JsonNode request) {
+ String formatRecordsAs = textOrNull(request, "formatRecordsAs");
+ if (formatRecordsAs != null && !formatRecordsAs.isBlank()
+ && !"NONE".equalsIgnoreCase(formatRecordsAs)) {
+ throw new AwsException("BadRequestException",
+ "formattedRecords is not supported by this local RDS Data API implementation.", 400);
+ }
+ }
+
+ private static void rejectResultSetOptions(JsonNode request) {
+ JsonNode resultSetOptions = request.get("resultSetOptions");
+ if (resultSetOptions != null && !resultSetOptions.isNull()
+ && (!resultSetOptions.isObject() || !resultSetOptions.isEmpty())) {
+ throw new AwsException("BadRequestException",
+ "resultSetOptions is not supported by this local RDS Data API implementation.", 400);
+ }
+ }
+
+ private static String requiredText(JsonNode request, String name) {
+ String value = textOrNull(request, name);
+ if (value == null || value.isBlank()) {
+ throw new AwsException("BadRequestException", name + " is required.", 400);
+ }
+ return value;
+ }
+
+ private static String textOrNull(JsonNode request, String name) {
+ JsonNode node = request.get(name);
+ if (node == null || node.isNull()) {
+ return null;
+ }
+ return node.asText();
+ }
+
+ private static AwsException databaseError(SQLException e) {
+ return new AwsException("DatabaseErrorException", e.getMessage(), 400);
+ }
+
+ private static final class TransactionContext {
+ private final String id;
+ private final Connection connection;
+ private final String resourceArn;
+ private final String database;
+ private volatile Instant expiresAt;
+
+ private TransactionContext(String id, Connection connection, String resourceArn, String database, Duration ttl) {
+ this.id = id;
+ this.connection = connection;
+ this.resourceArn = resourceArn;
+ this.database = database;
+ refresh(ttl);
+ }
+
+ private void refresh(Duration ttl) {
+ this.expiresAt = Instant.now().plus(ttl);
+ }
+ }
+
+ private record Credentials(String username, String password) {
+ }
+}
diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml
index d2f61e4f7f..e6a971e4ac 100644
--- a/src/main/resources/application.yml
+++ b/src/main/resources/application.yml
@@ -182,6 +182,9 @@ floci:
default-postgres-image: "postgres:16-alpine"
default-mysql-image: "mysql:8.0"
default-mariadb-image: "mariadb:11"
+ rds-data:
+ enabled: true
+ transaction-ttl-seconds: 180
neptune:
enabled: true
proxy-base-port: 8182
diff --git a/src/test/java/io/github/hectorvent/floci/core/common/ServiceCatalogRoutingIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/core/common/ServiceCatalogRoutingIntegrationTest.java
index 2fdfadc839..eb8d4cb585 100644
--- a/src/test/java/io/github/hectorvent/floci/core/common/ServiceCatalogRoutingIntegrationTest.java
+++ b/src/test/java/io/github/hectorvent/floci/core/common/ServiceCatalogRoutingIntegrationTest.java
@@ -43,6 +43,15 @@ class ServiceCatalogRoutingIntegrationTest {
assertTrue(catalog.byCredentialScope("cognito-idp").orElseThrow().supportsProtocol(ServiceProtocol.QUERY));
}
+ @Test
+ void rdsDataResolvesAsRestJsonByCredentialScope() {
+ ServiceDescriptor descriptor = catalog.byCredentialScope("rds-data").orElseThrow();
+
+ assertEquals("rds-data", descriptor.externalKey());
+ assertEquals(ServiceProtocol.REST_JSON, descriptor.defaultProtocol());
+ assertTrue(descriptor.supportsProtocol(ServiceProtocol.REST_JSON));
+ }
+
@Test
void unknownTargetsRemainUnresolved() {
assertTrue(catalog.matchTarget("UnknownService.DoThing").isEmpty());
diff --git a/src/test/java/io/github/hectorvent/floci/core/common/ServiceEnablementIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/core/common/ServiceEnablementIntegrationTest.java
index da6e59067a..78fc053ceb 100644
--- a/src/test/java/io/github/hectorvent/floci/core/common/ServiceEnablementIntegrationTest.java
+++ b/src/test/java/io/github/hectorvent/floci/core/common/ServiceEnablementIntegrationTest.java
@@ -156,6 +156,25 @@ class ServiceEnablementIntegrationTest {
.body("message", equalTo("Service es is not enabled."));
}
+ @Test
+ void signedRdsDataExecuteRequestsReturnJsonWhenServiceDisabled() {
+ given()
+ .contentType("application/json")
+ .header("Authorization", authorization("rds-data"))
+ .body("""
+ {"resourceArn":"arn:aws:rds:us-east-1:000000000000:cluster:db","secretArn":"arn","database":"app","sql":"select 1"}
+ """)
+ .when()
+ .post("/Execute")
+ .then()
+ .statusCode(400)
+ .contentType("application/json")
+ .header("X-Amzn-Errortype", "ServiceNotAvailableException")
+ .header("x-amzn-query-error", "ServiceNotAvailableException;Sender")
+ .body("__type", equalTo("ServiceNotAvailableException"))
+ .body("message", equalTo("Service rds-data is not enabled."));
+ }
+
private static JsonNode cborBody(byte[] body) throws Exception {
return CBOR_MAPPER.readTree(body);
}
@@ -174,6 +193,7 @@ class ServiceEnablementIntegrationTest {
"floci.services.ecs.enabled", "false",
"floci.services.lambda.enabled", "false",
"floci.services.opensearch.enabled", "false",
+ "floci.services.rds-data.enabled", "false",
"floci.services.sqs.enabled", "false"
);
}
diff --git a/src/test/java/io/github/hectorvent/floci/services/iam/IamActionRegistryTest.java b/src/test/java/io/github/hectorvent/floci/services/iam/IamActionRegistryTest.java
index fcfb099348..aa15dfc446 100644
--- a/src/test/java/io/github/hectorvent/floci/services/iam/IamActionRegistryTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/iam/IamActionRegistryTest.java
@@ -95,6 +95,22 @@ class IamActionRegistryTest {
assertEquals("dynamodb:PutItem", registry.resolve("dynamodb", ctx));
}
+ @Test
+ void resolvesRdsDataRestJsonRoutes() {
+ assertEquals("rds-data:ExecuteStatement", registry.resolve("rds-data",
+ mockCtx("POST", "/Execute", new MultivaluedHashMap<>(), MediaType.APPLICATION_JSON_TYPE, "{}")));
+ assertEquals("rds-data:ExecuteSql", registry.resolve("rds-data",
+ mockCtx("POST", "/ExecuteSql", new MultivaluedHashMap<>(), MediaType.APPLICATION_JSON_TYPE, "{}")));
+ assertEquals("rds-data:BatchExecuteStatement", registry.resolve("rds-data",
+ mockCtx("POST", "/BatchExecute", new MultivaluedHashMap<>(), MediaType.APPLICATION_JSON_TYPE, "{}")));
+ assertEquals("rds-data:BeginTransaction", registry.resolve("rds-data",
+ mockCtx("POST", "/BeginTransaction", new MultivaluedHashMap<>(), MediaType.APPLICATION_JSON_TYPE, "{}")));
+ assertEquals("rds-data:CommitTransaction", registry.resolve("rds-data",
+ mockCtx("POST", "/CommitTransaction", new MultivaluedHashMap<>(), MediaType.APPLICATION_JSON_TYPE, "{}")));
+ assertEquals("rds-data:RollbackTransaction", registry.resolve("rds-data",
+ mockCtx("POST", "/RollbackTransaction", new MultivaluedHashMap<>(), MediaType.APPLICATION_JSON_TYPE, "{}")));
+ }
+
@Test
void returnsNullForUnknownRestJsonRoute() {
ContainerRequestContext ctx = mockCtx(
@@ -133,4 +149,4 @@ class IamActionRegistryTest {
}).when(ctx).setEntityStream(any(InputStream.class));
return ctx;
}
-}
\ No newline at end of file
+}
diff --git a/src/test/java/io/github/hectorvent/floci/services/rdsdata/RdsDataControllerIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/rdsdata/RdsDataControllerIntegrationTest.java
new file mode 100644
index 0000000000..a3e44deb54
--- /dev/null
+++ b/src/test/java/io/github/hectorvent/floci/services/rdsdata/RdsDataControllerIntegrationTest.java
@@ -0,0 +1,145 @@
+package io.github.hectorvent.floci.services.rdsdata;
+
+import io.quarkus.test.junit.QuarkusTest;
+import org.junit.jupiter.api.Test;
+
+import static io.restassured.RestAssured.given;
+import static org.hamcrest.Matchers.equalTo;
+
+@QuarkusTest
+class RdsDataControllerIntegrationTest {
+
+ @Test
+ void executeRejectsSqlParametersWithJsonDataApiError() {
+ given()
+ .contentType("application/json")
+ .body("""
+ {
+ "resourceArn": "arn:aws:rds:us-east-1:000000000000:cluster:missing",
+ "secretArn": "arn:aws:secretsmanager:us-east-1:000000000000:secret:missing",
+ "database": "app",
+ "sql": "select 1",
+ "parameters": [{"name": "id", "value": {"longValue": 1}}]
+ }
+ """)
+ .when()
+ .post("/Execute")
+ .then()
+ .statusCode(400)
+ .contentType("application/json")
+ .header("X-Amzn-Errortype", "BadRequestException")
+ .header("x-amzn-query-error", "BadRequestException;Sender")
+ .body("__type", equalTo("BadRequestException"));
+ }
+
+ @Test
+ void executeRejectsFormattedRecordsWithJsonDataApiError() {
+ given()
+ .contentType("application/json")
+ .body("""
+ {
+ "resourceArn": "arn:aws:rds:us-east-1:000000000000:cluster:missing",
+ "secretArn": "arn:aws:secretsmanager:us-east-1:000000000000:secret:missing",
+ "database": "app",
+ "sql": "select 1",
+ "formatRecordsAs": "JSON"
+ }
+ """)
+ .when()
+ .post("/Execute")
+ .then()
+ .statusCode(400)
+ .contentType("application/json")
+ .header("X-Amzn-Errortype", "BadRequestException")
+ .header("x-amzn-query-error", "BadRequestException;Sender")
+ .body("__type", equalTo("BadRequestException"));
+ }
+
+ @Test
+ void malformedJsonReturnsBadRequestJsonDataApiError() {
+ given()
+ .contentType("application/json")
+ .body("{")
+ .when()
+ .post("/Execute")
+ .then()
+ .statusCode(400)
+ .contentType("application/json")
+ .header("X-Amzn-Errortype", "BadRequestException")
+ .header("x-amzn-query-error", "BadRequestException;Sender")
+ .body("__type", equalTo("BadRequestException"));
+ }
+
+ @Test
+ void executeUnknownResourceReturnsJsonDataApiError() {
+ given()
+ .contentType("application/json")
+ .body("""
+ {
+ "resourceArn": "arn:aws:rds:us-east-1:000000000000:cluster:missing",
+ "secretArn": "arn:aws:secretsmanager:us-east-1:000000000000:secret:missing",
+ "database": "app",
+ "sql": "select 1"
+ }
+ """)
+ .when()
+ .post("/Execute")
+ .then()
+ .statusCode(400)
+ .contentType("application/json")
+ .header("X-Amzn-Errortype", "BadRequestException")
+ .header("x-amzn-query-error", "BadRequestException;Sender")
+ .body("__type", equalTo("BadRequestException"));
+ }
+
+ @Test
+ void commitUnknownTransactionReturnsTypedJsonError() {
+ given()
+ .contentType("application/json")
+ .body("""
+ {
+ "resourceArn": "arn:aws:rds:us-east-1:000000000000:cluster:missing",
+ "secretArn": "arn:aws:secretsmanager:us-east-1:000000000000:secret:missing",
+ "transactionId": "missing-tx"
+ }
+ """)
+ .when()
+ .post("/CommitTransaction")
+ .then()
+ .statusCode(404)
+ .contentType("application/json")
+ .header("X-Amzn-Errortype", "TransactionNotFoundException")
+ .header("x-amzn-query-error", "TransactionNotFoundException;Sender")
+ .body("__type", equalTo("TransactionNotFoundException"));
+ }
+
+ @Test
+ void batchExecuteReturnsJsonUnsupportedError() {
+ given()
+ .contentType("application/json")
+ .body("{}")
+ .when()
+ .post("/BatchExecute")
+ .then()
+ .statusCode(400)
+ .contentType("application/json")
+ .header("X-Amzn-Errortype", "BadRequestException")
+ .header("x-amzn-query-error", "BadRequestException;Sender")
+ .body("__type", equalTo("BadRequestException"));
+ }
+
+ @Test
+ void executeSqlReturnsJsonUnsupportedError() {
+ given()
+ .contentType("application/json")
+ .body("{}")
+ .when()
+ .post("/ExecuteSql")
+ .then()
+ .statusCode(400)
+ .contentType("application/json")
+ .header("X-Amzn-Errortype", "BadRequestException")
+ .header("x-amzn-query-error", "BadRequestException;Sender")
+ .body("__type", equalTo("BadRequestException"));
+ }
+}
diff --git a/src/test/java/io/github/hectorvent/floci/services/rdsdata/RdsDataFieldMapperTest.java b/src/test/java/io/github/hectorvent/floci/services/rdsdata/RdsDataFieldMapperTest.java
new file mode 100644
index 0000000000..981fcfc505
--- /dev/null
+++ b/src/test/java/io/github/hectorvent/floci/services/rdsdata/RdsDataFieldMapperTest.java
@@ -0,0 +1,78 @@
+package io.github.hectorvent.floci.services.rdsdata;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import org.junit.jupiter.api.Test;
+
+import java.math.BigDecimal;
+import java.sql.Date;
+import java.sql.Time;
+import java.sql.Timestamp;
+import java.sql.Types;
+import java.time.LocalDate;
+import java.time.LocalDateTime;
+import java.time.LocalTime;
+
+import static org.junit.jupiter.api.Assertions.assertArrayEquals;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+class RdsDataFieldMapperTest {
+
+ private final ObjectMapper objectMapper = new ObjectMapper();
+
+ @Test
+ void mapsCoreDataApiFieldVariants() throws Exception {
+ ObjectNode stringField = RdsDataFieldMapper.toField(objectMapper, "hello", Types.VARCHAR);
+ ObjectNode longField = RdsDataFieldMapper.toField(objectMapper, 42L, Types.BIGINT);
+ ObjectNode blobField = RdsDataFieldMapper.toField(objectMapper, new byte[] {1, 2, 3}, Types.BLOB);
+ ObjectNode nullField = RdsDataFieldMapper.toField(objectMapper, null, Types.VARCHAR);
+
+ assertEquals("hello", stringField.get("stringValue").asText());
+ assertEquals(42L, longField.get("longValue").asLong());
+ assertArrayEquals(new byte[] {1, 2, 3}, blobField.get("blobValue").binaryValue());
+ assertTrue(nullField.get("isNull").asBoolean());
+ }
+
+ @Test
+ void mapsBooleanAndDoubleFollowUpVariants() throws Exception {
+ ObjectNode booleanField = RdsDataFieldMapper.toField(objectMapper, true, Types.BOOLEAN);
+ ObjectNode doubleField = RdsDataFieldMapper.toField(objectMapper, 1.5d, Types.DOUBLE);
+
+ assertTrue(booleanField.get("booleanValue").asBoolean());
+ assertEquals(1.5d, doubleField.get("doubleValue").asDouble());
+ }
+
+ @Test
+ void mapsSqlTemporalAndDecimalValues() throws Exception {
+ ObjectNode timestampField = RdsDataFieldMapper.toField(objectMapper,
+ Timestamp.valueOf("2026-06-09 12:34:56.123456789"), Types.TIMESTAMP);
+ ObjectNode localDateTimeField = RdsDataFieldMapper.toField(objectMapper,
+ LocalDateTime.of(2021, 3, 4, 5, 6, 7, 891_000_000), Types.TIMESTAMP);
+ ObjectNode wholeSecondLocalDateTimeField = RdsDataFieldMapper.toField(objectMapper,
+ LocalDateTime.of(2021, 3, 4, 5, 6, 7), Types.TIMESTAMP);
+ ObjectNode dateField = RdsDataFieldMapper.toField(objectMapper, Date.valueOf("2026-06-09"), Types.DATE);
+ ObjectNode localDateField = RdsDataFieldMapper.toField(objectMapper, LocalDate.of(2021, 3, 4), Types.DATE);
+ ObjectNode timeField = RdsDataFieldMapper.toField(objectMapper, Time.valueOf("12:34:56"), Types.TIME);
+ ObjectNode localTimeField = RdsDataFieldMapper.toField(objectMapper, LocalTime.of(5, 6, 7, 891_000_000), Types.TIME);
+ ObjectNode integerDecimalField = RdsDataFieldMapper.toField(objectMapper, new BigDecimal("42"), Types.DECIMAL);
+ ObjectNode fractionalDecimalField = RdsDataFieldMapper.toField(objectMapper, new BigDecimal("42.25"), Types.DECIMAL);
+
+ assertEquals("2026-06-09 12:34:56.123456789", timestampField.get("stringValue").asText());
+ assertEquals("2021-03-04 05:06:07.891", localDateTimeField.get("stringValue").asText());
+ assertEquals("2021-03-04 05:06:07", wholeSecondLocalDateTimeField.get("stringValue").asText());
+ assertEquals("2026-06-09", dateField.get("stringValue").asText());
+ assertEquals("2021-03-04", localDateField.get("stringValue").asText());
+ assertEquals("12:34:56", timeField.get("stringValue").asText());
+ assertEquals("05:06:07.891", localTimeField.get("stringValue").asText());
+ assertEquals("42", integerDecimalField.get("stringValue").asText());
+ assertEquals("42.25", fractionalDecimalField.get("stringValue").asText());
+ }
+
+ @Test
+ void mapsBitNumberToBoolean() throws Exception {
+ ObjectNode bitField = RdsDataFieldMapper.toField(objectMapper, 1, Types.BIT);
+
+ assertTrue(bitField.get("booleanValue").asBoolean());
+ }
+}
diff --git a/src/test/java/io/github/hectorvent/floci/services/rdsdata/RdsDataResourceResolverTest.java b/src/test/java/io/github/hectorvent/floci/services/rdsdata/RdsDataResourceResolverTest.java
new file mode 100644
index 0000000000..b32b4b04af
--- /dev/null
+++ b/src/test/java/io/github/hectorvent/floci/services/rdsdata/RdsDataResourceResolverTest.java
@@ -0,0 +1,57 @@
+package io.github.hectorvent.floci.services.rdsdata;
+
+import io.github.hectorvent.floci.core.common.AwsException;
+import io.github.hectorvent.floci.services.rds.RdsService;
+import io.github.hectorvent.floci.services.rds.model.DatabaseEngine;
+import io.github.hectorvent.floci.services.rds.model.DbCluster;
+import io.github.hectorvent.floci.services.rds.model.DbEndpoint;
+import io.github.hectorvent.floci.services.rds.model.DbInstanceStatus;
+import org.junit.jupiter.api.Test;
+
+import java.time.Instant;
+import java.util.ArrayList;
+import java.util.List;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+class RdsDataResourceResolverTest {
+
+ @Test
+ void resolvesClusterArnToContainerTarget() {
+ RdsService rdsService = mock(RdsService.class);
+ DbCluster cluster = new DbCluster("cluster1", DatabaseEngine.MYSQL, "8.0", "admin", "secret",
+ "app", DbInstanceStatus.AVAILABLE, new DbEndpoint("localhost", 7001),
+ new DbEndpoint("localhost", 7001), false, new ArrayList<>(), null, Instant.now(), 7001);
+ cluster.setDbClusterArn("arn:aws:rds:us-east-1:000000000000:cluster:cluster1");
+ cluster.setContainerHost("127.0.0.1");
+ cluster.setContainerPort(3306);
+ when(rdsService.listDbClusters(null)).thenReturn(List.of(cluster));
+ when(rdsService.listDbInstances(null)).thenReturn(List.of());
+
+ RdsDataResourceResolver.DatabaseTarget target = new RdsDataResourceResolver(rdsService)
+ .resolve("arn:aws:rds:us-east-1:000000000000:cluster:cluster1");
+
+ assertEquals(DatabaseEngine.MYSQL, target.engine());
+ assertEquals("127.0.0.1", target.host());
+ assertEquals(3306, target.port());
+ assertEquals("app", target.databaseName());
+ }
+
+ @Test
+ void missingResourceUsesModeledDataApiBadRequestCode() {
+ RdsService rdsService = mock(RdsService.class);
+ when(rdsService.listDbClusters(null)).thenReturn(List.of());
+ when(rdsService.listDbInstances(null)).thenReturn(List.of());
+ when(rdsService.getDbCluster("missing")).thenThrow(new AwsException("DBClusterNotFoundFault", "missing", 404));
+ when(rdsService.getDbInstance("missing")).thenThrow(new AwsException("DBInstanceNotFound", "missing", 404));
+
+ AwsException error = assertThrows(AwsException.class, () -> new RdsDataResourceResolver(rdsService)
+ .resolve("arn:aws:rds:us-east-1:000000000000:cluster:missing"));
+
+ assertEquals("BadRequestException", error.getErrorCode());
+ assertEquals(400, error.getHttpStatus());
+ }
+}
diff --git a/src/test/java/io/github/hectorvent/floci/services/rdsdata/RdsDataServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/rdsdata/RdsDataServiceTest.java
new file mode 100644
index 0000000000..7f7a94daea
--- /dev/null
+++ b/src/test/java/io/github/hectorvent/floci/services/rdsdata/RdsDataServiceTest.java
@@ -0,0 +1,429 @@
+package io.github.hectorvent.floci.services.rdsdata;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.node.ArrayNode;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import io.github.hectorvent.floci.core.common.AwsException;
+import io.github.hectorvent.floci.services.rds.model.DatabaseEngine;
+import io.github.hectorvent.floci.services.secretsmanager.SecretsManagerService;
+import org.junit.jupiter.api.Test;
+
+import java.lang.reflect.Proxy;
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.SQLException;
+import java.sql.Statement;
+import java.time.Duration;
+import java.util.UUID;
+import java.util.concurrent.atomic.AtomicBoolean;
+
+import static org.junit.jupiter.api.Assertions.assertArrayEquals;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.doThrow;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+class RdsDataServiceTest {
+
+ private static final String RESOURCE_ARN = "arn:aws:rds:us-east-1:000000000000:cluster:test";
+ private static final String FALLBACK_RESOURCE_ARN = "arn:aws:rds:us-west-2:111111111111:cluster:test";
+ private static final String OTHER_RESOURCE_ARN = "arn:aws:rds:us-east-1:000000000000:cluster:other";
+ private static final String UNKNOWN_RESOURCE_ARN = "arn:aws:rds:us-east-1:000000000000:cluster:missing";
+ private static final String SECRET_ARN = "arn:aws:secretsmanager:us-east-1:000000000000:secret:local/rds-data";
+ private static final String REGION = "us-east-1";
+
+ private final ObjectMapper objectMapper = new ObjectMapper();
+
+ @Test
+ void executesSqlAndMapsDataApiResultShape() throws Exception {
+ TestHarness harness = new TestHarness();
+ harness.createTables();
+
+ ObjectNode insert = harness.request("""
+ insert into data_api_items(id, title, score, payload, active, created_at)
+ values ('s1', 'First', 42, X'010203', true, timestamp '2026-06-09 12:34:56.123456789')
+ """);
+ ObjectNode insertResponse = harness.service.executeStatement(insert, REGION);
+ assertEquals(1L, insertResponse.get("numberOfRecordsUpdated").asLong());
+
+ ObjectNode select = harness.request("""
+ select title as title, score as score, payload as payload, null as nothing,
+ active as active, created_at as created_at
+ from data_api_items where id = 's1'
+ """);
+ select.put("includeResultMetadata", true);
+ ObjectNode selectResponse = harness.service.executeStatement(select, REGION);
+
+ ArrayNode metadata = (ArrayNode) selectResponse.get("columnMetadata");
+ assertEquals("title", metadata.get(0).get("name").asText().toLowerCase());
+ assertEquals("score", metadata.get(1).get("name").asText().toLowerCase());
+
+ ArrayNode row = (ArrayNode) selectResponse.get("records").get(0);
+ assertEquals("First", row.get(0).get("stringValue").asText());
+ assertEquals(42L, row.get(1).get("longValue").asLong());
+ assertArrayEquals(new byte[] {1, 2, 3}, row.get(2).get("blobValue").binaryValue());
+ assertTrue(row.get(3).get("isNull").asBoolean());
+ assertTrue(row.get(4).get("booleanValue").asBoolean());
+ assertEquals("2026-06-09 12:34:56.123456789", row.get(5).get("stringValue").asText());
+ assertEquals(0L, selectResponse.get("numberOfRecordsUpdated").asLong());
+ }
+
+ @Test
+ void commitsRollsBackAndRejectsInvalidTransactionRequests() throws Exception {
+ TestHarness harness = new TestHarness();
+ harness.createTables();
+
+ String committedTx = harness.service.beginTransaction(harness.beginRequest(), REGION).get("transactionId").asText();
+ ObjectNode insertCommitted = harness.request("insert into data_api_items(id, title, score) values ('commit', 'Commit', 1)");
+ insertCommitted.put("transactionId", committedTx);
+ harness.service.executeStatement(insertCommitted, REGION);
+ ObjectNode commitResponse = harness.service.commitTransaction(harness.transactionRequest(committedTx));
+ assertEquals("Transaction Committed", commitResponse.get("transactionStatus").asText());
+ assertEquals(1L, harness.countById("commit"));
+
+ String rolledBackTx = harness.service.beginTransaction(harness.beginRequest(), REGION).get("transactionId").asText();
+ ObjectNode insertRolledBack = harness.request("insert into data_api_items(id, title, score) values ('rollback', 'Rollback', 1)");
+ insertRolledBack.put("transactionId", rolledBackTx);
+ harness.service.executeStatement(insertRolledBack, REGION);
+ ObjectNode rollbackResponse = harness.service.rollbackTransaction(harness.transactionRequest(rolledBackTx));
+ assertEquals("Rollback Complete", rollbackResponse.get("transactionStatus").asText());
+ assertEquals(0L, harness.countById("rollback"));
+
+ ObjectNode unknownTxRequest = harness.request("select 1");
+ unknownTxRequest.put("transactionId", "missing");
+ AwsException unknownTx = assertThrows(AwsException.class,
+ () -> harness.service.executeStatement(unknownTxRequest, REGION));
+ assertEquals("TransactionNotFoundException", unknownTx.getErrorCode());
+
+ String mismatchTx = harness.service.beginTransaction(harness.beginRequest(), REGION).get("transactionId").asText();
+ ObjectNode mismatchedResource = harness.request("select 1");
+ mismatchedResource.put("transactionId", mismatchTx);
+ mismatchedResource.put("resourceArn", OTHER_RESOURCE_ARN);
+ AwsException resourceMismatch = assertThrows(AwsException.class,
+ () -> harness.service.executeStatement(mismatchedResource, REGION));
+ assertEquals("TransactionNotFoundException", resourceMismatch.getErrorCode());
+
+ ObjectNode unresolvableResource = harness.request("select 1");
+ unresolvableResource.put("transactionId", mismatchTx);
+ unresolvableResource.put("resourceArn", UNKNOWN_RESOURCE_ARN);
+ AwsException unresolvableResourceMismatch = assertThrows(AwsException.class,
+ () -> harness.service.executeStatement(unresolvableResource, REGION));
+ assertEquals("TransactionNotFoundException", unresolvableResourceMismatch.getErrorCode());
+
+ ObjectNode mismatchedDatabase = harness.request("select 1");
+ mismatchedDatabase.put("transactionId", mismatchTx);
+ mismatchedDatabase.put("database", "other");
+ AwsException databaseMismatch = assertThrows(AwsException.class,
+ () -> harness.service.executeStatement(mismatchedDatabase, REGION));
+ assertEquals("TransactionNotFoundException", databaseMismatch.getErrorCode());
+
+ harness.service.rollbackTransaction(harness.transactionRequest(mismatchTx));
+ }
+
+ @Test
+ void normalizesFallbackResourceArnForTransactionIdentity() throws Exception {
+ TestHarness harness = new TestHarness();
+ harness.createTables();
+
+ String tx = harness.service.beginTransaction(harness.beginRequest(FALLBACK_RESOURCE_ARN), REGION)
+ .get("transactionId").asText();
+ ObjectNode insert = harness.request(FALLBACK_RESOURCE_ARN,
+ "insert into data_api_items(id, title, score) values ('fallback', 'Fallback', 1)");
+ insert.put("transactionId", tx);
+ harness.service.executeStatement(insert, REGION);
+
+ ObjectNode commitResponse = harness.service.commitTransaction(harness.transactionRequest(FALLBACK_RESOURCE_ARN, tx));
+
+ assertEquals("Transaction Committed", commitResponse.get("transactionStatus").asText());
+ assertEquals(1L, harness.countById("fallback"));
+ }
+
+ @Test
+ void commitsWithOriginalTransactionArnAfterResourceLookupFails() throws Exception {
+ TestHarness harness = new TestHarness();
+ harness.createTables();
+
+ String tx = harness.service.beginTransaction(harness.beginRequest(), REGION).get("transactionId").asText();
+ ObjectNode insert = harness.request("""
+ insert into data_api_items(id, title, score)
+ values ('deleted-resource', 'Deleted', 1)
+ """);
+ insert.put("transactionId", tx);
+ harness.service.executeStatement(insert, REGION);
+ doThrow(new AwsException("BadRequestException", "resource is gone", 400))
+ .when(harness.resolver).resolve(RESOURCE_ARN);
+
+ ObjectNode commitResponse = harness.service.commitTransaction(harness.transactionRequest(tx));
+
+ assertEquals("Transaction Committed", commitResponse.get("transactionStatus").asText());
+ doReturn(harness.target).when(harness.resolver).resolve(RESOURCE_ARN);
+ assertEquals(1L, harness.countById("deleted-resource"));
+ }
+
+ @Test
+ void validatesRequiredAwsFieldsForDataApiRequests() throws Exception {
+ TestHarness harness = new TestHarness();
+ harness.createTables();
+
+ ObjectNode executeMissingSecret = harness.request("select 1");
+ executeMissingSecret.remove("secretArn");
+ AwsException missingExecuteSecret = assertThrows(AwsException.class,
+ () -> harness.service.executeStatement(executeMissingSecret, REGION));
+ assertEquals("BadRequestException", missingExecuteSecret.getErrorCode());
+ assertEquals("secretArn is required.", missingExecuteSecret.getMessage());
+
+ ObjectNode beginMissingSecret = harness.beginRequest();
+ beginMissingSecret.remove("secretArn");
+ AwsException missingBeginSecret = assertThrows(AwsException.class,
+ () -> harness.service.beginTransaction(beginMissingSecret, REGION));
+ assertEquals("BadRequestException", missingBeginSecret.getErrorCode());
+ assertEquals("secretArn is required.", missingBeginSecret.getMessage());
+
+ String tx = harness.service.beginTransaction(harness.beginRequest(), REGION).get("transactionId").asText();
+ ObjectNode commitMissingResource = harness.transactionRequest(tx);
+ commitMissingResource.remove("resourceArn");
+ AwsException missingCommitResource = assertThrows(AwsException.class,
+ () -> harness.service.commitTransaction(commitMissingResource));
+ assertEquals("BadRequestException", missingCommitResource.getErrorCode());
+ assertEquals("resourceArn is required.", missingCommitResource.getMessage());
+
+ ObjectNode rollbackMismatchedResource = harness.transactionRequest(tx);
+ rollbackMismatchedResource.put("resourceArn", OTHER_RESOURCE_ARN);
+ AwsException rollbackMismatch = assertThrows(AwsException.class,
+ () -> harness.service.rollbackTransaction(rollbackMismatchedResource));
+ assertEquals("TransactionNotFoundException", rollbackMismatch.getErrorCode());
+
+ harness.service.rollbackTransaction(harness.transactionRequest(tx));
+ }
+
+ @Test
+ void rejectsUnsupportedExecuteOptions() throws Exception {
+ TestHarness harness = new TestHarness();
+ harness.createTables();
+
+ ObjectNode formattedRecords = harness.request("select 1");
+ formattedRecords.put("formatRecordsAs", "JSON");
+ AwsException formattedRecordsError = assertThrows(AwsException.class,
+ () -> harness.service.executeStatement(formattedRecords, REGION));
+ assertEquals("BadRequestException", formattedRecordsError.getErrorCode());
+
+ ObjectNode malformedParameters = harness.request("select 1");
+ malformedParameters.set("parameters", objectMapper.createObjectNode());
+ AwsException malformedParametersError = assertThrows(AwsException.class,
+ () -> harness.service.executeStatement(malformedParameters, REGION));
+ assertEquals("BadRequestException", malformedParametersError.getErrorCode());
+
+ ObjectNode resultSetOptions = harness.request("select 1");
+ ObjectNode options = objectMapper.createObjectNode();
+ options.put("decimalReturnType", "STRING");
+ resultSetOptions.set("resultSetOptions", options);
+ AwsException resultSetOptionsError = assertThrows(AwsException.class,
+ () -> harness.service.executeStatement(resultSetOptions, REGION));
+ assertEquals("BadRequestException", resultSetOptionsError.getErrorCode());
+ }
+
+ @Test
+ void rollsBackExpiredTransactionsDuringCleanup() throws Exception {
+ TestHarness harness = new TestHarness(Duration.ofMillis(50));
+ harness.createTables();
+
+ String tx = harness.service.beginTransaction(harness.beginRequest(), REGION).get("transactionId").asText();
+ ObjectNode insert = harness.request("insert into data_api_items(id, title, score) values ('expired', 'Expired', 1)");
+ insert.put("transactionId", tx);
+ harness.service.executeStatement(insert, REGION);
+ Thread.sleep(500);
+
+ ObjectNode nextTxRequest = harness.request("select 1");
+ nextTxRequest.put("transactionId", tx);
+ AwsException expired = assertThrows(AwsException.class,
+ () -> harness.service.executeStatement(nextTxRequest, REGION));
+
+ assertEquals("TransactionNotFoundException", expired.getErrorCode());
+ assertEquals(0L, harness.countById("expired"));
+ }
+
+ @Test
+ void shutdownRollsBackOpenTransactions() throws Exception {
+ TestHarness harness = new TestHarness();
+ harness.createTables();
+
+ String tx = harness.service.beginTransaction(harness.beginRequest(), REGION).get("transactionId").asText();
+ ObjectNode insert = harness.request("insert into data_api_items(id, title, score) values ('shutdown', 'Shutdown', 1)");
+ insert.put("transactionId", tx);
+ harness.service.executeStatement(insert, REGION);
+
+ harness.service.shutdown();
+
+ assertEquals(0L, harness.countById("shutdown"));
+ }
+
+ @Test
+ void closesConnectionWhenTransactionSetupFails() {
+ RdsDataResourceResolver resolver = mock(RdsDataResourceResolver.class);
+ SecretsManagerService secrets = fallbackSecrets();
+ RdsDataResourceResolver.DatabaseTarget target = target();
+ when(resolver.resolve(RESOURCE_ARN)).thenReturn(target);
+ AtomicBoolean closed = new AtomicBoolean(false);
+ RdsDataConnectionFactory failingFactory = new RdsDataConnectionFactory() {
+ @Override
+ Connection open(RdsDataResourceResolver.DatabaseTarget target,
+ String username,
+ String password,
+ String database) {
+ return throwingSetAutoCommitConnection(closed);
+ }
+ };
+ RdsDataService service = new RdsDataService(resolver, secrets, objectMapper, failingFactory, Duration.ofSeconds(60));
+
+ AwsException error = assertThrows(AwsException.class, () -> service.beginTransaction(beginRequest(), REGION));
+
+ assertEquals("DatabaseErrorException", error.getErrorCode());
+ assertTrue(closed.get());
+ }
+
+ private ObjectNode beginRequest() {
+ ObjectNode request = objectMapper.createObjectNode();
+ request.put("resourceArn", RESOURCE_ARN);
+ request.put("secretArn", SECRET_ARN);
+ request.put("database", "app");
+ return request;
+ }
+
+ private static SecretsManagerService fallbackSecrets() {
+ SecretsManagerService secrets = mock(SecretsManagerService.class);
+ when(secrets.getSecretValue(any(), any(), any(), any()))
+ .thenThrow(new AwsException("ResourceNotFoundException",
+ "Secrets Manager can't find the specified secret.", 400));
+ return secrets;
+ }
+
+ private static RdsDataResourceResolver.DatabaseTarget target() {
+ return target(RESOURCE_ARN);
+ }
+
+ private static RdsDataResourceResolver.DatabaseTarget target(String resourceArn) {
+ return new RdsDataResourceResolver.DatabaseTarget(resourceArn, DatabaseEngine.MYSQL,
+ "127.0.0.1", 3306, "sa", "", "app");
+ }
+
+ private static Connection throwingSetAutoCommitConnection(AtomicBoolean closed) {
+ return (Connection) Proxy.newProxyInstance(Connection.class.getClassLoader(),
+ new Class<?>[] {Connection.class},
+ (proxy, method, args) -> {
+ if ("setAutoCommit".equals(method.getName())) {
+ throw new SQLException("setAutoCommit failed");
+ }
+ if ("close".equals(method.getName())) {
+ closed.set(true);
+ return null;
+ }
+ if ("isClosed".equals(method.getName())) {
+ return closed.get();
+ }
+ Class<?> returnType = method.getReturnType();
+ if (returnType == boolean.class) {
+ return false;
+ }
+ if (returnType == int.class) {
+ return 0;
+ }
+ if (returnType == long.class) {
+ return 0L;
+ }
+ return null;
+ });
+ }
+
+ private final class TestHarness {
+ private final String jdbcUrl = "jdbc:h2:mem:rdsdata_" + UUID.randomUUID()
+ + ";MODE=MySQL;DATABASE_TO_LOWER=TRUE;DB_CLOSE_DELAY=-1";
+ private final RdsDataResourceResolver resolver;
+ private final RdsDataResourceResolver.DatabaseTarget target;
+ private final RdsDataService service;
+
+ private TestHarness() {
+ this(Duration.ofSeconds(60));
+ }
+
+ private TestHarness(Duration transactionTtl) {
+ resolver = mock(RdsDataResourceResolver.class);
+ SecretsManagerService secrets = fallbackSecrets();
+ target = target();
+ when(resolver.resolve(RESOURCE_ARN)).thenReturn(target);
+ when(resolver.resolve(FALLBACK_RESOURCE_ARN)).thenReturn(target);
+ when(resolver.resolve(OTHER_RESOURCE_ARN)).thenReturn(target(OTHER_RESOURCE_ARN));
+ when(resolver.resolve(UNKNOWN_RESOURCE_ARN))
+ .thenThrow(new AwsException("BadRequestException", "resource is missing", 400));
+ RdsDataConnectionFactory connectionFactory = new RdsDataConnectionFactory() {
+ @Override
+ Connection open(RdsDataResourceResolver.DatabaseTarget target,
+ String username,
+ String password,
+ String database) throws SQLException {
+ return DriverManager.getConnection(jdbcUrl, "sa", "");
+ }
+ };
+ service = new RdsDataService(resolver, secrets, objectMapper, connectionFactory, transactionTtl);
+ }
+
+ private void createTables() throws SQLException {
+ try (Connection connection = DriverManager.getConnection(jdbcUrl, "sa", "");
+ Statement statement = connection.createStatement()) {
+ statement.execute("""
+ create table data_api_items(
+ id varchar(64) primary key,
+ title varchar(255),
+ score bigint,
+ payload blob,
+ active boolean,
+ created_at timestamp(9)
+ )
+ """);
+ }
+ }
+
+ private ObjectNode request(String sql) {
+ return request(RESOURCE_ARN, sql);
+ }
+
+ private ObjectNode request(String resourceArn, String sql) {
+ ObjectNode request = beginRequest(resourceArn);
+ request.put("sql", sql);
+ return request;
+ }
+
+ private ObjectNode beginRequest() {
+ return beginRequest(RESOURCE_ARN);
+ }
+
+ private ObjectNode beginRequest(String resourceArn) {
+ ObjectNode request = objectMapper.createObjectNode();
+ request.put("resourceArn", resourceArn);
+ request.put("secretArn", SECRET_ARN);
+ request.put("database", "app");
+ return request;
+ }
+
+ private ObjectNode transactionRequest(String transactionId) {
+ return transactionRequest(RESOURCE_ARN, transactionId);
+ }
+
+ private ObjectNode transactionRequest(String resourceArn, String transactionId) {
+ ObjectNode request = objectMapper.createObjectNode();
+ request.put("resourceArn", resourceArn);
+ request.put("secretArn", SECRET_ARN);
+ request.put("transactionId", transactionId);
+ return request;
+ }
+
+ private long countById(String id) {
+ ObjectNode request = request("select count(*) as count from data_api_items where id = '" + id + "'");
+ ObjectNode response = service.executeStatement(request, REGION);
+ return response.get("records").get(0).get(0).get("longValue").asLong();
+ }
+ }
+}
diff --git a/src/test/resources/application.yml b/src/test/resources/application.yml
index 735f8e1a61..1b8dedaabe 100644
--- a/src/test/resources/application.yml
+++ b/src/test/resources/application.yml
@@ -100,6 +100,9 @@ floci:
default-postgres-image: "postgres:16-alpine"
default-mysql-image: "mysql:8.0"
default-mariadb-image: "mariadb:11"
+ rds-data:
+ enabled: true
+ transaction-ttl-seconds: 180
neptune:
enabled: true
proxy-base-port: 8182