From 5897ea9f99bdee56c417132a88d4b83e626bdde0 Mon Sep 17 00:00:00 2001 From: "Stefan J. Betz" Date: Mon, 30 Sep 2024 18:30:28 +0200 Subject: [PATCH] completed first working version --- main.go | 37 ++++++++++++++++++++++++++++++++++--- 1 file changed, 34 insertions(+), 3 deletions(-) diff --git a/main.go b/main.go index 8e63c0a..9552ef6 100644 --- a/main.go +++ b/main.go @@ -2,8 +2,10 @@ package main import ( "encoding/csv" + "errors" "fmt" "os" + "strings" "time" ) @@ -20,6 +22,12 @@ const ( CERT_CN ) +const ( + OUTPUT_EXPIRED = "CRITICAL: already expired" + OUTPUT_EXPIRES_30D = "WARNING: expires in < 30d" + OUTPUT_EXPIRES_90D = "INFO: expires in < 90d" +) + var TIME_90D = time.Now().Add(90 * 24 * time.Hour) var TIME_30D = time.Now().Add(30 * 24 * time.Hour) var TIME_NOW = time.Now() @@ -77,15 +85,38 @@ func (c *Certificates) process_line(line []string) { } } +func (c *Certificate) get_certificate_cn() (*string, error) { + t := strings.Split(*c.CN, "/") + for _, item := range t { + if strings.HasPrefix(item, "CN=") { + cn := strings.TrimPrefix(item, "CN=") + return &cn, nil + } + } + return nil, errors.New("No CN found!") +} + func (c *Certificates) print_expired() { for _, cert := range c.Certificates { + var cn *string + var err error + var expired string if cert.Expired.Before(TIME_NOW) { - fmt.Println("That certificate is already expired!") + cn, err = cert.get_certificate_cn() + expired = OUTPUT_EXPIRED } else if cert.Expired.Before(TIME_30D) { - fmt.Println("This certificate will expire in <= 30d") + cn, err = cert.get_certificate_cn() + expired = OUTPUT_EXPIRES_30D } else if cert.Expired.Before(TIME_90D) { - fmt.Println("This certificate will expire in <= 90d") + cn, err = cert.get_certificate_cn() + expired = OUTPUT_EXPIRES_90D + } else { + continue + } + if err != nil { + panic(err) } + fmt.Println("Expiry Date:", cert.Expired.Format(time.DateOnly), "CN:", *cn, "SN:", *cert.Serial, expired) } }