/
admin_commands.go
266 lines (236 loc) · 6.29 KB
/
admin_commands.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
package cmd
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"strings"
"github.com/manyminds/api2go/jsonapi"
"github.com/urfave/cli"
"go.uber.org/multierr"
"github.com/smartcontractkit/chainlink/core/utils"
"github.com/smartcontractkit/chainlink/core/web/presenters"
)
func initAdminSubCmds(client *Client) []cli.Command {
return []cli.Command{
{
Name: "chpass",
Usage: "Change your API password remotely",
Action: client.ChangePassword,
},
{
Name: "login",
Usage: "Login to remote client by creating a session cookie",
Action: client.RemoteLogin,
Flags: []cli.Flag{
cli.StringFlag{
Name: "file, f",
Usage: "text file holding the API email and password needed to create a session cookie",
},
cli.BoolFlag{
Name: "bypass-version-check",
Usage: "Bypass versioning check for compatibility of remote node",
},
},
},
{
Name: "logout",
Usage: "Delete any local sessions",
Action: client.Logout,
},
{
Name: "users",
Usage: "Create, edit permissions, or delete API users",
Subcommands: cli.Commands{
{
Name: "list",
Usage: "Lists all API users and their roles",
Action: client.ListUsers,
},
{
Name: "create",
Usage: "Create a new API user",
Action: client.CreateUser,
Flags: []cli.Flag{
cli.StringFlag{
Name: "email",
Usage: "Email of new user to create",
Required: true,
},
cli.StringFlag{
Name: "role",
Usage: "Permission level of new user. Options: 'admin', 'edit', 'run', 'view'.",
Required: true,
},
},
},
{
Name: "chrole",
Usage: "Changes an API user's role",
Action: client.ChangeRole,
Flags: []cli.Flag{
cli.StringFlag{
Name: "email",
Usage: "email of user to be editted",
Required: true,
},
cli.StringFlag{
Name: "newrole",
Usage: "new permission level role to set for user. Options: 'admin', 'edit', 'run', 'view'.",
Required: true,
},
},
},
{
Name: "delete",
Usage: "Delete an API user",
Action: client.DeleteUser,
Flags: []cli.Flag{
cli.StringFlag{
Name: "email",
Usage: "Email of API user to delete",
Required: true,
},
},
},
},
},
}
}
type AdminUsersPresenter struct {
JAID
presenters.UserResource
}
var adminUsersTableHeaders = []string{"Email", "Role", "Has API token", "Created at", "Updated at"}
func (p *AdminUsersPresenter) ToRow() []string {
row := []string{
p.ID,
string(p.Role),
p.HasActiveApiToken,
p.CreatedAt.String(),
p.UpdatedAt.String(),
}
return row
}
// RenderTable implements TableRenderer
func (p *AdminUsersPresenter) RenderTable(rt RendererTable) error {
rows := [][]string{p.ToRow()}
renderList(adminUsersTableHeaders, rows, rt.Writer)
return utils.JustError(rt.Write([]byte("\n")))
}
type AdminUsersPresenters []AdminUsersPresenter
// RenderTable implements TableRenderer
func (ps AdminUsersPresenters) RenderTable(rt RendererTable) error {
rows := [][]string{}
for _, p := range ps {
rows = append(rows, p.ToRow())
}
if _, err := rt.Write([]byte("Users\n")); err != nil {
return err
}
renderList(adminUsersTableHeaders, rows, rt.Writer)
return utils.JustError(rt.Write([]byte("\n")))
}
// ListUsers renders all API users and their roles
func (cli *Client) ListUsers(c *cli.Context) (err error) {
resp, err := cli.HTTP.Get("/v2/users/", nil)
if err != nil {
return cli.errorOut(err)
}
defer func() {
if cerr := resp.Body.Close(); cerr != nil {
err = multierr.Append(err, cerr)
}
}()
return cli.renderAPIResponse(resp, &AdminUsersPresenters{})
}
// CreateUser creates a new user by prompting for email, password, and role
func (cli *Client) CreateUser(c *cli.Context) (err error) {
resp, err := cli.HTTP.Get("/v2/users/", nil)
if err != nil {
return cli.errorOut(err)
}
defer func() {
if cerr := resp.Body.Close(); cerr != nil {
err = multierr.Append(err, cerr)
}
}()
var links jsonapi.Links
var users AdminUsersPresenters
if err := cli.deserializeAPIResponse(resp, &users, &links); err != nil {
return cli.errorOut(err)
}
for _, user := range users {
if strings.EqualFold(user.Email, c.String("email")) {
return cli.errorOut(fmt.Errorf("user with email %s already exists", user.Email))
}
}
fmt.Println("Password of new user:")
pwd := cli.PasswordPrompter.Prompt()
request := struct {
Email string `json:"email"`
Role string `json:"role"`
Password string `json:"password"`
}{
Email: c.String("email"),
Role: c.String("role"),
Password: pwd,
}
requestData, err := json.Marshal(request)
if err != nil {
return cli.errorOut(err)
}
buf := bytes.NewBuffer(requestData)
response, err := cli.HTTP.Post("/v2/users", buf)
if err != nil {
return cli.errorOut(err)
}
defer func() {
if cerr := response.Body.Close(); cerr != nil {
err = multierr.Append(err, cerr)
}
}()
return cli.renderAPIResponse(response, &AdminUsersPresenter{}, "Successfully created new API user")
}
// ChangeRole can change a user's role
func (cli *Client) ChangeRole(c *cli.Context) (err error) {
request := struct {
Email string `json:"email"`
NewRole string `json:"newRole"`
}{
Email: c.String("email"),
NewRole: c.String("newrole"),
}
requestData, err := json.Marshal(request)
if err != nil {
return cli.errorOut(err)
}
buf := bytes.NewBuffer(requestData)
response, err := cli.HTTP.Patch("/v2/users", buf)
if err != nil {
return cli.errorOut(err)
}
defer func() {
if cerr := response.Body.Close(); cerr != nil {
err = multierr.Append(err, cerr)
}
}()
return cli.renderAPIResponse(response, &AdminUsersPresenter{}, "Successfully updated API user")
}
// DeleteUser deletes an API user by email
func (cli *Client) DeleteUser(c *cli.Context) (err error) {
email := c.String("email")
if email == "" {
return cli.errorOut(errors.New("email flag is empty, must specify an email"))
}
response, err := cli.HTTP.Delete(fmt.Sprintf("/v2/users/%s", email))
if err != nil {
return cli.errorOut(err)
}
defer func() {
if cerr := response.Body.Close(); cerr != nil {
err = multierr.Append(err, cerr)
}
}()
return cli.renderAPIResponse(response, &AdminUsersPresenter{}, "Successfully deleted API user")
}