Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
fix(dynamodb): honor configured endpoint instead of silently using real AWS (#20551)
h3n4l61a15391b372
diff --git a/backend/plugin/db/dynamodb/dynamodb.go b/backend/plugin/db/dynamodb/dynamodb.goindex 87bc1c01a0..49a0e57885 100644--- a/backend/plugin/db/dynamodb/dynamodb.go+++ b/backend/plugin/db/dynamodb/dynamodb.go@@ -5,7 +5,9 @@ import ("context""database/sql""encoding/json"+ "fmt""slices"+ "strings""time""github.com/aws/aws-sdk-go-v2/aws"@@ -53,11 +55,50 @@ func (d *Driver) Open(ctx context.Context, _ storepb.Engine, conf db.ConnectionCreturn nil, errors.Wrapf(err, "failed to load AWS config")}d.awsConfig = cfg- client := dynamodb.NewFromConfig(cfg)- d.client = client++ var optFns []func(*dynamodb.Options)+ // Honor a custom endpoint (e.g. DynamoDB Local) when the data source+ // specifies a host. Without this the AWS SDK resolves the real AWS endpoint+ // for the region and the configured host:port is silently ignored, routing+ // queries intended for a local/compatible endpoint to real AWS.+ if endpoint := dynamoDBEndpoint(conf.DataSource); endpoint != "" {+ optFns = append(optFns, func(o *dynamodb.Options) {+ o.BaseEndpoint = aws.String(endpoint)+ })+ }+ d.client = dynamodb.NewFromConfig(cfg, optFns...)return d, nil}+// dynamoDBEndpoint returns a custom DynamoDB endpoint URL derived from the data+// source host/port, or "" to fall back to the AWS SDK's default (real AWS)+// endpoint resolution. A custom endpoint lets bytebase target DynamoDB Local —+// or any DynamoDB-compatible service — instead of real AWS.+func dynamoDBEndpoint(ds *storepb.DataSource) string {+ host := ds.GetHost()+ if host == "" {+ // No host configured: use the AWS SDK's default endpoint for the region.+ return ""+ }+ port := ds.GetPort()+ // If the host already carries a scheme, treat it as a full base URL and+ // only append the port when one isn't already present.+ if strings.Contains(host, "://") {+ if port != "" && !strings.Contains(host[strings.Index(host, "://")+len("://"):], ":") {+ return host + ":" + port+ }+ return host+ }+ scheme := "http"+ if ds.GetUseSsl() {+ scheme = "https"+ }+ if port != "" {+ return fmt.Sprintf("%s://%s:%s", scheme, host, port)+ }+ return fmt.Sprintf("%s://%s", scheme, host)+}+// Close closes the driver.func (*Driver) Close(_ context.Context) error {return nildiff --git a/backend/plugin/db/dynamodb/dynamodb_test.go b/backend/plugin/db/dynamodb/dynamodb_test.gonew file mode 100644index 0000000000..09bbf410f3--- /dev/null+++ b/backend/plugin/db/dynamodb/dynamodb_test.go@@ -0,0 +1,58 @@+package dynamodb++import (+ "testing"++ storepb "github.com/bytebase/bytebase/backend/generated-go/store"+)++func TestDynamoDBEndpoint(t *testing.T) {+ testCases := []struct {+ name string+ ds *storepb.DataSource+ want string+ }{+ {+ // No host: fall back to the AWS SDK default (real AWS) endpoint.+ name: "no_host_uses_default_aws",+ ds: &storepb.DataSource{Region: "ap-northeast-1"},+ want: "",+ },+ {+ // DynamoDB Local: host + port, no TLS -> http endpoint.+ name: "local_http",+ ds: &storepb.DataSource{Host: "localhost", Port: "18000"},+ want: "http://localhost:18000",+ },+ {+ name: "host_only_no_port",+ ds: &storepb.DataSource{Host: "127.0.0.1"},+ want: "http://127.0.0.1",+ },+ {+ // useSsl toggles the scheme to https.+ name: "https_via_use_ssl",+ ds: &storepb.DataSource{Host: "dynamo.internal", Port: "8000", UseSsl: true},+ want: "https://dynamo.internal:8000",+ },+ {+ // Explicit scheme in host is respected; port appended once.+ name: "scheme_prefixed_host_appends_port",+ ds: &storepb.DataSource{Host: "http://localhost", Port: "18000"},+ want: "http://localhost:18000",+ },+ {+ // Explicit scheme + port already in host: used verbatim.+ name: "scheme_prefixed_full_url",+ ds: &storepb.DataSource{Host: "https://dynamodb.example.com:443"},+ want: "https://dynamodb.example.com:443",+ },+ }+ for _, tc := range testCases {+ t.Run(tc.name, func(t *testing.T) {+ if got := dynamoDBEndpoint(tc.ds); got != tc.want {+ t.Errorf("dynamoDBEndpoint() = %q, want %q", got, tc.want)+ }+ })+ }+}