Skip to content

Commit

Permalink
Merge pull request #176 from pspete/review-customtypes
Browse files Browse the repository at this point in the history
Review customtypes
  • Loading branch information
pspete committed Jun 30, 2019
2 parents 5affd07 + cba4978 commit cfc4faa
Show file tree
Hide file tree
Showing 17 changed files with 349 additions and 329 deletions.
12 changes: 11 additions & 1 deletion CHANGELOG.md
@@ -1,6 +1,6 @@
# psPAS

## **3.0.0** (June 30th 2019)
## **3.0.0** (July 1st 2019)

_2 years since first commit Anniversary Edition_

Expand Down Expand Up @@ -56,6 +56,16 @@ _2 years since first commit Anniversary Edition_
- Added SAML authentication option.
- Added Shared authentication option
- Removed `$SecureMode` & `$AdditionalInfo` parameters.
-`Get-PASPSMConnectionParameter`
- Now saves an RDP file returned from an API request.
- `path` parameter now expects a folder to save the file to.
- Output file is named automatically
- `Export-PASPlatform`
- `path` parameter now expects a folder to save the file to.
- Output file is named automatically
- `Export-PASPSMRecording`
- `path` parameter now expects a folder to save the file to.
- Output file is named automatically
- Fixes
- `New-PASUser`
- Added `ChangePassOnNextLogon` parameter for working with latest API method
Expand Down
17 changes: 17 additions & 0 deletions README.md
Expand Up @@ -533,6 +533,23 @@ secretManagement : @{automaticManagementEnabled=True; lastModifiedTime=155986422
createdTime : 06/06/2019 23:37:02
````

#### Using Methods

Methods present on objects returned from psPAS functions can be leveraged to get the data you need with ease.

- The `psPAS.CyberArk.Vault.Safe` object returned by `Get-PASSafe` has a ScriptMethod (`SafeMembers()`), which will run a query for the members of the safe:

```powershell
#List all safes where AppUser is not a member
Get-PASSafe | Where-Object{ ($_.safemembers() | Select-Object -ExpandProperty UserName) -notcontains "AppUser"}
```

- Retrieved credentials can be immediately converted into Secure Strings:

```powershell
(Get-PASAccount -id 330_5 | Get-PASAccountPassword).ToSecureString()
```

#### API Sessions

- If actions are required to be performed under the context of different user accounts, it is possible to work with different authenticated sessions:
Expand Down
56 changes: 19 additions & 37 deletions Tests/Export-PASPSMRecording.Tests.ps1
Expand Up @@ -57,22 +57,36 @@ Describe $FunctionName {

BeforeEach {

Mock Invoke-PASRestMethod -MockWith { }
Mock Invoke-PASRestMethod -MockWith {
[PSCustomObject]@{
Content = New-Object Byte[] 512
Headers = @{"Content-Disposition" = "attachment; filename=FILENAME.zip" }
}
}

$InputObj = [pscustomobject]@{
"RecordingID" = "SomeID"
"path" = "$env:Temp\test.avi"
"path" = "$env:Temp"

}

Mock Out-PASFile -MockWith { }

}

It "throws if path is invalid" {
{ $InputObj | Export-PASPlatform -PlatformID SomePlatform -path A:\test.avi } | Should throw
{ $InputObj | Export-PASPSMRecording -PlatformID SomePlatform -path A:\test.avi } | Should throw
}

It "throws if InputFile resolves to a folder" {
{ $InputObj | Export-PASPlatform -PlatformID SomePlatform -path $pwd } | Should throw
It "throws if InputFile resolves to a file" {

$InputObj = [pscustomobject]@{
"RecordingID" = "SomeID"
"path" = "$env:Temp\test.avi"

}

{ $InputObj | Export-PASPSMRecording -PlatformID SomePlatform -path $pwd } | Should throw
}

It "sends request" {
Expand Down Expand Up @@ -112,38 +126,6 @@ Describe $FunctionName {

}

Context "Output" {

BeforeEach {

Mock Invoke-PASRestMethod -MockWith {

New-Object Byte[] 512

}

$InputObj = [pscustomobject]@{
"RecordingID" = "SomeID"
"path" = "$env:Temp\test.avi"
}

}

it "saves output file" {
$InputObj | Export-PASPSMRecording
Test-Path "$env:Temp\test.avi" | should Be $true

}

it "reports error saving outputfile" {
Mock Set-Content -MockWith { throw something }
{ $InputObj | Export-PASPSMRecording } | should throw "Error Saving $env:Temp\test.avi"
}



}

}

}
34 changes: 5 additions & 29 deletions Tests/Export-PASPlatform.Tests.ps1
Expand Up @@ -44,6 +44,8 @@ Describe $FunctionName {

}

Mock Out-PASFile -MockWith { }

Context "Mandatory Parameters" {

$Parameters = @{Parameter = 'PlatformID' },
Expand All @@ -67,14 +69,6 @@ Describe $FunctionName {
{ Export-PASPlatform -PlatformID SomePlatform -path A:\test.txt } | Should throw
}

It "throws if InputFile resolves to a folder" {
{ Export-PASPlatform -PlatformID SomePlatform -path $pwd } | Should throw
}

It "throws if InputFile does not have a zip extention" {
{ Export-PASPlatform -PlatformID SomePlatform -path README.MD } | Should throw
}

It "sends request" {

Assert-MockCalled Invoke-PASRestMethod -Scope Describe -Times 1 -Exactly
Expand All @@ -98,29 +92,11 @@ Describe $FunctionName {
}

It "throws error if version requirement not met" {
$Script:ExternalVersion = "1.0"
{ Export-PASPlatform -PlatformID SomePlatform -path "$env:Temp\testExport.zip" } | Should Throw
$Script:ExternalVersion = "0.0"
$Script:ExternalVersion = "1.0"
{ Export-PASPlatform -PlatformID SomePlatform -path "$env:Temp\testExport.zip" } | Should Throw
$Script:ExternalVersion = "0.0"
}


}

Context "Output" {

it "saves output file" {

Test-Path "$env:Temp\testExport.zip" | should Be $true

}

it "reports error saving outputfile" {
Mock Set-Content -MockWith { throw something }
{ Export-PASPlatform -PlatformID SomePlatform -path "$env:Temp\testExport.zip" } | should throw "Error Saving $env:Temp\testExport.zip"
}



}

}
Expand Down
40 changes: 12 additions & 28 deletions Tests/Get-PASPSMConnectionParameter.Tests.ps1
Expand Up @@ -76,6 +76,12 @@ Describe $FunctionName {
$Script:BaseURI = "https://SomeURL/SomeApp"
$Script:ExternalVersion = "0.0"
$Script:WebSession = New-Object Microsoft.PowerShell.Commands.WebRequestSession

Mock Out-PASFile -MockWith { }
}

It "throws if path is invalid" {
{ $InputObj | Get-PASPSMConnectionParameter -ConnectionMethod RDP -path A:\test.txt } | Should throw
}

It "sends request" {
Expand Down Expand Up @@ -134,7 +140,7 @@ Describe $FunctionName {

Assert-MockCalled Invoke-PASRestMethod -ParameterFilter {

$WebSession.Headers["Accept"] -eq 'application/json' } -Times 1 -Exactly -Scope It
$WebSession.Headers["Accept"] -eq 'application/octet-stream' } -Times 1 -Exactly -Scope It

}

Expand Down Expand Up @@ -182,9 +188,10 @@ Describe $FunctionName {

Context "Output" {


BeforeEach {
Mock Invoke-PASRestMethod -MockWith {
[PSCustomObject]@{"Prop1" = "VAL1"; "Prop2" = "Val2"; "Prop3" = "Val3" }
[PSCustomObject]@{"PSMGWRequest" = "VAL1"; "PSMGWURL" = "Val2"; "Prop3" = "Val3" }
}

$InputObj = [pscustomobject]@{
Expand All @@ -194,40 +201,17 @@ Describe $FunctionName {

}

$AdHocObj = [pscustomobject]@{
"ConnectionComponent" = "SomeConnectionComponent"
"UserName" = "SomeUser"
"secret" = "SomeSecret" | ConvertTo-SecureString -AsPlainText -Force
"address" = "Some.Address"
"platformID" = "SomePlatform"

}

$Script:BaseURI = "https://SomeURL/SomeApp"
$Script:ExternalVersion = "0.0"
$Script:WebSession = New-Object Microsoft.PowerShell.Commands.WebRequestSession
}

it "provides output" {

$InputObj | Get-PASPSMConnectionParameter -ConnectionMethod RDP | Should not BeNullOrEmpty

}

It "has output with expected number of properties" {

($InputObj | Get-PASPSMConnectionParameter -ConnectionMethod RDP | Get-Member -MemberType NoteProperty).length | Should Be 3

Mock Out-PASFile -MockWith { }
}

it "outputs object with expected typename" {

$InputObj | Get-PASPSMConnectionParameter -ConnectionMethod RDP | get-member | select-object -expandproperty typename -Unique | Should Be psPAS.CyberArk.Vault.PSM.Connection.RDP

It "outputs PSMGW connection information" {
$InputObj | Get-PASPSMConnectionParameter -ConnectionMethod PSMGW | Should -Not -Be Null
}



}

}
Expand Down
53 changes: 4 additions & 49 deletions Tests/Get-PASResponse.Tests.ps1
Expand Up @@ -64,12 +64,12 @@ Describe $FunctionName {

$ApplicationSave = New-MockObject -Type Microsoft.PowerShell.Commands.WebResponseObject
$ApplicationSave | Add-Member -MemberType NoteProperty -Name StatusCode -Value 200 -Force
$ApplicationSave | Add-Member -MemberType NoteProperty -Name Headers -Value @{ "Content-Type" = 'application/save' } -Force
$ApplicationSave | Add-Member -MemberType NoteProperty -Name Headers -Value @{ "Content-Type" = 'application/save' ; "Content-Disposition" = "attachment; filename=FILENAME.zip" } -Force
$ApplicationSave | Add-Member -MemberType NoteProperty -Name Content -Value $([System.Text.Encoding]::Ascii.GetBytes("Expected")) -Force

$OctetStream = New-MockObject -Type Microsoft.PowerShell.Commands.WebResponseObject
$OctetStream | Add-Member -MemberType NoteProperty -Name StatusCode -Value 200 -Force
$OctetStream | Add-Member -MemberType NoteProperty -Name Headers -Value @{ "Content-Type" = 'application/octet-stream' } -Force
$OctetStream | Add-Member -MemberType NoteProperty -Name Headers -Value @{ "Content-Type" = 'application/octet-stream' ; "Content-Disposition" = "attachment; filename=FILENAME.zip" } -Force
$OctetStream | Add-Member -MemberType NoteProperty -Name Content -Value $([System.Text.Encoding]::Ascii.GetBytes("Expected")) -Force

}
Expand All @@ -89,57 +89,12 @@ Describe $FunctionName {

It "returns expected application-save value" {
$result = Get-PASResponse -APIResponse $ApplicationSave
$([System.Text.Encoding]::ASCII.GetString($result)) | Should Be "Expected"
$([System.Text.Encoding]::ASCII.GetString($result.Content)) | Should Be "Expected"
}

It "returns expected octet-stream value" {
$result = Get-PASResponse -APIResponse $OctetStream
$([System.Text.Encoding]::ASCII.GetString($result)) | Should Be "Expected"
}

}

Context New-PASSession {

BeforeEach {

Mock Get-ParentFunction -MockWith {

[PSCustomObject]@{
FunctionName = "New-PASSession"
}

}

$RandomString = "ZDE0YTY3MzYtNTk5Ni00YjFiLWFhMWUtYjVjMGFhNjM5MmJiOzY0MjY0NkYyRkE1NjY3N0M7MDAwMDAwMDI4ODY3MDkxRDUzMjE3NjcxM0ZBODM2REZGQTA2MTQ5NkFCRTdEQTAzNzQ1Q0JDNkRBQ0Q0NkRBMzRCODcwNjA0MDAwMDAwMDA7"

$ClassicToken = New-MockObject -Type Microsoft.PowerShell.Commands.WebResponseObject
$ClassicToken | Add-Member -MemberType NoteProperty -Name StatusCode -Value 200 -Force
$ClassicToken | Add-Member -MemberType NoteProperty -Name Headers -Value @{ "Content-Type" = 'application/json; charset=utf-8' } -Force
$ClassicToken | Add-Member -MemberType NoteProperty -Name Content -Value $([PSCustomObject]@{CyberArkLogonResult = $RandomString } | ConvertTo-Json) -Force

$V10Token = New-MockObject -Type Microsoft.PowerShell.Commands.WebResponseObject
$V10Token | Add-Member -MemberType NoteProperty -Name StatusCode -Value 200 -Force
$V10Token | Add-Member -MemberType NoteProperty -Name Headers -Value @{ "Content-Type" = 'application/json; charset=utf-8' } -Force
$V10Token | Add-Member -MemberType NoteProperty -Name Content -Value $($RandomString | ConvertTo-Json) -Force

$SharedToken = New-MockObject -Type Microsoft.PowerShell.Commands.WebResponseObject
$SharedToken | Add-Member -MemberType NoteProperty -Name StatusCode -Value 200 -Force
$SharedToken | Add-Member -MemberType NoteProperty -Name Headers -Value @{ "Content-Type" = 'application/json; charset=utf-8' } -Force
$SharedToken | Add-Member -MemberType NoteProperty -Name Content -Value $([PSCustomObject]@{LogonResult = $RandomString } | ConvertTo-Json) -Force

}

It "returns expected Classic API Logon Token" {
Get-PASResponse -APIResponse $ClassicToken | Select-Object -ExpandProperty CyberArkLogonResult | Should Be $RandomString
}

It "returns expected V10 API Logon Token" {
Get-PASResponse -APIResponse $V10Token | Select-Object -ExpandProperty CyberArkLogonResult | Should Be $RandomString
}

It "returns expected Shared Authentication Logon Token" {
Get-PASResponse -APIResponse $SharedToken | Select-Object -ExpandProperty CyberArkLogonResult | Should Be $RandomString
$([System.Text.Encoding]::ASCII.GetString($result.Content)) | Should Be "Expected"
}

}
Expand Down
15 changes: 15 additions & 0 deletions Tests/New-PASSession.Tests.ps1
Expand Up @@ -163,6 +163,15 @@ Describe $FunctionName {

It "sends request to expected v10 URL for CyberArk Authentication" {

$RandomString = "ZDE0YTY3MzYtNTk5Ni00YjFiLWFhMWUtYjVjMGFhNjM5MmJiOzY0MjY0NkYyRkE1NjY3N0M7MDAwMDAwMDI4ODY3MDkxRDUzMjE3NjcxM0ZBODM2REZGQTA2MTQ5NkFCRTdEQTAzNzQ1Q0JDNkRBQ0Q0NkRBMzRCODcwNjA0MDAwMDAwMDA7"


Mock Invoke-PASRestMethod -MockWith {

$RandomString

}

$Credentials | New-PASSession -BaseURI "https://P_URI" -type CyberArk
Assert-MockCalled Invoke-PASRestMethod -ParameterFilter {

Expand Down Expand Up @@ -242,6 +251,12 @@ Describe $FunctionName {

It "sends request to expected URL for Shared Authentication" {

Mock Invoke-PASRestMethod -MockWith {
[PSCustomObject]@{
"LogonResult" = "AAAAAAA\\\REEEAAAAALLLLYYYYY\\\\LOOOOONNNNGGGGG\\\ACCCCCEEEEEEEESSSSSSS\\\\\\TTTTTOOOOOKKKKKEEEEEN"
}
}

New-PASSession -BaseURI "https://P_URI" -UseSharedAuthentication

Assert-MockCalled Invoke-PASRestMethod -ParameterFilter {
Expand Down

0 comments on commit cfc4faa

Please sign in to comment.