Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update username auth to use both modules. #1706

Merged
merged 1 commit into from
May 12, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Tasks/AzurePowerShell/task.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"version": {
"Major": 1,
"Minor": 0,
"Patch": 23
"Patch": 24
Copy link
Contributor Author

@ericsciple ericsciple May 12, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For username/password auth, both Azure and AzureRM modules should be imported since we don't have a way to control which one the user really wants.

},
"demands": [
"azureps"
Expand Down
2 changes: 1 addition & 1 deletion Tasks/AzurePowerShell/task.loc.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"version": {
"Major": 1,
"Minor": 0,
"Patch": 23
"Patch": 24
},
"demands": [
"azureps"
Expand Down
54 changes: 31 additions & 23 deletions Tasks/Common/VstsAzureHelpers_/ImportFunctions.ps1
Original file line number Diff line number Diff line change
@@ -1,31 +1,43 @@
function Import-AzureModule {
[CmdletBinding()]
param([switch]$PreferAzureRM)
param(
[Parameter(Mandatory = $true)]
[ValidateSet('Azure', 'AzureRM')]
[string[]]$PreferredModule)

Trace-VstsEnteringInvocation $MyInvocation
try {
Write-Verbose "Env:PSModulePath: '$env:PSMODULEPATH'"
if ($PreferAzureRM) {
if (!(Import-FromModulePath -Classic:$false) -and
!(Import-FromSdkPath -Classic:$false) -and
!(Import-FromModulePath -Classic:$true) -and
!(Import-FromSdkPath -Classic:$true))
{
if ($PreferredModule -contains 'Azure' -and $PreferredModule -contains 'AzureRM') {
# Attempt to import Azure and AzureRM.
$azure = (Import-FromModulePath -Classic:$true) -or (Import-FromSdkPath -Classic:$true)
$azureRM = (Import-FromModulePath -Classic:$false) -or (Import-FromSdkPath -Classic:$false)
if (!$azure -and !$azureRM) {
throw (Get-VstsLocString -Key AZ_ModuleNotFound)
}
} else {
} elseif ($PreferredModule -contains 'Azure') {
# Attempt to import Azure but fallback to AzureRM.
if (!(Import-FromModulePath -Classic:$true) -and
!(Import-FromSdkPath -Classic:$true) -and
!(Import-FromModulePath -Classic:$false) -and
!(Import-FromSdkPath -Classic:$false))
{
throw (Get-VstsLocString -Key AZ_ModuleNotFound)
}
} else {
# Attempt to import AzureRM but fallback to Azure.
if (!(Import-FromModulePath -Classic:$false) -and
!(Import-FromSdkPath -Classic:$false) -and
!(Import-FromModulePath -Classic:$true) -and
!(Import-FromSdkPath -Classic:$true))
{
throw (Get-VstsLocString -Key AZ_ModuleNotFound)
}
}

# Validate the Classic version.
$minimumVersion = [version]'0.8.10.1'
if ($script:isClassic -and $script:classicVersion -lt $minimumVersion) {
if ($script:azureModule -and $script:azureModule.Version -lt $minimumVersion) {
throw (Get-VstsLocString -Key AZ_RequiresMinVersion0 -ArgumentList $minimumVersion)
}
} finally {
Expand Down Expand Up @@ -59,12 +71,9 @@ function Import-FromModulePath {
$module = Import-Module -Name $module.Path -Global -PassThru
Write-Verbose "Imported module version: $($module.Version)"

# Store the mode.
$script:isClassic = $Classic.IsPresent

if ($script:isClassic) {
# The Azure module was imported.
$script:classicVersion = $module.Version
if ($Classic) {
# Store the imported Azure module.
$script:azureModule = $module
} else {
# The AzureRM module was imported.

Expand All @@ -74,10 +83,10 @@ function Import-FromModulePath {
throw (Get-VstsLocString -Key AZ_AzureRMProfileModuleNotFound)
}

# Import the AzureRM.profile module.
# Import and then store the AzureRM.profile module.
Write-Host "##[command]Import-Module -Name $($profileModule.Path) -Global"
$profileModule = Import-Module -Name $profileModule.Path -Global -PassThru
Write-Verbose "Imported module version: $($profileModule.Version)"
$script:azureRMProfileModule = Import-Module -Name $profileModule.Path -Global -PassThru
Write-Verbose "Imported module version: $($script:azureRMProfileModule.Version)"
}

return $true
Expand Down Expand Up @@ -111,12 +120,11 @@ function Import-FromSdkPath {
$module = Import-Module -Name $path -Global -PassThru
Write-Verbose "Imported module version: $($module.Version)"

# Store the mode.
$script:isClassic = $Classic.IsPresent

# Store the imported module.
if ($Classic) {
# The Azure module was imported.
$script:classicVersion = $module.Version
$script:azureModule = $module
} else {
$script:azureRMProfileModule = $module
}

return $true
Expand Down
32 changes: 20 additions & 12 deletions Tasks/Common/VstsAzureHelpers_/InitializeFunctions.ps1
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
$script:isClassic = $null
$script:classicVersion = $null

function Add-Certificate {
function Add-Certificate {
[CmdletBinding()]
param([Parameter(Mandatory=$true)]$Endpoint)

Expand All @@ -28,7 +25,7 @@ function Initialize-AzureSubscription {

if ($Endpoint.Auth.Scheme -eq 'Certificate') {
# Certificate is only supported for the Azure module.
if (!$script:isClassic) {
if (!$script:azureModule) {
throw (Get-VstsLocString -Key AZ_CertificateAuthNotSupported)
}

Expand All @@ -49,7 +46,9 @@ function Initialize-AzureSubscription {
$psCredential = New-Object System.Management.Automation.PSCredential(
$Endpoint.Auth.Parameters.UserName,
(ConvertTo-SecureString $Endpoint.Auth.Parameters.Password -AsPlainText -Force))
if ($script:isClassic) {

# Add account (Azure).
if ($script:azureModule) {
try {
Write-Host "##[command]Add-AzureAccount -Credential $psCredential"
$null = Add-AzureAccount -Credential $psCredential
Expand All @@ -58,9 +57,10 @@ function Initialize-AzureSubscription {
Write-VstsTaskError -Message $_.Exception.Message
throw (New-Object System.Exception((Get-VstsLocString -Key AZ_CredentialsError), $_.Exception))
}
}

Set-CurrentAzureSubscription -SubscriptionId $Endpoint.Data.SubscriptionId -StorageAccount $StorageAccount
} else {
# Add account (AzureRM).
if ($script:azureRMProfileModule) {
try {
Write-Host "##[command]Add-AzureRMAccount -Credential $psCredential"
$null = Add-AzureRMAccount -Credential $psCredential
Expand All @@ -69,14 +69,22 @@ function Initialize-AzureSubscription {
Write-VstsTaskError -Message $_.Exception.Message
throw (New-Object System.Exception((Get-VstsLocString -Key AZ_CredentialsError), $_.Exception))
}
}

# Select subscription (Azure).
if ($script:azureModule) {
Set-CurrentAzureSubscription -SubscriptionId $Endpoint.Data.SubscriptionId -StorageAccount $StorageAccount
}

# Select subscription (AzureRM).
if ($script:azureRMProfileModule) {
Set-CurrentAzureRMSubscription -SubscriptionId $Endpoint.Data.SubscriptionId
}
} elseif ($Endpoint.Auth.Scheme -eq 'ServicePrincipal') {
$psCredential = New-Object System.Management.Automation.PSCredential(
$Endpoint.Auth.Parameters.ServicePrincipalId,
(ConvertTo-SecureString $Endpoint.Auth.Parameters.ServicePrincipalKey -AsPlainText -Force))
if ($script:isClassic -and $script:classicVersion -lt ([version]'0.9.9')) {
if ($script:azureModule -and $script:azureModule.Version -lt ([version]'0.9.9')) {
# Service principals arent supported from 0.9.9 and greater in the Azure module.
try {
Write-Host "##[command]Add-AzureAccount -ServicePrincipal -Tenant $($Endpoint.Auth.Parameters.TenantId) -Credential $psCredential"
Expand All @@ -88,9 +96,9 @@ function Initialize-AzureSubscription {
}

Set-CurrentAzureSubscription -SubscriptionId $Endpoint.Data.SubscriptionId -StorageAccount $StorageAccount
} elseif ($script:isClassic) {
} elseif ($script:azureModule) {
# Throw if >=0.9.9 Azure.
throw (Get-VstsLocString -Key "AZ_ServicePrincipalAuthNotSupportedAzureVersion0" -ArgumentList $script:classicVersion)
throw (Get-VstsLocString -Key "AZ_ServicePrincipalAuthNotSupportedAzureVersion0" -ArgumentList $script:azureModule.Version)
} else {
# Else, this is AzureRM.
try {
Expand All @@ -117,7 +125,7 @@ function Set-CurrentAzureSubscription {
[string]$StorageAccount)

$additional = @{ }
if ($script:isClassic -and $script:classicVersion -lt ([version]'0.8.15')) {
if ($script:azureModule.Version -lt ([version]'0.8.15')) {
$additional['Default'] = $true # The Default switch is required prior to 0.8.15.
}

Expand Down
17 changes: 14 additions & 3 deletions Tasks/Common/VstsAzureHelpers_/VstsAzureHelpers_.psm1
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Private module-scope variables.
$script:isClassic = $null
$script:classicVersion = $null
$script:azureModule = $null
$script:azureRMProfileModule = $null

# Override the DebugPreference.
if ($global:DebugPreference -eq 'Continue') {
Expand Down Expand Up @@ -32,8 +32,19 @@ function Initialize-Azure {
$endpoint = Get-VstsEndpoint -Name $serviceName -Require
$storageAccount = Get-VstsInput -Name StorageAccount

# Determine which modules are preferred.
$preferredModules = @( )
if ($serviceNameInput -eq 'ConnectedServiceNameARM') {
$preferredModules += 'AzureRM'
} elseif ($endpoint.Auth.Scheme -eq 'UserNamePassword') {
$preferredModules += 'Azure'
$preferredModules += 'AzureRM'
} else {
$preferredModules += 'Azure'
}

# Import/initialize the Azure module.
Import-AzureModule -PreferAzureRM:($serviceNameInput -eq 'ConnectedServiceNameARM')
Import-AzureModule -PreferredModule $preferredModules
Initialize-AzureSubscription -Endpoint $endpoint -StorageAccount $storageAccount
} finally {
Trace-VstsLeavingInvocation $MyInvocation
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
[CmdletBinding()]
param()

# Arrange.
. $PSScriptRoot/../../lib/Initialize-Test.ps1
$module = Microsoft.PowerShell.Core\Import-Module $PSScriptRoot/../../../Tasks/AzurePowerShell/ps_modules/VstsAzureHelpers_ -PassThru
$variableSets = @(
@{
ClassicModulePathResult = $true
ClassicSdkPathResult = $null
RMModulePathResult = $null
RMSdkPathResult = $null
}
@{
ClassicModulePathResult = $false
ClassicSdkPathResult = $true
RMModulePathResult = $null
RMSdkPathResult = $null
}
@{
ClassicModulePathResult = $false
ClassicSdkPathResult = $false
RMModulePathResult = $true
RMSdkPathResult = $null
}
@{
ClassicModulePathResult = $false
ClassicSdkPathResult = $false
RMModulePathResult = $false
RMSdkPathResult = $true
}
)
foreach ($variableSet in $variableSets) {
Write-Verbose ('-' * 80)
Unregister-Mock Import-FromModulePath
Unregister-Mock Import-FromSdkPath
Register-Mock Import-FromModulePath
Register-Mock Import-FromSdkPath
if ($variableSet.RMModulePathResult -ne $null) {
Register-Mock Import-FromModulePath { $variableSet.RMModulePathResult } -- -Classic: $false
}

if ($variableSet.RMSdkPathResult -ne $null) {
Register-Mock Import-FromSdkPath { $variableSet.RMSdkPathResult } -- -Classic: $false
}

if ($variableSet.ClassicModulePathResult -ne $null) {
Register-Mock Import-FromModulePath { $variableSet.ClassicModulePathResult } -- -Classic: $true
}

if ($variableSet.ClassicSdkPathResult -ne $null) {
Register-Mock Import-FromSdkPath { $variableSet.ClassicSdkPathResult } -- -Classic: $true
}

# Act.
& $module Import-AzureModule -PreferredModule 'Azure'

# Assert.
Assert-WasCalled Import-FromModulePath -Times $(if ($variableSet.RMModulePathResult -eq $null) { 0 } else { 1 }) -- -Classic: $false
Assert-WasCalled Import-FromSdkPath -Times $(if ($variableSet.RMSdkPathResult -eq $null) { 0 } else { 1 }) -- -Classic: $false
Assert-WasCalled Import-FromModulePath -Times $(if ($variableSet.ClassicModulePathResult -eq $null) { 0 } else { 1 }) -- -Classic: $true
Assert-WasCalled Import-FromSdkPath -Times $(if ($variableSet.ClassicSdkPathResult -eq $null) { 0 } else { 1 }) -- -Classic: $true
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
[CmdletBinding()]
param()

# Arrange.
. $PSScriptRoot/../../lib/Initialize-Test.ps1
$module = Microsoft.PowerShell.Core\Import-Module $PSScriptRoot/../../../Tasks/AzurePowerShell/ps_modules/VstsAzureHelpers_ -PassThru
$variableSets = @(
@{
RMModulePathResult = $true
RMSdkPathResult = $null
ClassicModulePathResult = $null
ClassicSdkPathResult = $null
}
@{
RMModulePathResult = $false
RMSdkPathResult = $true
ClassicModulePathResult = $null
ClassicSdkPathResult = $null
}
@{
RMModulePathResult = $false
RMSdkPathResult = $false
ClassicModulePathResult = $true
ClassicSdkPathResult = $null
}
@{
RMModulePathResult = $false
RMSdkPathResult = $false
ClassicModulePathResult = $false
ClassicSdkPathResult = $true
}
)
foreach ($variableSet in $variableSets) {
Write-Verbose ('-' * 80)
Unregister-Mock Import-FromModulePath
Unregister-Mock Import-FromSdkPath
Register-Mock Import-FromModulePath
Register-Mock Import-FromSdkPath
if ($variableSet.RMModulePathResult -ne $null) {
Register-Mock Import-FromModulePath { $variableSet.RMModulePathResult } -- -Classic: $false
}

if ($variableSet.RMSdkPathResult -ne $null) {
Register-Mock Import-FromSdkPath { $variableSet.RMSdkPathResult } -- -Classic: $false
}

if ($variableSet.ClassicModulePathResult -ne $null) {
Register-Mock Import-FromModulePath { $variableSet.ClassicModulePathResult } -- -Classic: $true
}

if ($variableSet.ClassicSdkPathResult -ne $null) {
Register-Mock Import-FromSdkPath { $variableSet.ClassicSdkPathResult } -- -Classic: $true
}

# Act.
& $module Import-AzureModule -PreferredModule 'AzureRM'

# Assert.
Assert-WasCalled Import-FromModulePath -Times $(if ($variableSet.RMModulePathResult -eq $null) { 0 } else { 1 }) -- -Classic: $false
Assert-WasCalled Import-FromSdkPath -Times $(if ($variableSet.RMSdkPathResult -eq $null) { 0 } else { 1 }) -- -Classic: $false
Assert-WasCalled Import-FromModulePath -Times $(if ($variableSet.ClassicModulePathResult -eq $null) { 0 } else { 1 }) -- -Classic: $true
Assert-WasCalled Import-FromSdkPath -Times $(if ($variableSet.ClassicSdkPathResult -eq $null) { 0 } else { 1 }) -- -Classic: $true
}
Loading