<commit_before>/*
Copyright 2020 The cert-manager Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package venafi

import (
	"context"
	"encoding/json"
	"fmt"

	"github.com/Venafi/vcert/v4/pkg/endpoint"
	certificatesv1 "k8s.io/api/certificates/v1"
	corev1 "k8s.io/api/core/v1"
	apierrors "k8s.io/apimachinery/pkg/api/errors"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	certificatesclient "k8s.io/client-go/kubernetes/typed/certificates/v1"
	corelisters "k8s.io/client-go/listers/core/v1"
	"k8s.io/client-go/tools/record"

	apiutil "github.com/jetstack/cert-manager/pkg/api/util"
	cmapi "github.com/jetstack/cert-manager/pkg/apis/certmanager/v1"
	experimentalapi "github.com/jetstack/cert-manager/pkg/apis/experimental/v1alpha1"
	controllerpkg "github.com/jetstack/cert-manager/pkg/controller"
	"github.com/jetstack/cert-manager/pkg/controller/certificatesigningrequests"
	"github.com/jetstack/cert-manager/pkg/controller/certificatesigningrequests/util"
	venaficlient "github.com/jetstack/cert-manager/pkg/issuer/venafi/client"
	venafiapi "github.com/jetstack/cert-manager/pkg/issuer/venafi/client/api"
	logf "github.com/jetstack/cert-manager/pkg/logs"
	"github.com/jetstack/cert-manager/pkg/util/pki"
	utilpki "github.com/jetstack/cert-manager/pkg/util/pki"
)

const (
	CSRControllerName = "certificatesigningrequests-issuer-venafi"
)

// Venafi is a Kubernetes CertificateSigningRequest controller, responsible for
// signing CertificateSigningRequests that reference a cert-manager Venafi
// Issuer or ClusterIssuer
type Venafi struct {
	issuerOptions controllerpkg.IssuerOptions
	secretsLister corelisters.SecretLister
	certClient    certificatesclient.CertificateSigningRequestInterface
	recorder      record.EventRecorder

	clientBuilder venaficlient.VenafiClientBuilder
}

func init() {
	controllerpkg.Register(CSRControllerName, func(ctx *controllerpkg.Context) (controllerpkg.Interface, error) {
		return controllerpkg.NewBuilder(ctx, CSRControllerName).
			For(certificatesigningrequests.New(apiutil.IssuerVenafi, NewVenafi(ctx))).
			Complete()
	})
}

func NewVenafi(ctx *controllerpkg.Context) *Venafi {
	return &Venafi{
		issuerOptions: ctx.IssuerOptions,
		secretsLister: ctx.KubeSharedInformerFactory.Core().V1().Secrets().Lister(),
		certClient:    ctx.Client.CertificatesV1().CertificateSigningRequests(),
		recorder:      ctx.Recorder,
		clientBuilder: venaficlient.New,
	}
}

// Sign attempts to sign the given CertificateSigningRequest based on the
// provided Venafi Issuer or ClusterIssuer. This function will update the resource
// if signing was successful. Returns an error which, if not nil, should
// trigger a retry.
// Since this signer takes some time to sign the request, this controller will
// set a "pick ID" annotation value that is used to fetch the latest state of
// the request in subsequent re-syncs.
func (v *Venafi) Sign(ctx context.Context, csr *certificatesv1.CertificateSigningRequest, issuerObj cmapi.GenericIssuer) error {
	log := logf.FromContext(ctx, "sign")
	log = logf.WithRelatedResource(log, issuerObj)

	resourceNamespace := v.issuerOptions.ResourceNamespace(issuerObj)

	client, err := v.clientBuilder(resourceNamespace, v.secretsLister, issuerObj)
	if apierrors.IsNotFound(err) {
		message := "Required secret resource not found"
		v.recorder.Event(csr, corev1.EventTypeWarning, "SecretNotFound", message)
		log.Error(err, message)
		return nil
	}

	if err != nil {
		message := fmt.Sprintf("Failed to initialise venafi client for signing: %s", err)
		v.recorder.Event(csr, corev1.EventTypeWarning, "ErrorVenafiInit", message)
		log.Error(err, message)
		return err
	}

	var customFields []venafiapi.CustomField
	if annotation, exists := csr.GetAnnotations()[experimentalapi.CertificateSigningRequestVenafiCustomFieldsAnnotationKey]; exists && annotation != "" {
		err := json.Unmarshal([]byte(annotation), &customFields)
		if err != nil {
			message := fmt.Sprintf("Failed to parse %q annotation: %s", experimentalapi.CertificateSigningRequestVenafiCustomFieldsAnnotationKey, err)
			v.recorder.Event(csr, corev1.EventTypeWarning, "ErrorCustomFields", message)
			util.CertificateSigningRequestSetFailed(csr, "ErrorCustomFields", message)
			_, err = v.certClient.UpdateStatus(ctx, csr, metav1.UpdateOptions{})
			return err
		}
	}

	duration, err := pki.DurationFromCertificateSigningRequest(csr)
	if err != nil {
		message := fmt.Sprintf("Failed to parse requested duration: %s", err)
		log.Error(err, message)
		v.recorder.Event(csr, corev1.EventTypeWarning, "ErrorParseDuration", message)
		util.CertificateSigningRequestSetFailed(csr, "ErrorParseDuration", message)
		_, err := v.certClient.UpdateStatus(ctx, csr, metav1.UpdateOptions{})
		return err
	}

	pickupID := csr.GetAnnotations()[experimentalapi.CertificateSigningRequestVenafiPickupIDAnnotationKey]

	// check if the pickup ID annotation is there, if not set it up.
	if len(pickupID) == 0 {
		pickupID, err := client.RequestCertificate(csr.Spec.Request, duration, customFields)
		// Check some known error types
		if err != nil {
			switch err.(type) {

			case venaficlient.ErrCustomFieldsType:
				log.Error(err, "")
				v.recorder.Event(csr, corev1.EventTypeWarning, "ErrorCustomFields", err.Error())
				util.CertificateSigningRequestSetFailed(csr, "ErrorCustomFields", err.Error())
				_, err := v.certClient.UpdateStatus(ctx, csr, metav1.UpdateOptions{})
				return err

			default:
				message := fmt.Sprintf("Failed to request venafi certificate: %s", err)
				log.Error(err, message)
				v.recorder.Event(csr, corev1.EventTypeWarning, "ErrorRequest", message)
				util.CertificateSigningRequestSetFailed(csr, "ErrorRequest", message)
				_, err := v.certClient.UpdateStatus(ctx, csr, metav1.UpdateOptions{})
				return err
			}
		}

		if csr.Annotations == nil {
			csr.Annotations = make(map[string]string)<commit_msg>Adds comment about what the pickup ID is in the CSR controller<commit_after>/*
Copyright 2020 The cert-manager Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package venafi

import (
	"context"
	"encoding/json"
	"fmt"

	"github.com/Venafi/vcert/v4/pkg/endpoint"
	certificatesv1 "k8s.io/api/certificates/v1"
	corev1 "k8s.io/api/core/v1"
	apierrors "k8s.io/apimachinery/pkg/api/errors"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	certificatesclient "k8s.io/client-go/kubernetes/typed/certificates/v1"
	corelisters "k8s.io/client-go/listers/core/v1"
	"k8s.io/client-go/tools/record"

	apiutil "github.com/jetstack/cert-manager/pkg/api/util"
	cmapi "github.com/jetstack/cert-manager/pkg/apis/certmanager/v1"
	experimentalapi "github.com/jetstack/cert-manager/pkg/apis/experimental/v1alpha1"
	controllerpkg "github.com/jetstack/cert-manager/pkg/controller"
	"github.com/jetstack/cert-manager/pkg/controller/certificatesigningrequests"
	"github.com/jetstack/cert-manager/pkg/controller/certificatesigningrequests/util"
	venaficlient "github.com/jetstack/cert-manager/pkg/issuer/venafi/client"
	venafiapi "github.com/jetstack/cert-manager/pkg/issuer/venafi/client/api"
	logf "github.com/jetstack/cert-manager/pkg/logs"
	"github.com/jetstack/cert-manager/pkg/util/pki"
	utilpki "github.com/jetstack/cert-manager/pkg/util/pki"
)

const (
	CSRControllerName = "certificatesigningrequests-issuer-venafi"
)

// Venafi is a Kubernetes CertificateSigningRequest controller, responsible for
// signing CertificateSigningRequests that reference a cert-manager Venafi
// Issuer or ClusterIssuer
type Venafi struct {
	issuerOptions controllerpkg.IssuerOptions
	secretsLister corelisters.SecretLister
	certClient    certificatesclient.CertificateSigningRequestInterface
	recorder      record.EventRecorder

	clientBuilder venaficlient.VenafiClientBuilder
}

func init() {
	controllerpkg.Register(CSRControllerName, func(ctx *controllerpkg.Context) (controllerpkg.Interface, error) {
		return controllerpkg.NewBuilder(ctx, CSRControllerName).
			For(certificatesigningrequests.New(apiutil.IssuerVenafi, NewVenafi(ctx))).
			Complete()
	})
}

func NewVenafi(ctx *controllerpkg.Context) *Venafi {
	return &Venafi{
		issuerOptions: ctx.IssuerOptions,
		secretsLister: ctx.KubeSharedInformerFactory.Core().V1().Secrets().Lister(),
		certClient:    ctx.Client.CertificatesV1().CertificateSigningRequests(),
		recorder:      ctx.Recorder,
		clientBuilder: venaficlient.New,
	}
}

// Sign attempts to sign the given CertificateSigningRequest based on the
// provided Venafi Issuer or ClusterIssuer. This function will update the resource
// if signing was successful. Returns an error which, if not nil, should
// trigger a retry.
// Since this signer takes some time to sign the request, this controller will
// set a "pick ID" annotation value that is used to fetch the latest state of
// the request in subsequent re-syncs.
func (v *Venafi) Sign(ctx context.Context, csr *certificatesv1.CertificateSigningRequest, issuerObj cmapi.GenericIssuer) error {
	log := logf.FromContext(ctx, "sign")
	log = logf.WithRelatedResource(log, issuerObj)

	resourceNamespace := v.issuerOptions.ResourceNamespace(issuerObj)

	client, err := v.clientBuilder(resourceNamespace, v.secretsLister, issuerObj)
	if apierrors.IsNotFound(err) {
		message := "Required secret resource not found"
		v.recorder.Event(csr, corev1.EventTypeWarning, "SecretNotFound", message)
		log.Error(err, message)
		return nil
	}

	if err != nil {
		message := fmt.Sprintf("Failed to initialise venafi client for signing: %s", err)
		v.recorder.Event(csr, corev1.EventTypeWarning, "ErrorVenafiInit", message)
		log.Error(err, message)
		return err
	}

	var customFields []venafiapi.CustomField
	if annotation, exists := csr.GetAnnotations()[experimentalapi.CertificateSigningRequestVenafiCustomFieldsAnnotationKey]; exists && annotation != "" {
		err := json.Unmarshal([]byte(annotation), &customFields)
		if err != nil {
			message := fmt.Sprintf("Failed to parse %q annotation: %s", experimentalapi.CertificateSigningRequestVenafiCustomFieldsAnnotationKey, err)
			v.recorder.Event(csr, corev1.EventTypeWarning, "ErrorCustomFields", message)
			util.CertificateSigningRequestSetFailed(csr, "ErrorCustomFields", message)
			_, err = v.certClient.UpdateStatus(ctx, csr, metav1.UpdateOptions{})
			return err
		}
	}

	duration, err := pki.DurationFromCertificateSigningRequest(csr)
	if err != nil {
		message := fmt.Sprintf("Failed to parse requested duration: %s", err)
		log.Error(err, message)
		v.recorder.Event(csr, corev1.EventTypeWarning, "ErrorParseDuration", message)
		util.CertificateSigningRequestSetFailed(csr, "ErrorParseDuration", message)
		_, err := v.certClient.UpdateStatus(ctx, csr, metav1.UpdateOptions{})
		return err
	}

	// The signing process with Venafi is slow. The "pickupID" allows us to track
	// the progress of the certificate signing. It is set as an annotation the
	// first time the Certificate is reconciled.
	pickupID := csr.GetAnnotations()[experimentalapi.CertificateSigningRequestVenafiPickupIDAnnotationKey]

	// check if the pickup ID annotation is there, if not set it up.
	if len(pickupID) == 0 {
		pickupID, err := client.RequestCertificate(csr.Spec.Request, duration, customFields)
		// Check some known error types
		if err != nil {
			switch err.(type) {

			case venaficlient.ErrCustomFieldsType:
				log.Error(err, "")
				v.recorder.Event(csr, corev1.EventTypeWarning, "ErrorCustomFields", err.Error())
				util.CertificateSigningRequestSetFailed(csr, "ErrorCustomFields", err.Error())
				_, err := v.certClient.UpdateStatus(ctx, csr, metav1.UpdateOptions{})
				return err

			default:
				message := fmt.Sprintf("Failed to request venafi certificate: %s", err)
				log.Error(err, message)
				v.recorder.Event(csr, corev1.EventTypeWarning, "ErrorRequest", message)
				util.CertificateSigningRequestSetFailed(csr, "ErrorRequest", message)
				_, err := v.certClient.UpdateStatus(ctx, csr, metav1.UpdateOptions{})
				return err
			}
		}

		if csr.Annotations == nil {
			csr.Annotations = make(map[string]string)
{'fraction_non_alphanumeric': 0.1066017316017316, 'fraction_numerical': 0.0063388991960420535, 'mean_word_length': 5.093735280263778, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4151241', 'n_tokens_mistral': 4321, 'n_tokens_neox': 4079, 'n_words': 960}