@@ -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.
109117if (! $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
10161029generated for. For ARM templates, it will be test-resources.json.env. For
10171030Bicep 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
10281033By default, the -CI parameter will print out secrets to logs with Azure Pipelines log
10291034commands that cause them to be redacted. For CI environments that don't support this (like
10301035stress 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
10331043Connect-AzAccount -Subscription 'REPLACE_WITH_SUBSCRIPTION_ID'
10341044New-TestResources.ps1 keyvault
0 commit comments