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: reject empty IAM credentials in SaaS mode
Vincent Huang3fd36e27a25b
diff --git a/backend/api/v1/instance_service.go b/backend/api/v1/instance_service.goindex 634892fcae..1fae047270 100644--- a/backend/api/v1/instance_service.go+++ b/backend/api/v1/instance_service.go@@ -711,6 +711,39 @@ func (s *InstanceService) validateIAMCredentialForSaaS(dataSource *storepb.DataS)}+ // An IAM extension whose fields are all empty behaves exactly like the+ // default credential chain at connect time (the host's own cloud+ // identity), so reject it for the same reason.+ return validateIAMCredentialNotEmpty(dataSource)+}++func validateIAMCredentialNotEmpty(dataSource *storepb.DataSource) error {+ switch {+ case dataSource.GetAwsCredential() != nil:+ awsCredential := dataSource.GetAwsCredential()+ if awsCredential.GetAccessKeyId() == "" && awsCredential.GetRoleArn() == "" {+ return connect.NewError(+ connect.CodeInvalidArgument,+ errors.New("AWS credential requires an access key ID or a role ARN in SaaS mode"),+ )+ }+ case dataSource.GetGcpCredential() != nil:+ if dataSource.GetGcpCredential().GetContent() == "" {+ return connect.NewError(+ connect.CodeInvalidArgument,+ errors.New("GCP credential content is required in SaaS mode"),+ )+ }+ case dataSource.GetAzureCredential() != nil:+ azureCredential := dataSource.GetAzureCredential()+ if azureCredential.GetTenantId() == "" || azureCredential.GetClientId() == "" || azureCredential.GetClientSecret() == "" {+ return connect.NewError(+ connect.CodeInvalidArgument,+ errors.New("Azure credential requires tenant ID, client ID, and client secret in SaaS mode"),+ )+ }+ default:+ }return nil}@@ -1348,6 +1381,12 @@ func (s *InstanceService) UpdateDataSource(ctx context.Context, req *connect.Reqif err := validateAndSanitizeDataSourceTLS(dataSource); err != nil {return nil, connect.NewError(connect.CodeInvalidArgument, err)}+ // Validate the merged result so untouched stored credentials keep passing:+ // the create-path SaaS checks never run for updates, which would otherwise+ // let an update strip an IAM credential down to the default chain.+ if err := s.validateIAMCredentialForSaaS(dataSource); err != nil {+ return nil, err+ }if err := s.checkInstanceDataSources(ctx, instance, metadata.GetDataSources()); err != nil {return nil, errdiff --git a/backend/api/v1/instance_service_test.go b/backend/api/v1/instance_service_test.goindex 319328fd56..00c0bb30b5 100644--- a/backend/api/v1/instance_service_test.go+++ b/backend/api/v1/instance_service_test.go@@ -15,6 +15,114 @@ import ("github.com/bytebase/bytebase/backend/store")+func TestValidateIAMCredentialForSaaS(t *testing.T) {+ saas := &InstanceService{profile: &config.Profile{SaaS: true}}+ selfHosted := &InstanceService{profile: &config.Profile{SaaS: false}}++ awsDS := func(credential *storepb.DataSource_AWSCredential) *storepb.DataSource {+ ds := &storepb.DataSource{AuthenticationType: storepb.DataSource_AWS_RDS_IAM}+ if credential != nil {+ ds.IamExtension = &storepb.DataSource_AwsCredential{AwsCredential: credential}+ }+ return ds+ }++ testCases := []struct {+ name string+ service *InstanceService+ ds *storepb.DataSource+ wantErr bool+ }{+ {+ name: "saas rejects nil iam extension",+ service: saas,+ ds: awsDS(nil),+ wantErr: true,+ },+ {+ // An all-empty credential behaves exactly like the default chain+ // at connect time: the host's own AWS identity.+ name: "saas rejects empty aws credential",+ service: saas,+ ds: awsDS(&storepb.DataSource_AWSCredential{}),+ wantErr: true,+ },+ {+ name: "saas accepts aws access key",+ service: saas,+ ds: awsDS(&storepb.DataSource_AWSCredential{AccessKeyId: "AKIAIOSFODNN7EXAMPLE", SecretAccessKey: "secret"}),+ wantErr: false,+ },+ {+ // Role-only is the legitimate SaaS cross-account pattern: the+ // tenant's role trusts the host identity as the base principal.+ name: "saas accepts aws role arn without keys",+ service: saas,+ ds: awsDS(&storepb.DataSource_AWSCredential{RoleArn: "arn:aws:iam::123456789012:role/tenant"}),+ wantErr: false,+ },+ {+ name: "saas rejects empty gcp credential",+ service: saas,+ ds: &storepb.DataSource{+ AuthenticationType: storepb.DataSource_GOOGLE_CLOUD_SQL_IAM,+ IamExtension: &storepb.DataSource_GcpCredential{GcpCredential: &storepb.DataSource_GCPCredential{}},+ },+ wantErr: true,+ },+ {+ name: "saas accepts gcp credential content",+ service: saas,+ ds: &storepb.DataSource{+ AuthenticationType: storepb.DataSource_GOOGLE_CLOUD_SQL_IAM,+ IamExtension: &storepb.DataSource_GcpCredential{GcpCredential: &storepb.DataSource_GCPCredential{Content: `{"type":"service_account"}`}},+ },+ wantErr: false,+ },+ {+ name: "saas rejects incomplete azure credential",+ service: saas,+ ds: &storepb.DataSource{+ AuthenticationType: storepb.DataSource_AZURE_IAM,+ IamExtension: &storepb.DataSource_AzureCredential_{AzureCredential: &storepb.DataSource_AzureCredential{TenantId: "tenant"}},+ },+ wantErr: true,+ },+ {+ name: "saas accepts complete azure credential",+ service: saas,+ ds: &storepb.DataSource{+ AuthenticationType: storepb.DataSource_AZURE_IAM,+ IamExtension: &storepb.DataSource_AzureCredential_{AzureCredential: &storepb.DataSource_AzureCredential{TenantId: "tenant", ClientId: "client", ClientSecret: "secret"}},+ },+ wantErr: false,+ },+ {+ name: "saas ignores password authentication",+ service: saas,+ ds: &storepb.DataSource{AuthenticationType: storepb.DataSource_PASSWORD},+ wantErr: false,+ },+ {+ // Self-hosted deployments may use the default credential chain.+ name: "self-hosted accepts empty aws credential",+ service: selfHosted,+ ds: awsDS(&storepb.DataSource_AWSCredential{}),+ wantErr: false,+ },+ }+ for _, tc := range testCases {+ t.Run(tc.name, func(t *testing.T) {+ err := tc.service.validateIAMCredentialForSaaS(tc.ds)+ if tc.wantErr {+ require.Error(t, err)+ } else {+ require.NoError(t, err)+ }+ })+ }+}+func TestValidateExtraConnectionParametersRejectsTiDBAllowAllFiles(t *testing.T) {err := validateExtraConnectionParameters(storepb.Engine_TIDB, map[string]string{"allowAllFiles": "true",