This is an automated email from the ASF dual-hosted git repository.
juzhiyuan 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 d18fcf165 fix(gRPC): set :authority header to the upstream host (#7939)
d18fcf165 is described below
commit d18fcf1658fe80430367d2f169839d5a197ba855
Author: 罗泽轩 <[email protected]>
AuthorDate: Tue Sep 20 09:16:00 2022 +0800
fix(gRPC): set :authority header to the upstream host (#7939)
Signed-off-by: spacewander <[email protected]>
---
apisix/cli/ngx_tpl.lua | 8 +++++
t/APISIX.pm | 13 +++++++
t/node/grpc-proxy.t | 96 ++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 117 insertions(+)
diff --git a/apisix/cli/ngx_tpl.lua b/apisix/cli/ngx_tpl.lua
index e6995d21c..87c60b4b7 100644
--- a/apisix/cli/ngx_tpl.lua
+++ b/apisix/cli/ngx_tpl.lua
@@ -748,6 +748,14 @@ http {
apisix.grpc_access_phase()
}
+ {% if use_apisix_openresty then %}
+ # For servers which obey the standard, when `:authority` is
missing,
+ # `host` will be used instead. When used with apisix-base, we can
do
+ # better by setting `:authority` directly
+ grpc_set_header ":authority" $upstream_host;
+ {% else %}
+ grpc_set_header "Host" $upstream_host;
+ {% end %}
grpc_set_header Content-Type application/grpc;
grpc_socket_keepalive on;
grpc_pass $upstream_scheme://apisix_backend;
diff --git a/t/APISIX.pm b/t/APISIX.pm
index 9f964b889..28c2348a5 100644
--- a/t/APISIX.pm
+++ b/t/APISIX.pm
@@ -189,6 +189,19 @@ my $grpc_location = <<_EOC_;
apisix.grpc_access_phase()
}
+_EOC_
+
+if ($version =~ m/\/apisix-nginx-module/) {
+ $grpc_location .= <<_EOC_;
+ grpc_set_header ":authority" \$upstream_host;
+_EOC_
+} else {
+ $grpc_location .= <<_EOC_;
+ grpc_set_header "Host" \$upstream_host;
+_EOC_
+}
+
+$grpc_location .= <<_EOC_;
grpc_set_header Content-Type application/grpc;
grpc_socket_keepalive on;
grpc_pass \$upstream_scheme://apisix_backend;
diff --git a/t/node/grpc-proxy.t b/t/node/grpc-proxy.t
index ed6eb2799..18427afe6 100644
--- a/t/node/grpc-proxy.t
+++ b/t/node/grpc-proxy.t
@@ -185,3 +185,99 @@ GET /hello
--- error_code: 502
--- error_log
upstream: "grpc://127.0.0.1:80"
+
+
+
+=== TEST 7: set authority header
+--- log_level: debug
+--- http2
+--- apisix_yaml
+routes:
+ -
+ id: 1
+ uris:
+ - /helloworld.Greeter/SayHello
+ methods: [
+ POST
+ ]
+ upstream:
+ scheme: grpc
+ nodes:
+ "127.0.0.1:50051": 1
+ type: roundrobin
+#END
+--- exec
+grpcurl -import-path ./t/grpc_server_example/proto -proto helloworld.proto
-plaintext -d '{"name":"apisix"}' 127.0.0.1:1984 helloworld.Greeter.SayHello
+--- response_body
+{
+ "message": "Hello apisix"
+}
+--- grep_error_log eval
+qr/grpc header: "(:authority|host): [^"]+"/
+--- grep_error_log_out eval
+qr/grpc header: "(:authority|host): 127.0.0.1:1984"/
+
+
+
+=== TEST 8: set authority header to node header
+--- log_level: debug
+--- http2
+--- apisix_yaml
+routes:
+ -
+ id: 1
+ uris:
+ - /helloworld.Greeter/SayHello
+ methods: [
+ POST
+ ]
+ upstream:
+ scheme: grpc
+ pass_host: node
+ nodes:
+ "127.0.0.1:50051": 1
+ type: roundrobin
+#END
+--- exec
+grpcurl -import-path ./t/grpc_server_example/proto -proto helloworld.proto
-plaintext -d '{"name":"apisix"}' 127.0.0.1:1984 helloworld.Greeter.SayHello
+--- response_body
+{
+ "message": "Hello apisix"
+}
+--- grep_error_log eval
+qr/grpc header: "(:authority|host): [^"]+"/
+--- grep_error_log_out eval
+qr/grpc header: "(:authority|host): 127.0.0.1:50051"/
+
+
+
+=== TEST 9: set authority header to specific value
+--- log_level: debug
+--- http2
+--- apisix_yaml
+routes:
+ -
+ id: 1
+ uris:
+ - /helloworld.Greeter/SayHello
+ methods: [
+ POST
+ ]
+ upstream:
+ scheme: grpc
+ pass_host: rewrite
+ upstream_host: hello.world
+ nodes:
+ "127.0.0.1:50051": 1
+ type: roundrobin
+#END
+--- exec
+grpcurl -import-path ./t/grpc_server_example/proto -proto helloworld.proto
-plaintext -d '{"name":"apisix"}' 127.0.0.1:1984 helloworld.Greeter.SayHello
+--- response_body
+{
+ "message": "Hello apisix"
+}
+--- grep_error_log eval
+qr/grpc header: "(:authority|host): [^"]+"/
+--- grep_error_log_out eval
+qr/grpc header: "(:authority|host): hello.world"/