Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix TLSPC certificate export #238

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion RELEASE.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
- Fix path error when running in parallel on PS Core, [#235](https://github.com/Venafi/VenafiPS/issues/235)
- Fix incorrect path for Sodium in `Export-VcCertificate`, [#234](https://github.com/Venafi/VenafiPS/issues/234)
6 changes: 3 additions & 3 deletions VenafiPS/Private/Invoke-VenafiParallel.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ function Invoke-VenafiParallel {

# if we only have 1 item or limited to 1 at a time, no need for parallel
if ( $PSVersionTable.PSVersion.Major -ge 7 -and @($InputObject).Count -gt 1 -and $ThrottleLimit -gt 1 ) {

$thisDir = $PSScriptRoot
$starterSb = {

Expand All @@ -145,10 +146,9 @@ function Invoke-VenafiParallel {
do {

# let threads run
Start-Sleep -Seconds 1
Start-Sleep -Milliseconds 100

$completedJobsCount =
$job.ChildJobs.Where({ $_.State -notin 'NotStarted', 'Running' }).Count
$completedJobsCount = $job.ChildJobs.Where({ $_.State -notin 'NotStarted', 'Running' }).Count

# get latest job info
$job | Receive-Job
Expand Down
4 changes: 2 additions & 2 deletions VenafiPS/Private/Split-CertificateData.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ function Split-CertificateData {
if ($pemLines[$i].Contains('-----END')) {
$thisPemLines = $pemLines[$iStart..$i]
if ( $pemLines[$i].Contains('KEY-----')) {
$keyPem = ($thisPemLines | Join-String -Separator "`n") -replace "`r"
$keyPem = ($thisPemLines -join "`n") -replace "`r"
}
else {
$certPem += ($thisPemLines | Join-String -Separator "`n") -replace "`r"
$certPem += ($thisPemLines -join "`n") -replace "`r"
}
continue
}
Expand Down
57 changes: 35 additions & 22 deletions VenafiPS/Public/Export-VcCertificate.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ function Export-VcCertificate {
Export certificate data in PEM format. You can retrieve the certificate, chain, and key.

.PARAMETER ID
Full path to the certificate
Certificate ID, also known as uuid. Use Find-VcCertificate or Get-VcCertificate to determine the ID.
You can pipe those functions as well.

.PARAMETER PrivateKeyPassword
Password required to include the private key.
Expand All @@ -18,6 +19,7 @@ function Export-VcCertificate {

.PARAMETER OutPath
Folder path to save the certificate to. The name of the file will be determined automatically.
For each certificate a directory will be created in this folder with the format Name-ID.

.PARAMETER ThrottleLimit
Limit the number of threads when running in parallel; the default is 100. Applicable to PS v7+ only.
Expand Down Expand Up @@ -155,24 +157,25 @@ function Export-VcCertificate {

end {
if ( $PrivateKeyPassword ) {
$currDir = $PSScriptRoot
$sb = {

$out = [pscustomobject] @{
certificateId = $PSItem.ID
error = ''
}

Import-Module (Join-Path -Path (Split-Path $using:thisDir -Parent) -ChildPath 'import/PSSodium/PSSodium.psd1') -Force

$params = $PSItem.InvokeParams

$thisCert = Get-VcCertificate -id $PSItem.ID

if ( -not $thisCert.dekHash ) {
$out.error = 'Private key not found'
return $out
}

Import-Module (Join-Path -Path (Split-Path $using:currDir -Parent) -ChildPath 'import/PSSodium/PSSodium.psd1') -Force

$params = $PSItem.InvokeParams

$publicKey = Invoke-VenafiRestMethod -UriLeaf "edgeencryptionkeys/$($thisCert.dekHash)" | Select-Object -ExpandProperty key

$privateKeyPasswordEnc = ConvertTo-SodiumEncryptedString -Text $PSItem.PrivateKeyPassword -PublicKey $publicKey
Expand All @@ -181,7 +184,15 @@ function Export-VcCertificate {

$innerResponse = Invoke-VenafiRestMethod @params

if ( -not $innerResponse.Content ) { return }
if ($innerResponse.StatusCode -notin 200, 201, 202) {
$out.error = $innerResponse.StatusDescription
return $out
}

if ( -not $innerResponse.Content ) {
$out.error = 'No certificate data received'
return $out
}

$zipFile = New-TemporaryFile
$unzipPath = Join-Path -Path (Split-Path -Path $zipFile -Parent) -ChildPath $PSItem.ID
Expand All @@ -190,18 +201,15 @@ function Export-VcCertificate {
# always save the zip file then decide to copy to the final destination or return contents
[IO.File]::WriteAllBytes($zipFile, $innerResponse.Content)

$unzipFiles = Expand-Archive -Path $zipFile -DestinationPath $unzipPath -PassThru

if ($innerResponse.StatusCode -notin 200, 201, 202) {
$out.error = $innerResponse.StatusDescription
return $out
}
Expand-Archive -Path $zipFile -DestinationPath $unzipPath
$unzipFiles = Get-ChildItem -Path $unzipPath

if ( $using:outPath ) {
# copy files to final desination
$outPath = Resolve-Path -Path $using:OutPath
$unzipFiles | Copy-Item -Destination $outPath -Force
$out | Add-Member @{'outPath' = $outPath }
$dest = Join-Path -Path (Resolve-Path -Path $using:OutPath) -ChildPath ('{0}-{1}' -f $thisCert.certificateName, $thisCert.certificateId)
$null = New-Item -Path $dest -ItemType Directory -Force
$unzipFiles | Copy-Item -Destination $dest -Force
$out | Add-Member @{'outPath' = $dest }
}
else {
# pull in the contents so we can provide them
Expand All @@ -221,13 +229,15 @@ function Export-VcCertificate {
}
if ($pemLines[$i].Contains('-----END')) {
$thisPemLines = $pemLines[$iStart..$i]
$certPem += $thisPemLines | Join-String
$certPem += $thisPemLines -join "`n"
continue
}
}

$out | Add-Member @{'CertPem' = $certPem[0] }
$out | Add-Member @{'ChainPem' = $certPem[1..($certPem.Count - 1)] }
if ( $using:IncludeChain ) {
$out | Add-Member @{'ChainPem' = $certPem[1..($certPem.Count - 1)] }
}
}
}
}
Expand All @@ -240,8 +250,11 @@ function Export-VcCertificate {
}
}
else {
# cert/chain only, no private key. different api call.
# cert/chain only, no private key. different api call, better performance.
$sb = {

$thisCert = Get-VcCertificate -id $PSItem.ID

$params = $PSItem.InvokeParams
$innerResponse = Invoke-VenafiRestMethod @params
$certificateData = $innerResponse.Content
Expand All @@ -253,8 +266,9 @@ function Export-VcCertificate {

if ( $certificateData ) {
if ( $using:OutPath ) {
# if ( $certificateData ) {
$outFile = Join-Path -Path (Resolve-Path -Path $using:OutPath) -ChildPath ('{0}.{1}' -f $PSItem.ID, $PSItem.InvokeParams.Body.format)
$dest = Join-Path -Path (Resolve-Path -Path $using:OutPath) -ChildPath ('{0}-{1}' -f $thisCert.certificateName, $thisCert.certificateId)
$null = New-Item -Path $dest -ItemType Directory -Force
$outFile = Join-Path -Path $dest -ChildPath ('{0}.{1}' -f $PSItem.ID, $PSItem.InvokeParams.Body.format)
try {
$sw = [IO.StreamWriter]::new($outFile, $false, [Text.Encoding]::ASCII)
$sw.WriteLine($certificateData)
Expand All @@ -264,8 +278,7 @@ function Export-VcCertificate {
if ($null -ne $sw) { $sw.Close() }
}

$out | Add-Member @{'outFile' = $outFile }
# }
$out | Add-Member @{'outPath' = $dest }
}
else {
$out | Add-Member @{'certificateData' = $certificateData }
Expand Down
Loading