浏览代码

update deprecated x509 methods

marco 1 年之前
父节点
当前提交
efe14fb3bc
共有 2 个文件被更改,包括 17 次插入8 次删除
  1. 0 4
      .golangci.yml
  2. 17 4
      pkg/apiserver/middlewares/v1/tls_auth.go

+ 0 - 4
.golangci.yml

@@ -310,10 +310,6 @@ issues:
     # Will fix,  might be trickier
     # Will fix,  might be trickier
     #
     #
 
 
-    - linters:
-        - staticcheck
-      text: "x509.ParseCRL has been deprecated since Go 1.19: Use ParseRevocationList instead"
-
     # https://github.com/pkg/errors/issues/245
     # https://github.com/pkg/errors/issues/245
     - linters:
     - linters:
         - depguard
         - depguard

+ 17 - 4
pkg/apiserver/middlewares/v1/tls_auth.go

@@ -4,6 +4,7 @@ import (
 	"bytes"
 	"bytes"
 	"crypto"
 	"crypto"
 	"crypto/x509"
 	"crypto/x509"
+	"encoding/pem"
 	"fmt"
 	"fmt"
 	"io"
 	"io"
 	"net/http"
 	"net/http"
@@ -130,17 +131,29 @@ func (ta *TLSAuth) isCRLRevoked(cert *x509.Certificate) (bool, error) {
 		return false, nil
 		return false, nil
 	}
 	}
 
 
-	crl, err := x509.ParseCRL(crlContent)
+	crlBinary, rest := pem.Decode(crlContent)
+	if len(rest) > 0 {
+		ta.logger.Warn("CRL file contains more than one PEM block, skipping check")
+		return false, nil
+	}
+
+	crl, err := x509.ParseRevocationList(crlBinary.Bytes)
 	if err != nil {
 	if err != nil {
-		ta.logger.Warnf("could not parse CRL file, skipping check: %s", err)
+		ta.logger.Errorf("could not parse CRL file, skipping check: %s", err)
 		return false, nil
 		return false, nil
 	}
 	}
 
 
-	if crl.HasExpired(time.Now().UTC()) {
+	now := time.Now().UTC()
+
+	if now.After(crl.NextUpdate) {
 		ta.logger.Warn("CRL has expired, will still validate the cert against it.")
 		ta.logger.Warn("CRL has expired, will still validate the cert against it.")
 	}
 	}
 
 
-	for _, revoked := range crl.TBSCertList.RevokedCertificates {
+	if now.Before(crl.ThisUpdate) {
+		ta.logger.Warn("CRL is not yet valid, will still validate the cert against it.")
+	}
+
+	for _, revoked := range crl.RevokedCertificateEntries {
 		if revoked.SerialNumber.Cmp(cert.SerialNumber) == 0 {
 		if revoked.SerialNumber.Cmp(cert.SerialNumber) == 0 {
 			return true, fmt.Errorf("client certificate is revoked by CRL")
 			return true, fmt.Errorf("client certificate is revoked by CRL")
 		}
 		}