Skip to content

Commit

Permalink
feat(rdb): add privileges per databases in user list (#1314)
Browse files Browse the repository at this point in the history
Co-authored-by: Olivier Cano <[email protected]>
  • Loading branch information
remyleone and kindermoumoute authored Aug 11, 2020
1 parent a5449a9 commit 51b50b8
Show file tree
Hide file tree
Showing 5 changed files with 823 additions and 0 deletions.
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

0 comments on commit 51b50b8

Please sign in to comment.