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

gRPC - Request/Response body as byte array/JSON #12626

Closed
ashoke-cube opened this issue Aug 13, 2020 · 7 comments
Closed

gRPC - Request/Response body as byte array/JSON #12626

ashoke-cube opened this issue Aug 13, 2020 · 7 comments
Labels
area/lua stale stalebot believes this issue/PR has not been touched recently

Comments

@ashoke-cube
Copy link

I have set up a lua filter for accessing the request/response details [headers and body] for a gRPC service. I am able to see proper headers data. The request/response data is of the form 'userdata : xxxxxxxxxxx'

For example:

print (request_handle:body()) -> userdata: 0x40dae6a0

  1. Is there a utility to get the data in byte array/JSON format?
  2. I understand the conversion requires .proto files, how do I specify it in the filter?
@dio
Copy link
Member

dio commented Aug 16, 2020

I'm started to think probably we need to consolidate C++ implemented utilities (that is exposed to the Lua world) into a dedicated "wrapper", either to be attached to the stream handle or available globally.

We see e.g.

  1. lua API: add base64Escape function to stream handle #12552 to add base64 encoding helper
  2. lua: timestamp in millis #10329 to add timestamping helper
  3. This issue to add get JSON string from a protobuf message (this request also can be implemented in C++, using MessageUtil::getJsonStringFromMessage)

If attaching to stream handler wrapper is preferred (this is in line with httpCall):

function envoy_on_request(request_handle) {
  request_handle:utility():base64Encode("ok");
  request_handle:utility():currentTimestamp();
  request_handle:utility():getJsonStringFromProtoMessageBuffer(request_handle.body());
}

Or if we prefer it to be available globally, since the functionality doesn't need access to the current stream.

function envoy_on_request(request_handle) {
  envoyBase64Encode("ok");
  envoyCurrentTimestamp();
  envoyGetJsonStringFromProtoMessageBuffer(request_handle.body());
}

cc. @mattklein123 @mpuncel

@mattklein123
Copy link
Member

Sure all of that SGTM @dio

@paawan01
Copy link

Is there anything currently available to achieve this in lua filter ?

@stale
Copy link

stale bot commented Sep 20, 2020

This issue has been automatically marked as stale because it has not had activity in the last 30 days. It will be closed in the next 7 days unless it is tagged "help wanted" or other activity occurs. Thank you for your contributions.

@stale stale bot added the stale stalebot believes this issue/PR has not been touched recently label Sep 20, 2020
@dio
Copy link
Member

dio commented Sep 20, 2020

@paawan01 you can get the data "bytes", via getBytes() API.

request_handle:body() doesn't return a string. It returns a buffer wrapper.

Hence a call to it ends up with a value that looks like userdata: 0x40dae6a0

Try this instead:

local body = request_handle:body()
local bytes = tostring(body:getBytes(0, body:length()))

But yeah you can't really work with the encoded protobuf messages in Lua yet, but you can forward that data to another service.

@stale stale bot removed the stale stalebot believes this issue/PR has not been touched recently label Sep 20, 2020
@github-actions
Copy link

github-actions bot commented Dec 9, 2020

This issue has been automatically marked as stale because it has not had activity in the last 30 days. It will be closed in the next 7 days unless it is tagged "help wanted" or "no stalebot" or other activity occurs. Thank you for your contributions.

@github-actions github-actions bot added the stale stalebot believes this issue/PR has not been touched recently label Dec 9, 2020
@github-actions
Copy link

This issue has been automatically closed because it has not had activity in the last 37 days. If this issue is still valid, please ping a maintainer and ask them to label it as "help wanted" or "no stalebot". Thank you for your contributions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/lua stale stalebot believes this issue/PR has not been touched recently
Projects
None yet
Development

No branches or pull requests

4 participants