github.com/blend/go-sdk@v1.20220411.3/examples/certutil/notafter/main.go (about)

     1  /*
     2  
     3  Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file.
     5  
     6  */
     7  
     8  package main
     9  
    10  import (
    11  	"errors"
    12  	"fmt"
    13  	"log"
    14  	"time"
    15  
    16  	"github.com/blend/go-sdk/certutil"
    17  )
    18  
    19  const testCertChain = `-----BEGIN CERTIFICATE-----
    20  MIIDBDCCAeygAwIBAgIRAJmlkpC8Q9rL5W+8tNKDaz0wDQYJKoZIhvcNAQELBQAw
    21  ETEPMA0GA1UEChMGZ28tc2RrMB4XDTE5MDMyOTE3MTMxM1oXDTI0MDMyOTE3MTMx
    22  M1owKzEVMBMGA1UEChMMZ28tc2RrIHVzZXJzMRIwEAYDVQQDEwlkZXYubG9jYWww
    23  ggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQDQNLHSwaKJCBUHSP5MwQ/R
    24  f+b2Lzh+DLN+CAPkoIWxZ99cjc+hUJ2WvdPmMzq4jfk+KhWzLWps45iR+jSFV5GQ
    25  gxI8ZmyLH9QwA5byr22Sy3P7XNbebUGz0AIU3ZxgQLtWRhC9bZ0vnLrmK/cwaEAz
    26  Q6Jh4YKOSafgiL8wb7QfieKhdkgwsNctxSmDe//V9pyiFfDtcECQA4bzb+XHQn6+
    27  +w8FlERFvVmLRUiR29jvbmbPCJF/VH/244KUztzap9BVIbMPgvjYya615Mou6iGE
    28  eomAue/XN01m37ky1k+C6PLzFqoOIWI2bw+pAq+GH7ijt2n2nevuT8yvWnsnY6IN
    29  AgMBAAGjPTA7MA4GA1UdDwEB/wQEAwIHgDATBgNVHSUEDDAKBggrBgEFBQcDATAU
    30  BgNVHREEDTALgglkZXYubG9jYWwwDQYJKoZIhvcNAQELBQADggEBACDdtI3LRnRR
    31  57YGXJ4Pyw10V/W84qNk2uWPEsMQESO8ufc8uS1vqLER5EIuKfbgAe/+U3aX0wyZ
    32  sCr39kqFOITq7yfBwm0Ige81n5KVy+hOlGDdkG37ol/bGB1+rf7MClokmAVFhtI2
    33  kfeWSfLI+SODvtvM7nnpyCwtvJjWNILcoEl6BKByHQ3vbDEKs2w4XGsftfyy5vGW
    34  YABlakh47OldjuGzSAPJUECsOOK9RlrZLynNqiGRfj/LaS2C0zZrDM89kuPngM9U
    35  X+maxcJQFXbK7NT+SoiVeT54vTbwSm3friwRkh2JTRnAZKhDSCHJEsgVXVh2XMjH
    36  LXfRlJqO8qk=
    37  -----END CERTIFICATE-----
    38  -----BEGIN CERTIFICATE-----
    39  MIIC0DCCAbigAwIBAgIRAMA5rXAuCDKJ4DUJi5TffXUwDQYJKoZIhvcNAQELBQAw
    40  ETEPMA0GA1UEChMGZ28tc2RrMB4XDTE5MDMyOTE3MTMxM1oXDTE5MDQyODE3MTMx
    41  M1owETEPMA0GA1UEChMGZ28tc2RrMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIB
    42  CgKCAQEAp4XHxZEPKoRbHSOLSPIMN1XoMlvEFYbfkwkZEKCpVw2pBZIB/9KD8JLF
    43  aMbsYxouPjPLq10t460qQWNVBKQACeq9hUOHN2ZtV0oH6a//fdxqDkinrWIhhVeg
    44  MSxWCIYqUlyjiSdFXTCIjuI2FNKN9S2fcvCeLqGjhU3IFypG9dYKZkQ2Oqnejbuo
    45  TLL6wz85T9MN5cr2gbAmWGAu5SFn8gEeJImgxbRMPebFejk6X1hwgzpVicgXE3Ib
    46  ZxxCJKfLfgwfEhvqd6JC+A4UgfcnqMNTic/ztkEJDdhq6OPkKaoanlZUlamBOuwt
    47  T8rqsa8ekHnJjRNDfwKPvM0o7i6XxwIDAQABoyMwITAOBgNVHQ8BAf8EBAMCAqQw
    48  DwYDVR0TAQH/BAUwAwEB/zANBgkqhkiG9w0BAQsFAAOCAQEANE8vXN1s+ohBi3+S
    49  xnnSr3CCamS6h9MSRmEb1Q1jCZ+HQLUeQgIdLCbwxPB10i4fJkV5vTo9r/OER/G5
    50  XhAKT1IZexLhcbyr9tf3s7Xd43QU1MU4Q3gRHra5w+RNgi3Dnq1zp6OHwdPDUA9R
    51  INM2BEhXOwDefHYmEHATCCeyr3GGm12Ot1EdFfy6YCU5Q6xiHB4/liTmHcbruYAP
    52  XMNCo8B1Pd/psRLF8hcqp0jeHvy54GfJwT1WvHzdpQ+yB5TbsVyxTH3jI/E4s6ZL
    53  LMb3TkFNROSPTD9CVPU1kVVCI7A2X/AKtEc1sbmv0G2FLH2UqzsLqJKj8ZSmDfGP
    54  H/db8w==
    55  -----END CERTIFICATE-----
    56  `
    57  
    58  const days = 24 * time.Hour
    59  
    60  func main() {
    61  	warningThreshold := 60 * days
    62  
    63  	parsedCerts, err := certutil.ParseCertPEM([]byte(testCertChain))
    64  	if err != nil {
    65  		log.Fatal(err)
    66  	}
    67  	if len(parsedCerts) == 0 {
    68  		log.Fatal(errors.New("invalid pem; empty certs"))
    69  	}
    70  	for _, cert := range parsedCerts {
    71  		if time.Now().UTC().After(cert.NotAfter) {
    72  			fmt.Printf("fatal; certificate %s expired %s", cert.Subject.CommonName, cert.NotAfter.Format(time.RFC3339))
    73  		} else if delta := cert.NotAfter.Sub(time.Now().UTC()); delta < warningThreshold {
    74  			fmt.Printf("warning; certificate %s will expire in %v", cert.Subject.CommonName, formatDays(delta))
    75  		} else {
    76  			fmt.Printf("ok; certificate %s is still valid", cert.Subject.CommonName)
    77  		}
    78  		fmt.Println()
    79  	}
    80  }
    81  
    82  func formatDays(d time.Duration) string {
    83  	if d > days {
    84  		return fmt.Sprintf("%d days", d/days)
    85  	}
    86  	return d.String()
    87  }