Skip to content

Commit

Permalink
Move static database validation to only be executed on service start (#…
Browse files Browse the repository at this point in the history
…42900)

* refactor: remove ValidateDatabase from servicecfg

* test: update database configuration tests

* test(config): update load database config

* chore: code review suggestions
  • Loading branch information
gabrielcorado authored Jun 13, 2024
1 parent e33400a commit 5af7106
Show file tree
Hide file tree
Showing 10 changed files with 200 additions and 437 deletions.
7 changes: 7 additions & 0 deletions api/types/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -922,6 +922,13 @@ func (d *DatabaseV3) CheckAndSetDefaults() error {
}
}

const defaultKRB5FilePath = "/etc/krb5.conf"
// The presence of AD Domain indicates the AD configuration will be used.
// In those cases, set the default KRB5 file location if not present.
if d.Spec.AD.Domain != "" && d.Spec.AD.Krb5File == "" {
d.Spec.AD.Krb5File = defaultKRB5FilePath
}

return nil
}

Expand Down
56 changes: 56 additions & 0 deletions api/types/database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1064,3 +1064,59 @@ func TestDatabaseSpanner(t *testing.T) {
})
}
}

func TestDatabaseGCPCloudSQL(t *testing.T) {
t.Parallel()

for _, test := range []struct {
inputName string
inputSpec DatabaseSpecV3
expectError bool
}{
{
inputName: "gcp-valid-configuration",
inputSpec: DatabaseSpecV3{
Protocol: DatabaseProtocolPostgreSQL,
URI: "localhost:5432",
GCP: GCPCloudSQL{
ProjectID: "project-1",
InstanceID: "instance-1",
},
},
expectError: false,
},
{
inputName: "gcp-project-id-specified-without-instance-id",
inputSpec: DatabaseSpecV3{
Protocol: DatabaseProtocolPostgreSQL,
URI: "localhost:5432",
GCP: GCPCloudSQL{
ProjectID: "project-1",
},
},
expectError: true,
},
{
inputName: "gcp-instance-id-specified-without-project-id",
inputSpec: DatabaseSpecV3{
Protocol: DatabaseProtocolPostgreSQL,
URI: "localhost:5432",
GCP: GCPCloudSQL{
InstanceID: "instance-1",
},
},
expectError: true,
},
} {
t.Run(test.inputName, func(t *testing.T) {
_, err := NewDatabaseV3(Metadata{
Name: test.inputName,
}, test.inputSpec)
if test.expectError {
require.Error(t, err)
} else {
require.NoError(t, err)
}
})
}
}
76 changes: 5 additions & 71 deletions lib/config/configuration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2576,11 +2576,12 @@ func TestAppsCLF(t *testing.T) {
}
}

// TestDatabaseConfig ensures reading database the configuration won't return
// error.
func TestDatabaseConfig(t *testing.T) {
tests := []struct {
inConfigString string
desc string
outError string
}{
{
desc: "valid database config",
Expand Down Expand Up @@ -2612,7 +2613,6 @@ db_service:
command: ["uname", "-p"]
period: 1h
`,
outError: "",
},
{
desc: "missing database name",
Expand All @@ -2623,7 +2623,6 @@ db_service:
- protocol: postgres
uri: localhost:5432
`,
outError: "empty database name",
},
{
desc: "unsupported database protocol",
Expand All @@ -2635,7 +2634,6 @@ db_service:
protocol: unknown
uri: localhost:5432
`,
outError: `unsupported database "foo" protocol`,
},
{
desc: "missing database uri",
Expand All @@ -2646,7 +2644,6 @@ db_service:
- name: foo
protocol: postgres
`,
outError: `database "foo" URI is empty`,
},
{
desc: "invalid database uri (missing port)",
Expand All @@ -2658,7 +2655,6 @@ db_service:
protocol: postgres
uri: 192.168.1.1
`,
outError: `invalid database "foo" address`,
},
}
for _, tt := range tests {
Expand All @@ -2667,12 +2663,7 @@ db_service:
ConfigString: base64.StdEncoding.EncodeToString([]byte(tt.inConfigString)),
}
err := Configure(&clf, servicecfg.MakeDefaultConfig(), false)
if tt.outError != "" {
require.Error(t, err)
require.Contains(t, err.Error(), tt.outError)
} else {
require.NoError(t, err)
}
require.NoError(t, err)
})
}
}
Expand All @@ -2687,7 +2678,6 @@ func TestDatabaseCLIFlags(t *testing.T) {
inFlags CommandLineFlags
desc string
outDatabase servicecfg.Database
outError string
}{
{
desc: "valid database config",
Expand All @@ -2711,36 +2701,7 @@ func TestDatabaseCLIFlags(t *testing.T) {
Command: []string{"hostname"},
},
},
TLS: servicecfg.DatabaseTLS{
Mode: servicecfg.VerifyFull,
},
},
},
{
desc: "unsupported database protocol",
inFlags: CommandLineFlags{
DatabaseName: "foo",
DatabaseProtocol: "unknown",
DatabaseURI: "localhost:5432",
},
outError: `unsupported database "foo" protocol`,
},
{
desc: "missing database uri",
inFlags: CommandLineFlags{
DatabaseName: "foo",
DatabaseProtocol: defaults.ProtocolPostgres,
},
outError: `database "foo" URI is empty`,
},
{
desc: "invalid database uri (missing port)",
inFlags: CommandLineFlags{
DatabaseName: "foo",
DatabaseProtocol: defaults.ProtocolPostgres,
DatabaseURI: "localhost",
},
outError: `invalid database "foo" address`,
},
{
desc: "RDS database",
Expand All @@ -2767,9 +2728,6 @@ func TestDatabaseCLIFlags(t *testing.T) {
types.OriginLabel: types.OriginConfigFile,
},
DynamicLabels: services.CommandLabels{},
TLS: servicecfg.DatabaseTLS{
Mode: servicecfg.VerifyFull,
},
},
},
{
Expand Down Expand Up @@ -2800,9 +2758,6 @@ func TestDatabaseCLIFlags(t *testing.T) {
types.OriginLabel: types.OriginConfigFile,
},
DynamicLabels: services.CommandLabels{},
TLS: servicecfg.DatabaseTLS{
Mode: servicecfg.VerifyFull,
},
},
},
{
Expand All @@ -2820,7 +2775,6 @@ func TestDatabaseCLIFlags(t *testing.T) {
Protocol: defaults.ProtocolPostgres,
URI: "localhost:5432",
TLS: servicecfg.DatabaseTLS{
Mode: servicecfg.VerifyFull,
CACert: fixtures.LocalhostCert,
},
GCP: servicecfg.DatabaseGCP{
Expand All @@ -2847,12 +2801,8 @@ func TestDatabaseCLIFlags(t *testing.T) {
Name: "sqlserver",
Protocol: defaults.ProtocolSQLServer,
URI: "sqlserver.example.com:1433",
TLS: servicecfg.DatabaseTLS{
Mode: servicecfg.VerifyFull,
},
AD: servicecfg.DatabaseAD{
KeytabFile: "/etc/keytab",
Krb5File: defaults.Krb5FilePath,
Domain: "EXAMPLE.COM",
SPN: "MSSQLSvc/sqlserver.example.com:1433",
},
Expand All @@ -2877,9 +2827,6 @@ func TestDatabaseCLIFlags(t *testing.T) {
MySQL: servicecfg.MySQLOptions{
ServerVersion: "8.0.28",
},
TLS: servicecfg.DatabaseTLS{
Mode: servicecfg.VerifyFull,
},
StaticLabels: map[string]string{
types.OriginLabel: types.OriginConfigFile,
},
Expand Down Expand Up @@ -2911,9 +2858,6 @@ func TestDatabaseCLIFlags(t *testing.T) {
types.OriginLabel: types.OriginConfigFile,
},
DynamicLabels: services.CommandLabels{},
TLS: servicecfg.DatabaseTLS{
Mode: servicecfg.VerifyFull,
},
},
},
{
Expand Down Expand Up @@ -2941,9 +2885,6 @@ func TestDatabaseCLIFlags(t *testing.T) {
types.OriginLabel: types.OriginConfigFile,
},
DynamicLabels: services.CommandLabels{},
TLS: servicecfg.DatabaseTLS{
Mode: servicecfg.VerifyFull,
},
},
},
{
Expand Down Expand Up @@ -2976,9 +2917,6 @@ func TestDatabaseCLIFlags(t *testing.T) {
types.OriginLabel: types.OriginConfigFile,
},
DynamicLabels: services.CommandLabels{},
TLS: servicecfg.DatabaseTLS{
Mode: servicecfg.VerifyFull,
},
},
},
}
Expand All @@ -2990,12 +2928,8 @@ func TestDatabaseCLIFlags(t *testing.T) {

config := servicecfg.MakeDefaultConfig()
err := Configure(&tt.inFlags, config, false)
if tt.outError != "" {
require.Contains(t, err.Error(), tt.outError)
} else {
require.NoError(t, err)
require.Equal(t, []servicecfg.Database{tt.outDatabase}, config.Databases.Databases)
}
require.NoError(t, err)
require.Equal(t, []servicecfg.Database{tt.outDatabase}, config.Databases.Databases)
})
}
}
Expand Down
Loading

0 comments on commit 5af7106

Please sign in to comment.