Skip to content

Commit

Permalink
WebAdministrationDsc.Common: Remove unused functions (#629)
Browse files Browse the repository at this point in the history
- WebAdministrationDsc
  - Removed the common function `Find-Certificate` in favor of the command
    present in the module DscResource.Common.
  - Removed the function `Get-CurrentUser` since no code were using it.
  • Loading branch information
johlju authored Dec 5, 2023
1 parent 4c8f063 commit 1f5a356
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 687 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ For older change log history see the [historic changelog](HISTORIC_CHANGELOG.md)

## [Unreleased]

### Removed

- WebAdministrationDsc
- Removed the common function `Find-Certificate` in favor of the command
present in the module DscResource.Common.
- Removed the function `Get-CurrentUser` since no code were using it.

## [4.1.0] - 2023-01-03

### Fixed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@
'Start-ProcessWithTimeout'
'Test-DscPropertyState'
'New-CimCredentialInstance'
'Get-CurrentUser'
'Find-Certificate'
'New-TerminatingError'
'Get-WebConfigurationPropertyValue'
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,184 +199,6 @@ function New-CimCredentialInstance
return New-CimInstance @newCimInstanceParameters
}

<#
.SYNOPSIS
This is used to get the current user context when the resource
script runs.
.NOTES
We are putting this in a function so we can mock it with pester
#>
function Get-CurrentUser
{
[CmdletBinding()]
[OutputType([System.String])]
param ()

return [System.Security.Principal.WindowsIdentity]::GetCurrent()
}

<#
.SYNOPSIS
Locates one or more certificates using the passed certificate selector parameters.
If more than one certificate is found matching the selector criteria, they will be
returned in order of descending expiration date.
.PARAMETER Thumbprint
The thumbprint of the certificate to find.
.PARAMETER FriendlyName
The friendly name of the certificate to find.
.PARAMETER Subject
The subject of the certificate to find.
.PARAMETER DNSName
The subject alternative name of the certificate to export must contain these values.
.PARAMETER Issuer
The issuer of the certiicate to find.
.PARAMETER KeyUsage
The key usage of the certificate to find must contain these values.
.PARAMETER EnhancedKeyUsage
The enhanced key usage of the certificate to find must contain these values.
.PARAMETER Store
The Windows Certificate Store Name to search for the certificate in.
Defaults to 'My'.
.PARAMETER AllowExpired
Allows expired certificates to be returned.
#>
function Find-Certificate
{
[CmdletBinding()]
[OutputType([System.Security.Cryptography.X509Certificates.X509Certificate2[]])]
param
(
[Parameter()]
[String]
$Thumbprint,

[Parameter()]
[String]
$FriendlyName,

[Parameter()]
[String]
$Subject,

[Parameter()]
[String[]]
$DNSName,

[Parameter()]
[String]
$Issuer,

[Parameter()]
[String[]]
$KeyUsage,

[Parameter()]
[String[]]
$EnhancedKeyUsage,

[Parameter()]
[String]
$Store = 'My',

[Parameter()]
[Boolean]
$AllowExpired = $false
)

$certPath = Join-Path -Path 'Cert:\LocalMachine' -ChildPath $Store

if (-not (Test-Path -Path $certPath))
{
# The Certificate Path is not valid
New-InvalidArgumentException `
-Message ($script:localizedData.CertificatePathError -f $certPath) `
-ArgumentName 'Store'
} # if

# Assemble the filter to use to select the certificate
$certFilters = @()
if ($PSBoundParameters.ContainsKey('Thumbprint'))
{
$certFilters += @('($_.Thumbprint -eq $Thumbprint)')
} # if

if ($PSBoundParameters.ContainsKey('FriendlyName'))
{
$certFilters += @('($_.FriendlyName -eq $FriendlyName)')
} # if

if ($PSBoundParameters.ContainsKey('Subject'))
{
$certFilters += @('(@(Compare-Object `
-ReferenceObject (($_.Subject -split ", ").trim()|sort-object) `
-DifferenceObject (($subject -split ",").trim()|sort-object)| `
Where-Object -Property SideIndicator -eq "=>").Count -eq 0)')
} # if

if ($PSBoundParameters.ContainsKey('Issuer'))
{
$certFilters += @('($_.Issuer -eq $Issuer)')
} # if

if (-not $AllowExpired)
{
$certFilters += @('(((Get-Date) -le $_.NotAfter) -and ((Get-Date) -ge $_.NotBefore))')
} # if

if ($PSBoundParameters.ContainsKey('DNSName'))
{
$certFilters += @('(@(Compare-Object `
-ReferenceObject $_.DNSNameList.Unicode `
-DifferenceObject $DNSName | `
Where-Object -Property SideIndicator -eq "=>").Count -eq 0)')
} # if

if ($PSBoundParameters.ContainsKey('KeyUsage'))
{
$certFilters += @('(@(Compare-Object `
-ReferenceObject ($_.Extensions.KeyUsages -split ", ") `
-DifferenceObject $KeyUsage | `
Where-Object -Property SideIndicator -eq "=>").Count -eq 0)')
} # if

if ($PSBoundParameters.ContainsKey('EnhancedKeyUsage'))
{
$certFilters += @('(@(Compare-Object `
-ReferenceObject ($_.EnhancedKeyUsageList.FriendlyName) `
-DifferenceObject $EnhancedKeyUsage | `
Where-Object -Property SideIndicator -eq "=>").Count -eq 0)')
} # if

# Join all the filters together
$certFilterScript = '(' + ($certFilters -join ' -and ') + ')'

Write-Verbose -Message ($script:localizedData.SearchingForCertificateUsingFilters `
-f $store, $certFilterScript)

$certs = Get-ChildItem -Path $certPath |
Where-Object -FilterScript ([ScriptBlock]::Create($certFilterScript))

# Sort the certificates
if ($certs.count -gt 1)
{
$certs = $certs | Sort-Object -Descending -Property 'NotAfter'
} # if

return $certs
} # end function Find-Certificate

<#
.SYNOPSIS
Internal function to throw terminating error with specified
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,4 @@ ConvertFrom-StringData @'
PropertyValueOfTypeDoesNotMatch = {0} value does not match. Current value is '{1}', but expected the value '{2}'. (WACOMMON0006)
UnableToCompareType = Unable to compare the type {0} as it is not handled by the Test-DscPropertyState cmdlet. (WACOMMON0007)
StartProcess = Started the process with id {0} using the path '{1}', and with a timeout value of {2} seconds. (WACOMMON0009)
CertificatePathError = Certificate Path '{0}' is not valid. (WACOMMON0010)
SearchingForCertificateUsingFilters = Looking for certificate in Store '{0}' using filter '{1}'. (WACOMMON0011)
'@
Loading

0 comments on commit 1f5a356

Please sign in to comment.