You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by sp...@apache.org on 2021/04/01 05:06:41 UTC

[apisix] branch master updated: fix: make sure SNI is changed with HTTP host (#3948)

This is an automated email from the ASF dual-hosted git repository.

spacewander pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/apisix.git


The following commit(s) were added to refs/heads/master by this push:
     new d67da18  fix: make sure SNI is changed with HTTP host (#3948)
d67da18 is described below

commit d67da18d53fd02b872b096fe593a990bfaa3766d
Author: 罗泽轩 <sp...@gmail.com>
AuthorDate: Thu Apr 1 13:06:33 2021 +0800

    fix: make sure SNI is changed with HTTP host (#3948)
    
    Move the variables under locations to avoid being reset by proxy-mirror.
    Fix #3864
    Signed-off-by: spacewander <sp...@gmail.com>
---
 apisix/cli/ngx_tpl.lua   | 36 +++++++++++++++++---------------
 t/APISIX.pm              | 15 +++++++++-----
 t/cli/test_access_log.sh | 41 +++++++++++++++++++++++++++++++++++++
 t/node/https-proxy.t     | 53 ++++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 124 insertions(+), 21 deletions(-)

diff --git a/apisix/cli/ngx_tpl.lua b/apisix/cli/ngx_tpl.lua
index b1e95a7..cf7d033 100644
--- a/apisix/cli/ngx_tpl.lua
+++ b/apisix/cli/ngx_tpl.lua
@@ -397,17 +397,6 @@ http {
         {% end %}
         # http server configuration snippet ends
 
-        set $upstream_scheme             'http';
-        set $upstream_host               $http_host;
-        set $upstream_uri                '';
-        set $ctx_ref                     '';
-
-        {% if enabled_plugins["dubbo-proxy"] then %}
-        set $dubbo_service_name          '';
-        set $dubbo_service_version       '';
-        set $dubbo_method                '';
-        {% end %}
-
         {% if with_module_status then %}
         location = /apisix/nginx_status {
             allow 127.0.0.0/24;
@@ -419,6 +408,10 @@ http {
 
         {% if enable_admin and not port_admin then %}
         location /apisix/admin {
+            set $upstream_scheme             'http';
+            set $upstream_host               $http_host;
+            set $upstream_uri                '';
+
             {%if allow_admin then%}
                 {% for _, allow_ip in ipairs(allow_admin) do %}
                 allow {*allow_ip*};
@@ -440,11 +433,27 @@ http {
         }
         {% end %}
 
+        {% if http.proxy_ssl_server_name then %}
+        proxy_ssl_name $upstream_host;
+        proxy_ssl_server_name on;
+        {% end %}
+
         location / {
             set $upstream_mirror_host        '';
             set $upstream_upgrade            '';
             set $upstream_connection         '';
 
+            set $upstream_scheme             'http';
+            set $upstream_host               $http_host;
+            set $upstream_uri                '';
+            set $ctx_ref                     '';
+
+            {% if enabled_plugins["dubbo-proxy"] then %}
+            set $dubbo_service_name          '';
+            set $dubbo_service_version       '';
+            set $dubbo_method                '';
+            {% end %}
+
             access_by_lua_block {
                 apisix.http_access_phase()
             }
@@ -456,11 +465,6 @@ http {
             proxy_set_header   X-Real-IP         $remote_addr;
             proxy_pass_header  Date;
 
-            {% if http.proxy_ssl_server_name then %}
-            proxy_ssl_name $host;
-            proxy_ssl_server_name on;
-            {% end %}
-
             ### the following x-forwarded-* headers is to send to upstream server
 
             set $var_x_forwarded_for        $remote_addr;
diff --git a/t/APISIX.pm b/t/APISIX.pm
index 2182430..90b3901 100644
--- a/t/APISIX.pm
+++ b/t/APISIX.pm
@@ -397,7 +397,7 @@ _EOC_
     lua_shared_dict plugin-api-breaker   10m;
     lua_capture_error_log                 1m;    # plugin error-log-logger
 
-    proxy_ssl_name \$host;
+    proxy_ssl_name \$upstream_host;
     proxy_ssl_server_name on;
 
     resolver $dns_addrs_str;
@@ -514,10 +514,6 @@ _EOC_
             apisix.http_ssl_phase()
         }
 
-        set \$upstream_scheme             'http';
-        set \$upstream_host               \$http_host;
-        set \$upstream_uri                '';
-        set \$ctx_ref                     '';
         set \$dubbo_service_name          '';
         set \$dubbo_service_version       '';
         set \$dubbo_method                '';
@@ -529,6 +525,10 @@ _EOC_
         }
 
         location /apisix/admin {
+            set \$upstream_scheme             'http';
+            set \$upstream_host               \$http_host;
+            set \$upstream_uri                '';
+
             content_by_lua_block {
                 apisix.http_admin()
             }
@@ -545,6 +545,11 @@ _EOC_
             set \$upstream_upgrade            '';
             set \$upstream_connection         '';
 
+            set \$upstream_scheme             'http';
+            set \$upstream_host               \$http_host;
+            set \$upstream_uri                '';
+            set \$ctx_ref                     '';
+
             set \$upstream_cache_zone            off;
             set \$upstream_cache_key             '';
             set \$upstream_cache_bypass          '';
diff --git a/t/cli/test_access_log.sh b/t/cli/test_access_log.sh
index d1fc400..d755df5 100755
--- a/t/cli/test_access_log.sh
+++ b/t/cli/test_access_log.sh
@@ -183,3 +183,44 @@ fi
 make stop
 
 echo "don't log uninitialized access log variable when the HTTP request is malformed"
+
+# TLS upstream
+
+echo "
+apisix:
+    admin_api_mtls:
+        admin_ssl_cert: '../t/certs/apisix_admin_ssl.crt'
+        admin_ssl_cert_key: '../t/certs/apisix_admin_ssl.key'
+    port_admin: 9180
+    https_admin: true
+nginx_config:
+  http:
+    access_log_format: '\"\$upstream_scheme://\$upstream_host\" \$ssl_server_name'
+" > conf/config.yaml
+
+make run
+sleep 2
+
+curl -k -i https://127.0.0.1:9180/apisix/admin/routes/1  -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d \
+    '{"uri":"/apisix/admin/routes/1", "upstream":{"nodes":{"localhost:9180":1},"scheme":"https","type":"roundrobin","pass_host":"node"}}'
+
+curl -i http://127.0.0.1:9080/apisix/admin/routes/1
+sleep 4
+tail -n 2 logs/access.log > output.log
+
+# APISIX
+if ! grep '"https://localhost" -' output.log; then
+    echo "failed: should find upstream scheme"
+    cat output.log
+    exit 1
+fi
+
+# admin
+if ! grep '"http://localhost" localhost' output.log; then
+    echo "failed: should find upstream scheme"
+    cat output.log
+    exit 1
+fi
+
+make stop
+echo "passed: should find upstream scheme"
diff --git a/t/node/https-proxy.t b/t/node/https-proxy.t
index 8f8ed21..a776f30 100644
--- a/t/node/https-proxy.t
+++ b/t/node/https-proxy.t
@@ -207,3 +207,56 @@ GET /hello
 --- error_code: 502
 --- error_log
 upstream: "http://127.0.0.1:80/hello"
+
+
+
+=== TEST 9: rewrite SNI
+--- log_level: debug
+--- apisix_yaml
+routes:
+  -
+    uri: /uri
+    upstream:
+        scheme: https
+        nodes:
+            "127.0.0.1:1983": 1
+        type: roundrobin
+        pass_host: "rewrite",
+        upstream_host: "www.test.com",
+#END
+--- request
+GET /uri
+--- more_headers
+host: www.sni.com
+--- error_log
+Receive SNI: www.test.com
+--- response_body
+uri: /uri
+host: www.test.com
+x-real-ip: 127.0.0.1
+
+
+
+=== TEST 10: node's SNI
+--- log_level: debug
+--- apisix_yaml
+routes:
+  -
+    uri: /uri
+    upstream:
+        scheme: https
+        nodes:
+            "localhost:1983": 1
+        type: roundrobin
+        pass_host: "node",
+#END
+--- request
+GET /uri
+--- more_headers
+host: www.sni.com
+--- error_log
+Receive SNI: localhost
+--- response_body
+uri: /uri
+host: localhost
+x-real-ip: 127.0.0.1