Skip to content

Commit

Permalink
解决cert.leaf :*crypto/x509.certificate nil时产生的空指针报错
Browse files Browse the repository at this point in the history
  • Loading branch information
aaakoako committed Mar 10, 2023
1 parent ca9ae75 commit 335ebfc
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion code/initialization/gin.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,38 @@ package initialization

import (
"crypto/tls"
"crypto/x509"
"fmt"
"github.com/gin-gonic/gin"
"log"
"net/http"
"time"

"github.com/gin-gonic/gin"
)

func loadCertificate(config Config) (cert tls.Certificate, err error) {
cert, err = tls.LoadX509KeyPair(config.CertFile, config.KeyFile)
if err != nil {
return cert, fmt.Errorf("failed to load certificate: %v", err)
}

// check certificate expiry
if len(cert.Certificate) == 0 {
return cert, fmt.Errorf("no certificates found in %s", config.CertFile)
}
parsedCert, err := x509.ParseCertificate(cert.Certificate[0])
if err != nil {
return cert, fmt.Errorf("failed to parse certificate: %v", err)
}
cert.Leaf = parsedCert
certExpiry := cert.Leaf.NotAfter
if certExpiry.Before(time.Now()) {
return cert, fmt.Errorf("certificate expired on %v", certExpiry)
}

return cert, nil
}

func startHTTPServer(config Config, r *gin.Engine) (err error) {
log.Printf("http server started: http://localhost:%d/webhook/event\n", config.HttpPort)
err = r.Run(fmt.Sprintf(":%d", config.HttpPort))
Expand Down

0 comments on commit 335ebfc

Please sign in to comment.