Skip to content

Commit

Permalink
ADD Import-PASPlatform
Browse files Browse the repository at this point in the history
New function added.
Import-PASPlatform is able to import a CPM platform.
Available in CyberArk 10.2
  • Loading branch information
pspete committed Mar 16, 2018
1 parent be52689 commit 5a8912d
Show file tree
Hide file tree
Showing 3 changed files with 272 additions and 1 deletion.
171 changes: 171 additions & 0 deletions Tests/Import-PASPlatform.Tests.ps1
@@ -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

}

}

}

}
99 changes: 99 additions & 0 deletions psPAS/Functions/Platforms/Import-PASPlatform.ps1
@@ -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

}
3 changes: 2 additions & 1 deletion psPAS/psPAS.psd1
Expand Up @@ -130,7 +130,8 @@
'Get-PASComponentSummary',
'Get-PASComponentDetail',
'Suspend-PASPSMSession',
'Resume-PASPSMSession'
'Resume-PASPSMSession',
'Import-PASPlatform'
)

AliasesToExport = @(
Expand Down

0 comments on commit 5a8912d

Please sign in to comment.