From 26018099bb4efc9f1150e30ca6208ec6dd4e802e Mon Sep 17 00:00:00 2001 From: David VIEJO Date: Mon, 4 Nov 2024 17:31:22 +0100 Subject: [PATCH] Fix panic when parsing x509 Signed-off-by: David VIEJO --- controllers/utils/utils.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/controllers/utils/utils.go b/controllers/utils/utils.go index 780d972e..cd647ff4 100644 --- a/controllers/utils/utils.go +++ b/controllers/utils/utils.go @@ -6,12 +6,13 @@ import ( "crypto/x509" "encoding/pem" "errors" - log "github.com/sirupsen/logrus" "net" "strconv" "strings" "time" + log "github.com/sirupsen/logrus" + corev1 "k8s.io/api/core/v1" v12 "k8s.io/api/core/v1" apiextv1beta1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1" @@ -70,7 +71,13 @@ func ParseECDSAPrivateKey(contents []byte) (*ecdsa.PrivateKey, error) { return ecdsaKey, nil } func ParseX509Certificate(contents []byte) (*x509.Certificate, error) { + if len(contents) == 0 { + return nil, errors.New("certificate pem is empty") + } block, _ := pem.Decode(contents) + if block == nil { + return nil, errors.New("failed to decode PEM block") + } crt, err := x509.ParseCertificate(block.Bytes) if err != nil { return nil, err