Skip to content

Commit

Permalink
New-TssSecretPolicy - new command
Browse files Browse the repository at this point in the history
  • Loading branch information
wsmelton committed Aug 27, 2021
1 parent 15621d3 commit 45bfda9
Show file tree
Hide file tree
Showing 3 changed files with 243 additions and 0 deletions.
135 changes: 135 additions & 0 deletions docs/commands/secret-policies/New-TssSecretPolicy.md
@@ -0,0 +1,135 @@
# New-TssSecretPolicy

## SYNOPSIS
Create a new Secret Policy

## SYNTAX

```
New-TssSecretPolicy [-TssSession] <Session> -Name <String> [-Description <String>] [-Active] [-WhatIf]
[-Confirm] [<CommonParameters>]
```

## DESCRIPTION
Create a new Secret Policy, configure Policy Items using Update-TssSecretPolicy

## EXAMPLES

### EXAMPLE 1
```
$session = New-TssSession -SecretServer https://alpha -Credential $ssCred
New-TssSecretPolicy -TssSession $session -Name 'Require Checkout'
```

Create a new secret policy setting enforcing various policy items

## 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
Secret Policy Name

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

Required: True
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
```

### -Description
Secret Policy Description

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

Required: False
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
```

### -Active
Activate the policy after creation

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

Required: False
Position: Named
Default value: False
Accept pipeline input: False
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.SecretPolicies.Policy
## NOTES
Requires TssSession object returned by New-TssSession

## RELATED LINKS

[https://thycotic-ps.github.io/thycotic.secretserver/commands/secret-policies/New-TssSecretPolicy](https://thycotic-ps.github.io/thycotic.secretserver/commands/secret-policies/New-TssSecretPolicy)

[https://github.com/thycotic-ps/thycotic.secretserver/blob/main/src/functions/secret-policies/New-TssSecretPolicy.ps1](https://github.com/thycotic-ps/thycotic.secretserver/blob/main/src/functions/secret-policies/New-TssSecretPolicy.ps1)

83 changes: 83 additions & 0 deletions src/functions/secret-policies/New-TssSecretPolicy.ps1
@@ -0,0 +1,83 @@
function New-TssSecretPolicy {
<#
.SYNOPSIS
Create a new Secret Policy
.DESCRIPTION
Create a new Secret Policy, configure Policy Items using Update-TssSecretPolicy
.LINK
https://thycotic-ps.github.io/thycotic.secretserver/commands/secret-policies/New-TssSecretPolicy
.LINK
https://github.com/thycotic-ps/thycotic.secretserver/blob/main/src/functions/secret-policies/New-TssSecretPolicy.ps1
.EXAMPLE
$session = New-TssSession -SecretServer https://alpha -Credential $ssCred
New-TssSecretPolicy -TssSession $session -Name 'Require Checkout'
Create a new secret policy setting enforcing various policy items
.NOTES
Requires TssSession object returned by New-TssSession
#>
[CmdletBinding(SupportsShouldProcess)]
[OutputType('Thycotic.PowerShell.SecretPolicies.Policy')]
param (
# TssSession object created by New-TssSession for authentication
[Parameter(Mandatory,ValueFromPipeline,Position = 0)]
[Thycotic.PowerShell.Authentication.Session]
$TssSession,

# Secret Policy Name
[Parameter(Mandatory)]
[string]
$Name,

# Secret Policy Description
[string]
$Description,

# Activate the policy after creation
[switch]
$Active
)
begin {
$tssNewParams = $PSBoundParameters
$invokeParams = . $GetInvokeApiParams $TssSession
}
process {
Write-Verbose "Provided command parameters: $(. $GetInvocation $PSCmdlet.MyInvocation)"
if ($tssNewParams.ContainsKey('TssSession') -and $TssSession.IsValidSession()) {
. $CheckVersion $TssSession '10.9.000064' $PSCmdlet.MyInvocation
$uri = $TssSession.ApiUrl, 'secret-policy' -join '/'
$invokeParams.Uri = $uri
$invokeParams.Method = 'POST'

$newBody = @{data = @{} }
switch ($tssNewParams.Keys) {
'Name' { $newBody.data.Add('secretPolicyName',$Name) }
'Description' { $newBody.data.Add('secretPolicyDescription',$Description) }
'Active' { $newBody.data.Add('active',[boolean]$Active) }
}
$invokeParams.Body = $newBody | ConvertTo-Json -Depth 100

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

if ($restResponse) {
[Thycotic.PowerShell.SecretPolicies.Policy]$restResponse
}
} else {
Write-Warning "No valid session found"
}
}
}
25 changes: 25 additions & 0 deletions tests/secret-policies/New-TssSecretPolicy.Tests.ps1
@@ -0,0 +1,25 @@
BeforeDiscovery {
$commandName = Split-Path ($PSCommandPath.Replace('.Tests.ps1','')) -Leaf
}
Describe "$commandName verify parameters" {
BeforeDiscovery {
[object[]]$knownParameters = 'TssSession','Name','Description','Active'

[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.SecretPolicies.Policy" -TestCases $commandDetails {
$_.OutputType.Name | Should -Be 'Thycotic.PowerShell.SecretPolicies.Policy'
}
}
}

0 comments on commit 45bfda9

Please sign in to comment.