-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
policy.go
68 lines (58 loc) · 1.74 KB
/
policy.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
package view
import (
"context"
"github.com/derailed/k9s/internal"
"github.com/derailed/k9s/internal/client"
"github.com/derailed/k9s/internal/render"
"github.com/derailed/k9s/internal/ui"
"github.com/gdamore/tcell"
)
const (
group = "Group"
user = "User"
sa = "ServiceAccount"
)
// Policy presents a RBAC rules viewer based on what a given user/group or sa can do.
type Policy struct {
ResourceViewer
subjectKind, subjectName string
}
// NewPolicy returns a new viewer.
func NewPolicy(app *App, subject, name string) *Policy {
p := Policy{
ResourceViewer: NewBrowser(client.NewGVR("policy")),
subjectKind: subject,
subjectName: name,
}
p.GetTable().SetColorerFn(render.Policy{}.ColorerFunc())
p.SetBindKeysFn(p.bindKeys)
p.GetTable().SetSortCol(nameCol, false)
p.SetContextFn(p.subjectCtx)
p.GetTable().SetEnterFn(blankEnterFn)
return &p
}
func (p *Policy) subjectCtx(ctx context.Context) context.Context {
ctx = context.WithValue(ctx, internal.KeySubjectKind, mapSubject(p.subjectKind))
ctx = context.WithValue(ctx, internal.KeyPath, mapSubject(p.subjectKind)+":"+p.subjectName)
return context.WithValue(ctx, internal.KeySubjectName, p.subjectName)
}
func (p *Policy) bindKeys(aa ui.KeyActions) {
aa.Delete(ui.KeyShiftA, tcell.KeyCtrlSpace, ui.KeySpace)
aa.Add(ui.KeyActions{
ui.KeyShiftN: ui.NewKeyAction("Sort Name", p.GetTable().SortColCmd(nameCol, true), false),
ui.KeyShiftO: ui.NewKeyAction("Sort Group", p.GetTable().SortColCmd("GROUP", true), false),
ui.KeyShiftB: ui.NewKeyAction("Sort Binding", p.GetTable().SortColCmd("BINDING", true), false),
})
}
func mapSubject(subject string) string {
switch subject {
case "g":
return group
case "s":
return sa
case "u":
return user
default:
return subject
}
}