-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathmain.go
85 lines (75 loc) · 2.21 KB
/
main.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
package main
import (
"bytes"
"context"
"encoding/json"
"fmt"
"log"
"net"
"github.com/envoyproxy/go-control-plane/envoy/api/v2/core"
auth "github.com/envoyproxy/go-control-plane/envoy/service/auth/v2"
"github.com/golang/protobuf/jsonpb"
"google.golang.org/grpc"
rpc "istio.io/gogo-genproto/googleapis/google/rpc"
)
// empty struct because this isn't a fancy example
type AuthorizationServer struct{}
// inject a header that can be used for future rate limiting
func (a *AuthorizationServer) Check(ctx context.Context, req *auth.CheckRequest) (*auth.CheckResponse, error) {
httpRequest := req.Attributes.Request.Http
socketAddress := req.Attributes.Source.Address.GetSocketAddress()
fmt.Printf("Source IP:port %s:%d\n", socketAddress.GetAddress(), socketAddress.GetPortValue())
marshaler := jsonpb.Marshaler{}
jsonString, _ := marshaler.MarshalToString(httpRequest)
var out bytes.Buffer
err := json.Indent(&out, []byte(jsonString), "", " ")
if err == nil {
println(out.String())
return &auth.CheckResponse{
Status: &rpc.Status{
Code: int32(rpc.OK),
},
HttpResponse: &auth.CheckResponse_OkResponse{
OkResponse: &auth.OkHttpResponse{
// https://www.envoyproxy.io/docs/envoy/latest/api-v2/service/auth/v2/external_auth.proto#service-auth-v2-checkrequest
Headers: []*core.HeaderValueOption{
{
Header: &core.HeaderValue{
Key: "x-ext-auth-id",
Value: "curl",
},
},
{
Header: &core.HeaderValue{
Key: "x-ext-auth-id-user",
Value: "bob",
},
},
},
},
},
}, nil
} else {
println("Error encoding JSON: " + err.Error())
return &auth.CheckResponse{
Status: &rpc.Status{
Code: int32(rpc.PERMISSION_DENIED),
},
HttpResponse: &auth.CheckResponse_DeniedResponse{},
}, nil
}
}
func main() {
// create a TCP listener on port 5010
lis, err := net.Listen("tcp", ":5010")
if err != nil {
log.Fatalf("failed to listen: %v", err)
}
log.Printf("listening on %s", lis.Addr())
grpcServer := grpc.NewServer()
authServer := &AuthorizationServer{}
auth.RegisterAuthorizationServer(grpcServer, authServer)
if err := grpcServer.Serve(lis); err != nil {
log.Fatalf("Failed to start server: %v", err)
}
}