From 20dc14c2af4f4cda61dff2b6c2edb30e7263c79c Mon Sep 17 00:00:00 2001 From: Shawn Melton <11204251+wsmelton@users.noreply.github.com> Date: Tue, 19 Oct 2021 16:56:20 -0500 Subject: [PATCH] Search-TssSecretsByUrl - new command to check which Secret(s) is returned for a given URL, endpoint used by WPF --- .../Search-TssSecretsByUrl.md | 73 ++++++++++++++ src/Thycotic.SecretServer.Format.ps1xml | 98 ++++++++++++------- src/Thycotic.SecretServer.Types.ps1xml | 17 ++++ .../classes/secret-extensions/Secret.cs | 25 +++++ .../secret-extensions/SecretMatchType.cs | 14 +++ .../Search-TssSecretsByUrl.ps1 | 69 +++++++++++++ .../Search-TssSecretsByUrl.Tests.ps1 | 24 +++++ 7 files changed, 285 insertions(+), 35 deletions(-) create mode 100644 docs/commands/secret-extensions/Search-TssSecretsByUrl.md create mode 100644 src/Thycotic.SecretServer/classes/secret-extensions/Secret.cs create mode 100644 src/Thycotic.SecretServer/enums/secret-extensions/SecretMatchType.cs create mode 100644 src/functions/secret-extensions/Search-TssSecretsByUrl.ps1 create mode 100644 tests/secret-extensions/Search-TssSecretsByUrl.Tests.ps1 diff --git a/docs/commands/secret-extensions/Search-TssSecretsByUrl.md b/docs/commands/secret-extensions/Search-TssSecretsByUrl.md new file mode 100644 index 00000000..50d851c0 --- /dev/null +++ b/docs/commands/secret-extensions/Search-TssSecretsByUrl.md @@ -0,0 +1,73 @@ +# Search-TssSecretsByUrl + +## SYNOPSIS +Search for Secrets that match a URL for Web Password Filler + +## SYNTAX + +``` +Search-TssSecretsByUrl [-TssSession] [-Url ] [] +``` + +## DESCRIPTION +Search for Secrets that match a URL for Web Password Filler + +## EXAMPLES + +### EXAMPLE 1 +``` +$session = New-TssSession -SecretServer https://alpha -Credential $ssCred +Search-TssWpfSecretsByUrl -TssSession $session -Url 'https://citibank.com/login' +``` + +Return Secrets that match the URL provided + +## PARAMETERS + +### -TssSession +TssSession object created by New-TssSession for authentication + +```yaml +Type: Session +Parameter Sets: (All) +Aliases: + +Required: True +Position: 1 +Default value: None +Accept pipeline input: True (ByValue) +Accept wildcard characters: False +``` + +### -Url +URL to search against + +```yaml +Type: String +Parameter Sets: (All) +Aliases: + +Required: False +Position: Named +Default value: None +Accept pipeline input: False +Accept wildcard characters: False +``` + +### CommonParameters +This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see [about_CommonParameters](http://go.microsoft.com/fwlink/?LinkID=113216). + +## INPUTS + +## OUTPUTS + +### Thycotic.PowerShell.SecretExtensions.Secret +## NOTES +Requires TssSession object returned by New-TssSession + +## RELATED LINKS + +[https://thycotic-ps.github.io/thycotic.secretserver/commands/secret-extensions/Search-TssSecretsByUrl](https://thycotic-ps.github.io/thycotic.secretserver/commands/secret-extensions/Search-TssSecretsByUrl) + +[https://github.com/thycotic-ps/thycotic.secretserver/blob/main/src/functions/secret-extensions/Search-TssSecretsByUrl.ps1](https://github.com/thycotic-ps/thycotic.secretserver/blob/main/src/functions/secret-extensions/Search-TssSecretsByUrl.ps1) + diff --git a/src/Thycotic.SecretServer.Format.ps1xml b/src/Thycotic.SecretServer.Format.ps1xml index 4bac93ba..9611593f 100644 --- a/src/Thycotic.SecretServer.Format.ps1xml +++ b/src/Thycotic.SecretServer.Format.ps1xml @@ -144,53 +144,21 @@ - - - - - - - - - SecretTemplateFieldId - - - DisplayName - - - FieldSlugName + Id Name - IsRequired - - - IsFile - - - IsPassword - - - IsNotes - - - IsUrl - - - HideOnView - - - ExposeForDisplay + Fields - SortOrder + PasswordTypeId @@ -3906,5 +3874,65 @@ + + + SecretExtensions.Secret + + Thycotic.PowerShell.SecretExtensions.Secret + + + + + 18 + + + 10 + + + 20 + + + 15 + + + 20 + + + 35 + + + 15 + + + + + + + SecretTemplateId + + + SecretId + + + SecretName + + + MatchOrderType + + + HasOwnerOrEditAccess + + + SecretUrl + + + RequireComment + + + + + + + diff --git a/src/Thycotic.SecretServer.Types.ps1xml b/src/Thycotic.SecretServer.Types.ps1xml index fb566b56..595a90bb 100644 --- a/src/Thycotic.SecretServer.Types.ps1xml +++ b/src/Thycotic.SecretServer.Types.ps1xml @@ -321,6 +321,23 @@ + + Thycotic.PowerShell.SecretExtensions.Secret + + + SecretTemplateId + SecretTypeId + + + SecretId + Id + + + SecretName + Name + + + Thycotic.PowerShell.Diagnostics.Diagnostic diff --git a/src/Thycotic.SecretServer/classes/secret-extensions/Secret.cs b/src/Thycotic.SecretServer/classes/secret-extensions/Secret.cs new file mode 100644 index 00000000..602f7118 --- /dev/null +++ b/src/Thycotic.SecretServer/classes/secret-extensions/Secret.cs @@ -0,0 +1,25 @@ +using System; +using System.Threading.Tasks; +using System.Management.Automation; +using System.Management.Automation.Runspaces; +using Thycotic.PowerShell.Enums; + +namespace Thycotic.PowerShell.SecretExtensions +{ + public class Secret + { + public bool HasOwnerOrEditAccess { get; set; } + public int Id { get; set; } + public bool IsButtonBound { get; set; } + public bool IsFavoriteSecret { get; set; } + public bool IsSystemFolder { get; set; } + public SecretMatchType MatchOrderType { get; set; } + public string Name { get; set; } + public string RedirectUrl { get; set; } + public bool RequireComment { get; set; } + public int ResultPriority { get; set; } + public int SecretTypeId { get; set; } + public string SecretUrl { get; set; } + public bool ShouldRedirect { get; set; } + } +} \ No newline at end of file diff --git a/src/Thycotic.SecretServer/enums/secret-extensions/SecretMatchType.cs b/src/Thycotic.SecretServer/enums/secret-extensions/SecretMatchType.cs new file mode 100644 index 00000000..188de1c2 --- /dev/null +++ b/src/Thycotic.SecretServer/enums/secret-extensions/SecretMatchType.cs @@ -0,0 +1,14 @@ +using System; +using System.Threading.Tasks; +using System.Management.Automation; +using System.Management.Automation.Runspaces; + +namespace Thycotic.PowerShell.Enums +{ + public enum SecretMatchType + { + Loose, + Partial, + Exact + } +} \ No newline at end of file diff --git a/src/functions/secret-extensions/Search-TssSecretsByUrl.ps1 b/src/functions/secret-extensions/Search-TssSecretsByUrl.ps1 new file mode 100644 index 00000000..bd5e8fd9 --- /dev/null +++ b/src/functions/secret-extensions/Search-TssSecretsByUrl.ps1 @@ -0,0 +1,69 @@ +function Search-TssSecretsByUrl { + <# + .SYNOPSIS + Search for Secrets that match a URL for Web Password Filler + + .DESCRIPTION + Search for Secrets that match a URL for Web Password Filler + + .LINK + https://thycotic-ps.github.io/thycotic.secretserver/commands/secret-extensions/Search-TssSecretsByUrl + + .LINK + https://github.com/thycotic-ps/thycotic.secretserver/blob/main/src/functions/secret-extensions/Search-TssSecretsByUrl.ps1 + + .EXAMPLE + $session = New-TssSession -SecretServer https://alpha -Credential $ssCred + Search-TssWpfSecretsByUrl -TssSession $session -Url 'https://citibank.com/login' + + Return Secrets that match the URL provided + + .NOTES + Requires TssSession object returned by New-TssSession + #> + [CmdletBinding()] + [OutputType('Thycotic.PowerShell.SecretExtensions.Secret')] + param ( + # TssSession object created by New-TssSession for authentication + [Parameter(Mandatory,ValueFromPipeline,Position = 0)] + [Thycotic.PowerShell.Authentication.Session] + $TssSession, + + # URL to search against + [string] + $Url + ) + begin { + $tssParams = $PSBoundParameters + $invokeParams = . $GetInvokeApiParams $TssSession + } + process { + Get-TssInvocation $PSCmdlet.MyInvocation + if ($tssParams.ContainsKey('TssSession') -and $TssSession.IsValidSession()) { + Compare-TssVersion $TssSession '10.9.000064' $PSCmdlet.MyInvocation + $uri = $TssSession.ApiUrl, 'secret-extensions', 'search-by-url' -join '/' + $invokeParams.Uri = $uri + $invokeParams.Method = 'POST' + $invokeParams.Body = "`"$Url`"" + + Write-Verbose "Performing the operation $($invokeParams.Method) $($invokeParams.Uri) with $($invokeParams.Body)" + try { + $apiResponse = Invoke-TssApi @invokeParams + $restResponse = . $ProcessResponse $apiResponse + } catch { + Write-Warning "Issue on search request" + $err = $_ + . $ErrorHandling $err + } + + if ($restResponse.records.Count -le 0 -and $restResponse.records.Length -eq 0) { + Write-Warning "No records found" + } + if ($restResponse.model) { + [Thycotic.PowerShell.SecretExtensions.Secret[]]$restResponse.model + } + } else { + Write-Warning "No valid session found" + } + } +} \ No newline at end of file diff --git a/tests/secret-extensions/Search-TssSecretsByUrl.Tests.ps1 b/tests/secret-extensions/Search-TssSecretsByUrl.Tests.ps1 new file mode 100644 index 00000000..e67d6eaa --- /dev/null +++ b/tests/secret-extensions/Search-TssSecretsByUrl.Tests.ps1 @@ -0,0 +1,24 @@ +BeforeDiscovery { + $commandName = Split-Path ($PSCommandPath.Replace('.Tests.ps1','')) -Leaf +} +Describe "$commandName verify parameters" { + BeforeDiscovery { + [object[]]$knownParameters = 'TssSession', 'Url' + [object[]]$currentParams = ([Management.Automation.CommandMetaData]$ExecutionContext.SessionState.InvokeCommand.GetCommand($commandName,'Function')).Parameters.Keys + [object[]]$commandDetails = [System.Management.Automation.CommandInfo]$ExecutionContext.SessionState.InvokeCommand.GetCommand($commandName,'Function') + $unknownParameters = Compare-Object -ReferenceObject $knownParameters -DifferenceObject $currentParams -PassThru + } + Context "Verify parameters" -Foreach @{currentParams = $currentParams } { + It "$commandName should contain <_> parameter" -TestCases $knownParameters { + $_ -in $currentParams | Should -Be $true + } + It "$commandName should not contain parameter: <_>" -TestCases $unknownParameters { + $_ | Should -BeNullOrEmpty + } + } + Context "Command specific details" { + It "$commandName should set OutputType to Thycotic.PowerShell.SecretExtensions.Secret" -TestCases $commandDetails { + $_.OutputType.Name | Should -Be 'Thycotic.PowerShell.SecretExtensions.Secret' + } + } +} \ No newline at end of file