client: Only retry TLS dial on certificate verification errors

Closes #3642

Signed-off-by: Stéphane Graber <stgraber@stgraber.org>
This commit is contained in:
Stéphane Graber 2026-07-16 11:39:15 -04:00
parent d55af0889c
commit bd77bb04ab
No known key found for this signature in database
GPG Key ID: C638974D64792D67

View File

@ -118,8 +118,19 @@ func tlsHTTPClient(client *http.Client, tlsClientCert string, tlsClientKey strin
conn, err := tlsDial(network, addr, transport.TLSClientConfig, false)
if err != nil {
// We may have gotten redirected to a non-Incus machine
return tlsDial(network, addr, transport.TLSClientConfig, true)
// On certificate verification failure, we may have gotten redirected to a
// non-Incus machine, retry with the dialed address as the server name.
var certVerifyErr *tls.CertificateVerificationError
hostnameErr := x509.HostnameError{}
if errors.As(err, &certVerifyErr) || errors.As(err, &hostnameErr) {
conn, retryErr := tlsDial(network, addr, transport.TLSClientConfig, true)
if retryErr == nil {
return conn, nil
}
}
// Return the initial error as the retry error may be misleading.
return nil, err
}
return conn, nil