Skip to content

Commit cd43c03

Browse files
danieljurekazure-sdk
authored andcommitted
Add support for Federated Auth to test resources scripts
1 parent ab90ef6 commit cd43c03

File tree

6 files changed

+206
-79
lines changed

6 files changed

+206
-79
lines changed

eng/common/TestResources/New-TestResources.ps1

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

95+
# Default behavior is to use -UserAuth
9596
[Parameter()]
96-
[switch] $UserAuth,
97+
[switch] $ServicePrincipalAuth,
98+
99+
[Parameter()]
100+
[switch] $FederatedAuth,
101+
97102

98103
# Captures any arguments not declared here (no parameter errors)
99104
# This enables backwards compatibility with old script versions in
@@ -105,6 +110,16 @@ param (
105110

106111
. $PSScriptRoot/SubConfig-Helpers.ps1
107112

113+
if ($FederatedAuth -and $ServicePrincipalAuth) {
114+
Write-Error "Only one of 'FederatedAuth' and 'ServicePrincipalAuth' can be set."
115+
exit 1
116+
}
117+
118+
$UserAuth = $true
119+
if ($ServicePrincipalAuth) {
120+
$UserAuth = $false
121+
}
122+
108123
# By default stop for any error.
109124
if (!$PSBoundParameters.ContainsKey('ErrorAction')) {
110125
$ErrorActionPreference = 'Stop'
@@ -268,7 +283,6 @@ function BuildDeploymentOutputs([string]$serviceName, [object]$azContext, [objec
268283
# Add default values
269284
$deploymentOutputs = [Ordered]@{
270285
"${serviceDirectoryPrefix}CLIENT_ID" = $TestApplicationId;
271-
"${serviceDirectoryPrefix}CLIENT_SECRET" = $TestApplicationSecret;
272286
"${serviceDirectoryPrefix}TENANT_ID" = $azContext.Tenant.Id;
273287
"${serviceDirectoryPrefix}SUBSCRIPTION_ID" = $azContext.Subscription.Id;
274288
"${serviceDirectoryPrefix}RESOURCE_GROUP" = $resourceGroup.ResourceGroupName;
@@ -280,6 +294,10 @@ function BuildDeploymentOutputs([string]$serviceName, [object]$azContext, [objec
280294
"AZURE_SERVICE_DIRECTORY" = $serviceName.ToUpperInvariant();
281295
}
282296

297+
if (!$FederatedAuth) {
298+
$deploymentOutputs["${serviceDirectoryPrefix}CLIENT_SECRET"] = $TestApplicationSecret;
299+
}
300+
283301
MergeHashes $environmentVariables $(Get-Variable deploymentOutputs)
284302

285303
foreach ($key in $deployment.Outputs.Keys) {
@@ -518,8 +536,9 @@ try {
518536
}
519537
}
520538

521-
# If a provisioner service principal was provided, log into it to perform the pre- and post-scripts and deployments.
522-
if ($ProvisionerApplicationId) {
539+
# If a provisioner service principal was provided (and not using Federated
540+
# Auth), log into it to perform the pre- and post-scripts and deployments.
541+
if ($ProvisionerApplicationId -and !$FederatedAuth) {
523542
$null = Disable-AzContextAutosave -Scope Process
524543

525544
Log "Logging into service principal '$ProvisionerApplicationId'."
@@ -614,7 +633,7 @@ try {
614633
}
615634
}
616635

617-
if ($UserAuth) {
636+
if (!$CI -and !$FederatedAuth -and $UserAuth) {
618637
if ($TestApplicationId) {
619638
Write-Warning "The specified TestApplicationId '$TestApplicationId' will be ignored when UserAuth is set."
620639
}
@@ -625,8 +644,8 @@ try {
625644
$userAccountName = $userAccount.UserPrincipalName
626645
Log "User authentication with user '$userAccountName' ('$TestApplicationId') will be used."
627646
}
628-
# If no test application ID was specified during an interactive session, create a new service principal.
629-
elseif (!$CI -and !$TestApplicationId) {
647+
# If user has specified -ServicePrincipalAuth
648+
elseif (!$CI -and $ServicePrincipalAuth) {
630649
# Cache the created service principal in this session for frequent reuse.
631650
$servicePrincipal = if ($AzureTestPrincipal -and (Get-AzADServicePrincipal -ApplicationId $AzureTestPrincipal.AppId) -and $AzureTestSubscription -eq $SubscriptionId) {
632651
Log "TestApplicationId was not specified; loading cached service principal '$($AzureTestPrincipal.AppId)'"
@@ -686,7 +705,9 @@ try {
686705
# Make sure pre- and post-scripts are passed formerly required arguments.
687706
$PSBoundParameters['TestApplicationId'] = $TestApplicationId
688707
$PSBoundParameters['TestApplicationOid'] = $TestApplicationOid
689-
$PSBoundParameters['TestApplicationSecret'] = $TestApplicationSecret
708+
if (!$FederatedAuth) {
709+
$PSBoundParameters['TestApplicationSecret'] = $TestApplicationSecret
710+
}
690711

691712
# If the role hasn't been explicitly assigned to the resource group and a cached service principal or user authentication is in use,
692713
# query to see if the grant is needed.
@@ -704,7 +725,7 @@ try {
704725
# considered a critical failure, as the test application may have subscription-level permissions and not require
705726
# the explicit grant.
706727
if (!$resourceGroupRoleAssigned) {
707-
$idSlug = if ($userAuth) { "User '$userAccountName' ('$TestApplicationId')"} else { "Test Application '$TestApplicationId'"};
728+
$idSlug = if ($UserAuth) { "User '$userAccountName' ('$TestApplicationId')" } else { "Test Application '$TestApplicationId'"};
708729
Log "Attempting to assign the 'Owner' role for '$ResourceGroupName' to the $idSlug"
709730
$ownerAssignment = New-AzRoleAssignment `
710731
-RoleDefinitionName "Owner" `
@@ -734,7 +755,7 @@ try {
734755
if ($TenantId) {
735756
$templateParameters.Add('tenantId', $TenantId)
736757
}
737-
if ($TestApplicationSecret) {
758+
if ($TestApplicationSecret -and !$FederatedAuth) {
738759
$templateParameters.Add('testApplicationSecret', $TestApplicationSecret)
739760
}
740761

@@ -1029,6 +1050,18 @@ By default, the -CI parameter will print out secrets to logs with Azure Pipeline
10291050
commands that cause them to be redacted. For CI environments that don't support this (like
10301051
stress test clusters), this flag can be set to $false to avoid printing out these secrets to the logs.
10311052
1053+
.PARAMETER ServicePrincipalAuth
1054+
Use the signed in user's credentials to create a service principal for
1055+
provisioning. This is useful for some local development scenarios.
1056+
1057+
.PARAMETER FederatedAuth
1058+
Use signed in user's credentials for provisioninig. No service principal will be
1059+
created. This is used in CI where the execution context already has a signed in
1060+
user.
1061+
1062+
In cases where provisioner or test applications are specified, secrets for those
1063+
apps will not be exported or made available to pre- or post- scripts.
1064+
10321065
.EXAMPLE
10331066
Connect-AzAccount -Subscription 'REPLACE_WITH_SUBSCRIPTION_ID'
10341067
New-TestResources.ps1 keyvault

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

Lines changed: 28 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] [-FederatedAuth] [-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] [-FederatedAuth] [-NewTestResourcesRemainingArguments <Object>]
3636
[-ProgressAction <ActionPreference>] [-WhatIf] [-Confirm] [<CommonParameters>]
3737
```
3838

@@ -629,15 +629,32 @@ 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.
632+
### -ServicePrincipalAuth
633+
Use the signed in user's credentials to create a service principal for
634+
provisioning.
635+
This is useful for some local development scenarios.
635636
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.
637+
```yaml
638+
Type: SwitchParameter
639+
Parameter Sets: (All)
640+
Aliases:
641+
642+
Required: False
643+
Position: Named
644+
Default value: False
645+
Accept pipeline input: False
646+
Accept wildcard characters: False
647+
```
648+
649+
### -FederatedAuth
650+
Use signed in user's credentials for provisioninig.
651+
No service principal will be
652+
created.
653+
This is used in CI where the execution context already has a signed in
654+
user.
655+
656+
In cases where provisioner or test applications are specified, secrets for those
657+
apps will not be exported or made available to pre- or post- scripts.
641658
642659
```yaml
643660
Type: SwitchParameter
@@ -716,7 +733,7 @@ Accept wildcard characters: False
716733
```
717734
718735
### CommonParameters
719-
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).
736+
This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see [about_CommonParameters](http://go.microsoft.com/fwlink/?LinkID=113216).
720737
721738
## INPUTS
722739

eng/common/TestResources/Remove-TestResources.ps1

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

59+
[Parameter()]
60+
[switch] $FederatedAuth,
61+
5962
[Parameter()]
6063
[switch] $Force,
6164

@@ -110,7 +113,7 @@ function Retry([scriptblock] $Action, [int] $Attempts = 5) {
110113
}
111114
}
112115

113-
if ($ProvisionerApplicationId) {
116+
if ($ProvisionerApplicationId -and !$FederatedAuth) {
114117
$null = Disable-AzContextAutosave -Scope Process
115118

116119
Log "Logging into service principal '$ProvisionerApplicationId'"
@@ -305,6 +308,10 @@ Run script in CI mode. Infers various environment variable names based on CI con
305308
.PARAMETER Force
306309
Force removal of resource group without asking for user confirmation
307310
311+
.PARAMETER FederatedAuth
312+
Use signed in user's credentials for provisioninig. This is used in CI where
313+
the execution context already has a signed in user.
314+
308315
.EXAMPLE
309316
Remove-TestResources.ps1 keyvault -Force
310317
Use the currently logged-in account to delete the resources created for Key Vault testing.

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

Lines changed: 74 additions & 22 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>] [-FederatedAuth] [-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>] [-FederatedAuth] [-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>] [-FederatedAuth] [-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>] [-FederatedAuth]
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,27 @@ 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+
### -FederatedAuth
259+
Use signed in user's credentials for provisioninig.
260+
This is used in CI where
261+
the execution context already has a signed in user.
262+
263+
```yaml
264+
Type: SwitchParameter
265+
Parameter Sets: (All)
266+
Aliases:
267+
268+
Required: False
269+
Position: Named
270+
Default value: False
271+
Accept pipeline input: False
272+
Accept wildcard characters: False
273+
```
237274
238275
### -Force
239276
Force removal of resource group without asking for user confirmation
@@ -296,8 +333,23 @@ Accept pipeline input: False
296333
Accept wildcard characters: False
297334
```
298335
336+
### -ProgressAction
337+
{{ Fill ProgressAction Description }}
338+
339+
```yaml
340+
Type: ActionPreference
341+
Parameter Sets: (All)
342+
Aliases: proga
343+
344+
Required: False
345+
Position: Named
346+
Default value: None
347+
Accept pipeline input: False
348+
Accept wildcard characters: False
349+
```
350+
299351
### CommonParameters
300-
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).
352+
This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see [about_CommonParameters](http://go.microsoft.com/fwlink/?LinkID=113216).
301353
302354
## INPUTS
303355

0 commit comments

Comments
 (0)