-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.psm1
209 lines (152 loc) · 7.82 KB
/
main.psm1
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
Import-Module ./util/util.psm1
Import-Module Az
## Create resource group
$ApiVersion = "2018-02-01"
function CreateResourceGroup($resourceGroupname, $location) {
Get-AzResourceGroup -Name $resourceGroupname -Location $location -ErrorVariable rgNotExist -ErrorAction SilentlyContinue
if ($rgNotExist)
{
$newResourceGroup = New-AzResourceGroup $resourceGroupname $location
return $newResourceGroup
}
else
{
Write-Host("Resource group exist.")
}
}
# Create storage account
function NewStorageAccount($storageName, $resourceGroupName, $location) {
$storageAccountExist = $null
try
{
$storageAccountExist = Get-AzStorageAccount -ResourceGroupName $resourceGroupName -AccountName $storageName -ErrorAction Ignore
}
catch
{
$storageAccountExist = $null
}
if ($null -eq $storageAccountExist)
{
$storageAccount = New-AzStorageAccount -ResourceGroupName $resourceGroupName -AccountName $storageName -Location $location -SkuName Standard_LRS
return $storageAccount
}
else
{
Write-Host("Storage account exists")
}
}
function CreateEventSubscriptionEventHook($resourcegroup, $functionName, $subscriptionTitle, $functionCodeName, $resourceId) {
## ForceAppSettingsAzureWebJobsSecretStorageType $resourcegroup $functionName
$token = GetAccessToken
$azFuncAccessToken = Invoke-WebRequest "https://$functionName.scm.azurewebsites.net/api/functions/admin/masterkey" -Headers @{"Authorization"="Bearer $token"}
Write-Host($azFuncAccessToken.masterKey)
$subcriptionStatus = New-AzEventGridSubscription -EventSubscriptionName $subscriptionTitle -ResourceId "$resourceId" -endpoint "https://$functionName.azurewebsites.net/runtime/webhooks/EventGrid?functionName=$functionCodeName&code=$azFuncAccessToken" -EndpointType webhook -IncludedEventType Microsoft.Storage.BlobCreated
}
## Get token - the same as az account token command ##
function GetAccessToken() {
$currentAzureContext = Get-AzContext
$azureRmProfile = [Microsoft.Azure.Commands.Common.Authentication.Abstractions.AzureRmProfileProvider]::Instance.Profile;
$profileClient = New-Object Microsoft.Azure.Commands.ResourceManager.Common.RMProfileClient($azureRmProfile);
$token = $profileClient.AcquireAccessToken($currentAzureContext.Subscription.TenantId).AccessToken;
return $token
}
function CreateServicePlan($servicePlanName, $resourceGroup, $location) {
$svplanExist = Get-AzAppServicePlan -resourcegroupname $resourceGroup -name $servicePlanName
if ($null -eq $svplanExist)
{
$svplan = New-AzAppServicePlan -ResourceGroupName $resourceGroup -Name $servicePlanName -Location $location -Tier "Basic"
return $svplan
}
}
#####################################################################################
# place holder for code, might have to spit out app service plan codes.
#####################################################################################
function CreateFunctionApp($resourceGroupName, $location, $functionAppName, $storageAccountName) {
$rg = CreateResourceGroup $resourceGroupName $location
$svcPlan = CreateServicePlan "DefaultServicePlan" $resourceGroupName $location
$storageacc = NewStorageAccount $storageAccountName $resourceGroupName $location
$FunctionAppSettings = @{
alwaysOn=$True;
}
$webresource = Get-AzResource -name $functionAppName -ResourceGroupName $resourceGroupName -ResourceType "microsoft.web/sites"
if ($null -eq $webresource) {
# Provision the function app service
New-AzResource -ResourceGroupName $rg.ResourceGroupName -Location $rg.Location -ResourceName $functionAppName -ResourceType "microsoft.web/sites" -Kind "functionapp" -Properties $FunctionAppSettings -Force
Write-Host("Appsettings")
Write-Host($AzFunctionAppSettings)
## Set the correct application settings on the function app
Set-AzWebApp -Name $functionAppName -ResourceGroupName $rg.ResourceGroupName -AppSettings @{
#APPINSIGHTS_INSTRUMENTATIONKEY = $AppInsightsKey;
AzureWebJobsDashboard = $storageacc.Context.ConnectionString;
AzureWebJobsStorage = $storageacc.Context.ConnectionString;
FUNCTIONS_EXTENSION_VERSION = "~2";
FUNCTIONS_WORKER_RUNTIME = "dotnet";
}
}
else
{
Write-Host("App function name already exist")
}
}
## Get publishing profile and deploy application to scm zipdeploy ##
function DeployAppFunction($functionAppName, $resourceGroup, $filePath) {
$PublishingProfile = [xml](Get-AzWebAppPublishingProfile -ResourceGroupName $resourceGroup -Name $functionAppName)
$Username = (Select-Xml -Xml $PublishingProfile -XPath "//publishData/publishProfile[contains(@profileName,'Web Deploy')]/@userName").Node.Value
$Password = (Select-Xml -Xml $PublishingProfile -XPath "//publishData/publishProfile[contains(@profileName,'Web Deploy')]/@userPWD").Node.Value
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $Username,$Password)))
$apiUrl = "https://$functionAppName.scm.azurewebsites.net/api/zipdeploy";
Invoke-RestMethod -Uri $apiUrl -InFile $filePath -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} -Method Post -ContentType "multipart/form-date";
}
function SecureFunctionApp($resourceGroupName, $functionAppName, $targetOrigin) {
## disable remote debugging
## disable ftp
$targetResource = Get-AzResource -ResourceGroupName $resourceGroupName -ResourceType Microsoft.Web/sites/config -ResourceName $functionAppName -ApiVersion $ApiVersion
if ($null -ne $targetResource)
{
## disable remote debugging
$targetResource.Properties.remoteDebuggingEnabled = "False"
$targetResource.Properties.ftpsState = "Disabled"
$targetResource.Properties.cors = @{allowedOrigins = @("$targetOrigin")}
## disable cors ##
$targetResource | Set-AzResource -ApiVersion $ApiVersion -Force
}
}
function SecureCors($resourceGroupName, $functionAppName, $allowOrigin) {
## disable remote debugging
## disable ftp
$targetResource = Get-AzResource -ResourceGroupName $resourceGroupName -ResourceType Microsoft.Web/sites/config -ResourceName $functionAppName -ApiVersion $ApiVersion
if ($null -ne $targetResource)
{
## disable remote debugging
$targetResource.Properties.cors = @{allowedOrigins = @($allowOrigin)}
## disable cors ##
$targetResource | Set-AzResource -ApiVersion $ApiVersion -Force
}
}
function GetFunctionAppInfo($functionAppName, $resourceGroupName) {
## disable remote debugging
## disable ftp
$targetResource = Get-AzResource -ResourceGroupName $resourceGroupName -ResourceType Microsoft.Web/sites/config -ResourceName $functionAppName -ApiVersion $ApiVersion
if ($null -ne $targetResource)
{
Write-Host($targetResource)
}
else
{
Write-Host("No matching resources found.")
}
}
function SecureNetwork($resourceGroupName, $functionAppName) {
# Apply ip restriction
}
function SetAppSetting($functionAppName, $resourceGroupName, [hashtable] $functionAppSettings) {
## Adding key settings to app config
$functionAppSettings.add("AzureWebJobsSecretStorageType", "Files")
$setWebAppParams = @{
Name = $functionAppName
ResourceGroupName = $resourceGroupName
AppSettings = $functionAppSettings
}
$webApp = Set-AzWebApp @setWebAppParams
}
Export-ModuleMember -Function GetFunctionAppInfo, SecureCors, SecureFunctionApp, CreateResourceGroup, GetAccessToken, CreateFunctionApp, CreateServicePlan, DeployAppFunction, SetAppSetting, CreateEventSubscriptionEventHook, ApplySecurityPolicy