Monitoring SSL Certificate Expiry with Prometheus

  ·   3 min read

In today’s digital landscape, ensuring the security of your web applications is paramount. One critical aspect of this security is the management of SSL/TLS certificates. Expired certificates can lead to service disruptions and can compromise the trustworthiness of your applications. To prevent such scenarios, it’s essential to have a robust monitoring system in place. Prometheus, a leading open-source monitoring solution, can be effectively used to track SSL certificate expiry dates and alert you before they expire.

Why Monitor SSL Certificates?

SSL certificates are crucial for encrypting data between clients and servers. However, they come with an expiration date, and if not renewed in time, they can lead to:

  • Service Downtime: Users may be unable to access your services.
  • Security Risks: Expired certificates can be exploited by attackers.
  • Loss of Trust: Users may lose confidence in your service’s security.

Setting Up Prometheus for SSL Monitoring

To monitor SSL certificates with Prometheus, you can use the blackbox_exporter, a Prometheus exporter that allows probing of endpoints over HTTP, HTTPS, DNS, TCP, ICMP, and more. Here’s a step-by-step guide to setting it up:

Step 1: Install Blackbox Exporter

First, download and install the Blackbox Exporter. You can find the latest release on GitHub.

wget https://github.com/prometheus/blackbox_exporter/releases/download/v0.21.0/blackbox_exporter-0.21.0.linux-amd64.tar.gz
tar xvfz blackbox_exporter-0.21.0.linux-amd64.tar.gz
cd blackbox_exporter-0.21.0.linux-amd64
./blackbox_exporter

Step 2: Configure Blackbox Exporter

Create a configuration file blackbox.yml for the Blackbox Exporter. This file will define how the exporter probes your endpoints.

modules:
  http_2xx:
    prober: http
    timeout: 5s
    http:
      valid_http_versions: [ "HTTP/1.1", "HTTP/2" ]
      valid_status_codes: []  # Defaults to 2xx
      method: GET
      fail_if_ssl: false
      fail_if_not_ssl: false
      tls_config:
        insecure_skip_verify: false
  ssl_cert:
    prober: http
    timeout: 5s
    http:
      fail_if_ssl: false
      fail_if_not_ssl: false
      tls_config:
        insecure_skip_verify: false
      preferred_ip_protocol: "ip4"

Step 3: Configure Prometheus

Next, configure Prometheus to scrape metrics from the Blackbox Exporter. Add the following job to your prometheus.yml configuration file:

scrape_configs:
  - job_name: 'blackbox'
    metrics_path: /probe
    params:
      module: [ssl_cert]  # Use the 'ssl_cert' module
    static_configs:
      - targets:
        - https://yourdomain.com  # Replace with your domain
    relabel_configs:
      - source_labels: [__address__]
        target_label: __param_target
      - source_labels: [__param_target]
        target_label: instance
      - target_label: __address__
        replacement: 127.0.0.1:9115  # Blackbox Exporter's address

Step 4: Set Up Alerts

To ensure you are notified before a certificate expires, set up alerting rules in Prometheus. Create a file alert_rules.yml:

groups:
- name: ssl_cert_expiry
  rules:
  - alert: SSLCertificateExpiry
    expr: probe_ssl_earliest_cert_expiry - time() < 86400 * 30
    for: 10m
    labels:
      severity: warning
    annotations:
      summary: "SSL Certificate for {{ $labels.instance }} is expiring soon"
      description: "The SSL certificate for {{ $labels.instance }} will expire in less than 30 days."

Include this alerting rule in your Prometheus configuration:

rule_files:
  - "alert_rules.yml"

Step 5: Test Your Setup

Restart both the Blackbox Exporter and Prometheus to apply the new configurations. Verify that Prometheus is scraping the metrics and that alerts are firing as expected when certificates are nearing expiration.

Conclusion

By integrating Prometheus with the Blackbox Exporter, you can effectively monitor SSL certificate expiry dates and ensure timely renewals. This setup not only helps in maintaining service uptime but also enhances the security posture of your applications. With the right alerting mechanisms in place, you can proactively manage SSL certificates and avoid potential disruptions.

References