| | | 1 | | <# |
| | | 2 | | .SYNOPSIS |
| | | 3 | | Configures HTTPS options for a Kestrun server instance. |
| | | 4 | | .DESCRIPTION |
| | | 5 | | This function allows administrators to set or modify the HTTPS connection adapter options for a Kestrun |
| | | 6 | | server instance, including SSL protocols, client certificate modes, and server certificates. |
| | | 7 | | .PARAMETER Server |
| | | 8 | | The Kestrun server instance to configure. This parameter is mandatory and must be a valid server object. |
| | | 9 | | .PARAMETER Options |
| | | 10 | | The HttpsConnectionAdapterOptions object containing the desired HTTPS configuration settings. |
| | | 11 | | .PARAMETER SslProtocols |
| | | 12 | | Specifies the SSL protocols to be used for HTTPS connections. This parameter is optional and can be set to a specifi |
| | | 13 | | .PARAMETER ClientCertificateMode |
| | | 14 | | Specifies the client certificate mode for HTTPS connections. This parameter is optional and can be set to a specific |
| | | 15 | | .PARAMETER CheckCertificateRevocation |
| | | 16 | | If specified, enables certificate revocation checking for HTTPS connections. This parameter is optional and can be l |
| | | 17 | | .PARAMETER ServerCertificate |
| | | 18 | | Specifies the server certificate to be used for HTTPS connections. This parameter is optional and can be left unset |
| | | 19 | | .PARAMETER ServerCertificateChain |
| | | 20 | | Specifies the server certificate chain to be used for HTTPS connections. This parameter is optional and can be left |
| | | 21 | | .PARAMETER HandshakeTimeout |
| | | 22 | | Specifies the handshake timeout duration in seconds for HTTPS connections. This parameter is optional and can be lef |
| | | 23 | | .PARAMETER PassThru |
| | | 24 | | If specified, the cmdlet will return the modified server instance after applying the HTTPS options. |
| | | 25 | | .OUTPUTS |
| | | 26 | | [Kestrun.Hosting.KestrunHost] |
| | | 27 | | The modified Kestrun server instance with the applied HTTPS options. |
| | | 28 | | .EXAMPLE |
| | | 29 | | Set-KrServerHttpsOptions -Server $server -SslProtocols Tls12 |
| | | 30 | | This command sets the SSL protocols for the specified Kestrun server instance to use TLS 1.2. |
| | | 31 | | .EXAMPLE |
| | | 32 | | Set-KrServerHttpsOptions -Server $server -ClientCertificateMode RequireCertificate |
| | | 33 | | This command sets the client certificate mode for the specified Kestrun server instance to require a client certific |
| | | 34 | | .EXAMPLE |
| | | 35 | | Set-KrServerHttpsOptions -Server $server -CheckCertificateRevocation |
| | | 36 | | This command enables certificate revocation checking for the specified Kestrun server instance. |
| | | 37 | | .EXAMPLE |
| | | 38 | | Set-KrServerHttpsOptions -Server $server -ServerCertificate $cert |
| | | 39 | | This command sets the server certificate for the specified Kestrun server instance. |
| | | 40 | | .EXAMPLE |
| | | 41 | | Set-KrServerHttpsOptions -Server $server -HandshakeTimeout 30 |
| | | 42 | | This command sets the handshake timeout for the specified Kestrun server instance to 30 seconds. |
| | | 43 | | .NOTES |
| | | 44 | | This function is designed to be used in the context of a Kestrun server setup and allows for flexible configuration |
| | | 45 | | $ClientCertificateValidation, $ServerCertificateSelector, and $OnAuthenticate are currently not implemented in this |
| | | 46 | | #> |
| | | 47 | | function Set-KrServerHttpsOptions { |
| | | 48 | | [KestrunRuntimeApi('Definition')] |
| | | 49 | | [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseShouldProcessForStateChangingFunctions', '')] |
| | | 50 | | [CmdletBinding(defaultParameterSetName = 'Items')] |
| | | 51 | | param( |
| | | 52 | | [Parameter(Mandatory = $false, ValueFromPipeline = $true)] |
| | | 53 | | [Kestrun.Hosting.KestrunHost]$Server, |
| | | 54 | | [Parameter(Mandatory = $true, ParameterSetName = 'Options')] |
| | | 55 | | [Microsoft.AspNetCore.Server.Kestrel.Https.HttpsConnectionAdapterOptions]$Options, |
| | | 56 | | [Parameter( ParameterSetName = 'Items')] |
| | | 57 | | [System.Security.Authentication.SslProtocols]$SslProtocols, |
| | | 58 | | [Parameter( ParameterSetName = 'Items')] |
| | | 59 | | [Microsoft.AspNetCore.Server.Kestrel.Https.ClientCertificateMode]$ClientCertificateMode, |
| | | 60 | | [Parameter( ParameterSetName = 'Items')] |
| | | 61 | | [switch]$CheckCertificateRevocation, |
| | | 62 | | [Parameter( ParameterSetName = 'Items')] |
| | | 63 | | [System.Security.Cryptography.X509Certificates.X509Certificate2]$ServerCertificate, |
| | | 64 | | [Parameter( ParameterSetName = 'Items')] |
| | | 65 | | [System.Security.Cryptography.X509Certificates.X509Certificate2Collection]$ServerCertificateChain, |
| | | 66 | | [Parameter( ParameterSetName = 'Items')] |
| | | 67 | | [int]$HandshakeTimeout, |
| | | 68 | | [Parameter()] |
| | | 69 | | [switch]$PassThru |
| | | 70 | | ) |
| | | 71 | | begin { |
| | | 72 | | # Ensure the server instance is resolved |
| | 0 | 73 | | $Server = Resolve-KestrunServer -Server $Server |
| | | 74 | | } |
| | | 75 | | process { |
| | | 76 | | |
| | 0 | 77 | | if ($PSCmdlet.ParameterSetName -eq 'Items') { |
| | | 78 | | |
| | 0 | 79 | | $Options = [Microsoft.AspNetCore.Server.Kestrel.Https.HttpsConnectionAdapterOptions]::new() |
| | 0 | 80 | | if ($null -ne $SslProtocols) { |
| | 0 | 81 | | $options.SslProtocols = $SslProtocols |
| | | 82 | | } |
| | 0 | 83 | | if ($null -ne $ClientCertificateMode) { |
| | 0 | 84 | | $options.ClientCertificateMode = $ClientCertificateMode |
| | | 85 | | } |
| | 0 | 86 | | if ($CheckCertificateRevocation.IsPresent) { |
| | 0 | 87 | | $Options.CheckCertificateRevocation = $true |
| | | 88 | | } |
| | 0 | 89 | | if ($null -ne $ServerCertificate) { |
| | 0 | 90 | | $Options.ServerCertificate = $ServerCertificate |
| | | 91 | | } |
| | 0 | 92 | | if ($null -ne $ServerCertificateChain) { |
| | 0 | 93 | | $Options.ServerCertificateChain = $ServerCertificateChain |
| | | 94 | | } |
| | 0 | 95 | | if ($null -ne $HandshakeTimeout) { |
| | 0 | 96 | | $Options.HandshakeTimeout = [System.TimeSpan]::FromSeconds($HandshakeTimeout) |
| | | 97 | | } |
| | | 98 | | } |
| | | 99 | | |
| | 0 | 100 | | $Server.Options.HttpsConnectionAdapter = $Options |
| | | 101 | | |
| | 0 | 102 | | if ($PassThru.IsPresent) { |
| | | 103 | | # if the PassThru switch is specified, return the modified server instance |
| | 0 | 104 | | return $Server |
| | | 105 | | } |
| | | 106 | | } |
| | | 107 | | } |
| | | 108 | | |