From ad743fd7edad45f86b1a2d9575cb262766a6f78b Mon Sep 17 00:00:00 2001 From: Shivangi Date: Tue, 19 Nov 2019 11:30:55 +0530 Subject: [PATCH] removing common change --- .../InitializeAzModuleFunctions.ps1 | 2 +- Tasks/Common/VstsAzureHelpers_/Utility.ps1 | 27 ++++++++++++++++++- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/Tasks/Common/VstsAzureHelpers_/InitializeAzModuleFunctions.ps1 b/Tasks/Common/VstsAzureHelpers_/InitializeAzModuleFunctions.ps1 index c14feef3afdb..0d9ce7690415 100644 --- a/Tasks/Common/VstsAzureHelpers_/InitializeAzModuleFunctions.ps1 +++ b/Tasks/Common/VstsAzureHelpers_/InitializeAzModuleFunctions.ps1 @@ -99,7 +99,7 @@ function Initialize-AzSubscription { if ($Endpoint.Auth.Scheme -eq 'ServicePrincipal') { try { if ($Endpoint.Auth.Parameters.AuthenticationType -eq 'SPNCertificate') { - $servicePrincipalCertificate = Add-Certificate -Endpoint $Endpoint -ServicePrincipal + $servicePrincipalCertificate = Add-CertificateForAz -Endpoint $Endpoint Write-Host "##[command]Connect-AzAccount -ServicePrincipal -Tenant $($Endpoint.Auth.Parameters.TenantId) -CertificateThumbprint ****** -ApplicationId $($Endpoint.Auth.Parameters.ServicePrincipalId) -Environment $environmentName" $null = Connect-AzAccount -ServicePrincipal -Tenant $Endpoint.Auth.Parameters.TenantId ` diff --git a/Tasks/Common/VstsAzureHelpers_/Utility.ps1 b/Tasks/Common/VstsAzureHelpers_/Utility.ps1 index 7010bb28021f..6221ec1bf1ed 100644 --- a/Tasks/Common/VstsAzureHelpers_/Utility.ps1 +++ b/Tasks/Common/VstsAzureHelpers_/Utility.ps1 @@ -12,7 +12,7 @@ $pemFileContent = $Endpoint.Auth.Parameters.ServicePrincipalCertificate $pfxFilePath, $pfxFilePassword = ConvertTo-Pfx -pemFileContent $pemFileContent - $certificate = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2($pfxFilePath, $pfxFilePassword, [System.Security.Cryptography.X509Certificates.X509KeyStorageFlags]::PersistKeySet) + $certificate.Import($pfxFilePath, $pfxFilePassword, [System.Security.Cryptography.X509Certificates.X509KeyStorageFlags]::PersistKeySet) } else { $bytes = [System.Convert]::FromBase64String($Endpoint.Auth.Parameters.Certificate) @@ -32,6 +32,31 @@ return $certificate } +function Add-CertificateForAz { + [CmdletBinding()] + param( + [Parameter(Mandatory=$true)] $Endpoint + ) + + $pemFileContent = $Endpoint.Auth.Parameters.ServicePrincipalCertificate + $pfxFilePath, $pfxFilePassword = ConvertTo-Pfx -pemFileContent $pemFileContent + + # Add the certificate to the cert store. + $certificate = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2($pfxFilePath, $pfxFilePassword, [System.Security.Cryptography.X509Certificates.X509KeyStorageFlags]::PersistKeySet) + + $store = New-Object System.Security.Cryptography.X509Certificates.X509Store( + ([System.Security.Cryptography.X509Certificates.StoreName]::My), + ([System.Security.Cryptography.X509Certificates.StoreLocation]::CurrentUser)) + $store.Open(([System.Security.Cryptography.X509Certificates.OpenFlags]::ReadWrite)) + $store.Add($certificate) + $store.Close() + + #store the thumbprint in a global variable which will be used to remove the certificate later on + $script:Endpoint_Authentication_Certificate = $certificate.Thumbprint + Write-Verbose "Added certificate to the certificate store." + return $certificate +} + function Format-Splat { [CmdletBinding()] param([Parameter(Mandatory = $true)][hashtable]$Hashtable)