cockroach icon indicating copy to clipboard operation
cockroach copied to clipboard

security: `security_certificate_expiration_node_client` metric not updated after SIGHUP cert reload

Open aa-joshi opened this issue 8 months ago • 4 comments

Summary

The security_certificate_expiration_node_client Prometheus metric does not reflect the updated expiry value after a certificate reload via SIGHUP. The metric only updates after a full node restart. This affects both 24.3.11 and 25.2.4/25.2.6.

This is a distinct issue from the security_certificate_expiration_client cache bug (GH #142681 / #151040), which is about the per-SQL-user cache. This metric (node_client) is backed by a FunctionalGauge with a direct closure — no cert cache is involved.

Steps to Reproduce

  1. Create a cluster with a node client certificate (client.node.crt).
  2. Replace the certificate using --overwrite and a new --lifetime flag (same filename, same directory).
  3. Send SIGHUP to the CockroachDB process:
    kill -HUP <cockroach_pid>
    
  4. Verify the reload succeeded — logs should show certs_reload: Success: true.
  5. Confirm new expiry is on disk:
    cockroach cert list
    
  6. Scrape the metric:
    curl -sk https://localhost:8080/_status/vars | grep 'security_certificate_expiration_node_client'
    

Expected Behavior

The metric reflects the new certificate's expiry timestamp after the SIGHUP reload.

Actual Behavior

The metric remains stuck on the old expiry value. Bouncing the node (full restart) causes the metric to update correctly.

Root Cause Analysis

Traced through release-25.2:

  • SIGHUP → cm.LoadCertificates() → creates a new CertificateLoader, reads cert files from disk, parses them, and under cm.mu.Lock() swaps cm.nodeClientCert with the new CertInfo. This path appears correct and the success log confirms the new cert is parsed.
  • NodeClientExpiration is a *metric.Gauge created via NewFunctionalGauge. On each Prometheus scrape: ToPrometheusMetric() → Value() → fn() → reads cm.nodeClientCert → returns ci.ExpirationTime.Unix().

However, the closure in NodeClientExpiration reads cm.nodeClientCert without acquiring cm.mu.RLock(), unlike NodeExpiration which goes through cm.NodeCert() (a method that properly acquires the read lock). This is a data race that could cause the scrape to see a stale value of cm.nodeClientCert — potentially indefinitely depending on CPU/memory visibility.

The same race condition likely affects other metrics that read cert fields directly without the lock.

Additional Context

  • Customer replaces client.node.crt and client.root.crt in-place (same filenames, same directory), sends SIGHUP to the correct PID (verified via ps -fp), and sees certs_reload: Success: true in logs.
  • cockroach cert list shows the correct new expiry on disk post-reload.
  • Prometheus metric remains stale until node restart.

Jira issue: CRDB-62054

aa-joshi avatar Mar 25 '26 06:03 aa-joshi

Hi @aa-joshi, please add a branch-* label to identify the earliest affected branch for this C-bug

:owl: Hoot! I am a Blathers, a bot for CockroachDB. My owner is dev-inf.

blathers-crl avatar Mar 25 '26 06:03 blathers-crl

This issue has multiple T-eam labels. Please make sure it only has one, or else issue synchronization will not work correctly.

:owl: Hoot! I am a Blathers, a bot for CockroachDB. My owner is dev-inf.

blathers-crl avatar Mar 25 '26 06:03 blathers-crl

This issue has multiple T-eam labels. Please make sure it only has one, or else issue synchronization will not work correctly.

:owl: Hoot! I am a Blathers, a bot for CockroachDB. My owner is dev-inf.

blathers-crl avatar Mar 25 '26 06:03 blathers-crl

Hi @aa-joshi, please add a branch-* label to identify the earliest affected branch for this C-bug

:owl: Hoot! I am a Blathers, a bot for CockroachDB. My owner is dev-inf.

blathers-crl avatar Mar 25 '26 12:03 blathers-crl