-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
Copy pathauth.go
31 lines (27 loc) · 928 Bytes
/
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
package auth
import (
"context"
log "github.com/sirupsen/logrus"
auth "k8s.io/api/authorization/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
)
func CanI(ctx context.Context, kubeclientset kubernetes.Interface, verb, resource, namespace, name string) (bool, error) {
logCtx := log.WithFields(log.Fields{"verb": verb, "resource": resource, "namespace": namespace, "name": name})
logCtx.Debug("CanI")
review, err := kubeclientset.AuthorizationV1().SelfSubjectAccessReviews().Create(ctx, &auth.SelfSubjectAccessReview{
Spec: auth.SelfSubjectAccessReviewSpec{
ResourceAttributes: &auth.ResourceAttributes{
Namespace: namespace,
Verb: verb,
Group: "argoproj.io",
Resource: resource,
},
},
}, metav1.CreateOptions{})
if err != nil {
return false, err
}
logCtx.WithField("status", review.Status).Debug("CanI")
return review.Status.Allowed, nil
}