Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Disable-TssSecretCheckout - closes #119
- Loading branch information
Showing
4 changed files
with
190 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
function Disable-SecretCheckout { | ||
<# | ||
.SYNOPSIS | ||
Disables the Checkout setting for a Secret | ||
.DESCRIPTION | ||
Disables the Checkout setting for a Secret | ||
.EXAMPLE | ||
$session = New-TssSession -SecretServer https://alpha -Credential $ssCred | ||
Disable-TssSecretCheckout -TssSession $session -Id 28 | ||
Disable Secret 28's Checkout When Viewed setting | ||
.EXAMPLE | ||
$session = New-TssSession -SecretServer https://alpha -Credential $ssCred | ||
Disable-TssSecretCheckout -TssSession $session -Id 42,43,45 | ||
Disable Checkout When Viewed setting on Secret IDs 42, 43, and 45 | ||
.LINK | ||
https://thycotic-ps.github.io/thycotic.secretserver/commands/Disable-TssSecretCheckout | ||
.NOTES | ||
Requires TssSession object returned by New-TssSession | ||
#> | ||
[CmdletBinding(SupportsShouldProcess)] | ||
param ( | ||
# TssSession object created by New-TssSession for auth | ||
[Parameter(Mandatory,ValueFromPipeline,Position = 0)] | ||
[TssSession] | ||
$TssSession, | ||
|
||
# Secret Id | ||
[Parameter(Mandatory,ValueFromPipelineByPropertyName)] | ||
[Alias("SecretId")] | ||
[int[]] | ||
$Id, | ||
|
||
# Comment to provide for restricted secret (Require Comment is enabled) | ||
[Parameter(ParameterSetName = 'restricted')] | ||
[string] | ||
$Comment, | ||
|
||
# Associated Ticket Number | ||
[Parameter(ParameterSetName = 'restricted')] | ||
[int] | ||
$TicketNumber, | ||
|
||
#Associated Ticket System ID | ||
[Parameter(ParameterSetName = 'restricted')] | ||
[int] | ||
$TicketSystemId | ||
) | ||
begin { | ||
$tssParams = $PSBoundParameters | ||
$invokeParams = . $GetInvokeTssParams $TssSession | ||
|
||
$restrictedParamSet = . $ParameterSetParams 'Enable-TssSecretEmail' 'restricted' | ||
$restrictedParams = @() | ||
foreach ($r in $restrictedParamSet) { | ||
if ($tssParams.ContainsKey($r)) { | ||
$restrictedParams += $r | ||
} | ||
} | ||
} | ||
process { | ||
Write-Verbose "Provided command parameters: $(. $GetInvocation $PSCmdlet.MyInvocation)" | ||
if ($tssParams.ContainsKey('TssSession') -and $TssSession.IsValidSession()) { | ||
. $CheckVersion $TssSession '10.9.000000' $PSCmdlet.MyInvocation | ||
foreach ($secret in $Id) { | ||
$uri = $TssSession.ApiUrl, 'secrets', $secret, 'security-checkout' -join '/' | ||
$invokeParams.Uri = $uri | ||
|
||
# data object for Checkout Settings | ||
$CheckoutBody = @{ | ||
data = @{ | ||
checkOutEnabled = $false | ||
} | ||
} | ||
$invokeParams.Method = 'PATCH' | ||
$invokeParams.Body = $CheckoutBody | ConvertTo-Json | ||
|
||
if ($PSCmdlet.ShouldProcess("SecretId: $secret", "Pre-check out secret for setting Checkout settings")) { | ||
. $CheckOutSecret $TssSession $tssParams $secret | ||
} | ||
|
||
if ($PSCmdlet.ShouldProcess("SecretId: $secret", "$($invokeParams.Method) $($invokeParams.Uri) with:`n$($invokeParams.Body)`n")) { | ||
Write-Verbose "$($invokeParams.Method) $($invokeParams.Uri) with:`n$($invokeParams.Body)`n" | ||
try { | ||
$restResponse = Invoke-TssRestApi @invokeParams | ||
} catch { | ||
Write-Warning "Issue configuring [$secret] Checkout settings" | ||
$err = $_ | ||
. $ErrorHandling $err | ||
} | ||
|
||
if (-not $restResponse.checkOutEnabled.value) { | ||
Write-Verbose "Secret [$secret] Checkout disabled" | ||
} | ||
} | ||
} | ||
} else { | ||
Write-Warning "No valid session found" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
BeforeDiscovery { | ||
$commandName = Split-Path ($PSCommandPath.Replace('.Tests.ps1','')) -Leaf | ||
. ([IO.Path]::Combine([string]$PSScriptRoot, '..', 'constants.ps1')) | ||
} | ||
Describe "$commandName verify parameters" { | ||
BeforeDiscovery { | ||
[object[]]$knownParameters = 'TssSession', 'Id', 'Comment', 'TicketNumber', 'TicketSystemId' | ||
[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 | ||
} | ||
} | ||
} | ||
Describe "$commandName functions" { | ||
Context "Checking" { | ||
BeforeAll { | ||
$session = [pscustomobject]@{ | ||
ApiVersion = 'api/v1' | ||
Take = 2147483647 | ||
SecretServer = 'http://alpha/' | ||
ApiUrl = 'http://alpha/api/v1' | ||
AccessToken = 'AgJf5YLFWtzw2UcBrM1s1KB2BGZ5Ufc4qLZ' | ||
RefreshToken = '9oacYFZZ0YqgBNg0L7VNIF6-Z9ITE51Qplj' | ||
TokenType = 'bearer' | ||
ExpiresIn = 1199 | ||
} | ||
Mock -Verifiable -CommandName Get-TssVersion -MockWith { | ||
return @{ | ||
Version = '10.9.000033' | ||
} | ||
} | ||
|
||
$secretId = Get-Random -Maximum 60 | ||
Mock -Verifiable -CommandName Invoke-TssRestApi -ParameterFilter { $Uri -eq "$($session.ApiUrl)/secrets/$secretId/security-checkout"; $Method -eq 'PATCH' } -MockWith { | ||
return [pscustomobject]@{ | ||
checkOutEnabled = @{fieldInputType = 'String'; sortOrder = ""; value = $false; label = 'Require Check Out'; name = ""; description = 'When enabled a user will need to checkout a Secret before they can access it. Checkout will prevent other users from accessing the Secret while it is checked out.' ; readOnly = ""; placeholder = ""; hidden = $false; hideOnView = ""; hasHistory = ""; isRequired = ""; readOnlyReason = ""; helpLink = ""} | ||
} | ||
} | ||
$object = Disable-TssSecretCheckout -TssSession $session -Id $secretId | ||
Assert-VerifiableMock | ||
} | ||
It "Should be empty" { | ||
$object | Should -BeNullOrEmpty | ||
} | ||
} | ||
} |