Kong / kong-python-pdk

Write Kong plugins in Python (Experimental)
Apache License 2.0
40 stars 14 forks source link

response.set_raw_body support #63

Open mpromny opened 2 years ago

mpromny commented 2 years ago

HI, I'd like to ask whether there is a chance that the response.set_raw_body() functionality introduced in Kong 2.8.0 will be added soon.

Kind Regards, Mateusz

fffonion commented 2 years ago

@mpromny All Kong PDK functions supported by Kong are automatically supported in python-pdk; there might be some missing docs (and .pyi interfaces) but you can use them.

mpromny commented 2 years ago

@fffonion Again, thank you for your immediate help. It works as you said.

mpromny commented 2 years ago

Hi @fffonion ,

Would you please help me once more? Unfortunately, I am forced to reopen this issue and ask for some guidelines or clarification on what I am doing wrong.

Requirements:

Current Env:

Current Status:

Code:

    def rewrite(self, kong: Kong):
        kong.service.request.enable_buffering()

    def access (self, kong: Kong):
        try:
            if true:
                kong.service.request.enable_buffering()
                self.transform_request_body(kong, kong.request.get_body())
        except Exception as err:
            kong.log.err(f"TRANSFORMATION - error: {err}")
            return self.error_response(kong, "Transformation issue")

    def body_filter(self, kong: Kong):
        body0 = kong.response.get_raw_body()
        body1 = kong.service.response.get_body()
        body2 = kong.service.response.get_raw_body()
        kong.response.set_raw_body("{}")
        kong.log.debug(f"PHASE body_filter")
        kong.response.exit(404, "body_filter phase")

Issue/Questions:

1) I cannot activate body_filter phase. I would ask for any tips on what I might be doing wrong. Based on the documentation:

Plugin needs to call kong.service.request.enable_buffering() on `rewrite` or `access` phase prior calling this function.

Clarification:

2) I found information that kong.service.request.enable_buffering() works only for http/1.1. Therefore I assume that get/set response body will not work with istio/envoy which uses http2. Would you please confirm that?

Kind regards, Mateusz

fffonion commented 2 years ago

Hi @mpromny there're two factors in play here:

mpromny commented 2 years ago

Hi @fffonion,

1) Unfortunately, I was not able to get the response body in response phase

I tested the below pasted code:

    def response(self, kong: Kong):
        body = kong.service.response.get_raw_body()

        kong.log.err(f" { body[0]} ")
        kong.response.exit(404, "body_filter phase")

and was able only to get/see request body instead of response body:

Request Body:
{"mat": "test"}

2) Lua plugins

May I ask you to confirm, if I understand correctly:

We have to use "lua plugins" if we want to:

3) I would ask for some guidelines/advice on how you think it would be best to implement response/request body processing in Kong. Do you think that there is any chance that access to response body will be easier in the near future?