...
1function BuildServiceDirectoryPrefix([string]$serviceName) {
2 $serviceName = $serviceName -replace '[\./\\]', '_'
3 return $serviceName.ToUpperInvariant() + "_"
4}
5
6# If the ServiceDirectory has multiple segments use the last directory name
7# e.g. D:\foo\bar -> bar or foo/bar -> bar
8function GetServiceLeafDirectoryName([string]$serviceDirectory) {
9 return $serviceDirectory ? (Split-Path -Leaf $serviceDirectory) : ""
10}
11
12function GetUserName() {
13 $UserName = $env:USER ?? $env:USERNAME
14 # Remove spaces, etc. that may be in $UserName
15 $UserName = $UserName -replace '\W'
16 return $UserName
17}
18
19function GetBaseName([string]$user, [string]$serviceDirectoryName) {
20 # Handle service directories in nested directories, e.g. `data/aztables`
21 $serviceDirectorySafeName = $serviceDirectoryName -replace '[\./\\]', ''
22 return "$user$serviceDirectorySafeName".ToLowerInvariant()
23}
24
25function ShouldMarkValueAsSecret([string]$serviceName, [string]$key, [string]$value, [array]$allowedValues = @())
26{
27 $logOutputNonSecret = @(
28 # Environment Variables
29 "RESOURCEGROUP_NAME",
30 # Deployment Outputs
31 "CLIENT_ID",
32 "TENANT_ID",
33 "SUBSCRIPTION_ID",
34 "RESOURCE_GROUP",
35 "LOCATION",
36 "ENVIRONMENT",
37 "AUTHORITY_HOST",
38 "RESOURCE_MANAGER_URL",
39 "SERVICE_MANAGEMENT_URL",
40 "ENDPOINT_SUFFIX",
41 "SERVICE_DIRECTORY",
42 # This is used in many places and is harder to extract from the base subscription config, so hardcode it for now.
43 "STORAGE_ENDPOINT_SUFFIX",
44 # Parameters
45 "Environment",
46 "SubscriptionId",
47 "TenantId",
48 "TestApplicationId",
49 "TestApplicationOid",
50 "ProvisionerApplicationId"
51 )
52
53 $serviceDirectoryPrefix = BuildServiceDirectoryPrefix $serviceName
54
55 $suffix1 = $key -replace $serviceDirectoryPrefix, ""
56 $suffix2 = $key -replace "AZURE_", ""
57 $variants = @($key, $suffix1, $suffix2)
58 if ($variants | Where-Object { $logOutputNonSecret -contains $_ }) {
59 return $false
60 }
61
62 if ($allowedValues -contains $value) {
63 return $false
64 }
65
66 return $true
67}
68
69function SetSubscriptionConfiguration([object]$subscriptionConfiguration)
70{
71 foreach($pair in $subscriptionConfiguration.GetEnumerator()) {
72 if ($pair.Value -is [Hashtable]) {
73 foreach($nestedPair in $pair.Value.GetEnumerator()) {
74 # Mark values as secret so we don't print json blobs containing secrets in the logs.
75 # Prepend underscore to the variable name, so we can still access the variable names via environment
76 # variables if they get set subsequently.
77 if (ShouldMarkValueAsSecret "AZURE_" $nestedPair.Name $nestedPair.Value) {
78 Write-Host "##vso[task.setvariable variable=_$($nestedPair.Name);issecret=true;]$($nestedPair.Value)"
79 }
80 }
81 } else {
82 if (ShouldMarkValueAsSecret "AZURE_" $pair.Name $pair.Value) {
83 Write-Host "##vso[task.setvariable variable=_$($pair.Name);issecret=true;]$($pair.Value)"
84 }
85 }
86 }
87
88 Write-Host ($subscriptionConfiguration | ConvertTo-Json)
89 $serialized = $subscriptionConfiguration | ConvertTo-Json -Compress
90 Write-Host "##vso[task.setvariable variable=SubscriptionConfiguration;]$serialized"
91}
92
93function UpdateSubscriptionConfiguration([object]$subscriptionConfigurationBase, [object]$subscriptionConfiguration)
94{
95 foreach ($pair in $subscriptionConfiguration.GetEnumerator()) {
96 if ($pair.Value -is [Hashtable]) {
97 if (!$subscriptionConfigurationBase.ContainsKey($pair.Name)) {
98 $subscriptionConfigurationBase[$pair.Name] = @{}
99 }
100 foreach($nestedPair in $pair.Value.GetEnumerator()) {
101 # Mark values as secret so we don't print json blobs containing secrets in the logs.
102 # Prepend underscore to the variable name, so we can still access the variable names via environment
103 # variables if they get set subsequently.
104 if (ShouldMarkValueAsSecret "AZURE_" $nestedPair.Name $nestedPair.Value) {
105 Write-Host "##vso[task.setvariable variable=_$($nestedPair.Name);issecret=true;]$($nestedPair.Value)"
106 }
107 $subscriptionConfigurationBase[$pair.Name][$nestedPair.Name] = $nestedPair.Value
108 }
109 } else {
110 if (ShouldMarkValueAsSecret "AZURE_" $pair.Name $pair.Value) {
111 Write-Host "##vso[task.setvariable variable=_$($pair.Name);issecret=true;]$($pair.Value)"
112 }
113 $subscriptionConfigurationBase[$pair.Name] = $pair.Value
114 }
115 }
116
117 $serialized = $subscriptionConfigurationBase | ConvertTo-Json -Compress
118 Write-Host ($subscriptionConfigurationBase | ConvertTo-Json)
119 Write-Host "##vso[task.setvariable variable=SubscriptionConfiguration;]$serialized"
120}
View as plain text