PowerShell / Modules

MIT License
112 stars 25 forks source link

[SecretsManagement] Get-Secret returns the first item in a ByteArray #40

Closed jeremymcgee73 closed 4 years ago

jeremymcgee73 commented 4 years ago

When returning a bytearray in Get-Secret, only the first item of the array is returned. This should return the whole array to properly support ByteArrays. I worked on this for a few hours thinking it was my implementation. Then, I realized It was a bug. Thanks!

Steps to reproduce

function Get-Secret
{
    param (
        [string] $Name,
        [hashtable] $AdditionalParameters
    )

    if ([WildcardPattern]::ContainsWildcardCharacters($Name))
    {
        throw "The Name parameter cannot contain wild card characters."
    }

    $enc = [system.Text.Encoding]::UTF8
    $string1 = "Testing byte array"
    $Array = $enc.GetBytes($string1)

    return $Array
}

Expected behavior

(Get-Secret -Name "Binary" -Vault "Test").GetType().Name
Byte[]

Actual behavior

(Get-Secret -Name "Binary" -Vault "Test").GetType().Name
Byte
PaulHigin commented 4 years ago

This is a PowerShell peculiarity, where the pipeline automatically uwraps object arrays. I forgot to fix this in my test script extension, but have it in my tests. You need to do the following:

$secret = $enc.GetBytes($string1)
...

if ($secret -is [byte[]])
{
    return @(,$secret)  # Return as array instead of individual array objects.
}

I'll update my test extension to do this.