-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathAA-SecureStorageAccounts.ps1
147 lines (140 loc) · 7.67 KB
/
AA-SecureStorageAccounts.ps1
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
#
# Importing modules...
#
Import-Module Az.Accounts
Import-Module Az.Resources
Import-Module Az.Compute
Import-Module Az.Automation
#
# Connnection Phase
#
$connectionName = "AzureRunAsConnection"
try
{
$servicePrincipalConnection = Get-AutomationConnection -Name $connectionName
Connect-AzAccount `
-ServicePrincipal `
-Subscription 'subscriptionname.here' `
-TenantId $servicePrincipalConnection.TenantId `
-ApplicationId $servicePrincipalConnection.ApplicationId `
-CertificateThumbprint $servicePrincipalConnection.CertificateThumbprint
}
catch {
if (!$servicePrincipalConnection)
{
$ErrorMessage = "Connection $connectionName not found."
throw $ErrorMessage
} else{
Write-Error -Message $_.Exception
throw $_.Exception
}
}
#
# Functions
#
Function Msgbox($caption,$message,$type,$MaxSize){
if ($MaxSize -eq $null) { $MaxSize = 95}
$sCaption = $caption.Length
$sMessage = $Message.Length
If (($sCaption + $sMessage) -ge $MaxSize) {
$MaxSize = ($sCaption + $sMessage) + 20
}
$vDynamicSpace = $MaxSize - ($sCaption + $sMessage)
$vDynamicSpace = " " * $vDynamicSpace
$vText = $caption + $message + $vDynamicSpace + "["
if ($type -eq '0') {
$vText += " OKAY "
}Elseif ($type -eq '1'){
$vText += " WARNING "
}Else{
$vText += " ERROR "
}
$vText += "]"
Write-Output $vText
}
#
# Script Body
#
#Loading JSON File from the Storage Account container designated for this script
$vResourceGroupname = "ResourceGroupName"
$vAutomationAccountName = "svc-azdev-automation"
$vContainerName = "storageaccount-security-automated"
#I'm retrieving the storage account name from a Azure Automation variable. You should add that or specify in the code.
$vaStorageAccount = Get-AzAutomationVariable $vAutomationAccountName -Name "StorageAccount" -ResourceGroupName $vResourceGroupname
$StartTime = Get-Date
$EndTime = $startTime.AddHours(1.0)
$stgAccount = Get-AzStorageAccount -Name $vaStorageAccount.value -ResourceGroupName $vResourceGroupname
$SASToken = New-AzStorageAccountSASToken -Service Blob -ResourceType Container,Object -Permission "racwdlup" -startTime $StartTime -ExpiryTime $EndTime -Context $StgAccount.Context
$stgcontext = New-AzStorageContext -storageAccountName $stgAccount.StorageAccountName -SasToken $SASToken
$tmpBlobCopyOperation = Get-AzStorageBlobContent -Container $vContainerName -Blob ("PublicIPs.json") -Destination (Get-Location).path -Context $stgcontext
#Global Variables
$JSONPublicIPs = Get-Content -Raw -Path ((Get-Location).Path + "\PublicIPs.json") | ConvertFrom-Json
$vEndPoint = "Microsoft.Storage"
Set-Item Env:\SuppressAzurePowerShellBreakingChangeWarnings "true"
#Just remove the ResourceGroupName from the line below to get the entire subscription
$VNETs = Get-AzVirtualNetwork | where { ($_.Location -eq "canadacentral") -or ($_.location -eq "canadaeast")}
ForEach ($SingleVNET in $VNETs){
$Subnets = Get-AzVirtualNetwork -Name $SingleVNET.Name | Get-AzVirtualNetworkSubnetConfig
ForEach ($SingleSubnet in $Subnets){
$tmp = Set-AzVirtualNetworkSubnetConfig -Name $SingleSubnet.Name -VirtualNetwork $SingleVNET -AddressPrefix $SingleSubnet.AddressPrefix -ServiceEndpoint $vEndPoint
}
Msgbox "Updating Virtual Network:" $SingleVNET.Name 0
$tmp = $SingleVNET | Set-AzVirtualNetwork
}
# Storage Account Configuration
$StorageAccounts = Get-AzStorageAccount | Where-Object { (($_.Tags.Keys -notcontains "ms-resource-usage") -and ( ($_.Location -eq "canadacentral") -or ($_.Location -eq "canadaeast"))) -and ($_.StorageAccountName -ne 'stgAccountName') }
Msgbox "Stage 1/2: " "Enabling Firewall and specific IP addresses" 0
ForEach ($SingleStorageAccount in $StorageAccounts){
Msgbox "Updating Storage Account: " $SingleStorageAccount.StorageAccountName 0
$tmp = Update-AzStorageAccountNetworkRuleSet -ResourceGroupName $SingleStorageAccount.ResourceGroupName -Name $SingleStorageAccount.StorageAccountName -DefaultAction Deny
If ($tmp) {Msgbox "Storage Account (Default Action): " "Configured to Deny (required when using Virtual Network" 0}
$tmp = Update-AzStorageAccountNetworkRuleSet -ResourceGroupName $SingleStorageAccount.ResourceGroupName -Name $SingleStorageAccount.StorageAccountName -IPRule $JSONPublicIPs -ErrorVariable tmpErrorVar -ErrorAction SilentlyContinue
If ($tmp) {
Msgbox "Storage Account (Public IPs Action): " "All IPs from the PublicIPs.json were published" 0
} Else {
If ($tmpErrorVar) {
If ($tmpErrorVar[0].Exception.Message.Contains("networkAcls.virtualNetworkRules[*].id(unique)")) {
Msgbox "Storage Account (Public IPs Action): " "There are duplicated entries on the list. Check the JSON file" 2
}
If ($tmpErrorVar[0].Exception.Message.Contains("networkAcls.ipRule[*].value")) {
Msgbox "Storage Account (Public IPs Action):" "There are wrong IPs/Subnets in the JSON file." 2
}
}
}
#Cleaning up non-existent subnets
$NonExistentSubnets = (Get-AzStorageAccountNetworkRuleSet -ResourceGroupName $SingleStorageAccount.ResourceGroupName -Name $SingleStorageAccount.StorageAccountName).VirtualNetworkRules | Where-Object { $_.State -eq "NetworkSourceDeleted" }
If ($NonExistentSubnets){
ForEach ($SingleNonExistentSubnet in $NonExistentSubnets) {
$tmp = Remove-AzStorageAccountNetworkRule -ResourceGroupName $SingleStorageAccount.ResourceGroupName -Name $SingleStorageAccount.StorageAccountName -VirtualNetworkId $SingleNonExistentSubnet.VirtualNetworkResourceId
If ($tmp) {
Msgbox "Virtual network Rule (deleted subnet):" "Subnet marked as non-existent was removed." 0
}
}
}
}
Msgbox "Stage 2/2: " "Adding Virtual Networks within the Storage Account Firewall" 0
$StorageAccounts = $null
$SingleStorageAccount = $null
$SingleVNET = $null
$VNETs = $null
$StorageAccounts = Get-AzStorageAccount | Where-Object { (($_.Tags.Keys -notcontains "ms-resource-usage") -and ( ($_.Location -eq "canadacentral") -or ($_.Location -eq "canadaeast"))) -and ($_.StorageAccountName -ne 'StorageAccountName') }
$VNETs = Get-AzVirtualNetwork | where { ($_.Location -eq "canadacentral") -or ($_.location -eq "canadaeast")}
ForEach ($SingleStorageAccount in $StorageAccounts){
Msgbox "Virtual Network Updates on the following Storage Account: " $SingleStorageAccount.StorageAccountName 0
$tmpSTGRules = $null
$tmpStgRules = Get-AzStorageAccountNetworkRuleSet -ResourceGroupName $SingleStorageAccount.ResourceGroupName -Name $SingleStorageAccount.StorageAccountName
ForEach ($SingleVNET in $VNETs){
$Subnets = Get-AzVirtualNetwork -Name $SingleVNET.Name | Get-AzVirtualNetworkSubnetConfig
ForEach ($SingleSubnet in $Subnets){
If ($tmpSTGRules.VirtualNetworkRules.Count -ne 0) {
If ($tmpStgRules.virtualNetworkRules.VirtualNetworkResourceId.Contains($SingleSubnet.Id) -eq $False ) {
$tmpOperation = Add-AzStorageAccountNetworkRule -ResourceGroupName $SingleStorageAccount.ResourceGroupName -Name $SingleStorageAccount.StorageAccountName -VirtualNetworkResourceId $SingleSubnet.Id #-ErrorVariable tmpErrorVar -ErrorAction SilentlyContinue
}
} Else{
$tmpOperation = Add-AzStorageAccountNetworkRule -ResourceGroupName $SingleStorageAccount.ResourceGroupName -Name $SingleStorageAccount.StorageAccountName -VirtualNetworkResourceId $SingleSubnet.Id #-ErrorVariable tmpErrorVar -ErrorAction SilentlyContinue
}
}
}
start-sleep 15
}
Msgbox "Operation Complete: " "All Storage Accounts were updated." 0