ACMESharp-Extensions/ACMESharp-Extensions.psm1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333

$ErrorActionPreference = 'Stop'

function Resolve-ExtensionModule {
<#
.PARAMETER ModuleName
Required, the name of the PowerShell module that is an ACMESharp Extension Module.

.PARAMETER ModuleVersion
An optional version spec, useful if multiple version of the target Extension Module
are installed.

The spec can be an exact version string or a `-like` pattern to be matched.

.PARAMETER AcmeVersion
An optional version spec, useful if multiple versions of the core ACMESharp module is installed,
this will specify which module installation will be targeted for enabling the module.

The spec can be an exact version string or a `-like` pattern to be matched.
#>

    param(
        [Parameter(Mandatory)]
        [string]$ModuleName,
        [Parameter(Mandatory=$false)]
        [string]$ModuleVersion,

        [Parameter(Mandatory=$false)]
        [string]$AcmeVersion
    )

    ## Get any modules that are resident in the current session and
    ## any module versions that are available on the current system
    $acmeMods = @(Get-Module ACMESharp) + @(Get-Module -ListAvailable ACMESharp | Sort-Object -Descending Version)
    $provMods = @(Get-Module $ModuleName) + (Get-Module -ListAvailable $ModuleName | Sort-Object -Descending Version)

    if ($AcmeVersion) {
        $acmeMod = $acmeMods | Where-Object { $_.Version -like $AcmeVersion } | Select-Object -First 1
    }
    else {
        $acmeMod = $acmeMods | Select-Object -First 1
    }

    if ($ModuleVersion) {
        $provMod = $provMods | Where-Object { $_.Version -like $ModuleVersion } | Select-Object -First 1
    }
    else {
        $provMod = $provMods | Select-Object -First 1
    }

    if (-not $provMod -or -not $provMod.ModuleBase) {
        Write-Error "Cannot resolve extension module's base [$ModuleName]"
        return
    }
    if (-not $acmeMod -or -not $acmeMod.ModuleBase) {
        Write-Error "Cannot resolve ACMESharp module's own base"
        return
    }
    if (-not (Test-Path $acmeMod.ModuleBase)) {
        Write-Error "Cannot find ACMESharp module base [$($x.ModuleBase)]"
        return
    }

    $extRoot = "$($acmeMod.ModuleBase)\EXT"
    $extPath = "$($extRoot)\$($provMod.Name).extlnk"

    [ordered]@{
        acmeMod = $acmeMod
        provMod = $provMod
        extRoot = $extRoot
        extPath = $extPath
    }        
}

function Get-ExtensionModule {
<#
.PARAMETER ModuleName
Optional, the name of the PowerShell module that is an enabled ACMESharp Extension Module. If unspecified, then all enabled Extension Modules will be returned.

.PARAMETER AcmeVersion
An optional version spec, useful if multiple versions of the core ACMESharp module is installed,
this will specify which module installation will be targeted for inspecting for enabled modules.

The spec can be an exact version string or a `-like` pattern to be matched.
#>

    param(
        [Parameter(Mandatory=$false)]
        [string]$ModuleName,

        [Parameter(Mandatory=$false)]
        [string]$AcmeVersion
    )

    ## Get any modules that are resident in the current session and
    ## any module versions that are available on the current system
    $acmeMods = @(Get-Module ACMESharp) + @(Get-Module -ListAvailable ACMESharp | Sort-Object -Descending Version)
    if ($AcmeVersion) {
        $acmeMod = $acmeMods | Where-Object { $_.Version -like $AcmeVersion } | Select-Object -First 1
    }
    else {
        $acmeMod = $acmeMods | Select-Object -First 1
    }
    
    if (-not $acmeMod -or -not $acmeMod.ModuleBase) {
        Write-Error "Cannot resolve ACMESharp module's own base"
        return
    }
    if (-not (Test-Path $acmeMod.ModuleBase)) {
        Write-Error "Cannot find ACMESharp module base [$($x.ModuleBase)]"
        return
    }

    $extRoot = "$($acmeMod.ModuleBase)\EXT"
    $extPath = "$($extRoot)\*.extlnk"

    $extLinks = Get-ChildItem $extPath
    if ($ModuleName) {
        $extLinks = $extLinks | Where-Object { ($_.Name -replace '\.extlnk$','') -eq $ModuleName }
    }

    $extLinks | Select-Object @{
        Name = "Name"
        Expression = { $_.Name -replace '\.extlnk$','' }
    },@{
        Name = "JSON"
        Expression = { Get-Content $_ | ConvertFrom-Json }
    } | Select-Object Name,@{
        Name = "Version"
        Expression = { $_.JSON.Version }
    },@{
        Name = "Path"
        Expression = { $_.JSON.Path }
    }
}

function Enable-ExtensionModule {
<#
.PARAMETER ModuleName
Required, the name of the PowerShell module that is an ACMESharp Extension Module.

.PARAMETER ModuleVersion
An optional version spec, useful if multiple version of the target Extension Module
are installed.

The spec can be an exact version string or a `-like` pattern to be matched.

.PARAMETER AcmeVersion
An optional version spec, useful if multiple versions of the core ACMESharp module is installed,
this will specify which module installation will be targeted for enabling the module.

The spec can be an exact version string or a `-like` pattern to be matched.
#>

    param(
        [Parameter(Mandatory)]
        [string]$ModuleName,
        [Parameter(Mandatory=$false)]
        [string]$ModuleVersion,

        [Parameter(Mandatory=$false)]
        [string]$AcmeVersion
    )
    
    $deps = Resolve-ExtensionModule -ModuleName $ModuleName -AcmeVersion $AcmeVersion
    if (-not $deps) {
        return
    }

    if (Test-Path $deps.extPath) {
        Write-Error "Extension module already enabled"
        return
    }
    mkdir -Force $deps.extRoot
    Write-Output "Installing Extension Module to [$($deps.extPath)]"
    @{
        Path = $deps.provMod.ModuleBase
        Version = $deps.provMod.Version.ToString()
    } | ConvertTo-Json > $deps.extPath
}

function Disable-ExtensionModule {
<#
.PARAMETER ModuleName
Required, the name of the PowerShell module that is an ACMESharp Extension Module.

.PARAMETER ModuleVersion
An optional version spec, useful if multiple version of the target Extension Module
are installed.

The spec can be an exact version string or a `-like` pattern to be matched.

.PARAMETER AcmeVersion
An optional version spec, useful if multiple versions of the core ACMESharp module is installed,
this will specify which module installation will be targeted for enabling the module.

The spec can be an exact version string or a `-like` pattern to be matched.
#>

    param(
        [Parameter(Mandatory)]
        [string]$ModuleName,
        [Parameter(Mandatory=$false)]
        [string]$ModuleVersion,

        [Parameter(Mandatory=$false)]
        [string]$AcmeVersion
    )
    
    $deps = Resolve-ExtensionModule -ModuleName $ModuleName -AcmeVersion $AcmeVersion
    if (-not $deps) {
        return
    }

    if (-not (Test-Path $deps.extPath)) {
        Write-Error "Extension module is not enabled"
        return
    }
    Write-Output "Removing Extension Module installed at [$($deps.extPath)]"
    Remove-Item -Confirm $deps.extPath
}

function Export-ExtensionAsMarkdown {
    [CmdletBinding()]
    param(
        [Parameter(ParameterSetName="ChallengeHandler")]
        [string]$ChallengeHandler,
        [Parameter(ParameterSetName="Installer")]
        [string]$Installer,
        [Parameter(ParameterSetName="Vault")]
        [string]$Vault,
        [Parameter(ParameterSetName="ChallengeDecoder")]
        [string]$ChallengeDecoder,
        [Parameter(ParameterSetName="PkiTool")]
        [string]$PkiTool
    )

    if ($ChallengeHandler) {
        $extType = "Challenge Handler"
        $extName = $ChallengeHandler
        $ext = ACMESharp\Get-ChallengeHandlerProfile -GetChallengeHandler $ChallengeHandler
        $extParams = $ext.Parameters
        $extInst = [ACMESharp.ACME.ChallengeHandlerExtManager]::GetProvider($extName)
    }
    if ($Installer) {
        $extType = "Installer"
        $extName = $Installer
        $ext = ACMESharp\Get-InstallerProfile -GetInstaller $Installer
        $extParams = $ext.Parameters
        $extInst = [ACMESharp.Installer.InstallerExtManager]::GetProvider($extName)
    }
    if ($Vault) {
        $extType = "Vault Storage"
        $extName = $Vault
        ## Forces the load of Vault-related assemblies
        ACMESharp\Get-VaultProfile -ListProfiles | Out-Null
        $ext = [ACMESharp.Vault.VaultExtManager]::GetProviderInfo($extName)
        $extInst = [ACMESharp.Vault.VaultExtManager]::GetProvider($extName)
        $extParams = $extInst.DescribeParameters()
    }
    if ($ChallengeDecoder) {
        $extType = "Challenge Decoder"
        $extName = $ChallengeDecoder
        $ext = ACMESharp\Get-ChallengeHandlerProfile -GetChallengeType $ChallengeDecoder
        $extParams = $ext.Parameters
    }
    if ($PkiTool) {
        $extType = "PKI Tool"
        $extName = $PkiTool
        ## Forces the load of Vault-related assemblies
        ACMESharp\Get-ChallengeHandlerProfile -ListChallengeTypes | Out-Null
        $ext = [ACMESharp.PKI.PkiToolExtManager]::GetProviderInfo($extName)
        $extInst = [ACMESharp.PKI.PkiToolExtManager]::GetProvider($extName)
        $extParams = $extInst.DescribeParameters()
    }

    if (-not $ext) {
        Write-Error "Could not resolve $extType [$extName]"
        return
    }

    ## Get all the type-specific props excluding all the common ones
    $props = $ext.GetType().GetMembers() |
            Where-Object { $_.MemberType -eq "Property" } |
            Where-Object { $_.Name -notin @('Name', 'Label', 'Description', 'Parameters') } |
            Select-Object -ExpandProperty Name

    $extName = if($ext.Name) { $ext.Name }
    ## TODO: Aliases???
    $extLabel = if ($ext.Label) { $ext.Label } else { $extName }

    Write-Output ""
    Write-Output "# $($extType): $($extLabel)"
    if ($ext.Description) {
        Write-Output ""
        Write-Output $ext.Description
    }
    else {
        Write-Output "*(no description)*"
    }
    Write-Output ""
    Write-Output @"
| | |
|-|-|
| **Name:** | ``$($extName)``
"@

    foreach ($p in $props) {
        Write-Output "| **$($p):** | $($ext.$p)"
    }
    if ($extInst) {
        $asmName = $extInst.GetType().Assembly.GetName()
        Write-Output "| **Assembly:** | ``$($asmName.Name)``"
        Write-Output "| **Version:** | ``$($asmName.Version)``"
    }
    Write-Output ""

    if ($extParams) {
        Write-Output("## Parameters")
        foreach ($p in $extParams) {
            $pLabel = if ($p.Label) { $p.Label } else { $p.Name }
            Write-Output @"
---
### $pLabel

$($p.Description)

| | |
|-|-|
| **Name:** | ``$($p.Name)``
| **Type:** | $($p.Type)
| **IsRequired:** | $($p.IsRequired)
| **IsMultiValued:** | $($p.IsMultiValued)

"@

        }
    }
}