Skip to content

Commit 3d032ef

Browse files
Sync eng/common directory with azure-sdk-tools for PR 8249 (Azure#29673)
Sync eng/common directory with azure-sdk-tools for PR Azure/azure-sdk-tools#8249 See [eng/common workflow](https://github.com/Azure/azure-sdk-tools/blob/main/eng/common/README.md#workflow) --------- Co-authored-by: Daniel Jurek <[email protected]>
1 parent 4cda395 commit 3d032ef

File tree

6 files changed

+223
-94
lines changed

6 files changed

+223
-94
lines changed

eng/common/TestResources/New-TestResources.ps1

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,9 @@ param (
9292
[Parameter()]
9393
[switch] $SuppressVsoCommands = ($null -eq $env:SYSTEM_TEAMPROJECTID),
9494

95+
# Default behavior is to use logged in credentials
9596
[Parameter()]
96-
[switch] $UserAuth,
97+
[switch] $ServicePrincipalAuth,
9798

9899
# Captures any arguments not declared here (no parameter errors)
99100
# This enables backwards compatibility with old script versions in
@@ -105,6 +106,13 @@ param (
105106

106107
. $PSScriptRoot/SubConfig-Helpers.ps1
107108

109+
if (!$ServicePrincipalAuth) {
110+
# Clear secrets if not using Service Principal auth. This prevents secrets
111+
# from being passed to pre- and post-scripts.
112+
$PSBoundParameters['TestApplicationSecret'] = $TestApplicationSecret = ''
113+
$PSBoundParameters['ProvisionerApplicationSecret'] = $ProvisionerApplicationSecret = ''
114+
}
115+
108116
# By default stop for any error.
109117
if (!$PSBoundParameters.ContainsKey('ErrorAction')) {
110118
$ErrorActionPreference = 'Stop'
@@ -267,9 +275,6 @@ function BuildDeploymentOutputs([string]$serviceName, [object]$azContext, [objec
267275
$serviceDirectoryPrefix = BuildServiceDirectoryPrefix $serviceName
268276
# Add default values
269277
$deploymentOutputs = [Ordered]@{
270-
"${serviceDirectoryPrefix}CLIENT_ID" = $TestApplicationId;
271-
"${serviceDirectoryPrefix}CLIENT_SECRET" = $TestApplicationSecret;
272-
"${serviceDirectoryPrefix}TENANT_ID" = $azContext.Tenant.Id;
273278
"${serviceDirectoryPrefix}SUBSCRIPTION_ID" = $azContext.Subscription.Id;
274279
"${serviceDirectoryPrefix}RESOURCE_GROUP" = $resourceGroup.ResourceGroupName;
275280
"${serviceDirectoryPrefix}LOCATION" = $resourceGroup.Location;
@@ -280,6 +285,12 @@ function BuildDeploymentOutputs([string]$serviceName, [object]$azContext, [objec
280285
"AZURE_SERVICE_DIRECTORY" = $serviceName.ToUpperInvariant();
281286
}
282287

288+
if ($ServicePrincipalAuth) {
289+
$deploymentOutputs["${serviceDirectoryPrefix}CLIENT_ID"] = $TestApplicationId;
290+
$deploymentOutputs["${serviceDirectoryPrefix}CLIENT_SECRET"] = $TestApplicationSecret;
291+
$deploymentOutputs["${serviceDirectoryPrefix}TENANT_ID"] = $azContext.Tenant.Id;
292+
}
293+
283294
MergeHashes $environmentVariables $(Get-Variable deploymentOutputs)
284295

285296
foreach ($key in $deployment.Outputs.Keys) {
@@ -518,8 +529,8 @@ try {
518529
}
519530
}
520531

521-
# If a provisioner service principal was provided, log into it to perform the pre- and post-scripts and deployments.
522-
if ($ProvisionerApplicationId) {
532+
# If a provisioner service principal was provided log into it to perform the pre- and post-scripts and deployments.
533+
if ($ProvisionerApplicationId -and $ServicePrincipalAuth) {
523534
$null = Disable-AzContextAutosave -Scope Process
524535

525536
Log "Logging into service principal '$ProvisionerApplicationId'."
@@ -614,9 +625,9 @@ try {
614625
}
615626
}
616627

617-
if ($UserAuth) {
628+
if (!$CI -and !$ServicePrincipalAuth) {
618629
if ($TestApplicationId) {
619-
Write-Warning "The specified TestApplicationId '$TestApplicationId' will be ignored when UserAuth is set."
630+
Write-Warning "The specified TestApplicationId '$TestApplicationId' will be ignored when -ServicePrincipalAutth is not set."
620631
}
621632

622633
$userAccount = (Get-AzADUser -UserPrincipalName (Get-AzContext).Account)
@@ -625,8 +636,8 @@ try {
625636
$userAccountName = $userAccount.UserPrincipalName
626637
Log "User authentication with user '$userAccountName' ('$TestApplicationId') will be used."
627638
}
628-
# If no test application ID was specified during an interactive session, create a new service principal.
629-
elseif (!$CI -and !$TestApplicationId) {
639+
# If user has specified -ServicePrincipalAuth
640+
elseif (!$CI -and $ServicePrincipalAuth) {
630641
# Cache the created service principal in this session for frequent reuse.
631642
$servicePrincipal = if ($AzureTestPrincipal -and (Get-AzADServicePrincipal -ApplicationId $AzureTestPrincipal.AppId) -and $AzureTestSubscription -eq $SubscriptionId) {
632643
Log "TestApplicationId was not specified; loading cached service principal '$($AzureTestPrincipal.AppId)'"
@@ -686,7 +697,9 @@ try {
686697
# Make sure pre- and post-scripts are passed formerly required arguments.
687698
$PSBoundParameters['TestApplicationId'] = $TestApplicationId
688699
$PSBoundParameters['TestApplicationOid'] = $TestApplicationOid
689-
$PSBoundParameters['TestApplicationSecret'] = $TestApplicationSecret
700+
if ($ServicePrincipalAuth) {
701+
$PSBoundParameters['TestApplicationSecret'] = $TestApplicationSecret
702+
}
690703

691704
# If the role hasn't been explicitly assigned to the resource group and a cached service principal or user authentication is in use,
692705
# query to see if the grant is needed.
@@ -704,7 +717,7 @@ try {
704717
# considered a critical failure, as the test application may have subscription-level permissions and not require
705718
# the explicit grant.
706719
if (!$resourceGroupRoleAssigned) {
707-
$idSlug = if ($userAuth) { "User '$userAccountName' ('$TestApplicationId')"} else { "Test Application '$TestApplicationId'"};
720+
$idSlug = if (!$ServicePrincipalAuth) { "User '$userAccountName' ('$TestApplicationId')" } else { "Test Application '$TestApplicationId'"};
708721
Log "Attempting to assign the 'Owner' role for '$ResourceGroupName' to the $idSlug"
709722
$ownerAssignment = New-AzRoleAssignment `
710723
-RoleDefinitionName "Owner" `
@@ -734,7 +747,7 @@ try {
734747
if ($TenantId) {
735748
$templateParameters.Add('tenantId', $TenantId)
736749
}
737-
if ($TestApplicationSecret) {
750+
if ($TestApplicationSecret -and $ServicePrincipalAuth) {
738751
$templateParameters.Add('testApplicationSecret', $TestApplicationSecret)
739752
}
740753

@@ -1016,19 +1029,16 @@ The environment file will be named for the test resources template that it was
10161029
generated for. For ARM templates, it will be test-resources.json.env. For
10171030
Bicep templates, test-resources.bicep.env.
10181031
1019-
.PARAMETER UserAuth
1020-
Create the resource group and deploy the template using the signed in user's credentials.
1021-
No service principal will be created or used.
1022-
1023-
The environment file will be named for the test resources template that it was
1024-
generated for. For ARM templates, it will be test-resources.json.env. For
1025-
Bicep templates, test-resources.bicep.env.
1026-
10271032
.PARAMETER SuppressVsoCommands
10281033
By default, the -CI parameter will print out secrets to logs with Azure Pipelines log
10291034
commands that cause them to be redacted. For CI environments that don't support this (like
10301035
stress test clusters), this flag can be set to $false to avoid printing out these secrets to the logs.
10311036
1037+
.PARAMETER ServicePrincipalAuth
1038+
Use the provisioner SP credentials to deploy, and pass the test SP credentials
1039+
to tests. If provisioner and test SP are not set, provision an SP with user
1040+
credentials and pass the new SP to tests.
1041+
10321042
.EXAMPLE
10331043
Connect-AzAccount -Subscription 'REPLACE_WITH_SUBSCRIPTION_ID'
10341044
New-TestResources.ps1 keyvault

eng/common/TestResources/New-TestResources.ps1.md

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ New-TestResources.ps1 [-BaseName <String>] [-ResourceGroupName <String>] [-Servi
1919
[-TestApplicationOid <String>] [-SubscriptionId <String>] [-DeleteAfterHours <Int32>] [-Location <String>]
2020
[-Environment <String>] [-ResourceType <String>] [-ArmTemplateParameters <Hashtable>]
2121
[-AdditionalParameters <Hashtable>] [-EnvironmentVariables <Hashtable>] [-CI] [-Force] [-OutFile]
22-
[-SuppressVsoCommands] [-UserAuth] [-NewTestResourcesRemainingArguments <Object>]
22+
[-SuppressVsoCommands] [-ServicePrincipalAuth] [-NewTestResourcesRemainingArguments <Object>]
2323
[-ProgressAction <ActionPreference>] [-WhatIf] [-Confirm] [<CommonParameters>]
2424
```
2525

@@ -32,7 +32,7 @@ New-TestResources.ps1 [-BaseName <String>] [-ResourceGroupName <String>] [-Servi
3232
-ProvisionerApplicationSecret <String> [-DeleteAfterHours <Int32>] [-Location <String>]
3333
[-Environment <String>] [-ResourceType <String>] [-ArmTemplateParameters <Hashtable>]
3434
[-AdditionalParameters <Hashtable>] [-EnvironmentVariables <Hashtable>] [-CI] [-Force] [-OutFile]
35-
[-SuppressVsoCommands] [-UserAuth] [-NewTestResourcesRemainingArguments <Object>]
35+
[-SuppressVsoCommands] [-ServicePrincipalAuth] [-NewTestResourcesRemainingArguments <Object>]
3636
[-ProgressAction <ActionPreference>] [-WhatIf] [-Confirm] [<CommonParameters>]
3737
```
3838

@@ -629,15 +629,11 @@ Accept pipeline input: False
629629
Accept wildcard characters: False
630630
```
631631
632-
### -UserAuth
633-
Create the resource group and deploy the template using the signed in user's credentials.
634-
No service principal will be created or used.
635-
636-
The environment file will be named for the test resources template that it was
637-
generated for.
638-
For ARM templates, it will be test-resources.json.env.
639-
For
640-
Bicep templates, test-resources.bicep.env.
632+
### -ServicePrincipalAuth
633+
Use the provisioner SP credentials to deploy, and pass the test SP credentials
634+
to tests.
635+
If provisioner and test SP are not set, provision an SP with user
636+
credentials and pass the new SP to tests.
641637
642638
```yaml
643639
Type: SwitchParameter

eng/common/TestResources/Remove-TestResources.ps1

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@ param (
5656
[ValidateSet('test', 'perf')]
5757
[string] $ResourceType = 'test',
5858

59+
[Parameter(ParameterSetName = 'Default+Provisioner')]
60+
[Parameter(ParameterSetName = 'ResourceGroup+Provisioner')]
61+
[Parameter()]
62+
[switch] $ServicePrincipalAuth,
63+
5964
[Parameter()]
6065
[switch] $Force,
6166

@@ -110,7 +115,7 @@ function Retry([scriptblock] $Action, [int] $Attempts = 5) {
110115
}
111116
}
112117

113-
if ($ProvisionerApplicationId) {
118+
if ($ProvisionerApplicationId -and $ServicePrincipalAuth) {
114119
$null = Disable-AzContextAutosave -Scope Process
115120

116121
Log "Logging into service principal '$ProvisionerApplicationId'"
@@ -305,6 +310,9 @@ Run script in CI mode. Infers various environment variable names based on CI con
305310
.PARAMETER Force
306311
Force removal of resource group without asking for user confirmation
307312
313+
.PARAMETER ServicePrincipalAuth
314+
Log in with provided Provisioner application credentials.
315+
308316
.EXAMPLE
309317
Remove-TestResources.ps1 keyvault -Force
310318
Use the currently logged-in account to delete the resources created for Key Vault testing.

eng/common/TestResources/Remove-TestResources.ps1.md

Lines changed: 71 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,32 +14,36 @@ Deletes the resource group deployed for a service directory from Azure.
1414

1515
### Default (Default)
1616
```
17-
Remove-TestResources.ps1 [-BaseName <String>] [-SubscriptionId <String>] [-ServiceDirectory] <String>
18-
[-Environment <String>] [-Force] [-RemoveTestResourcesRemainingArguments <Object>] [-WhatIf] [-Confirm]
17+
Remove-TestResources.ps1 [-BaseName <String>] [-SubscriptionId <String>] [[-ServiceDirectory] <String>]
18+
[-Environment <String>] [-ResourceType <String>] [-ServicePrincipalAuth] [-Force]
19+
[-RemoveTestResourcesRemainingArguments <Object>] [-ProgressAction <ActionPreference>] [-WhatIf] [-Confirm]
1920
[<CommonParameters>]
2021
```
2122

2223
### Default+Provisioner
2324
```
2425
Remove-TestResources.ps1 -BaseName <String> -TenantId <String> [-SubscriptionId <String>]
2526
-ProvisionerApplicationId <String> -ProvisionerApplicationSecret <String> [[-ServiceDirectory] <String>]
26-
[-Environment <String>] [-Force] [-RemoveTestResourcesRemainingArguments <Object>] [-WhatIf] [-Confirm]
27+
[-Environment <String>] [-ResourceType <String>] [-ServicePrincipalAuth] [-Force]
28+
[-RemoveTestResourcesRemainingArguments <Object>] [-ProgressAction <ActionPreference>] [-WhatIf] [-Confirm]
2729
[<CommonParameters>]
2830
```
2931

3032
### ResourceGroup+Provisioner
3133
```
32-
Remove-TestResources.ps1 -ResourceGroupName <String> -TenantId <String> [-SubscriptionId <String>]
34+
Remove-TestResources.ps1 [-ResourceGroupName <String>] -TenantId <String> [-SubscriptionId <String>]
3335
-ProvisionerApplicationId <String> -ProvisionerApplicationSecret <String> [[-ServiceDirectory] <String>]
34-
[-Environment <String>] [-CI] [-Force] [-RemoveTestResourcesRemainingArguments <Object>] [-WhatIf] [-Confirm]
36+
[-Environment <String>] [-CI] [-ResourceType <String>] [-ServicePrincipalAuth] [-Force]
37+
[-RemoveTestResourcesRemainingArguments <Object>] [-ProgressAction <ActionPreference>] [-WhatIf] [-Confirm]
3538
[<CommonParameters>]
3639
```
3740

3841
### ResourceGroup
3942
```
40-
Remove-TestResources.ps1 -ResourceGroupName <String> [-SubscriptionId <String>] [[-ServiceDirectory] <String>]
41-
[-Environment <String>] [-CI] [-Force] [-RemoveTestResourcesRemainingArguments <Object>] [-WhatIf] [-Confirm]
42-
[<CommonParameters>]
43+
Remove-TestResources.ps1 [-ResourceGroupName <String>] [-SubscriptionId <String>]
44+
[[-ServiceDirectory] <String>] [-Environment <String>] [-CI] [-ResourceType <String>] [-ServicePrincipalAuth]
45+
[-Force] [-RemoveTestResourcesRemainingArguments <Object>] [-ProgressAction <ActionPreference>] [-WhatIf]
46+
[-Confirm] [<CommonParameters>]
4347
```
4448

4549
## DESCRIPTION
@@ -112,7 +116,7 @@ Type: String
112116
Parameter Sets: ResourceGroup+Provisioner, ResourceGroup
113117
Aliases:
114118

115-
Required: True
119+
Required: False
116120
Position: Named
117121
Default value: None
118122
Accept pipeline input: False
@@ -193,32 +197,51 @@ specified - in which to discover pre removal script named 'remove-test-resources
193197
194198
```yaml
195199
Type: String
196-
Parameter Sets: Default
200+
Parameter Sets: (All)
197201
Aliases:
198202

199-
Required: True
203+
Required: False
200204
Position: 1
201205
Default value: None
202206
Accept pipeline input: False
203207
Accept wildcard characters: False
204208
```
205209
210+
### -Environment
211+
Name of the cloud environment.
212+
The default is the Azure Public Cloud
213+
('PublicCloud')
214+
206215
```yaml
207216
Type: String
208-
Parameter Sets: Default+Provisioner, ResourceGroup+Provisioner, ResourceGroup
217+
Parameter Sets: (All)
209218
Aliases:
210219

211220
Required: False
212-
Position: 1
213-
Default value: None
221+
Position: Named
222+
Default value: AzureCloud
214223
Accept pipeline input: False
215224
Accept wildcard characters: False
216225
```
217226
218-
### -Environment
219-
Name of the cloud environment.
220-
The default is the Azure Public Cloud
221-
('PublicCloud')
227+
### -CI
228+
Run script in CI mode.
229+
Infers various environment variable names based on CI convention.
230+
231+
```yaml
232+
Type: SwitchParameter
233+
Parameter Sets: ResourceGroup+Provisioner, ResourceGroup
234+
Aliases:
235+
236+
Required: False
237+
Position: Named
238+
Default value: False
239+
Accept pipeline input: False
240+
Accept wildcard characters: False
241+
```
242+
243+
### -ResourceType
244+
{{ Fill ResourceType Description }}
222245
223246
```yaml
224247
Type: String
@@ -227,13 +250,25 @@ Aliases:
227250

228251
Required: False
229252
Position: Named
230-
Default value: AzureCloud
253+
Default value: Test
231254
Accept pipeline input: False
232255
Accept wildcard characters: False
233256
```
234257
235-
### -CI
236-
Run script in CI mode. Infers various environment variable names based on CI convention.
258+
### -ServicePrincipalAuth
259+
Log in with provided Provisioner application credentials.
260+
261+
```yaml
262+
Type: SwitchParameter
263+
Parameter Sets: (All)
264+
Aliases:
265+
266+
Required: False
267+
Position: Named
268+
Default value: False
269+
Accept pipeline input: False
270+
Accept wildcard characters: False
271+
```
237272
238273
### -Force
239274
Force removal of resource group without asking for user confirmation
@@ -296,6 +331,21 @@ Accept pipeline input: False
296331
Accept wildcard characters: False
297332
```
298333
334+
### -ProgressAction
335+
{{ Fill ProgressAction Description }}
336+
337+
```yaml
338+
Type: ActionPreference
339+
Parameter Sets: (All)
340+
Aliases: proga
341+
342+
Required: False
343+
Position: Named
344+
Default value: None
345+
Accept pipeline input: False
346+
Accept wildcard characters: False
347+
```
348+
299349
### CommonParameters
300350
This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see [about_CommonParameters](https://go.microsoft.com/fwlink/?LinkID=113216).
301351

0 commit comments

Comments
 (0)