SecretManagement.PleasantPasswordServer.Extension/Public/Get-SecretInfo.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
using namespace Microsoft.PowerShell.SecretManagement
using namespace System.Collections.ObjectModel
function Get-SecretInfo
{
    param (
        [Parameter(Mandatory)]
        [string]
        $VaultName,

        [Parameter()]
        [string]
        $Filter,

        [Parameter()]
        [hashtable]
        $AdditionalParameters
    )

    trap
    {
        Write-VaultError -ErrorRecord $_
    }

    $Token = Invoke-LoginToPleasant -AdditionalParameters $AdditionalParameters
    $headers = @{
        "Accept"        = "application/json"
        "Authorization" = "$Token"
    }

    $PasswordServerURL = [string]::Concat($AdditionalParameters.ServerURL, ":", $AdditionalParameters.Port)

    $Filter = $PSBoundParameters.Filter

    if ($Filter -ne '*')
    {
        $body = @{
            "search" = "$Filter"
        }

        $Secrets = Invoke-RestMethod -method post -Uri "$PasswordServerURL/api/v5/rest/search" -body (ConvertTo-Json $body) -Headers $headers -ContentType 'application/json'
        $id = $Secrets.Credentials.id

        if ($id.Count -gt 1)
        {
            throw "Multiple ambiguous entries found for $Name, please remove the duplicate entry"
        }

        if ($null -eq $id)
        {
            throw "No secret with $Name is found"
        }

        $Params = @{
            Method      = 'GET'
            Uri         = "$PasswordServerURL/api/v5/rest/Entries/$id"
            Headers     = $headers
            ContentType = 'application/json'
        }

        $CredentialMetadata = Invoke-RestMethod @Params

        $param = @{
            ServerURL = $PasswordServerURL
            Token     = $Token
            GroupID   = $CredentialMetadata.GroupId
        }

        $FolderPath = Get-SecretFolder @param

        $Name = $CredentialMetadata.Name

        [ReadOnlyDictionary[String, Object]]$metadata = [ordered]@{
            CustomUserFields = $CredentialMetadata.CustomUserFields
            Attachments      = $CredentialMetadata.Attachments
            Tags             = $CredentialMetadata.Tags
            Url              = $CredentialMetadata.Url
            Notes            = $CredentialMetadata.Notes
            Created          = if ([string]::IsNullOrWhiteSpace($Credential.Created))
            {
                ''
            }
            else
            {
                [DateTime]$Credential.Created
            }
            Modified         = if ([string]::IsNullOrWhiteSpace($Credential.Modified))
            {
                ''
            }
            else
            {
                [DateTime]$Credential.Modified
            }
            Expires          = if ([string]::IsNullOrWhiteSpace($Credential.Expires))
            {
                ''
            }
            else
            {
                [DateTime]$Credential.Expires
            }
            FolderName       = $FolderPath[0]
            Id               = $Folder.FolderID
        } | ConvertTo-ReadOnlyDictionary

        return [SecretInformation]::new(
            $Name,
            [SecretType]::PSCredential,
            $VaultName,
            $metadata
        )
    }
    else
    {
        $Params = @{
            Method      = 'GET'
            Uri         = "$PasswordServerURL/api/v5/rest/folders/"
            Headers     = $headers
            ContentType = 'application/json'
        }

        $AllFolders = Invoke-RestMethod @Params

        $PPSStructure = Get-Children -Folder $AllFolders

        [Object[]]$secretInfoResult = foreach ($Folder in $PPSStructure)
        {
            # Credentials
            $AllCredentials = $Folder.Credentials

            foreach ($Credential in $AllCredentials)
            {
                $Name = $Credential.Name

                [ReadOnlyDictionary[String, Object]]$metadata = [ordered]@{
                    CustomUserFields = $Credential.CustomUserFields
                    Attachments      = $Credential.Attachments
                    Tags             = $Credential.Tags
                    Url              = $Credential.Url
                    Notes            = $Credential.Notes
                    Created          = if ([string]::IsNullOrWhiteSpace($Credential.Created))
                    {
                        ''
                    }
                    else
                    {
                        [DateTime]$Credential.Created
                    }
                    Modified         = if ([string]::IsNullOrWhiteSpace($Credential.Modified))
                    {
                        ''
                    }
                    else
                    {
                        [DateTime]$Credential.Modified
                    }
                    Expires          = if ([string]::IsNullOrWhiteSpace($Credential.Expires))
                    {
                        ''
                    }
                    else
                    {
                        [DateTime]$Credential.Expires
                    }
                    FolderName       = $Folder.Folder
                    Id               = $Folder.FolderID
                } | ConvertTo-ReadOnlyDictionary

                [SecretInformation]::new(
                    $Name,
                    [SecretType]::PSCredential,
                    $VaultName,
                    $metadata
                )

            }
        }

        [Object[]]$sortedInfoResult = $secretInfoResult | Sort-Object -Unique -Property Name
        if ($sortedInfoResult.count -lt $secretInfoResult.count)
        {
            $nonUniqueFilteredRecords = Compare-Object $sortedInfoResult $secretInfoResult -Property Name | Where-Object SideIndicator -eq '=>'
            Write-Warning "Vault ${VaultName}: Entries with non-unique titles were detected, the duplicates were filtered out.)"
            Write-Warning "Vault ${VaultName}: Filtered Non-Unique Titles: $($nonUniqueFilteredRecords.Name -join ', ')"
        }

        return $secretInfoResult
    }
}