Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[v9] Advertise correct MySQL server version #12340

Merged
merged 6 commits into from
May 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions api/types/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,13 @@ type Database interface {
GetTLS() DatabaseTLS
// SetStatusCA sets the database CA certificate in the status field.
SetStatusCA(string)
// GetMySQL returns the database options from spec.
GetMySQL() MySQLOptions
// GetMySQLServerVersion returns the MySQL server version either from configuration or
// reported by the database.
GetMySQLServerVersion() string
// SetMySQLServerVersion sets the runtime MySQL server version.
SetMySQLServerVersion(version string)
// GetAWS returns the database AWS metadata.
GetAWS() AWS
// SetStatusAWS sets the database AWS metadata in the status field.
Expand Down Expand Up @@ -250,6 +257,26 @@ func (d *DatabaseV3) SetStatusCA(ca string) {
d.Status.CACert = ca
}

// GetMySQL returns the MySQL options from spec.
func (d *DatabaseV3) GetMySQL() MySQLOptions {
return d.Spec.MySQL
}

// GetMySQLServerVersion returns the MySQL server version reported by the database or the value from configuration
// if the first one is not available.
func (d *DatabaseV3) GetMySQLServerVersion() string {
if d.Status.MySQL.ServerVersion != "" {
return d.Status.MySQL.ServerVersion
}

return d.Spec.MySQL.ServerVersion
}

// SetMySQLServerVersion sets the runtime MySQL server version.
func (d *DatabaseV3) SetMySQLServerVersion(version string) {
d.Status.MySQL.ServerVersion = version
}

// IsEmpty returns true if AWS metadata is empty.
func (a AWS) IsEmpty() bool {
return cmp.Equal(a, AWS{})
Expand Down Expand Up @@ -375,6 +402,9 @@ func (d *DatabaseV3) CheckAndSetDefaults() error {
if d.Spec.URI == "" {
return trace.BadParameter("database %q URI is empty", d.GetName())
}
if d.Spec.MySQL.ServerVersion != "" && d.Spec.Protocol != "mysql" {
return trace.BadParameter("MySQL ServerVersion can be only set for MySQL database")
}
// In case of RDS, Aurora or Redshift, AWS information such as region or
// cluster ID can be extracted from the endpoint if not provided.
switch {
Expand Down
49 changes: 49 additions & 0 deletions api/types/database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,52 @@ func TestDatabaseStatus(t *testing.T) {
database.SetStatusAWS(awsMeta)
require.Equal(t, awsMeta, database.GetAWS())
}

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

t.Run("correct config", func(t *testing.T) {
database, err := NewDatabaseV3(Metadata{
Name: "test",
}, DatabaseSpecV3{
Protocol: "mysql",
URI: "localhost:5432",
MySQL: MySQLOptions{
ServerVersion: "8.0.18",
},
})
require.NoError(t, err)
require.Equal(t, "8.0.18", database.GetMySQLServerVersion())
})

t.Run("incorrect config - wrong protocol", func(t *testing.T) {
_, err := NewDatabaseV3(Metadata{
Name: "test",
}, DatabaseSpecV3{
Protocol: "Postgres",
URI: "localhost:5432",
MySQL: MySQLOptions{
ServerVersion: "8.0.18",
},
})
require.Error(t, err)
require.Contains(t, err.Error(), "ServerVersion")
})
}

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

database, err := NewDatabaseV3(Metadata{
Name: "test",
}, DatabaseSpecV3{
Protocol: "mysql",
URI: "localhost:5432",
})
require.NoError(t, err)

require.Equal(t, "", database.GetMySQLServerVersion())

database.SetMySQLServerVersion("8.0.1")
require.Equal(t, "8.0.1", database.GetMySQLServerVersion())
}
4 changes: 2 additions & 2 deletions api/types/events/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ type AuditEvent interface {

// GetType returns event type
GetType() string
// SetCode sets unique type
// SetType sets unique type
SetType(string)

// GetTime returns event time
Expand All @@ -73,7 +73,7 @@ type AuditEvent interface {

// Emitter creates and manages audit log streams
type Emitter interface {
// Emit emits a single audit event
// EmitAuditEvent emits a single audit event.
EmitAuditEvent(context.Context, AuditEvent) error
}

Expand Down
4 changes: 2 additions & 2 deletions api/types/role.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ type Role interface {

// GetNamespaces gets a list of namespaces this role is allowed or denied access to.
GetNamespaces(RoleConditionType) []string
// GetNamespaces sets a list of namespaces this role is allowed or denied access to.
// SetNamespaces sets a list of namespaces this role is allowed or denied access to.
SetNamespaces(RoleConditionType, []string)

// GetNodeLabels gets the map of node labels this role is allowed or denied access to.
Expand Down Expand Up @@ -118,7 +118,7 @@ type Role interface {

// GetDatabaseNames gets a list of database names this role is allowed or denied access to.
GetDatabaseNames(RoleConditionType) []string
// SetDatabasenames sets a list of database names this role is allowed or denied access to.
// SetDatabaseNames sets a list of database names this role is allowed or denied access to.
SetDatabaseNames(RoleConditionType, []string)

// GetDatabaseUsers gets a list of database users this role is allowed or denied access to.
Expand Down
Loading