From c67e998dad394a70d2a1d922c1956d5098509424 Mon Sep 17 00:00:00 2001 From: YuanSheng Wang Date: Tue, 4 Aug 2020 23:20:34 +0800 Subject: [PATCH] bugfix: fail to handshake if there is no certificate matched by host. (#1980) * bugfix: fail to handshake if there is no certificate matched by host. * fix: grpc proxy test case --- apisix/http/router/radixtree_sni.lua | 2 +- apisix/init.lua | 3 ++- t/grpc-proxy-test.sh | 17 ++++++++++++++++- t/router/radixtree-sni.t | 11 ++++------- 4 files changed, 23 insertions(+), 10 deletions(-) diff --git a/apisix/http/router/radixtree_sni.lua b/apisix/http/router/radixtree_sni.lua index 07c689fa..4c7843b3 100644 --- a/apisix/http/router/radixtree_sni.lua +++ b/apisix/http/router/radixtree_sni.lua @@ -163,7 +163,7 @@ function _M.match_and_set(api_ctx) local sni_rev = sni:reverse() local ok = radixtree_router:dispatch(sni_rev, nil, api_ctx) if not ok then - core.log.warn("failed to find any SSL certificate by SNI: ", sni) + core.log.error("failed to find any SSL certificate by SNI: ", sni) return false end diff --git a/apisix/init.lua b/apisix/init.lua index da2df142..29536ded 100644 --- a/apisix/init.lua +++ b/apisix/init.lua @@ -159,8 +159,9 @@ function _M.http_ssl_phase() local ok, err = router.router_ssl.match_and_set(api_ctx) if not ok then if err then - core.log.warn("failed to fetch ssl config: ", err) + core.log.error("failed to fetch ssl config: ", err) end + ngx_exit(-1) end end diff --git a/t/grpc-proxy-test.sh b/t/grpc-proxy-test.sh index 6f8f2d14..5ca32340 100755 --- a/t/grpc-proxy-test.sh +++ b/t/grpc-proxy-test.sh @@ -18,6 +18,14 @@ set -ex +#set ssl +curl http://127.0.0.1:9080/apisix/admin/ssl/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d ' +{ + "cert": "-----BEGIN CERTIFICATE-----\nMIIEojCCAwqgAwIBAgIJAK253pMhgCkxMA0GCSqGSIb3DQEBCwUAMFYxCzAJBgNV\nBAYTAkNOMRIwEAYDVQQIDAlHdWFuZ0RvbmcxD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7ZhlmDEtqE\nU58FBOS36O7xRDdpXwsHLnCXhbFu9du41mom0W4UdzjgVI9gUqG71+SXrKr7lTc3\ndMHcSbplxXkBJawND/Q1rzLG5JvIRHO1AGJLmRgIdl8jNgtxgV2QSkoyKlNVbM2H\nWy6ZSKM03lIj74+rcKuU3N87dX4jDuwV0sPXjzJxL7NpR/fHwgndgyPcI14y2cGz\nzMC44EyQdTw+B/YfMnoZx83xaaMNMqV6GYNnTHi0TO2TAoHBAKmdrh9WkE2qsr59\nIoHHygh7Wzez+Ewr6hfgoEK4+QzlBlX+XV/9rxIaE0jS3Sk1txadk5oFDebimuSk\nlQkv1pXUOqh+xSAwk5v88dBAfh2dnnSa8HFN3oz+ZfQYtnBcc4DR1y2X+fVNgr3i\nnxruU2gsAIPFRnmvwKPc1YIH9A6kIzqaoNt1f9VM243D6fNzkO4uztWEApBkkJgR\n4s/yOjp6ovS9JG1NMXWjXQPcwTq3sQVLnAHxZRJmOvx69UmK4QKBwFYXXjeXiU3d\nbcrPfe6qNGjfzK+BkhWznuFUMbuxyZWDYQD5yb6ukUosrj7pmZv3BxKcKCvmONU+\nCHgIXB+hG+R9S2mCcH1qBQoP/RSm+TUzS/Bl2UeuhnFZh2jSZQy3OwryUi6nhF0u\nLDzMI/6aO1ggsI23Ri0Y9ZtqVKczTkxzdQKR9xvoNBUufjimRlS80sJCEB3Qm20S\nwzarryret/7GFW1/3cz+hTj9/d45i25zArr3Pocfpur5mfz3fJO8jg==\n-----END RSA PRIVATE KEY-----\n", + "sni": "test.com" +}' + #test grpc proxy curl http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d ' { @@ -32,4 +40,11 @@ curl http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f1 } }' -./build-cache/grpcurl -insecure -import-path ./build-cache/proto -proto helloworld.proto -d '{"name":"apisix"}' 127.0.0.1:9443 helloworld.Greeter.SayHello +echo "127.0.0.1 test.com" | sudo tee -a /etc/hosts + +./build-cache/grpcurl -insecure -import-path ./build-cache/proto -proto helloworld.proto -d '{"name":"apisix"}' test.com:9443 helloworld.Greeter.SayHello + + +#delete test data +curl http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X DELETE +curl http://127.0.0.1:9080/apisix/admin/ssl/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X DELETE diff --git a/t/router/radixtree-sni.t b/t/router/radixtree-sni.t index 06388a76..5fe1ccdc 100644 --- a/t/router/radixtree-sni.t +++ b/t/router/radixtree-sni.t @@ -205,7 +205,7 @@ location /t { GET /t --- response_body connected: 1 -failed to do SSL handshake: certificate host mismatch +failed to do SSL handshake: handshake failed --- error_log failed to find any SSL certificate by SNI @@ -558,12 +558,11 @@ location /t { GET /t --- response_body connected: 1 -failed to do SSL handshake: certificate host mismatch +failed to do SSL handshake: handshake failed --- error_log lua ssl server name: "aa.bb.test2.com" failed to find any SSL certificate by SNI: aa.bb.test2.com matched SNI: *.test2.com --- no_error_log -[error] [alert] @@ -641,11 +640,10 @@ location /t { GET /t --- response_body connected: 1 -failed to do SSL handshake: certificate host mismatch +failed to do SSL handshake: handshake failed --- error_log lua ssl server name: "www.test2.com" --- no_error_log -[error] [alert] @@ -853,12 +851,11 @@ location /t { GET /t --- response_body connected: 1 -failed to do SSL handshake: certificate host mismatch +failed to do SSL handshake: handshake failed --- error_log lua ssl server name: "aa.bb.test2.com" failed to find any SSL certificate by SNI: aa.bb.test2.com matched SNIs: ["*.test2.com","test2.com"] --- no_error_log -[error] [alert]