Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New function added. Import-PASPlatform is able to import a CPM platform. Available in CyberArk 10.2
- Loading branch information
Showing
3 changed files
with
272 additions
and
1 deletion.
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,171 @@ | ||
#Get Current Directory | ||
$Here = Split-Path -Parent $MyInvocation.MyCommand.Path | ||
|
||
#Get Function Name | ||
$FunctionName = (Split-Path -Leaf $MyInvocation.MyCommand.Path) -Replace ".Tests.ps1" | ||
|
||
#Assume ModuleName from Repository Root folder | ||
$ModuleName = Split-Path (Split-Path $Here -Parent) -Leaf | ||
|
||
#Resolve Path to Module Directory | ||
$ModulePath = Resolve-Path "$Here\..\$ModuleName" | ||
|
||
#Define Path to Module Manifest | ||
$ManifestPath = Join-Path "$ModulePath" "$ModuleName.psd1" | ||
|
||
if( -not (Get-Module -Name $ModuleName -All)) { | ||
|
||
Import-Module -Name "$ManifestPath" -Force -ErrorAction Stop | ||
|
||
} | ||
|
||
BeforeAll { | ||
|
||
$Script:RequestBody = $null | ||
|
||
} | ||
|
||
AfterAll { | ||
|
||
$Script:RequestBody = $null | ||
|
||
} | ||
|
||
Describe $FunctionName { | ||
|
||
InModuleScope $ModuleName { | ||
|
||
Mock Invoke-PASRestMethod -MockWith { | ||
[PSCustomObject]@{"PlatformID" = "SomePlatform"} | ||
} | ||
|
||
$InputObj = [pscustomobject]@{ | ||
"sessionToken" = @{"Authorization" = "P_AuthValue"} | ||
"WebSession" = New-Object Microsoft.PowerShell.Commands.WebRequestSession | ||
"BaseURI" = "https://P_URI" | ||
"PVWAAppName" = "P_App" | ||
} | ||
|
||
#Create a 512b file to test with | ||
$file = [System.IO.File]::Create("$env:Temp\testPlatform.zip") | ||
$file.SetLength(0.5kb) | ||
$file.Close() | ||
|
||
Context "Mandatory Parameters" { | ||
|
||
$Parameters = @{Parameter = 'BaseURI'}, | ||
@{Parameter = 'SessionToken'}, | ||
@{Parameter = 'ImportFile'} | ||
|
||
It "specifies parameter <Parameter> as mandatory" -TestCases $Parameters { | ||
|
||
param($Parameter) | ||
|
||
(Get-Command Import-PASPlatform).Parameters["$Parameter"].Attributes.Mandatory | Should Be $true | ||
|
||
} | ||
|
||
} | ||
|
||
$response = $InputObj | Import-PASPlatform -ImportFile $($file.name) | ||
|
||
Context "Input" { | ||
|
||
It "throws if InputFile does not exist" { | ||
{$InputObj | Import-PASPlatform -ImportFile SomeFile.txt} | Should throw | ||
} | ||
|
||
It "throws if InputFile resolves to a folder" { | ||
{$InputObj | Import-PASPlatform -ImportFile $pwd} | Should throw | ||
} | ||
|
||
It "throws if InputFile does not have a zip extention" { | ||
{$InputObj | Import-PASPlatform -ImportFile README.MD} | Should throw | ||
} | ||
|
||
It "sends request" { | ||
|
||
Assert-MockCalled Invoke-PASRestMethod -Times 1 -Exactly -Scope Describe | ||
|
||
} | ||
|
||
It "sends request to expected endpoint" { | ||
|
||
Assert-MockCalled Invoke-PASRestMethod -ParameterFilter { | ||
|
||
$URI -eq "$($InputObj.BaseURI)/$($InputObj.PVWAAppName)/API/Platforms/Import" | ||
|
||
} -Times 1 -Exactly -Scope Describe | ||
|
||
} | ||
|
||
It "uses expected method" { | ||
|
||
Assert-MockCalled Invoke-PASRestMethod -ParameterFilter {$Method -match 'POST' } -Times 1 -Exactly -Scope Describe | ||
|
||
} | ||
|
||
It "sends request with expected body" { | ||
|
||
Assert-MockCalled Invoke-PASRestMethod -ParameterFilter { | ||
|
||
$Script:RequestBody = $Body | ConvertFrom-Json | ||
|
||
($Script:RequestBody.ImportFile) -ne $null | ||
|
||
} -Times 1 -Exactly -Scope Describe | ||
|
||
} | ||
|
||
It "has a request body with expected number of properties" { | ||
|
||
($Script:RequestBody | Get-Member -MemberType NoteProperty).length | Should Be 1 | ||
|
||
} | ||
|
||
It "has body content of expected length" { | ||
|
||
($Script:RequestBody.ImportFile).length | Should Be 512 | ||
|
||
} | ||
|
||
|
||
} | ||
|
||
Context "Output" { | ||
|
||
it "provides output" { | ||
|
||
$response | Should not BeNullOrEmpty | ||
|
||
} | ||
|
||
It "has output with expected number of properties" { | ||
|
||
($response | Get-Member -MemberType NoteProperty).length | Should Be 1 | ||
|
||
} | ||
|
||
it "outputs object with expected typename" { | ||
|
||
$response | get-member | select-object -expandproperty typename -Unique | Should Be System.Management.Automation.PSCustomObject | ||
|
||
} | ||
|
||
$DefaultProps = @{Property = 'sessionToken'}, | ||
@{Property = 'WebSession'}, | ||
@{Property = 'BaseURI'}, | ||
@{Property = 'PVWAAppName'} | ||
|
||
It "does not return default property <Property> in response" -TestCases $DefaultProps { | ||
param($Property) | ||
|
||
$response.$Property | Should Be $null | ||
|
||
} | ||
|
||
} | ||
|
||
} | ||
|
||
} |
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,99 @@ | ||
function Import-PASPlatform { | ||
<# | ||
.SYNOPSIS | ||
Import a new platform | ||
.DESCRIPTION | ||
Import a new CPM platform. | ||
.PARAMETER ImportFile | ||
The zip file that contains the platform. | ||
.PARAMETER sessionToken | ||
Hashtable containing the session token returned from New-PASSession | ||
.PARAMETER WebSession | ||
WebRequestSession object returned from New-PASSession | ||
.PARAMETER BaseURI | ||
PVWA Web Address | ||
Do not include "/PasswordVault/" | ||
.PARAMETER PVWAAppName | ||
The name of the CyberArk PVWA Virtual Directory. | ||
Defaults to PasswordVault | ||
.EXAMPLE | ||
$token | Import-PASPlatform -ImportFile CustomApp.zip | ||
Imports CustomApp.zip Platform package | ||
.INPUTS | ||
SessionToken, ImportFile, WebSession & BaseURI can be piped by property name | ||
.OUTPUTS | ||
None | ||
.NOTES | ||
Minimum CyberArk version 10.2 | ||
#> | ||
[CmdletBinding(SupportsShouldProcess)] | ||
param( | ||
[parameter( | ||
Mandatory = $true, | ||
ValueFromPipelinebyPropertyName = $true | ||
)] | ||
[ValidateNotNullOrEmpty()] | ||
[ValidateScript( { Test-Path -Path $_ -PathType Leaf})] | ||
[ValidatePattern( '\.zip$' )] | ||
[string]$ImportFile, | ||
|
||
[parameter( | ||
Mandatory = $true, | ||
ValueFromPipelinebyPropertyName = $true | ||
)] | ||
[ValidateNotNullOrEmpty()] | ||
[hashtable]$SessionToken, | ||
|
||
[parameter( | ||
ValueFromPipelinebyPropertyName = $true | ||
)] | ||
[Microsoft.PowerShell.Commands.WebRequestSession]$WebSession, | ||
|
||
[parameter( | ||
Mandatory = $true, | ||
ValueFromPipelinebyPropertyName = $true | ||
)] | ||
[string]$BaseURI, | ||
|
||
[parameter( | ||
Mandatory = $false, | ||
ValueFromPipelinebyPropertyName = $true | ||
)] | ||
[string]$PVWAAppName = "PasswordVault" | ||
) | ||
|
||
BEGIN {}#begin | ||
|
||
PROCESS { | ||
|
||
#Create URL for request | ||
$URI = "$baseURI/$PVWAAppName/API/Platforms/Import" | ||
|
||
$FileBytes = [System.IO.File]::ReadAllBytes($ImportFile) | ||
|
||
$Body = @{"ImportFile" = $FileBytes} | ConvertTo-Json | ||
|
||
if($PSCmdlet.ShouldProcess($ImportFile, "Imports Platform Package")) { | ||
|
||
#send request to web service | ||
Invoke-PASRestMethod -Uri $URI -Method POST -Body $Body -Headers $SessionToken -WebSession $WebSession | ||
|
||
} | ||
|
||
}#process | ||
|
||
END {}#end | ||
|
||
} |
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