apache / apisix-java-plugin-runner

APISIX Plugin Runner in Java
https://apisix.apache.org/
Apache License 2.0
130 stars 97 forks source link

help request: How to dynamic add/remove headers in "ext-plugin" with Java Runner #219

Open zhlu001 opened 1 year ago

zhlu001 commented 1 year ago

Description

Is it possible to dynamic to add headers in the ext-plugin (We implement a java based plugin with "apisix-runner-starter, 0.4.0" )? the methods we tried this method: public void filter(HttpRequest request, HttpResponse response, PluginFilterChain chain ... request.getHeaders().put("abc","abc"); request.getHeaders().put("567","565"); ...

Expectation: upstream api receives the combined headers (original + dynamic).
client 10 headers --> apisix ext-plugin(java runner) add 2 headers=> 12 headers -> upstream, 12 headers

Actual upstream api still receives the original headers client 10 headers --> apisix ext-plugin(java runner) add 2 headers=> 12 headers -> upstream, 10 headers

We noticed that line 223 in the "RpcCallHandler" in the "org.apache.apisix.plugin.runner.handler" ;package still using the original request, even the ext-plugin add or remove the header

Typically, data transmission after plugin should expose interface to implementer. but currently, plugin only could change the header value, could not dynamically add / remove headers

thx

Environment

tzssangglass commented 1 year ago

ref: https://github.com/apache/apisix-java-plugin-runner/blob/e18da5ca41607e7a6f79b53d9c1c85f0708025f4/sample/src/main/java/org/apache/apisix/plugin/runner/filter/RewriteRequestDemoFilter.java#L79

use request.setHeader

zhlu001 commented 1 year ago

Hi tzssanglass, Thanks for your reply, we've tried. it doesn't work. client (original 10 header)--> plugin (add 2 headers) -> upstream(still 10 headers)

we debugged following. in the plugin operated header does not impact original headers, request.setHeader request.getHeaders().put() request.getHeaders().clear()

In above debugger info, We noticed that line 223 in the "RpcCallHandler" in the "org.apache.apisix.plugin.runner.handler";package still using the original request, even the ext-plugin add or remove the header

tzssangglass commented 1 year ago

I can't reproduce your problem.

filter:

package com.example.demo;

import org.apache.apisix.plugin.runner.HttpRequest;
import org.apache.apisix.plugin.runner.HttpResponse;
import org.apache.apisix.plugin.runner.filter.PluginFilter;
import org.apache.apisix.plugin.runner.filter.PluginFilterChain;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

@Component
public class DemoFilter implements PluginFilter {
    private final Logger logger = LoggerFactory.getLogger(DemoFilter.class);

    @Override
    public String name() {
        return "DemoFilter";
    }

    @Override
    public void filter(HttpRequest request, HttpResponse response, PluginFilterChain chain) {
        logger.warn("DemoFilter is running");
        request.setHeader("X-APISIX-Plugin-Runner", "0.4.0");
        chain.filter(request, response);
    }
}

route:

curl -i http://127.0.0.1:9080/apisix/admin/routes/1  -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
{
    "uri": "/get",
    "plugins": {
        "ext-plugin-pre-req": {
            "conf" : [
                {"name": "DemoFilter", "value": "{\"enable\":\"feature\"}"}
            ]
        }
    },
    "upstream": {
        "type": "roundrobin",
        "nodes": {
            "httpbin.org:80": 1
        }
    }
}'

test:

curl http://127.0.0.1:9080/get
{
  "args": {},
  "headers": {
    "Accept": "*/*",
    "Host": "127.0.0.1",
    "User-Agent": "curl/7.79.1",
    "X-Apisix-Plugin-Runner": "0.4.0",
    "X-Forwarded-Host": "127.0.0.1"
  },
  "origin": "127.0.0.1, 103.116.72.14",
  "url": "http://127.0.0.1/get"
}

you can see: "X-Apisix-Plugin-Runner": "0.4.0" that upstream receive

zhlu001 commented 1 year ago

Hi,

We use spring boot as receiver with @.***”. Besides, we tested response.setheader(), somehow, it works. But we do not know why.

Thx.

Best regards,

Zhanpeng LU

Business Analytics Asia Pacific (CI/PDD2) Bosch (China) Investment Ltd. | 333 Fuquan (N.) Road | Shanghai 200335 | P.R. CHINA @.**@.> ​ From: tzssangglass @.> Sent: Thursday, December 8, 2022 10:06 To: apache/apisix-java-plugin-runner @.> Cc: LU Zhanpeng (CI/PDD2) @.>; Author @.> Subject: Re: [apache/apisix-java-plugin-runner] help request: How to dynamic add/remove headers in "ext-plugin" with Java Runner (Issue #219)

I can't reproduce your problem.

filter:

package com.example.demo;

import org.apache.apisix.plugin.runner.HttpRequest;

import org.apache.apisix.plugin.runner.HttpResponse;

import org.apache.apisix.plugin.runner.filter.PluginFilter;

import org.apache.apisix.plugin.runner.filter.PluginFilterChain;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import org.springframework.stereotype.Component;

@Component

public class DemoFilter implements PluginFilter {

private final Logger logger = LoggerFactory.getLogger(DemoFilter.class);

@Override

public String name() {

    return "DemoFilter";

}

@Override

public void filter(HttpRequest request, HttpResponse response, PluginFilterChain chain) {

    logger.warn("DemoFilter is running");

    request.setHeader("X-APISIX-Plugin-Runner", "0.4.0");

    chain.filter(request, response);

}

}

route:

curl -i http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '

{

"uri": "/get",

"plugins": {

    "ext-plugin-pre-req": {

        "conf" : [

            {"name": "DemoFilter", "value": "{\"enable\":\"feature\"}"}

        ]

    }

},

"upstream": {

    "type": "roundrobin",

    "nodes": {

        "httpbin.org:80": 1

    }

}

}'

test:

curl http://127.0.0.1:9080/get

{

"args": {},

"headers": {

"Accept": "*/*",

"Host": "127.0.0.1",

"User-Agent": "curl/7.79.1",

"X-Apisix-Plugin-Runner": "0.4.0",

"X-Forwarded-Host": "127.0.0.1"

},

"origin": "127.0.0.1, 103.116.72.14",

"url": "http://127.0.0.1/get"

}

you can see: "X-Apisix-Plugin-Runner": "0.4.0" that upstream receive

— Reply to this email directly, view it on GitHubhttps://eur03.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fapache%2Fapisix-java-plugin-runner%2Fissues%2F219%23issuecomment-1341872721&data=05%7C01%7Czhanpeng.lu%40cn.bosch.com%7C8269ea59b61d41a1da4a08dad8c0b373%7C0ae51e1907c84e4bbb6d648ee58410f4%7C0%7C0%7C638060619398523548%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=2FwWEBMdVOGYp0A38czGP6DxQNMES3S0aoNF%2BngYUdo%3D&reserved=0, or unsubscribehttps://eur03.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fnotifications%2Funsubscribe-auth%2FARTFJ6ZIUWTWPDGFTBJJY23WME67BANCNFSM6AAAAAASWZ2MKQ&data=05%7C01%7Czhanpeng.lu%40cn.bosch.com%7C8269ea59b61d41a1da4a08dad8c0b373%7C0ae51e1907c84e4bbb6d648ee58410f4%7C0%7C0%7C638060619398523548%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=js%2FscMNX3FvzNjLIESv6JuJkt6WfGyyabSbmBiHNqnY%3D&reserved=0. You are receiving this because you authored the thread.Message ID: @.**@.>>

tzssangglass commented 1 year ago

Besides, we tested response.setheader(), somehow, it works. But we do not know why.

You can test this in debug mode to make sure the request is entering the filter.

zhlu001 commented 1 year ago

Client (postman ) --> plugin --> upstream (Spring boot Rest ) @.***

@.***

Best regards,

Zhanpeng LU

Business Analytics Asia Pacific (CI/PDD2) Bosch (China) Investment Ltd. | 333 Fuquan (N.) Road | Shanghai 200335 | P.R. CHINA @.**@.> ​ From: tzssangglass @.> Sent: Thursday, December 8, 2022 10:50 To: apache/apisix-java-plugin-runner @.> Cc: LU Zhanpeng (CI/PDD2) @.>; Author @.> Subject: Re: [apache/apisix-java-plugin-runner] help request: How to dynamic add/remove headers in "ext-plugin" with Java Runner (Issue #219)

Besides, we tested response.setheader(), somehow, it works. But we do not know why.

You can test this in debug mode to make sure the request is entering the filter.

— Reply to this email directly, view it on GitHubhttps://eur03.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fapache%2Fapisix-java-plugin-runner%2Fissues%2F219%23issuecomment-1341906325&data=05%7C01%7Czhanpeng.lu%40cn.bosch.com%7C3312c971c05341fc98eb08dad8c6d5c9%7C0ae51e1907c84e4bbb6d648ee58410f4%7C0%7C0%7C638060645745516713%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=Nx14eeg4LasbbRmk9D4%2BGZUfNFDtYlr7wduRhurpOwk%3D&reserved=0, or unsubscribehttps://eur03.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fnotifications%2Funsubscribe-auth%2FARTFJ623PE6MQVHFIHEVXUTWMFEDXANCNFSM6AAAAAASWZ2MKQ&data=05%7C01%7Czhanpeng.lu%40cn.bosch.com%7C3312c971c05341fc98eb08dad8c6d5c9%7C0ae51e1907c84e4bbb6d648ee58410f4%7C0%7C0%7C638060645745516713%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=EMjbIvdgQ6lG0MEOmF%2FpJykQJmLANr3x%2FvMCNFaJMbQ%3D&reserved=0. You are receiving this because you authored the thread.Message ID: @.**@.>>

tzssangglass commented 1 year ago

Such a conversation is meaningless and difficult to carry on. You can try to reproduce the steps I gave above.

and read: https://github.com/apache/apisix-java-plugin-runner/blob/main/docs/en/latest/how-it-works.md#debug