forked from Azure/acs-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
validate.go
181 lines (158 loc) · 5.79 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
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package v20170831
import (
"fmt"
"regexp"
"strings"
validator "gopkg.in/go-playground/validator.v9"
)
var validate *validator.Validate
func init() {
validate = validator.New()
}
// Validate implements APIObject
func (a *AgentPoolProfile) Validate() error {
// Don't need to call validate.Struct(a)
// It is handled by Properties.Validate()
return validatePoolName(a.Name)
}
// Validate implements APIObject
func (l *LinuxProfile) Validate() error {
// Don't need to call validate.Struct(l)
// It is handled by Properties.Validate()
if e := validate.Var(l.SSH.PublicKeys[0].KeyData, "required"); e != nil {
return fmt.Errorf("KeyData in LinuxProfile.SSH.PublicKeys cannot be empty string")
}
return nil
}
func handleValidationErrors(e validator.ValidationErrors) error {
err := e[0]
ns := err.Namespace()
switch ns {
// TODO: Add more validation here
case "Properties.LinuxProfile", "Properties.ServicePrincipalProfile.ClientID",
"Properties.ServicePrincipalProfile.Secret", "Properties.WindowsProfile.AdminUsername",
"Properties.WindowsProfile.AdminPassword":
return fmt.Errorf("missing %s", ns)
default:
if strings.HasPrefix(ns, "Properties.AgentPoolProfiles") {
switch {
case strings.HasSuffix(ns, ".Name") || strings.HasSuffix(ns, "VMSize"):
return fmt.Errorf("missing %s", ns)
case strings.HasSuffix(ns, ".Count"):
return fmt.Errorf("AgentPoolProfile count needs to be in the range [%d,%d]", MinAgentCount, MaxAgentCount)
case strings.HasSuffix(ns, ".OSDiskSizeGB"):
return fmt.Errorf("Invalid os disk size of %d specified. The range of valid values are [%d, %d]", err.Value().(int), MinDiskSizeGB, MaxDiskSizeGB)
case strings.HasSuffix(ns, ".StorageProfile"):
return fmt.Errorf("Unknown storageProfile '%s'. Must specify %s", err.Value().(string), ManagedDisks)
default:
break
}
}
}
return fmt.Errorf("Namespace %s is not caught, %+v", ns, e)
}
// Validate implements APIObject
func (a *Properties) Validate() error {
if e := validate.Struct(a); e != nil {
return handleValidationErrors(e.(validator.ValidationErrors))
}
// Don't need to call validate.Struct(m)
// It is handled by Properties.Validate()
if e := validateDNSName(a.DNSPrefix); e != nil {
return e
}
if e := validateUniqueProfileNames(a.AgentPoolProfiles); e != nil {
return e
}
for _, agentPoolProfile := range a.AgentPoolProfiles {
if e := agentPoolProfile.Validate(); e != nil {
return e
}
}
if e := a.LinuxProfile.Validate(); e != nil {
return e
}
if e := validateVNET(a); e != nil {
return e
}
return nil
}
func validatePoolName(poolName string) error {
// we will cap at length of 12 and all lowercase letters since this makes up the VMName
poolNameRegex := `^([a-z][a-z0-9]{0,11})$`
re, err := regexp.Compile(poolNameRegex)
if err != nil {
return err
}
submatches := re.FindStringSubmatch(poolName)
if len(submatches) != 2 {
return fmt.Errorf("pool name '%s' is invalid. A pool name must start with a lowercase letter, have max length of 12, and only have characters a-z0-9", poolName)
}
return nil
}
func validateDNSName(dnsName string) error {
dnsNameRegex := `^([A-Za-z][A-Za-z0-9-]{1,43}[A-Za-z0-9])$`
re, err := regexp.Compile(dnsNameRegex)
if err != nil {
return err
}
if !re.MatchString(dnsName) {
return fmt.Errorf("DNS name '%s' is invalid. The DNS name must contain between 3 and 45 characters. The name can contain only letters, numbers, and hyphens. The name must start with a letter and must end with a letter or a number. (length was %d)", dnsName, len(dnsName))
}
return nil
}
func validateUniqueProfileNames(profiles []*AgentPoolProfile) error {
profileNames := make(map[string]bool)
for _, profile := range profiles {
if _, ok := profileNames[profile.Name]; ok {
return fmt.Errorf("profile name '%s' already exists, profile names must be unique across pools", profile.Name)
}
profileNames[profile.Name] = true
}
return nil
}
func validateVNET(a *Properties) error {
var customVNETCount int
var isCustomVNET bool
for _, agentPool := range a.AgentPoolProfiles {
if agentPool.IsCustomVNET() {
customVNETCount++
isCustomVNET = agentPool.IsCustomVNET()
}
}
if !(customVNETCount == 0 || customVNETCount == len(a.AgentPoolProfiles)) {
return fmt.Errorf("Multiple VNET Subnet configurations specified. Each agent pool profile must all specify a custom VNET Subnet, or none at all")
}
subIDMap := make(map[string]int)
resourceGroupMap := make(map[string]int)
agentVNETMap := make(map[string]int)
if isCustomVNET {
for _, agentPool := range a.AgentPoolProfiles {
agentSubID, agentRG, agentVNET, _, err := GetVNETSubnetIDComponents(agentPool.VnetSubnetID)
if err != nil {
return err
}
subIDMap[agentSubID] = subIDMap[agentSubID] + 1
resourceGroupMap[agentRG] = resourceGroupMap[agentRG] + 1
agentVNETMap[agentVNET] = agentVNETMap[agentVNET] + 1
}
// TODO: Add more validation to ensure all agent pools belong to the same VNET, subscription, and resource group
// if(len(subIDMap) != len(a.AgentPoolProfiles))
// return errors.New("Multiple VNETS specified. Each agent pool must reference the same VNET (but it is ok to reference different subnets on that VNET)")
// }
}
return nil
}
// GetVNETSubnetIDComponents extract subscription, resourcegroup, vnetname, subnetname from the vnetSubnetID
func GetVNETSubnetIDComponents(vnetSubnetID string) (string, string, string, string, error) {
vnetSubnetIDRegex := `^\/subscriptions\/([^\/]*)\/resourceGroups\/([^\/]*)\/providers\/Microsoft.Network\/virtualNetworks\/([^\/]*)\/subnets\/([^\/]*)$`
re, err := regexp.Compile(vnetSubnetIDRegex)
if err != nil {
return "", "", "", "", err
}
submatches := re.FindStringSubmatch(vnetSubnetID)
if len(submatches) != 4 {
return "", "", "", "", err
}
return submatches[1], submatches[2], submatches[3], submatches[4], nil
}