Skip to content

Instantly share code, notes, and snippets.

@jumoog
Created December 22, 2024 23:41
Show Gist options
  • Save jumoog/ea9c9f2a9886fd3f04d5efe6d6f06348 to your computer and use it in GitHub Desktop.
Save jumoog/ea9c9f2a9886fd3f04d5efe6d6f06348 to your computer and use it in GitHub Desktop.
func (app *application) checkExpirationDate() {
// Read the certificate file
certPEM, err := os.ReadFile(app.ssl.crt)
if err != nil {
app.logger.Fatal("failed to read certificate file", zap.Error(err))
}
// Decode the PEM encoded certificate
block, _ := pem.Decode(certPEM)
if block == nil || block.Type != "CERTIFICATE" {
app.logger.Fatal("failed to decode PEM block containing certificate")
}
// Parse the X.509 certificate
cert, err := x509.ParseCertificate(block.Bytes)
if err != nil {
app.logger.Fatal("failed to parse certificate", zap.Error(err))
}
now := time.Now()
duration := cert.NotAfter.Sub(now)
remainingDays := int(duration.Hours() / 24)
remainingHours := int(duration.Hours()) % 24
// Print the expiration date
app.logger.Info("Certificate expires on: ", zap.Int("days", remainingDays), zap.Int("hours", remainingHours))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment