-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
validate.go
55 lines (48 loc) · 1.67 KB
/
validate.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
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016-present Datadog, Inc.
// Package validate provides hostname validation helpers
package validate
import (
"fmt"
"regexp"
"strings"
"github.com/DataDog/datadog-agent/pkg/util/log"
)
const maxLength = 255
var (
validHostnameRfc1123 = regexp.MustCompile(`^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])$`)
localhostIdentifiers = []string{
"localhost",
"localhost.localdomain",
"localhost6.localdomain6",
"ip6-localhost",
}
)
// ValidHostname determines whether the passed string is a valid hostname.
// In case it's not, the returned error contains the details of the failure.
func ValidHostname(hostname string) error {
if hostname == "" {
return fmt.Errorf("hostname is empty")
} else if isLocal(hostname) {
return fmt.Errorf("%s is a local hostname", hostname)
} else if len(hostname) > maxLength {
log.Errorf("ValidHostname: name exceeded the maximum length of %d characters", maxLength)
return fmt.Errorf("name exceeded the maximum length of %d characters", maxLength)
} else if !validHostnameRfc1123.MatchString(hostname) {
log.Errorf("ValidHostname: %s is not RFC1123 compliant", hostname)
return fmt.Errorf("%s is not RFC1123 compliant", hostname)
}
return nil
}
// check whether the name is in the list of local hostnames
func isLocal(name string) bool {
name = strings.ToLower(name)
for _, val := range localhostIdentifiers {
if val == name {
return true
}
}
return false
}