2021-03-16 11:31:04 -07:00
|
|
|
package tls
|
|
|
|
|
|
|
|
import (
|
2022-04-26 22:18:14 +02:00
|
|
|
"context"
|
2021-03-16 11:31:04 -07:00
|
|
|
|
|
|
|
"github.com/kyverno/kyverno/pkg/config"
|
|
|
|
"github.com/pkg/errors"
|
2022-05-17 16:14:31 +02:00
|
|
|
corev1 "k8s.io/api/core/v1"
|
2022-04-26 22:18:14 +02:00
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
|
|
"k8s.io/client-go/kubernetes"
|
2021-03-16 11:31:04 -07:00
|
|
|
)
|
|
|
|
|
2021-05-04 22:10:01 -07:00
|
|
|
var ErrorsNotFound = "root CA certificate not found"
|
|
|
|
|
2021-03-16 11:31:04 -07:00
|
|
|
// ReadRootCASecret returns the RootCA from the pre-defined secret
|
2022-05-11 16:58:14 +02:00
|
|
|
func ReadRootCASecret(client kubernetes.Interface) ([]byte, error) {
|
|
|
|
sname := GenerateRootCASecretName()
|
|
|
|
stlsca, err := client.CoreV1().Secrets(config.KyvernoNamespace()).Get(context.TODO(), sname, metav1.GetOptions{})
|
2021-03-16 11:31:04 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-05-11 09:11:50 +02:00
|
|
|
// try "tls.crt"
|
2022-05-17 16:14:31 +02:00
|
|
|
result := stlsca.Data[corev1.TLSCertKey]
|
2022-05-11 09:11:50 +02:00
|
|
|
// if not there, try old "rootCA.crt"
|
|
|
|
if len(result) == 0 {
|
2022-05-12 16:07:25 +02:00
|
|
|
result = stlsca.Data[RootCAKey]
|
2022-05-11 09:11:50 +02:00
|
|
|
}
|
2021-03-16 11:31:04 -07:00
|
|
|
if len(result) == 0 {
|
2022-05-11 16:58:14 +02:00
|
|
|
return nil, errors.Errorf("%s in secret %s/%s", ErrorsNotFound, config.KyvernoNamespace(), stlsca.Name)
|
2021-03-16 11:31:04 -07:00
|
|
|
}
|
|
|
|
return result, nil
|
|
|
|
}
|