-
Notifications
You must be signed in to change notification settings - Fork 2.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: convert grpc-status-details-bin in header to HTTP response body… #7639
Changes from 19 commits
5caf726
cd98b50
0b2999a
ebaf9a1
2627139
cf81984
3680e65
eab7556
14bb8fe
082748a
993f52f
43ba525
13d4556
f1f4fa2
ebb6ec2
c631ef0
3721e7d
da6ffef
2787472
48871b7
ac34232
b7fd88b
cdfd619
61b185e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -157,6 +157,96 @@ function _M.protos() | |
end | ||
|
||
|
||
local grpc_status_proto = [[ | ||
syntax = "proto3"; | ||
|
||
package grpc_status; | ||
|
||
message Any { | ||
// A URL/resource name that uniquely identifies the type of the serialized | ||
// protocol buffer message. This string must contain at least | ||
// one "/" character. The last segment of the URL's path must represent | ||
// the fully qualified name of the type (as in | ||
// `path/google.protobuf.Duration`). The name should be in a canonical form | ||
// (e.g., leading "." is not accepted). | ||
// | ||
// In practice, teams usually precompile into the binary all types that they | ||
// expect it to use in the context of Any. However, for URLs which use the | ||
// scheme `http`, `https`, or no scheme, one can optionally set up a type | ||
// server that maps type URLs to message definitions as follows: | ||
// | ||
// * If no scheme is provided, `https` is assumed. | ||
// * An HTTP GET on the URL must yield a [google.protobuf.Type][] | ||
// value in binary format, or produce an error. | ||
// * Applications are allowed to cache lookup results based on the | ||
// URL, or have them precompiled into a binary to avoid any | ||
// lookup. Therefore, binary compatibility needs to be preserved | ||
// on changes to types. (Use versioned type names to manage | ||
// breaking changes.) | ||
// | ||
// Note: this functionality is not currently available in the official | ||
// protobuf release, and it is not used for type URLs beginning with | ||
// type.googleapis.com. | ||
// | ||
// Schemes other than `http`, `https` (or the empty scheme) might be | ||
// used with implementation specific semantics. | ||
// | ||
string type_url = 1; | ||
|
||
// Must be a valid serialized protocol buffer of the above specified type. | ||
bytes value = 2; | ||
} | ||
|
||
// The `Status` type defines a logical error model that is suitable for | ||
// different programming environments, including REST APIs and RPC APIs. It is | ||
// used by [gRPC](https://github.com/grpc). Each `Status` message contains | ||
// three pieces of data: error code, error message, and error details. | ||
// | ||
// You can find out more about this error model and how to work with it in the | ||
// [API Design Guide](https://cloud.google.com/apis/design/errors). | ||
message ErrorStatus { | ||
// The status code, which should be an enum value of [google.rpc.Code][google.rpc.Code]. | ||
int32 code = 1; | ||
|
||
// A developer-facing error message, which should be in English. Any | ||
// user-facing error message should be localized and sent in the | ||
// [google.rpc.Status.details][google.rpc.Status.details] field, or localized by the client. | ||
string message = 2; | ||
|
||
// A list of messages that carry the error details. There is a common set of | ||
// message types for APIs to use. | ||
repeated Any details = 3; | ||
} | ||
]] | ||
|
||
|
||
local status_pb_state | ||
local function init_status_pb_state() | ||
if not status_pb_state then | ||
-- clear current pb state | ||
local old_pb_state = pb.state(nil) | ||
|
||
-- initialize protoc compiler | ||
protoc.reload() | ||
local status_protoc = protoc.new() | ||
-- do not use loadfile here, it can not load proto file when use relative address | ||
-- after luarocks install apisix | ||
local ok, err = status_protoc:load(grpc_status_proto, "grpc_status.proto") | ||
if not ok then | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should set the old_pb_state back when the error happened? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
status_protoc:reset() | ||
return "failed to load grpc status protocol: " .. err | ||
end | ||
|
||
status_pb_state = pb.state(old_pb_state) | ||
end | ||
end | ||
|
||
|
||
function _M.fetch_status_pb_state() | ||
return status_pb_state | ||
end | ||
|
||
|
||
function _M.init() | ||
local err | ||
protos, err = core.config.new("/protos", { | ||
|
@@ -168,6 +258,15 @@ function _M.init() | |
err) | ||
return | ||
end | ||
|
||
if not status_pb_state then | ||
err = init_status_pb_state() | ||
if err then | ||
core.log.error("failed to init grpc status proto: ", | ||
err) | ||
return | ||
end | ||
end | ||
end | ||
|
||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,18 +14,89 @@ | |
-- See the License for the specific language governing permissions and | ||
-- limitations under the License. | ||
-- | ||
local util = require("apisix.plugins.grpc-transcode.util") | ||
local util = require("apisix.plugins.grpc-transcode.util") | ||
local grpc_proto = require("apisix.plugins.grpc-transcode.proto") | ||
local core = require("apisix.core") | ||
local pb = require("pb") | ||
local ngx = ngx | ||
local string = string | ||
local ngx_decode_base64 = ngx.decode_base64 | ||
local ipairs = ipairs | ||
local pcall = pcall | ||
|
||
return function(ctx, proto, service, method, pb_option) | ||
|
||
local function handle_error_response(status_detail_type) | ||
local grpc_status = ngx.header["grpc-status-details-bin"] | ||
if grpc_status then | ||
grpc_status = ngx_decode_base64(grpc_status) | ||
if grpc_status == nil then | ||
ngx.arg[1] = "grpc-status-details-bin is not base64 format" | ||
return "grpc-status-details-bin is not base64 format" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can exact the error message to a variable so we don't need to repeat them? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
end | ||
|
||
local status_pb_state = grpc_proto.fetch_status_pb_state() | ||
local old_pb_state = pb.state(status_pb_state) | ||
|
||
local ok, decoded_grpc_status = pcall(pb.decode, "grpc_status.ErrorStatus", grpc_status) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should restore the pb_state here so the error won't pollute the pb_state There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
if not ok then | ||
ngx.arg[1] = "failed to call pb.decode to decode grpc-status-details-bin" | ||
return "failed to call pb.decode to decode grpc-status-details-bin, err: " | ||
.. decoded_grpc_status | ||
end | ||
|
||
if not decoded_grpc_status then | ||
ngx.arg[1] = "failed to decode grpc-status-details-bin" | ||
return "failed to decode grpc-status-details-bin" | ||
end | ||
|
||
pb.state(old_pb_state) | ||
|
||
local details = decoded_grpc_status.details | ||
if status_detail_type and details then | ||
local decoded_details = {} | ||
for _, detail in ipairs(details) do | ||
local ok, err_or_value = pcall(pb.decode, status_detail_type, detail.value) | ||
if not ok then | ||
ngx.arg[1] = "failed to call pb.decode to decode details in " | ||
.. "grpc-status-details-bin" | ||
return "failed to call pb.decode to decode details in " | ||
.. "grpc-status-details-bin, err: " .. err_or_value | ||
end | ||
|
||
if not err_or_value then | ||
ngx.arg[1] = "failed to decode details in grpc-status-details-bin" | ||
return "failed to decode details in grpc-status-details-bin" | ||
end | ||
|
||
core.table.insert(decoded_details, err_or_value) | ||
end | ||
|
||
decoded_grpc_status.details = decoded_details | ||
end | ||
|
||
local resp_body = {error = decoded_grpc_status} | ||
local response, err = core.json.encode(resp_body) | ||
if not response then | ||
ngx.arg[1] = "failed to json_encode response body" | ||
return "failed to json_encode response body, error: " .. err | ||
end | ||
|
||
ngx.arg[1] = response | ||
end | ||
end | ||
|
||
|
||
return function(ctx, proto, service, method, pb_option, show_status_in_body, status_detail_type) | ||
local buffer = core.response.hold_body_chunk(ctx) | ||
if not buffer then | ||
return nil | ||
end | ||
|
||
-- handle error response after the last response chunk | ||
if ngx.status >= 300 and show_status_in_body then | ||
return handle_error_response(status_detail_type) | ||
end | ||
|
||
-- when body has already been read by other plugin | ||
-- the buffer is an empty string | ||
if buffer == "" and ctx.resp_body then | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done