From e71829284cf8d77c8e9cf3da0344b32311bc560e Mon Sep 17 00:00:00 2001 From: David Viejo Date: Wed, 6 Nov 2024 08:24:11 +0100 Subject: [PATCH] Fix panic when parsing x509 (#245) 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