Skip to content
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

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
5caf726
feat: convert grpc-status-details-bin in header to HTTP response body…
monkeyDluffy6017 Jul 10, 2022
cd98b50
change: minor tweaks.
levy001 Aug 10, 2022
0b2999a
test: change the proto uri
levy001 Aug 10, 2022
ebaf9a1
Merge branch 'master' into covert-grpc-status-details-bin-in-header-t…
levy001 Aug 12, 2022
2627139
doc: fix doc lint error.
monkeyDluffy6017 Aug 12, 2022
cf81984
change: minor tweaks.
monkeyDluffy6017 Aug 12, 2022
3680e65
Merge branch 'master' into covert-grpc-status-details-bin-in-header-t…
monkeyDluffy6017 Aug 12, 2022
eab7556
change: minor tweaks.
monkeyDluffy6017 Aug 12, 2022
14bb8fe
change: minor tweaks
monkeyDluffy6017 Aug 12, 2022
082748a
bugfix: recovery pb state after create a new one
levy001 Aug 14, 2022
993f52f
test: remove redundant --- LAST
levy001 Aug 14, 2022
43ba525
change: for test
levy001 Aug 14, 2022
13d4556
change: for test
levy001 Aug 14, 2022
f1f4fa2
change: use grpc_status.proto which is lua string
monkeyDluffy6017 Aug 15, 2022
ebb6ec2
Merge branch 'master' into covert-grpc-status-details-bin-in-header-t…
levy001 Aug 15, 2022
c631ef0
change: for test
levy001 Aug 15, 2022
3721e7d
change: use permalink
monkeyDluffy6017 Aug 16, 2022
da6ffef
Merge branch 'covert-grpc-status-details-bin-in-header-to-http-respon…
monkeyDluffy6017 Aug 16, 2022
2787472
change: resolve code lint error
monkeyDluffy6017 Aug 16, 2022
48871b7
change: resolve review comment
monkeyDluffy6017 Aug 16, 2022
ac34232
change: optimize error log format
monkeyDluffy6017 Aug 18, 2022
b7fd88b
fix: recovery old pb state after return error
monkeyDluffy6017 Aug 18, 2022
cdfd619
Merge branch 'master' into covert-grpc-status-details-bin-in-header-t…
monkeyDluffy6017 Sep 13, 2022
61b185e
Merge branch 'master' into covert-grpc-status-details-bin-in-header-t…
monkeyDluffy6017 Nov 21, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions apisix/plugins/grpc-transcode.lua
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,18 @@ local schema = {
"disable_hooks",
}
},
show_status_in_body = {
description = "show decoded grpc-status-details-bin in response body",
type = "boolean",
default = false
},
-- https://github.com/googleapis/googleapis/blob/b7cb84f5d42e6dba0fdcc2d8689313f6a8c9d7b9/
-- google/rpc/status.proto#L46
status_detail_type = {
description = "the message type of the grpc-status-details-bin's details part, "
.. "if not given, the details part will not be decoded",
type = "string",
},
},
additionalProperties = true,
required = { "proto_id", "service", "method" },
Expand Down Expand Up @@ -143,6 +155,7 @@ function _M.access(conf, ctx)
end

ctx.proto_obj = proto_obj

end


Expand All @@ -152,24 +165,27 @@ function _M.header_filter(conf, ctx)
end

ngx.header["Content-Type"] = "application/json"
ngx.header["Trailer"] = {"grpc-status", "grpc-message"}
ngx.header.content_length = nil

local headers = ngx.resp.get_headers()

if headers["grpc-status"] ~= nil and headers["grpc-status"] ~= "0" then
local http_status = status_rel[headers["grpc-status"]]
if http_status ~= nil then
ngx.status = http_status
else
ngx.status = 599
end
return
else
-- The error response body does not contain grpc-status and grpc-message
ngx.header["Trailer"] = {"grpc-status", "grpc-message"}
end

end


function _M.body_filter(conf, ctx)
if ngx.status >= 300 then
if ngx.status >= 300 and not conf.show_status_in_body then
return
end

Expand All @@ -178,7 +194,8 @@ function _M.body_filter(conf, ctx)
return
end

local err = response(ctx, proto_obj, conf.service, conf.method, conf.pb_option)
local err = response(ctx, proto_obj, conf.service, conf.method, conf.pb_option,
conf.show_status_in_body, conf.status_detail_type)
if err then
core.log.error("transform response error: ", err)
return
Expand Down
100 changes: 100 additions & 0 deletions apisix/plugins/grpc-transcode/proto.lua
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,97 @@ 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 the proto file when using a relative address
-- after luarocks install apisix
local ok, err = status_protoc:load(grpc_status_proto, "grpc_status.proto")
if not ok then
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should set the old_pb_state back when the error happened?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

status_protoc:reset()
pb.state(old_pb_state)
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", {
Expand All @@ -168,6 +259,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


Expand Down
92 changes: 85 additions & 7 deletions apisix/plugins/grpc-transcode/response.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,94 @@
-- 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 err_msg

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
err_msg = "grpc-status-details-bin is not base64 format"
ngx.arg[1] = err_msg
return err_msg
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)
Copy link
Member

Choose a reason for hiding this comment

The 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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

pb.state(old_pb_state)
if not ok then
err_msg = "failed to call pb.decode to decode grpc-status-details-bin"
ngx.arg[1] = err_msg
return err_msg .. ", err: " .. decoded_grpc_status
end

if not decoded_grpc_status then
err_msg = "failed to decode grpc-status-details-bin"
ngx.arg[1] = err_msg
return err_msg
end

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
err_msg = "failed to call pb.decode to decode details in "
.. "grpc-status-details-bin"
ngx.arg[1] = err_msg
return err_msg .. ", err: " .. err_or_value
end

if not err_or_value then
err_msg = "failed to decode details in grpc-status-details-bin"
ngx.arg[1] = err_msg
return err_msg
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
err_msg = "failed to json_encode response body"
ngx.arg[1] = err_msg
return err_msg .. ", 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
Expand All @@ -44,17 +120,19 @@ return function(ctx, proto, service, method, pb_option)

util.set_options(proto, pb_option)

local err_msg
local decoded = pb.decode(m.output_type, buffer)
if not decoded then
ngx.arg[1] = "failed to decode response data by protobuf"
return "failed to decode response data by protobuf"
err_msg = "failed to decode response data by protobuf"
ngx.arg[1] = err_msg
return err_msg
end

local response, err = core.json.encode(decoded)
if not response then
core.log.error("failed to call json_encode data: ", err)
response = "failed to json_encode response body"
return response
err_msg = "failed to json_encode response body"
ngx.arg[1] = err_msg
return err_msg .. ", err: " .. err
end

ngx.arg[1] = response
Expand Down
Loading