Skip to content
This repository has been archived by the owner on Oct 28, 2022. It is now read-only.

Add cmdlets to change cli Settings of ESG #439

Merged
merged 17 commits into from
Aug 13, 2019
Merged
Show file tree
Hide file tree
Changes from 16 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
122 changes: 122 additions & 0 deletions module/PowerNSX.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -3464,6 +3464,39 @@ Function ValidateEdgeDns {
else {
throw "Specify a valid Edge DNS object."
}

}

function ValidateCliSettings {
Param (
[Parameter (Mandatory=$true)]
[object]$argument
)

if ($argument -is [System.Xml.XmlElement] ) {

if ( -not ( $argument | get-member -name edgeId -Membertype Properties)) {
throw "XML Element specified does not contain an edgeId property."
}

if ( -not ( $argument | get-member -name remoteAccess -Membertype Properties)) {
throw "XML Element specified does not contain an remoteAccess property."
}

if ( -not ( $argument | get-member -name sshLoginBannerText -Membertype Properties)) {
throw "XML Element specified does not contain an sshLoginBannerText property."
}

if ( -not ( $argument | get-member -name passwordExpiry -Membertype Properties)) {
throw "XML Element specified does not contain an passwordExpiry property."
}

$true
}
else {
throw "Specify a valid CliSettings Configuration object."
}

}

Function ValidateIPsec {
Expand Down Expand Up @@ -13914,6 +13947,31 @@ function Set-NsxEdge {

Disable the Edge Firewall on ESG Edge01

.EXAMPLE
Get-NsxEdge Edge01 | Set-NsxEdge -password Vmware1!Vmware1!

Change the SSH Password

.EXAMPLE
Get-NsxEdge Edge01 | Set-NsxEdge -remoteAccess:$true

Enable the SSH on ESG (you can use also use Enable-NsxSSHEdgeSSH)

.EXAMPLE
Get-NsxEdge Edge01 | Set-NsxEdge -username powernsx -password Vmware1!Vmware1!

Set the SSH username to PowerNSX (You need to change/set the password on the sametime)

.EXAMPLE
Get-NsxEdge Edge01 | Set-NsxEdge -sshLoginBannerText "My Login Banner"

Change the SSH Login Banner

.EXAMPLE
Get-NsxEdge Edge01 | Set-NsxEdge -passwordExpiry 30

Change the SSH Password Expiration to 30 (days)

#>

[CmdletBinding()]
Expand All @@ -13926,6 +13984,25 @@ function Set-NsxEdge {
[Parameter (Mandatory=$False)]
#Prompt for confirmation. Specify as -confirm:$false to disable confirmation prompt
[switch]$Confirm=$true,

#cliSettings
[Parameter (Mandatory=$false)]
[ValidateNotNullorEmpty()]
[String]$userName,
[Parameter (Mandatory=$false)]
[ValidateNotNullorEmpty()]
[String]$password,
[Parameter (Mandatory=$false)]
[ValidateNotNullorEmpty()]
[boolean]$remoteAccess,
[Parameter (Mandatory=$false)]
[ValidateNotNullorEmpty()]
[ValidateRange(1,99999)]
[int]$passwordExpiry,
[Parameter (Mandatory=$false)]
[ValidateNotNullorEmpty()]
[string]$sshLoginBannerText,
dcoghlan marked this conversation as resolved.
Show resolved Hide resolved

[Parameter (Mandatory=$False)]
#PowerNSX Connection object
[ValidateNotNullOrEmpty()]
Expand All @@ -13947,6 +14024,51 @@ function Set-NsxEdge {
$_Edge.RemoveChild($edgeSummary) | out-null
}

#cliSettings
if ( $PsBoundParameters.ContainsKey('userName') ) {
if ( $PsBoundParameters.ContainsKey('password') ) {
dcoghlan marked this conversation as resolved.
Show resolved Hide resolved
if ( invoke-xpathquery -node $_Edge -querymethod SelectSingleNode -Query "child::cliSettings/userName" ) {
$_Edge.cliSettings.username = $userName
} else {
Add-XmlElement -xmlroot $_Edge.cliSettings -xmlElementName "userName" -xmlElementText $userName
}
dcoghlan marked this conversation as resolved.
Show resolved Hide resolved
} else {
throw "You need to specify a password for change username..."
}
}

if ( $PsBoundParameters.ContainsKey('password') ) {
if ( invoke-xpathquery -node $_Edge -querymethod SelectSingleNode -Query "child::cliSettings/password" ) {
$_Edge.cliSettings.password = $password
} else {
Add-XmlElement -xmlRoot $_Edge.cliSettings -xmlElementName "password" -xmlElementText $password
}
}

if ( $PsBoundParameters.ContainsKey('remoteAccess') ) {
if ( invoke-xpathquery -node $_Edge -querymethod SelectSingleNode -Query "child::cliSettings/remoteAccess" ) {
$_Edge.cliSettings.remoteAccess = $remoteAccess.ToString().ToLower()
} else {
Add-XmlElement -xmlroot $_Edge.cliSettings -xmlElementName "remoteAccess" -xmlElementText $remoteAccess.ToString().ToLower()
}
}

if ( $PsBoundParameters.ContainsKey('passwordExpiry') ) {
if ( invoke-xpathquery -node $_Edge -querymethod SelectSingleNode -Query "child::cliSettings/passwordExpiry" ) {
$_Edge.cliSettings.passwordExpiry = $passwordExpiry
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This also needs to be cast as a string.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On what release it don't work ? (because work for me on Windows with PowerShell 6/Core

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

needs to work on Powershell 5.1 on Windows

} else {
Add-XmlElement -xmlroot $_Edge.cliSettings -xmlElementName "passwordExpiry" -xmlElementText $passwordExpiry.ToString()
}
}

if ( $PsBoundParameters.ContainsKey('sshLoginBannerText') ) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why? any other edge xml update doesnt require this? are you saying no cli settings can be updated unless credential is specified? I havent tested this yet, but it sounds wrong.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Try and you will see that... (may be crazy when see that...)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i tested updating the banner text with the clisettings api - and yes it rejects it without credentials, however, i also tried it by updating the full edge api - and it works fine. Another reason for including in set-nsxedge :D

if ( invoke-xpathquery -node $_Edge -querymethod SelectSingleNode -Query "child::cliSettings/sshLoginBannerText" ) {
$_Edge.cliSettings.sshLoginBannerText = $sshLoginBannerText
} else {
Add-XmlElement -xmlroot $_Edge.cliSettings -xmlElementName "sshLoginBannerText" -xmlElementText $sshLoginBannerText
}
}

$URI = "/api/4.0/edges/$($_Edge.Id)"
$body = $_Edge.OuterXml

Expand Down
45 changes: 45 additions & 0 deletions tests/integration/04.Edge.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -726,6 +726,51 @@ Describe "Edge" {
}
}

Context "CliSettings" {

it "Can retrieve cliSettings" {
$edge = Get-NsxEdge $name
$edge.cliSettings | should not be $null
#By default it is admin
$edge.cliSettings.userName | should be "admin"
#By default it is 99999
$edge.cliSettings.passwordExpiry | should be "99999"
}

it "Can disable SSH" {
$edge = Get-NsxEdge $name
Get-NsxEdge $name | Set-NsxEdge -remoteAccess:$false -confirm:$false
$edge = Get-NsxEdge $name
$edge.cliSettings.remoteAccess | should be "false"
}

it "Can enable SSH" {
Get-NsxEdge $name | Set-NsxEdge -remoteAccess:$true -confirm:$false
$edge = Get-NsxEdge $name
$edge.cliSettings.remoteAccess | should be "true"
}

it "Change (SSH) username (and Password)" {
#it is mandatory to change username (and Password) on the same time (bug or feature ?)
Get-NsxEdge $name | Set-NsxEdge -userName powernsxviasetnsxedge -Password "Vmware1!Vmware1!" -confirm:$false
$edge = Get-NsxEdge $name
$edge.cliSettings.userName | should be "powernsxviasetnsxedge"
#It is impossible to check if the password is modified...
}

it "Change Password Expiry" {
Get-NsxEdge $name | Set-NsxEdge -passwordExpiry 4242 -confirm:$false
$edge = Get-NsxEdge $name
$edge.cliSettings.passwordExpiry | should be "4242"
}

it "Change sshLoginBannerText" {
Get-NsxEdge $name | Set-NsxEdge -sshLoginBannerText "Secured by Set-NsxEdge" -confirm:$false
$edge = Get-NsxEdge $name
$edge.cliSettings.sshLoginBannerText | should be "Secured by Set-NsxEdge"
}
}

Context "Misc" {

it "Can enable firewall via Set-NsxEdge" {
Expand Down