@@ -92,8 +92,9 @@ param (
92
92
[Parameter ()]
93
93
[switch ] $SuppressVsoCommands = ($null -eq $env: SYSTEM_TEAMPROJECTID ),
94
94
95
+ # Default behavior is to use logged in credentials
95
96
[Parameter ()]
96
- [switch ] $UserAuth ,
97
+ [switch ] $ServicePrincipalAuth ,
97
98
98
99
# Captures any arguments not declared here (no parameter errors)
99
100
# This enables backwards compatibility with old script versions in
@@ -105,6 +106,13 @@ param (
105
106
106
107
. $PSScriptRoot / SubConfig- Helpers.ps1
107
108
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
+
108
116
# By default stop for any error.
109
117
if (! $PSBoundParameters.ContainsKey (' ErrorAction' )) {
110
118
$ErrorActionPreference = ' Stop'
@@ -267,9 +275,6 @@ function BuildDeploymentOutputs([string]$serviceName, [object]$azContext, [objec
267
275
$serviceDirectoryPrefix = BuildServiceDirectoryPrefix $serviceName
268
276
# Add default values
269
277
$deploymentOutputs = [Ordered ]@ {
270
- " ${serviceDirectoryPrefix} CLIENT_ID" = $TestApplicationId ;
271
- " ${serviceDirectoryPrefix} CLIENT_SECRET" = $TestApplicationSecret ;
272
- " ${serviceDirectoryPrefix} TENANT_ID" = $azContext.Tenant.Id ;
273
278
" ${serviceDirectoryPrefix} SUBSCRIPTION_ID" = $azContext.Subscription.Id ;
274
279
" ${serviceDirectoryPrefix} RESOURCE_GROUP" = $resourceGroup.ResourceGroupName ;
275
280
" ${serviceDirectoryPrefix} LOCATION" = $resourceGroup.Location ;
@@ -280,6 +285,12 @@ function BuildDeploymentOutputs([string]$serviceName, [object]$azContext, [objec
280
285
" AZURE_SERVICE_DIRECTORY" = $serviceName.ToUpperInvariant ();
281
286
}
282
287
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
+
283
294
MergeHashes $environmentVariables $ (Get-Variable deploymentOutputs)
284
295
285
296
foreach ($key in $deployment.Outputs.Keys ) {
@@ -518,8 +529,8 @@ try {
518
529
}
519
530
}
520
531
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 ) {
523
534
$null = Disable-AzContextAutosave - Scope Process
524
535
525
536
Log " Logging into service principal '$ProvisionerApplicationId '."
@@ -614,9 +625,9 @@ try {
614
625
}
615
626
}
616
627
617
- if ($UserAuth ) {
628
+ if (! $CI -and ! $ServicePrincipalAuth ) {
618
629
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."
620
631
}
621
632
622
633
$userAccount = (Get-AzADUser - UserPrincipalName (Get-AzContext ).Account)
@@ -625,8 +636,8 @@ try {
625
636
$userAccountName = $userAccount.UserPrincipalName
626
637
Log " User authentication with user '$userAccountName ' ('$TestApplicationId ') will be used."
627
638
}
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 ) {
630
641
# Cache the created service principal in this session for frequent reuse.
631
642
$servicePrincipal = if ($AzureTestPrincipal -and (Get-AzADServicePrincipal - ApplicationId $AzureTestPrincipal.AppId ) -and $AzureTestSubscription -eq $SubscriptionId ) {
632
643
Log " TestApplicationId was not specified; loading cached service principal '$ ( $AzureTestPrincipal.AppId ) '"
@@ -686,7 +697,9 @@ try {
686
697
# Make sure pre- and post-scripts are passed formerly required arguments.
687
698
$PSBoundParameters [' TestApplicationId' ] = $TestApplicationId
688
699
$PSBoundParameters [' TestApplicationOid' ] = $TestApplicationOid
689
- $PSBoundParameters [' TestApplicationSecret' ] = $TestApplicationSecret
700
+ if ($ServicePrincipalAuth ) {
701
+ $PSBoundParameters [' TestApplicationSecret' ] = $TestApplicationSecret
702
+ }
690
703
691
704
# If the role hasn't been explicitly assigned to the resource group and a cached service principal or user authentication is in use,
692
705
# query to see if the grant is needed.
@@ -704,7 +717,7 @@ try {
704
717
# considered a critical failure, as the test application may have subscription-level permissions and not require
705
718
# the explicit grant.
706
719
if (! $resourceGroupRoleAssigned ) {
707
- $idSlug = if ($userAuth ) { " User '$userAccountName ' ('$TestApplicationId ')" } else { " Test Application '$TestApplicationId '" };
720
+ $idSlug = if (! $ServicePrincipalAuth ) { " User '$userAccountName ' ('$TestApplicationId ')" } else { " Test Application '$TestApplicationId '" };
708
721
Log " Attempting to assign the 'Owner' role for '$ResourceGroupName ' to the $idSlug "
709
722
$ownerAssignment = New-AzRoleAssignment `
710
723
- RoleDefinitionName " Owner" `
@@ -734,7 +747,7 @@ try {
734
747
if ($TenantId ) {
735
748
$templateParameters.Add (' tenantId' , $TenantId )
736
749
}
737
- if ($TestApplicationSecret ) {
750
+ if ($TestApplicationSecret -and $ServicePrincipalAuth ) {
738
751
$templateParameters.Add (' testApplicationSecret' , $TestApplicationSecret )
739
752
}
740
753
@@ -1016,19 +1029,16 @@ The environment file will be named for the test resources template that it was
1016
1029
generated for. For ARM templates, it will be test-resources.json.env. For
1017
1030
Bicep templates, test-resources.bicep.env.
1018
1031
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
-
1027
1032
. PARAMETER SuppressVsoCommands
1028
1033
By default, the -CI parameter will print out secrets to logs with Azure Pipelines log
1029
1034
commands that cause them to be redacted. For CI environments that don't support this (like
1030
1035
stress test clusters), this flag can be set to $false to avoid printing out these secrets to the logs.
1031
1036
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
+
1032
1042
. EXAMPLE
1033
1043
Connect-AzAccount -Subscription 'REPLACE_WITH_SUBSCRIPTION_ID'
1034
1044
New-TestResources.ps1 keyvault
0 commit comments