/
auth.go
66 lines (56 loc) · 1.19 KB
/
auth.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
package v1beta1
import (
"strings"
"github.com/rancher/norman/types/slice"
)
var (
ReadVerbs = []string{
"get",
"list",
"watch",
}
WriteVerbs = []string{
"create",
"delete",
"get",
"list",
"patch",
"update",
"watch",
}
)
type Permission struct {
Role string `json:"role,omitempty"`
Verbs []string `json:"verbs,omitempty"`
APIGroup string `json:"apiGroup,omitempty"`
Resource string `json:"resource,omitempty"`
URL string `json:"url,omitempty"`
Name string `json:"name,omitempty"`
}
func (p Permission) MaybeString() interface{} {
if p.Role != "" {
return "role=" + p.Role
}
buf := strings.Builder{}
if slice.StringsEqual(WriteVerbs, p.Verbs) {
buf.WriteString("write ")
} else if len(p.Verbs) > 0 && !slice.StringsEqual(ReadVerbs, p.Verbs) {
buf.WriteString(strings.Join(p.Verbs, ","))
buf.WriteString(" ")
}
if p.URL == "" {
if p.APIGroup != "" || strings.Contains(p.Resource, "/") {
buf.WriteString(p.APIGroup)
buf.WriteString("/")
}
buf.WriteString(p.Resource)
if p.Name != "" {
buf.WriteString(" ")
buf.WriteString(p.Name)
}
} else {
buf.WriteString("url=")
buf.WriteString(p.URL)
}
return buf.String()
}