diff --git a/pkg/api/util/names.go b/pkg/api/util/names.go index 2f3e2a0e7..228c24c8d 100644 --- a/pkg/api/util/names.go +++ b/pkg/api/util/names.go @@ -38,14 +38,19 @@ func ComputeCertificateRequestName(crt *cmapi.Certificate) (string, error) { return "", err } - crtName := crt.Name - if len(crtName) >= 52 { - // shorten the cert name to 52 chars to ensure the total length of the name - // also shorten the 52 char string to the last non-symbol character - // is less than or equal to 64 characters - validCharIndexes := regexp.MustCompile(`[a-zA-Z\d]`).FindAllStringIndex(fmt.Sprintf("%.52s", crtName), -1) - crtName = crtName[:validCharIndexes[len(validCharIndexes)-1][1]] - } + crtName := DNSSafeSchortenTo52Characters(crt.Name) return fmt.Sprintf("%s-%d", crtName, hashF.Sum32()), nil } + +func DNSSafeSchortenTo52Characters(in string) string { + if len(in) >= 52 { + // shorten the cert name to 52 chars to ensure the total length of the name + // also shorten the 52 char string to the last non-symbol character + // is less than or equal to 64 characters + validCharIndexes := regexp.MustCompile(`[a-zA-Z\d]`).FindAllStringIndex(fmt.Sprintf("%.52s", in), -1) + in = in[:validCharIndexes[len(validCharIndexes)-1][1]] + } + + return in +} diff --git a/pkg/controller/acmeorders/util.go b/pkg/controller/acmeorders/util.go index 2ec6443f0..d9c4bc3b4 100644 --- a/pkg/controller/acmeorders/util.go +++ b/pkg/controller/acmeorders/util.go @@ -21,13 +21,12 @@ import ( "encoding/json" "fmt" "hash/fnv" - "regexp" - - "github.com/jetstack/cert-manager/pkg/acme" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "github.com/jetstack/cert-manager/pkg/acme" acmecl "github.com/jetstack/cert-manager/pkg/acme/client" + "github.com/jetstack/cert-manager/pkg/api/util" cmacme "github.com/jetstack/cert-manager/pkg/apis/acme/v1" cmapi "github.com/jetstack/cert-manager/pkg/apis/certmanager/v1" "github.com/jetstack/cert-manager/pkg/controller/acmeorders/selectors" @@ -88,14 +87,7 @@ func buildChallengeName(orderName string, chSpec cmacme.ChallengeSpec) (string, return "", err } - if len(orderName) >= 52 { - // shorten the cert name to 52 chars to ensure the total length of the name - // also shorten the 52 char string to the last non-symbol character - // is less than or equal to 64 characters - validCharIndexes := regexp.MustCompile(`[a-zA-Z\d]`).FindAllStringIndex(fmt.Sprintf("%.52s", orderName), -1) - orderName = orderName[:validCharIndexes[len(validCharIndexes)-1][1]] - } - + orderName = util.DNSSafeSchortenTo52Characters(orderName) return fmt.Sprintf("%s-%d", orderName, hash), nil } diff --git a/pkg/controller/certificaterequests/acme/acme.go b/pkg/controller/certificaterequests/acme/acme.go index 67c6f0e57..1ee6e6d95 100644 --- a/pkg/controller/certificaterequests/acme/acme.go +++ b/pkg/controller/certificaterequests/acme/acme.go @@ -22,7 +22,6 @@ import ( "encoding/json" "fmt" "hash/fnv" - "regexp" k8sErrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -246,14 +245,6 @@ func computeACMEOrderName(cr *v1.CertificateRequest, orderSpec cmacme.OrderSpec) return "", err } - crName := cr.Name - if len(crName) >= 52 { - // shorten the cert name to 52 chars to ensure the total length of the name - // also shorten the 52 char string to the last non-symbol character - // is less than or equal to 64 characters - validCharIndexes := regexp.MustCompile(`[a-zA-Z\d]`).FindAllStringIndex(fmt.Sprintf("%.52s", crName), -1) - crName = crName[:validCharIndexes[len(validCharIndexes)-1][1]] - } - + crName := apiutil.DNSSafeSchortenTo52Characters(cr.Name) return fmt.Sprintf("%s-%d", crName, hashF.Sum32()), nil }