This repository has been archived by the owner on Jan 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathConfigureTestLocalSettings.ps1
165 lines (137 loc) · 5.69 KB
/
ConfigureTestLocalSettings.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
param(
[Parameter(Mandatory=$True)][string]$ResourceGroupName,
[Parameter(Mandatory=$True)][string]$Name,
[Parameter(Mandatory=$True)][string]$SourceRootDir)
$ErrorActionPreference = "Stop"
function ConvertSecretToPlainText($Secret) {
$bstr = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($Secret)
return [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($bstr)
}
$documentDbUrl = "https://$Name.documents.azure.com/"
$resource = Get-AzResource `
-ResourceType "Microsoft.DocumentDb/databaseAccounts" `
-ResourceGroupName $ResourceGroupName `
-ResourceName $Name `
-ApiVersion 2020-04-01
$primaryMasterKey = (Invoke-AzResourceAction `
-Action listKeys `
-ResourceId $resource.ResourceId `
-ApiVersion 2020-04-01 `
-Force).primaryMasterKey
$resource = Get-AzResource `
-ResourceType "Microsoft.Search/searchServices" `
-ResourceGroupName $ResourceGroupName `
-ResourceName $Name `
-ApiVersion 2020-04-01
# Get the primary admin API key for search.
$primaryKey = (Invoke-AzResourceAction `
-Action listAdminKeys `
-ResourceId $resource.ResourceId `
-ApiVersion 2020-08-01 `
-Force).PrimaryKey
# Create a test specific storage account
Write-Host "Creating a test specific storage account"
$StackName = ($Name + $env:Build_BuildNumber).Replace(".", "")
Write-Host "##vso[task.setvariable variable=StackName;isOutput=true]$StackName"
$TestStorageName = $StackName + "test"
New-AzStorageAccount -ResourceGroupName $ResourceGroupName `
-Name $TestStorageName `
-Location (Get-AzResourceGroup -Name $ResourceGroupName).Location `
-SkuName Standard_LRS `
-Kind StorageV2 `
-Tag @{ "stackName" = $StackName; }
# Get storage key.
$accountKey = (Get-AzStorageAccountKey -ResourceGroupName $ResourceGroupName -Name $TestStorageName).Value[0]
$connectionString = "DefaultEndpointsProtocol=https;AccountName=$TestStorageName;AccountKey=$accountKey;EndpointSuffix=core.windows.net"
$outFile = "$SourceRootDir\Eklee.Azure.Functions.GraphQl.Tests\local.settings.json"
$settings = @{ Search = @{ ServiceName="$Name"; ApiKey="$primaryKey" }; DocumentDb = @{ Key="$primaryMasterKey";Url="$documentDbUrl";RequestUnits="400" }; TableStorage=@{ConnectionString="$connectionString"} } | ConvertTo-Json -Depth 10
$settings | Out-File $outFile -Encoding ASCII
Write-Host "Configuration file: $outFile"
$values = @( @{ "key" = "endpoint"; "value" = "http://localhost:7071"; "enabled" = "true" } )
$environmentFile = @{ "id"="d9a0b2d1-5c39-4671-83fb-e9a1f7f404a1"; "name" = "Eklee.Azure.Functions.Http.Local"; "values" = $values }
Get-AzKeyVaultSecret -VaultName $Name| ForEach-Object {
$keyName = $_.Name
if ($keyName.StartsWith("postman-")) {
$keyVaultKeyName = $keyName.Replace("postman-","")
Write-Host "Processing $keyVaultKeyName"
$secret = (Get-AzKeyVaultSecret -VaultName $Name -Name $keyName)
if (!$secret){
Write-Host "Unable to find $keyVaultKeyName in $Name"
} else {
$text = ConvertSecretToPlainText -Secret $secret.SecretValue
$environmentFile.values += @{ "key" = $keyVaultKeyName; "value" = $text; "enabled" = "true" }
}
}
}
$environmentFile | ConvertTo-Json | Out-File $SourceRootDir\Tests\Eklee.Azure.Functions.GraphQl.Local.postman_environment.json -Encoding ASCII
$localSettingsFileContent = '{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "%StorageConnection%",
"FUNCTIONS_WORKER_RUNTIME": "dotnet"
},
"GraphQl": {
"EnableMetrics": "true",
"ExposeExceptions": "true"
},
"DocumentDb": {
"Url": "%DocumentDbUrl%",
"Key": "%DocumentDbKey%",
"RequestUnits": "400"
},
"Search": {
"ServiceName": "%SearchServiceName%",
"ApiKey": "%SearchServiceKey%"
},
"TableStorage": {
"ConnectionString": "%StorageConnection%"
},
"Security": {
"Audience": "%audienceId%",
"Issuers": "%issuer1% %issuer2%"
},
"Tenants": [
{
"Issuer": "%issuer1%",
"DocumentDb": {
"Key": "%DocumentDbKey%",
"Url": "%DocumentDbUrl%",
"RequestUnits": "400"
},
"Search": {
"ServiceName": "%SearchServiceName%",
"ApiKey": "%SearchServiceKey%"
},
"TableStorage": {
"ConnectionString": "%StorageConnection%"
}
},
{
"Issuer": "%issuer2%",
"DocumentDb": {
"Key": "%DocumentDbKey%",
"Url": "%DocumentDbUrl%",
"RequestUnits": "400"
},
"Search": {
"ServiceName": "%SearchServiceName%",
"ApiKey": "%SearchServiceKey%"
},
"TableStorage": {
"ConnectionString": "%StorageConnection%"
}
}
]
}'
$audienceId = ConvertSecretToPlainText -Secret (Get-AzKeyVaultSecret -VaultName $Name -Name "local-audienceId").SecretValue
$issuer1 = ConvertSecretToPlainText -Secret (Get-AzKeyVaultSecret -VaultName $Name -Name "local-issuer1").SecretValue
$issuer2 = ConvertSecretToPlainText -Secret (Get-AzKeyVaultSecret -VaultName $Name -Name "local-issuer2").SecretValue
$localSettingsFileContent = $localSettingsFileContent.Replace("%audienceId%", $audienceId)
$localSettingsFileContent = $localSettingsFileContent.Replace("%issuer1%", $issuer1)
$localSettingsFileContent = $localSettingsFileContent.Replace("%issuer2%", $issuer2)
$localSettingsFileContent = $localSettingsFileContent.Replace("%DocumentDbKey%", $primaryMasterKey)
$localSettingsFileContent = $localSettingsFileContent.Replace("%DocumentDbUrl%", $documentDbUrl )
$localSettingsFileContent = $localSettingsFileContent.Replace("%SearchServiceName%", $Name)
$localSettingsFileContent = $localSettingsFileContent.Replace("%SearchServiceKey%", $primaryKey)
$localSettingsFileContent = $localSettingsFileContent.Replace("%StorageConnection%", $connectionString)
$localSettingsFileContent | Out-File $SourceRootDir\Examples\Eklee.Azure.Functions.GraphQl.Example\local.settings.json -Encoding ASCII