-
Notifications
You must be signed in to change notification settings - Fork 116
/
proxy.go
271 lines (239 loc) · 7.39 KB
/
proxy.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
package proxy
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"regexp"
"strings"
"github.com/golang/protobuf/ptypes/any"
"github.com/uber-go/tally/v4"
"go.uber.org/zap"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/types/known/structpb"
proxyv1cfg "github.com/lyft/clutch/backend/api/config/module/proxy/v1"
proxyv1 "github.com/lyft/clutch/backend/api/proxy/v1"
"github.com/lyft/clutch/backend/module"
)
const (
Name = "clutch.module.proxy"
HostHeaderKey = "Host"
)
func New(cfg *any.Any, log *zap.Logger, scope tally.Scope) (module.Module, error) {
config := &proxyv1cfg.Config{}
err := cfg.UnmarshalTo(config)
if err != nil {
return nil, err
}
err = validateConfigPaths(config)
if err != nil {
return nil, err
}
m := &mod{
client: &http.Client{},
services: config.Services,
logger: log,
scope: scope,
}
return m, nil
}
type mod struct {
client *http.Client
services []*proxyv1cfg.Service
logger *zap.Logger
scope tally.Scope
}
func (m *mod) Register(r module.Registrar) error {
proxyv1.RegisterProxyAPIServer(r.GRPCServer(), m)
return r.RegisterJSONGateway(proxyv1.RegisterProxyAPIHandler)
}
func (m *mod) RequestProxy(ctx context.Context, req *proxyv1.RequestProxyRequest) (*proxyv1.RequestProxyResponse, error) {
isAllowed, err := isAllowedRequest(m.services, req.Service, req.Path, req.HttpMethod)
if err != nil {
m.logger.Error("Unable to parse the configured URL", zap.Error(err))
return nil, fmt.Errorf("unable to parse the configured URL for service [%s]", req.Service)
}
if !isAllowed {
return nil, status.Error(codes.InvalidArgument, "This request is not allowed, check the proxy configuration.")
}
// If its allowed lookup the service
var service *proxyv1cfg.Service
for _, s := range m.services {
if s.Name == req.Service {
service = s
}
}
// Set all additional headers if specified
headers := http.Header{}
for k, v := range service.Headers {
headers.Add(k, v)
}
// Parse the URL by joining both the HOST and PATH specifed by the config
parsedUrl, err := url.Parse(fmt.Sprintf("%s%s", service.Host, req.Path))
if err != nil {
m.logger.Error("Unable to parse the configured URL", zap.Error(err))
return nil, fmt.Errorf("unable to parse the configured URL for service [%s]", service.Name)
}
// Constructing the request object
request := &http.Request{
Method: req.HttpMethod,
URL: parsedUrl,
Header: headers,
}
addExcludedHeaders(request)
if req.Request != nil {
requestJSON, err := protojson.Marshal(req.Request)
if err != nil {
return nil, err
}
buff := bytes.NewBuffer(requestJSON)
request.Body = io.NopCloser(buff)
}
response, err := m.client.Do(request)
if err != nil {
m.scope.Tagged(map[string]string{
"service": service.Name,
"path": req.Path,
}).Counter("request.error").Inc(1)
m.logger.Error("proxy request error", zap.Error(err))
return nil, err
}
m.scope.Tagged(map[string]string{
"service": service.Name,
"path": req.Path,
"status_code": fmt.Sprintf("%d", response.StatusCode),
}).Counter("request").Inc(1)
// Extract headers from response
// TODO: It might make sense to provide a list of allowed headers, as there can be a lot.
resHeaders := make(map[string]*structpb.ListValue, len(response.Header))
for key, headers := range response.Header {
headerValues := make([]*structpb.Value, len(headers))
for i, h := range headers {
headerValues[i] = structpb.NewStringValue(h)
}
resHeaders[key] = &structpb.ListValue{
Values: headerValues,
}
}
proxyResponse := &proxyv1.RequestProxyResponse{
HttpStatus: int32(response.StatusCode),
Headers: resHeaders,
}
var bodyData interface{}
err = json.NewDecoder(response.Body).Decode(&bodyData)
switch {
// There is no body data so do nothing
case err == io.EOF:
case err != nil:
m.logger.Error("Unable to decode response body", zap.Error(err))
return nil, err
default:
bodyStruct, err := structpb.NewValue(bodyData)
if err != nil {
m.logger.Error("Unable to create structpb from body data", zap.Error(err))
return nil, err
}
proxyResponse.Response = bodyStruct
}
return proxyResponse, nil
}
func (m *mod) RequestProxyGet(ctx context.Context, req *proxyv1.RequestProxyGetRequest) (*proxyv1.RequestProxyGetResponse, error) {
// Validate that it's a GET.
if req.HttpMethod != http.MethodGet {
return nil, status.Errorf(codes.InvalidArgument, "non-GET request passed to GET specific endpoint")
}
// Proxy the call to the original proxy method and return the response.
resp, err := m.RequestProxy(ctx, getRequestToRequest(req))
if err != nil {
return nil, err
}
return responseToGetResponse(resp), nil
}
func responseToGetResponse(resp *proxyv1.RequestProxyResponse) *proxyv1.RequestProxyGetResponse {
return &proxyv1.RequestProxyGetResponse{
HttpStatus: resp.HttpStatus,
Headers: resp.Headers,
Response: resp.Response,
}
}
func getRequestToRequest(req *proxyv1.RequestProxyGetRequest) *proxyv1.RequestProxyRequest {
return &proxyv1.RequestProxyRequest{
Service: req.Service,
HttpMethod: req.HttpMethod,
Path: req.Path,
Request: req.Request,
}
}
func isAllowedRequest(services []*proxyv1cfg.Service, service, path, method string) (bool, error) {
for _, s := range services {
if s.Name == service {
for _, ar := range s.AllowedRequests {
switch t := ar.PathType.(type) {
case *proxyv1cfg.AllowRequest_Path:
parsedUrl, err := url.Parse(fmt.Sprintf("%s%s", s.Host, path))
if err != nil {
return false, err
}
if parsedUrl.Path == t.Path && strings.EqualFold(method, ar.Method) {
return true, nil
}
case *proxyv1cfg.AllowRequest_PathRegex:
r, err := regexp.Compile(t.PathRegex)
if err != nil {
return false, err
}
if r.MatchString(path) {
return true, nil
}
default:
return false, fmt.Errorf("path type not supported: %T", t)
}
}
// return early here as were done checking allowed request for this service
return false, nil
}
}
return false, nil
}
/*
For headers that get ignored in the header map, this helper adds their values back to the designated
fields on the Request struct.
Context:
https://github.com/golang/go/issues/29865
https://github.com/golang/go/blob/8c94aa40e6f5e61e8a570e9d20b7d0d4ad8c382d/src/net/http/request.go#L88
*/
// TODO: add the other headers that get excluded from the request
func addExcludedHeaders(request *http.Request) {
// Get() is case insensitive
if hostHeader := request.Header.Get(HostHeaderKey); hostHeader != "" {
request.Host = hostHeader
}
}
func validateConfigPaths(config *proxyv1cfg.Config) error {
for _, service := range config.Services {
for _, ar := range service.AllowedRequests {
switch t := ar.PathType.(type) {
case *proxyv1cfg.AllowRequest_Path:
// For exact path type, validate that string constructs a parsable URL
_, err := url.Parse(fmt.Sprintf("%s%s", service.Host, t.Path))
if err != nil {
return fmt.Errorf("unable to parse the configured URL for service [%s]", service.Name)
}
case *proxyv1cfg.AllowRequest_PathRegex:
// For path regex type, validate that expression can be parsed
_, err := regexp.Compile(t.PathRegex)
if err != nil {
return err
}
default:
return fmt.Errorf("path type not supported: %T", t)
}
}
}
return nil
}