2023-04-05 15:51:07 +02:00
|
|
|
package tls
|
2022-07-11 23:19:47 +05:30
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/x509"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/kyverno/kyverno/pkg/config"
|
|
|
|
"google.golang.org/grpc/credentials"
|
2022-08-24 15:08:24 +02:00
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
2022-07-11 23:19:47 +05:30
|
|
|
"k8s.io/client-go/kubernetes"
|
|
|
|
)
|
|
|
|
|
|
|
|
func FetchCert(
|
|
|
|
ctx context.Context,
|
|
|
|
certs string,
|
2022-08-24 15:08:24 +02:00
|
|
|
kubeClient kubernetes.Interface,
|
|
|
|
) (credentials.TransportCredentials, error) {
|
|
|
|
secret, err := kubeClient.CoreV1().Secrets(config.KyvernoNamespace()).Get(ctx, certs, metav1.GetOptions{})
|
2022-07-11 23:19:47 +05:30
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error fetching certificate from secret")
|
|
|
|
}
|
|
|
|
|
|
|
|
cp := x509.NewCertPool()
|
|
|
|
if !cp.AppendCertsFromPEM(secret.Data["ca.pem"]) {
|
|
|
|
return nil, fmt.Errorf("credentials: failed to append certificates")
|
|
|
|
}
|
|
|
|
|
|
|
|
transportCreds := credentials.NewClientTLSFromCert(cp, "")
|
|
|
|
return transportCreds, nil
|
|
|
|
}
|