/
grpc-rls-v2.go
197 lines (168 loc) · 5.31 KB
/
grpc-rls-v2.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package services
import (
// stdlib
"context"
"crypto/tls"
"encoding/json"
"fmt"
"net/http"
"strings"
// third party
"google.golang.org/grpc"
"google.golang.org/protobuf/types/known/wrapperspb"
// first party (protobuf)
core "github.com/emissary-ingress/emissary/v3/pkg/api/envoy/api/v2/core"
pb "github.com/emissary-ingress/emissary/v3/pkg/api/envoy/service/ratelimit/v2"
// first party
"github.com/datawire/dlib/dgroup"
"github.com/datawire/dlib/dhttp"
"github.com/datawire/dlib/dlog"
)
// GRPCRLSV2 server object (all fields are required).
type GRPCRLSV2 struct {
Port int16
Backend string
SecurePort int16
SecureBackend string
Cert string
Key string
ProtocolVersion string
}
// Start initializes the HTTP server.
func (g *GRPCRLSV2) Start(ctx context.Context) <-chan bool {
dlog.Printf(ctx, "GRPCRLSV2: %s listening on %d/%d", g.Backend, g.Port, g.SecurePort)
grpcHandler := grpc.NewServer()
dlog.Printf(ctx, "registering v2 service")
pb.RegisterRateLimitServiceServer(grpcHandler, g)
cer, err := tls.LoadX509KeyPair(g.Cert, g.Key)
if err != nil {
dlog.Error(ctx, err)
panic(err) // TODO: do something better
}
sc := &dhttp.ServerConfig{
Handler: grpcHandler,
TLSConfig: &tls.Config{
Certificates: []tls.Certificate{cer},
},
}
grp := dgroup.NewGroup(ctx, dgroup.GroupConfig{})
grp.Go("cleartext", func(ctx context.Context) error {
return sc.ListenAndServe(ctx, fmt.Sprintf(":%v", g.Port))
})
grp.Go("tls", func(ctx context.Context) error {
return sc.ListenAndServeTLS(ctx, fmt.Sprintf(":%v", g.SecurePort), "", "")
})
dlog.Print(ctx, "starting gRPC rls service")
exited := make(chan bool)
go func() {
if err := grp.Wait(); err != nil {
dlog.Error(ctx, err)
panic(err) // TODO: do something better
}
close(exited)
}()
return exited
}
// Check checks the request object.
func (g *GRPCRLSV2) ShouldRateLimit(ctx context.Context, r *pb.RateLimitRequest) (*pb.RateLimitResponse, error) {
rs := &RLSResponseV2{}
dlog.Printf(ctx, "shouldRateLimit descriptors: %v\n", r.Descriptors)
descEntries := make(map[string]string)
for _, desc := range r.Descriptors {
for _, entry := range desc.Entries {
descEntries[entry.Key] = entry.Value
}
}
// Sets overallCode. If x-ambassador-test-allow is present and has value "true", then
// respond with OK. In any other case, respond with OVER_LIMIT.
if allowValue := descEntries["kat-req-rls-allow"]; allowValue == "true" {
rs.SetOverallCode(pb.RateLimitResponse_OK)
} else {
rs.SetOverallCode(pb.RateLimitResponse_OVER_LIMIT)
// Response headers and body only make sense when the overall code is not OK,
// so we append them here, if they exist.
// Append requested headers.
for _, token := range strings.Split(descEntries["kat-req-rls-headers-append"], ";") {
header := strings.Split(strings.TrimSpace(token), "=")
if len(header) > 1 {
dlog.Printf(ctx, "appending header %s : %s", header[0], header[1])
rs.AddHeader(true, header[0], header[1])
}
}
// Set the content-type header, since we're returning json
rs.AddHeader(true, "content-type", "application/json")
rs.AddHeader(true, "kat-resp-rls-protocol-version", g.ProtocolVersion)
// Sets results body.
results := make(map[string]interface{})
// TODO: Pass back descriptors
results["descriptors"] = ""
results["backend"] = g.Backend
results["status"] = rs.GetOverallCode()
if rs.GetHTTPHeaderMap() != nil {
results["headers"] = *rs.GetHTTPHeaderMap()
}
body, err := json.MarshalIndent(results, "", " ")
if err != nil {
body = []byte(fmt.Sprintf("Error: %v", err))
}
// Sets response body.
dlog.Printf(ctx, "setting response body: %s", string(body))
rs.SetBody(string(body))
}
return rs.GetResponse(), nil
}
// RLSResponseV2 constructs an rls response object.
type RLSResponseV2 struct {
headers []*core.HeaderValueOption
body string
overallCode pb.RateLimitResponse_Code
}
// AddHeader adds a header to the response. When append param is true, Envoy will
// append the value to an existent request header instead of overriding it.
func (r *RLSResponseV2) AddHeader(a bool, k, v string) {
val := &core.HeaderValueOption{
Header: &core.HeaderValue{
Key: k,
Value: v,
},
Append: &wrapperspb.BoolValue{Value: a},
}
r.headers = append(r.headers, val)
}
// GetHTTPHeaderMap returns HTTP header mapping of the response header-options.
func (r *RLSResponseV2) GetHTTPHeaderMap() *http.Header {
h := &http.Header{}
for _, v := range r.headers {
h.Add(v.Header.Key, v.Header.Value)
}
return h
}
// SetBody sets the rls response message body.
func (r *RLSResponseV2) SetBody(s string) {
r.body = s
}
// SetOverallCode sets the rls response HTTP status code.
func (r *RLSResponseV2) SetOverallCode(code pb.RateLimitResponse_Code) {
r.overallCode = code
}
// GetOverallCode returns the rls response HTTP status code.
func (r *RLSResponseV2) GetOverallCode() pb.RateLimitResponse_Code {
return r.overallCode
}
// GetResponse returns the gRPC rls response object.
func (r *RLSResponseV2) GetResponse() *pb.RateLimitResponse {
rs := &pb.RateLimitResponse{}
rs.OverallCode = r.overallCode
for _, h := range r.headers {
hdr := h.Header
if hdr != nil {
rs.Headers = append(rs.Headers,
&core.HeaderValue{
Key: hdr.Key,
Value: hdr.Value,
},
)
}
}
return rs
}