Skip to content

Commit

Permalink
only get first certificate in chain
Browse files Browse the repository at this point in the history
  • Loading branch information
dem4gus committed Jul 5, 2023
1 parent 059a390 commit 8bcabc4
Showing 1 changed file with 16 additions and 18 deletions.
34 changes: 16 additions & 18 deletions pkg/monitor/cluster/certificateexpirationstatuses.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"crypto/x509"
"encoding/pem"
"fmt"
"time"

corev1 "k8s.io/api/core/v1"
Expand All @@ -19,30 +20,23 @@ import (

func (mon *Monitor) emitCertificateExpirationStatuses(ctx context.Context) error {
// report NotAfter dates for Geneva (always), Ingress, and API (on managed domain) certificates
var certs []x509.Certificate
var certs []*x509.Certificate

mdsdCert, err := mon.getCertificate(ctx, operator.SecretName, operator.Namespace, genevalogging.GenevaCertName)
if err != nil {
return err
}
certs = append(certs, *mdsdCert[0])
certs = append(certs, mdsdCert)

if dns.IsManagedDomain(mon.oc.Properties.ClusterProfile.Domain) {
infraID := mon.oc.Properties.InfraID
ingressCertificateName := infraID + "-ingress"
apiCertificateName := infraID + "-apiserver"

ingressCertificate, err := mon.getCertificate(ctx, ingressCertificateName, operator.Namespace, corev1.TLSCertKey)
if err != nil {
return err
}

apiCertificate, err := mon.getCertificate(ctx, apiCertificateName, operator.Namespace, corev1.TLSCertKey)
if err != nil {
return err
for _, secretName := range []string{infraID + "-ingress", infraID + "-apiserver"} {
certificate, err := mon.getCertificate(ctx, secretName, operator.Namespace, corev1.TLSCertKey)
if err != nil {
return err
}
certs = append(certs, certificate)
}

certs = append(certs, *ingressCertificate[0], *apiCertificate[0])
}

for _, cert := range certs {
Expand All @@ -55,12 +49,16 @@ func (mon *Monitor) emitCertificateExpirationStatuses(ctx context.Context) error
return nil
}

func (mon *Monitor) getCertificate(ctx context.Context, secretName, secretNamespace, secretKey string) ([]*x509.Certificate, error) {
func (mon *Monitor) getCertificate(ctx context.Context, secretName, secretNamespace, secretKey string) (*x509.Certificate, error) {
secret, err := mon.cli.CoreV1().Secrets(secretNamespace).Get(ctx, secretName, metav1.GetOptions{})
if err != nil {
return nil, err
return &x509.Certificate{}, err
}

certBlock, _ := pem.Decode(secret.Data[secretKey])
return x509.ParseCertificates(certBlock.Bytes)
if certBlock == nil {
return &x509.Certificate{}, fmt.Errorf("certificate data for %s not found", secretName)
}
// we only care about the first certificate in the block
return x509.ParseCertificate(certBlock.Bytes)
}

0 comments on commit 8bcabc4

Please sign in to comment.