From 06accc642faf825181665d05e123bfd3d050aef6 Mon Sep 17 00:00:00 2001 From: Shawn Melton <11204251+wsmelton@users.noreply.github.com> Date: Sun, 7 Feb 2021 22:06:21 -0600 Subject: [PATCH] New-TssFolder - closes #65 --- src/functions/folders/New-Folder.ps1 | 118 ++++++++++++++++++++++++++ tests/folders/New-TssFolder.Tests.ps1 | 97 +++++++++++++++++++++ 2 files changed, 215 insertions(+) create mode 100644 src/functions/folders/New-Folder.ps1 create mode 100644 tests/folders/New-TssFolder.Tests.ps1 diff --git a/src/functions/folders/New-Folder.ps1 b/src/functions/folders/New-Folder.ps1 new file mode 100644 index 00000000..e5b27b76 --- /dev/null +++ b/src/functions/folders/New-Folder.ps1 @@ -0,0 +1,118 @@ +function New-Folder { + <# + .SYNOPSIS + Create a new folde + + .DESCRIPTION + Create a new folder + + .EXAMPLE + $session = New-TssSession -SecretServer https://alpha -Credential $ssCred + $folderStub = Get-TssFolderStub -TssSession $session + New-TssFolder -TssSession $session -FolderStub $folderStub -FolderName 'tssNewFolder' -ParentFolderId -1 + + Creates a folder named "tssNewFolder" at the root of Secret Server application + + .EXAMPLE + $session = New-TssSession -SecretServer https://alpha -Credential $ssCred + $folderStub = Get-TssFolderStub -TssSession $session + New-TssFolder -TssSession $session -FolderStub $folderStub -FolderName 'IT Dept' -ParentFolderId 27 -InheritPermissions:$false + + Creates a folder named "IT Dept" under parent folder 27 with Inherit Permissins disabled (set to No if viewed in the UI) + + .EXAMPLE + $session = New-TssSession -SecretServer https://alpha -Credential $ssCred + Get-TssFolderStub -TssSession $session | New-TssFolder -TssSession $session -FolderName 'Marketing Dept' -ParentFolderId 27 -InheritPermissions -InheritSecretPolicy + + Creates a folder named "Marketing Dept" under parent folder 27 with inheritance enabled for Permissions and Secret Policy + + .LINK + https://thycotic.secretserver.github.io/commands/New-TssSecret + + .NOTES + Requires TssSession object returned by New-TssSession + #> + [CmdletBinding(SupportsShouldProcess)] + [OutputType('TssSecret')] + param ( + # TssSession object created by New-TssSession for auth + [Parameter(Mandatory, + ValueFromPipeline, + Position = 0)] + [TssSession]$TssSession, + + # Input object obtained via Get-TssFolderStub + [Parameter(Mandatory, Position = 1, ValueFromPipeline)] + [TssFolder] + $FolderStub, + + # Folder Name + [Parameter(Mandatory)] + [string] + $FolderName, + + # Parent Folder ID, use -1 to create root folder + [Parameter(Mandatory)] + [Alias('ParentFolder')] + [int] + $ParentFolderId, + + # Secret Policy ID + [Alias('SecretPolicy')] + [int] + $SecretPolicyId, + + # Inherit Permissions + [switch] + $InheritPermissions, + + # Inherit Secret Policy + [switch] + $InheritSecretPolicy + ) + + begin { + $tssParams = $PSBoundParameters + $invokeParams = @{ } + } + + process { + Write-Verbose "Provided command parameters: $(. $GetInvocation $PSCmdlet.MyInvocation)" + if ($tssParams.ContainsKey('TssSession') -and $TssSession.IsValidSession()) { + $restResponse = $null + $uri = $TssSession.ApiUrl, 'folders' -join '/' + $invokeParams.Uri = $uri + $invokeParams.Method = 'POST' + + $FolderStub.FolderName = $FolderName + $FolderStub.ParentFolderId = $ParentFolderId + + if ($tssParams.ContainsKey('SecretPolicyId')) { + $FolderStub.SecretPolicyId = $SecretPolicyId + } + if ($tssParams.ContainsKey('InheritPermissions')) { + $FolderStub.InheritPermissions = $InheritPermissions + } + if ($tssParams.ContainsKey('InheritSecretPolicy')) { + $FolderStub.InheritSecretPolicy = $InheritSecretPolicy + } + + $invokeParams.Body = ($FolderStub | ConvertTo-Json) + $invokeParams.PersonalAccessToken = $TssSession.AccessToken + Write-Verbose "$($invokeParams.Method) $uri with:`n $FolderStub" + if (-not $PSCmdlet.ShouldProcess($FolderStub.FolderName, "$($invokeParams.Method) $uri with $($invokeParams.Body)")) { return } + try { + $restResponse = Invoke-TssRestApi @invokeParams + } catch { + Write-Warning "Issue creating folder [$ReportName]" + $err = $_.ErrorDetails.Message + Write-Error $err + } + if ($restResponse) { + . $TssFolderObject $restResponse + } + } else { + Write-Warning "No valid session found" + } + } +} \ No newline at end of file diff --git a/tests/folders/New-TssFolder.Tests.ps1 b/tests/folders/New-TssFolder.Tests.ps1 new file mode 100644 index 00000000..a3dfd6a9 --- /dev/null +++ b/tests/folders/New-TssFolder.Tests.ps1 @@ -0,0 +1,97 @@ +BeforeDiscovery { + $commandName = Split-Path ($PSCommandPath.Replace('.Tests.ps1','')) -Leaf + . ([IO.Path]::Combine([string]$PSScriptRoot, '..', 'constants.ps1')) +} +Describe "$commandName verify parameters" { + BeforeDiscovery { + [object[]]$knownParameters = 'TssSession','FolderStub', 'FolderName', 'ParentFolderId', 'SecretPolicyId', 'InheritPermissions', 'InheritSecretPolicy' + [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 parmaeters" -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 TssSecret" -TestCases $commandDetails { + $_.OutputType.Name | Should -Be 'TssSecret' + } + } +} +Describe "$commandName works" { + BeforeDiscovery { + $session = New-TssSession -SecretServer $ss -Credential $ssCred + $invokeParams = @{ + Uri = "$ss/api/v1/folders?take=$($session.take)" + ExpandProperty = 'records' + PersonalAccessToken = $session.AccessToken + } + $getFolders = Invoke-TssRestApi @invokeParams + $tssSecretFolder = $getFolders.Where({$_.FolderPath -eq '\tss_module_testing\NewFolder'}) + + $stub = Get-TssFolderStub -TssSession $session + + $testCase = [pscustomobject]@{ + FolderName = "tssNewFolder$(Get-Random)" + ParentFolder = $tssSecretFolder.Id + InheritPermissions = $true + } + + $newParams = @{ + TssSession = $session + FolderStub = $stub + FolderName = $testCase.FolderName + ParentFolderId = $testCase.ParentFolder + InheritPermissions = $testCase.InheritPermissions + } + $newFolder = New-TssFolder @newParams + $createdFolder = Get-TssFolder -TssSession $session -Id $newFolder.Id + + $session.SessionExpire() + $props = 'FolderId', 'FolderName', 'ParentFolderId', 'InheritPermissions' + } + Context "Checking" -ForEach @{newFolder = $newFolder} { + It "Should not be empty" { + $newFolder | Should -Not -BeNullOrEmpty + } + It "Should output <_> property" -TestCases $props { + $newFolder.PSObject.Properties.Name | Should -Contain $_ + } + } + Context "Validate created folder" -Foreach @{createdFolder = $createdFolder} { + AfterAll { + $session = New-TssSession -SecretServer $ss -Credential $ssCred + + $invokeParams = @{ + Uri = "$ss/api/v1/folders/$($createdFolder.FolderId)" + Method = 'DELETE' + PersonalAccessToken = $session.AccessToken + } + $deletedFolder = Invoke-TssRestApi @invokeParams + if ($deletedFolder.id -ne $createdFolder.FolderId) { + Write-Host "Unable to properly delete created test folder" + } + $session.SessionExpire() + } + It "Should not be empty" { + $createdFolder | Should -Not -BeNullOrEmpty + } + It "Should have set FolderName to <_.FolderName>" -TestCases $testCase { + $createdFolder.FolderName | Should -Be $_.FolderName + } + It "Should not have zero for FolderId" -TestCases $testCase { + $createdFolder.FolderId | Should -Not -Be 0 + } + It "Should have ParentFolderId set to <_.ParentFolder>" -TestCases $testCase { + $createdFolder.ParentFolderId | Should -Be $_.ParentFolder + } + It "Should have InheritPermissions set to <_.InheritPermissions>" -TestCases $testCase { + $createdFolder.InheritPermissions | Should -Be $_.InheritPermissions + } + } +} \ No newline at end of file