Public/Get-CIF3Config.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
function Get-CIF3Config {
    <#
    .SYNOPSIS
        Get CIF3 module configuration.

    .DESCRIPTION
        Get CIF3 module configuration

    .PARAMETER Source
        Get the config data from either...

            CIF3: the live module variable used for command defaults
            YML: the serialized .cif.yml that loads when importing the module

        Defaults to CIF3

    .PARAMETER Path
        If specified, read config from this YML file.

        Defaults to .cif.yml in the user's home directory.

    .FUNCTIONALITY
        CIF - Collective Intelligence Framework
    #>

    [CmdletBinding(DefaultParameterSetName = 'Source')]
    param(
        [Parameter(ParameterSetName = 'Source')]
        [ValidateSet("CIF3", "YML")]
        $Source = "CIF3",

        [Parameter(ParameterSetName = 'Path')]
        [Parameter(ParameterSetName = 'Source')]
        $Path = $Script:_CIF3Ymlpath
    )

    if ($PSCmdlet.ParameterSetName -eq 'Source' -and $Source -eq "CIF3" -and -not $PSBoundParameters.ContainsKey('Path')) {
        $Script:CIF3
    }
    else {
        function Decrypt {
            param($String)
            try {
                if ($String -notlike '' -and [System.Environment]::OSVersion.Platform -eq 'Win32NT') {
                    $SecureString = ConvertTo-SecureString $String -ErrorAction Stop
            
                    if ($SecureString -is [System.Security.SecureString]) {
                        [System.Runtime.InteropServices.marshal]::PtrToStringAuto(
                            [System.Runtime.InteropServices.marshal]::SecureStringToBSTR(
                                $SecureString))
                    }
                }
                else {
                    # If we're not on Windows, just return the regular String value since it shouldn't be encrypted/decrypted
                    return $String
                }
            }
            catch {
                # If not a SecureString, just return the regular String value
                return $String
            }
        }
        $TempObj = Get-Content -Path $Path -Raw | ConvertFrom-Yaml | Select-Object -Property `
            @{l = 'Proxy'; e = { $_.client.proxy } },
            @{l = 'Uri'; e = { Decrypt $_.client.remote } },
            @{l = 'Token'; e = { Decrypt $_.client.token } },
            @{l = 'ReadToken'; e = { Decrypt $_.client.read_token } },
            @{l = 'WriteToken'; e = { Decrypt $_.client.write_token } },
            @{l = 'ForceVerbose'; e = { $_.client.force_verbose } },
            @{l = 'NoVerifySsl'; e = { $_.client.no_verify_ssl } }

        # yml file can use 'token' or 'read_token' as name; if no 'token' property, look for 'read_token'
        if ($null -eq $TempObj.Token -and $null -ne $TempObj.ReadToken) {
            $TempObj.Token = $TempObj.ReadToken
        }
        # backfill ReadToken prop if it's empty
        if ($null -ne $TempObj.Token -and $null -eq $TempObj.ReadToken) {
            $TempObj.ReadToken = $TempObj.Token
        }

        # Nice oneliner to convert PSCustomObject to Hashtable: https://stackoverflow.com/questions/3740128/pscustomobject-to-hashtable
        $TempObj.PSObject.Properties | ForEach-Object -Begin { $h = @{ } } -Process { $h."$($_.Name)" = $_.Value } -End { $h } 
    }

}