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 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 |
$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 Scope Specifies the scope of extension resolution. Extensions can be installed either globally, accessible to all users, or specific to the current user. When resolving extensions, you can scope the resolution to one or the other, or both. By default extensions are resolved relative to both scopes, with global scope taking priority. Valid values are 'Global', 'User' or 'AllScopes". The default value is 'AllScopes'. .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)] [ValidateSet("AllScopes", "Global", "User")] [string]$Scope="AllScopes", [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" if ($Scope -ne "Global") { $extRoot = [ACMESharp.POSH.AcmeCmdlet]::UserExtensionsRoot $extPath = "$($extRoot)\$($provMod.Name).extlnk" $extScope = "User" } if ($Scope -ne "User" -and ((-not $extPath) -or (-not (Test-Path $extPath)))) { $extRoot = [ACMESharp.POSH.AcmeCmdlet]::SystemExtensionsRoot $extPath = "$($extRoot)\$($provMod.Name).extlnk" $extScope = "Global" } [ordered]@{ acmeMod = $acmeMod provMod = $provMod extRoot = $extRoot extPath = $extPath extScope = $extScope } } 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 Scope Specifies the scope of extension resolution. Extensions can be installed either globally, accessible to all users, or specific to the current user. When resolving extensions, you can scope the resolution to one or the other, or both. By default extensions are resolved relative to both scopes, with user scope taking priority. Valid values are 'Global', 'User' or 'All". The default value is 'AllScopes'. .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)] [ValidateSet("AllScopes", "Global", "User")] [string]$Scope="AllScopes", [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" $userExtLinks = @() if ($Scope -ne "Global") { $extRoot = [ACMESharp.POSH.AcmeCmdlet]::UserExtensionsRoot $extPath = "$($extRoot)\*.extlnk" if (Test-Path -Path $extRoot -PathType Container) { $userExtLinks = Get-ChildItem $extPath $userExtLinks | Add-Member -NotePropertyName ExtScope -NotePropertyValue User } } $sysExtLinks = @() if ($Scope -ne "User") { $extRoot = [ACMESharp.POSH.AcmeCmdlet]::SystemExtensionsRoot $extPath = "$($extRoot)\*.extlnk" if (Test-Path -Path $extRoot -PathType Container) { $sysExtLinks = Get-ChildItem $extPath $sysExtLinks | Add-Member -NotePropertyName ExtScope -NotePropertyValue Global } } if ($ModuleName) { $extLinks = $userExtLinks | Where-Object { ($_.Name -replace '\.extlnk$','') -eq $ModuleName } if (-not $extLinks) { $extLinks = $sysExtLinks | Where-Object { ($_.Name -replace '\.extlnk$','') -eq $ModuleName } } } else { $extLinks = @($userExtLinks) + @($sysExtLinks) } $extLinks | Select-Object @{ Name = "Name" Expression = { $_.Name -replace '\.extlnk$','' } },@{ Name = "Scope" Expression = { $_.ExtScope } },@{ Name = "JSON" Expression = { Get-Content $_ | ConvertFrom-Json } } | Select-Object Name,Scope,@{ 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 Scope Specifies the scope under which the extension will be enabled. Extensions can be enabled either globally, accessible to all users, or specific to the current user. When resolving extensions, you can scope the resolution to one or the other, or both. By default extensions are resolved relative to both scopes, with user scope taking priority. For enabling an extension, you can specify `Global` or `User` scope. The default value is 'Global'. .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)] [ValidateSet("Global", "User")] [string]$Scope="Global", [Parameter(Mandatory=$false)] [string]$AcmeVersion ) $deps = Resolve-ExtensionModule -ModuleName $ModuleName -Scope $Scope -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 Scope Specifies the scope under which the extension will be disabled. Extensions can be enabled either globally, accessible to all users, or specific to the current user. When resolving extensions, you can scope the resolution to one or the other, or both. By default extensions are resolved relative to both scopes, with user scope taking priority. For disabling an extension, you can specify `Global`, `User` or `AllScopes` scope. The default value is 'AllScopes'. .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)] [ValidateSet("AllScopes", "Global", "User")] [string]$Scope="AllScopes", [Parameter(Mandatory=$false)] [string]$AcmeVersion ) $deps = Resolve-ExtensionModule -ModuleName $ModuleName -Scope $Scope -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) "@ } } } |