Skip to content

Commit

Permalink
Module - standardize error handling calling Invoke-TssRestApi
Browse files Browse the repository at this point in the history
  • Loading branch information
wsmelton committed Dec 27, 2020
1 parent fc21c00 commit 7b6b827
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 114 deletions.
23 changes: 10 additions & 13 deletions src/functions/Disable-TssSecret.ps1
Expand Up @@ -6,15 +6,6 @@
.DESCRIPTION
Disables a secret from Secret Server
.PARAMETER TssSession
TssSession object created by New-TssSession
.PARAMETER Id
Secret ID to disable (mark inactive)
.PARAMETER Raw
Output the raw response from the REST API endpoint
.EXAMPLE
PS C:\> $session = New-TssSession -SecretServer https://alpha -Credential $ssCred
PS C:\> Disable-TssSecret -Id 93
Expand All @@ -26,19 +17,19 @@
#>
[cmdletbinding(SupportsShouldProcess)]
param(
# TssSession object passed for auth info
# TssSession object created by New-TssSession for auth
[Parameter(Mandatory,
ValueFromPipeline,
Position = 0)]
[TssSession]$TssSession,

# Delete only specific Secret, Secret Id
# Secret ID to disable (mark inactive)
[Parameter(Mandatory,ValueFromPipelineByPropertyName)]
[Alias("SecretId")]
[int[]]
$Id,

# output the raw response from the API endpoint
# Output the raw response from the REST API endpoint
[switch]
$Raw
)
Expand All @@ -58,7 +49,13 @@
$invokeParams.Method = 'DELETE'

if (-not $PSCmdlet.ShouldProcess("$($invokeParams.Method) $uri")) { return }
$restResponse = Invoke-TssRestApi @invokeParams
try {
$restResponse = Invoke-TssRestApi @invokeParams
} catch {
Write-Warning "Issue disabling secret [$secret]"
$err = $_.ErrorDetails.Message
Write-Error $err
}

if ($tssParams['Raw']) {
return $restResponse
Expand Down
33 changes: 12 additions & 21 deletions src/functions/Get-TssFolder.ps1
Expand Up @@ -6,21 +6,6 @@
.DESCRIPTION
Get a folder(s) from Secret Server
.PARAMETER TssSession
TssSession object created by New-TssSession
.PARAMETER Id
Folder ID to retrieve, accepts an array of IDs
.PARAMETER Recurse
Retrieve all child folders within the requested folder
.PARAMETER IncludeTemplates
Include allowable Secret Templates of the requested folder
.PARAMETER Raw
Output the raw response from the REST API endpoint
.EXAMPLE
PS C:\> $session = New-TssSession -SecretServer https://alpha -Credential $ssCred
PS C:\> Get-TssFolder -TssSession $session -Id 4
Expand All @@ -45,31 +30,31 @@
[cmdletbinding()]
[OutputType('TssFolder')]
param(
# TssSession object passed for auth info
# TssSession object created by New-TssSession for auth
[Parameter(Mandatory,
ValueFromPipeline,
Position = 0)]
[TssSession]$TssSession,

# Return only specific Secret, Secret Id
# Folder ID to retrieve
[Parameter(Mandatory,ValueFromPipelineByPropertyName)]
[Alias("FolderId")]
[int[]]
$Id,

# get all children folders
# Retrieve all child folders within the requested folder
[Parameter(ParameterSetName = 'filter')]
[Alias("GetAllChildren")]
[switch]
$Recurse,

# Get associated templates
# Include allowable Secret Templates of the requested folder
[Parameter(ParameterSetName = 'filter')]
[Alias("IncludeAssociatedTemplates")]
[switch]
$IncludeTemplates,

# output the raw response from the API endpoint
# Output the raw response from the REST API endpoint
[switch]
$Raw
)
Expand All @@ -89,7 +74,13 @@
$invokeParams.Method = 'GET'

$invokeParams.PersonalAccessToken = $TssSession.AccessToken
$restResponse = Invoke-TssRestApi @invokeParams
try {
$restResponse = Invoke-TssRestApi @invokeParams
} catch {
Write-Warning "Issue getting folder [$folder]"
$err = $_.ErrorDetails.Message
Write-Error $err
}

if ($tssParams['Raw']) {
return $restResponse
Expand Down
28 changes: 11 additions & 17 deletions src/functions/Get-TssSecret.ps1
Expand Up @@ -6,18 +6,6 @@
.DESCRIPTION
Get a secret(s) from Secret Server
.PARAMETER TssSession
TssSession object created by New-TssSession
.PARAMETER Id
Secret ID to retrieve, accepts an array of IDs
.PARAMETER Comment
Comment to provide for restricted secret (Require Comment is enabled)
.PARAMETER Raw
Output the raw response from the REST API endpoint
.EXAMPLE
PS C:\> $session = New-TssSession -SecretServer https://alpha -Credential $ssCred
PS C:\> Get-TssSecret -TssSession $session -Id 93
Expand All @@ -43,23 +31,23 @@
[cmdletbinding()]
[OutputType('TssSecret')]
param(
# TssSession object passed for auth info
# TssSession object created by New-TssSession for auth
[Parameter(Mandatory,
ValueFromPipeline,
Position = 0)]
[TssSession]$TssSession,

# Return only specific Secret, Secret Id
# Secret ID to retrieve
[Parameter(Mandatory,ValueFromPipelineByPropertyName)]
[Alias("SecretId")]
[int[]]
$Id,

# Provide comment for restricted secret
# Comment to provide for restricted secret (Require Comment is enabled)
[string]
$Comment,

# output the raw response from the API endpoint
# Output the raw response from the REST API endpoint
[switch]
$Raw
)
Expand All @@ -86,7 +74,13 @@
}

$invokeParams.PersonalAccessToken = $TssSession.AccessToken
$restResponse = Invoke-TssRestApi @invokeParams
try {
$restResponse = Invoke-TssRestApi @invokeParams
} catch {
Write-Warning "Issue getting secret [$secret]"
$err = $_.ErrorDetails.Message
Write-Error $err
}

if ($tssParams['Raw']) {
return $restResponse
Expand Down
23 changes: 10 additions & 13 deletions src/functions/Get-TssSecretTemplate.ps1
Expand Up @@ -6,15 +6,6 @@
.DESCRIPTION
Get a secret template(s) from Secret Server
.PARAMETER TssSession
TssSession object created by New-TssSession
.PARAMETER Id
Secret template ID to retrieve, accepts an array of IDs
.PARAMETER Raw
Output the raw response from the REST API endpoint
.EXAMPLE
PS C:\> $session = New-TssSession -SecretServer https://alpha -Credential $ssCred
PS C:\> Get-TssSecretTemplate -Id 93
Expand All @@ -26,19 +17,19 @@
#>
[cmdletbinding()]
param(
# TssSession object passed for auth info
# TssSession object created by New-TssSession for auth
[Parameter(Mandatory,
ValueFromPipeline,
Position = 0)]
[TssSession]$TssSession,

# Return only specific Secret, Secret Id
# Secret template ID to retrieve
[Parameter(Mandatory,ValueFromPipelineByPropertyName)]
[Alias("TemplateId")]
[int[]]
$Id,

# output the raw response from the API endpoint
# Output the raw response from the REST API endpoint
[switch]
$Raw
)
Expand All @@ -57,7 +48,13 @@
$invokeParams.PersonalAccessToken = $TssSession.AccessToken

Write-Verbose "$($invokeParas.Method) $uri with $body"
$restResponse = Invoke-TssRestApi @invokeParams
try {
$restResponse = Invoke-TssRestApi @invokeParams
} catch {
Write-Warning "Issue getting template [$template]"
$err = $_.ErrorDetails.Message
Write-Error $err
}

if ($tssParams['Raw']) {
return $restResponse
Expand Down
16 changes: 7 additions & 9 deletions src/functions/Invoke-TssRestApi.ps1
Expand Up @@ -13,27 +13,25 @@
#>
[Cmdletbinding()]
param(
# The REST API Url
# Secret Server REST API URL
[Parameter(Mandatory,ValueFromPipelineByPropertyName)]
[Alias('Url')]
[uri]
$Uri,

# A Personal Access Token
# Valid Access Token issued by Secret Server
[Parameter(ValueFromPipelineByPropertyName)]
[Alias('PAT')]
[string]
$PersonalAccessToken,

# Specifies the method used for the web request
# Method used for the web request, supported by Secret Server
[Parameter(ValueFromPipelineByPropertyName)]
[ValidateSet('GET','DELETE', 'PATCH', 'POST', 'PUT')]
[string]
$Method,

# Specifies the body of the request.
# If this value is a string, it will be passed as-is
# Otherwise, this value will be converted into JSON.
[Parameter(ValueFromPipelineByPropertyName)]
[Object]
$Body,
Expand All @@ -43,12 +41,12 @@
[string]
$ContentType = 'application/json',

# Specifies the headers of the web request. Enter a hash table or dictionary.
# Header of the web request. Enter a hash table or dictionary.
[System.Collections.IDictionary]
[Alias('Header')]
$Headers,

# Indicates that the cmdlet uses the credentials of the current user to send the web request (winauth).
# Indicates using the credentials of the current user to send the web request (winauth).
[Alias('UseDefaultCredential')]
[switch]
$UseDefaultCredentials,
Expand All @@ -69,7 +67,7 @@
[switch]
$ProxyUseDefaultCredentials,

# The typename of the results.
# Output a custom type name for the results.
[Parameter(ValueFromPipelineByPropertyName)]
[string[]]
$PSTypeName,
Expand All @@ -83,7 +81,7 @@
[string[]]
$RemoveProperty,

# If provided, will expand a given property returned from the REST api.
# Expand a given property from an object
[string]
$ExpandProperty
)
Expand Down
6 changes: 1 addition & 5 deletions src/functions/New-TssSession.ps1
Expand Up @@ -41,9 +41,7 @@
[cmdletbinding(SupportsShouldProcess)]
param(
# Secret Server URL
[Parameter(ParameterSetName = 'New',
Mandatory,
HelpMessage = 'URL for Secret Server, e.g. https://vault.company.com/SecretServer')]
[Parameter(ParameterSetName = 'New',Mandatory)]
[Parameter(ParameterSetName = 'tss',
Mandatory)]
[Alias('Server')]
Expand All @@ -70,8 +68,6 @@
begin {
$invokeParams = . $GetInvokeTssParams $PSBoundParameters
$newTssParams = . $GetNewTssParams $PSBoundParameters

# $TssSession = [TssSession]::new()
}

process {
Expand Down

0 comments on commit 7b6b827

Please sign in to comment.