Skip to content

Commit

Permalink
Merge pull request #278 from gregmankes/user_auth_plugin_dbaas
Browse files Browse the repository at this point in the history
add mysql user auth settings for database users
  • Loading branch information
bentranter authored Nov 18, 2019
2 parents 6c8415f + 59175fb commit c333a45
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 4 deletions.
21 changes: 17 additions & 4 deletions databases.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ const (
SQLModeTraditional = "TRADITIONAL"
)

// SQL Auth constants allow for MySQL-specific user auth plugins
const (
SQLAuthPluginNative = "mysql_native_password"
SQLAuthPluginCachingSHA2 = "caching_sha2_password"
)

// DatabasesService is an interface for interfacing with the databases endpoints
// of the DigitalOcean API.
// See: https://developers.digitalocean.com/documentation/v2#databases
Expand Down Expand Up @@ -143,9 +149,15 @@ type DatabaseConnection struct {

// DatabaseUser represents a user in the database
type DatabaseUser struct {
Name string `json:"name,omitempty"`
Role string `json:"role,omitempty"`
Password string `json:"password,omitempty"`
Name string `json:"name,omitempty"`
Role string `json:"role,omitempty"`
Password string `json:"password,omitempty"`
MySQLSettings *DatabaseMySQLUserSettings `json:"mysql_settings,omitempty"`
}

// DatabaseMySQLUserSettings contains MySQL-specific user settings
type DatabaseMySQLUserSettings struct {
AuthPlugin string `json:"auth_plugin"`
}

// DatabaseMaintenanceWindow represents the maintenance_window of a database
Expand Down Expand Up @@ -235,7 +247,8 @@ type DatabaseCreatePoolRequest struct {

// DatabaseCreateUserRequest is used to create a new database user
type DatabaseCreateUserRequest struct {
Name string `json:"name"`
Name string `json:"name"`
MySQLSettings *DatabaseMySQLUserSettings `json:"mysql_settings,omitempty"`
}

// DatabaseCreateDBRequest is used to create a new engine-specific database within the cluster
Expand Down
110 changes: 110 additions & 0 deletions databases_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1297,3 +1297,113 @@ func TestDatabases_UpdateFirewallRules(t *testing.T) {
})
require.NoError(t, err)
}

func TestDatabases_CreateDatabaseUserWithMySQLSettings(t *testing.T) {
setup()
defer teardown()

dbID := "deadbeef-dead-4aa5-beef-deadbeef347d"

path := fmt.Sprintf("/v2/databases/%s/users", dbID)

responseJSON := []byte(fmt.Sprintf(`{
"user": {
"name": "foo",
"mysql_settings": {
"auth_plugin": "%s"
}
}
}`, SQLAuthPluginNative))
expectedUser := &DatabaseUser{
Name: "foo",
MySQLSettings: &DatabaseMySQLUserSettings{
AuthPlugin: SQLAuthPluginNative,
},
}

mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodPost)
w.WriteHeader(http.StatusOK)
w.Write(responseJSON)
})

user, _, err := client.Databases.CreateUser(ctx, dbID, &DatabaseCreateUserRequest{
Name: expectedUser.Name,
MySQLSettings: expectedUser.MySQLSettings,
})
require.NoError(t, err)
require.Equal(t, expectedUser, user)
}

func TestDatabases_ListDatabaseUsersWithMySQLSettings(t *testing.T) {
setup()
defer teardown()

dbID := "deadbeef-dead-4aa5-beef-deadbeef347d"

path := fmt.Sprintf("/v2/databases/%s/users", dbID)

responseJSON := []byte(fmt.Sprintf(`{
"users": [
{
"name": "foo",
"mysql_settings": {
"auth_plugin": "%s"
}
}
]
}`, SQLAuthPluginNative))
expectedUsers := []DatabaseUser{
{
Name: "foo",
MySQLSettings: &DatabaseMySQLUserSettings{
AuthPlugin: SQLAuthPluginNative,
},
},
}

mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
w.WriteHeader(http.StatusOK)
w.Write(responseJSON)
})

users, _, err := client.Databases.ListUsers(ctx, dbID, &ListOptions{})
require.NoError(t, err)
require.Equal(t, expectedUsers, users)
}

func TestDatabases_GetDatabaseUserWithMySQLSettings(t *testing.T) {
setup()
defer teardown()

dbID := "deadbeef-dead-4aa5-beef-deadbeef347d"
userID := "d290a0a0-27da-42bd-a4b2-bcecf43b8832"

path := fmt.Sprintf("/v2/databases/%s/users/%s", dbID, userID)

responseJSON := []byte(fmt.Sprintf(`{
"user": {
"name": "foo",
"mysql_settings": {
"auth_plugin": "%s"
}
}
}`, SQLAuthPluginNative))
expectedUser := &DatabaseUser{
Name: "foo",
MySQLSettings: &DatabaseMySQLUserSettings{
AuthPlugin: SQLAuthPluginNative,
},
}

mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
w.WriteHeader(http.StatusOK)
w.Write(responseJSON)
})

user, _, err := client.Databases.GetUser(ctx, dbID, userID)
require.NoError(t, err)
require.Equal(t, expectedUser, user)
}

0 comments on commit c333a45

Please sign in to comment.