Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

Callout-based Service Extensions : Is it possible to share data between on_request on_response?

Hello all,

I am currently doing some tests using the Callout-based Service Extensions  with python sdk.

My current uses case:

  • On on_request_headers : I need to perform an API call that will return a 403 or 200
    • If we have a 403 response, we send an imediate_response with the body and headers received from the API 
    • If we have a 200, we let the request hit the origin

This seems to work fine, however:

  • This 200 response from the API returns also a cookie that we need to send back to client.
  • And since we need to send back to the client; we need to use the function on_response_headers after hitting the origin
    • Unfortunately, I was not able to find a way to share data (inside the same request/context/thread) between on_request_headers  and on_response_headers

My question is: do you know if this is possible in this current implementation?

Thanks and best regards

 

 

1 3 358
3 REPLIES 3

Hello @laurodd

I understand that you would like to know if there is a way to share the data between on_request and on_response using Callout-based Service Extensions with python sdk. Please correct me if I misunderstood.

Please be noted that it is possible to share data between the on_request and on_response phases by using Flask HTTP Middleware. The key to sharing data between on_request and on_response phases is to use a context object or a similar mechanism that is accessible throughout the lifecycle of the request which can be achieved by using Flask HTTP Middleware. This allows you to store data during the request handling phase and retrieve it during the response phase, facilitating the sharing of data between these phases.

For more information regarding this, please refer to :

  1. Flask HTTP Middleware
  2. Flask - Store values in memory between requests
  3. Flask Middleware with both Request and Response

I hope the above provided information is helpful.

Thanks & Regards,
Manish Bavireddy. 

Hello @Manish_B  Thanks a lot for your comment! 😀

I was wondering how I could run flask in this context? Can you help me on that?

I ask that because the doc is saying the following:

  • A load balancer communicates with a callout extension by using Envoy's ext proc gRPC API. This API lets the extension service respond to events in the lifecycle of an HTTP request by examining and modifying the headers or the body of the request.

So, following the example "Developing callouts" github page:

  •  We need the app running a gRPC server (because envoy and the LB uses ext_proc gRPC API)
    • if __name__ == '__main__':
        CalloutServer().run()
  • We need to code our logic inside of one of the callbacks available :
    • on_request_headers: Process request headers.
    • on_response_headers: Process response headers.
    • on_request_body: Process request body.
    • on_response_body: Process response body.

So, I don't understand how we could actually use a WSGI(flask) inside the gRPC server and the callbacks.
If you could give more details about it, I would really appreciate it;

thanks and best regards

 

 

 

Hello @laurodd

Thanks for your response.

Yes! your understanding is correct, Flask is an HTTP server, while gRPC is an RPC (Remote Procedure Call) framework that works over HTTP/2. They are fundamentally different in how they handle requests. However, you can integrate them to work together. Please refer to this blog for more information.

In addition to the information provided earlier, you could use metadata to share data between on_request and on_response phases using callout-based service extensions in a gRPC server. gRPC metadata is a key-value pair of data that is sent with initial or final gRPC requests or responses. It is used to provide additional information about the call, such as authentication credentials, tracing information, or custom headers.

For more information, please refer to :

  1. gRPC Metadata
  2. Examples on Metadata

I hope this is helpful!

Thanks & Regards,  
Manish Bavireddy