Skip to content

Commit

Permalink
New-TssIpRestriction - new command to create IP Address Restrictions
Browse files Browse the repository at this point in the history
  • Loading branch information
wsmelton committed Oct 1, 2021
1 parent e07a5c3 commit 1af98a1
Show file tree
Hide file tree
Showing 5 changed files with 269 additions and 17 deletions.
34 changes: 17 additions & 17 deletions .vscode/tss.code-snippets
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
" .NOTES",
" Requires TssSession object returned by New-TssSession",
" #>",
" [CmdletBinding()]",
" [CmdletBinding(SupportsShouldProcess)]",
" [OutputType('${7:Class name}')]",
" param (",
" # TssSession object created by New-TssSession for authentication",
Expand All @@ -45,9 +45,9 @@
" \\$invokeParams = . \\$GetInvokeApiParams \\$TssSession",
" }",
" process {",
" Write-Verbose \"Provided command parameters: \\$(. \\$GetInvocation \\$PSCmdlet.MyInvocation)\"",
" Get-TssInvocation \\$PSCmdlet.MyInvocation",
" if (\\$tssNewParams.ContainsKey('TssSession') -and \\$TssSession.IsValidSession()) {",
" . \\$CheckVersion \\$TssSession '10.9.000064' \\$PSCmdlet.MyInvocation",
" Compare-TssVersion \\$TssSession '10.9.000064' \\$PSCmdlet.MyInvocation",
" \\$restResponse = \\$null",
" \\$uri = \\$TssSession.ApiUrl, '${11:endpoint}' -join '/'",
" \\$invokeParams.Uri = \\$uri",
Expand All @@ -64,7 +64,7 @@
" \\$apiResponse = Invoke-TssApi @invokeParams",
" \\$restResponse = . \\$ProcessResponse \\$apiResponse",
" } catch {",
" Write-Warning \"Issue creating report [${4}]\"",
" Write-Warning \"Issue creating ${12:Reference} [${4}]\"",
" \\$err = \\$_",
" . \\$ErrorHandling \\$err",
" }",
Expand Down Expand Up @@ -129,9 +129,9 @@
" \\$invokeParams = . \\$GetInvokeApiParams \\$TssSession",
" }",
" process {",
" Write-Verbose \"Provided command parameters: \\$(. \\$GetInvocation \\$PSCmdlet.MyInvocation)\"",
" Get-TssInvocation \\$PSCmdlet.MyInvocation",
" if (\\$tssParams.ContainsKey('TssSession') -and \\$TssSession.IsValidSession()) {",
" . \\$CheckVersion \\$TssSession '10.9.000064' \\$PSCmdlet.MyInvocation",
" Compare-TssVersion \\$TssSession '10.9.000064' \\$PSCmdlet.MyInvocation",
" \\$uri = \\$TssSession.ApiUrl, '${10:endpoint}' -join '/'",
" \\$uri = \\$uri, \"sortBy[0].direction=asc&sortBy[0].name=\\$SortBy&take=\\$(\\$TssSession.Take)\" -join '?'",
" \\$invokeParams.Method = 'GET'",
Expand Down Expand Up @@ -217,9 +217,9 @@
" \\$invokeParams = . \\$GetInvokeApiParams \\$TssSession",
" }",
" process {",
" Write-Verbose \"Provided command parameters: \\$(. \\$GetInvocation \\$PSCmdlet.MyInvocation)\"",
" Get-TssInvocation \\$PSCmdlet.MyInvocation",
" if (\\$tssParams.ContainsKey('TssSession') -and \\$TssSession.IsValidSession()) {",
" . \\$CheckVersion \\$TssSession '10.9.000064' \\$PSCmdlet.MyInvocation",
" Compare-TssVersion \\$TssSession '10.9.000064' \\$PSCmdlet.MyInvocation",
" foreach (\\$${10:var name} in \\$${4}) {",
" \\$restResponse = \\$null",
" \\$uri = \\$TssSession.ApiUrl, '${11:endpoint}', \\$${10} -join '/'",
Expand Down Expand Up @@ -294,9 +294,9 @@
" \\$invokeParams = . \\$GetInvokeApiParams \\$TssSession",
" }",
" process {",
" Write-Verbose \"Provided command parameters: \\$(. \\$GetInvocation \\$PSCmdlet.MyInvocation)\"",
" Get-TssInvocation \\$PSCmdlet.MyInvocation",
" if (\\$tssParams.ContainsKey('TssSession') -and \\$TssSession.IsValidSession()) {",
" . \\$CheckVersion \\$TssSession '10.9.000064' \\$PSCmdlet.MyInvocation",
" Compare-TssVersion \\$TssSession '10.9.000064' \\$PSCmdlet.MyInvocation",
" foreach (\\$${10:var name} in \\$${3}) {",
" \\$restResponse = \\$null",
" \\$uri = \\$TssSession.ApiUrl, '${11:endpoint}', \\$${10} -join '/'",
Expand Down Expand Up @@ -371,9 +371,9 @@
" \\$invokeParams = . \\$GetInvokeApiParams \\$TssSession",
" }",
" process {",
" Write-Verbose \"Provided command parameters: \\$(. \\$GetInvocation \\$PSCmdlet.MyInvocation)\"",
" Get-TssInvocation \\$PSCmdlet.MyInvocation",
" if (\\$setParams.ContainsKey('TssSession') -and \\$TssSession.IsValidSession()) {",
" . \\$CheckVersion \\$TssSession '10.9.000064' \\$PSCmdlet.MyInvocation",
" Compare-TssVersion \\$TssSession '10.9.000064' \\$PSCmdlet.MyInvocation",
" \\$uri = \\$TssSession.ApiUrl, '${6:endpoint}' -join '/'",
" \\$invokeParams.Uri = \\$uri",
" \\$invokeParams.Method = 'PATCH'",
Expand Down Expand Up @@ -456,9 +456,9 @@
" \\$invokeParams = . \\$GetInvokeApiParams \\$TssSession",
" }",
" process {",
" Write-Verbose \"Provided command parameters: \\$(. \\$GetInvocation \\$PSCmdlet.MyInvocation)\"",
" Get-TssInvocation \\$PSCmdlet.MyInvocation",
" if (\\$updateParams.ContainsKey('TssSession') -and \\$TssSession.IsValidSession()) {",
" . \\$CheckVersion \\$TssSession '10.9.000064' \\$PSCmdlet.MyInvocation",
" Compare-TssVersion \\$TssSession '10.9.000064' \\$PSCmdlet.MyInvocation",
" \\$restResponse = \\$null",
" \\$uri = \\$TssSession.ApiUrl, '${5:endpoint}' -join '/'",
" \\$invokeParams.Uri = \\$uri",
Expand Down Expand Up @@ -773,9 +773,9 @@
"prefix": "tssprocbody",
"body": [
"process {",
" Write-Verbose \"Provided command parameters: \\$(. \\$GetInvocation \\$PSCmdlet.MyInvocation)\"",
" Get-TssInvocation \\$PSCmdlet.MyInvocation",
" if (\\$tssParams.ContainsKey('TssSession') -and \\$TssSession.IsValidSession()) {",
" . \\$CheckVersion \\$TssSession '10.9.000064' \\$PSCmdlet.MyInvocation",
" Compare-TssVersion \\$TssSession '10.9.000064' \\$PSCmdlet.MyInvocation",
" \\$uri = \\$TssSession.ApiUrl, ${1:endpoint} -join '/'",
" \\$invokeParams.Uri = \\$uri",
" \\$invokeParams.Method = '${2:Method}'",
Expand Down Expand Up @@ -858,7 +858,7 @@
"prefix": "tssrestbasicforeaget",
"body": [
" if (\\$tssParams.ContainsKey('TssSession') -and \\$TssSession.IsValidSession()) {",
" . \\$CheckVersion \\$TssSession '10.9.000064' \\$PSCmdlet.MyInvocation",
" Compare-TssVersion \\$TssSession '10.9.000064' \\$PSCmdlet.MyInvocation",
" foreach (\\$${1:object name} in \\$${2:Parameter}) {",
" \\$restResponse = \\$null",
" \\$uri = \\$TssSession.ApiUrl, ${3:endpoint} -join '/'",
Expand Down
128 changes: 128 additions & 0 deletions docs/commands/ipaddress-restrictions/New-TssIpRestriction.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
# New-TssIpRestriction

## SYNOPSIS
Create a new IP Address Restriction

## SYNTAX

```
New-TssIpRestriction [-TssSession] <Session> -Name <String> -Range <String> [-WhatIf] [-Confirm]
[<CommonParameters>]
```

## DESCRIPTION
Create a new IP Address Restriction

## EXAMPLES

### EXAMPLE 1
```
$session = New-TssSession -SecretServer https://alpha -Credential $ssCred
New-TssIpRestriction -TssSession $session -Name 'Corp Network' -Range '172.56.23.0/24'
```

Create a new IP restriction named "Corp Network"

### EXAMPLE 2
```
$session = New-TssSession -SecretServer https://alpha -Credential $ssCred
New-TssIpRestriction -TssSession $session -Name 'Remote Office' -Range '172.56.24.72', '172.56.25.72'
```

Create a new IP restriction named "Remote Office"

## PARAMETERS

### -TssSession
TssSession object created by New-TssSession for authentication

```yaml
Type: Session
Parameter Sets: (All)
Aliases:

Required: True
Position: 1
Default value: None
Accept pipeline input: True (ByValue)
Accept wildcard characters: False
```
### -Name
IP Address Restriction Name
```yaml
Type: String
Parameter Sets: (All)
Aliases:

Required: True
Position: Named
Default value: None
Accept pipeline input: True (ByValue)
Accept wildcard characters: False
```
### -Range
IP Address Range, CIDR
```yaml
Type: String
Parameter Sets: (All)
Aliases:

Required: True
Position: Named
Default value: None
Accept pipeline input: True (ByValue)
Accept wildcard characters: False
```
### -WhatIf
Shows what would happen if the cmdlet runs.
The cmdlet is not run.
```yaml
Type: SwitchParameter
Parameter Sets: (All)
Aliases: wi

Required: False
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
```
### -Confirm
Prompts you for confirmation before running the cmdlet.
```yaml
Type: SwitchParameter
Parameter Sets: (All)
Aliases: cf

Required: False
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
```
### CommonParameters
This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see [about_CommonParameters](http://go.microsoft.com/fwlink/?LinkID=113216).
## INPUTS
## OUTPUTS
### Thycotic.PowerShell.IpRestrictions.IpRestriction
## NOTES
Requires TssSession object returned by New-TssSession
## RELATED LINKS
[https://thycotic-ps.github.io/thycotic.secretserver/commands/ipaddress-restrictions/New-TssIpRestriction](https://thycotic-ps.github.io/thycotic.secretserver/commands/ipaddress-restrictions/New-TssIpRestriction)
[https://github.com/thycotic-ps/thycotic.secretserver/blob/main/src/functions/ipaddress-restrictions/New-TssIpRestriction.ps1](https://github.com/thycotic-ps/thycotic.secretserver/blob/main/src/functions/ipaddress-restrictions/New-TssIpRestriction.ps1)
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;
using System.Threading.Tasks;
using System.Management.Automation;
using System.Management.Automation.Runspaces;

namespace Thycotic.PowerShell.IpRestrictions
{
public class IpRestriction
{
public int Id { get; set; }
public string Name { get; set; }
public string Range { get; set; }
}
}
86 changes: 86 additions & 0 deletions src/functions/ipaddress-restrictions/New-TssIpRestriction.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
function New-TssIpRestriction {
<#
.SYNOPSIS
Create a new IP Address Restriction
.DESCRIPTION
Create a new IP Address Restriction
.LINK
https://thycotic-ps.github.io/thycotic.secretserver/commands/ipaddress-restrictions/New-TssIpRestriction
.LINK
https://github.com/thycotic-ps/thycotic.secretserver/blob/main/src/functions/ipaddress-restrictions/New-TssIpRestriction.ps1
.EXAMPLE
$session = New-TssSession -SecretServer https://alpha -Credential $ssCred
New-TssIpRestriction -TssSession $session -Name 'Corp Network' -Range '172.56.23.0/24'
Create a new IP restriction named "Corp Network"
.EXAMPLE
$session = New-TssSession -SecretServer https://alpha -Credential $ssCred
New-TssIpRestriction -TssSession $session -Name 'Remote Office' -Range '172.56.24.72', '172.56.25.72'
Create a new IP restriction named "Remote Office"
.NOTES
Requires TssSession object returned by New-TssSession
#>
[CmdletBinding(SupportsShouldProcess)]
[OutputType('Thycotic.PowerShell.IpRestrictions.IpRestriction')]
param (
# TssSession object created by New-TssSession for authentication
[Parameter(Mandatory,ValueFromPipeline,Position = 0)]
[Thycotic.PowerShell.Authentication.Session]
$TssSession,

# IP Address Restriction Name
[Parameter(Mandatory,ValueFromPipeline)]
[string]
$Name,

# IP Address Range, CIDR
[Parameter(Mandatory,ValueFromPipeline)]
[string]
$Range
)
begin {
$tssNewParams = $PSBoundParameters
$invokeParams = . $GetInvokeApiParams $TssSession
}
process {
Get-TssInvocation $PSCmdlet.MyInvocation
if ($tssNewParams.ContainsKey('TssSession') -and $TssSession.IsValidSession()) {
Compare-TssVersion $TssSession '10.9.000064' $PSCmdlet.MyInvocation
$restResponse = $null
$uri = $TssSession.ApiUrl, 'ipaddress-restrictions' -join '/'
$invokeParams.Uri = $uri
$invokeParams.Method = 'POST'

$newBody = [ordered]@{}
switch ($tssNewParams.Keys) {
'Name' { $newBody.Add('name',$Name) }
'Range' { $newBody.Add('range',$Range) }
}
$invokeParams.Body = ($newBody | ConvertTo-Json)

Write-Verbose "Performing the operation $($invokeParams.Method) $($invokeParams.Uri) with:`n $newBody"
if (-not $PSCmdlet.ShouldProcess("IP Restriction: $Name", "$($invokeParams.Method) $($invokeParams.Uri) with $($invokeParams.Body)")) { return }
try {
$apiResponse = Invoke-TssApi @invokeParams
$restResponse = . $ProcessResponse $apiResponse
} catch {
Write-Warning "Issue creating IP Restriction [Name]"
$err = $_
. $ErrorHandling $err
}

if ($restResponse) {
[Thycotic.PowerShell.IpRestrictions.IpRestriction]$restResponse
}
} else {
Write-Warning "No valid session found"
}
}
}
24 changes: 24 additions & 0 deletions tests/ipaddress-restrictions/New-TssIpRestriction.Tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
BeforeDiscovery {
$commandName = Split-Path ($PSCommandPath.Replace('.Tests.ps1','')) -Leaf
}
Describe "$commandName verify parameters" {
BeforeDiscovery {
[object[]]$knownParameters = 'TssSession', 'Name', 'Range'
[object[]]$currentParams = ([Management.Automation.CommandMetaData]$ExecutionContext.SessionState.InvokeCommand.GetCommand($commandName,'Function')).Parameters.Keys
[object[]]$commandDetails = [System.Management.Automation.CommandInfo]$ExecutionContext.SessionState.InvokeCommand.GetCommand($commandName,'Function')
$unknownParameters = Compare-Object -ReferenceObject $knownParameters -DifferenceObject $currentParams -PassThru
}
Context "Verify parameters" -Foreach @{currentParams = $currentParams } {
It "$commandName should contain <_> parameter" -TestCases $knownParameters {
$_ -in $currentParams | Should -Be $true
}
It "$commandName should not contain parameter: <_>" -TestCases $unknownParameters {
$_ | Should -BeNullOrEmpty
}
}
Context "Command specific details" {
It "$commandName should set OutputType to Thycotic.PowerShell.IpRestrictions.IpAddress" -TestCases $commandDetails {
$_.OutputType.Name | Should -Be 'Thycotic.PowerShell.IpRestrictions.IpAddress'
}
}
}

0 comments on commit 1af98a1

Please sign in to comment.