-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
authz.go
125 lines (106 loc) · 2.92 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package main
import (
"flag"
"fmt"
"log"
"net"
"os"
"strings"
"encoding/base64"
"golang.org/x/net/context"
"google.golang.org/grpc"
"github.com/casbin/casbin"
rpcstatus "google.golang.org/genproto/googleapis/rpc/status"
envoy_type "github.com/envoyproxy/go-control-plane/envoy/type/v3"
auth "github.com/envoyproxy/go-control-plane/envoy/service/auth/v3"
core "github.com/envoyproxy/go-control-plane/envoy/config/core/v3"
"github.com/gogo/googleapis/google/rpc"
)
var (
grpcport = flag.String("grpc", "9000", "grpcport")
)
type AuthorizationServer struct{}
func (a *AuthorizationServer) Check(ctx context.Context, req *auth.CheckRequest) (*auth.CheckResponse, error) {
log.Println(">>> Server Performing authorization check!")
e:= casbin.NewEnforcer("./example/model.conf", "./example/policy.csv")
method := req.Attributes.Request.Http.Method
path := req.Attributes.Request.Http.Path
authHeader, ok := req.Attributes.Request.Http.Headers["authorization"]
if !ok {
log.Fatalf("failed to receive headers")
}
var splitToken []string
splitToken = strings.Split(authHeader, "Bearer ")
if len(splitToken) == 2 {
token := splitToken[1]
tokenbyte, err := base64.StdEncoding.DecodeString(token)
if err != nil {
panic("malformed input")
}
tokenStr := string(tokenbyte[:])
tokenvalue := strings.Split(tokenStr, ",")
username := tokenvalue[1]
if e.Enforce(username, path, method) {
return &auth.CheckResponse{
Status: &rpcstatus.Status{
Code: int32(rpc.OK),
},
HttpResponse: &auth.CheckResponse_OkResponse{
OkResponse: &auth.OkHttpResponse{
Headers: []*core.HeaderValueOption{
{
Header: &core.HeaderValue{
Key: "casbin-authorized",
Value: "allowed",
},
},
},
},
},
}, nil
}else {
return &auth.CheckResponse{
Status: &rpcstatus.Status{
Code: int32(rpc.PERMISSION_DENIED),
},
HttpResponse: &auth.CheckResponse_DeniedResponse{
DeniedResponse: &auth.DeniedHttpResponse{
Status: &envoy_type.HttpStatus{
Code: envoy_type.StatusCode_Unauthorized,
},
Body: "PERMISSION_DENIED",
},
},
}, nil
}
}
return &auth.CheckResponse{
Status: &rpcstatus.Status{
Code: int32(rpc.UNAUTHENTICATED),
},
HttpResponse: &auth.CheckResponse_DeniedResponse{
DeniedResponse: &auth.DeniedHttpResponse{
Status: &envoy_type.HttpStatus{
Code: envoy_type.StatusCode_Unauthorized,
},
Body: "Authorization Header malformed or not provided",
},
},
}, nil
}
func main(){
flag.Parse()
if *grpcport == "" {
fmt.Fprintln(os.Stderr, "missing -grpcport flag ")
flag.Usage()
os.Exit(2)
}
lis, err := net.Listen("tcp", *grpcport)
if err != nil {
log.Fatalf("failed to listen: %v", err)
}
s := grpc.NewServer()
auth.RegisterAuthorizationServer(s, &AuthorizationServer{})
log.Printf("Starting gRPC Server at %s", *grpcport)
s.Serve(lis)
}