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

bug: the request body is not rewrited in plugin : ext-plugin-post-req #274

Closed
gaoxingliang opened this issue Nov 17, 2023 · 3 comments
Closed

Comments

@gaoxingliang
Copy link

Issue description

I have a env :
wolf --> ext-plugin-post-req (decrypt the request)-> my upstream (python) ---> ext-plugin-post-resp (encrypt the response)

but when I check the content in my upstream, the body is still encrypted.

my post-req filter:
logger.info("input headers:{}, raw input:{}", request.getHeaders(), request.getBody());
        User user = userService.tryFindUser(request.getHeader(Constants.HEADER_USER_ID));
        if (user == null) {
            response.setStatusCode(403);
            response.setBody(ERROR_NOT_FOUND);
            logger.warn("未找到用户:{}", request.getHeaders());
        } else {
            try {
                String decryptedBody = userService.decryptBody(request.getBody(), user);
                request.setBody(decryptedBody);
                request.setHeader(HEADER_REQUESTBODY_ENCRYPTED_FLAG, "true");
                logger.info("DecryptRequestFilter:request:{}, user:{},{}", request.getRequestId(), user.getUserid(), decryptedBody);
            } catch (Exception e) {
                logger.error("decrypt request failure", e);
                response.setStatusCode(400);
                response.setBody(ERROR_DECRYPT_REQUEST_FAILURE);
            }
        }

and related log(you can see the request body is decrypted successfully.)
image

but the log in my python code(it says it's still encrypted )
image

my post-resp filter:
String userId = request.getUpstreamHeaders().get(Constants.HEADER_USER_ID);
        logger.info("Current user id : {}, {}, body from upstream:{}", userId, request.getUpstreamHeaders(), request.getBody());
        User user = userService.tryFindUser(userId);
        if (user == null) {
            response.setStatusCode(403);
            response.setBody(Constants.ERROR_NOT_FOUND);
            logger.warn("not found the user, maybe disabled. wolfuserid: {}", userId);
        } else {
            String encryptedBody = userService.encryptBody(request.getBody(), user);
            response.setBody(encryptedBody);
            response.setHeader(Constants.HEADER_RESPONSEBODY_ENCRYPTED_FLAG, "true");
            response.setStatusCode(request.getUpstreamStatusCode());
            logger.info("EncryptResponseFilter success:user(wolf):{},{}", user.getUserid(), encryptedBody);
        }

Environment

runner:0.0.4

Minimal test code / Steps to reproduce the issue

What's the actual result? (including assertion message & call stack if applicable)

What's the expected result?

the post-req filter should change the body sent to upstream.

@gaoxingliang
Copy link
Author

I found the apisix supports rewrite the request body since 3.6.0. apache/apisix#9990
Does the java plugin runner need any changes to support this?

@gaoxingliang
Copy link
Author

The source code has to be changed to make it work.
Some reference: #254
I have a fully working example at:
https://github.com/gaoxingliang/apisix-java-plugin-runner/blob/encrypt-response/readme-new.md

@monkeyDluffy6017
Copy link

Would you like to create a pr?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants