Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(gRPC): set :authority header to the upstream host #7939

Merged
merged 1 commit into from
Sep 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions apisix/cli/ngx_tpl.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
13 changes: 13 additions & 0 deletions t/APISIX.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
96 changes: 96 additions & 0 deletions t/node/grpc-proxy.t
Original file line number Diff line number Diff line change
Expand Up @@ -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"/