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"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
v1 "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-05-11 14:58:14 +00: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 18:31:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-05-11 07:11:50 +00:00
|
|
|
// try "tls.crt"
|
2022-05-11 08:05:13 +00:00
|
|
|
result := stlsca.Data[v1.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
|
|
|
|
}
|