Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge subscription configuration file values with the supplied subscription config #8422

Merged
merged 5 commits into from
Jun 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 21 additions & 25 deletions eng/common/TestResources/SubConfig-Helpers.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ function ShouldMarkValueAsSecret([string]$serviceName, [string]$key, [string]$va

function SetSubscriptionConfiguration([object]$subscriptionConfiguration)
{
foreach($pair in $subscriptionConfiguration.GetEnumerator()) {
foreach ($pair in $subscriptionConfiguration.GetEnumerator()) {
if ($pair.Value -is [Hashtable]) {
foreach($nestedPair in $pair.Value.GetEnumerator()) {
# Mark values as secret so we don't print json blobs containing secrets in the logs.
Expand All @@ -126,36 +126,32 @@ function SetSubscriptionConfiguration([object]$subscriptionConfiguration)
}
}

Write-Host ($subscriptionConfiguration | ConvertTo-Json)
$serialized = $subscriptionConfiguration | ConvertTo-Json -Compress
Write-Host "##vso[task.setvariable variable=SubscriptionConfiguration;]$serialized"
return $subscriptionConfiguration
}

function UpdateSubscriptionConfiguration([object]$subscriptionConfigurationBase, [object]$subscriptionConfiguration)
{
foreach ($pair in $subscriptionConfiguration.GetEnumerator()) {
if ($pair.Value -is [Hashtable]) {
if (!$subscriptionConfigurationBase.ContainsKey($pair.Name)) {
$subscriptionConfigurationBase[$pair.Name] = @{}
}
foreach($nestedPair in $pair.Value.GetEnumerator()) {
# Mark values as secret so we don't print json blobs containing secrets in the logs.
# Prepend underscore to the variable name, so we can still access the variable names via environment
# variables if they get set subsequently.
if (ShouldMarkValueAsSecret "AZURE_" $nestedPair.Name $nestedPair.Value) {
Write-Host "##vso[task.setvariable variable=_$($nestedPair.Name);issecret=true;]$($nestedPair.Value)"
}
$subscriptionConfigurationBase[$pair.Name][$nestedPair.Name] = $nestedPair.Value
}
} else {
if (ShouldMarkValueAsSecret "AZURE_" $pair.Name $pair.Value) {
Write-Host "##vso[task.setvariable variable=_$($pair.Name);issecret=true;]$($pair.Value)"
foreach ($pair in $subscriptionConfiguration.GetEnumerator()) {
if ($pair.Value -is [Hashtable]) {
if (!$subscriptionConfigurationBase.ContainsKey($pair.Name)) {
$subscriptionConfigurationBase[$pair.Name] = @{}
}
foreach($nestedPair in $pair.Value.GetEnumerator()) {
# Mark values as secret so we don't print json blobs containing secrets in the logs.
# Prepend underscore to the variable name, so we can still access the variable names via environment
# variables if they get set subsequently.
if (ShouldMarkValueAsSecret "AZURE_" $nestedPair.Name $nestedPair.Value) {
Write-Host "##vso[task.setvariable variable=_$($nestedPair.Name);issecret=true;]$($nestedPair.Value)"
}
$subscriptionConfigurationBase[$pair.Name] = $pair.Value
$subscriptionConfigurationBase[$pair.Name][$nestedPair.Name] = $nestedPair.Value
}
} else {
if (ShouldMarkValueAsSecret "AZURE_" $pair.Name $pair.Value) {
Write-Host "##vso[task.setvariable variable=_$($pair.Name);issecret=true;]$($pair.Value)"
}
$subscriptionConfigurationBase[$pair.Name] = $pair.Value
}
}

$serialized = $subscriptionConfigurationBase | ConvertTo-Json -Compress
Write-Host ($subscriptionConfigurationBase | ConvertTo-Json)
Write-Host "##vso[task.setvariable variable=SubscriptionConfiguration;]$serialized"
return $subscriptionConfigurationBase
}
80 changes: 47 additions & 33 deletions eng/common/TestResources/build-test-resource-config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,38 +9,52 @@ parameters:
- name: EnvVars
type: object
default: null
- name: SubscriptionConfigurationFilePaths
type: object
default: null

steps:
- ${{ if parameters.SubscriptionConfiguration }}:
- pwsh: |
$config = @'
${{ parameters.SubscriptionConfiguration }}
'@ | ConvertFrom-Json -AsHashtable

. ./eng/common/TestResources/SubConfig-Helpers.ps1
SetSubscriptionConfiguration $config
displayName: Initialize SubscriptionConfiguration variable
${{ if parameters.EnvVars }}:
env: ${{ parameters.EnvVars }}

- ${{ if parameters.SubscriptionConfigurations }}:
- pwsh: |
Write-Host "##vso[task.setvariable variable=SubscriptionConfiguration;]{}"
displayName: Initialize SubscriptionConfiguration variable for merging
condition: eq(variables['SubscriptionConfiguration'], '')

- ${{ each config in parameters.SubscriptionConfigurations }}:
- pwsh: |
$configBase = @'
$(SubscriptionConfiguration)
'@ | ConvertFrom-Json -AsHashtable
$config = @'
${{ config }}
'@ | ConvertFrom-Json -AsHashtable

. ./eng/common/TestResources/SubConfig-Helpers.ps1
UpdateSubscriptionConfiguration $configBase $config

displayName: Merge Test Resource Configurations
${{ if parameters.EnvVars }}:
env: ${{ parameters.EnvVars }}
- pwsh: |
. ./eng/common/TestResources/SubConfig-Helpers.ps1

$finalConfig = @{}
$baseSubConfigRaw = @'
${{ parameters.SubscriptionConfiguration }}
'@.Trim()
if ($baseSubConfigRaw) {
$baseSubConfig = $baseSubConfigRaw | ConvertFrom-Json -AsHashtable

Write-Host "Setting base sub config"
$finalConfig = SetSubscriptionConfiguration $baseSubConfig
}

$subConfigJsonsRaw = @'
${{ convertToJson(parameters.SubscriptionConfigurations) }}
'@.Trim() -replace '"{', '{' -replace '}"', '}'

if ($subConfigJsonsRaw) {
$subConfigs = $subConfigJsonsRaw | ConvertFrom-Json -AsHashtable

foreach ($subConfig in $subConfigs) {
Write-Host "Merging sub config from list"
$finalConfig = UpdateSubscriptionConfiguration $finalConfig $subConfig
}
}

$subConfigFilesRaw = @'
${{ convertToJson(parameters.SubscriptionConfigurationFilePaths) }}
'@.Trim()

if ($subConfigFilesRaw) {
weshaggard marked this conversation as resolved.
Show resolved Hide resolved
$subConfigFiles = $subConfigFilesRaw | ConvertFrom-Json -AsHashtable
foreach ($file in $subConfigFiles) {
Write-Host "Merging sub config from file: $file"
$subConfig = Get-Content $file | ConvertFrom-Json -AsHashtable
$finalConfig = UpdateSubscriptionConfiguration $finalConfig $subConfig
}
}

Write-Host ($finalConfig | ConvertTo-Json)
weshaggard marked this conversation as resolved.
Show resolved Hide resolved
$serialized = $finalConfig | ConvertTo-Json -Compress
Write-Host "##vso[task.setvariable variable=SubscriptionConfiguration;]$serialized"
displayName: Merge subscription configurations
16 changes: 2 additions & 14 deletions eng/common/TestResources/deploy-test-resources.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ parameters:
ServiceConnection: not-specified
ResourceType: test
UseFederatedAuth: false
SubscriptionConfigurationFilePath: ''


# SubscriptionConfiguration will be splatted into the parameters of the test
# resources script. It should be JSON in the form:
Expand Down Expand Up @@ -56,18 +54,9 @@ steps:
ScriptType: InlineScript
Inline: |
eng/common/scripts/Import-AzModules.ps1

if ('${{ parameters.SubscriptionConfigurationFilePath }}' -ne '') {
$subscriptionConfiguration = `
Get-Content '${{ parameters.SubscriptionConfigurationFilePath }}' `
| ConvertFrom-Json -AsHashtable;
} else {
# Multiline string termination ('@) needs to be at the beginning
# of the line
$subscriptionConfiguration = @'
${{ parameters.SubscriptionConfiguration }}
$subscriptionConfiguration = @'
${{ parameters.SubscriptionConfiguration }}
'@ | ConvertFrom-Json -AsHashtable;
}

# The subscriptionConfiguration may have ArmTemplateParameters defined, so
# pass those in via the ArmTemplateParameters flag, and handle any
Expand All @@ -86,7 +75,6 @@ steps:
- ${{ else }}:
- pwsh: |
eng/common/scripts/Import-AzModules.ps1

$subscriptionConfiguration = @'
${{ parameters.SubscriptionConfiguration }}
'@ | ConvertFrom-Json -AsHashtable;
Expand Down
15 changes: 2 additions & 13 deletions eng/common/TestResources/remove-test-resources.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ parameters:
ResourceType: test
EnvVars: {}
UseFederatedAuth: false
SubscriptionConfigurationFilePath: ''

# SubscriptionConfiguration will be splat into the parameters of the test
# resources script. It should be JSON in the form:
Expand Down Expand Up @@ -39,20 +38,10 @@ steps:
Inline: |
eng/common/scripts/Import-AzModules.ps1


if ('${{ parameters.SubscriptionConfigurationFilePath }}' -ne '') {
$subscriptionConfiguration = `
Get-Content '${{ parameters.SubscriptionConfigurationFilePath }}' `
| ConvertFrom-Json -AsHashtable;
} else {
# Multiline string termination ("@) needs to be at the beginning
# of the line
$subscriptionConfiguration = @"
${{ parameters.SubscriptionConfiguration }}
$subscriptionConfiguration = @"
${{ parameters.SubscriptionConfiguration }}
"@ | ConvertFrom-Json -AsHashtable;

}

eng/common/TestResources/Remove-TestResources.ps1 `
@subscriptionConfiguration `
-ResourceType '${{ parameters.ResourceType }}' `
Expand Down
2 changes: 2 additions & 0 deletions eng/common/TestResources/sub-config/AzurePublicMsft.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
"SubscriptionId": "2cd617ea-1866-46b1-90e3-fffb087ebf9b",
"TenantId": "72f988bf-86f1-41af-91ab-2d7cd011db47",
"TestApplicationId": "f850650c-1fcf-4489-b46f-71af2e30d360",
"TestApplicationSecret": "",
danieljurek marked this conversation as resolved.
Show resolved Hide resolved
"TestApplicationOid": "30511c9d-ba1a-4c7b-b422-5b543da11b3f",
"ProvisionerApplicationId": "f850650c-1fcf-4489-b46f-71af2e30d360",
"ProvisionerApplicationSecret": "",
"ProvisionerApplicationOid": "30511c9d-ba1a-4c7b-b422-5b543da11b3f",
"Environment": "AzureCloud",
"AzureSubscription": "Azure SDK Test Resources"
Expand Down
Loading