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: expose AWS credentials for DynamoDB instances and assume role in the driver
Vincent Huangd8bccd0d16b3
diff --git a/backend/plugin/db/dynamodb/dynamodb.go b/backend/plugin/db/dynamodb/dynamodb.goindex 49a0e57885..6b4852caf3 100644--- a/backend/plugin/db/dynamodb/dynamodb.go+++ b/backend/plugin/db/dynamodb/dynamodb.go@@ -54,6 +54,9 @@ func (d *Driver) Open(ctx context.Context, _ storepb.Engine, conf db.ConnectionCif err != nil {return nil, errors.Wrapf(err, "failed to load AWS config")}+ if err := util.AssumeRoleIfNeeded(ctx, &cfg, conf.ConnectionContext, conf.DataSource.GetAwsCredential()); err != nil {+ return nil, err+ }d.awsConfig = cfgvar optFns []func(*dynamodb.Options)diff --git a/backend/plugin/db/dynamodb/dynamodb_test.go b/backend/plugin/db/dynamodb/dynamodb_test.goindex 09bbf410f3..07bb259062 100644--- a/backend/plugin/db/dynamodb/dynamodb_test.go+++ b/backend/plugin/db/dynamodb/dynamodb_test.go@@ -1,9 +1,12 @@package dynamodbimport (+ "context"+ "strings""testing"storepb "github.com/bytebase/bytebase/backend/generated-go/store"+ "github.com/bytebase/bytebase/backend/plugin/db")func TestDynamoDBEndpoint(t *testing.T) {@@ -56,3 +59,56 @@ func TestDynamoDBEndpoint(t *testing.T) {})}}++// setHermeticAWSEnv pins base credentials and points STS at an unroutable local+// endpoint so Open never reaches real AWS: an attempted AssumeRole fails fast+// and deterministically instead of depending on ambient credentials or network.+func setHermeticAWSEnv(t *testing.T) {+ t.Setenv("AWS_ACCESS_KEY_ID", "AKIAIOSFODNN7EXAMPLE")+ t.Setenv("AWS_SECRET_ACCESS_KEY", "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY")+ t.Setenv("AWS_SESSION_TOKEN", "")+ t.Setenv("AWS_ENDPOINT_URL_STS", "http://127.0.0.1:1")+}++func TestOpenAssumesRoleWhenRoleArnSet(t *testing.T) {+ setHermeticAWSEnv(t)++ d := newDriver()+ _, err := d.Open(context.Background(), storepb.Engine_DYNAMODB, db.ConnectionConfig{+ ConnectionContext: db.ConnectionContext{InstanceID: "dynamodb-test"},+ DataSource: &storepb.DataSource{+ Region: "us-east-1",+ IamExtension: &storepb.DataSource_AwsCredential{+ AwsCredential: &storepb.DataSource_AWSCredential{+ RoleArn: "arn:aws:iam::123456789012:role/bytebase-test-role",+ },+ },+ },+ })+ if err == nil {+ t.Fatal("Open() = nil error; want an assume-role failure, meaning role_arn was ignored")+ }+ if !strings.Contains(err.Error(), "failed to assume role") {+ t.Errorf("Open() error = %q, want it to mention the assume-role attempt", err.Error())+ }+}++func TestOpenWithoutRoleArnSkipsAssumeRole(t *testing.T) {+ setHermeticAWSEnv(t)++ d := newDriver()+ if _, err := d.Open(context.Background(), storepb.Engine_DYNAMODB, db.ConnectionConfig{+ ConnectionContext: db.ConnectionContext{InstanceID: "dynamodb-test"},+ DataSource: &storepb.DataSource{+ Region: "us-east-1",+ IamExtension: &storepb.DataSource_AwsCredential{+ AwsCredential: &storepb.DataSource_AWSCredential{+ AccessKeyId: "AKIAIOSFODNN7EXAMPLE",+ SecretAccessKey: "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY",+ },+ },+ },+ }); err != nil {+ t.Fatalf("Open() error = %v, want nil when no role_arn is set", err)+ }+}diff --git a/frontend/src/components/instance/DataSourceForm.tsx b/frontend/src/components/instance/DataSourceForm.tsxindex e37dc54e4a..7cc0cc64a1 100644--- a/frontend/src/components/instance/DataSourceForm.tsx+++ b/frontend/src/components/instance/DataSourceForm.tsx@@ -272,6 +272,27 @@ export function DataSourceForm({}}, [basicInfo.engine, t]);+ // DynamoDB always authenticates with AWS IAM, but data sources created+ // before the form exposed credentials still carry PASSWORD. Render those in+ // the AWS IAM mode and stamp the real authentication type only when a+ // credential field is edited, so opening an old instance stays pristine.+ const dynamoDBDataSource = useMemo(+ () => ({+ ...dataSource,+ authenticationType: DataSource_AuthenticationType.AWS_RDS_IAM,+ }),+ [dataSource]+ );+ const updateDynamoDB = useCallback(+ (updates: Partial<EditDataSource>) => {+ update({+ authenticationType: DataSource_AuthenticationType.AWS_RDS_IAM,+ ...updates,+ });+ },+ [update]+ );+const extraConnectionParamsList = useMemo(() => {const params = dataSource.extraConnectionParameters || {};return Object.entries(params).map(([key, value]) => ({ key, value }));@@ -874,23 +895,12 @@ export function DataSourceForm({{/* AWS Region */}{isAwsIAM && (- <FormField- className="sm:col-span-3 sm:col-start-1"- title={- <>- {t("instance.database-region")}{" "}- <span className="text-error">*</span>- </>- }- >- <Input- value={dataSource.region ?? ""}- className="w-full"- disabled={!allowEdit}- placeholder="database region, for example, us-east-1"- onChange={(e) => update({ region: e.target.value })}- />- </FormField>+ <AwsRegionField+ region={dataSource.region ?? ""}+ required+ allowEdit={allowEdit}+ onChange={(region) => update({ region })}+ />)}{/* Password / External Secret */}@@ -1673,6 +1683,24 @@ export function DataSourceForm({</>)}+ {/* DynamoDB AWS IAM credentials */}+ {basicInfo.engine === Engine.DYNAMODB && (+ <>+ <CredentialSourceForm+ dataSource={dynamoDBDataSource}+ engine={basicInfo.engine}+ allowEdit={allowEdit}+ onDataSourceChange={updateDynamoDB}+ />+ <AwsRegionField+ region={dataSource.region ?? ""}+ required={dataSource.iamExtension?.case === "awsCredential"}+ allowEdit={allowEdit}+ onChange={(region) => updateDynamoDB({ region })}+ />+ </>+ )}+{/* Oracle SID/Service Name */}{basicInfo.engine === Engine.ORACLE && (<OracleSIDServiceNameInput@@ -2146,3 +2174,36 @@ function OracleSIDServiceNameInput({</div>);}++function AwsRegionField({+ region,+ required,+ allowEdit,+ onChange,+}: Readonly<{+ region: string;+ required: boolean;+ allowEdit: boolean;+ onChange: (region: string) => void;+}>) {+ const { t } = useTranslation();+ return (+ <FormField+ className="sm:col-span-3 sm:col-start-1"+ title={+ <>+ {t("instance.database-region")}+ {required && <span className="text-error"> *</span>}+ </>+ }+ >+ <Input+ value={region}+ className="w-full"+ disabled={!allowEdit}+ placeholder="database region, for example, us-east-1"+ onChange={(e) => onChange(e.target.value)}+ />+ </FormField>+ );+}diff --git a/frontend/src/components/instance/DataSourceSection.tsx b/frontend/src/components/instance/DataSourceSection.tsxindex f288c20810..ff2b744c54 100644--- a/frontend/src/components/instance/DataSourceSection.tsx+++ b/frontend/src/components/instance/DataSourceSection.tsx@@ -106,8 +106,12 @@ export function DataSourceSection({[setDataSourceEditState]);+ // DynamoDB has a single admin data source: no read-only tabs or tips.+ const showDataSourceTabs =+ !isCreating && basicInfo.engine !== Engine.DYNAMODB;+// Show RO tips when not creating and no RO data source- const showROTips = !isCreating && !hasReadOnlyDataSource;+ const showROTips = showDataSourceTabs && !hasReadOnlyDataSource;return (<>@@ -132,7 +136,7 @@ export function DataSourceSection({<div className="mt-2 gap-y-2 gap-x-4 border-none">{/* Data source tabs */}- {!isCreating && (+ {showDataSourceTabs && (<div className="mb-4 flex items-center gap-x-2 border-b border-block-border"><buttontype="button"diff --git a/frontend/src/components/instance/InstanceFormBody.tsx b/frontend/src/components/instance/InstanceFormBody.tsxindex 283f43af5e..4d9b1066be 100644--- a/frontend/src/components/instance/InstanceFormBody.tsx+++ b/frontend/src/components/instance/InstanceFormBody.tsx@@ -950,8 +950,18 @@ export function InstanceFormBody({ onOpenInfoPanel }: InstanceFormBodyProps) {if (ds.type !== DataSourceType.ADMIN) return ds;const updated = { ...ds };switch (engine) {- case Engine.SNOWFLAKE:+ case Engine.SNOWFLAKE: {+ if (+ updated.host === "127.0.0.1" ||+ updated.host === "host.docker.internal"+ ) {+ updated.host = "";+ }+ break;+ }case Engine.DYNAMODB: {+ updated.authenticationType =+ DataSource_AuthenticationType.AWS_RDS_IAM;if (updated.host === "127.0.0.1" ||updated.host === "host.docker.internal"@@ -1736,13 +1746,10 @@ export function InstanceFormBody({ onOpenInfoPanel }: InstanceFormBodyProps) {</div>{/* Credentials (auth method, username, password) */}+ <DataSourceSection hideOptions onOpenInfoPanel={onOpenInfoPanel} />+{basicInfo.engine !== Engine.DYNAMODB && (<>- <DataSourceSection- hideOptions- onOpenInfoPanel={onOpenInfoPanel}- />-<div className="mt-6"><SyncDatabasesisCreating={isCreating}diff --git a/frontend/src/components/instance/InstanceFormContext.tsx b/frontend/src/components/instance/InstanceFormContext.tsxindex 92a68f7c55..853d22920f 100644--- a/frontend/src/components/instance/InstanceFormContext.tsx+++ b/frontend/src/components/instance/InstanceFormContext.tsx@@ -290,6 +290,12 @@ export function InstanceFormProvider({if (ds.authenticationType === DataSource_AuthenticationType.AWS_RDS_IAM) {+ // DynamoDB can run on the deployment's default credential chain,+ // where the region may also come from the environment. A specific+ // credential must pin its region.+ if (basicInfo.engine === Engine.DYNAMODB) {+ return ds.iamExtension?.case !== "awsCredential" || !!ds.region;+ }return !!ds.region;}if (basicInfo.engine === Engine.ORACLE) {