From fb3de08bbd3077e291050c50f4ed60fc5697fbf8 Mon Sep 17 00:00:00 2001 From: Fernandez Ludovic Date: Sun, 8 Feb 2026 21:07:02 +0100 Subject: [PATCH] refactor: rename sanitizeDomain --- cmd/internal/storage/certificates.go | 14 +++++++------- cmd/internal/storage/certificates_reader.go | 2 +- cmd/internal/storage/certificates_test.go | 9 +++++++-- cmd/internal/storage/certificates_writer.go | 2 +- 4 files changed, 16 insertions(+), 11 deletions(-) diff --git a/cmd/internal/storage/certificates.go b/cmd/internal/storage/certificates.go index f22bb0ae9..bd46cef3f 100644 --- a/cmd/internal/storage/certificates.go +++ b/cmd/internal/storage/certificates.go @@ -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 != '@' }), "", ) diff --git a/cmd/internal/storage/certificates_reader.go b/cmd/internal/storage/certificates_reader.go index e334a6972..416d76615 100644 --- a/cmd/internal/storage/certificates_reader.go +++ b/cmd/internal/storage/certificates_reader.go @@ -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) } diff --git a/cmd/internal/storage/certificates_test.go b/cmd/internal/storage/certificates_test.go index 8500342e2..c3e330dfa 100644 --- a/cmd/internal/storage/certificates_test.go +++ b/cmd/internal/storage/certificates_test.go @@ -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)) }) } } diff --git a/cmd/internal/storage/certificates_writer.go b/cmd/internal/storage/certificates_writer.go index 0471cc737..7ad1fb70e 100644 --- a/cmd/internal/storage/certificates_writer.go +++ b/cmd/internal/storage/certificates_writer.go @@ -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 {