-
Notifications
You must be signed in to change notification settings - Fork 1
/
json_transcoder_filter.cc
436 lines (358 loc) · 15.6 KB
/
json_transcoder_filter.cc
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
#include "extensions/filters/http/grpc_json_transcoder/json_transcoder_filter.h"
#include "envoy/common/exception.h"
#include "envoy/http/filter.h"
#include <arpa/inet.h>
#include "common/common/assert.h"
#include "common/common/base64.h"
#include "common/common/enum_to_int.h"
#include "common/common/utility.h"
#include "common/filesystem/filesystem_impl.h"
#include "common/http/filter_utility.h"
#include "common/common/empty_string.h"
#include "common/grpc/common.h"
#include "common/http/headers.h"
#include "common/http/utility.h"
#include "common/protobuf/protobuf.h"
#include "google/api/annotations.pb.h"
#include "google/api/http.pb.h"
#include "grpc_transcoding/json_request_translator.h"
#include "grpc_transcoding/path_matcher_utility.h"
#include "grpc_transcoding/response_to_json_translator.h"
using Envoy::Protobuf::DescriptorPool;
using Envoy::Protobuf::FileDescriptor;
using Envoy::Protobuf::FileDescriptorSet;
using Envoy::Protobuf::io::ZeroCopyInputStream;
using Envoy::ProtobufUtil::Status;
using Envoy::ProtobufUtil::error::Code;
using google::grpc::transcoding::JsonRequestTranslator;
using google::grpc::transcoding::PathMatcherBuilder;
using google::grpc::transcoding::PathMatcherUtility;
using google::grpc::transcoding::RequestInfo;
using google::grpc::transcoding::ResponseToJsonTranslator;
using google::grpc::transcoding::Transcoder;
using google::grpc::transcoding::TranscoderInputStream;
namespace Envoy {
namespace Extensions {
namespace HttpFilters {
namespace GrpcJsonTranscoder {
namespace {
// Transcoder:
// https://github.com/grpc-ecosystem/grpc-httpjson-transcoding/blob/master/src/include/grpc_transcoding/transcoder.h
// implementation based on JsonRequestTranslator & ResponseToJsonTranslator
class TranscoderImpl : public Transcoder {
public:
/**
* Construct a transcoder implementation
* @param request_translator a JsonRequestTranslator that does the request translation
* @param response_translator a ResponseToJsonTranslator that does the response translation
*/
TranscoderImpl(std::unique_ptr<JsonRequestTranslator> request_translator,
std::unique_ptr<ResponseToJsonTranslator> response_translator)
: request_translator_(std::move(request_translator)),
response_translator_(std::move(response_translator)),
request_stream_(request_translator_->Output().CreateInputStream()),
response_stream_(response_translator_->CreateInputStream()) {}
// Transcoder
::google::grpc::transcoding::TranscoderInputStream* RequestOutput() override {
return request_stream_.get();
}
ProtobufUtil::Status RequestStatus() override { return request_translator_->Output().Status(); }
ZeroCopyInputStream* ResponseOutput() override { return response_stream_.get(); }
ProtobufUtil::Status ResponseStatus() override { return response_translator_->Status(); }
private:
std::unique_ptr<JsonRequestTranslator> request_translator_;
std::unique_ptr<ResponseToJsonTranslator> response_translator_;
std::unique_ptr<TranscoderInputStream> request_stream_;
std::unique_ptr<TranscoderInputStream> response_stream_;
};
} // namespace
JsonTranscoderConfig::JsonTranscoderConfig(
const envoy::config::filter::http::transcoder::v2::GrpcJsonTranscoder& proto_config) {
FileDescriptorSet descriptor_set;
switch (proto_config.descriptor_set_case()) {
case envoy::config::filter::http::transcoder::v2::GrpcJsonTranscoder::kProtoDescriptor:
if (!descriptor_set.ParseFromString(
Filesystem::fileReadToEnd(proto_config.proto_descriptor()))) {
throw EnvoyException("transcoding_filter: Unable to parse proto descriptor");
}
break;
case envoy::config::filter::http::transcoder::v2::GrpcJsonTranscoder::kProtoDescriptorBin:
if (!descriptor_set.ParseFromString(proto_config.proto_descriptor_bin())) {
throw EnvoyException("transcoding_filter: Unable to parse proto descriptor");
}
break;
default:
NOT_REACHED;
}
for (const auto& file : descriptor_set.file()) {
if (descriptor_pool_.BuildFile(file) == nullptr) {
throw EnvoyException("transcoding_filter: Unable to build proto descriptor pool");
}
}
PathMatcherBuilder<const Protobuf::MethodDescriptor*> pmb;
for (const auto& service_name : proto_config.services()) {
auto service = descriptor_pool_.FindServiceByName(service_name);
if (service == nullptr) {
throw EnvoyException("transcoding_filter: Could not find '" + service_name +
"' in the proto descriptor");
}
for (int i = 0; i < service->method_count(); ++i) {
auto method = service->method(i);
if (!PathMatcherUtility::RegisterByHttpRule(
pmb, method->options().GetExtension(google::api::http), method)) {
throw EnvoyException("transcoding_filter: Cannot register '" + method->full_name() +
"' to path matcher");
}
}
}
path_matcher_ = pmb.Build();
type_helper_.reset(
new google::grpc::transcoding::TypeHelper(Protobuf::util::NewTypeResolverForDescriptorPool(
Grpc::Common::typeUrlPrefix(), &descriptor_pool_)));
const auto print_config = proto_config.print_options();
print_options_.add_whitespace = print_config.add_whitespace();
print_options_.always_print_primitive_fields = print_config.always_print_primitive_fields();
print_options_.always_print_enums_as_ints = print_config.always_print_enums_as_ints();
print_options_.preserve_proto_field_names = print_config.preserve_proto_field_names();
match_incoming_request_route_ = proto_config.match_incoming_request_route();
}
bool JsonTranscoderConfig::matchIncomingRequestInfo() const {
return match_incoming_request_route_;
}
ProtobufUtil::Status JsonTranscoderConfig::createTranscoder(
const Http::HeaderMap& headers, ZeroCopyInputStream& request_input,
google::grpc::transcoding::TranscoderInputStream& response_input,
std::unique_ptr<Transcoder>& transcoder, const Protobuf::MethodDescriptor*& method_descriptor) {
const ProtobufTypes::String method = headers.Method()->value().c_str();
ProtobufTypes::String path = headers.Path()->value().c_str();
ProtobufTypes::String args;
const size_t pos = path.find('?');
if (pos != std::string::npos) {
args = path.substr(pos + 1);
path = path.substr(0, pos);
}
struct RequestInfo request_info;
std::vector<VariableBinding> variable_bindings;
method_descriptor =
path_matcher_->Lookup(method, path, args, &variable_bindings, &request_info.body_field_path);
if (!method_descriptor) {
return ProtobufUtil::Status(Code::NOT_FOUND, "Could not resolve " + path + " to a method");
}
auto status = methodToRequestInfo(method_descriptor, &request_info);
if (!status.ok()) {
return status;
}
for (const auto& binding : variable_bindings) {
google::grpc::transcoding::RequestWeaver::BindingInfo resolved_binding;
status = type_helper_->ResolveFieldPath(*request_info.message_type, binding.field_path,
&resolved_binding.field_path);
if (!status.ok()) {
return status;
}
resolved_binding.value = binding.value;
request_info.variable_bindings.emplace_back(std::move(resolved_binding));
}
std::unique_ptr<JsonRequestTranslator> request_translator{
new JsonRequestTranslator(type_helper_->Resolver(), &request_input, request_info,
method_descriptor->client_streaming(), true)};
const auto response_type_url =
Grpc::Common::typeUrl(method_descriptor->output_type()->full_name());
std::unique_ptr<ResponseToJsonTranslator> response_translator{new ResponseToJsonTranslator(
type_helper_->Resolver(), response_type_url, method_descriptor->server_streaming(),
&response_input, print_options_)};
transcoder.reset(
new TranscoderImpl(std::move(request_translator), std::move(response_translator)));
return ProtobufUtil::Status();
}
ProtobufUtil::Status
JsonTranscoderConfig::methodToRequestInfo(const Protobuf::MethodDescriptor* method,
google::grpc::transcoding::RequestInfo* info) {
auto request_type_url = Grpc::Common::typeUrl(method->input_type()->full_name());
info->message_type = type_helper_->Info()->GetTypeByTypeUrl(request_type_url);
if (info->message_type == nullptr) {
ENVOY_LOG(debug, "Cannot resolve input-type: {}", method->input_type()->full_name());
return ProtobufUtil::Status(Code::NOT_FOUND,
"Could not resolve type: " + method->input_type()->full_name());
}
return ProtobufUtil::Status();
}
JsonTranscoderFilter::JsonTranscoderFilter(JsonTranscoderConfig& config) : config_(config) {}
Http::FilterHeadersStatus JsonTranscoderFilter::decodeHeaders(Http::HeaderMap& headers,
bool end_stream) {
const auto status =
config_.createTranscoder(headers, request_in_, response_in_, transcoder_, method_);
if (!status.ok()) {
// If transcoder couldn't be created, it might be a normal gRPC request, so the filter will
// just pass-through the request to upstream.
return Http::FilterHeadersStatus::Continue;
}
headers.removeContentLength();
headers.insertContentType().value().setReference(Http::Headers::get().ContentTypeValues.Grpc);
headers.insertEnvoyOriginalPath().value(*headers.Path());
headers.insertPath().value("/" + method_->service()->full_name() + "/" + method_->name());
headers.insertMethod().value().setReference(Http::Headers::get().MethodValues.Post);
headers.insertTE().value().setReference(Http::Headers::get().TEValues.Trailers);
if (!config_.matchIncomingRequestInfo()) {
decoder_callbacks_->clearRouteCache();
}
if (end_stream) {
request_in_.finish();
const auto& request_status = transcoder_->RequestStatus();
if (!request_status.ok()) {
ENVOY_LOG(debug, "Transcoding request error {}", request_status.ToString());
error_ = true;
decoder_callbacks_->sendLocalReply(Http::Code::BadRequest, request_status.error_message(),
nullptr);
return Http::FilterHeadersStatus::StopIteration;
}
Buffer::OwnedImpl data;
readToBuffer(*transcoder_->RequestOutput(), data);
if (data.length() > 0) {
decoder_callbacks_->addDecodedData(data, true);
}
}
return Http::FilterHeadersStatus::Continue;
}
Http::FilterDataStatus JsonTranscoderFilter::decodeData(Buffer::Instance& data, bool end_stream) {
ASSERT(!error_);
if (!transcoder_) {
return Http::FilterDataStatus::Continue;
}
request_in_.move(data);
if (end_stream) {
request_in_.finish();
}
readToBuffer(*transcoder_->RequestOutput(), data);
const auto& request_status = transcoder_->RequestStatus();
if (!request_status.ok()) {
ENVOY_LOG(debug, "Transcoding request error {}", request_status.ToString());
error_ = true;
decoder_callbacks_->sendLocalReply(Http::Code::BadRequest, request_status.error_message(),
nullptr);
return Http::FilterDataStatus::StopIterationNoBuffer;
}
return Http::FilterDataStatus::Continue;
}
Http::FilterTrailersStatus JsonTranscoderFilter::decodeTrailers(Http::HeaderMap&) {
ASSERT(!error_);
if (!transcoder_) {
return Http::FilterTrailersStatus::Continue;
}
request_in_.finish();
Buffer::OwnedImpl data;
readToBuffer(*transcoder_->RequestOutput(), data);
if (data.length()) {
decoder_callbacks_->addDecodedData(data, true);
}
return Http::FilterTrailersStatus::Continue;
}
void JsonTranscoderFilter::setDecoderFilterCallbacks(
Http::StreamDecoderFilterCallbacks& callbacks) {
decoder_callbacks_ = &callbacks;
}
Http::FilterHeadersStatus JsonTranscoderFilter::encodeHeaders(Http::HeaderMap& headers,
bool end_stream) {
if (!Grpc::Common::isGrpcResponseHeader(headers, end_stream)) {
error_ = true;
}
if (error_ || !transcoder_) {
return Http::FilterHeadersStatus::Continue;
}
response_headers_ = &headers;
if (end_stream) {
// In gRPC wire protocol, headers frame with end_stream is a trailers-only response.
// The return value from encodeTrailers is ignored since it is always continue.
encodeTrailers(headers);
return Http::FilterHeadersStatus::Continue;
}
// currently hardcoding as Html, later on will change according to the content-type of HttpBody
headers.insertContentType().value().setReference(Http::Headers::get().ContentTypeValues.Html);
if (!method_->server_streaming()) {
return Http::FilterHeadersStatus::StopIteration;
}
return Http::FilterHeadersStatus::Continue;
}
Http::FilterDataStatus JsonTranscoderFilter::encodeData(Buffer::Instance& data, bool end_stream) {
if (error_ || !transcoder_) {
return Http::FilterDataStatus::Continue;
}
// response_in_.move(data);
// if (end_stream) {
// response_in_.finish();
// }
// readToBuffer(*transcoder_->ResponseOutput(), data);
decoder_.decode(data, frames);
if (frames.empty()) {
// We don't have enough data to decode for one single frame, stop iteration until more data
// comes in.
return Http::FilterDataStatus::StopIterationNoBuffer;
}
if (!method_->server_streaming() && !end_stream) {
// Buffer until the response is complete.
return Http::FilterDataStatus::StopIterationAndBuffer;
}
// how to : parse into Httpbody proto message
for (auto& frame : frames) {
Buffer::OwnedImpl temp;
temp.add(&frame.flags_, 1);
const uint32_t length = htonl(frame.length_);
temp.add(&length, 4);
if (frame.length_ > 0) {
temp.add(*frame.data_);
}
data.add(Base64::encode(temp, temp.length()));
}
// TODO(lizan): Check ResponseStatus
return Http::FilterDataStatus::Continue;
}
Http::FilterTrailersStatus JsonTranscoderFilter::encodeTrailers(Http::HeaderMap& trailers) {
if (error_ || !transcoder_) {
return Http::FilterTrailersStatus::Continue;
}
response_in_.finish();
Buffer::OwnedImpl data;
readToBuffer(*transcoder_->ResponseOutput(), data);
if (data.length()) {
encoder_callbacks_->addEncodedData(data, true);
}
if (method_->server_streaming()) {
// For streaming case, the headers are already sent, so just continue here.
return Http::FilterTrailersStatus::Continue;
}
const absl::optional<Grpc::Status::GrpcStatus> grpc_status =
Grpc::Common::getGrpcStatus(trailers);
if (!grpc_status || grpc_status.value() == Grpc::Status::GrpcStatus::InvalidCode) {
response_headers_->Status()->value(enumToInt(Http::Code::ServiceUnavailable));
} else {
response_headers_->Status()->value(Grpc::Utility::grpcToHttpStatus(grpc_status.value()));
response_headers_->insertGrpcStatus().value(enumToInt(grpc_status.value()));
}
const Http::HeaderEntry* grpc_message_header = trailers.GrpcMessage();
if (grpc_message_header) {
response_headers_->insertGrpcMessage().value(*grpc_message_header);
}
response_headers_->insertContentLength().value(
encoder_callbacks_->encodingBuffer() ? encoder_callbacks_->encodingBuffer()->length() : 0);
return Http::FilterTrailersStatus::Continue;
}
void JsonTranscoderFilter::setEncoderFilterCallbacks(
Http::StreamEncoderFilterCallbacks& callbacks) {
encoder_callbacks_ = &callbacks;
}
// TODO(lizan): Incorporate watermarks to bound buffer sizes
bool JsonTranscoderFilter::readToBuffer(Protobuf::io::ZeroCopyInputStream& stream,
Buffer::Instance& data) {
const void* out;
int size;
while (stream.Next(&out, &size)) {
data.add(out, size);
if (size == 0) {
return true;
}
}
return false;
}
} // namespace GrpcJsonTranscoder
} // namespace HttpFilters
} // namespace Extensions
} // namespace Envoy