Skip to content

Commit 8a1a75e

Browse files
committed
Merge remote-tracking branch 'origin/dev'
Conflicts: clients/vmClient/vmClient.go
2 parents 7daeb26 + c25453f commit 8a1a75e

File tree

1 file changed

+98
-25
lines changed

1 file changed

+98
-25
lines changed

clients/vmClient/vmClient.go

Lines changed: 98 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,20 @@ import (
88
"encoding/xml"
99
"errors"
1010
"fmt"
11-
azure "github.com/MSOpenTech/azure-sdk-for-go"
12-
"github.com/MSOpenTech/azure-sdk-for-go/clients/imageClient"
13-
"github.com/MSOpenTech/azure-sdk-for-go/clients/locationClient"
14-
"github.com/MSOpenTech/azure-sdk-for-go/clients/storageServiceClient"
15-
"github.com/MSOpenTech/azure-sdk-for-go/clients/vmDiskClient"
1611
"io"
1712
"io/ioutil"
1813
"os"
1914
"os/user"
2015
"path"
2116
"strings"
2217
"time"
18+
"unicode"
19+
20+
azure "github.com/MSOpenTech/azure-sdk-for-go"
21+
"github.com/MSOpenTech/azure-sdk-for-go/clients/imageClient"
22+
"github.com/MSOpenTech/azure-sdk-for-go/clients/locationClient"
23+
"github.com/MSOpenTech/azure-sdk-for-go/clients/storageServiceClient"
24+
"github.com/MSOpenTech/azure-sdk-for-go/clients/vmDiskClient"
2325
)
2426

2527
const (
@@ -44,11 +46,17 @@ const (
4446
provisioningConfDoesNotExistsError = "You should set azure VM provisioning config first"
4547
invalidCertExtensionError = "Certificate %s is invalid. Please specify %s certificate."
4648
invalidOSError = "You must specify correct OS param. Valid values are 'Linux' and 'Windows'"
49+
invalidDnsLengthError = "The DNS name must be between 3 and 25 characters."
50+
invalidPasswordLengthError = "Password must be between 4 and 30 characters."
51+
invalidPasswordError = "Password must have at least one upper case, lower case and numeric character."
4752
)
4853

4954
//Region public methods starts
5055

51-
func CreateAzureVM(role *Role, dnsName, location string) error {
56+
func CreateAzureVM(azureVMConfiguration *Role, dnsName, location string) error {
57+
if azureVMConfiguration == nil {
58+
return fmt.Errorf(azure.ParamNotSpecifiedError, "azureVMConfiguration")
59+
}
5260
if len(dnsName) == 0 {
5361
return fmt.Errorf(azure.ParamNotSpecifiedError, "dnsName")
5462
}
@@ -57,31 +65,37 @@ func CreateAzureVM(role *Role, dnsName, location string) error {
5765
}
5866

5967
fmt.Println("Creating hosted service... ")
68+
69+
err := verifyDNSname(dnsName)
70+
if err != nil {
71+
return err
72+
}
73+
6074
requestId, err := CreateHostedService(dnsName, location)
6175
if err != nil {
6276
return err
6377
}
6478

6579
azure.WaitAsyncOperation(requestId)
6680

67-
if role.UseCertAuth {
81+
if azureVMConfiguration.UseCertAuth {
6882
fmt.Println("Uploading cert...")
6983

70-
err = uploadServiceCert(dnsName, role.CertPath)
84+
err = uploadServiceCert(dnsName, azureVMConfiguration.CertPath)
7185
if err != nil {
7286
return err
7387
}
7488
}
7589

7690
fmt.Println("Deploying azure VM configuration... ")
7791

78-
vMDeployment := createVMDeploymentConfig(role)
92+
vMDeployment := createVMDeploymentConfig(azureVMConfiguration)
7993
vMDeploymentBytes, err := xml.Marshal(vMDeployment)
8094
if err != nil {
8195
return err
8296
}
8397

84-
requestURL := fmt.Sprintf(azureDeploymentListURL, role.RoleName)
98+
requestURL := fmt.Sprintf(azureDeploymentListURL, azureVMConfiguration.RoleName)
8599
requestId, err = azure.SendAzurePostRequest(requestURL, vMDeploymentBytes)
86100
if err != nil {
87101
return err
@@ -100,6 +114,11 @@ func CreateHostedService(dnsName, location string) (string, error) {
100114
return "", fmt.Errorf(azure.ParamNotSpecifiedError, "location")
101115
}
102116

117+
err := verifyDNSname(dnsName)
118+
if err != nil {
119+
return "", err
120+
}
121+
103122
result, reason, err := CheckHostedServiceNameAvailability(dnsName)
104123
if err != nil {
105124
return "", err
@@ -133,6 +152,11 @@ func CheckHostedServiceNameAvailability(dnsName string) (bool, string, error) {
133152
return false, "", fmt.Errorf(azure.ParamNotSpecifiedError, "dnsName")
134153
}
135154

155+
err := verifyDNSname(dnsName)
156+
if err != nil {
157+
return false, "", err
158+
}
159+
136160
requestURL := fmt.Sprintf(azureHostedServiceAvailabilityURL, dnsName)
137161
response, err := azure.SendAzureGetRequest(requestURL)
138162
if err != nil {
@@ -153,6 +177,11 @@ func DeleteHostedService(dnsName string) error {
153177
return fmt.Errorf(azure.ParamNotSpecifiedError, "dnsName")
154178
}
155179

180+
err := verifyDNSname(dnsName)
181+
if err != nil {
182+
return err
183+
}
184+
156185
requestURL := fmt.Sprintf(azureHostedServiceURL, dnsName)
157186
requestId, err := azure.SendAzureDeleteRequest(requestURL)
158187
if err != nil {
@@ -163,9 +192,9 @@ func DeleteHostedService(dnsName string) error {
163192
return nil
164193
}
165194

166-
func CreateAzureVMConfiguration(name, instanceSize, imageName, location string) (*Role, error) {
167-
if len(name) == 0 {
168-
return nil, fmt.Errorf(azure.ParamNotSpecifiedError, "name")
195+
func CreateAzureVMConfiguration(dnsName, instanceSize, imageName, location string) (*Role, error) {
196+
if len(dnsName) == 0 {
197+
return nil, fmt.Errorf(azure.ParamNotSpecifiedError, "dnsName")
169198
}
170199
if len(instanceSize) == 0 {
171200
return nil, fmt.Errorf(azure.ParamNotSpecifiedError, "instanceSize")
@@ -179,32 +208,36 @@ func CreateAzureVMConfiguration(name, instanceSize, imageName, location string)
179208

180209
fmt.Println("Creating azure VM configuration... ")
181210

182-
err := locationClient.ResolveLocation(location)
211+
err := verifyDNSname(dnsName)
212+
if err != nil {
213+
return nil, err
214+
}
215+
216+
err = locationClient.ResolveLocation(location)
183217
if err != nil {
184218
return nil, err
185219
}
186220

187-
role, err := createAzureVMRole(name, instanceSize, imageName, location)
221+
role, err := createAzureVMRole(dnsName, instanceSize, imageName, location)
188222
if err != nil {
189223
return nil, err
190224
}
191225

192226
return role, nil
193227
}
194228

195-
func AddAzureLinuxProvisioningConfig(azureVMConfig *Role, userName, password, certPath string) (*Role, error) {
229+
func AddAzureLinuxProvisioningConfig(azureVMConfiguration *Role, userName, password, certPath string) (*Role, error) {
230+
if azureVMConfiguration == nil {
231+
return nil, fmt.Errorf(azure.ParamNotSpecifiedError, "azureVMConfiguration")
232+
}
196233
if len(userName) == 0 {
197234
return nil, fmt.Errorf(azure.ParamNotSpecifiedError, "userName")
198235
}
199-
if len(password) == 0 {
200-
return nil, fmt.Errorf(azure.ParamNotSpecifiedError, "password")
201-
}
202236

203237
fmt.Println("Adding azure provisioning configuration... ")
204238

205239
configurationSets := ConfigurationSets{}
206-
207-
provisioningConfig, err := createLinuxProvisioningConfig(azureVMConfig.RoleName, userName, password, certPath)
240+
provisioningConfig, err := createLinuxProvisioningConfig(azureVMConfiguration.RoleName, userName, password, certPath)
208241
if err != nil {
209242
return nil, err
210243
}
@@ -218,17 +251,20 @@ func AddAzureLinuxProvisioningConfig(azureVMConfig *Role, userName, password, ce
218251

219252
configurationSets.ConfigurationSet = append(configurationSets.ConfigurationSet, networkConfig)
220253

221-
azureVMConfig.ConfigurationSets = configurationSets
254+
azureVMConfiguration.ConfigurationSets = configurationSets
222255

223256
if len(certPath) > 0 {
224-
azureVMConfig.UseCertAuth = true
225-
azureVMConfig.CertPath = certPath
257+
azureVMConfiguration.UseCertAuth = true
258+
azureVMConfiguration.CertPath = certPath
226259
}
227260

228-
return azureVMConfig, nil
261+
return azureVMConfiguration, nil
229262
}
230263

231264
func SetAzureVMExtension(azureVMConfiguration *Role, name string, publisher string, version string, referenceName string, state string, publicConfigurationValue string, privateConfigurationValue string) (*Role, error) {
265+
if azureVMConfiguration == nil {
266+
return nil, fmt.Errorf(azure.ParamNotSpecifiedError, "azureVMConfiguration")
267+
}
232268
if len(name) == 0 {
233269
return nil, fmt.Errorf(azure.ParamNotSpecifiedError, "name")
234270
}
@@ -275,6 +311,9 @@ func SetAzureVMExtension(azureVMConfiguration *Role, name string, publisher stri
275311
}
276312

277313
func SetAzureDockerVMExtension(azureVMConfiguration *Role, dockerCertDir string, dockerPort int, version string) (*Role, error) {
314+
if azureVMConfiguration == nil {
315+
return nil, fmt.Errorf(azure.ParamNotSpecifiedError, "azureVMConfiguration")
316+
}
278317
if len(dockerCertDir) == 0 {
279318
return nil, fmt.Errorf(azure.ParamNotSpecifiedError, "dockerCertDir")
280319
}
@@ -686,6 +725,11 @@ func createLinuxProvisioningConfig(dnsName, userName, userPassword, certPath str
686725
disableSshPasswordAuthentication = true
687726
// We need to set dummy password otherwise azure API will throw an error
688727
userPassword = "P@ssword1"
728+
} else {
729+
err := verifyPassword(userPassword)
730+
if err != nil {
731+
return provisioningConfig, err
732+
}
689733
}
690734

691735
provisioningConfig.DisableSshPasswordAuthentication = disableSshPasswordAuthentication
@@ -818,4 +862,33 @@ func createEndpoint(name string, protocol string, extertalPort int, internalPort
818862
return endpoint
819863
}
820864

865+
func verifyDNSname(dns string) error {
866+
if len(dns) < 3 || len(dns) > 25 {
867+
return fmt.Errorf(invalidDnsLengthError)
868+
}
869+
870+
return nil
871+
}
872+
873+
func verifyPassword(password string) error {
874+
if len(password) < 4 || len(password) > 30 {
875+
return fmt.Errorf(invalidPasswordLengthError)
876+
}
877+
878+
next:
879+
for _, classes := range map[string][]*unicode.RangeTable{
880+
"upper case": {unicode.Upper, unicode.Title},
881+
"lower case": {unicode.Lower},
882+
"numeric": {unicode.Number, unicode.Digit},
883+
} {
884+
for _, r := range password {
885+
if unicode.IsOneOf(classes, r) {
886+
continue next
887+
}
888+
}
889+
return fmt.Errorf(invalidPasswordError)
890+
}
891+
return nil
892+
}
893+
821894
//Region private methods ends

0 commit comments

Comments
 (0)