- Published on
Calculate if certificate expiry is within duration
502 words3 min read
func main() {
pk, err := os.ReadFile("./domain.crt")
if err != nil {
log.Fatal("error reading file: ", err)
}
pp, _ := pem.Decode(pk)
if pp == nil {
log.Fatal("unable to decode public key")
}
cert, err := x509.ParseCertificate(pp.Bytes)
if err != nil {
log.Fatal("unable to parse certificate: ", err)
}
{
fmt.Println()
fmt.Println("Duration: 3 Hours")
fmt.Println("==================")
fmt.Printf("Current date/time: %s\n", time.Now().Format(time.RFC3339))
fmt.Printf("Certificate Expiration: %s\n", cert.NotAfter.Format(time.RFC3339))
d, err := time.ParseDuration("3h")
if err != nil {
log.Fatal("unable to parse duration: ", err)
}
if time.Now().Add(d).Before(cert.NotAfter) {
fmt.Printf("Status: %s\n", "Certificate not ready for rotation")
} else {
fmt.Printf("Status: %s\n", "Certificate is ready for rotation")
}
}
{
fmt.Println()
fmt.Println("Duration: 125 Hours")
fmt.Println("==================")
fmt.Printf("Current date/time: %s\n", time.Now().Format(time.RFC3339))
fmt.Printf("Certificate Expiration: %s\n", cert.NotAfter.Format(time.RFC3339))
d, err := time.ParseDuration("120h")
if err != nil {
log.Fatal("unable to parse duration: ", err)
}
if time.Now().Add(d).Before(cert.NotAfter) {
fmt.Printf("Status: %s\n", "Certificate not ready for rotation")
} else {
fmt.Printf("Status: %s\n", "Certificate is ready for rotation")
}
}
}