-
Notifications
You must be signed in to change notification settings - Fork 681
/
grpc-rls.go
200 lines (171 loc) · 5.32 KB
/
grpc-rls.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
198
199
200
package services
import (
// stdlib
"context"
"crypto/tls"
"encoding/json"
"fmt"
"log"
"net/http"
"strings"
// third party
"github.com/golang/protobuf/ptypes/wrappers"
"google.golang.org/grpc"
// first party (protobuf)
core "github.com/datawire/ambassador/pkg/api/envoy/api/v2/core"
pb "github.com/datawire/ambassador/pkg/api/envoy/service/ratelimit/v2"
pb_legacy "github.com/datawire/ambassador/pkg/api/pb/lyft/ratelimit"
// first party
"github.com/datawire/dlib/dgroup"
"github.com/datawire/dlib/dhttp"
)
// GRPCRLS server object (all fields are required).
type GRPCRLS struct {
Port int16
Backend string
SecurePort int16
SecureBackend string
Cert string
Key string
ProtocolVersion string
}
// Start initializes the HTTP server.
func (g *GRPCRLS) Start() <-chan bool {
log.Printf("GRPCRLS: %s listening on %d/%d", g.Backend, g.Port, g.SecurePort)
grpcHandler := grpc.NewServer()
if g.ProtocolVersion != "v2" {
log.Printf("registering v2alpha service")
pb_legacy.RegisterRateLimitServiceServer(grpcHandler, g)
} else {
log.Printf("registering v2 service")
pb.RegisterRateLimitServiceServer(grpcHandler, g)
}
cer, err := tls.LoadX509KeyPair(g.Cert, g.Key)
if err != nil {
log.Fatal(err)
}
sc := &dhttp.ServerConfig{
Handler: grpcHandler,
TLSConfig: &tls.Config{
Certificates: []tls.Certificate{cer},
},
}
grp := dgroup.NewGroup(context.TODO(), 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), "", "")
})
log.Print("starting gRPC rls service")
exited := make(chan bool)
go func() {
log.Fatal(grp.Wait())
close(exited)
}()
return exited
}
// Check checks the request object.
func (g *GRPCRLS) ShouldRateLimit(ctx context.Context, r *pb.RateLimitRequest) (*pb.RateLimitResponse, error) {
rs := &RLSResponse{}
log.Printf("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["x-ambassador-test-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["x-ambassador-test-headers-append"], ";") {
header := strings.Split(strings.TrimSpace(token), "=")
if len(header) > 1 {
log.Printf("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, "x-grpc-service-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.
log.Printf("setting response body: %s", string(body))
rs.SetBody(string(body))
}
return rs.GetResponse(), nil
}
// RLSResponse constructs an rls response object.
type RLSResponse 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 *RLSResponse) AddHeader(a bool, k, v string) {
val := &core.HeaderValueOption{
Header: &core.HeaderValue{
Key: k,
Value: v,
},
Append: &wrappers.BoolValue{Value: a},
}
r.headers = append(r.headers, val)
}
// GetHTTPHeaderMap returns HTTP header mapping of the response header-options.
func (r *RLSResponse) 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 *RLSResponse) SetBody(s string) {
r.body = s
}
// SetOverallCode sets the rls response HTTP status code.
func (r *RLSResponse) SetOverallCode(code pb.RateLimitResponse_Code) {
r.overallCode = code
}
// GetOverallCode returns the rls response HTTP status code.
func (r *RLSResponse) GetOverallCode() pb.RateLimitResponse_Code {
return r.overallCode
}
// GetResponse returns the gRPC rls response object.
func (r *RLSResponse) GetResponse() *pb.RateLimitResponse {
rs := &pb.RateLimitResponse{}
rs.OverallCode = r.overallCode
rs.RawBody = []byte(r.body)
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
}