PowerShell / SecretManagement

PowerShell module to consistent usage of secrets through different extension vaults
MIT License
328 stars 46 forks source link

[Feature Request] Constructor for SecretInformation Metadata that accepts hashtable #108

Closed JustinGrote closed 3 years ago

JustinGrote commented 3 years ago

For typical Powershell users writing script vault extensions, the ReadOnlyDictionary class is obtuse to construct, and therefore [SecretInformation] should also supply a constructor that accepts a hashtable or OrderedDictionary and then converts that to ReadOnlyDictionary for the object property.

Alternatively OrderedDictionary could be used, as when it is output to the user as a property the get field could add .AsReadOnly() and you have the same effect of a string,object dictionary. Powershell users already have a shorthand for this and are familar with it: [ordered]@{}

I had to make this function just to accommodate it, so this kind of thing might scare off vault extension authors without a good grasp of .NET

using namespace System.Collections.ObjectModel
using namespace System.Collections.Generic
function ConvertTo-ReadOnlyDictionary {
    <#
        .SYNOPSIS
        Converts a hashtable to a ReadOnlyDictionary[String,Object]. Needed for SecretInformation
    #>
    [CmdletBinding()]
    param(
        [Parameter(ValueFromPipeline)][hashtable]$hashtable
    )
    process {
        $dictionary = [Dictionary[string,object]]::new([StringComparer]::OrdinalIgnoreCase)
        $hashtable.GetEnumerator().foreach{
            $dictionary[$_.Name] = $_.Value
        }
        [ReadOnlyDictionary[string,object]]::new($dictionary)
    }
}
PaulHigin commented 3 years ago

Implemented in GA release