refactor: rename sanitizeDomain

This commit is contained in:
Fernandez Ludovic 2026-02-08 21:07:02 +01:00
commit fb3de08bbd
4 changed files with 16 additions and 11 deletions

View file

@ -4,6 +4,7 @@ import (
"crypto"
"crypto/x509"
"fmt"
"log/slog"
"os"
"path/filepath"
"strings"
@ -72,20 +73,19 @@ func getCertificatesArchivePath(basePath string) string {
return filepath.Join(basePath, baseArchivesFolderName)
}
// sanitizedDomain Make sure no funny chars are in the cert names (like wildcards ;)).
// FIXME rename
func sanitizedDomain(domain string) string {
safe, err := idna.ToASCII(strings.NewReplacer(":", "-", "*", "_").Replace(domain))
// SanitizedName Make sure no funny chars are in the cert names (like wildcards ;)).
func SanitizedName(name string) string {
safe, err := idna.ToASCII(strings.NewReplacer(":", "-", "*", "_").Replace(name))
if err != nil {
log.Fatal("Could not sanitize the local certificate ID.",
log.DomainAttr(domain),
log.Fatal("Could not sanitize the name.",
slog.String("name", name),
log.ErrorAttr(err),
)
}
return strings.Join(
strings.FieldsFunc(safe, func(r rune) bool {
return !unicode.IsLetter(r) && !unicode.IsNumber(r) && r != '-' && r != '_' && r != '.'
return !unicode.IsLetter(r) && !unicode.IsNumber(r) && r != '-' && r != '_' && r != '.' && r != '@'
}),
"",
)

View file

@ -62,5 +62,5 @@ func (s *CertificatesStorage) GetRootPath() string {
}
func (s *CertificatesStorage) GetFileName(domain, extension string) string {
return filepath.Join(s.rootPath, sanitizedDomain(domain)+extension)
return filepath.Join(s.rootPath, SanitizedName(domain)+extension)
}

View file

@ -6,7 +6,7 @@ import (
"github.com/stretchr/testify/assert"
)
func Test_sanitizedDomain(t *testing.T) {
func Test_SanitizedName(t *testing.T) {
// IDN examples from https://www.iana.org/domains/reserved
testCases := []struct {
desc string
@ -73,13 +73,18 @@ func Test_sanitizedDomain(t *testing.T) {
value: "127.0.0.1",
expected: "127.0.0.1",
},
{
desc: "email",
value: "fée@example.com",
expected: "xn--fe@example-b7a.com",
},
}
for _, test := range testCases {
t.Run(test.desc, func(t *testing.T) {
t.Parallel()
assert.Equal(t, test.expected, sanitizedDomain(test.value))
assert.Equal(t, test.expected, SanitizedName(test.value))
})
}
}

View file

@ -98,7 +98,7 @@ func (s *CertificatesStorage) Archive(domain string) error {
return fmt.Errorf("could not check/create the archive folder %q: %w", s.archivePath, err)
}
baseFilename := filepath.Join(s.rootPath, sanitizedDomain(domain))
baseFilename := filepath.Join(s.rootPath, SanitizedName(domain))
matches, err := filepath.Glob(baseFilename + ".*")
if err != nil {