Skip to content

Commit

Permalink
Fix New-VenafiSession -AccessToken, undo double json on verbose (#253)
Browse files Browse the repository at this point in the history
  • Loading branch information
gdbarron authored Jan 31, 2024
1 parent 77e9526 commit 0d51990
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 16 deletions.
5 changes: 2 additions & 3 deletions RELEASE.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
- Add parallel functionality to `Remove-VdcObject`. PS Core for now, Windows PowerShell coming soon.
- Fix invalid function reference with `New-VdcCapiApplication`, [#247](https://github.com/Venafi/VenafiPS/issues/247)
- Fix wilcard certificate not accepted with `New-VdcCapiApplication`, [#248](https://github.com/Venafi/VenafiPS/issues/248)
- Fix property not found error with `New-VenafiSession -AccessToken`, [#252](https://github.com/Venafi/VenafiPS/issues/252)
- Update `Invoke-VenafiRestMethod` to ensure parameter verbose output does not convert the body to json twice
30 changes: 17 additions & 13 deletions VenafiPS/Public/Invoke-VenafiRestMethod.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -202,39 +202,43 @@ function Invoke-VenafiRestMethod {
# in the case of inital authentication, eg, there won't be any
if ( $allHeaders ) { $params.Headers = $allHeaders }

if ( $UseDefaultCredential.IsPresent -and $Certificate ) {
throw 'You cannot use UseDefaultCredential and Certificate parameters together'
}

if ( $UseDefaultCredential.IsPresent ) {
$params.Add('UseDefaultCredentials', $true)
}

if ( $Body ) {
$restBody = $Body
switch ($Method.ToLower()) {
'head' {
# a head method requires the params be provided as a query string, not body
# invoke-webrequest does not do this so we have to build the string manually
$newUri = New-HttpQueryString -Uri $uri -QueryParameter $Body
$params.Uri = $newUri
$params.Body = $null
Write-Verbose $newUri
}

'get' {
$params.Body = $restBody
$params.Body = $Body
}

Default {
$restBody = ConvertTo-Json $Body -Depth 20 -Compress
$params.Body = $restBody
$preJsonBody = $Body
$params.Body = (ConvertTo-Json $Body -Depth 20 -Compress)
}
}
}

if ( $UseDefaultCredential.IsPresent -and $Certificate ) {
throw 'You cannot use UseDefaultCredential and Certificate parameters together'
if ( $preJsonBody ) {
$paramsToWrite = $params.Clone()
$paramsToWrite.Body = $preJsonBody
$paramsToWrite | Write-VerboseWithSecret
} else {
$params | Write-VerboseWithSecret
}

if ( $UseDefaultCredential.IsPresent ) {
$params.Add('UseDefaultCredentials', $true)
}

$params | Write-VerboseWithSecret

# ConvertTo-Json, used in Write-VerboseWithSecret, has an issue with certificates
# add this param after
if ( $Certificate ) {
Expand Down
3 changes: 3 additions & 0 deletions VenafiPS/Public/New-VenafiSession.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -353,12 +353,15 @@ function New-VenafiSession {
# we don't have the expiry so create one
# rely on the api call itself to fail if access token is invalid
Expires = (Get-Date).AddMonths(12)
AccessToken = $null
}
$newSession.Token.AccessToken = if ( $AccessToken -is [string] ) { New-Object System.Management.Automation.PSCredential('AccessToken', ($AccessToken | ConvertTo-SecureString -AsPlainText -Force)) }
elseif ($AccessToken -is [pscredential]) { $AccessToken }
elseif ($AccessToken -is [securestring]) { New-Object System.Management.Automation.PSCredential('AccessToken', $AccessToken) }
else { throw 'Unsupported type for -AccessToken. Provide either a String, SecureString, or PSCredential.' }

# validate token
$null = Invoke-VenafiRestMethod -UriRoot 'vedauth' -UriLeaf 'Authorize/Verify' -VenafiSession $newSession
}

'VaultAccessToken' {
Expand Down

0 comments on commit 0d51990

Please sign in to comment.