You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by "Crazy-xyr (via GitHub)" <gi...@apache.org> on 2025/09/28 05:31:53 UTC

[I] feat: As a user, I want to healthcheck add mtls support [apisix]

Crazy-xyr opened a new issue, #12641:
URL: https://github.com/apache/apisix/issues/12641

   ### Description
   
   When the upstream needs to use mtls, healthcheck does not reuse ssl certificates, so that all health checks return http 400 code.
   
   Hope to be able to support such scenarios


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [I] feat: As a user, I want to healthcheck add mtls support [apisix]

Posted by "laz-xyr (via GitHub)" <gi...@apache.org>.
laz-xyr commented on issue #12641:
URL: https://github.com/apache/apisix/issues/12641#issuecomment-3707788546

   The official recommended mtls handshake is tcpsock:setclientcert and tcpsock:sslhandshake. The resty.healthcheck library is also implemented using this method.
   https://github.com/api7/lua-resty-healthcheck/blob/09a672e0784c75231ceca51e0a4cb39313c020f5/lib/resty/healthcheck.lua#L1062-L1073
   But in patch.lua, through 
   ```lua
   local params = { 
            mode = "client", 
            protocol = "any", 
            verify = verify and "peer" or "none", 
            certificate = options.client_cert_path, 
            key = options.client_priv_key_path, 
            options = { 
                "all", 
                "no_sslv2", 
                "no_sslv3", 
                "no_tlsv1" 
            } 
        } 
   local sec_sock, err = ssl.wrap(self.sock, params)
   
   if not sec_sock then
   
   return false, err
   
   end
   ```
   replaced setclientcert,  requires passing the certificate when calling sslhandshake.
   In the above test unit,  the resty.healthcheck library  to read the incoming upstream mTLS certificate configuration and  set the certificate through setclientcert, then  call `sock:sslhandshake(nil, https_sni,self.checks.active.https_verify_certificate)`  without the incoming certificate 


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [I] feat: As a user, I want to healthcheck add mtls support [apisix]

Posted by "laz-xyr (via GitHub)" <gi...@apache.org>.
laz-xyr commented on issue #12641:
URL: https://github.com/apache/apisix/issues/12641#issuecomment-3775728955

   @Baoyuantop It seems that no anyone is handling [pr](https://github.com/api7/lua-resty-healthcheck/pull/50).


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [I] feat: As a user, I want to healthcheck add mtls support [apisix]

Posted by "Crazy-xyr (via GitHub)" <gi...@apache.org>.
Crazy-xyr commented on issue #12641:
URL: https://github.com/apache/apisix/issues/12641#issuecomment-3342344616

   Currently, I can only rely on balancer.retries, but this will result in unnecessary invalid requests. The access_log records status as 5xx,200


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [I] feat: As a user, I want to healthcheck add mtls support [apisix]

Posted by "Baoyuantop (via GitHub)" <gi...@apache.org>.
Baoyuantop commented on issue #12641:
URL: https://github.com/apache/apisix/issues/12641#issuecomment-3707588752

   This is a feature-request issue, not a bug. APISIX's health check module was not designed with mTLS in mind:
   
   1. Schema level: The health check configuration lacks client certificate-related configuration fields.
   2. Implementation level: The `create_checker` function in `healthcheck_manager.lua` does not read and pass the upstream mTLS certificate configuration to the underlying `resty.healthcheck` library.
   
   The underlying library already has full mTLS capabilities; however, APISIX does not read the upstream TLS configuration and pass the ssl_cert and ssl_key parameters when calling healthcheck.new


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [I] feat: As a user, I want to healthcheck add mtls support [apisix]

Posted by "YapWC (via GitHub)" <gi...@apache.org>.
YapWC commented on issue #12641:
URL: https://github.com/apache/apisix/issues/12641#issuecomment-3706875914

   Hi I can verify that the most probable root cause is with ngx.socket.tcp():setclientcert not sure what is causing the error where client cert is not being sent to upstream.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [I] feat: As a user, I want to healthcheck add mtls support [apisix]

Posted by "laz-xyr (via GitHub)" <gi...@apache.org>.
laz-xyr commented on issue #12641:
URL: https://github.com/apache/apisix/issues/12641#issuecomment-3707922296

   
   
   > This is a feature-request issue, not a bug. APISIX's health check module was not designed with mTLS in mind:
   > 
   > 1. Schema level: The health check configuration lacks client certificate-related configuration fields.
   > 2. Implementation level: The `create_checker` function in `healthcheck_manager.lua` does not read and pass the upstream mTLS certificate configuration to the underlying `resty.healthcheck` library.
   > 
   > The underlying library already has full mTLS capabilities; however, APISIX does not read the upstream TLS configuration and pass the ssl_cert and ssl_key parameters when calling healthcheck.new
   
   @YapWC @Baoyuantop 
   The official recommendation for the mtls handshake is tcpsock:setclientcert and tcpsock:sslhandshake. The resty.healthcheck library is also implemented using this method.
   
   However, apisix additionally implements the tcpsock:tlshandshake(https://github.com/api7/apisix-nginx-module/pull/1) method.
   
   Local test result: Method tcpsock:tlshandshake can handle the mtls handshake, while tcpsock:sslhandshake will fail.
   
   But sslhandshake(in the above test 2) can mtls handshake in the original openresty environment, I don't know where apisix broke this function
   
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [I] feat: As a user, I want to healthcheck add mtls support [apisix]

Posted by "laz-xyr (via GitHub)" <gi...@apache.org>.
laz-xyr commented on issue #12641:
URL: https://github.com/apache/apisix/issues/12641#issuecomment-3709101454

   @YapWC  
   
   > it suppose to be able to perform mTLS as the `resty.healtcheck` lib actual do support mTLS
   
   Yes.
   Just like the README.md of the `resty.healtcheck` library, it supports mTLS, 
   but the apisix-nginx-module library breaks the official `tcpsock:sslhandshake` function And made `sslhandshake` point to its custom `tlshandshake` function . 
   https://github.com/api7/apisix-nginx-module/blob/0327bd759eeab36f2fa20b1f8b80eaf1239c1298/patch/1.27.1.1/lua-resty-core-tlshandshake.patch#L299-L335
   
   The mTLS certificate of the tlshandshake function requires `opts` parameter passing instead of tcpsock:setclientcert. like `TEST 4`
    This causes the `healtcheck` library to be unable to use the passed certificate. 
   
   We need to make `resty.healtcheck` library use tlshandshake function first, just like [resty.http](https://github.com/api7/lua-resty-http/blob/f2a90e38c7b7bac47cba2d08fcdeec351683c596/lib/resty/http_connect.lua#L237C9-L250) 
   @Baoyuantop  i can commit pr to api7's `resty.healtcheck` library ?
   
   Only in this way can the `create_checker` function add the mTLS function
   
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [I] feat: As a user, I want to healthcheck add mtls support [apisix]

Posted by "Crazy-xyr (via GitHub)" <gi...@apache.org>.
Crazy-xyr commented on issue #12641:
URL: https://github.com/apache/apisix/issues/12641#issuecomment-3342331638

   The important api interfaces in the upstream need to be connected via mtls. My apisix supports both internal dedicated  (priority) and public network access.I pass the health check detection. When the dedicated fails, connected to the upstream using the public network


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [I] feat: As a user, I want to healthcheck add mtls support [apisix]

Posted by "Crazy-xyr (via GitHub)" <gi...@apache.org>.
Crazy-xyr commented on issue #12641:
URL: https://github.com/apache/apisix/issues/12641#issuecomment-3345601817

   @Baoyuantop I found through the current `lua-resty-healthcheck-api7 = 3.2.0` test that healthcheck is unable to send the client certificate. Check the source code, is found ngx.socket.tcp():[setclientcert](https://github.com/openresty/lua-nginx-module?tab=readme-ov-file#tcpsocksetclientcert)  that sets the client certificate, but does not send it during the sock:handshake().
   
   Does `apisix` have any additional patch or modifications to `ngx.socket.tcp()` ? The same TEST1 `content_by_lua_block` works in the openresty environment
   
   healthcheck.lua [source code](https://github.com/api7/lua-resty-healthcheck/blob/09a672e0784c75231ceca51e0a4cb39313c020f5/lib/resty/healthcheck.lua#L1036-L1158)
   <details>
   <summary> Reproduce in the test unit </summary>
   
   ```t
   
   BEGIN {
       if ($ENV{TEST_EVENTS_MODULE} ne "lua-resty-events") {
           $SkipReason = "Only for lua-resty-events events module";
       }
   }
   
   use t::APISIX 'no_plan';
   
   repeat_each(1);
   log_level('info');
   no_root_location();
   no_shuffle();
   worker_connections(256);
   
   
   add_block_preprocessor(sub {
       my ($block) = @_;
   
       if (!$block->http_config) {
           my $http_config = <<'_EOC_';
   lua_shared_dict test_shm 8m;
   
   server {
       listen 8765 ssl;
       ssl_certificate ../../certs/mtls_server.crt;
       ssl_certificate_key ../../certs/mtls_server.key;
       ssl_client_certificate ../../certs/mtls_ca.crt;
       ssl_verify_client on;
   
       location /healthz {
           return 200 'ok';
       }
   }
   
   _EOC_
           $block->set_value("http_config", $http_config);
       }
   
       if (!$block->request) {
           $block->set_value("request", "GET /t");
       }
   
   });
   
   run_tests();
   
   __DATA__
   
   === TEST 1: mtls check via healthcheck
   --- config
       location /t {
           content_by_lua_block {
               local events = require("apisix.events")
   
               -- ngx.say(events:get_healthcheck_events_module())
   
               local f = assert(io.open('/workspace/t/certs/mtls_client.crt'))
               local cert_data = f:read('*a')
               f:close()
               f = assert(io.open('/workspace/t/certs/mtls_client.key'))
               local key_data = f:read('*a')
               f:close()
   
               local healthcheck = require("resty.healthcheck")
               local checker = healthcheck.new({
                   name = "testing",
                   shm_name = "test_shm",
                   events_module = events:get_healthcheck_events_module(),
                   ssl_cert = cert,
                   ssl_key = key,
                   checks = {
                   active = {
                           type = "https",
                           https_verify_certificate = false,
                           http_path = "/healthz",
                           healthy  = {
                               interval = 1,
                               successes = 3
                           },
                           unhealthy  = {
                               interval = 1,
                               http_failures = 3,
                           }
                       },
                   }
               })
   
               local ok, err = checker:add_target("127.0.0.1", 8765, "127.0.0.1", false)
               ngx.sleep(3) -- wait for check
               ngx.status = 200
               ngx.say("passed")
           }
           
       }
   --- request
   GET /t
   --- response_body
   passed
   --- error_log
   client sent no required SSL certificate
   
   
   
   === TEST 2: mtls check via socket
   --- config
       location /t {
           content_by_lua_block {
               local f = assert(io.open('/workspace/t/certs/mtls_client.crt'))
               local cert_data = f:read('*a')
               f:close()
               f = assert(io.open('/workspace/t/certs/mtls_client.key'))
               local key_data = f:read('*a')
               f:close()
   
   
               local ssl = require('ngx.ssl')
               local cert = assert(ssl.parse_pem_cert(cert_data))
               local key = assert(ssl.parse_pem_priv_key(key_data))
               ngx.log(ngx.ERR, "cert: ", type(cert)," key: ",type(key))
   
   
               local sock = ngx.socket.tcp()
               sock:settimeout(6000) 
   
               local ok, err = sock:connect("127.0.0.1", 8765)
               if not ok then
                   ngx.say("err: ", err)
                   return
               end
   
               ok, err = sock:setclientcert(cert, key)
               if not ok then
                   ngx.say("err: ", err)
                   sock:close()
                   ngx.status = 500
                   return
               end
   
               session, err = sock:sslhandshake(nil,"127.0.0.1")
               if not session then
                   ngx.say("err: ", err)
                   sock:close()
                   ngx.status = 500
                   return
               end
   
               local req = "GET /healthz HTTP/1.1\r\nConnection: close\r\nHost: 127.0.0.1\r\n\r\n"
               ok, err = sock:send(req)
               if not ok then
                   ngx.say("err: ", err)
                   sock:close()
                   ngx.status = 500
                   return
               end
   
               local response, err = sock:receive()
               if not response then
                   ngx.say("err: ", err)
                   sock:close()
                   ngx.status = 500
                   return
               end
   
               local re_find = ngx.re.find
               local from, to = re_find(response,
                             [[^HTTP/\d+\.\d+\s+(\d+)]],
                             "joi", nil, 1)
               local status
   
               if from then
                   status = tonumber(response:sub(from, to))
               else
                   status = 500
               end
   
               ngx.status = status
               ngx.say(response)
           }
           
       }
   --- request
   GET /t
   --- error_code: 400
   --- response_body
   HTTP/1.1 400 Bad Request
   --- error_log
   cert: cdata key: cdata
   client sent no required SSL certificate
   ```
   </details>


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [I] feat: As a user, I want to healthcheck add mtls support [apisix]

Posted by "laz-xyr (via GitHub)" <gi...@apache.org>.
laz-xyr commented on issue #12641:
URL: https://github.com/apache/apisix/issues/12641#issuecomment-3717898966

   The healthcheck pr has been submitted
   https://github.com/api7/lua-resty-healthcheck/pull/50


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [I] feat: As a user, I want to healthcheck add mtls support [apisix]

Posted by "YapWC (via GitHub)" <gi...@apache.org>.
YapWC commented on issue #12641:
URL: https://github.com/apache/apisix/issues/12641#issuecomment-3708995157

   > 2\. Implementation level: The `create_checker` function in `healthcheck_manager.lua` does not read and pass the upstream mTLS certificate configuration to the underlying `resty.healthcheck` library.
   
   We are aware that it is not passed in `create_checker` function but the test created by @laz-xyr did pass the ssl_cert and ssl_key.
   
   Test 1:
   ```
   ...
               local healthcheck = require("resty.healthcheck")
               local checker = healthcheck.new({
                   name = "testing",
                   shm_name = "test_shm",
                   events_module = events:get_healthcheck_events_module(),
                   ssl_cert = cert_data,
                   ssl_key = key_data,
                   checks = {
                   active = {
                           type = "https",
                           https_verify_certificate = false,
                           http_path = "/healthz",
                           healthy  = {
                               interval = 1,
                               successes = 3
                           },
                           unhealthy  = {
                               interval = 1,
                               http_failures = 3,
                           }
                       },
                   }
               })
   ...
   ```
   And it suppose to be able to perform mTLS as the `resty.healtcheck` lib actual do support mTLS, right?
   
   The problem here is that even after passing the required variable for mTLS, it was not performed and failed. Therefore, as suggested by @laz-xyr this could be where the potential issue lies.
   
   > [@YapWC](https://github.com/YapWC) [@Baoyuantop](https://github.com/Baoyuantop) The official recommendation for the mtls handshake is tcpsock:setclientcert and tcpsock:sslhandshake. The resty.healthcheck library is also implemented using this method.
   > 
   > However, apisix additionally implements the tcpsock:tlshandshake([api7/apisix-nginx-module#1](https://github.com/api7/apisix-nginx-module/pull/1)) method.
   > 
   > Local test result: Method tcpsock:tlshandshake can handle the mtls handshake, while tcpsock:sslhandshake will fail.
   > 
   > But sslhandshake(in the above test 2) can mtls handshake in the original openresty environment, I don't know where apisix broke this function
   
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [I] feat: As a user, I want to healthcheck add mtls support [apisix]

Posted by "laz-xyr (via GitHub)" <gi...@apache.org>.
laz-xyr commented on issue #12641:
URL: https://github.com/apache/apisix/issues/12641#issuecomment-3707763955

   > This is a feature-request issue, not a bug. APISIX's health check module was not designed with mTLS in mind:
   > 
   > 1. Schema level: The health check configuration lacks client certificate-related configuration fields.
   > 2. Implementation level: The `create_checker` function in `healthcheck_manager.lua` does not read and pass the upstream mTLS certificate configuration to the underlying `resty.healthcheck` library.
   > 
   > The underlying library already has full mTLS capabilities; however, APISIX does not read the upstream TLS configuration and pass the ssl_cert and ssl_key parameters when calling healthcheck.new
   
   I only installed the resty.healthcheck library on openresty, and the result of mtls is working. But I manually modified create_checker and passed the upstream mTLS certificate configuration to the underlying resty.healthcheck library. The result is still the same problem. It's probably that ngx.socket.tcp() has been patched by apisix. I found a piece of code. I'm not sure if it's due to this. Further testing is needed
   
   https://github.com/apache/apisix/blob/b4c1527035f020b06e6198131e2dd94ef42dc144/apisix/patch.lua#L267-L330


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [I] feat: As a user, I want to healthcheck add mtls support [apisix]

Posted by "Baoyuantop (via GitHub)" <gi...@apache.org>.
Baoyuantop commented on issue #12641:
URL: https://github.com/apache/apisix/issues/12641#issuecomment-3717042755

   Hi @laz-xyr, we welcome detailed technical solutions and will review them.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [I] feat: As a user, I want to healthcheck add mtls support [apisix]

Posted by "laz-xyr (via GitHub)" <gi...@apache.org>.
laz-xyr commented on issue #12641:
URL: https://github.com/apache/apisix/issues/12641#issuecomment-3707932005

   Local tlshandshake test 
   <details>
   <summary> t/node/healthcheck4.t </summary>
   
   ``` t
   
   === TEST 4: mtls check via socket:tlshandshake
   --- config
       location /t {
           content_by_lua_block {
   
               local sock = ngx.socket.tcp()
               sock:settimeout(6000) 
   
               local ok, err = sock:connect("127.0.0.1", 8765)
               if not ok then
                   ngx.say("err: ", err)
                   return
               end
   
               local session, err = sock:tlshandshake(opts)
               if not session then
                   ngx.say("err: ", err)
                   sock:close()
                   ngx.status = 500
                   return
               end
   
               local req = "GET /healthz HTTP/1.1\r\nConnection: close\r\nHost: 127.0.0.1\r\n\r\n"
               ok, err = sock:send(req)
               if not ok then
                   ngx.say("err: ", err)
                   sock:close()
                   ngx.status = 500
                   return
               end
   
               local response, err = sock:receive()
               if not response then
                   ngx.say("err: ", err)
                   sock:close()
                   ngx.status = 500
                   return
               end
   
               local re_find = ngx.re.find
               local from, to = re_find(response,
                             [[^HTTP/\d+\.\d+\s+(\d+)]],
                             "joi", nil, 1)
               local status
   
               if from then
                   status = tonumber(response:sub(from, to))
               else
                   status = 500
               end
   
               ngx.status = status
               ngx.say(response)
           }
           
       }
   --- request
   GET /t
   --- response_body
   HTTP/1.1 200 OK
   --- no_error_log
   
   ```
   
   </details>
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [I] feat: As a user, I want to healthcheck add mtls support [apisix]

Posted by "Baoyuantop (via GitHub)" <gi...@apache.org>.
Baoyuantop commented on issue #12641:
URL: https://github.com/apache/apisix/issues/12641#issuecomment-3342717165

   This request seems useful. Welcome to share detailed proposals.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org