/
rbac.go
179 lines (148 loc) · 3.28 KB
/
rbac.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
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of K9s
package render
import (
"fmt"
"strings"
"github.com/derailed/k9s/internal/model1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
)
const allVerbs = "*"
var (
k8sVerbs = []string{
"get",
"list",
"watch",
"create",
"patch",
"update",
"delete",
"deletecollection",
}
httpTok8sVerbs = map[string]string{
"post": "create",
"put": "update",
}
)
// Rbac renders a rbac to screen.
type Rbac struct {
Base
}
// ColorerFunc colors a resource row.
func (Rbac) ColorerFunc() model1.ColorerFunc {
return model1.DefaultColorer
}
// Header returns a header row.
func (Rbac) Header(ns string) model1.Header {
h := make(model1.Header, 0, 10)
h = append(h,
model1.HeaderColumn{Name: "NAME"},
model1.HeaderColumn{Name: "API-GROUP"},
)
h = append(h, rbacVerbHeader()...)
return append(h, model1.HeaderColumn{Name: "VALID", Wide: true})
}
// Render renders a K8s resource to screen.
func (r Rbac) Render(o interface{}, ns string, ro *model1.Row) error {
p, ok := o.(PolicyRes)
if !ok {
return fmt.Errorf("expecting RuleRes but got %T", o)
}
ro.ID = p.Resource
ro.Fields = make(model1.Fields, 0, len(r.Header(ns)))
ro.Fields = append(ro.Fields,
cleanseResource(p.Resource),
p.Group,
)
ro.Fields = append(ro.Fields, asVerbs(p.Verbs)...)
ro.Fields = append(ro.Fields, "")
return nil
}
// ----------------------------------------------------------------------------
// Helpers...
func asVerbs(verbs []string) []string {
const (
verbLen = 4
unknownLen = 30
)
r := make([]string, 0, len(k8sVerbs)+1)
for _, v := range k8sVerbs {
r = append(r, toVerbIcon(hasVerb(verbs, v)))
}
var unknowns []string
for _, v := range verbs {
if hv, ok := httpTok8sVerbs[v]; ok {
v = hv
}
if !hasVerb(k8sVerbs, v) && v != allVerbs {
unknowns = append(unknowns, v)
}
}
return append(r, Truncate(strings.Join(unknowns, ","), unknownLen))
}
func toVerbIcon(ok bool) string {
if ok {
return "[green::b] ✓ [::]"
}
return "[orangered::b] × [::]"
}
func hasVerb(verbs []string, verb string) bool {
if len(verbs) == 1 && verbs[0] == allVerbs {
return true
}
for _, v := range verbs {
if hv, ok := httpTok8sVerbs[v]; ok {
if hv == verb {
return true
}
}
if v == verb {
return true
}
}
return false
}
// RuleRes represents an rbac rule.
type RuleRes struct {
Resource, Group string
ResourceName string
NonResourceURL string
Verbs []string
}
// NewRuleRes returns a new rule.
func NewRuleRes(res, grp string, vv []string) RuleRes {
return RuleRes{
Resource: res,
Group: grp,
Verbs: vv,
}
}
// GetObjectKind returns a schema object.
func (r RuleRes) GetObjectKind() schema.ObjectKind {
return nil
}
// DeepCopyObject returns a container copy.
func (r RuleRes) DeepCopyObject() runtime.Object {
return r
}
// Rules represents a collection of rules.
type Rules []RuleRes
// Upsert adds a new rule.
func (rr Rules) Upsert(r RuleRes) Rules {
idx, ok := rr.find(r.Resource)
if !ok {
return append(rr, r)
}
rr[idx] = r
return rr
}
// Find locates a row by id. Returns false is not found.
func (rr Rules) find(res string) (int, bool) {
for i, r := range rr {
if r.Resource == res {
return i, true
}
}
return 0, false
}