Skip to content

Commit

Permalink
fix(ps): add ConvertFrom-PEM to SetupUtils
Browse files Browse the repository at this point in the history
  • Loading branch information
szymonos committed Oct 22, 2024
1 parent 0a4829d commit 934dd8e
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 1 deletion.
1 change: 1 addition & 0 deletions .assets/scripts/modules_update.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ begin {
'do-common' = @{
SetupUtils = @{
certs = @(
'ConvertFrom-PEM'
'ConvertTo-PEM'
'Get-Certificate'
)
Expand Down
53 changes: 53 additions & 0 deletions modules/SetupUtils/Functions/certs.ps1
Original file line number Diff line number Diff line change
@@ -1,3 +1,56 @@
function ConvertFrom-PEM {
[CmdletBinding()]
[OutputType([System.Security.Cryptography.X509Certificates.X509Certificate2[]])]
param (
[Parameter(Mandatory, ValueFromPipeline, ParameterSetName = 'FromString')]
[string]$InputObject,

[Parameter(Mandatory, Position = 0, ParameterSetName = 'FromPath')]
[ValidateScript({ Test-Path $_ -PathType 'Leaf' }, ErrorMessage = "'{0}' is not a valid file path.")]
[string]$Path
)

begin {
# list to store input certificate strings
$pemTxt = [System.Collections.Generic.List[string]]::new()
# hashset for storing parsed pem certificates
$pemSplit = [System.Collections.Generic.HashSet[string]]::new()
# list to store decoded certificates
$x509Certs = [System.Collections.Generic.List[Security.Cryptography.X509Certificates.X509Certificate2]]::new()
}

process {
switch ($PsCmdlet.ParameterSetName) {
FromPath {
# read certificate file
Resolve-Path $Path | ForEach-Object {
$pemTxt.Add([IO.File]::ReadAllText($_))
}
continue
}
FromString {
$InputObject.ForEach({ $pemTxt.Add($_) })
continue
}
}
}

end {
# parse certificate string
[regex]::Matches(
[string]::Join("`n", $pemTxt).Replace("`r`n", "`n"),
'(?<=-{5}BEGIN[\w ]+CERTIFICATE-{5}\n)[\S\n]+(?=\n-{5}END[\w ]+CERTIFICATE-{5})'
).Value.ForEach({ $pemSplit.Add($_) | Out-Null })
# convert PEM encoded certificates to X509 certificate objects
foreach ($pem in $pemSplit) {
$decCrt = [Security.Cryptography.X509Certificates.X509Certificate2]::new([Convert]::FromBase64String($pem))
$x509Certs.Add($decCrt)
}

return $x509Certs
}
}

function ConvertTo-PEM {
[CmdletBinding()]
[OutputType([System.Collections.Generic.List[string]])]
Expand Down
3 changes: 2 additions & 1 deletion modules/SetupUtils/SetupUtils.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
RootModule = 'SetupUtils.psm1'

# Version number of this module.
ModuleVersion = '0.3.1'
ModuleVersion = '0.4.0'

# Supported PSEditions
CompatiblePSEditions = @('Core')
Expand Down Expand Up @@ -71,6 +71,7 @@
# Functions to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no functions to export.
FunctionsToExport = @(
# certs
'ConvertFrom-PEM'
'ConvertTo-PEM'
'Get-Certificate'
# common
Expand Down
1 change: 1 addition & 0 deletions modules/SetupUtils/SetupUtils.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ $ErrorActionPreference = 'Stop'
$exportModuleMemberParams = @{
Function = @(
# certs
'ConvertFrom-PEM'
'ConvertTo-PEM'
'Get-Certificate'
# common
Expand Down

0 comments on commit 934dd8e

Please sign in to comment.