2019-10-31 09:27:28 +08:00
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
#
|
2019-09-10 09:42:55 +08:00
|
|
|
package t::APISIX;
|
2019-04-11 16:53:21 +08:00
|
|
|
|
|
|
|
use lib 'lib';
|
|
|
|
use Cwd qw(cwd);
|
|
|
|
use Test::Nginx::Socket::Lua::Stream -Base;
|
|
|
|
|
2019-06-19 15:51:28 +08:00
|
|
|
repeat_each(1);
|
2019-06-17 23:17:41 +08:00
|
|
|
log_level('info');
|
|
|
|
no_long_string();
|
|
|
|
no_shuffle();
|
2020-11-03 13:46:24 +08:00
|
|
|
no_root_location(); # avoid generated duplicate 'location /'
|
2019-08-21 23:10:34 +08:00
|
|
|
worker_connections(128);
|
2021-03-29 23:38:07 +08:00
|
|
|
master_on();
|
2019-06-17 23:17:41 +08:00
|
|
|
|
2020-02-26 21:26:01 +08:00
|
|
|
my $apisix_home = $ENV{APISIX_HOME} || cwd();
|
2021-01-20 09:47:09 +08:00
|
|
|
my $nginx_binary = $ENV{'TEST_NGINX_BINARY'} || 'nginx';
|
2021-04-29 11:07:05 +08:00
|
|
|
$ENV{TEST_NGINX_HTML_DIR} ||= html_dir();
|
2019-04-11 16:53:21 +08:00
|
|
|
|
|
|
|
sub read_file($) {
|
|
|
|
my $infile = shift;
|
2020-02-26 21:26:01 +08:00
|
|
|
open my $in, "$apisix_home/$infile"
|
2019-04-11 16:53:21 +08:00
|
|
|
or die "cannot open $infile for reading: $!";
|
2020-02-26 21:26:01 +08:00
|
|
|
my $data = do { local $/; <$in> };
|
2019-04-11 16:53:21 +08:00
|
|
|
close $in;
|
2020-02-26 21:26:01 +08:00
|
|
|
$data;
|
2019-04-11 16:53:21 +08:00
|
|
|
}
|
|
|
|
|
2020-03-26 09:48:18 +08:00
|
|
|
sub local_dns_resolver() {
|
|
|
|
open my $in, "/etc/resolv.conf" or die "cannot open /etc/resolv.conf";
|
|
|
|
my @lines = <$in>;
|
|
|
|
my @dns_addrs = ();
|
|
|
|
foreach my $line (@lines){
|
|
|
|
$line =~ m/^nameserver\s+(\d+[.]\d+[.]\d+[.]\d+)\s*$/;
|
|
|
|
if ($1) {
|
|
|
|
push(@dns_addrs, $1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
close($in);
|
|
|
|
return @dns_addrs
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
my $dns_addrs_str = "";
|
|
|
|
my $dns_addrs_tbl_str = "";
|
|
|
|
my $enable_local_dns = $ENV{"ENABLE_LOCAL_DNS"};
|
|
|
|
if ($enable_local_dns) {
|
|
|
|
my @dns_addrs = local_dns_resolver();
|
|
|
|
$dns_addrs_tbl_str = "{";
|
|
|
|
foreach my $addr (@dns_addrs){
|
|
|
|
$dns_addrs_str = "$dns_addrs_str $addr";
|
|
|
|
$dns_addrs_tbl_str = "$dns_addrs_tbl_str\"$addr\", ";
|
|
|
|
}
|
|
|
|
$dns_addrs_tbl_str = "$dns_addrs_tbl_str}";
|
|
|
|
} else {
|
|
|
|
$dns_addrs_str = "8.8.8.8 114.114.114.114";
|
|
|
|
$dns_addrs_tbl_str = "{\"8.8.8.8\", \"114.114.114.114\"}";
|
|
|
|
}
|
2021-02-05 09:00:20 +08:00
|
|
|
my $custom_dns_server = $ENV{"CUSTOM_DNS_SERVER"};
|
|
|
|
if ($custom_dns_server) {
|
|
|
|
$dns_addrs_tbl_str = "{\"$custom_dns_server\"}";
|
|
|
|
}
|
2020-03-26 09:48:18 +08:00
|
|
|
|
|
|
|
|
2020-08-18 20:18:55 +08:00
|
|
|
my $default_yaml_config = read_file("conf/config-default.yaml");
|
2020-11-25 14:11:24 +08:00
|
|
|
# enable example-plugin as some tests require it
|
2020-12-18 08:29:17 +08:00
|
|
|
$default_yaml_config =~ s/# - example-plugin/- example-plugin/;
|
2021-04-07 14:40:28 +08:00
|
|
|
$default_yaml_config =~ s/enable_export_server: true/enable_export_server: false/;
|
2020-11-10 08:46:54 +08:00
|
|
|
|
2020-08-18 20:18:55 +08:00
|
|
|
my $user_yaml_config = read_file("conf/config.yaml");
|
2020-11-20 11:10:01 +08:00
|
|
|
my $ssl_crt = read_file("t/certs/apisix.crt");
|
|
|
|
my $ssl_key = read_file("t/certs/apisix.key");
|
|
|
|
my $ssl_ecc_crt = read_file("t/certs/apisix_ecc.crt");
|
|
|
|
my $ssl_ecc_key = read_file("t/certs/apisix_ecc.key");
|
|
|
|
my $test2_crt = read_file("t/certs/test2.crt");
|
|
|
|
my $test2_key = read_file("t/certs/test2.key");
|
2020-08-18 20:18:55 +08:00
|
|
|
$user_yaml_config = <<_EOC_;
|
|
|
|
apisix:
|
|
|
|
node_listen: 1984
|
|
|
|
stream_proxy:
|
|
|
|
tcp:
|
|
|
|
- 9100
|
|
|
|
admin_key: null
|
2021-02-20 15:38:45 +08:00
|
|
|
enable_resolv_search_opt: false
|
2020-08-18 20:18:55 +08:00
|
|
|
_EOC_
|
2020-03-05 14:48:27 +08:00
|
|
|
|
2020-06-26 17:25:58 +08:00
|
|
|
my $etcd_enable_auth = $ENV{"ETCD_ENABLE_AUTH"} || "false";
|
|
|
|
|
|
|
|
if ($etcd_enable_auth eq "true") {
|
2020-08-18 20:18:55 +08:00
|
|
|
$user_yaml_config .= <<_EOC_;
|
|
|
|
etcd:
|
|
|
|
user: root
|
|
|
|
password: 5tHkHhYkjr6cQY
|
|
|
|
_EOC_
|
2020-06-26 17:25:58 +08:00
|
|
|
}
|
|
|
|
|
2020-09-16 14:27:56 +08:00
|
|
|
my $custom_hmac_auth = $ENV{"CUSTOM_HMAC_AUTH"} || "false";
|
|
|
|
if ($custom_hmac_auth eq "true") {
|
|
|
|
$user_yaml_config .= <<_EOC_;
|
|
|
|
plugin_attr:
|
|
|
|
hmac-auth:
|
|
|
|
signature_key: X-APISIX-HMAC-SIGNATURE
|
|
|
|
algorithm_key: X-APISIX-HMAC-ALGORITHM
|
2020-09-28 09:10:26 +08:00
|
|
|
date_key: X-APISIX-DATE
|
2020-09-16 14:27:56 +08:00
|
|
|
access_key: X-APISIX-HMAC-ACCESS-KEY
|
2020-09-18 20:25:41 +08:00
|
|
|
signed_headers_key: X-APISIX-HMAC-SIGNED-HEADERS
|
2020-09-16 14:27:56 +08:00
|
|
|
_EOC_
|
|
|
|
}
|
|
|
|
|
2020-06-26 17:25:58 +08:00
|
|
|
|
2020-02-28 08:46:42 +08:00
|
|
|
my $profile = $ENV{"APISIX_PROFILE"};
|
|
|
|
|
|
|
|
|
|
|
|
my $apisix_file;
|
|
|
|
my $debug_file;
|
|
|
|
my $config_file;
|
|
|
|
if ($profile) {
|
|
|
|
$apisix_file = "apisix-$profile.yaml";
|
|
|
|
$debug_file = "debug-$profile.yaml";
|
|
|
|
$config_file = "config-$profile.yaml";
|
|
|
|
} else {
|
|
|
|
$apisix_file = "apisix.yaml";
|
|
|
|
$debug_file = "debug.yaml";
|
|
|
|
$config_file = "config.yaml";
|
|
|
|
}
|
2019-06-22 08:24:02 +08:00
|
|
|
|
2019-04-11 16:53:21 +08:00
|
|
|
|
2021-01-20 09:47:09 +08:00
|
|
|
my $dubbo_upstream = "";
|
|
|
|
my $dubbo_location = "";
|
|
|
|
my $version = eval { `$nginx_binary -V 2>&1` };
|
|
|
|
if ($version =~ m/\/mod_dubbo/) {
|
|
|
|
$dubbo_upstream = <<_EOC_;
|
|
|
|
upstream apisix_dubbo_backend {
|
|
|
|
server 0.0.0.1;
|
|
|
|
balancer_by_lua_block {
|
|
|
|
apisix.http_balancer_phase()
|
|
|
|
}
|
|
|
|
|
|
|
|
multi 1;
|
|
|
|
keepalive 320;
|
|
|
|
}
|
|
|
|
|
|
|
|
_EOC_
|
|
|
|
|
|
|
|
$dubbo_location = <<_EOC_;
|
|
|
|
location \@dubbo_pass {
|
|
|
|
access_by_lua_block {
|
|
|
|
apisix.dubbo_access_phase()
|
|
|
|
}
|
|
|
|
|
|
|
|
dubbo_pass_all_headers on;
|
|
|
|
dubbo_pass_body on;
|
|
|
|
dubbo_pass \$dubbo_service_name \$dubbo_service_version \$dubbo_method apisix_dubbo_backend;
|
|
|
|
|
|
|
|
header_filter_by_lua_block {
|
|
|
|
apisix.http_header_filter_phase()
|
|
|
|
}
|
|
|
|
|
|
|
|
body_filter_by_lua_block {
|
|
|
|
apisix.http_body_filter_phase()
|
|
|
|
}
|
|
|
|
|
|
|
|
log_by_lua_block {
|
|
|
|
apisix.http_log_phase()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_EOC_
|
|
|
|
}
|
|
|
|
|
2021-01-26 11:04:16 +08:00
|
|
|
my $grpc_location = <<_EOC_;
|
|
|
|
location \@grpc_pass {
|
|
|
|
access_by_lua_block {
|
|
|
|
apisix.grpc_access_phase()
|
|
|
|
}
|
|
|
|
|
|
|
|
grpc_set_header Content-Type application/grpc;
|
|
|
|
grpc_socket_keepalive on;
|
|
|
|
grpc_pass \$upstream_scheme://apisix_backend;
|
|
|
|
|
|
|
|
header_filter_by_lua_block {
|
|
|
|
apisix.http_header_filter_phase()
|
|
|
|
}
|
|
|
|
|
|
|
|
body_filter_by_lua_block {
|
|
|
|
apisix.http_body_filter_phase()
|
|
|
|
}
|
|
|
|
|
|
|
|
log_by_lua_block {
|
|
|
|
apisix.http_log_phase()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_EOC_
|
|
|
|
|
2019-04-11 16:53:21 +08:00
|
|
|
add_block_preprocessor(sub {
|
|
|
|
my ($block) = @_;
|
2019-09-12 13:27:18 +08:00
|
|
|
my $wait_etcd_sync = $block->wait_etcd_sync // 0.1;
|
2019-04-11 16:53:21 +08:00
|
|
|
|
2021-02-20 16:01:00 +08:00
|
|
|
if ($block->apisix_yaml && (!defined $block->yaml_config)) {
|
|
|
|
$user_yaml_config = <<_EOC_;
|
|
|
|
apisix:
|
|
|
|
node_listen: 1984
|
|
|
|
config_center: yaml
|
|
|
|
enable_admin: false
|
|
|
|
_EOC_
|
|
|
|
}
|
|
|
|
|
2020-12-10 15:51:00 +08:00
|
|
|
my $lua_deps_path = <<_EOC_;
|
|
|
|
lua_package_path "$apisix_home/?.lua;$apisix_home/?/init.lua;$apisix_home/deps/share/lua/5.1/?/init.lua;$apisix_home/deps/share/lua/5.1/?.lua;$apisix_home/apisix/?.lua;$apisix_home/t/?.lua;;";
|
|
|
|
lua_package_cpath "$apisix_home/?.so;$apisix_home/deps/lib/lua/5.1/?.so;$apisix_home/deps/lib64/lua/5.1/?.so;;";
|
|
|
|
_EOC_
|
|
|
|
|
2019-08-19 14:02:33 +08:00
|
|
|
my $main_config = $block->main_config // <<_EOC_;
|
|
|
|
worker_rlimit_core 500M;
|
2020-06-26 17:25:58 +08:00
|
|
|
env ENABLE_ETCD_AUTH;
|
2020-02-28 08:46:42 +08:00
|
|
|
env APISIX_PROFILE;
|
2021-05-04 10:41:08 +08:00
|
|
|
env PATH; # for searching external plugin runner's binary
|
2020-11-03 13:46:24 +08:00
|
|
|
env TEST_NGINX_HTML_DIR;
|
2019-08-19 14:02:33 +08:00
|
|
|
_EOC_
|
|
|
|
|
2020-10-17 08:21:47 +08:00
|
|
|
# set default `timeout` to 5sec
|
|
|
|
my $timeout = $block->timeout // 5;
|
|
|
|
$block->set_value("timeout", $timeout);
|
|
|
|
|
2019-09-12 13:27:18 +08:00
|
|
|
my $stream_enable = $block->stream_enable;
|
2021-04-29 11:07:05 +08:00
|
|
|
my $stream_conf_enable = $block->stream_conf_enable;
|
|
|
|
my $extra_stream_config = $block->extra_stream_config // '';
|
2019-09-12 13:27:18 +08:00
|
|
|
my $stream_config = $block->stream_config // <<_EOC_;
|
2020-12-10 15:51:00 +08:00
|
|
|
$lua_deps_path
|
2019-09-12 13:27:18 +08:00
|
|
|
lua_socket_log_errors off;
|
|
|
|
|
2020-04-22 17:47:16 +08:00
|
|
|
lua_shared_dict lrucache-lock-stream 10m;
|
|
|
|
|
2019-09-12 13:27:18 +08:00
|
|
|
upstream apisix_backend {
|
|
|
|
server 127.0.0.1:1900;
|
|
|
|
balancer_by_lua_block {
|
|
|
|
apisix.stream_balancer_phase()
|
|
|
|
}
|
|
|
|
}
|
2021-03-25 23:40:40 +08:00
|
|
|
_EOC_
|
2019-09-12 13:27:18 +08:00
|
|
|
|
2021-03-25 23:40:40 +08:00
|
|
|
my $stream_init_by_lua_block = $block->stream_init_by_lua_block // <<_EOC_;
|
2020-02-26 21:26:01 +08:00
|
|
|
if os.getenv("APISIX_ENABLE_LUACOV") == "1" then
|
|
|
|
require("luacov.runner")("t/apisix.luacov")
|
|
|
|
jit.off()
|
|
|
|
end
|
2019-09-16 10:58:27 +08:00
|
|
|
|
2019-09-12 13:27:18 +08:00
|
|
|
require "resty.core"
|
|
|
|
|
|
|
|
apisix = require("apisix")
|
|
|
|
apisix.stream_init()
|
2021-03-25 23:40:40 +08:00
|
|
|
_EOC_
|
2019-09-12 13:27:18 +08:00
|
|
|
|
2021-03-25 23:40:40 +08:00
|
|
|
$stream_config .= <<_EOC_;
|
|
|
|
init_by_lua_block {
|
|
|
|
$stream_init_by_lua_block
|
|
|
|
}
|
2019-09-12 13:27:18 +08:00
|
|
|
init_worker_by_lua_block {
|
|
|
|
apisix.stream_init_worker()
|
|
|
|
}
|
|
|
|
|
2021-04-29 11:07:05 +08:00
|
|
|
$extra_stream_config
|
2020-11-28 13:12:17 +08:00
|
|
|
|
2019-09-12 13:27:18 +08:00
|
|
|
# fake server, only for test
|
|
|
|
server {
|
|
|
|
listen 1995;
|
|
|
|
|
|
|
|
content_by_lua_block {
|
2019-09-16 10:58:27 +08:00
|
|
|
local sock = ngx.req.socket()
|
|
|
|
local data = sock:receive("1")
|
2019-09-12 13:27:18 +08:00
|
|
|
ngx.say("hello world")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_EOC_
|
|
|
|
|
|
|
|
if (defined $stream_enable) {
|
|
|
|
$block->set_value("stream_config", $stream_config);
|
|
|
|
}
|
|
|
|
|
|
|
|
my $stream_server_config = $block->stream_server_config // <<_EOC_;
|
|
|
|
preread_by_lua_block {
|
|
|
|
-- wait for etcd sync
|
|
|
|
ngx.sleep($wait_etcd_sync)
|
|
|
|
apisix.stream_preread_phase()
|
|
|
|
}
|
|
|
|
|
|
|
|
proxy_pass apisix_backend;
|
|
|
|
|
|
|
|
log_by_lua_block {
|
|
|
|
apisix.stream_log_phase()
|
|
|
|
}
|
|
|
|
_EOC_
|
|
|
|
|
|
|
|
if (defined $stream_enable) {
|
|
|
|
$block->set_value("stream_server_config", $stream_server_config);
|
|
|
|
}
|
|
|
|
|
2021-04-29 11:07:05 +08:00
|
|
|
if (defined $stream_conf_enable) {
|
|
|
|
$main_config .= <<_EOC_;
|
|
|
|
stream {
|
|
|
|
$stream_config
|
|
|
|
}
|
|
|
|
_EOC_
|
|
|
|
}
|
|
|
|
|
|
|
|
$block->set_value("main_config", $main_config);
|
|
|
|
|
2021-03-12 09:09:19 +08:00
|
|
|
my $extra_init_by_lua = $block->extra_init_by_lua // "";
|
2019-06-11 12:20:57 +08:00
|
|
|
my $init_by_lua_block = $block->init_by_lua_block // <<_EOC_;
|
2019-07-03 15:44:24 +08:00
|
|
|
if os.getenv("APISIX_ENABLE_LUACOV") == "1" then
|
|
|
|
require("luacov.runner")("t/apisix.luacov")
|
|
|
|
jit.off()
|
|
|
|
end
|
|
|
|
|
2019-09-16 10:58:27 +08:00
|
|
|
require "resty.core"
|
|
|
|
|
2019-06-11 12:20:57 +08:00
|
|
|
apisix = require("apisix")
|
2020-03-26 09:48:18 +08:00
|
|
|
local args = {
|
|
|
|
dns_resolver = $dns_addrs_tbl_str,
|
|
|
|
}
|
|
|
|
apisix.http_init(args)
|
2021-03-12 09:09:19 +08:00
|
|
|
$extra_init_by_lua
|
2019-06-11 12:20:57 +08:00
|
|
|
_EOC_
|
|
|
|
|
2021-03-14 18:06:04 +08:00
|
|
|
my $extra_init_worker_by_lua = $block->extra_init_worker_by_lua // "";
|
|
|
|
|
2019-04-11 16:53:21 +08:00
|
|
|
my $http_config = $block->http_config // '';
|
|
|
|
$http_config .= <<_EOC_;
|
2020-12-10 15:51:00 +08:00
|
|
|
$lua_deps_path
|
2019-04-11 16:53:21 +08:00
|
|
|
|
2019-07-17 23:52:15 +08:00
|
|
|
lua_shared_dict plugin-limit-req 10m;
|
|
|
|
lua_shared_dict plugin-limit-count 10m;
|
|
|
|
lua_shared_dict plugin-limit-conn 10m;
|
|
|
|
lua_shared_dict prometheus-metrics 10m;
|
2020-12-09 10:07:57 +08:00
|
|
|
lua_shared_dict internal_status 10m;
|
2019-07-17 23:52:15 +08:00
|
|
|
lua_shared_dict upstream-healthcheck 32m;
|
|
|
|
lua_shared_dict worker-events 10m;
|
2020-04-22 17:47:16 +08:00
|
|
|
lua_shared_dict lrucache-lock 10m;
|
2020-08-29 23:14:16 +08:00
|
|
|
lua_shared_dict balancer_ewma 1m;
|
|
|
|
lua_shared_dict balancer_ewma_locks 1m;
|
|
|
|
lua_shared_dict balancer_ewma_last_touched_at 1m;
|
2020-10-17 08:21:47 +08:00
|
|
|
lua_shared_dict plugin-limit-count-redis-cluster-slot-lock 1m;
|
2020-10-29 15:29:52 +08:00
|
|
|
lua_shared_dict tracing_buffer 10m; # plugin skywalking
|
2021-01-27 21:50:18 +08:00
|
|
|
lua_shared_dict access_tokens 1m; # plugin authz-keycloak
|
|
|
|
lua_shared_dict discovery 1m; # plugin authz-keycloak
|
2020-10-29 15:29:52 +08:00
|
|
|
lua_shared_dict plugin-api-breaker 10m;
|
2020-11-30 14:43:16 +08:00
|
|
|
lua_capture_error_log 1m; # plugin error-log-logger
|
2019-06-10 14:20:24 +08:00
|
|
|
|
2021-04-01 13:06:33 +08:00
|
|
|
proxy_ssl_name \$upstream_host;
|
2021-01-26 23:36:12 +08:00
|
|
|
proxy_ssl_server_name on;
|
|
|
|
|
2020-03-26 09:48:18 +08:00
|
|
|
resolver $dns_addrs_str;
|
2019-06-10 14:57:11 +08:00
|
|
|
resolver_timeout 5;
|
|
|
|
|
2020-01-10 15:17:17 +08:00
|
|
|
underscores_in_headers on;
|
2019-06-11 23:47:00 +08:00
|
|
|
lua_socket_log_errors off;
|
2021-01-27 22:38:09 +08:00
|
|
|
client_body_buffer_size 8k;
|
2019-06-11 23:47:00 +08:00
|
|
|
|
2019-06-14 10:27:45 +08:00
|
|
|
upstream apisix_backend {
|
|
|
|
server 0.0.0.1;
|
|
|
|
balancer_by_lua_block {
|
2019-07-01 17:24:38 +08:00
|
|
|
apisix.http_balancer_phase()
|
2019-06-14 10:27:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
keepalive 32;
|
|
|
|
}
|
|
|
|
|
2021-01-20 09:47:09 +08:00
|
|
|
$dubbo_upstream
|
|
|
|
|
2019-04-11 16:53:21 +08:00
|
|
|
init_by_lua_block {
|
2019-06-11 12:20:57 +08:00
|
|
|
$init_by_lua_block
|
2019-05-08 10:38:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
init_worker_by_lua_block {
|
2019-07-01 17:24:38 +08:00
|
|
|
require("apisix").http_init_worker()
|
2021-03-14 18:06:04 +08:00
|
|
|
$extra_init_worker_by_lua
|
2019-04-11 16:53:21 +08:00
|
|
|
}
|
2019-06-14 10:27:45 +08:00
|
|
|
|
2020-11-28 13:12:17 +08:00
|
|
|
log_format main escape=default '\$remote_addr - \$remote_user [\$time_local] \$http_host "\$request" \$status \$body_bytes_sent \$request_time "\$http_referer" "\$http_user_agent" \$upstream_addr \$upstream_status \$upstream_response_time "\$upstream_scheme://\$upstream_host\$upstream_uri"';
|
|
|
|
|
2019-07-25 16:06:07 +08:00
|
|
|
# fake server, only for test
|
2019-06-14 10:27:45 +08:00
|
|
|
server {
|
|
|
|
listen 1980;
|
2019-06-17 23:17:41 +08:00
|
|
|
listen 1981;
|
|
|
|
listen 1982;
|
2020-03-15 09:22:13 +08:00
|
|
|
listen 5044;
|
|
|
|
|
2019-11-11 09:49:23 +08:00
|
|
|
_EOC_
|
2019-09-27 15:57:04 +08:00
|
|
|
|
2021-04-10 18:55:20 +08:00
|
|
|
if (defined $block->upstream_server_config) {
|
|
|
|
$http_config .= $block->upstream_server_config;
|
|
|
|
}
|
|
|
|
|
2019-11-11 09:49:23 +08:00
|
|
|
my $ipv6_fake_server = "";
|
|
|
|
if (defined $block->listen_ipv6) {
|
|
|
|
$ipv6_fake_server = "listen \[::1\]:1980;";
|
|
|
|
}
|
|
|
|
|
|
|
|
$http_config .= <<_EOC_;
|
|
|
|
$ipv6_fake_server
|
2019-09-27 15:57:04 +08:00
|
|
|
server_tokens off;
|
|
|
|
|
|
|
|
location / {
|
|
|
|
content_by_lua_block {
|
|
|
|
require("lib.server").go()
|
|
|
|
}
|
|
|
|
|
|
|
|
more_clear_headers Date;
|
|
|
|
}
|
2020-12-15 20:18:25 +08:00
|
|
|
|
|
|
|
location = /v3/auth/authenticate {
|
|
|
|
content_by_lua_block {
|
|
|
|
ngx.log(ngx.WARN, "etcd auth failed!")
|
|
|
|
}
|
|
|
|
}
|
2020-11-11 11:38:25 +08:00
|
|
|
|
|
|
|
location = /.well-known/openid-configuration {
|
|
|
|
content_by_lua_block {
|
|
|
|
ngx.say([[
|
|
|
|
{"issuer":"https://samples.auth0.com/","authorization_endpoint":"https://samples.auth0.com/authorize","token_endpoint":"https://samples.auth0.com/oauth/token","device_authorization_endpoint":"https://samples.auth0.com/oauth/device/code","userinfo_endpoint":"https://samples.auth0.com/userinfo","mfa_challenge_endpoint":"https://samples.auth0.com/mfa/challenge","jwks_uri":"https://samples.auth0.com/.well-known/jwks.json","registration_endpoint":"https://samples.auth0.com/oidc/register","revocation_endpoint":"https://samples.auth0.com/oauth/revoke","scopes_supported":["openid","profile","offline_access","name","given_name","family_name","nickname","email","email_verified","picture","created_at","identities","phone","address"],"response_types_supported":["code","token","id_token","code token","code id_token","token id_token","code token id_token"],"code_challenge_methods_supported":["S256","plain"],"response_modes_supported":["query","fragment","form_post"],"subject_types_supported":["public"],"id_token_signing_alg_values_supported":["HS256","RS256"],"token_endpoint_auth_methods_supported":["client_secret_basic","client_secret_post"],"claims_supported":["aud","auth_time","created_at","email","email_verified","exp","family_name","given_name","iat","identities","iss","name","nickname","phone_number","picture","sub"],"request_uri_parameter_supported":false}
|
|
|
|
]])
|
|
|
|
}
|
|
|
|
}
|
2019-09-27 15:57:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
server {
|
|
|
|
listen 1983 ssl;
|
|
|
|
ssl_certificate cert/apisix.crt;
|
|
|
|
ssl_certificate_key cert/apisix.key;
|
|
|
|
lua_ssl_trusted_certificate cert/apisix.crt;
|
2021-04-10 18:55:20 +08:00
|
|
|
_EOC_
|
2019-06-14 10:27:45 +08:00
|
|
|
|
2021-04-10 18:55:20 +08:00
|
|
|
if (defined $block->upstream_server_config) {
|
|
|
|
$http_config .= $block->upstream_server_config;
|
|
|
|
}
|
|
|
|
|
|
|
|
$http_config .= <<_EOC_;
|
2019-06-22 10:44:07 +08:00
|
|
|
server_tokens off;
|
|
|
|
|
2021-01-26 23:36:12 +08:00
|
|
|
ssl_certificate_by_lua_block {
|
|
|
|
local ngx_ssl = require "ngx.ssl"
|
|
|
|
ngx.log(ngx.WARN, "Receive SNI: ", ngx_ssl.server_name())
|
|
|
|
}
|
|
|
|
|
2019-06-17 23:17:41 +08:00
|
|
|
location / {
|
|
|
|
content_by_lua_block {
|
|
|
|
require("lib.server").go()
|
|
|
|
}
|
2019-06-22 10:44:07 +08:00
|
|
|
|
|
|
|
more_clear_headers Date;
|
2019-06-14 10:27:45 +08:00
|
|
|
}
|
|
|
|
}
|
2019-06-17 23:17:41 +08:00
|
|
|
|
2019-04-11 16:53:21 +08:00
|
|
|
_EOC_
|
|
|
|
|
|
|
|
$block->set_value("http_config", $http_config);
|
|
|
|
|
2019-06-22 10:44:07 +08:00
|
|
|
my $TEST_NGINX_HTML_DIR = $ENV{TEST_NGINX_HTML_DIR} ||= html_dir();
|
2019-08-24 09:10:11 +08:00
|
|
|
my $ipv6_listen_conf = '';
|
|
|
|
if (defined $block->listen_ipv6) {
|
|
|
|
$ipv6_listen_conf = "listen \[::1\]:12345;"
|
|
|
|
}
|
2019-06-22 10:44:07 +08:00
|
|
|
|
2019-06-13 12:01:36 +08:00
|
|
|
my $config = $block->config // '';
|
2019-06-15 22:34:26 +08:00
|
|
|
$config .= <<_EOC_;
|
2019-08-24 09:10:11 +08:00
|
|
|
$ipv6_listen_conf
|
2019-06-22 10:44:07 +08:00
|
|
|
|
2021-04-05 11:41:25 +08:00
|
|
|
listen 1994 ssl;
|
2019-06-22 10:44:07 +08:00
|
|
|
ssl_certificate cert/apisix.crt;
|
|
|
|
ssl_certificate_key cert/apisix.key;
|
|
|
|
lua_ssl_trusted_certificate cert/apisix.crt;
|
|
|
|
|
|
|
|
ssl_certificate_by_lua_block {
|
2019-07-01 17:24:38 +08:00
|
|
|
apisix.http_ssl_phase()
|
2019-06-22 10:44:07 +08:00
|
|
|
}
|
|
|
|
|
2021-01-20 09:47:09 +08:00
|
|
|
set \$dubbo_service_name '';
|
|
|
|
set \$dubbo_service_version '';
|
|
|
|
set \$dubbo_method '';
|
2020-11-28 13:12:17 +08:00
|
|
|
|
2019-06-17 21:08:17 +08:00
|
|
|
location = /apisix/nginx_status {
|
2019-06-22 08:24:02 +08:00
|
|
|
allow 127.0.0.0/24;
|
2019-06-17 21:08:17 +08:00
|
|
|
access_log off;
|
|
|
|
stub_status;
|
|
|
|
}
|
|
|
|
|
2019-06-13 12:01:36 +08:00
|
|
|
location /apisix/admin {
|
2021-04-01 13:06:33 +08:00
|
|
|
set \$upstream_scheme 'http';
|
|
|
|
set \$upstream_host \$http_host;
|
|
|
|
set \$upstream_uri '';
|
|
|
|
|
2019-06-13 12:01:36 +08:00
|
|
|
content_by_lua_block {
|
2019-07-01 17:24:38 +08:00
|
|
|
apisix.http_admin()
|
2019-06-13 12:01:36 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-18 15:05:09 +08:00
|
|
|
location /v1/ {
|
|
|
|
content_by_lua_block {
|
|
|
|
apisix.http_control()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-08 10:38:45 +08:00
|
|
|
location / {
|
2020-03-19 08:10:03 +08:00
|
|
|
set \$upstream_mirror_host '';
|
2019-06-15 22:34:26 +08:00
|
|
|
set \$upstream_upgrade '';
|
|
|
|
set \$upstream_connection '';
|
2019-06-14 10:27:45 +08:00
|
|
|
|
2021-04-01 13:06:33 +08:00
|
|
|
set \$upstream_scheme 'http';
|
|
|
|
set \$upstream_host \$http_host;
|
|
|
|
set \$upstream_uri '';
|
|
|
|
set \$ctx_ref '';
|
|
|
|
|
2020-03-15 22:26:08 +08:00
|
|
|
set \$upstream_cache_zone off;
|
|
|
|
set \$upstream_cache_key '';
|
|
|
|
set \$upstream_cache_bypass '';
|
|
|
|
set \$upstream_no_cache '';
|
|
|
|
|
|
|
|
proxy_cache \$upstream_cache_zone;
|
|
|
|
proxy_cache_valid any 10s;
|
|
|
|
proxy_cache_min_uses 1;
|
|
|
|
proxy_cache_methods GET HEAD;
|
|
|
|
proxy_cache_lock_timeout 5s;
|
|
|
|
proxy_cache_use_stale off;
|
|
|
|
proxy_cache_key \$upstream_cache_key;
|
|
|
|
proxy_no_cache \$upstream_no_cache;
|
|
|
|
proxy_cache_bypass \$upstream_cache_bypass;
|
|
|
|
|
2019-05-08 10:38:45 +08:00
|
|
|
access_by_lua_block {
|
2019-06-14 10:27:45 +08:00
|
|
|
-- wait for etcd sync
|
2019-06-15 22:34:26 +08:00
|
|
|
ngx.sleep($wait_etcd_sync)
|
2019-07-01 17:24:38 +08:00
|
|
|
apisix.http_access_phase()
|
2019-05-08 10:38:45 +08:00
|
|
|
}
|
|
|
|
|
2019-06-14 10:27:45 +08:00
|
|
|
proxy_http_version 1.1;
|
2019-06-15 22:34:26 +08:00
|
|
|
proxy_set_header Host \$upstream_host;
|
|
|
|
proxy_set_header Upgrade \$upstream_upgrade;
|
|
|
|
proxy_set_header Connection \$upstream_connection;
|
|
|
|
proxy_set_header X-Real-IP \$remote_addr;
|
2019-06-14 10:27:45 +08:00
|
|
|
proxy_pass_header Date;
|
2019-06-15 22:34:26 +08:00
|
|
|
proxy_pass \$upstream_scheme://apisix_backend\$upstream_uri;
|
2020-03-19 08:10:03 +08:00
|
|
|
mirror /proxy_mirror;
|
2019-06-14 10:27:45 +08:00
|
|
|
|
2019-05-08 10:38:45 +08:00
|
|
|
header_filter_by_lua_block {
|
2019-07-01 17:24:38 +08:00
|
|
|
apisix.http_header_filter_phase()
|
2019-05-08 10:38:45 +08:00
|
|
|
}
|
|
|
|
|
2019-08-21 23:10:34 +08:00
|
|
|
body_filter_by_lua_block {
|
|
|
|
apisix.http_body_filter_phase()
|
|
|
|
}
|
|
|
|
|
|
|
|
log_by_lua_block {
|
|
|
|
apisix.http_log_phase()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-26 11:04:16 +08:00
|
|
|
$grpc_location
|
2021-01-20 09:47:09 +08:00
|
|
|
$dubbo_location
|
|
|
|
|
2020-03-19 08:10:03 +08:00
|
|
|
location = /proxy_mirror {
|
|
|
|
internal;
|
|
|
|
|
|
|
|
if (\$upstream_mirror_host = "") {
|
|
|
|
return 200;
|
|
|
|
}
|
|
|
|
|
2020-11-05 14:48:58 +08:00
|
|
|
proxy_http_version 1.1;
|
2020-11-05 08:56:18 +08:00
|
|
|
proxy_set_header Host \$upstream_host;
|
2020-03-19 08:10:03 +08:00
|
|
|
proxy_pass \$upstream_mirror_host\$request_uri;
|
|
|
|
}
|
2019-05-08 10:38:45 +08:00
|
|
|
_EOC_
|
|
|
|
|
|
|
|
$block->set_value("config", $config);
|
|
|
|
|
2019-09-09 17:42:20 +08:00
|
|
|
my $user_apisix_yaml = $block->apisix_yaml // "";
|
|
|
|
if ($user_apisix_yaml) {
|
|
|
|
$user_apisix_yaml = <<_EOC_;
|
2020-02-28 08:46:42 +08:00
|
|
|
>>> ../conf/$apisix_file
|
2019-09-09 17:42:20 +08:00
|
|
|
$user_apisix_yaml
|
|
|
|
_EOC_
|
|
|
|
}
|
|
|
|
|
2020-08-18 20:18:55 +08:00
|
|
|
my $yaml_config = $block->yaml_config // $user_yaml_config;
|
2020-11-09 08:12:52 +08:00
|
|
|
|
|
|
|
if ($block->extra_yaml_config) {
|
|
|
|
$yaml_config .= $block->extra_yaml_config;
|
|
|
|
}
|
|
|
|
|
2019-10-09 11:26:54 +08:00
|
|
|
my $user_debug_config = $block->debug_config // "";
|
2019-05-08 10:38:45 +08:00
|
|
|
|
2019-04-11 16:53:21 +08:00
|
|
|
my $user_files = $block->user_files;
|
|
|
|
$user_files .= <<_EOC_;
|
2020-02-28 08:46:42 +08:00
|
|
|
>>> ../conf/$debug_file
|
2019-10-09 11:26:54 +08:00
|
|
|
$user_debug_config
|
2020-08-18 20:18:55 +08:00
|
|
|
>>> ../conf/config-default.yaml
|
|
|
|
$default_yaml_config
|
2020-02-28 08:46:42 +08:00
|
|
|
>>> ../conf/$config_file
|
2020-08-18 20:18:55 +08:00
|
|
|
$yaml_config
|
2019-06-22 10:44:07 +08:00
|
|
|
>>> ../conf/cert/apisix.crt
|
|
|
|
$ssl_crt
|
|
|
|
>>> ../conf/cert/apisix.key
|
|
|
|
$ssl_key
|
2020-08-24 09:23:06 +08:00
|
|
|
>>> ../conf/cert/apisix_ecc.crt
|
|
|
|
$ssl_ecc_crt
|
|
|
|
>>> ../conf/cert/apisix_ecc.key
|
|
|
|
$ssl_ecc_key
|
2020-06-08 10:52:25 +08:00
|
|
|
>>> ../conf/cert/test2.crt
|
|
|
|
$test2_crt
|
|
|
|
>>> ../conf/cert/test2.key
|
|
|
|
$test2_key
|
2019-09-09 17:42:20 +08:00
|
|
|
$user_apisix_yaml
|
2019-04-11 16:53:21 +08:00
|
|
|
_EOC_
|
|
|
|
|
|
|
|
$block->set_value("user_files", $user_files);
|
|
|
|
|
|
|
|
$block;
|
|
|
|
});
|
|
|
|
|
2021-02-05 09:00:20 +08:00
|
|
|
sub run_or_exit ($) {
|
|
|
|
my ($cmd) = @_;
|
|
|
|
my $output = `$cmd`;
|
|
|
|
if ($?) {
|
|
|
|
warn "$output";
|
|
|
|
exit 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
add_cleanup_handler(sub {
|
|
|
|
if ($ENV{FLUSH_ETCD}) {
|
|
|
|
delete $ENV{APISIX_PROFILE};
|
|
|
|
run_or_exit "etcdctl del --prefix /apisix";
|
|
|
|
run_or_exit "./bin/apisix init_etcd";
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-04-11 16:53:21 +08:00
|
|
|
1;
|