Created
December 22, 2024 23:41
-
-
Save jumoog/ea9c9f2a9886fd3f04d5efe6d6f06348 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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