This repository has been archived by the owner on Nov 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
sample.go
494 lines (422 loc) · 16.1 KB
/
sample.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
package main
import (
"errors"
"flag"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"sort"
"strings"
"time"
"github.com/Azure/azure-sdk-for-go/profiles/latest/network/mgmt/network"
"github.com/Azure/azure-sdk-for-go/profiles/latest/resources/mgmt/resources"
"github.com/Azure/go-autorest/autorest"
"github.com/Azure/go-autorest/autorest/adal"
"github.com/Azure/go-autorest/autorest/azure"
"github.com/Azure/go-autorest/autorest/to"
"github.com/marstr/guid"
)
const (
sampleSecurityGroupName string = "samplesecuritygroup"
sampleSecurityGroupLabel string = "Auto-generated Go-Sample Network Security Group"
)
// Authentication environment variable names.
const (
azureEnvVarNameSubscriptionID string = "AZURE_SUBSCRIPTION_ID"
azureEnvVarNameClientID string = "AZURE_CLIENT_ID"
azureEnvVarNameClientSecret string = "AZURE_CLIENT_SECRET"
azureEnvVarNameTenantID string = "AZURE_TENANT_ID"
)
// Authentication environment variable values & the
var (
subscriptionID string
clientID string
clientSecret string
tenantID string
token *adal.ServicePrincipalToken
)
// User adjustable variables to customized execution environment depending on
// Azure Subscription requirements.
var (
sampleLocation = "westus2"
)
// Capture flow control options as defined by the arguments provided to this program.
var (
output io.Writer
pause bool
delay uint
help bool
)
// Exit status codes associated with various error cases.
const (
ExitSuccess = iota
ExitAuthenticationFailure
ExitResourceGroupCreationFailure
ExitVirtualNetworkCreationFailure
ExitNetworkSecuritryGroupCreationFailure
ExitSecurityRuleCreationFailure
ExitSubnetCreationFailure
)
func main() {
exitStatus := ExitSuccess
defer func() {
os.Exit(exitStatus)
}()
if help {
return
}
//Ensure authentication is setup correctly before continuing.
if nil == token {
fmt.Fprintln(os.Stderr, "Fatal Error: Authentication Failed.")
exitStatus = ExitAuthenticationFailure
return
}
bearer := autorest.NewBearerAuthorizer(token)
cancel := make(chan struct{})
// Make an isolated environment to store assets created for this sample.
resourceGroupClient, resourceGroupName, err := createResourceGroup(cancel)
if err != nil {
exitStatus = ExitResourceGroupCreationFailure
return
}
defer deleteResourceGroup(resourceGroupClient, resourceGroupName, cancel)
// Create a network that will be the target of network security groups
_, vNetName, err := createVirtualNetwork(resourceGroupName, cancel)
if nil != err {
exitStatus = ExitVirtualNetworkCreationFailure
return
}
// Create two Network Security Groups, one to be used for front-end requests, another for back-end
nsgClient := network.NewSecurityGroupsClient(subscriptionID)
nsgClient.Authorizer = bearer
frontEndNSG, err := createNetworkSecurityGroup(nsgClient, resourceGroupName, "frontend", cancel)
if nil != err {
exitStatus = ExitNetworkSecuritryGroupCreationFailure
return
}
backEndNSG, err := createNetworkSecurityGroup(nsgClient, resourceGroupName, "backend", cancel)
if nil != err {
exitStatus = ExitNetworkSecuritryGroupCreationFailure
return
}
//Create Subnets to host Virtual Machines which will be protected by the rules above.
subNetClient := network.NewSubnetsClient(subscriptionID)
subNetClient.Authorizer = bearer
frontendAddressPrefix := to.StringPtr("192.168.1.0/24")
frontendSubnet := network.Subnet{
Name: to.StringPtr("frontendSubnet"),
SubnetPropertiesFormat: &network.SubnetPropertiesFormat{
AddressPrefix: frontendAddressPrefix,
NetworkSecurityGroup: &frontEndNSG,
},
}
backendAddressPrefix := to.StringPtr("192.168.2.0/24")
backendSubnet := network.Subnet{
Name: to.StringPtr("backendSubnet"),
SubnetPropertiesFormat: &network.SubnetPropertiesFormat{
AddressPrefix: backendAddressPrefix,
NetworkSecurityGroup: &backEndNSG,
},
}
_, err = executeWithStatus(func() (resp autorest.Response, err error) {
respChan, errChan := subNetClient.CreateOrUpdate(resourceGroupName, vNetName, *frontendSubnet.Name, frontendSubnet, cancel)
resp, err = (<-respChan).Response, <-errChan
return
}, fmt.Sprintf("Creating Subnet '%s'", *frontendSubnet.Name))
if err != nil {
exitStatus = ExitSubnetCreationFailure
return
}
_, err = executeWithStatus(func() (autorest.Response, error) {
respChan, errChan := subNetClient.CreateOrUpdate(resourceGroupName, vNetName, *backendSubnet.Name, backendSubnet, cancel)
return (<-respChan).Response, <-errChan
}, fmt.Sprintf("Creating Subnet '%s'", *backendSubnet.Name))
if err != nil {
exitStatus = ExitSubnetCreationFailure
return
}
// Create the security rules that should be enforced, and associate them with their respective security group.
ruleClient := network.NewSecurityRulesClient(subscriptionID)
ruleClient.Authorizer = autorest.NewBearerAuthorizer(token)
anyPortRange := "*"
anyAddressPrefix := "*"
sshPortRange := "22"
sshRuleDesc := "Allow SSH"
sshRulePriority := int32(100)
frontendSSHRuleName := "ALLOW-SSH"
frontendSSHRule := network.SecurityRule{
Name: &frontendSSHRuleName,
SecurityRulePropertiesFormat: &network.SecurityRulePropertiesFormat{
Access: network.SecurityRuleAccessAllow,
DestinationAddressPrefix: &anyAddressPrefix,
DestinationPortRange: &sshPortRange,
Direction: network.SecurityRuleDirectionInbound,
Description: &sshRuleDesc,
Priority: &sshRulePriority,
Protocol: network.SecurityRuleProtocolTCP,
SourceAddressPrefix: &anyAddressPrefix,
SourcePortRange: &anyPortRange,
},
}
_, err = executeWithStatus(func() (autorest.Response, error) {
respChan, errChan := ruleClient.CreateOrUpdate(resourceGroupName, *frontEndNSG.Name, frontendSSHRuleName, frontendSSHRule, cancel)
return (<-respChan).Response, <-errChan
}, fmt.Sprintf("Creating Security Rule '%s'", *frontendSSHRule.Description))
if nil != err {
exitStatus = ExitSecurityRuleCreationFailure
return
}
frontendHTTPRuleName := "ALLOW-HTTP"
frontendHTTPRule := network.SecurityRule{
SecurityRulePropertiesFormat: &network.SecurityRulePropertiesFormat{
Access: network.SecurityRuleAccessAllow,
DestinationAddressPrefix: &anyAddressPrefix,
DestinationPortRange: to.StringPtr("80"),
Direction: network.SecurityRuleDirectionInbound,
Description: to.StringPtr("Allow HTTP"),
Priority: to.Int32Ptr(101),
Protocol: network.SecurityRuleProtocolTCP,
SourceAddressPrefix: &anyAddressPrefix,
SourcePortRange: &anyPortRange,
},
}
executeWithStatus(func() (autorest.Response, error) {
respChan, errChan := ruleClient.CreateOrUpdate(resourceGroupName, *frontEndNSG.Name, frontendHTTPRuleName, frontendHTTPRule, cancel)
return (<-respChan).Response, <-errChan
}, fmt.Sprintf("Creating Security Rule '%s'", *frontendHTTPRule.Description))
if nil != err {
exitStatus = ExitSecurityRuleCreationFailure
return
}
sqlRuleName := "ALLOW-SQL"
backendSQLRule := network.SecurityRule{
SecurityRulePropertiesFormat: &network.SecurityRulePropertiesFormat{
Access: network.SecurityRuleAccessAllow,
DestinationAddressPrefix: &anyAddressPrefix,
DestinationPortRange: to.StringPtr("1433"),
Direction: network.SecurityRuleDirectionInbound,
Description: to.StringPtr("Allow SQL"),
Priority: to.Int32Ptr(100),
Protocol: network.SecurityRuleProtocolTCP,
SourceAddressPrefix: frontendAddressPrefix,
SourcePortRange: &anyPortRange,
},
}
executeWithStatus(func() (autorest.Response, error) {
respChan, errChan := ruleClient.CreateOrUpdate(resourceGroupName, *backEndNSG.Name, sqlRuleName, backendSQLRule, cancel)
return (<-respChan).Response, <-errChan
}, fmt.Sprintf("Creating Security Rule \"%s\"", sqlRuleName))
outDenyName := "DENY-OUT"
backendOutboundRule := network.SecurityRule{
SecurityRulePropertiesFormat: &network.SecurityRulePropertiesFormat{
Access: network.SecurityRuleAccessDeny,
DestinationAddressPrefix: &anyAddressPrefix,
DestinationPortRange: &anyPortRange,
Direction: network.SecurityRuleDirectionOutbound,
Description: to.StringPtr("Deny Outbound traffic"),
Priority: to.Int32Ptr(100),
Protocol: network.SecurityRuleProtocolAsterisk,
SourceAddressPrefix: &anyAddressPrefix,
SourcePortRange: &anyPortRange,
},
}
executeWithStatus(func() (autorest.Response, error) {
respChan, errChan := ruleClient.CreateOrUpdate(resourceGroupName, *backEndNSG.Name, outDenyName, backendOutboundRule, cancel)
return (<-respChan).Response, <-errChan
}, fmt.Sprintf("Creating Security Rule \"%s\"", outDenyName))
// Give the user time to go inspect their subscription if they desire.
if pause {
fmt.Printf("Press Enter to continue...")
fmt.Scanln()
} else if delay > 0 {
fmt.Fprintf(output, "Delaying %d seconds...", delay)
time.Sleep(time.Second * time.Duration(delay))
fmt.Fprintln(output, "DONE")
}
}
func init() {
// Setup execution environment
subscriptionID = os.Getenv(azureEnvVarNameSubscriptionID)
clientID = os.Getenv(azureEnvVarNameClientID)
clientSecret = os.Getenv(azureEnvVarNameClientSecret)
tenantID = os.Getenv(azureEnvVarNameTenantID)
useQuiet := flag.Bool("quiet", false, "Prevents status messages from being printed to stdout.")
flag.BoolVar(&pause, "pause", false, "After all sample assets are created, wait for user response before removing all assets created for this sample.")
flag.UintVar(&delay, "delay", 0, "An alternative to 'pause' which waits the specified number of seconds before removing all assets created for this sample.")
flag.BoolVar(&help, "help", false, "Instead of executing this sample, enumerates the available flags.")
flag.Parse()
if help {
flag.PrintDefaults()
return
}
if *useQuiet {
output = ioutil.Discard
} else {
output = os.Stdout
}
if errs := validateParameters(); len(errs) > 0 {
for _, err := range errs {
fmt.Fprintf(os.Stderr, "Invalid argument. Details: %v\n", err)
}
return
}
//Authenticate
authConfig, err := adal.NewOAuthConfig(azure.PublicCloud.ActiveDirectoryEndpoint, tenantID)
if nil != err {
fmt.Fprint(os.Stderr, err)
return
}
token, err = adal.NewServicePrincipalToken(*authConfig, clientID, clientSecret, azure.PublicCloud.ResourceManagerEndpoint)
if nil != err {
fmt.Fprint(os.Stderr, err)
return
}
}
func createNetworkSecurityGroup(client network.SecurityGroupsClient, resourceGroupName string, name string, cancel <-chan struct{}) (network.SecurityGroup, error) {
args := network.SecurityGroup{
Name: &name,
Location: &sampleLocation,
}
_, err := executeWithStatus(func() (autorest.Response, error) {
respChan, errChan := client.CreateOrUpdate(resourceGroupName, name, args, cancel)
return (<-respChan).Response, <-errChan
}, fmt.Sprintf("Creating Network Security Group '%s'", name))
result, err := client.Get(resourceGroupName, *args.Name, "")
return result, err
}
func createResourceGroup(cancel <-chan struct{}) (resources.GroupsClient, string, error) {
resourceGroupClient := resources.NewGroupsClient(subscriptionID)
resourceGroupClient.Authorizer = autorest.NewBearerAuthorizer(token)
resourceGroupName, err := getUniqueResourceGroupName(resourceGroupClient)
if err != nil {
fmt.Fprintf(os.Stderr, "%v", err)
return resourceGroupClient, resourceGroupName, err
}
resourceGroupParameters := resources.Group{
Location: &sampleLocation,
}
fmt.Fprintf(output, "Creating Resource Group '%s'...", resourceGroupName)
_, err = resourceGroupClient.CreateOrUpdate(resourceGroupName, resourceGroupParameters)
if err != nil {
fmt.Fprintln(output, "FAILED")
fmt.Fprintf(os.Stderr, "\tError: %v\n", err)
return resourceGroupClient, "", err
}
fmt.Fprintln(output, "SUCCESS")
return resourceGroupClient, resourceGroupName, err
}
func deleteResourceGroup(client resources.GroupsClient, name string, cancel <-chan struct{}) error {
_, err := executeWithStatus(func() (autorest.Response, error) {
respChan, errChan := client.Delete(name, cancel)
return <-respChan, <-errChan
}, fmt.Sprintf("Deleting Resource Group '%s'", name))
return err
}
func createVirtualNetwork(resourceGroupName string, cancel <-chan struct{}) (network.VirtualNetworksClient, string, error) {
const networkName = "sampleVirtualNetwork"
vNetParameters := network.VirtualNetwork{
Location: &sampleLocation,
VirtualNetworkPropertiesFormat: &network.VirtualNetworkPropertiesFormat{
AddressSpace: &network.AddressSpace{
AddressPrefixes: &[]string{"192.168.0.0/16"},
},
},
}
vNetClient := network.NewVirtualNetworksClient(subscriptionID)
vNetClient.Authorizer = autorest.NewBearerAuthorizer(token)
_, err := executeWithStatus(func() (autorest.Response, error) {
respChan, errChan := vNetClient.CreateOrUpdate(resourceGroupName, networkName, vNetParameters, cancel)
return (<-respChan).Response, <-errChan
}, fmt.Sprintf("Creating Virtual Network '%s'", networkName))
return vNetClient, networkName, err
}
func validateParameters() []error {
const preferredGUIDFormat guid.Format = guid.FormatD
const uuidErrTemplate = "argument '%s' was not of type Uuid as expected"
errs := make([]error, 0)
if "" == tenantID {
errs = append(errs, newMissingAzureAuthError("Azure Tenant ID", azureEnvVarNameTenantID))
} else if parsed, err := guid.Parse(tenantID); nil != err {
errs = append(errs, fmt.Errorf(uuidErrTemplate, azureEnvVarNameTenantID))
} else {
tenantID = parsed.Stringf(preferredGUIDFormat)
}
if "" == subscriptionID {
errs = append(errs, newMissingAzureAuthError("Azure Subscription ID", azureEnvVarNameSubscriptionID))
} else if parsed, err := guid.Parse(subscriptionID); nil != err {
errs = append(errs, fmt.Errorf(uuidErrTemplate, azureEnvVarNameSubscriptionID))
} else {
subscriptionID = parsed.Stringf(preferredGUIDFormat)
}
if "" == clientID {
errs = append(errs, newMissingAzureAuthError("Azure Client ID", azureEnvVarNameClientID))
} else if parsed, err := guid.Parse(clientID); nil != err {
errs = append(errs, fmt.Errorf(uuidErrTemplate, azureEnvVarNameClientID))
} else {
clientID = parsed.Stringf(preferredGUIDFormat)
}
if "" == clientSecret {
errs = append(errs, newMissingAzureAuthError("Azure Client Secret", azureEnvVarNameClientSecret))
}
return errs
}
func newMissingAzureAuthError(pretty string, envVarName string) error {
formatted := fmt.Sprintf("No value was provieded to act as the %s. Set enviroment variable \"%s\"", pretty, envVarName)
return errors.New(formatted)
}
func getUniqueResourceGroupName(client resources.GroupsClient) (string, error) {
const resourceGroupNamePrefix = "networkSecurityGroupSample"
groupList, err := client.List("", nil)
if nil != err {
return "", err
}
if http.StatusOK == groupList.Response.StatusCode {
any := false
seen := []string{}
for _, rg := range *groupList.Value {
if strings.HasPrefix(*rg.Name, resourceGroupNamePrefix) {
any = true
seen = append(seen, *rg.Name)
}
}
sort.Strings(seen)
if any && seen[0] == resourceGroupNamePrefix {
count := len(seen) - 1
for i := 0; i < count; i++ {
candidate := fmt.Sprintf("%s%d", resourceGroupNamePrefix, i)
if seen[i+1] != candidate {
return candidate, nil
}
}
return fmt.Sprintf("%s%d", resourceGroupNamePrefix, len(seen)-1), nil
}
return resourceGroupNamePrefix, nil
}
return "", fmt.Errorf("Bad response: %d", groupList.Response.StatusCode)
}
func executeWithStatus(operation func() (autorest.Response, error), message string) (response autorest.Response, err error) {
fmt.Fprintf(output, "%s...", message)
response, err = operation()
if nil == err && http.StatusOK == response.StatusCode {
fmt.Fprintln(output, "SUCCESS")
} else {
fmt.Fprintln(output, "FAILED")
fmt.Fprint(os.Stderr, getFailureStatus(err, response))
}
return
}
func getFailureStatus(err error, response autorest.Response) string {
retval := ""
if response.Response != nil {
retval += fmt.Sprintf("\tStatus Code: %d\n\tStatus: %s\n", response.StatusCode, response.Status)
}
if nil != err {
retval += fmt.Sprintf("\tError: %v\n", err)
}
if "" == retval {
retval = "An unknown error occurred.\n"
}
return retval
}