-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
322 lines (302 loc) · 16.1 KB
/
Program.cs
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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
using Azure;
using Azure.Core;
using Azure.Identity;
using Azure.ResourceManager;
using Azure.ResourceManager.Network;
using Azure.ResourceManager.Network.Models;
using Azure.ResourceManager.Resources;
using Azure.ResourceManager.Samples.Common;
using Azure.ResourceManager.Storage;
using Azure.ResourceManager.Storage.Models;
using Azure.ResourceManager.Compute;
using Azure.ResourceManager.Compute.Models;
namespace ManageStorageAccountNetworkRules
{
/**
* Azure Storage sample for managing storage account network rules -
* - Create a virtual network and subnet with storage service subnet access enabled
* - Create a storage account with access allowed only from the subnet
* - Create a public IP address
* - Create a virtual machine and associate the public IP address
* - Update the storage account with access also allowed from the public IP address
* - Update the storage account to restrict incoming traffic to HTTPS.
*/
public class Program
{
private static ResourceIdentifier? _resourceGroupId = null;
public static async Task RunSample(ArmClient client)
{
try
{
// ============================================================
// Create a virtual network and a subnet with storage service subnet access enabled
Utilities.Log("Creating a Virtual network and a subnet with storage service subnet access enabled...");
// Get default subscription
SubscriptionResource subscription = await client.GetDefaultSubscriptionAsync();
var rgName = Utilities.CreateRandomName("VirtualNetworkRG");
Utilities.Log($"creating resource group with name : {rgName}");
var rgLro = await subscription.GetResourceGroups().CreateOrUpdateAsync(WaitUntil.Completed, rgName, new ResourceGroupData(AzureLocation.EastUS));
var resourceGroup = rgLro.Value;
_resourceGroupId = resourceGroup.Id;
Utilities.Log("Created a resource group with name: " + resourceGroup.Data.Name);
// Create a virtual network
Utilities.Log("Creating a virtual network...");
var virtualNetworkName = Utilities.CreateRandomName("VirtualNetwork_");
var virtualNetworkCollection = resourceGroup.GetVirtualNetworks();
var subnetName = Utilities.CreateRandomName("subnet_");
var data = new VirtualNetworkData()
{
Location = AzureLocation.EastUS,
AddressPrefixes =
{
new string("10.0.0.0/28"),
},
};
var virtualNetworkLro = await virtualNetworkCollection.CreateOrUpdateAsync(WaitUntil.Completed, virtualNetworkName,data);
var virtualNetwork = virtualNetworkLro.Value;
Utilities.Log("Created a virtual network with name : " + virtualNetwork.Data.Name);
//Create a subnet
Utilities.Log("Creating a subnet..");
var subnetData = new SubnetData()
{
ServiceEndpoints =
{
new ServiceEndpointProperties()
{
Service = "Microsoft.Storage"
}
},
Name = subnetName,
AddressPrefix = "10.0.0.8/29",
};
var subnetLRro = await virtualNetwork.GetSubnets().CreateOrUpdateAsync(WaitUntil.Completed, subnetName, subnetData);
var subnet = subnetLRro.Value;
Utilities.Log("Created a subnet with name : " + subnet.Data.Name);
// ============================================================
// Create a storage account with access to it allowed only from a specific subnet
var subnetId = $"{virtualNetwork.Data.Id}/subnets/{subnetName}";
Utilities.Log($"Creating a storage account with access allowed only from the subnet{subnetId}");
var storageCollection = resourceGroup.GetStorageAccounts();
var accountName = Utilities.CreateRandomName("saname");
var sku = new StorageSku(StorageSkuName.StandardGrs);
var kind = StorageKind.StorageV2;
var resourceId = new ResourceIdentifier(subnetId);
var content = new StorageAccountCreateOrUpdateContent(sku, kind, AzureLocation.EastUS)
{
NetworkRuleSet = new StorageAccountNetworkRuleSet(StorageNetworkDefaultAction.Deny)
{
VirtualNetworkRules =
{
new StorageAccountVirtualNetworkRule(resourceId),
}
}
};
var storageAccountLro = await storageCollection.CreateOrUpdateAsync(WaitUntil.Completed,accountName,content);
var storageAccount = storageAccountLro.Value;
Utilities.Log("Created a storage account with name : " + storageAccount.Data.Name);
// ============================================================
// Create a public IP address
Utilities.Log("Creating a Public IP address...");
var publicAddressIPCollection = resourceGroup.GetPublicIPAddresses();
var publicIPAddressName = Utilities.CreateRandomName("pip");
var publicIPAddressdata = new PublicIPAddressData()
{
Location = AzureLocation.EastUS,
Sku = new PublicIPAddressSku()
{
Name = PublicIPAddressSkuName.Standard,
},
PublicIPAddressVersion = NetworkIPVersion.IPv4,
PublicIPAllocationMethod = NetworkIPAllocationMethod.Static,
DnsSettings = new PublicIPAddressDnsSettings()
{
DomainNameLabel = publicIPAddressName
}
};
var publicIPAddressLro = await publicAddressIPCollection.CreateOrUpdateAsync(WaitUntil.Completed, publicIPAddressName, publicIPAddressdata);
var publicIPAddress = publicIPAddressLro.Value;
Utilities.Log("Creating a Public IP address with name : " + publicIPAddress.Data.Name);
// ============================================================
// Create a virtual machine and associate the public IP address
// Create a virtual network
Utilities.Log("Creating a virtual network2...");
var virtualNetworkName2 = Utilities.CreateRandomName("VirtualNetwork2_");
var virtualNetworkCollection2 = resourceGroup.GetVirtualNetworks();
var data2 = new VirtualNetworkData()
{
Location = AzureLocation.EastUS,
AddressPrefixes =
{
new string("10.1.0.0/28"),
},
};
var virtualNetworkLro2 = await virtualNetworkCollection2.CreateOrUpdateAsync(WaitUntil.Completed, virtualNetworkName2, data2);
var virtualNetwork2 = virtualNetworkLro2.Value;
Utilities.Log("Created a virtual network2 with name : " + virtualNetwork2.Data.Name);
//Create a subnet
Utilities.Log("Creating a subnet2...");
var subnetName2 = Utilities.CreateRandomName("subnet2_");
var subnetData2 = new SubnetData()
{
ServiceEndpoints =
{
new ServiceEndpointProperties()
{
Service = "Microsoft.Storage"
}
},
Name = subnetName2,
AddressPrefix = "10.1.0.0/28",
};
var subnetLRro2 = await virtualNetwork2.GetSubnets().CreateOrUpdateAsync(WaitUntil.Completed, subnetName2, subnetData2);
var subnet2 = subnetLRro2.Value;
Utilities.Log("Created a subnet2 with name : " + subnet2.Data.Name);
//Create a networkInterface
Utilities.Log("Created a networkInterface");
var networkInterfaceData = new NetworkInterfaceData()
{
Location = AzureLocation.EastUS,
IPConfigurations =
{
new NetworkInterfaceIPConfigurationData()
{
Name = "internal",
Primary = true,
Subnet = new SubnetData
{
Name = subnetName2,
Id = new ResourceIdentifier($"{virtualNetwork2.Data.Id}/subnets/{subnetName2}")
},
PrivateIPAllocationMethod = NetworkIPAllocationMethod.Dynamic,
PublicIPAddress = publicIPAddress.Data,
}
}
};
var networkInterfaceName = Utilities.CreateRandomName("networkInterface");
var nic = (await resourceGroup.GetNetworkInterfaces().CreateOrUpdateAsync(WaitUntil.Completed, networkInterfaceName, networkInterfaceData)).Value;
Utilities.Log("Created a network interface with name : " + nic.Data.Name);
//Create a VM with the Public IP address
Utilities.Log("Creating a VM with the Public IP address...");
var virtualMachineCollection = resourceGroup.GetVirtualMachines();
var vmName = Utilities.CreateRandomName("vm");
var adminUsername = Utilities.CreateRandomName("admin");
var adminPassword = Utilities.CreateRandomName("Password");
var computerName = Utilities.CreateRandomName("computer");
var virtualMachinedata = new VirtualMachineData(AzureLocation.EastUS)
{
HardwareProfile = new VirtualMachineHardwareProfile()
{
VmSize = VirtualMachineSizeType.StandardD4V2
},
OSProfile = new VirtualMachineOSProfile()
{
AdminUsername = adminUsername,
AdminPassword = adminPassword,
ComputerName = computerName,
},
NetworkProfile = new VirtualMachineNetworkProfile()
{
NetworkInterfaces =
{
new VirtualMachineNetworkInterfaceReference()
{
Id = nic.Id,
Primary = true,
}
}
},
StorageProfile = new VirtualMachineStorageProfile()
{
OSDisk = new VirtualMachineOSDisk(DiskCreateOptionType.FromImage)
{
OSType = SupportedOperatingSystemType.Linux,
Caching = CachingType.ReadWrite,
ManagedDisk = new VirtualMachineManagedDisk()
{
StorageAccountType = StorageAccountType.StandardLrs
}
},
ImageReference = new ImageReference()
{
Publisher = "Canonical",
Offer = "UbuntuServer",
Sku = "16.04-LTS",
Version = "latest",
}
},
};
var virtualMachineLro = await virtualMachineCollection.CreateOrUpdateAsync(WaitUntil.Completed, vmName, virtualMachinedata);
var linuxVM = virtualMachineLro.Value;
Utilities.Log($"Created the VM with Id : " + linuxVM.Data.Id);
// ============================================================
// Update the storage account so that it can also be accessed from the PublicIP address
Utilities.Log($"Updating storage account with access also allowed from publicIP : {publicIPAddress.Data.IPAddress}");
var patch = new StorageAccountPatch()
{
PublicNetworkAccess = StoragePublicNetworkAccess.Enabled,
NetworkRuleSet = new StorageAccountNetworkRuleSet(StorageNetworkDefaultAction.Allow)
{
IPRules =
{
new StorageAccountIPRule(publicIPAddress.Data.IPAddress)
{
IPAddressOrRange = publicIPAddress.Data.IPAddress,
Action = StorageAccountNetworkRuleAction.Allow
}
}
}
};
_ =await storageAccount.UpdateAsync(patch);
Utilities.Log("Updated storage account");
// ============================================================
// Update the storage account to restrict incoming traffic to HTTPS
Utilities.Log($"Updating storage account to restrict incoming traffic to HTTPS");
var patch2 = new StorageAccountPatch()
{
EnableHttpsTrafficOnly = true,
};
_ = await storageAccount.UpdateAsync(patch2);
Utilities.Log("Updated storage account");
}
finally
{
try
{
if (_resourceGroupId is not null)
{
Utilities.Log($"Deleting Resource Group: {_resourceGroupId}");
await client.GetResourceGroupResource(_resourceGroupId).DeleteAsync(WaitUntil.Completed);
Utilities.Log($"Deleted Resource Group: {_resourceGroupId}");
}
}
catch (NullReferenceException)
{
Utilities.Log("Did not create any resources in Azure. No clean up is necessary");
}
catch (Exception g)
{
Utilities.Log(g);
}
}
}
public static async Task Main(string[] args)
{
try
{
var clientId = Environment.GetEnvironmentVariable("CLIENT_ID");
var clientSecret = Environment.GetEnvironmentVariable("CLIENT_SECRET");
var tenantId = Environment.GetEnvironmentVariable("TENANT_ID");
var subscription = Environment.GetEnvironmentVariable("SUBSCRIPTION_ID");
ClientSecretCredential credential = new ClientSecretCredential(tenantId, clientId, clientSecret);
ArmClient client = new ArmClient(credential, subscription);
await RunSample(client);
}
catch (Exception e)
{
Utilities.Log(e);
}
}
}
}