Skip to content

Commit

Permalink
Adjust API Url process
Browse files Browse the repository at this point in the history
Fixes #39
  • Loading branch information
wsmelton committed Jan 6, 2021
1 parent 1ec18bc commit ce0aeba
Show file tree
Hide file tree
Showing 14 changed files with 69 additions and 49 deletions.
2 changes: 1 addition & 1 deletion .vscode/tss.code-snippets
Expand Up @@ -48,7 +48,7 @@
"\t\tif (\\$tssParams.Contains('TssSession') -and \\$TssSession.IsValidSession()) {",
"\t\t\tforeach (\\$${7} in \\$${4}) {",
"\t\t\t\t\\$restResponse = \\$null",
"\t\t\t\t\\$uri = \\$TssSession.SecretServer + (\\$TssSession.ApiVersion, \"${1}\", \\$${8}.ToString() -join '/')",
"\t\t\t\t\\$uri = \\$TssSession.ApiUrl, '${1}', \\$${8}.ToString() -join '/'",
"\t\t\t\t\\$invokeParams.Uri = \\$uri",
"\t\t\t\t\\$invokeParams.Method = 'GET'",
"",
Expand Down
3 changes: 2 additions & 1 deletion src/classes/TssSession.class.ps1
@@ -1,6 +1,7 @@
class TssSession {
[string]$SecretServer
[string]$ApiVersion = "api/v1"
[string]$ApiVersion = 'api/v1'
[string]$ApiUrl
[string]$AccessToken
[string]$RefreshToken
[string]$TokenType
Expand Down
4 changes: 3 additions & 1 deletion src/functions/Disable-TssSecret.ps1
Expand Up @@ -39,16 +39,18 @@
}

process {
Write-Verbose "Provided command parameters: $(. $GetInvocation $PSCmdlet.MyInvocation)"
if ($tssParams.Contains('TssSession') -and $TssSession.IsValidSession()) {

foreach ($secret in $Id) {
$uri = $TssSession.SecretServer + ($TssSession.ApiVersion, "secrets", $secret.ToString() -join '/')
$uri = $TssSession.ApiUrl, "secrets", $secret.ToString() -join '/'

$invokeParams.Uri = $Uri
$invokeParams.PersonalAccessToken = $TssSession.AccessToken
$invokeParams.Method = 'DELETE'

if (-not $PSCmdlet.ShouldProcess("$($invokeParams.Method) $uri")) { return }
Write-Verbose "$($invokeParams.Method) $uri"
try {
$restResponse = Invoke-TssRestApi @invokeParams
} catch {
Expand Down
10 changes: 5 additions & 5 deletions src/functions/Find-TssSecret.ps1
Expand Up @@ -175,9 +175,9 @@
Write-Verbose "Provided command parameters: $(. $GetInvocation $PSCmdlet.MyInvocation)"
if ($tssParams.Contains('TssSession') -and $TssSession.IsValidSession()) {
if ($tssParams['Id']) {
$uri = $TssSession.SecretServer + ( $TssSession.ApiVersion, "secrets/lookup", $Id -join '/')
$uri = $TssSession.ApiUrl + ("secrets/lookup", $Id -join '/')
} else {
$uri = $TssSession.SecretServer + ( $TssSession.ApiVersion, "secrets/lookup" -join '/')
$uri = $TssSession.ApiUrl, "secrets/lookup" -join '/'
$uri += "?take=$($TssSession.Take)"
$uri += "&filter.includeRestricted=true"

Expand Down Expand Up @@ -239,16 +239,16 @@
}
}
}
$uriFilter = $filters -join "&"
$uriFilter = $filters -join '&'
Write-Verbose "Filters: $uriFilter"
$uri = $uri, $uriFilter -join "&"
$uri = $uri, $uriFilter -join '&'
}


$invokeParams.Uri = $uri
$invokeParams.PersonalAccessToken = $TssSession.AccessToken
$invokeParams.Method = 'GET'
Write-Verbose "$($invokeParams.Method) $uri with $body"
Write-Verbose "$($invokeParams.Method) $uri"
try {
$restResponse = Invoke-TssRestApi @invokeParams
} catch {
Expand Down
3 changes: 2 additions & 1 deletion src/functions/Get-TssFolder.ps1
Expand Up @@ -67,13 +67,14 @@
if ($tssParams.Contains('TssSession') -and $TssSession.IsValidSession()) {
foreach ($folder in $Id) {
$restResponse = $null
$uri = $TssSession.SecretServer + ($TssSession.ApiVersion, "folders", $folder.ToString() -join '/')
$uri = $TssSession.ApiUrl, 'folders', $folder.ToString() -join '/'
$uri = $uri + '?' + "getAllChildren=$GetChildren" + "&" + "includeAssociatedTemplates=$IncludeTemplates"

$invokeParams.Uri = $Uri
$invokeParams.Method = 'GET'

$invokeParams.PersonalAccessToken = $TssSession.AccessToken
Write-Verbose "$($invokeParams.Method) $uri"
try {
$restResponse = Invoke-TssRestApi @invokeParams
} catch {
Expand Down
4 changes: 3 additions & 1 deletion src/functions/Get-TssSecret.ps1
Expand Up @@ -100,7 +100,7 @@
if ($tssParams.Contains('TssSession') -and $TssSession.IsValidSession()) {
foreach ($secret in $Id) {
$restResponse = $null
$uri = $TssSession.SecretServer + ($TssSession.ApiVersion, "secrets", $secret.ToString() -join '/')
$uri = $TssSession.ApiUrl, 'secrets', $secret.ToString() -join '/'

$body = @{}
if ($PSCmdlet.ParameterSetName -eq 'restricted') {
Expand Down Expand Up @@ -136,6 +136,8 @@
}

$invokeParams.PersonalAccessToken = $TssSession.AccessToken

Write-Verbose "$($invokeParams.Method) $uri with $body"
try {
$restResponse = Invoke-TssRestApi @invokeParams
} catch {
Expand Down
4 changes: 3 additions & 1 deletion src/functions/Get-TssSecretField.ps1
Expand Up @@ -83,9 +83,10 @@
}

process {
Write-Verbose "Provided command parameters: $(. $GetInvocation $PSCmdlet.MyInvocation)"
if ($tssParams.Contains('TssSession') -and $TssSession.IsValidSession()) {
foreach ($secret in $Id) {
$uri = $TssSession.SecretServer + ($TssSession.ApiVersion, 'secrets', $secret.ToString() -join '/')
$uri = $TssSession.ApiUrl, 'secrets', $secret.ToString() -join '/'
$restResponse = $null

$body = @{}
Expand Down Expand Up @@ -134,6 +135,7 @@
$invokeParams.OutFile = $OutFile
}
}
Write-Verbose "$($invokeParams.Method) $uri with $body"
try {
$restResponse = Invoke-TssRestApi @invokeParams
} catch {
Expand Down
4 changes: 2 additions & 2 deletions src/functions/Get-TssSecretTemplate.ps1
Expand Up @@ -44,12 +44,12 @@
if ($tssParams.Contains('TssSession') -and $TssSession.IsValidSession()) {
foreach ($template in $Id) {
$restResponse = $null
$uri = $TssSession.SecretServer + ($TssSession.ApiVersion, "secret-templates", $template.ToString() -join '/')
$uri = $TssSession.ApiUrl, 'secret-templates', $template.ToString() -join '/'
$invokeParams.Uri = $Uri
$invokeParams.Method = 'GET'
$invokeParams.PersonalAccessToken = $TssSession.AccessToken

Write-Verbose "$($invokeParas.Method) $uri with $body"
Write-Verbose "$($invokeParas.Method) $uri"
try {
$restResponse = Invoke-TssRestApi @invokeParams
} catch {
Expand Down
39 changes: 23 additions & 16 deletions src/functions/New-TssSession.ps1
Expand Up @@ -26,19 +26,17 @@
A prompt to enter the password for the apiuser is given by PowerShell. Upon successful authentication the response from the oauth2/token endpoint is output to the console.
.EXAMPLE
PS C:\> $secretCred = [pscredential]::new('ssadmin',(ConvertTo-SecureString -String 'F@#R*(@#$SFSDF1234' -AsPlainText -Force)))
PS C:\> $session = nts https://ssvault.com/SecretServer $secretCred
Utilize alias for New-TssSession, nts, to create the session object
.EXAMPLE
PS C:\> $session = nts https://ssvault.com/SecretServer $secretCred
Utilize alias for New-TssSession, nts, to create the session object
Create a credential object
Use the alias nts to create a session object
.OUTPUTS
TssSession.
#>
[cmdletbinding(SupportsShouldProcess)]
[OutputType('TssSession')]
param(
# Secret Server URL
[Parameter(ParameterSetName = 'New',Mandatory)]
Expand Down Expand Up @@ -71,7 +69,6 @@
}

process {
Write-Verbose "Provided command parameters: $(. $GetInvocation $PSCmdlet.MyInvocation)"
if (-not $newTssParams['AccessToken']) {
if ($newTssParams.Contains('SecretServer')) {
$uri = $SecretServer, "oauth2/token" -join '/'
Expand Down Expand Up @@ -100,16 +97,26 @@

if ($newTssParams['Raw']) {
return $restResponse
} else {
[TssSession]@{
SecretServer = $restResponse.SecretServer
AccessToken = $restResponse.access_token
RefreshToken = $restResponse.refresh_token
ExpiresIn = $restResponse.expires_in
TokenType = $restResponse.token_type
StartTime = [datetime]::Now
TimeOfDeath = [datetime]::Now.Add([timespan]::FromSeconds($restResponse.expires_in))
}
if ($restResponse) {
$sessionObj = [TssSession]::new()
$sessionObj.SecretServer = $restResponse.SecretServer
$sessionObj.ApiUrl =
if ( ($restResponse.SecretServer).PathAndQuery -eq '/') {
[string]$restResponse.SecretServer + $sessionObj.ApiVersion
} elseif ( ($restResponse.SecretServer).PathAndQuery.Length -gt 1) {
[string]$restResponse.SecretServer, $sessionObj.ApiVersion -join '/'
} elseif ( ($restResponse.SecretServer).Segments -contains 'api/') {
[string]$restResponse.SecretServer
}
$sessionObj.AccessToken = $restResponse.access_token
$sessionObj.RefreshToken = $restResponse.refresh_token
$sessionObj.ExpiresIn = $restResponse.expires_in
$sessionObj.TokenType = $restResponse.token_type
$sessionObj.StartTime = [datetime]::Now
$sessionObj.TimeOfDeath = [datetime]::Now.Add([timespan]::FromSeconds($restResponse.expires_in))

return $sessionObj
}
}
if ($newTssParams['SecretServer'] -and $newTssParams['AccessToken']) {
Expand Down
8 changes: 4 additions & 4 deletions src/functions/Search-TssSecret.ps1
Expand Up @@ -179,7 +179,7 @@
process {
Write-Verbose "Provided command parameters: $(. $GetInvocation $PSCmdlet.MyInvocation)"
if ($tssParams.Contains('TssSession') -and $TssSession.IsValidSession()) {
$uri = $TssSession.SecretServer + ( $TssSession.ApiVersion, "secrets" -join '/')
$uri = $TssSession.ApiUrl, 'secrets' -join '/'
$uri += "?take=$($TssSession.Take)"
$uri += "&filter.includeRestricted=true"

Expand Down Expand Up @@ -241,14 +241,14 @@
}
}
}
$uriFilter = $filters -join "&"
$uriFilter = $filters -join '&'
Write-Verbose "Filters: $uriFilter"
$uri = $uri, $uriFilter -join "&"
$uri = $uri, $uriFilter -join '&'

$invokeParams.Uri = $uri
$invokeParams.PersonalAccessToken = $TssSession.AccessToken
$invokeParams.Method = 'GET'
Write-Verbose "$($invokeParams.Method) $uri with $body"
Write-Verbose "$($invokeParams.Method) $uri"
try {
$restResponse = Invoke-TssRestApi @invokeParams
} catch {
Expand Down
7 changes: 4 additions & 3 deletions src/functions/Set-TssSecret.ps1
Expand Up @@ -102,7 +102,7 @@

foreach ($secret in $Id) {
if ($tssParams.Contains('Field')) {
$uri = $TssSession.SecretServer + ($TssSession.ApiVersion, "secrets", $secret, "fields", $Field -join "/")
$uri = $TssSession.ApiUrl, 'secrets', $secret, 'fields', $Field -join "/"
if ($TssParams.Contains('Clear') -and $TssParams.Contains('Value')) {
Write-Warning "Clear and Value provided, only one is supported"
return
Expand All @@ -122,6 +122,7 @@
$invokeParams.Method = 'PUT'

if (-not $PSCmdlet.ShouldProcess("$($invokeParams.Method) $uri with $($invokeParams.Body)")) { return }
Write-Verbose "$($invokeParams.Method) $uri with $body"
try {
$restResponse = Invoke-TssRestApi @invokeParams
} catch {
Expand All @@ -144,7 +145,7 @@
data = @{ }
}

$uri = $TssSession.SecretServer + ($TssSession.ApiVersion, "secrets", $secret, "email" -join "/")
$uri = $TssSession.ApiUrl, 'secrets', $secret, 'email' -join "/"

if ($TssParams.Contains('EmailWhenChanged')) {
$sendEmailWhenChanged = @{
Expand Down Expand Up @@ -172,7 +173,7 @@
$invokeParams.Method = 'PATCH'

if (-not $PSCmdlet.ShouldProcess("$($invokeParams.Method) $uri with $($invokeParams.Body)")) { return }

Write-Verbose "$($invokeParams.Method) $uri with $body"
try {
$restResponse = Invoke-TssRestApi @invokeParams
} catch {
Expand Down
4 changes: 3 additions & 1 deletion src/parts/GetTssVersionObject.ps1
Expand Up @@ -4,6 +4,7 @@
.Description
Creates an instance of the TssVersion class to output based on the calling command
#>
[cmdletbinding()]
param(
[TssSession]
$TssSession,
Expand All @@ -20,11 +21,12 @@ begin {
process {
$source = $PSCmdlet.MyInvocation.MyCommand

$uri = $TssSession.SecretServer + ($TssSession.ApiVersion, "version" -join '/')
$uri = $TssSession.ApiUrl, 'version' -join '/'
$invokeParams.Uri = $Uri
$invokeParams.Method = 'GET'
$invokeParams.PersonalAccessToken = $TssSession.AccessToken

Write-Verbose "$($invokeParams.Method) $uri"
try {
$restResponse = Invoke-TssRestApi @invokeParams
} catch {
Expand Down
10 changes: 5 additions & 5 deletions tests/functions/Invoke-TssRestApi.Tests.ps1
Expand Up @@ -21,12 +21,12 @@ Describe "$commandName works" {
BeforeDiscovery {
$session = New-TssSession -SecretServer $ss -Credential $ssCred

$invokeParams = @{ }
$invokeParams.Uri = $session.SecretServer + ($session.ApiVersion, "version" -join '/')
$invokeParams.Method = 'GET'
$invokeParams.PersonalAccessToken = $TssSession.AccessToken
$invokeParams = @{
Uri = $session.ApiUrl, "version" -join '/'
Method = 'GET'
PersonalAccessToken = $session.AccessToken
}
$restResponse = Invoke-TssRestApi @invokeParams

$session.SessionExpire()
}
Context "Checking" -Foreach @{restResponse = $restResponse} {
Expand Down
16 changes: 9 additions & 7 deletions tests/functions/New-TssSession.Tests.ps1
Expand Up @@ -8,7 +8,7 @@ Describe "$commandName verify parameters" {
[object[]]$currentParams = ([Management.Automation.CommandMetaData]$ExecutionContext.SessionState.InvokeCommand.GetCommand($commandName, 'Function')).Parameters.Keys
$unknownParameters = Compare-Object -ReferenceObject $knownParameters -DifferenceObject $currentParams -PassThru
}
Context "Verify parameters" -Foreach @{currentParams = $currentParams} {
Context "Verify parameters" -Foreach @{currentParams = $currentParams } {
It "$commandName should contain <_> parameter" -TestCases $knownParameters {
$_ -in $currentParams | Should -Be $true
}
Expand All @@ -19,18 +19,20 @@ Describe "$commandName verify parameters" {
}

Describe "$commandName updates session object" {
BeforeAll {
$apiV = 'api/v1'
$session = New-TssSession -SecretServer $ss -Credential $ssCred
}
Context "Oauth2 authentication" {
BeforeEach {
[uri]$secretServer = $ss
$apiV = 'api/v1'
$session = New-TssSession -SecretServer $secretServer -Credential $ssCred
}
It "Populates SecretServer Propety" {
$session.SecretServer | Should -Be $secretServer
$session.SecretServer | Should -Be ([uri]$ss)
}
It "ApiVersion Propety is set" {
$session.ApiVersion | Should -Be $apiV
}
It "ApiUrl Propety is set" {
$session.ApiUrl | Should -Not -BeNullOrEmpty
}
It "Populates AccessToken Property" {
$session.AccessToken | Should -Not -BeNullOrEmpty
}
Expand Down

0 comments on commit ce0aeba

Please sign in to comment.