Move logic to utils

Signed-off-by: Maartje Eyskens <maartje@eyskens.me>
This commit is contained in:
Maartje Eyskens 2020-08-28 09:59:48 +02:00
parent bb89b50c8f
commit 69186afbdd
3 changed files with 17 additions and 29 deletions

View File

@ -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
}

View File

@ -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
}

View File

@ -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
}