blaisewang commented on code in PR #13763:
URL: https://github.com/apache/apisix/pull/13763#discussion_r3793586323
##########
apisix/plugins/chaitin-waf.lua:
##########
@@ -273,30 +300,35 @@ local function get_conf(conf, metadata)
real_client_ip = true,
}
- if metadata.config then
- t.connect_timeout = metadata.config.connect_timeout
- t.send_timeout = metadata.config.send_timeout
- t.read_timeout = metadata.config.read_timeout
- t.req_body_size = metadata.config.req_body_size
- t.keepalive_size = metadata.config.keepalive_size
- t.keepalive_timeout = metadata.config.keepalive_timeout
- if metadata.config.real_client_ip ~= nil then
- t.real_client_ip = metadata.config.real_client_ip
+ local function apply(config)
+ if not config then
+ return
end
- end
- if conf.config then
- t.connect_timeout = conf.config.connect_timeout
- t.send_timeout = conf.config.send_timeout
- t.read_timeout = conf.config.read_timeout
- t.req_body_size = conf.config.req_body_size
- t.keepalive_size = conf.config.keepalive_size
- t.keepalive_timeout = conf.config.keepalive_timeout
- if conf.config.real_client_ip ~= nil then
- t.real_client_ip = conf.config.real_client_ip
+ t.connect_timeout = config.connect_timeout
Review Comment:
done
##########
t/plugin/chaitin-waf-log-resp.t:
##########
@@ -0,0 +1,471 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+use t::APISIX 'no_plan';
+
+repeat_each(1);
+no_long_string();
+no_root_location();
+
+add_block_preprocessor(sub {
+ my ($block) = @_;
+
+ # pass_keepalive() keeps the connection usable for the response report,
+ # which the client sends over the same pooled connection
+ my $handler = $block->waf_server_handler // "pass_keepalive";
+ my $stream_default_server = <<_EOC_;
+ server {
+ listen 8088;
+ content_by_lua_block {
+ require("lib.chaitin_waf_server").$handler()
+ }
+ }
+_EOC_
+
+ $block->set_value("extra_stream_config", $stream_default_server);
+ $block->set_value("stream_conf_enable", 1);
+
+ # setup default conf.yaml
+ my $extra_yaml_config = $block->extra_yaml_config // <<_EOC_;
+apisix:
+ stream_proxy: # TCP/UDP L4 proxy
+ only: true # Enable L4 proxy only without L7 proxy.
+ tcp:
+ - addr: 9100 # Set the TCP proxy listening ports.
+ tls: true
+ - addr: "127.0.0.1:9101"
+ udp: # Set the UDP proxy listening ports.
+ - 9200
+ - "127.0.0.1:9201"
+plugins:
+ - chaitin-waf
+_EOC_
+
+ $block->set_value("extra_yaml_config", $extra_yaml_config);
+
+ if (!$block->request) {
+ # use /do instead of /t because stream server will inject a default /t
location
+ $block->set_value("request", "GET /do");
+ }
+
+ if ((!defined $block->error_log) && (!defined $block->no_error_log)) {
+ $block->set_value("no_error_log", "[error]");
+ }
+});
+
+run_tests;
+
+__DATA__
+
+=== TEST 1: configure the WAF service
+--- config
+ location /do {
+ content_by_lua_block {
+ local t = require("lib.test_admin").test
+ local code, body = t('/apisix/admin/plugin_metadata/chaitin-waf',
+ ngx.HTTP_PUT,
+ [[{
+ "nodes": [
+ {
+ "host": "127.0.0.1",
+ "port": 8088
+ }
+ ]
+ }]]
+ )
+ if code >= 300 then
+ ngx.status = code
+ return ngx.print(body)
+ end
+ ngx.say("passed")
+ }
+ }
+--- response_body
+passed
+
+
+
+=== TEST 2: the response is reported to the WAF service
+--- config
+ location /do {
+ content_by_lua_block {
+ local t = require("lib.test_admin").test
+ local http = require("resty.http")
+
+ local code, body = t('/apisix/admin/routes/1',
+ ngx.HTTP_PUT,
+ [[{
+ "methods": ["GET"],
+ "plugins": {
+ "chaitin-waf": {
+ "config": {
+ "log_resp": true
+ }
+ }
+ },
+ "upstream": {
+ "nodes": {
+ "127.0.0.1:1980": 1
+ },
+ "type": "roundrobin"
+ },
+ "uri": "/*"
+ }]]
+ )
+ if code >= 300 then
+ ngx.status = code
+ return ngx.print(body)
+ end
+
+ local httpc = http.new()
+ local res, err = httpc:request_uri("http://127.0.0.1:1984/hello")
+ if not res then
+ return ngx.say("request failed: ", err)
+ end
+ ngx.say("upstream response: ", res.body)
+ ngx.say("waf header: ", res.headers["X-APISIX-CHAITIN-WAF"])
+
+ -- give the timer that reports the response a chance to run
+ ngx.sleep(0.3)
+ }
+ }
+--- response_body
+upstream response: hello world
Review Comment:
done
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]