-
Notifications
You must be signed in to change notification settings - Fork 27
/
endpoint_validation.go
166 lines (133 loc) · 5.29 KB
/
endpoint_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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package service
import (
"fmt"
"regexp"
"strings"
"unicode/utf8"
"github.com/RedHatInsights/sources-api-go/dao"
"github.com/RedHatInsights/sources-api-go/model"
"github.com/RedHatInsights/sources-api-go/util"
)
// hostRegexp matches a valid host. The reference RFCs consulted for this are the following ones:
// - https://datatracker.ietf.org/doc/html/rfc1034#section-3.5
// - https://datatracker.ietf.org/doc/html/rfc2181#section-11
// Even though the RFC1034 states that labels "must start with a letter, end with a letter or digit, and have as
// interior characters only letters, digits, and hyphen", there are domains which have labels that start or end with
// digits. For example: https://github.com/opendns/public-domain-lists/blob/master/opendns-random-domains.txt . Also,
// length validation on labels and FQDN is left to do it with "splits", to avoid complicating the regex even more.
var fqdnRegexp = regexp.MustCompile(`^(?:[a-zA-Z\d](?:[a-zA-Z\d-]*[a-zA-Z\d])?\.)+[a-zA-Z](?:[a-zA-Z\d-]*[a-zA-Z])?$`)
// schemeRegexp matches a valid scheme, as per RFC 3986
var schemeRegexp = regexp.MustCompile(`^[a-zA-Z][a-zA-Z\d+\-.]*$`)
const (
defaultScheme = "https"
defaultPort = 443
defaultVerifySsl = false
maxFqdnLength = 255 // RFC2181
maxLabelLength = 63 // RFC2181
maxPort = 65535
)
func ValidateEndpointCreateRequest(dao dao.EndpointDao, ecr *model.EndpointCreateRequest) error {
sourceId, err := util.InterfaceToInt64(ecr.SourceIDRaw)
if err != nil {
return fmt.Errorf("the provided source ID is not valid")
}
if sourceId < 1 {
return fmt.Errorf("invalid source id")
}
ecr.SourceID = sourceId
if ecr.Default && !dao.CanEndpointBeSetAsDefaultForSource(sourceId) {
return fmt.Errorf("a default endpoint already exists for the provided source")
}
if !dao.SourceHasEndpoints(sourceId) {
ecr.Default = true
}
if !dao.IsRoleUniqueForSource(ecr.Role, sourceId) {
return fmt.Errorf("the role already exists for the given source")
}
if ecr.Scheme == nil || !schemeRegexp.MatchString(*ecr.Scheme) {
ecr.Scheme = util.StringRef(defaultScheme)
}
if ecr.Host != "" {
if utf8.RuneCountInString(ecr.Host) > maxFqdnLength {
return fmt.Errorf("the provided host is longer than %d characters", maxFqdnLength)
}
if !fqdnRegexp.MatchString(ecr.Host) {
return fmt.Errorf("the provided host is invalid")
}
labels := strings.Split(ecr.Host, ".")
for _, label := range labels {
if utf8.RuneCountInString(ecr.Host) > maxLabelLength {
return fmt.Errorf("the label '%s' is greater than %d characters", label, maxLabelLength)
}
}
}
if ecr.Port == nil || *ecr.Port <= 0 {
tmp := defaultPort
ecr.Port = &tmp
}
if *ecr.Port > maxPort {
return fmt.Errorf("invalid port number")
}
if ecr.VerifySsl == nil {
tmp := defaultVerifySsl
ecr.VerifySsl = &tmp
}
if *ecr.VerifySsl && (ecr.CertificateAuthority == nil || *ecr.CertificateAuthority == "") {
return fmt.Errorf("the certificate authority cannot be empty")
}
// The team decided that the availability statuses will default to "in_progress" whenever they come empty, since
// setting them as "unavailable" by default may lead to some confusion to the calling clients.
if ecr.AvailabilityStatus == "" {
ecr.AvailabilityStatus = model.InProgress
} else {
if _, ok := model.ValidEndpointAvailabilityStatuses[ecr.AvailabilityStatus]; !ok {
return fmt.Errorf("invalid availability status")
}
}
return nil
}
func ValidateEndpointEditRequest(dao dao.EndpointDao, sourceId int64, editRequest *model.EndpointEditRequest) error {
if editRequest.Default != nil && (*editRequest.Default && !dao.CanEndpointBeSetAsDefaultForSource(sourceId)) {
return fmt.Errorf("a default endpoint already exists for the provided source")
}
if editRequest.Role != nil && !dao.IsRoleUniqueForSource(*editRequest.Role, sourceId) {
return fmt.Errorf("the role already exists for the given source")
}
if editRequest.Scheme == nil || (editRequest.Scheme != nil && !schemeRegexp.MatchString(*editRequest.Scheme)) {
editRequest.Scheme = util.StringRef(defaultScheme)
}
if editRequest.Host != nil && *editRequest.Host != "" {
if utf8.RuneCountInString(*editRequest.Host) > maxFqdnLength {
return fmt.Errorf("the provided host is longer than %d characters", maxFqdnLength)
}
if !fqdnRegexp.MatchString(*editRequest.Host) {
return fmt.Errorf("the provided host is invalid")
}
labels := strings.Split(*editRequest.Host, ".")
for _, label := range labels {
if utf8.RuneCountInString(*editRequest.Host) > maxLabelLength {
return fmt.Errorf("the label '%s' is greater than %d characters", label, maxLabelLength)
}
}
}
if editRequest.Port == nil || (editRequest.Port != nil && *editRequest.Port <= 0) {
tmp := defaultPort
editRequest.Port = &tmp
}
if *editRequest.Port > maxPort {
return fmt.Errorf("invalid port number")
}
if editRequest.VerifySsl == nil {
tmp := defaultVerifySsl
editRequest.VerifySsl = &tmp
}
if *editRequest.VerifySsl && (editRequest.CertificateAuthority == nil || *editRequest.CertificateAuthority == "") {
return fmt.Errorf("the certificate authority cannot be empty")
}
if editRequest.AvailabilityStatus != nil {
if _, ok := model.ValidEndpointAvailabilityStatuses[*editRequest.AvailabilityStatus]; !ok {
return fmt.Errorf("invalid availability status")
}
}
return nil
}