Monitoring SSL certificate expiry with a Go script
The familiar phrase SSL certificate usually means an X.509 certificate used by TLS. When one expires, every client that enforces normal validation can reject the service, turning a renewal mistake into a full outage. Monitoring availability alone may miss the cause because the endpoint remains online while its identity can no longer be trusted.
A focused Go executable fills the SSL certificate expiry gap. It performs a real TLS handshake with the correct server name, reads the leaf certificate’s NotAfter value, and reports the time remaining. A static binary is easy to run on a small Linux server, in a container, or on a workstation without adding a certificate-monitoring framework to the stack.
Australian infrastructure makes the details worth planning. A SaaS business in Sydney may use a Sydney or Melbourne cloud region, while staff in Brisbane or Perth reach the same public service over different paths. DNS can return different edge nodes or certificate chains, so the check should target the public hostname rather than a convenient origin IP.
The operational context includes local rules and working habits. A .com.au domain must remain eligible and current with an auDA-accredited registrar, while the Privacy Act 1988 and Australian Privacy Principles may apply if inventories contain customer identifiers. Teams also need to respect daylight-saving changes across the Australian Eastern and Central time zones because a certificate deadline is an instant even when a roster is not.
Why expiry belongs in operational monitoring
Begin with the certificate a customer receives, not the certificate stored on an origin server. A CDN, load balancer, API gateway or reverse proxy may terminate TLS and present a different chain from the application host. The checker must therefore use the public DNS name, send it as SNI, and connect to the port that customers use.
Useful background appears in my network automation notes, but the authoritative target file should record the owner, environment, renewal method and consequence of failure. Without that metadata, an alert becomes noise that even a capable on-call engineer learns to ignore.
The checker should answer three separate questions: can the endpoint complete a handshake, which leaf certificate was presented, and when does that certificate expire? Keeping expiry distinct from availability prevents a DNS outage, firewall block or expired intermediate chain from being reported as the same condition. Chain elements can also be recorded separately when internal certificate authorities are in scope.
Build a small, verifiable Go checker
Go suits this job because its TLS and x509 packages expose the full peer chain, while a single static executable can run on nearly every target. Keep the first version deliberately small: one hostname and port per target, a ten-second timeout, UTC timestamps and thresholds for warning, critical and expired states.
The core handshake function can look like this:
package main
import (
"context"
"crypto/tls"
"crypto/x509"
"errors"
"net"
"time"
)
func expiry(ctx context.Context, address string) (time.Time, error) {
host, _, err := net.SplitHostPort(address)
if err != nil {
return time.Time{}, err
}
dialer := &tls.Dialer{
NetDialer: &net.Dialer{},
Config: &tls.Config{
ServerName: host,
MinVersion: tls.VersionTLS12,
},
}
conn, err := dialer.DialContext(ctx, "tcp", address)
if err != nil {
var invalid x509.CertificateInvalidError
if errors.As(err, &invalid) && invalid.Cert != nil {
return invalid.Cert.NotAfter, nil
}
return time.Time{}, err
}
defer conn.Close()
certs := conn.ConnectionState().PeerCertificates
if len(certs) == 0 {
return time.Time{}, errors.New("no peer certificate")
}
return certs[0].NotAfter, nil
}
The surrounding main function should iterate over a target inventory, calculate whole days remaining from time.Until, and emit JSON containing the hostname, expiry time, status and check timestamp. Handling an x509.CertificateInvalidError is important because an expired certificate can fail validation before a clean connection is returned, while Go can still expose the offending certificate in the error.
Do not set InsecureSkipVerify. Normal validation proves that the presented certificate is valid for the requested name and chains to a trusted root. Record a separate endpoint error when the handshake fails for another reason, so responders can distinguish a broken route from a certificate whose NotAfter has passed.
Schedule checks across hybrid infrastructure
Keep the target inventory outside the binary so adding an Australian edge or a self-hosted appliance does not require a rebuild. A plain JSON or YAML file can group production, corporate and lab services. CI should validate duplicate hostnames and reject entries without an owner, which is especially useful when teams in Sydney, Melbourne and Brisbane maintain the same catalogue.
Run the check daily at a fixed UTC time, then calculate local display times in the alert. Midnight UTC lands during Australian business hours and remains stable when daylight-saving clocks move. For critical certificates, repeat every few hours during the final week; for long-lived internal names, a daily check is usually enough.
A systemd timer, cron job or Kubernetes CronJob all work. The same pattern works on a bare-metal Kubernetes cluster, where a CronJob can run the container and publish a metric. On mixed estates, retain the static Linux binary and avoid tying expiry visibility to one cloud provider’s managed certificate service.
Design alerts that lead to action
Most teams need three warning points rather than one: 30 days for routine renewal, 14 days for escalation, and 72 hours for immediate action. Thresholds should reflect the actual lead time of the certificate authority, registrar, DNS provider and deployment pipeline. A ninety-day publicly issued certificate still needs replacement on a predictable schedule, while a long-lived internal certificate can use a different path.
Each target record should include:
- Public hostname and port, including any non-standard TLS endpoint
- Responsible team, escalation contact and linked runbook
- Environment, customer impact and certificate renewal method
- Expected lead time and a ticket or change reference
Each alert should include:
- Hostname and certificate serial number
- Expiry timestamp in UTC and the corresponding Australian local date
- Days remaining and current status
- Owning team and direct link to the renewal runbook
- Time of the check and the monitoring source
Send actionable notifications to email and a team chat, then use SMS or a mobile push notification for the critical threshold. That reflects a common Australian on-call reality: the person carrying a phone may see a message before email, especially outside major cities. Deduplicate unchanged results and keep a small state record so a daily job does not send the same warning all week. If a URL or label combines customer data with identifiers, review the Notifiable Data Breaches scheme under the Privacy Act 1988 and keep personal fields out of the target file.
Put renewal into a tested runbook
An expiry alert is only useful if the response is rehearsed. The runbook should identify the renewal owner, show how to issue and deploy the replacement, and require verification through the public hostname. It should also cover cases where automation succeeds but DNS, a CDN or a secret store still serves the previous certificate.
Test the checker against an expired test certificate, a near-expiry certificate, a changed chain, an unreachable endpoint and a hostname with a missing certificate. Confirm that timestamps remain correct across daylight-saving changes and that an expired response is classified differently from a timeout. ACSC guidance on cyber resilience reinforces the same principle: prevention is cheaper when monitoring produces a specific, owned action.
Choose one customer-facing hostname today: add it to the target inventory, run the checker against it, and schedule the daily job. Exercise the alert through to the named owner, then expand to the remaining public services and internal trust chains.
Karl Katzke