-
Notifications
You must be signed in to change notification settings - Fork 67
/
validation.go
61 lines (52 loc) · 1.75 KB
/
validation.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// SPDX-FileCopyrightText: 2024 SAP SE or an SAP affiliate company and Gardener contributors
//
// SPDX-License-Identifier: Apache-2.0
package dns
import (
"fmt"
"strings"
"k8s.io/apimachinery/pkg/util/validation"
)
func ValidateDomainName(name string) error {
check := NormalizeHostname(name)
if strings.HasPrefix(check, "_") {
// allow "_" prefix, as it is used for DNS challenges of Let's encrypt
check = "x" + check[1:]
}
var errs []string
if strings.HasPrefix(check, "*.") {
errs = validation.IsWildcardDNS1123Subdomain(check)
} else if strings.HasPrefix(check, "@.") {
// special case: allow apex label for Azure
errs = validation.IsDNS1123Subdomain(check[2:])
} else {
errs = validation.IsDNS1123Subdomain(check)
}
if len(errs) > 0 {
return fmt.Errorf("%q is no valid dns name (%v)", name, errs)
}
metaCheck := CalcMetaRecordDomainNameForValidation(check)
if strings.HasPrefix(metaCheck, "*.") {
errs = validation.IsWildcardDNS1123Subdomain(metaCheck)
} else {
errs = validation.IsDNS1123Subdomain(metaCheck)
}
if len(errs) > 0 {
return fmt.Errorf("metadata record %q of %q is no valid dns name (%v)", metaCheck, name, errs)
}
labels := strings.Split(strings.TrimPrefix(check, "*."), ".")
for i, label := range labels {
if i == 0 && label == "@" {
// special case: allow apex label for Azure
continue
}
if errs = validation.IsDNS1123Label(label); len(errs) > 0 {
return fmt.Errorf("%d. label %q of %q is not valid (%v)", i+1, label, name, errs)
}
}
metaLabels := strings.SplitN(strings.TrimPrefix(metaCheck, "*."), ".", 2)
if errs = validation.IsDNS1123Label(metaLabels[0]); len(errs) > 0 {
return fmt.Errorf("1. label %q of metadata record of %q is not valid (%v)", metaLabels[0], name, errs)
}
return nil
}