TLS handshake errors on EC2 data pipeline after certificate expiration

Problem – TLS Handshake Failures After Certificate Expiration

A nightly data‑pipeline runs on an Amazon EC2 instance (Amazon Linux 2). The pipeline pulls JSON payloads from several external HTTPS APIs, transforms the data, and writes the result to an S3 bucket. On 2024‑09‑15 the pipeline stopped processing. The first log entry after the scheduled start was:


2024-09-15 02:00:01,342 ERROR pipeline.runner - Failed to fetch https://api.partner.com/v1/data: TLS handshake failed: certificate has expired

Subsequent attempts produced the same error for all external endpoints, causing the entire job to abort. The EC2 instance hosts a reverse‑proxy (Nginx) that terminates TLS for inbound traffic, but the outbound connections also rely on a locally‑installed client certificate chain that was bundled with the OS.

Root Cause – Expired Server Certificate Used for Outbound TLS

The instance’s /etc/pki/tls/certs/ca-bundle.crt file is a concatenation of the system CA store and a custom self‑signed CA that signed the client certificate used for mutual TLS with the external APIs. The CA certificate expired on 2024‑09‑10. When curl or the Java HTTP client validates the server’s certificate chain, OpenSSL checks the expiration of every certificate in the chain, including the custom CA. Because the CA is expired, the validation fails before the handshake can complete, resulting in the “certificate has expired” error.

Key points:

  • The pipeline does not use a dedicated certificate‑renewal mechanism; it relies on the OS CA bundle.
  • Automatic OS updates were disabled to avoid service restarts, so the CA bundle was never refreshed.
  • The expiration was not caught by monitoring because only inbound TLS (Nginx) was being checked.

Debug – Step‑by‑Step Investigation

1. Verify the failure with a direct request


$ curl -v https://api.partner.com/v1/data
*   Trying 203.0.113.45:443...
* Connected to api.partner.com (203.0.113.45) port 443 (#0)
* TLS handshake: TLSv1.3, TLS handshake failed: certificate has expired

2. Inspect the certificate chain presented by the server


$ openssl s_client -connect api.partner.com:443 -servername api.partner.com -showcerts 

Output (truncated):


Certificate chain
 0 s:/CN=api.partner.com
   i:/CN=Partner Intermediate CA
 1 s:/CN=Partner Intermediate CA
   i:/CN=Partner Root CA
 2 s:/CN=Partner Root CA
   i:/CN=Custom Self‑Signed CA (expired 2024‑09‑10)

3. Check the local CA bundle for the expired entry


$ grep -A2 "Custom Self‑Signed CA" -n /etc/pki/tls/certs/ca-bundle.crt
12345:-----BEGIN CERTIFICATE-----
12346:MIID... (base64 data)
12347:-----END CERTIFICATE-----

Inspect the certificate’s validity:


$ openssl x509 -noout -dates -in /etc/pki/tls/certs/ca-bundle.crt | grep -A1 "12345"
notBefore=Jan  1 00:00:00 2020 GMT
notAfter=Sep 10 23:59:59 2024 GMT

4. Confirm that the OS package manager would have updated the bundle


$ yum info ca-certificates
Installed Packages
Name        : ca-certificates
Version     : 2023.2.23
Release     : 73.amzn2
...

The package version is older than the latest available (2024.1.12), indicating that automatic updates are disabled.

Solution – Renew the Custom CA and Automate Bundle Refresh

1. Generate a new self‑signed CA (valid for 3 years)


# Generate private key
openssl genrsa -out custom-ca.key 4096

# Create new CA certificate
openssl req -x509 -new -nodes -key custom-ca.key \
  -sha256 -days 1095 \
  -subj "/CN=Custom Self‑Signed CA" \
  -out custom-ca.crt

2. Re‑sign the client certificate used for outbound TLS


# Existing client CSR
openssl req -new -key client.key -out client.csr -subj "/CN=pipeline-client"

# Sign with new CA
openssl x509 -req -in client.csr -CA custom-ca.crt -CAkey custom-ca.key \
  -CAcreateserial -out client.crt -days 730 -sha256

3. Update the system CA bundle

Append the new CA to the bundle and remove the expired entry.


# Backup current bundle
cp /etc/pki/tls/certs/ca-bundle.crt /etc/pki/tls/certs/ca-bundle.crt.bak

# Remove expired CA (line numbers may vary)
sed -i '/Custom Self‑Signed CA/,/END CERTIFICATE-----/d' /etc/pki/tls/certs/ca-bundle.crt

# Append new CA
cat custom-ca.crt >> /etc/pki/tls/certs/ca-bundle.crt

# Verify new CA is present
grep -A2 "Custom Self‑Signed CA" /etc/pki/tls/certs/ca-bundle.crt

4. Deploy the renewed client certificate to the pipeline process


# Assume the pipeline reads from /opt/pipeline/certs/
mv client.crt /opt/pipeline/certs/
mv client.key /opt/pipeline/certs/
chmod 600 /opt/pipeline/certs/client.key
chmod 644 /opt/pipeline/certs/client.crt

5. Restart the pipeline service


systemctl restart data-pipeline.service

6. Automate future renewals

  • Enable yum-cron (or dnf-automatic) to apply security updates, including ca-certificates.
  • Store the custom CA in AWS Secrets Manager and rotate it with a Lambda that updates the instance via SSM.
  • Add a CloudWatch metric alarm that checks the SSLHandshakeFailure count from the pipeline logs.

Verify – Confirm Handshake Success and Pipeline Continuity

1. Test a direct HTTPS request


$ curl -v https://api.partner.com/v1/data
*   Trying 203.0.113.45:443...
* Connected to api.partner.com (203.0.113.45) port 443 (#0)
* TLSv1.3 (OUT), TLS handshake, Client hello (1):
* TLSv1.3 (IN), TLS handshake, Server hello (2):
* TLSv1.3 (IN), TLS handshake, Encrypted Extensions (8):
* TLSv1.3 (IN), TLS handshake, Certificate (11):
* TLSv1.3 (IN), TLS handshake, Certificate Verify (15):
* TLSv1.3 (IN), TLS handshake, Finished (20):
* TLSv1.3 (OUT), TLS change cipher, Change cipher spec (1):
* TLSv1.3 (OUT), TLS handshake, Finished (20):
* SSL connection using TLSv1.3 / TLS_AES_256_GCM_SHA384
* Server certificate verified
> GET /v1/data HTTP/1.1
...

2. Check pipeline logs for successful fetch


2024-09-16 02:00:12,018 INFO pipeline.runner - Fetched 1,024 records from https://api.partner.com/v1/data
2024-09-16 02:00:15,342 INFO pipeline.writer - Uploaded 1,024 records to s3://my-bucket/pipeline/2024-09-16/
2024-09-16 02:00:16,001 INFO pipeline.runner - Job completed successfully

3. Validate the new CA is in the bundle


$ openssl x509 -noout -subject -dates -in /etc/pki/tls/certs/ca-bundle.crt | grep "Custom Self‑Signed CA"
subject= /CN=Custom Self‑Signed CA
notBefore=Oct  1 00:00:00 2024 GMT
notAfter=Oct  1 00:00:00 2027 GMT

Prevent – Operational Guardrails to Avoid Recurrence

  • Automated CA expiration monitoring: Use a Lambda that parses /etc/pki/tls/certs/ca-bundle.crt, extracts notAfter dates, and publishes a CloudWatch metric. Set an alarm for certificates expiring within 30 days.
  • Enable OS security updates: Ensure yum-cron is enabled and configured to apply ca-certificates updates without requiring a reboot.
  • Centralize custom CAs: Store custom CA certificates in AWS Secrets Manager with automatic rotation. Deploy via SSM Run Command during the rotation window.
  • Pipeline health checks: Add a lightweight “TLS probe” step at the start of each run that attempts a HEAD request to a known endpoint and fails fast if the handshake fails.
  • Version pinning: Avoid hard‑coding paths to the CA bundle; use the system’s SSL_CERT_FILE environment variable so updates propagate automatically.

Related Topic Hub: Cloud Infrastructure Troubleshooting Hub

FAQ

  1. Why did the inbound Nginx TLS termination continue to work while outbound calls failed?
    Outbound connections validate the full certificate chain, including the custom CA, which had expired. Nginx uses its own server certificate and a separate CA bundle that was still valid, so inbound TLS was unaffected.
  2. Can I ignore the expired CA by setting curl --insecure or disabling verification in the Java client?
    That would bypass validation but defeats the security purpose of mutual TLS and may expose the pipeline to man‑in‑the‑middle attacks. The proper fix is to renew the CA.
  3. How do I automate renewal of a self‑signed CA without manual intervention?
    Store the CA private key in AWS Secrets Manager, schedule a Lambda to generate a new CA before the current one expires, update the ca-bundle.crt on all instances via SSM, and rotate any dependent client certificates.
  4. What metric should I monitor to detect future handshake failures early?
    Create a CloudWatch metric filter on the pipeline log pattern "TLS handshake failed" and set an alarm for a threshold of >0 occurrences within a 5‑minute window.
  5. Is there a way to test the CA bundle without restarting services?
    Yes. Use openssl verify -CAfile /etc/pki/tls/certs/ca-bundle.crt /path/to/server.crt to validate a server certificate against the updated bundle. No service restart is required for the verification step.