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