<commit_before>package acme

import (
	"context"
	"errors"
	"fmt"

	"github.com/golang/glog"
	"github.com/jetstack/cert-manager/pkg/apis/certmanager/v1alpha1"
	"github.com/jetstack/cert-manager/pkg/issuer/acme/client"
	"github.com/jetstack/cert-manager/third_party/crypto/acme"
)

const (
	successObtainedAuthorization = "ObtainAuthorization"
	reasonPresentChallenge       = "PresentChallenge"
	reasonSelfCheck              = "SelfCheck"
	errorGetACMEAccount          = "ErrGetACMEAccount"
	errorCheckAuthorization      = "ErrCheckAuthorization"
	errorObtainAuthorization     = "ErrObtainAuthorization"
	errorInvalidConfig           = "ErrInvalidConfig"

	messageObtainedAuthorization    = "Obtained authorization for domain %s"
	messagePresentChallenge         = "Presenting %s challenge for domain %s"
	messageSelfCheck                = "Performing self-check for domain %s"
	messageErrorGetACMEAccount      = "Error getting ACME account: "
	messageErrorCheckAuthorization  = "Error checking ACME domain validation: "
	messageErrorObtainAuthorization = "Error obtaining ACME domain authorization: "
	messageErrorMissingConfig       = "certificate.spec.acme must be specified"
)

// Prepare will ensure the issuer has been initialised and is ready to issue
// certificates for the domains listed on the Certificate resource.
//
// It will send the appropriate Letsencrypt authorizations, and complete
// challenge requests if neccessary.
func (a *Acme) Prepare(ctx context.Context, crt *v1alpha1.Certificate) error {
	if crt.Spec.ACME == nil {
		crt.UpdateStatusCondition(v1alpha1.CertificateConditionReady, v1alpha1.ConditionFalse, errorInvalidConfig, messageErrorMissingConfig)
		return fmt.Errorf(messageErrorMissingConfig)
	}

	glog.V(4).Infof("Getting ACME client")
	// obtain an ACME client
	cl, err := a.acmeClient()
	if err != nil {
		s := messageErrorGetACMEAccount + err.Error()
		crt.UpdateStatusCondition(v1alpha1.CertificateConditionReady, v1alpha1.ConditionFalse, errorGetACMEAccount, s)
		return errors.New(s)
	}

	orderURL := crt.Status.ACMEStatus().Order.URL
	glog.Infof("Checking existing order URL %q", orderURL)
	var order *acme.Order
	// if the existing order URL is blank, create a new order
	if orderURL == "" {
		glog.Infof("Existing order URL not set")
		if order, err = a.createOrder(ctx, cl, crt); err != nil {
			return err
		}
	} else {
		glog.Infof("Requesting order details for %q from ACME server")
		// if we fail to get an existing order by URL, we should create a new
		// order to replace it.
		if order, err = cl.GetOrder(ctx, orderURL); err != nil {
			glog.Infof("Error requesting existing order details for %q from ACME server: %v", err)
			// TODO: review this - should we instead back-off and try again?
			// perhaps instead attempt to parse the URL first, and create a new
			// order if the URL is actually invalid. Not sure ??
			if order, err = a.createOrder(ctx, cl, crt); err != nil {
				return err
			}
		}
	}

	glog.Infof("Order %q status is %q", order.URL, order.Status)
	switch order.Status {
	// create a new order if the old one is invalid
	case acme.StatusDeactivated, acme.StatusInvalid, acme.StatusRevoked:
		if order, err = a.createOrder(ctx, cl, crt); err != nil {
			return err
		}
	case acme.StatusValid:
		glog.Infof("Order %q already valid", order.URL)
		return nil
	case acme.StatusPending, acme.StatusProcessing:
		// if the order is pending or processing, we will proceed.
		// TODO: should we return nil on processing? need to check acme spec
	default:
		return fmt.Errorf("order %q unknown status: %q", order.URL, order.Status)
	}

	allAuthorizations, err := getAuthorizations(ctx, cl, order.Authorizations...)
	if err != nil {
		s := messageErrorCheckAuthorization + err.Error()
		crt.UpdateStatusCondition(v1alpha1.CertificateConditionReady, v1alpha1.ConditionFalse, errorCheckAuthorization, s)
		return errors.New(s)
	}

	failed, pending, valid := partitionAuthorizations(allAuthorizations...)
	glog.Infof("Authorizations for order %q: %d failed, %d pending, %d valid", len(failed), len(pending), len(valid))
	toCleanup := append(failed, valid...)
	for _, auth := range toCleanup {
		err := a.cleanupAuthorization(ctx, cl, crt, auth)
		if err != nil {
			// TODO: don't error if cleaning up fails, as it might be due to the resources already being cleaned up
			return err
		}
	}

	if len(failed) > 0 {
		glog.Infof("Found %d failed authorizations. Cleaning up pending authorizations and clearing order URL")<commit_msg>Fix log message print formatting<commit_after>package acme

import (
	"context"
	"errors"
	"fmt"

	"github.com/golang/glog"
	"github.com/jetstack/cert-manager/pkg/apis/certmanager/v1alpha1"
	"github.com/jetstack/cert-manager/pkg/issuer/acme/client"
	"github.com/jetstack/cert-manager/third_party/crypto/acme"
)

const (
	successObtainedAuthorization = "ObtainAuthorization"
	reasonPresentChallenge       = "PresentChallenge"
	reasonSelfCheck              = "SelfCheck"
	errorGetACMEAccount          = "ErrGetACMEAccount"
	errorCheckAuthorization      = "ErrCheckAuthorization"
	errorObtainAuthorization     = "ErrObtainAuthorization"
	errorInvalidConfig           = "ErrInvalidConfig"

	messageObtainedAuthorization    = "Obtained authorization for domain %s"
	messagePresentChallenge         = "Presenting %s challenge for domain %s"
	messageSelfCheck                = "Performing self-check for domain %s"
	messageErrorGetACMEAccount      = "Error getting ACME account: "
	messageErrorCheckAuthorization  = "Error checking ACME domain validation: "
	messageErrorObtainAuthorization = "Error obtaining ACME domain authorization: "
	messageErrorMissingConfig       = "certificate.spec.acme must be specified"
)

// Prepare will ensure the issuer has been initialised and is ready to issue
// certificates for the domains listed on the Certificate resource.
//
// It will send the appropriate Letsencrypt authorizations, and complete
// challenge requests if neccessary.
func (a *Acme) Prepare(ctx context.Context, crt *v1alpha1.Certificate) error {
	if crt.Spec.ACME == nil {
		crt.UpdateStatusCondition(v1alpha1.CertificateConditionReady, v1alpha1.ConditionFalse, errorInvalidConfig, messageErrorMissingConfig)
		return fmt.Errorf(messageErrorMissingConfig)
	}

	glog.V(4).Infof("Getting ACME client")
	// obtain an ACME client
	cl, err := a.acmeClient()
	if err != nil {
		s := messageErrorGetACMEAccount + err.Error()
		crt.UpdateStatusCondition(v1alpha1.CertificateConditionReady, v1alpha1.ConditionFalse, errorGetACMEAccount, s)
		return errors.New(s)
	}

	orderURL := crt.Status.ACMEStatus().Order.URL
	glog.Infof("Checking existing order URL %q", orderURL)
	var order *acme.Order
	// if the existing order URL is blank, create a new order
	if orderURL == "" {
		glog.Infof("Existing order URL not set")
		if order, err = a.createOrder(ctx, cl, crt); err != nil {
			return err
		}
	} else {
		glog.Infof("Requesting order details for %q from ACME server", crt.Name)
		// if we fail to get an existing order by URL, we should create a new
		// order to replace it.
		if order, err = cl.GetOrder(ctx, orderURL); err != nil {
			glog.Infof("Error requesting existing order details for %q from ACME server: %v", crt.Name, err)
			// TODO: review this - should we instead back-off and try again?
			// perhaps instead attempt to parse the URL first, and create a new
			// order if the URL is actually invalid. Not sure ??
			if order, err = a.createOrder(ctx, cl, crt); err != nil {
				return err
			}
		}
	}

	glog.Infof("Order %q status is %q", order.URL, order.Status)
	switch order.Status {
	// create a new order if the old one is invalid
	case acme.StatusDeactivated, acme.StatusInvalid, acme.StatusRevoked:
		if order, err = a.createOrder(ctx, cl, crt); err != nil {
			return err
		}
	case acme.StatusValid:
		glog.Infof("Order %q already valid", order.URL)
		return nil
	case acme.StatusPending, acme.StatusProcessing:
		// if the order is pending or processing, we will proceed.
		// TODO: should we return nil on processing? need to check acme spec
	default:
		return fmt.Errorf("order %q unknown status: %q", order.URL, order.Status)
	}

	allAuthorizations, err := getAuthorizations(ctx, cl, order.Authorizations...)
	if err != nil {
		s := messageErrorCheckAuthorization + err.Error()
		crt.UpdateStatusCondition(v1alpha1.CertificateConditionReady, v1alpha1.ConditionFalse, errorCheckAuthorization, s)
		return errors.New(s)
	}

	failed, pending, valid := partitionAuthorizations(allAuthorizations...)
	glog.Infof("Authorizations for Certificate %q: %d failed, %d pending, %d valid", crt.Name, len(failed), len(pending), len(valid))
	toCleanup := append(failed, valid...)
	for _, auth := range toCleanup {
		err := a.cleanupAuthorization(ctx, cl, crt, auth)
		if err != nil {
			// TODO: don't error if cleaning up fails, as it might be due to the resources already being cleaned up
			return err
		}
	}

	if len(failed) > 0 {
		glog.Infof("Found %d failed authorizations. Cleaning up pending authorizations and clearing order URL")
{'fraction_non_alphanumeric': 0.10040915625345571, 'fraction_numerical': 0.00398097976335287, 'mean_word_length': 4.072349971957375, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '1522551', 'n_tokens_mistral': 2884, 'n_tokens_neox': 2769, 'n_words': 862}