Skip to content

Commit

Permalink
Set-SecretField - support files via value
Browse files Browse the repository at this point in the history
If content of file provided via value, include FileName
  • Loading branch information
wsmelton committed May 7, 2021
1 parent c6173c8 commit 94f0654
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 12 deletions.
49 changes: 38 additions & 11 deletions src/functions/secrets/Set-SecretField.ps1
Expand Up @@ -32,10 +32,17 @@ function Set-SecretField {
.EXAMPLE
$session = New-TssSession -SecretServer https://alpha -Credential $ssCred
Set-TssSecretField -TssSession $session -Id 42 -Slug attached-file c:\files\attachment.txt
Set-TssSecretField -TssSession $session -Id 42 -Slug attached-file -Path c:\files\attachment.txt
Sets the attached-file field on Secret 42 to the attachment.txt (uploads the file to Secret Server)
.EXAMPLE
$session = New-TssSession -SecretServer https://alpha -Credential $ssCred
$content = Get-Content c:\files\attachment.txt
Set-TssSecretField -TssSession $session -Id 42 -Slug attached-file -Value $content -Filename 'attachment.txt'
Sets the attached-file field on Secret 42 to the contents of the attachment.txt file, providing the appropriate filename desired
.LINK
https://thycotic-ps.github.io/thycotic.secretserver/commands/Set-TssSecretField
Expand All @@ -45,7 +52,7 @@ function Set-SecretField {
.NOTES
Requires TssSession object returned by New-TssSession
#>
[cmdletbinding(SupportsShouldProcess, DefaultParameterSetName = 'all')]
[cmdletbinding(SupportsShouldProcess, DefaultParameterSetName = 'default')]
param(
# TssSession object created by New-TssSession for auth
[Parameter(Mandatory,ValueFromPipeline,Position = 0)]
Expand All @@ -72,7 +79,11 @@ function Set-SecretField {
[switch]
$Clear,

# Path of file to attach
# Filename to assign file contents provided from Value param to the field
[string]
$Filename,

# Path of file to attach to field
[ValidateScript( {
if (Test-Path $_ -PathType Container) {
throw "Path [$_] is a directory, provide full file path"
Expand Down Expand Up @@ -118,28 +129,44 @@ function Set-SecretField {
Write-Verbose "Provided command parameters: $(. $GetInvocation $PSCmdlet.MyInvocation)"
if ($setParams.ContainsKey('TssSession') -and $TssSession.IsValidSession()) {
. $CheckVersion $TssSession '10.9.0000' $PSCmdlet.MyInvocation
foreach ($secret in $Id) {
if ($setParams.ContainsKey('Clear') -and $setParams.ContainsKey('Value')) {
Write-Warning "Clear and Value provided, only one is supported"
return
}

if ($setParams.ContainsKey('Clear') -and $setParams.ContainsKey('Value')) {
Write-Warning "Clear and Value provided, only one is supported"
return
}
if ($setParams.ContainsKey('Filename') -and $setParams.ContainsKey('Path')) {
Write-Warning "Filename and Path provided, only one is supported"
return
}
if ($setParams.ContainsKey('Filename') -and -not $setParams.ContainsKey('Value')) {
Write-Warning "Value must be provided when using Filename"
return
}

foreach ($secret in $Id) {
$fieldBody = @{}
if ($setParams.ContainsKey('Clear')) {
$fieldBody.Add('value',"")
}
if ($setParams.ContainsKey('Value')) {
if ($setParams.ContainsKey('Value') -and -not $setParams.ContainsKey('Filename')) {
$fieldBody.Add('value',$Value)
}

if ($setParams.ContainsKey('Path')) {
$fileName = Split-Path $Path -Leaf
$fieldBody.Add('fileName',$fileName)
$pathFilename = Split-Path $Path -Leaf
$fieldBody.Add('fileName',$pathFilename)

$fileBinary = [IO.File]::ReadAllBytes($Path)
$fieldBody.Add('fileAttachment',$fileBinary)
}

if ($setParams.ContainsKey('Filename') -and $setParams.ContainsKey('Value')) {
$fieldBody.Add('fileName',$Filename)

$fileBinary = [System.Text.Encoding]::UTF8.GetBytes($Value)
$fieldBody.Add('fileAttachment',$fileBinary)
}

if ($restrictedParams.Count -gt 0) {
switch ($setParams.Keys) {
'Comment' { $fieldBody.Add('comment', $Comment) }
Expand Down
2 changes: 1 addition & 1 deletion tests/secrets/Set-SecretField.Tests.ps1
Expand Up @@ -4,7 +4,7 @@ BeforeDiscovery {
}
Describe "$commandName verify parameters" {
BeforeDiscovery {
[object[]]$knownParameters = 'TssSession','Id', 'Slug', 'Value', 'Clear', 'Path', 'Comment', 'ForceCheckIn', 'TicketNumber', 'TicketSystemId'
[object[]]$knownParameters = 'TssSession','Id', 'Slug', 'Value', 'Clear', 'Filename', 'Path', 'Comment', 'ForceCheckIn', '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
Expand Down

0 comments on commit 94f0654

Please sign in to comment.