-
Notifications
You must be signed in to change notification settings - Fork 151
/
custom_user.go
230 lines (199 loc) · 6.16 KB
/
custom_user.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package rdb
import (
"context"
"fmt"
"reflect"
"github.com/scaleway/scaleway-cli/v2/internal/core"
"github.com/scaleway/scaleway-cli/v2/internal/passwordgenerator"
"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,
Region: listUserRequest.Region,
},
scw.WithAllPages(),
)
if err != nil {
return resI, err
}
for _, privilege := range listPrivileges.Privileges {
user, userExists := index[privilege.UserName]
if !userExists {
continue
}
switch privilege.Permission {
case rdb.PermissionAll:
user.All = append(user.All, privilege.DatabaseName)
case rdb.PermissionReadonly:
user.ReadOnly = append(user.ReadOnly, privilege.DatabaseName)
case rdb.PermissionCustom:
user.Custom = append(user.Custom, privilege.DatabaseName)
case rdb.PermissionNone:
user.None = append(user.None, privilege.DatabaseName)
case rdb.PermissionReadwrite:
user.ReadWrite = append(user.ReadWrite, privilege.DatabaseName)
default:
core.ExtractLogger(ctx).Errorf("unsupported permission value %s", privilege.Permission)
}
}
return res, nil
}
return c
}
func userCreateBuilder(c *core.Command) *core.Command {
type rdbCreateUserRequestCustom struct {
*rdb.CreateUserRequest
GeneratePassword bool
}
type rdbCreateUserResponseCustom struct {
*rdb.User
Password string `json:"password"`
}
c.ArgSpecs.AddBefore("password", &core.ArgSpec{
Name: "generate-password",
Short: `Will generate a 21 character-length password that contains a mix of upper/lower case letters, numbers and special symbols`,
Required: false,
Deprecated: false,
Positional: false,
Default: core.DefaultValueSetter("true"),
})
c.ArgsType = reflect.TypeOf(rdbCreateUserRequestCustom{})
c.Run = func(ctx context.Context, argsI interface{}) (interface{}, error) {
client := core.ExtractClient(ctx)
api := rdb.NewAPI(client)
customRequest := argsI.(*rdbCreateUserRequestCustom)
createUserRequest := customRequest.CreateUserRequest
var err error
if customRequest.GeneratePassword && customRequest.Password == "" {
createUserRequest.Password, err = passwordgenerator.GeneratePassword(21, 1, 1, 1, 1)
if err != nil {
return nil, err
}
fmt.Printf("Your generated password is %s \n", createUserRequest.Password)
fmt.Printf("\n")
}
user, err := api.CreateUser(createUserRequest)
if err != nil {
return nil, err
}
result := rdbCreateUserResponseCustom{
User: user,
Password: createUserRequest.Password,
}
return result, nil
}
return c
}
func userUpdateBuilder(c *core.Command) *core.Command {
type rdbUpdateUserRequestCustom struct {
*rdb.UpdateUserRequest
GeneratePassword bool
}
type rdbUpdateUserResponseCustom struct {
*rdb.User
Password string `json:"password"`
}
c.ArgSpecs.AddBefore("password", &core.ArgSpec{
Name: "generate-password",
Short: `Will generate a 21 character-length password that contains a mix of upper/lower case letters, numbers and special symbols`,
Required: false,
Deprecated: false,
Positional: false,
Default: core.DefaultValueSetter("true"),
})
c.ArgsType = reflect.TypeOf(rdbUpdateUserRequestCustom{})
c.Run = func(ctx context.Context, argsI interface{}) (interface{}, error) {
client := core.ExtractClient(ctx)
api := rdb.NewAPI(client)
customRequest := argsI.(*rdbUpdateUserRequestCustom)
updateUserRequest := customRequest.UpdateUserRequest
var err error
if customRequest.GeneratePassword && customRequest.Password == nil {
updateUserRequest.Password = new(string)
*updateUserRequest.Password, err = passwordgenerator.GeneratePassword(21, 1, 1, 1, 1)
if err != nil {
return nil, err
}
fmt.Printf("Your generated password is %v \n", *updateUserRequest.Password)
fmt.Printf("\n")
}
user, err := api.UpdateUser(updateUserRequest)
if err != nil {
return nil, err
}
result := rdbUpdateUserResponseCustom{
User: user,
Password: *updateUserRequest.Password,
}
return result, nil
}
return c
}
func userGetURLCommand() *core.Command {
return &core.Command{
Namespace: "rdb",
Resource: "user",
Verb: "get-url",
Short: "Gets the URL to connect to the Database",
Long: "Provides the URL to connect to a Database on an Instance as the given user",
ArgsType: reflect.TypeOf(rdbGetURLArgs{}),
ArgSpecs: core.ArgSpecs{
{
Name: "instance-id",
Short: `ID of the Database Instance`,
Required: true,
Positional: true,
},
{
Name: "user",
Short: `User of the Database`,
Required: false,
Positional: false,
},
{
Name: "db",
Short: `Name of the Database to connect to`,
Required: false,
Positional: false,
},
},
Run: generateURL,
}
}