Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add (Get and Add) Device Fingerprint cmdlet #53

Merged
merged 15 commits into from
Apr 18, 2021
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
165 changes: 165 additions & 0 deletions PowerArubaCP/Public/DeviceFingerprint.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
#
# Copyright 2021, Alexis La Goutte <alexis.lagoutte at gmail dot com>
#
# SPDX-License-Identifier: Apache-2.0
#

function Add-ArubaCPDeviceFingerprint {

<#
.SYNOPSIS
Add a Device Fingerprint on ClearPass

.DESCRIPTION
Add a Device Fingerprint with mac address, hostname, ip (address) and device info (Category, Name, Family)

.EXAMPLE
Add-ArubaCPDeviceFingerprint -mac_address 000102030405 -hostname "My PowerArubaCP Device Fingerprint"

Add a Device Fingerprint with MAC Address 000102030405 and a hostname

.EXAMPLE
Add-ArubaCPDeviceFingerprint -mac_address 000102030405 -ip_address 192.0.2.1

Add a Device Fingerprint with MAC Address 000102030405 and an IP Address

.EXAMPLE
Add-ArubaCPDeviceFingerprint -mac_address 000102030405 -device_category Server -device_family ClearPass -device_name ClearPass VM

Add a Device Fingerprint with MAC Address 000102030405 with device information (Category, Name, Family)
#>

Param(
[Parameter (Mandatory = $true)]
[string]$mac_address,
[Parameter (Mandatory = $false)]
[string]$hostname,
[Parameter (Mandatory = $false)]
[ipaddress]$ip_address,
[Parameter (Mandatory = $false)]
[string]$device_category,
[Parameter (Mandatory = $false)]
[string]$device_name,
[Parameter (Mandatory = $false)]
[string]$device_family,
[Parameter (Mandatory = $False)]
[ValidateNotNullOrEmpty()]
[PSObject]$connection = $DefaultArubaCPConnection
)

Begin {
}

Process {

$uri = "api/device-profiler/device-fingerprint"

$_dfp = new-Object -TypeName PSObject


$_dfp | add-member -name "mac" -membertype NoteProperty -Value (Format-ArubaCPMacAddress $mac_address)

if ( $PsBoundParameters.ContainsKey('hostname') ) {
$_dfp | add-member -name "hostname" -membertype NoteProperty -Value $hostname
}

if ( $PsBoundParameters.ContainsKey('ip_address') ) {
$_dfp | add-member -name "ip" -membertype NoteProperty -Value $ip_address.ToString()
}

$_device = new-Object -TypeName PSObject
if ( $PsBoundParameters.ContainsKey('device_category') ) {
$_device | add-member -name "category" -membertype NoteProperty -Value $device_category
}

if ( $PsBoundParameters.ContainsKey('device_name') ) {
$_device | add-member -name "name" -membertype NoteProperty -Value $device_name
}

if ( $PsBoundParameters.ContainsKey('device_family') ) {
$_device | add-member -name "family" -membertype NoteProperty -Value $device_family
}

$_dfp | add-member -name "device" -membertype NoteProperty -Value $_device

$dfp = Invoke-ArubaCPRestMethod -method "POST" -body $_dfp -uri $uri -connection $connection
$dfp
alagoutte marked this conversation as resolved.
Show resolved Hide resolved
}

End {
}
}

function Get-ArubaCPDeviceFingerprint {

<#
.SYNOPSIS
Get Device Fingerprint info on CPPM

.DESCRIPTION
Get Device Fingerprint (hostname, ip, device category/name/family)

.EXAMPLE
Get-ArubaCPDeviceFingerprint -mac_address 000102030405

Get Device FingerPrint about Endpoint 000102030405 Aruba on the ClearPass

.EXAMPLE
Get-ArubaCPEndpoint 000102030405 | Get-ArubaCPDeviceFingerprint

Get Device FingerPrint using Endpoint 000102030405

.EXAMPLE
Get-ArubaCPDeviceFingerprint -ip_address 192.0.2.1

Get Device FingerPrint about Endpoint 192.0.2.1 Aruba on the ClearPass

#>

Param(
[Parameter (ParameterSetName = "mac_address", Mandatory = $true)]
alagoutte marked this conversation as resolved.
Show resolved Hide resolved
[string]$mac_address,
[Parameter (ParameterSetName = "ip_address", Mandatory = $true)]
[IPAddress]$ip_address,
[Parameter (ParameterSetName = "endpoint", Mandatory = $true, ValueFromPipeline = $true)]
[ValidateScript( { Confirm-ArubaCPEndpoint $_ })]
[psobject]$endpoint,
[Parameter (Mandatory = $false)]
[ValidateNotNullOrEmpty()]
[PSObject]$connection = $DefaultArubaCPConnection
)

Begin {
}

Process {

if ($connection.version -lt [version]"6.9.0") {
throw "Need ClearPass >= 6.9.0 for use this cmdlet"
}

$invokeParams = @{ }

$uri = "api/device-profiler/device-fingerprint/"

switch ( $PSCmdlet.ParameterSetName ) {
"mac_address" {
$uri += $mac_address
}
"ip_address" {
$uri += $ip_address.ToString()
}
"endpoint" {
$uri += $endpoint.mac_address
}
default { }
}

$dfp = Invoke-ArubaCPRestMethod -method "GET" -uri $uri @invokeParams -connection $connection

$dfp
}

End {
}
}
1 change: 1 addition & 0 deletions Tests/common.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,6 @@ Connect-ArubaCP @invokeParams -SkipCertificateCheck

$script:VersionBefore680 = $DefaultArubaCPConnection.Version -lt [version]"6.8.0"
$script:VersionBefore686 = $DefaultArubaCPConnection.Version -lt [version]"6.8.6"
$script:VersionBefore690 = $DefaultArubaCPConnection.Version -lt [version]"6.9.0"

Disconnect-ArubaCP -confirm:$false
4 changes: 4 additions & 0 deletions Tests/integration/Connection.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ Describe "Connect to a ClearPass (using multi connection)" {
It "Use Multi connection for call Get Service" -Skip:$VersionBefore680 {
{ Get-ArubaCPService -connection $cppm } | Should -Not -Throw
}
It "Use Multi connection for call Device Fingerprint" -Skip:$VersionBefore690 {
$ip = (Get-ArubaCPServerConfiguration -connection $cppm).management_ip
{ Get-ArubaCPDeviceFingerprint -ip_address $ip -connection $cppm } | Should -Not -Throw
}
}

It "Disconnect to a ClearPass (Multi connection)" {
Expand Down
124 changes: 124 additions & 0 deletions Tests/integration/DeviceFingerprint.Tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
#
# Copyright 2021, Alexis La Goutte <alexis dot lagoutte at gmail dot com>
#
# SPDX-License-Identifier: Apache-2.0
#
. ../common.ps1

BeforeAll {
Connect-ArubaCP @invokeParams
}

Describe "Get Device FingerPrint" {

BeforeAll {
if ($VersionBefore690 -eq 0) {
#Add 2 entries
Add-ArubaCPDeviceFingerPrint -mac_address 00-01-02-03-04-05 -ip_address 192.0.2.1
Add-ArubaCPDeviceFingerPrint -mac_address 00-01-02-03-04-06 -ip_address 192.0.2.2
#Need to wait the time of profiling... (2 seconds...)
Start-Sleep 2
}
}

It "Get Endpoint (with mac filter) Does not throw an error" -Skip:$VersionBefore690 {
{
Get-ArubaCPDeviceFingerPrint -mac_address 00-01-02-03-04-05
} | Should -Not -Throw
}

It "Search Device Fingerprint by mac (00-01-02-03-04-05)" -Skip:$VersionBefore690 {
$dfp = Get-ArubaCPDeviceFingerprint -mac_address 000102030405
$dfp.updated_at | Should -Not -BeNullOrEmpty
$dfp.added_at | Should -Not -BeNullOrEmpty
$dfp.mac | Should -Be "000102030405"
$dfp.device_category | Should -Not -BeNullOrEmpty
$dfp.device_name | Should -Not -BeNullOrEmpty
$dfp.device_family | Should -Not -BeNullOrEmpty
}

It "Search Device Fingerprint by Endpoint pipeline (00-01-02-03-04-06)" -Skip:$VersionBefore690 {
$dfp = Get-ArubaCPEndpoint -mac_address 000102030406 | Get-ArubaCPDeviceFingerprint
$dfp.updated_at | Should -Not -BeNullOrEmpty
$dfp.added_at | Should -Not -BeNullOrEmpty
$dfp.mac | Should -Be "000102030406"
$dfp.device_category | Should -Not -BeNullOrEmpty
$dfp.device_name | Should -Not -BeNullOrEmpty
$dfp.device_family | Should -Not -BeNullOrEmpty
}

It "Search Device Fingerprint by ip_address (192.0.2.2)" -Skip:$VersionBefore690 {
$dfp = Get-ArubaCPDeviceFingerprint -ip_address 192.0.2.2
$dfp.updated_at | Should -Not -BeNullOrEmpty
$dfp.added_at | Should -Not -BeNullOrEmpty
$dfp.ip | Should -Be "192.0.2.2"
$dfp.device_category | Should -Not -BeNullOrEmpty
$dfp.device_name | Should -Not -BeNullOrEmpty
$dfp.device_family | Should -Not -BeNullOrEmpty
}

It "Get Device Fingerprint throw a error when use with CPPM <= 6.9.0" -Skip: ($VersionBefore690 -eq 0) {
{ Get-ArubaCPDeviceFingerprint -ip_address 192.0.2.2 } | Should -Throw "Need ClearPass >= 6.9.0 for use this cmdlet"
}

AfterAll {
if ($VersionBefore690 -eq 0) {
#Remove 2 entries
Get-ArubaCPEndpoint -mac_address 00-01-02-03-04-05 | Remove-ArubaCPEndpoint -confirm:$false
Get-ArubaCPEndpoint -mac_address 00-01-02-03-04-06 | Remove-ArubaCPEndpoint -confirm:$false
}
}
}

Describe "Add Device Fingerprint" {

It "Add Device Fingerprint with hostname" -Skip:$VersionBefore690 {
Add-ArubaCPDeviceFingerprint -mac_address 00-01-02-03-04-07 -hostname pester_PowerArubaCP
Start-Sleep 2
$dfp = Get-ArubaCPDeviceFingerprint -mac_address 00-01-02-03-04-07
$dfp.hostname | Should -be "pester_PowerArubaCP"
$dfp.updated_at | Should -Not -BeNullOrEmpty
$dfp.added_at | Should -Not -BeNullOrEmpty
$dfp.mac | Should -Be "000102030407"
$dfp.device_category | Should -Not -BeNullOrEmpty
$dfp.device_name | Should -Not -BeNullOrEmpty
$dfp.device_family | Should -Not -BeNullOrEmpty
}

It "Add Device Fingerprint with IP (Address)" -Skip:$VersionBefore690 {
Add-ArubaCPDeviceFingerprint -mac_address 00-01-02-03-04-08 -ip_address 192.0.2.1
Start-Sleep 2
$dfp = Get-ArubaCPDeviceFingerprint -mac_address 00-01-02-03-04-08
$dfp.ip | Should -be "192.0.2.1"
$dfp.updated_at | Should -Not -BeNullOrEmpty
$dfp.added_at | Should -Not -BeNullOrEmpty
$dfp.mac | Should -Be "000102030408"
$dfp.device_category | Should -Not -BeNullOrEmpty
$dfp.device_name | Should -Not -BeNullOrEmpty
$dfp.device_family | Should -Not -BeNullOrEmpty
}

It "Add Device Fingerprint with device information" -Skip:$VersionBefore690 {
Add-ArubaCPDeviceFingerprint -mac_address 00-01-02-03-04-09 -device_category Server -device_family ClearPass -device_name ClearPass VM
Start-Sleep 2
$dfp = Get-ArubaCPDeviceFingerprint -mac_address 00-01-02-03-04-09
$dfp.updated_at | Should -Not -BeNullOrEmpty
$dfp.added_at | Should -Not -BeNullOrEmpty
$dfp.mac | Should -Be "000102030409"
$dfp.device_category | Should -Be "Server"
$dfp.device_name | Should -Be "Clearpass"
$dfp.device_family | Should -Be "ClearPass"
}

AfterAll {
if ($VersionBefore690 -eq 0) {
Get-ArubaCPEndpoint -mac_address 00-01-02-03-04-07 | Remove-ArubaCPEndpoint -confirm:$false
Get-ArubaCPEndpoint -mac_address 00-01-02-03-04-08 | Remove-ArubaCPEndpoint -confirm:$false
Get-ArubaCPEndpoint -mac_address 00-01-02-03-04-09 | Remove-ArubaCPEndpoint -confirm:$false
}
}
}

AfterAll {
Disconnect-ArubaCP -confirm:$false
}