-
Notifications
You must be signed in to change notification settings - Fork 18
/
appeal.go
461 lines (380 loc) · 11.1 KB
/
appeal.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
package cli
import (
"fmt"
"os"
"strings"
"github.com/MakeNowJust/heredoc"
guardianv1beta1 "github.com/raystack/guardian/api/proto/raystack/guardian/v1beta1"
"github.com/raystack/guardian/domain"
"github.com/raystack/salt/printer"
"github.com/raystack/salt/term"
"github.com/spf13/cobra"
"google.golang.org/protobuf/types/known/structpb"
)
func appealsCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "appeal",
Aliases: []string{"appeals"},
Short: "Manage appeals",
Annotations: map[string]string{
"group": "core",
},
Example: heredoc.Doc(`
$ guardian appeal create
$ guardian appeal approve
$ guardian appeal reject
$ guardian appeal list
$ guardian appeal status
$ guardian appeal cancel
`),
}
cmd.AddCommand(listAppealsCommand())
cmd.AddCommand(createAppealCommand())
cmd.AddCommand(approveApprovalStepCommand())
cmd.AddCommand(rejectApprovalStepCommand())
cmd.AddCommand(statusAppealCommand())
cmd.AddCommand(cancelAppealCommand())
cmd.AddCommand(addApproverCommand())
cmd.AddCommand(deleteApproverCommand())
bindFlagsFromConfig(cmd)
return cmd
}
func listAppealsCommand() *cobra.Command {
var statuses []string
var role string
var accountID string
cmd := &cobra.Command{
Use: "list",
Short: "List and filter appeals",
Example: heredoc.Doc(`
$ guardian appeal list
$ guardian appeal list --status=pending
$ guardian appeal list --role=viewer
`),
RunE: func(cmd *cobra.Command, args []string) error {
spinner := printer.Spin("")
defer spinner.Stop()
client, cancel, err := createClient(cmd)
if err != nil {
return err
}
defer cancel()
res, err := client.ListAppeals(cmd.Context(), &guardianv1beta1.ListAppealsRequest{
Statuses: statuses,
Role: role,
AccountId: accountID,
})
if err != nil {
return err
}
report := [][]string{}
appeals := res.GetAppeals()
spinner.Stop()
fmt.Printf(" \nShowing %d of %d appeals\n \n", len(appeals), len(appeals))
report = append(report, []string{"ID", "USER", "RESOURCE ID", "ROLE", "STATUS"})
for _, a := range appeals {
report = append(report, []string{
term.Greenf("%v", a.GetId()),
a.GetAccountId(),
fmt.Sprintf("%v", a.GetResourceId()),
a.GetRole(),
a.GetStatus(),
})
}
printer.Table(os.Stdout, report)
return nil
},
}
cmd.Flags().StringArrayVarP(&statuses, "status", "s", nil, "Filter by status(es)")
cmd.Flags().StringVarP(&role, "role", "r", "", "Filter by role")
cmd.Flags().StringVarP(&accountID, "account", "a", "", "Filter by account")
return cmd
}
func createAppealCommand() *cobra.Command {
var accountID, accountType, resourceID, role, optionsDuration, description string
cmd := &cobra.Command{
Use: "create",
Short: "Create a new appeal",
Example: heredoc.Doc(`
$ guardian appeal create
$ guardian appeal create --account=<account-id> --type=<account-type> --resource=<resource-id> --role=<role> --description="<description>"
`),
RunE: func(cmd *cobra.Command, args []string) error {
spinner := printer.Spin("")
defer spinner.Stop()
options := map[string]interface{}{}
if optionsDuration != "" {
options["duration"] = optionsDuration
}
optionsProto, err := structpb.NewStruct(options)
if err != nil {
return err
}
client, cancel, err := createClient(cmd)
if err != nil {
return err
}
defer cancel()
res, err := client.CreateAppeal(cmd.Context(), &guardianv1beta1.CreateAppealRequest{
AccountId: accountID,
AccountType: accountType,
Resources: []*guardianv1beta1.CreateAppealRequest_Resource{
{
Id: resourceID,
Role: role,
Options: optionsProto,
},
},
Description: description,
})
if err != nil {
fmt.Println("Create error")
return err
}
spinner.Stop()
appealID := res.GetAppeals()[0].GetId()
printer.Successf("appeal created with id: %v", appealID)
return nil
},
}
cmd.Flags().StringVarP(&accountID, "account", "a", "", "Email of the account to appeal")
cmd.MarkFlagRequired("account")
cmd.Flags().StringVarP(&accountType, "type", "t", "", "Type of the account")
cmd.MarkFlagRequired("type")
cmd.Flags().StringVarP(&resourceID, "resource", "R", "", "ID of the resource")
cmd.MarkFlagRequired("resource")
cmd.Flags().StringVarP(&role, "role", "r", "", "Role to be assigned")
cmd.MarkFlagRequired("role")
cmd.Flags().StringVarP(&optionsDuration, "duration", "d", "", "Duration of the access")
cmd.Flags().StringVar(&description, "description", "", "The description of the appeal")
return cmd
}
func approveApprovalStepCommand() *cobra.Command {
var approvalName string
cmd := &cobra.Command{
Use: "approve",
Short: "Approve an approval step",
Example: heredoc.Doc(`
$ guardian appeal approve <appeal-id> --step=<step-name>
`),
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
spinner := printer.Spin("")
defer spinner.Stop()
client, cancel, err := createClient(cmd)
if err != nil {
return err
}
defer cancel()
id := args[0]
_, err = client.UpdateApproval(cmd.Context(), &guardianv1beta1.UpdateApprovalRequest{
Id: id,
ApprovalName: approvalName,
Action: &guardianv1beta1.UpdateApprovalRequest_Action{
Action: "approve",
},
})
if err != nil {
return err
}
spinner.Stop()
fmt.Printf("appeal with id %v and approval name %v approved successfully", id, approvalName)
return nil
},
}
cmd.Flags().StringVarP(&approvalName, "step", "s", "", "Name of approval step")
cmd.MarkFlagRequired("approval-name")
return cmd
}
func rejectApprovalStepCommand() *cobra.Command {
var approvalName string
cmd := &cobra.Command{
Use: "reject",
Short: "Reject an approval step",
Example: heredoc.Doc(`
$ guardian appeal reject <appeal-id> --step=<step-name>
`),
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
spinner := printer.Spin("")
defer spinner.Stop()
client, cancel, err := createClient(cmd)
if err != nil {
return err
}
defer cancel()
id := args[0]
_, err = client.UpdateApproval(cmd.Context(), &guardianv1beta1.UpdateApprovalRequest{
Id: id,
ApprovalName: approvalName,
Action: &guardianv1beta1.UpdateApprovalRequest_Action{
Action: "reject",
},
})
if err != nil {
return err
}
spinner.Stop()
fmt.Printf("appeal with id %v and approval name %v rejected successfully", id, approvalName)
return nil
},
}
cmd.Flags().StringVarP(&approvalName, "step", "s", "", "Name of approval step")
cmd.MarkFlagRequired("approval-name")
return cmd
}
func statusAppealCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "status",
Short: "Approval status of an appeal",
Example: heredoc.Doc(`
$ guardian appeal status <appeal-id>
`),
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
spinner := printer.Spin("")
defer spinner.Stop()
client, cancel, err := createClient(cmd)
if err != nil {
return err
}
defer cancel()
id := args[0]
res, err := client.GetAppeal(cmd.Context(), &guardianv1beta1.GetAppealRequest{
Id: id,
})
if err != nil {
return err
}
spinner.Stop()
appeal := res.GetAppeal()
fmt.Printf(" \nAppeal status: %s\n", appeal.GetStatus())
approvals := appeal.Approvals
report := [][]string{}
report = append(report, []string{"ID", "NAME", "STATUS", "APPROVER(s)"})
fmt.Printf(" \nShowing %d approval steps\n \n", len(approvals))
for _, a := range approvals {
status := a.GetStatus()
actor := a.GetActor()
if actor != "" {
if status == domain.ApprovalStatusApproved {
status = fmt.Sprintf("approved by %s", actor)
} else if status == domain.ApprovalStatusRejected {
status = fmt.Sprintf("rejected by %s", actor)
}
}
report = append(report, []string{
fmt.Sprintf("%v", a.GetId()),
a.GetName(),
status,
strings.Join(a.GetApprovers(), " "),
})
}
printer.Table(os.Stdout, report)
return nil
},
}
return cmd
}
func cancelAppealCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "cancel",
Short: "Cancel an appeal",
Example: heredoc.Doc(`
$ guardian appeal cancel <appeal-id>
`),
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
spinner := printer.Spin("")
defer spinner.Stop()
client, cancel, err := createClient(cmd)
if err != nil {
return err
}
defer cancel()
id := args[0]
_, err = client.CancelAppeal(cmd.Context(), &guardianv1beta1.CancelAppealRequest{
Id: id,
})
if err != nil {
return err
}
spinner.Stop()
fmt.Printf("appeal with id `%v` cancelled successfully", id)
return nil
},
}
return cmd
}
func addApproverCommand() *cobra.Command {
var appealID, approvalID, email string
cmd := &cobra.Command{
Use: "add-approver",
Short: "Add a new approver to an approval step",
Example: heredoc.Doc(`
$ guardian appeal add-approver --appeal-id=<appeal-id> --approval-id=<approval-id> --email=<new-approver-email>
`),
RunE: func(cmd *cobra.Command, args []string) error {
spinner := printer.Spin("")
defer spinner.Stop()
client, cancel, err := createClient(cmd)
if err != nil {
return err
}
defer cancel()
if _, err := client.AddApprover(cmd.Context(), &guardianv1beta1.AddApproverRequest{
AppealId: appealID,
ApprovalId: approvalID,
Email: email,
}); err != nil {
return err
}
spinner.Stop()
fmt.Printf("%q added to the approval\n", email)
return nil
},
}
cmd.Flags().StringVar(&appealID, "appeal-id", "", "Appeal ID")
cmd.Flags().StringVar(&approvalID, "approval-id", "", "Approval ID or approval name")
cmd.Flags().StringVar(&email, "email", "", "New approver email")
cmd.MarkFlagRequired("appeal-id")
cmd.MarkFlagRequired("approval-id")
cmd.MarkFlagRequired("email")
return cmd
}
func deleteApproverCommand() *cobra.Command {
var appealID, approvalID, email string
cmd := &cobra.Command{
Use: "delete-approver",
Short: "Remove an existing approver from an approval step",
Example: heredoc.Doc(`
$ guardian appeal delete-approver --appeal-id=<appeal-id> --approval-id=<approval-id> --email=<new-approver-email>
`),
RunE: func(cmd *cobra.Command, args []string) error {
spinner := printer.Spin("")
defer spinner.Stop()
client, cancel, err := createClient(cmd)
if err != nil {
return err
}
defer cancel()
if _, err := client.DeleteApprover(cmd.Context(), &guardianv1beta1.DeleteApproverRequest{
AppealId: appealID,
ApprovalId: approvalID,
Email: email,
}); err != nil {
return err
}
spinner.Stop()
fmt.Printf("%q removed from the approval\n", email)
return nil
},
}
cmd.Flags().StringVar(&appealID, "appeal-id", "", "Appeal ID")
cmd.Flags().StringVar(&approvalID, "approval-id", "", "Approval ID or approval name")
cmd.Flags().StringVar(&email, "email", "", "New approver email")
cmd.MarkFlagRequired("appeal-id")
cmd.MarkFlagRequired("approval-id")
cmd.MarkFlagRequired("email")
return cmd
}