This repository has been archived by the owner on Apr 25, 2022. It is now read-only.
/
rbac.go
77 lines (74 loc) · 1.93 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
package openshift
import (
rbacv1 "k8s.io/api/rbac/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
// BuildClusterRole returns a k8s ClusterRole object for the
// lokistack gateway serviceaccount to allow creating:
// - TokenReviews to authenticate the user by bearer token.
// - SubjectAccessReview to authorize the user by bearer token.
// if having access to read/create logs.
func BuildClusterRole(opts Options) *rbacv1.ClusterRole {
return &rbacv1.ClusterRole{
TypeMeta: metav1.TypeMeta{
Kind: "ClusterRole",
APIVersion: rbacv1.SchemeGroupVersion.String(),
},
ObjectMeta: metav1.ObjectMeta{
Name: clusterRoleName(opts),
Labels: opts.BuildOpts.Labels,
},
Rules: []rbacv1.PolicyRule{
{
APIGroups: []string{
"authentication.k8s.io",
},
Resources: []string{
"tokenreviews",
},
Verbs: []string{
"create",
},
},
{
APIGroups: []string{
"authorization.k8s.io",
},
Resources: []string{
"subjectaccessreviews",
},
Verbs: []string{
"create",
},
},
},
}
}
// BuildClusterRoleBinding returns a k8s ClusterRoleBinding object for
// the lokistack gateway serviceaccount to grant access to:
// - rbac.authentication.k8s.io/TokenReviews
// - rbac.authorization.k8s.io/SubjectAccessReviews
func BuildClusterRoleBinding(opts Options) *rbacv1.ClusterRoleBinding {
return &rbacv1.ClusterRoleBinding{
TypeMeta: metav1.TypeMeta{
Kind: "ClusterRoleBinding",
APIVersion: rbacv1.SchemeGroupVersion.String(),
},
ObjectMeta: metav1.ObjectMeta{
Name: opts.BuildOpts.GatewayName,
Labels: opts.BuildOpts.Labels,
},
RoleRef: rbacv1.RoleRef{
APIGroup: "rbac.authorization.k8s.io",
Kind: "ClusterRole",
Name: clusterRoleName(opts),
},
Subjects: []rbacv1.Subject{
{
Kind: rbacv1.ServiceAccountKind,
Name: serviceAccountName(opts),
Namespace: opts.BuildOpts.GatewayNamespace,
},
},
}
}