forked from casbin/k8s-authz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
90 lines (80 loc) · 2.32 KB
/
server.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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"github.com/casbin/casbin/v2"
"github.com/golang/glog"
"k8s.io/api/admission/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
type CasbinServerHandler struct {
}
var (
operation_name string
)
func (cs *CasbinServerHandler) serve(w http.ResponseWriter, r *http.Request) {
var body []byte
if r.Body != nil {
if data, err := ioutil.ReadAll(r.Body); err == nil {
body = data
}
}
if len(body) == 0 {
glog.Error("empty body")
http.Error(w, "empty body", http.StatusBadRequest)
return
}
glog.Info("Received request")
if r.URL.Path != "/validate" {
glog.Error("no validate")
http.Error(w, "no validate", http.StatusBadRequest)
return
}
arRequest := v1.AdmissionRequest{}
if err := json.Unmarshal(body, &arRequest); err != nil {
glog.Error("incorrect body")
http.Error(w, "incorrect body", http.StatusBadRequest)
}
raw := v1.AdmissionReview{}.Request.Object.Raw
if err := json.Unmarshal([]byte(arRequest.Operation), &operation_name); err != nil {
glog.Error("incorrect body")
http.Error(w, "incorrect body", http.StatusBadRequest)
}
user := arRequest.UserInfo.Username
if err := json.Unmarshal(raw, &user); err != nil {
glog.Error("error deserializing User name")
return
}
if err := json.Unmarshal(raw, &operation_name); err != nil {
glog.Error("error deserializing Operation name")
return
}
e, err := casbin.NewEnforcer("./example/model.conf", "./example/policy.csv")
if err != nil {
glog.Errorf("Filed to load the policies: %v", err)
return
}
arReview := v1.AdmissionReview{}
arReview.Response = &v1.AdmissionResponse{
UID: arReview.Request.UID,
Allowed: true,
}
if !e.HasPermissionForUser(user, operation_name) {
arReview.Response.Allowed = false
arReview.Response.Result = &metav1.Status{
Message: " You are not authorized to perform any operations on these pods!",
}
}
resp, err := json.Marshal(arReview)
if err != nil {
glog.Errorf("Can't encode response: %v", err)
http.Error(w, fmt.Sprintf("could not encode response: %v", err), http.StatusInternalServerError)
}
glog.Info("Ready to write response ...")
if _, err := w.Write(resp); err != nil {
glog.Errorf("Can't write response: %v", err)
http.Error(w, fmt.Sprintf("could not write response: %v", err), http.StatusInternalServerError)
}
}