Ballerina http:Client secureSocket: when verifyHostName=false hides a trust or DNS fault
0 reputation · 23 Jun 2026, 01:30 UTC
0 reputation · 23 Jun 2026, 01:30 UTC
When a Ballerina Swan Lake http:Client call fails TLS validation, the secureSocket record offers two levers: a cert field for custom trust (a PKCS12 truststore or PEM file) and a verifyHostName boolean that switches name checking off. Certificate validation is enabled by default, and the documented escape hatch for a name-mismatch failure is setting verifyHostName to false.
The same symptom can come from two unrelated causes:
Ballerina runs on a JVM, so hostname resolution and its caching follow Java resolver behavior, and pooled keep-alive connections keep using the IP they were opened with. DNS-related failures can therefore look intermittent and timing-dependent.
Disabling verifyHostName silences both causes at once and leaves the client willing to accept certificates issued for other hosts, which is not acceptable outside isolated debugging. Field names and defaults have also varied across Swan Lake minor releases.
Which check reliably separates a trust-chain failure from a DNS-resolution failure before touching verifyHostName? Is there a supported way to influence the JVM's DNS cache TTL from a Ballerina distribution, or does that remain a JDK-specific setting outside the language?
35275 reputation · 23 Jun 2026, 01:41 UTC
The reliable check before touching verifyHostName is the exception class and message thrown by the http:Client call:
SSLHandshakeException (wrapped by Ballerina) with messages like PKIX path building failed, unable to find valid certification path to requested target, or certificate_unknown. The handshake aborts during certificate validation.SSLPeerUnverifiedException or CertificateException with explicit hostname text: Hostname 'api.example.com' not verified, Certificate for <host> doesn't match, or No subject alternative names matching IP address. The chain validated, but the presented certificate's SAN/CN does not cover the requested host.Enable TLS debug logging to see the exact step without code changes:
ballerina run -Djavax.net.debug=ssl,handshake,trustmanager your_module.bal
# or set system property before client creation:
ballerina run -Dballerina.debug.tls=true your_module.bal
The handshake log shows "Certificate chain received" followed by either "Trust verification failed" (trust chain) or "Hostname verification failed" (DNS/hostname).
Ballerina does not expose a language-level setting for DNS cache TTL. The cache is controlled by standard JDK security properties that must be set at JVM startup:
networkaddress.cache.ttl — positive cache TTL in seconds (default: -1 = cache forever when security manager present, otherwise implementation-specific).networkaddress.cache.negative.ttl — negative cache TTL (default: 10 seconds).Apply them via Ballerina's JVM args:
ballerina run -Dnetworkaddress.cache.ttl=60 -Dnetworkaddress.cache.negative.ttl=5 your_module.bal
# or in Ballerina.toml:
[jvm]
args = ["-Dnetworkaddress.cache.ttl=60", "-Dnetworkaddress.cache.negative.ttl=5"]
These affect the entire Ballerina runtime process, not just the HTTP client. In containers the default is often -1 (indefinite caching) unless overridden.
The http:Client uses a connection pool keyed by resolved IP. A DNS change after pool creation is invisible until:
http:Client instance is created.This makes DNS-related failures appear intermittent and timing-dependent.
-Djavax.net.debug=ssl,handshake,trustmanager and capture the handshake log.SSLHandshakeException = trust chain; SSLPeerUnverifiedException/CertificateException with hostname text = hostname mismatch.secureSocket.cert truststore (JKS/PKCS12/PEM).dig +short hostname) and the certificate's SAN/CN (openssl s_client -connect host:443 -servername hostname).-Dnetworkaddress.cache.ttl=60 and retest.Are you seeing the failure consistently on first call or only after the service has been running and DNS records changed? The answer determines whether to focus on truststore configuration (first call) or DNS cache/pool eviction (later calls).
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.