-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathauthz.go
76 lines (66 loc) · 1.91 KB
/
authz.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
package k8s
import (
"errors"
"fmt"
authV1 "k8s.io/api/authorization/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/kubernetes"
)
// ResourceAuthz checks whether a given Kubernetes client is authorized to
// perform a given action.
func ResourceAuthz(
k8sClient kubernetes.Interface,
namespace, verb, group, version, resource, name string,
) error {
ssar := &authV1.SelfSubjectAccessReview{
Spec: authV1.SelfSubjectAccessReviewSpec{
ResourceAttributes: &authV1.ResourceAttributes{
Namespace: namespace,
Verb: verb,
Group: group,
Version: version,
Resource: resource,
Name: name,
},
},
}
result, err := k8sClient.
AuthorizationV1().
SelfSubjectAccessReviews().
Create(ssar)
if err != nil {
return err
}
if result.Status.Allowed {
return nil
}
gk := schema.GroupKind{
Group: group,
Kind: resource,
}
if len(result.Status.Reason) > 0 {
return fmt.Errorf("not authorized to access %s: %s", gk, result.Status.Reason)
}
return fmt.Errorf("not authorized to access %s", gk)
}
// ServiceProfilesAccess checks whether the ServiceProfile CRD is installed
// on the cluster and the client is authorized to access ServiceProfiles.
func ServiceProfilesAccess(k8sClient kubernetes.Interface) error {
res, err := k8sClient.Discovery().ServerResourcesForGroupVersion(ServiceProfileAPIVersion)
if err != nil {
return err
}
if res.GroupVersion == ServiceProfileAPIVersion {
for _, apiRes := range res.APIResources {
if apiRes.Kind == ServiceProfileKind {
return ResourceAuthz(k8sClient, "", "list", "linkerd.io", "", "serviceprofiles", "")
}
}
}
return errors.New("ServiceProfile CRD not found")
}
// ClusterAccess verifies whether k8sClient is authorized to access all pods in
// all namespaces in the cluster.
func ClusterAccess(k8sClient kubernetes.Interface) error {
return ResourceAuthz(k8sClient, "", "list", "", "", "pods", "")
}