microsoft / azure-pipelines-tasks

Tasks for Azure Pipelines
https://aka.ms/tfbuild
MIT License
3.5k stars 2.61k forks source link

[BUG]: Calling Set-AzKeyVaultSecret in a module changes return type #19260

Open pbo-top opened 12 months ago

pbo-top commented 12 months ago

Task name

AzurePowerShell

Task version

5.230.0

Environment type (Please select at least one enviroment where you face this issue)

Azure DevOps Server type

dev.azure.com (formerly visualstudio.com)

Azure DevOps Server Version (if applicable)

No response

Operation system

ubuntu-latest

Task log

"Get Result1 via module" (console output):
Here: Microsoft.Azure.Commands.KeyVault.Models.PSKeyVaultSecret dette er bare *** tekst!
System.Object[]

"Get Result2 via module" (console output):
Here: dette er bare *** tekst!
System.String

Relevant log output

n/a

Aditional info

Calling the function Set-AzKeyVaultSecret inside a PS module used in pipeline (AzurePowerShell) will change the object-type of a given return value. It becomes a kind of "PSKeyVaultSecret" but with the intended value as a ordinary string. If it is not a bug, then there are some trickery at play I really don't get, and would like have explained.

If the behaviour is intended, how does one get the values casted out of the PSKeyVaultSecret object?

The masking of the return-text is also a bit strange selected, but that doesn't matter here.

The first task invokes the Set-AzKeyVaultSecret function, the second doesn't. Results are in the Task Log.

Sample pipeline snippets:

- task: AzurePowerShell@5
  displayName: "Get Result1 via module"
  inputs:
    azureSubscription: 'xx'
    ScriptType: 'InlineScript'
    azurePowerShellVersion: 'LatestVersion'
    pwsh: true
    Inline: |
      import-module ./MyModule
      $test = Get-Result1 -keyVaultName "kv-some-name"
      Write-Host "Here: $test"
      Write-Host $test.GetType()
    errorActionPreference: continue

- task: AzurePowerShell@5
  displayName: "Get Result2 via module"
  inputs:
    azureSubscription: 'xx'
    ScriptType: 'InlineScript'
    azurePowerShellVersion: 'LatestVersion'
    pwsh: true
    Inline: |
      import-module ./MyModule
      $test = Get-Result2 -keyVaultName "kv-some-vault"
      Write-Host "Here: $test"
      Write-Host $test.GetType()
    errorActionPreference: continue

Module function snippets:

function Get-Result1 {
    param (
        [Parameter(Mandatory = $true)][string] $keyVaultName
    )
    $test2 = "dette er bare noget tekst!"
    $secretvalue = ConvertTo-SecureString $test2 -AsPlainText -Force
    Set-AzKeyVaultSecret -VaultName $keyVaultName -Name "hemmelig2" -SecretValue $secretvalue
    return $test2
}

function Get-Result2 {
    param (
        [Parameter(Mandatory = $true)][string] $keyVaultName
    )
    $test2 = "dette er bare noget tekst!"
    $secretvalue = ConvertTo-SecureString $test2 -AsPlainText -Force
    # Set-AzKeyVaultSecret -VaultName $keyVaultName -Name "hemmelig2" -SecretValue $secretvalue
    return $test2
}
github-actions[bot] commented 6 months ago

This issue is stale because it has been open for 180 days with no activity. Remove the stale label or comment on the issue otherwise this will be closed in 5 days

pbo-top commented 5 months ago

Bug not solved/looked upon, preventing auto-close.