forked from robotechredmond/Azure-PowerShell-Snippets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
storage-cmk-sample.ps1
103 lines (71 loc) · 2.67 KB
/
storage-cmk-sample.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
# Sign-in to Azure via Azure Resource Manager
Login-AzureRmAccount
# Select Azure Subscription
$subscriptionId =
( Get-AzureRmSubscription |
Out-GridView `
-Title "Select an Azure Subscription ..." `
-PassThru
).SubscriptionId
Set-AzureRmContext -Subscription $subscriptionId
# Create new Resource Group
$location = "canadacentral"
$rgName = "enter-resource-group-name"
$rg = New-AzureRmResourceGroup `
-Location $location `
-Name $rgName
# Create new Storage Account
$saName = "enter-storage-account-name"
$sa = New-AzureRmStorageAccount `
-Location $location `
-ResourceGroupName $rgName `
-Name $saName `
-SkuName Standard_LRS `
-Kind Storage `
-EnableEncryptionService Blob `
-EnableHttpsTrafficOnly $true
# Assign Azure AD Identity to Storage Account
Set-AzureRmStorageAccount -ResourceGroupName $rgName -Name $saName -AssignIdentity
$sa = Get-AzureRmStorageAccount -ResourceGroupName $rgName -Name $saName
# Create new Key Vault
$kvName = "enter-keyvault-name"
$kv = New-AzureRmKeyVault `
-Location $location `
-ResourceGroupName $rgName `
-VaultName $kvName `
-EnableSoftDelete `
-Sku Standard # "Standard" or "Premium" - Premium needed for HSM
# Disable Purge on KeyVault
$kvResource = Get-AzureRmResource -ResourceId $kv.ResourceId
$kvResource.Properties |
Add-Member `
-MemberType NoteProperty `
-Name "enablePurgeProtection" `
-Value $true
Set-AzureRmResource -ResourceId $kv.ResourceId `
-Properties $kvResource.Properties -Force
# Generate encryption key in Key Vault
$keyName = "enter-key-name"
$keyDestination = "Software" # "Software" or "HSM"
$Expires = (Get-Date).AddYears(2).ToUniversalTime()
$NotBefore = (Get-Date).ToUniversalTime()
$key = Add-AzureKeyVaultKey `
-VaultName $kv.VaultName `
-Name $keyName `
-Destination $keyDestination `
-Expires $Expires `
-NotBefore $NotBefore
# Assign access to Key Vault for Storage Account
Set-AzureRmKeyVaultAccessPolicy `
-VaultName $kv.VaultName `
-ObjectId $sa.Identity.PrincipalId `
-PermissionsToKeys wrapkey,unwrapkey,get
# Enable SSE Encryption on Storage Account with Customer-Managed Key
Set-AzureRmStorageAccount `
-ResourceGroupName $sa.ResourceGroupName `
-Name $sa.StorageAccountName `
-KeyvaultEncryption `
-KeyName $key.Name `
-KeyVersion $key.Version `
-KeyVaultUri $kv.VaultUri `
-Debug