Skip to content

Commit

Permalink
Add IA Commands to PowerShell Core Version
Browse files Browse the repository at this point in the history
  • Loading branch information
tkoopman committed Sep 30, 2017
1 parent fc2dcf3 commit cb0d08a
Show file tree
Hide file tree
Showing 11 changed files with 65 additions and 25 deletions.
6 changes: 6 additions & 0 deletions Pester.Tests/Invoke-Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ $secpasswd = ConvertTo-SecureString $Settings.Management.Password -AsPlainText -
$mycreds = New-Object System.Management.Automation.PSCredential ($Settings.Management.User, $secpasswd)
Set-Variable -Scope Global -Name Session -Description "psCheckPoint Pester Session" -Option readonly -Value $(Open-CheckPointSession -NoCertificateValidation -ManagementServer $Settings.Management.Server -Credentials $mycreds -SessionName Pester -SessionDescription "psCheckPoint Pester Testing")

# Launch PowerShell Core Tests
if ($PSEdition -eq 'Desktop' -and $(Test-Path $Settings.psCore)) {
Write-Host "Starting PowerShell Core Tests"
& 'cmd' "/c start ""PowerShell Core Tests"" ""$($Settings.psCore)"" -NoExit -Command ..\..\Invoke-Tests.ps1"
}

# Run the tests. By default all tests matching *.Tests.ps1 will be executed.
# See https://github.com/pester/Pester for more information.
Invoke-Pester
Expand Down
3 changes: 2 additions & 1 deletion Pester.Tests/Pester.Settings.json.Example
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@
"IAGateway": {
"Server": "192.168.1.1",
"SharedSecret": "password"
}
},
"psCore": "C:\\Program Files\\PowerShell\\6.0.0-beta.7\\powershell.exe"
}
4 changes: 4 additions & 0 deletions Pester.Tests/Pester.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
<None Include="Pester.Settings.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="README.md" />
<None Include="Start-Debug.ps1" />
<None Include="Post-Build.ps1" />
<Content Include="Tests\Session.Tests.ps1">
Expand All @@ -63,6 +64,9 @@
<Content Include="Tests\Application.Tests.ps1">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="Tests\IA.Tests.ps1">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
<ItemGroup>
<Compile Include="Properties\AssemblyInfo.cs" />
Expand Down
13 changes: 13 additions & 0 deletions Pester.Tests/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# psCheckPoint Tests

## Testing Management Server & Gateway

You will need a Check Point management server and gateway for running these tests against. It is recommened you stand up a All-in-One management server & gateway on evaluation licenses for running these tests against.

A default install is almost enough. Just need to create an Access Role called "PesterRole" for the IA tests to be run against. Make sure you install policy after creating the role so the Gateway knows about it.

## Config file

You must create the config file Pester.Settings.json which should contain the details of your testing management server and gateway. This file is included in the .gitignore file so it will not get pushed to Github.

You can copy the example configuration Pester.Settings.json.Example to create your config file.
11 changes: 11 additions & 0 deletions Pester.Tests/Tests/IA.Tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Describe "Basic.IA" {
It "Add Identity" {
$(Add-CheckPointIdentity -Gateway $Settings.IAGateway.Server -SharedSecret $Settings.IAGateway.SharedSecret -NoCertificateValidation -IPAddress 192.168.1.2 -NoFetchUserGroups -NoFetchMachineGroups -NoCalculateRoles -Machine "PesterMachine" -Roles "PesterRole")[0] | Should BeOfType psCheckPoint.IA.AddIdentityResponse
}
It "Get Identity" {
$(Get-CheckPointIdentity -Gateway $Settings.IAGateway.Server -SharedSecret $Settings.IAGateway.SharedSecret -NoCertificateValidation -IPAddress 192.168.1.2)[0] | Should BeOfType psCheckPoint.IA.GetIdentityResponse
}
It "Remove Identity" {
$(Remove-CheckPointIdentity -Gateway $Settings.IAGateway.Server -SharedSecret $Settings.IAGateway.SharedSecret -NoCertificateValidation -IPAddress 192.168.1.2)[0] | Should BeOfType psCheckPoint.IA.RemoveIdentityResponse
}
}
5 changes: 3 additions & 2 deletions psCheckPoint.Core/psCheckPoint.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<OutputPath>bin\Debug\netcoreapp1.1\</OutputPath>
<OutputPath>bin\Debug\</OutputPath>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
Expand All @@ -22,6 +22,7 @@
<ItemGroup>
<Compile Include="..\psCheckPoint\*.cs" Link="%(FileName)" />
<Compile Include="..\psCheckPoint\Session\*.cs" Link="Session\%(FileName)" />
<Compile Include="..\psCheckPoint\IA\*.cs" Link="IA\%(FileName)" />
<Compile Include="..\psCheckPoint\Objects\*.cs" Link="Objects\%(FileName)" />
<Compile Include="..\psCheckPoint\Objects\AccessLayer\*.cs" Link="Objects\AccessLayer\%(FileName)" />
<Compile Include="..\psCheckPoint\Objects\AccessRule\*.cs" Link="Objects\AccessRule\%(FileName)" />
Expand All @@ -48,6 +49,6 @@
</ItemGroup>

<Target Name="PostBuild" AfterTargets="PostBuildEvent">
<Exec Command="copy $(OutDir)\psCheckPoint.Core.dll $(SolutionDir)\psCheckPoint\bin\$(ConfigurationName)\" />
<Exec Command="copy $(OutDir)\psCheckPoint.Core.dll $(SolutionDir)\psCheckPoint\bin\$(ConfigurationName)\&#xD;&#xA;copy $(OutDir)\psCheckPoint.Core.dll $(SolutionDir)\Pester.Tests\bin\$(ConfigurationName)\" />
</Target>
</Project>
2 changes: 1 addition & 1 deletion psCheckPoint/IA/AddCheckPointIdentity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public string[] Roles
protected override void BeginProcessing()
{
base.BeginProcessing();
batch = new Batch<Identity, AddIdentityResponse>(Gateway, SharedSecret, "add-identity");
batch = new Batch<Identity, AddIdentityResponse>(Gateway, SharedSecret, "add-identity", NoCertificateValidation.IsPresent);
}

/// <summary>
Expand Down
25 changes: 22 additions & 3 deletions psCheckPoint/IA/Batch.cs
Original file line number Diff line number Diff line change
@@ -1,25 +1,44 @@
using Newtonsoft.Json;
using psCheckPoint;
using System;
using System.Collections.Generic;
using System.Management.Automation;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Net.Security;
using System.Text;

namespace psCheckPoint.IA
{
[JsonObject(MemberSerialization.OptIn)]
internal class Batch<RequestType, ResponseType> : IDisposable
{
private HttpClient client = new HttpClient();
private HttpClient client;

public Batch(string gateway, string sharedSecret, string command)
public Batch(string gateway, string sharedSecret, string command, bool NoCertificateValidation)
{
Gateway = gateway;
SharedSecret = sharedSecret;
Command = command;

HttpClientHandler handler = new HttpClientHandler();
#if NETCOREAPP1_1
if (NoCertificateValidation)
{
handler.ServerCertificateCustomValidationCallback = (message, cert, chain, errors) => { return true; };
}
#else
if (NoCertificateValidation)
{
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(delegate { return true; });
}
else
{
ServicePointManager.ServerCertificateValidationCallback = null;
}
#endif

client = new HttpClient(handler);
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
}

Expand Down
15 changes: 0 additions & 15 deletions psCheckPoint/IA/CheckPointIACmdlet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,5 @@ public abstract class CheckPointIACmdlet : PSCmdlet
/// </summary>
[Parameter]
public SwitchParameter NoCertificateValidation { get; set; }

/// <summary>
/// Provides a one-time, preprocessing functionality for the cmdlet.
/// </summary>
protected override void BeginProcessing()
{
if (NoCertificateValidation.IsPresent)
{
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(delegate { return true; });
}
else
{
ServicePointManager.ServerCertificateValidationCallback = null;
}
}
}
}
4 changes: 2 additions & 2 deletions psCheckPoint/IA/GetCheckPointIdentity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace psCheckPoint.IA
/// <code>Get-CheckPointIdentity -Gateway 192.168.1.1 -SharedSecret *** -NoCertificateValidation -IPAddress 192.168.1.2</code>
/// </example>
[Cmdlet(VerbsCommon.Get, "CheckPointIdentity")]
[OutputType(typeof(AddIdentityResponse))]
[OutputType(typeof(GetIdentityResponse))]
public class GetCheckPointIdentity : CheckPointIACmdlet
{
/// <summary>
Expand All @@ -29,7 +29,7 @@ public class GetCheckPointIdentity : CheckPointIACmdlet
protected override void BeginProcessing()
{
base.BeginProcessing();
batch = new Batch<GetIdentity, GetIdentityResponse>(Gateway, SharedSecret, "show-identity");
batch = new Batch<GetIdentity, GetIdentityResponse>(Gateway, SharedSecret, "show-identity", NoCertificateValidation.IsPresent);
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion psCheckPoint/IA/RemoveCheckPointIdentity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public class RemoveCheckPointIdentity : CheckPointIACmdlet
protected override void BeginProcessing()
{
base.BeginProcessing();
batch = new Batch<RemoveIdentity, RemoveIdentityResponse>(Gateway, SharedSecret, "delete-identity");
batch = new Batch<RemoveIdentity, RemoveIdentityResponse>(Gateway, SharedSecret, "delete-identity", NoCertificateValidation.IsPresent);
}

/// <summary>
Expand Down

0 comments on commit cb0d08a

Please sign in to comment.