Skip to content

feat(rdb): add privileges per databases in user list #1314

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

Merged
merged 8 commits into from
Aug 11, 2020
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
2 changes: 2 additions & 0 deletions internal/namespaces/rdb/v1/custom.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,7 @@ func GetCommands() *core.Commands {

cmds.MustFind("rdb", "engine", "list").Override(engineListBuilder)

cmds.MustFind("rdb", "user", "list").Override(userListBuilder)

return cmds
}
77 changes: 77 additions & 0 deletions internal/namespaces/rdb/v1/custom_user.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package rdb

import (
"context"

"github.com/scaleway/scaleway-cli/internal/core"
"github.com/scaleway/scaleway-sdk-go/api/rdb/v1"
"github.com/scaleway/scaleway-sdk-go/scw"
)

// userListBuilder creates a table visualization of user's permission across different database in a given RDB instance
func userListBuilder(c *core.Command) *core.Command {
c.Interceptor = func(ctx context.Context, argsI interface{}, runner core.CommandRunner) (interface{}, error) {
type customUser struct {
Name string `json:"name"`
IsAdmin bool `json:"is_admin"`
ReadOnly []string `json:"readonly"`
ReadWrite []string `json:"readwrite"`
All []string `json:"all"`
Custom []string `json:"custom"`
None []string `json:"none"`
}

resI, err := runner(ctx, argsI)
if err != nil {
return nil, err
}

// We index user by their name and use customUser as the type holding the different privileges across databases
index := make(map[string]*customUser)
res := []*customUser(nil)
listUserRequest := argsI.(*rdb.ListUsersRequest)
listUserResponse := resI.([]*rdb.User)
for _, user := range listUserResponse {
user := &customUser{
Name: user.Name,
IsAdmin: user.IsAdmin,
ReadOnly: []string{},
ReadWrite: []string{},
All: []string{},
Custom: []string{},
None: []string{},
}
res = append(res, user)
index[user.Name] = user
}

api := rdb.NewAPI(core.ExtractClient(ctx))
listPrivileges, err := api.ListPrivileges(
&rdb.ListPrivilegesRequest{InstanceID: listUserRequest.InstanceID},
scw.WithAllPages(),
)
if err != nil {
return resI, err
}

for _, privilege := range listPrivileges.Privileges {
switch privilege.Permission {
case rdb.PermissionAll:
index[privilege.UserName].All = append(index[privilege.UserName].All, privilege.DatabaseName)
case rdb.PermissionReadonly:
index[privilege.UserName].ReadOnly = append(index[privilege.UserName].ReadOnly, privilege.DatabaseName)
case rdb.PermissionCustom:
index[privilege.UserName].Custom = append(index[privilege.UserName].Custom, privilege.DatabaseName)
case rdb.PermissionNone:
index[privilege.UserName].None = append(index[privilege.UserName].None, privilege.DatabaseName)
case rdb.PermissionReadwrite:
index[privilege.UserName].ReadWrite = append(index[privilege.UserName].ReadWrite, privilege.DatabaseName)
default:
core.ExtractLogger(ctx).Errorf("unsupported permission value %s", privilege.Permission)
}
}
return res, nil
}

return c
}
17 changes: 17 additions & 0 deletions internal/namespaces/rdb/v1/custom_user_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package rdb

import (
"testing"

"github.com/scaleway/scaleway-cli/internal/core"
)

func Test_ListUser(t *testing.T) {
t.Run("Simple", core.Test(&core.TestConfig{
Commands: GetCommands(),
BeforeFunc: createInstance("PostgreSQL-12"),
Cmd: "scw rdb user list instance-id={{ .Instance.ID }}",
Check: core.TestCheckGolden(),
AfterFunc: deleteInstance(),
}))
}
Loading