Skip to content

Commit

Permalink
Merge pull request #186 from AlexandreRatte/GetTypesFailOnDefinedType…
Browse files Browse the repository at this point in the history
…sNull

CertificateDsc.Common.psm1: Exclude assemblies having DefinedTypes as null instead of an empty array.
  • Loading branch information
PlagueHO committed Mar 15, 2019
2 parents 2cd3f75 + c242bdd commit 967ca58
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 19 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Expand Up @@ -2,6 +2,11 @@

## Unreleased

- CertificateDsc.Common.psm1
- Exclude assemblies that set DefinedTypes to null instead of an empty array
to prevent failures on GetTypes(). This issue occurred with the
Microsoft.WindowsAzure.Storage.dll assembly.

## 4.4.0.0

- Minor style corrections from PR for
Expand Down
9 changes: 6 additions & 3 deletions Modules/CertificateDsc.Common/CertificateDsc.Common.psm1
Expand Up @@ -117,8 +117,11 @@ function Test-Thumbprint
# Get FIPS registry key
$fips = [System.Int32] (Get-ItemProperty -Path 'HKLM:\SYSTEM\CurrentControlSet\Control\Lsa\FipsAlgorithmPolicy' -ErrorAction SilentlyContinue).Enabled

# Get a list of Hash Providers
$allHashProviders = [System.AppDomain]::CurrentDomain.GetAssemblies().GetTypes()
<#
Get a list of Hash Providers, but exclude assemblies that set DefinedTypes to null instead of an empty array.
Otherwise, the call to GetTypes() fails.
#>
$allHashProviders = ([System.AppDomain]::CurrentDomain.GetAssemblies() | Where-Object { $null -ne $_.DefinedTypes}).GetTypes()

if ($fips -eq $true)
{
Expand Down Expand Up @@ -740,7 +743,7 @@ function Get-CertificateTemplateName

if ($null -ne $templateExtensionText)
{
return Get-CertificateTemplateInformation -FormattedTemplate $templateExtensionText |
return Get-CertificateTemplateInformation -FormattedTemplate $templateExtensionText |
Select-Object -ExpandProperty Name
}
}
Expand Down
15 changes: 8 additions & 7 deletions Tests/Unit/CertificateDsc.Common.Tests.ps1
Expand Up @@ -24,26 +24,27 @@ try
InModuleScope $script:ModuleName {
$DSCResourceName = 'CertificateDsc.Common'
$invalidThumbprint = 'Zebra'
$definedRuntimeTypes = ([System.AppDomain]::CurrentDomain.GetAssemblies() | Where-Object -FilterScript { $null -ne $_.DefinedTypes}).GetTypes()

# This thumbprint is valid (but not FIPS valid)
$validThumbprint = (
[System.AppDomain]::CurrentDomain.GetAssemblies().GetTypes() | Where-Object {
$definedRuntimeTypes | Where-Object -FilterScript {
$_.BaseType.BaseType -eq [System.Security.Cryptography.HashAlgorithm] -and
($_.Name -cmatch 'Managed$' -or $_.Name -cmatch 'Provider$')
} | Select-Object -First 1 | ForEach-Object {
(New-Object $_).ComputeHash([String]::Empty) | ForEach-Object {
} | Select-Object -First 1 | ForEach-Object -Process {
(New-Object $_).ComputeHash([String]::Empty) | ForEach-Object -Process {
'{0:x2}' -f $_
}
}
) -join ''

# This thumbprint is valid for FIPS
$validFipsThumbprint = (
[System.AppDomain]::CurrentDomain.GetAssemblies().GetTypes() | Where-Object {
$definedRuntimeTypes | Where-Object -FilterScript {
$_.BaseType.BaseType -eq [System.Security.Cryptography.HashAlgorithm] -and
($_.Name -cmatch 'Provider$' -and $_.Name -cnotmatch 'MD5')
} | Select-Object -First 1 | ForEach-Object {
(New-Object $_).ComputeHash([String]::Empty) | ForEach-Object {
} | Select-Object -First 1 | ForEach-Object -Process {
(New-Object $_).ComputeHash([String]::Empty) | ForEach-Object -Process {
'{0:x2}' -f $_
}
}
Expand Down Expand Up @@ -1287,7 +1288,7 @@ Major Version Number=100
Minor Version Number=5
'@

$params = @{
TemplateExtensions = $testCertificateWithAltTemplateInformation.Extensions
}
Expand Down
7 changes: 4 additions & 3 deletions Tests/Unit/MSFT_CertReq.Tests.ps1
Expand Up @@ -26,12 +26,13 @@ $TestEnvironment = Initialize-TestEnvironment `
try
{
InModuleScope $script:DSCResourceName {
$definedRuntimeTypes = ([System.AppDomain]::CurrentDomain.GetAssemblies() | Where-Object -FilterScript { $null -ne $_.DefinedTypes}).GetTypes()
$validThumbprint = (
[System.AppDomain]::CurrentDomain.GetAssemblies().GetTypes() | Where-Object {
$definedRuntimeTypes | Where-Object -FilterScript {
$_.BaseType.BaseType -eq [System.Security.Cryptography.HashAlgorithm] -and
($_.Name -cmatch 'Managed$' -or $_.Name -cmatch 'Provider$')
} | Select-Object -First 1 | ForEach-Object {
(New-Object $_).ComputeHash([String]::Empty) | ForEach-Object {
} | Select-Object -First 1 | ForEach-Object -Process {
(New-Object $_).ComputeHash([String]::Empty) | ForEach-Object -Process {
'{0:x2}' -f $_
}
}
Expand Down
7 changes: 4 additions & 3 deletions Tests/Unit/MSFT_CertificateImport.Tests.ps1
Expand Up @@ -22,12 +22,13 @@ try
{
InModuleScope $script:DSCResourceName {
$DSCResourceName = 'MSFT_CertificateImport'
$definedRuntimeTypes = ([System.AppDomain]::CurrentDomain.GetAssemblies() | Where-Object -FilterScript { $null -ne $_.DefinedTypes}).GetTypes()
$validThumbprint = (
[System.AppDomain]::CurrentDomain.GetAssemblies().GetTypes() | Where-Object {
$definedRuntimeTypes | Where-Object -FilterScript {
$_.BaseType.BaseType -eq [System.Security.Cryptography.HashAlgorithm] -and
($_.Name -cmatch 'Managed$' -or $_.Name -cmatch 'Provider$')
} | Select-Object -First 1 | ForEach-Object {
(New-Object $_).ComputeHash([String]::Empty) | ForEach-Object {
} | Select-Object -First 1 | ForEach-Object -Process {
(New-Object $_).ComputeHash([String]::Empty) | ForEach-Object -Process {
'{0:x2}' -f $_
}
}
Expand Down
7 changes: 4 additions & 3 deletions Tests/Unit/MSFT_PfxImport.Tests.ps1
Expand Up @@ -28,12 +28,13 @@ try
{
InModuleScope $script:DSCResourceName {
$DSCResourceName = 'MSFT_PfxImport'
$definedRuntimeTypes = ([System.AppDomain]::CurrentDomain.GetAssemblies() | Where-Object -FilterScript { $null -ne $_.DefinedTypes}).GetTypes()
$validThumbprint = (
[System.AppDomain]::CurrentDomain.GetAssemblies().GetTypes() | Where-Object {
$definedRuntimeTypes | Where-Object -FilterScript {
$_.BaseType.BaseType -eq [System.Security.Cryptography.HashAlgorithm] -and
($_.Name -cmatch 'Managed$' -or $_.Name -cmatch 'Provider$')
} | Select-Object -First 1 | ForEach-Object {
(New-Object $_).ComputeHash([String]::Empty) | ForEach-Object {
} | Select-Object -First 1 | ForEach-Object -Process {
(New-Object $_).ComputeHash([String]::Empty) | ForEach-Object -Process {
'{0:x2}' -f $_
}
}
Expand Down

0 comments on commit 967ca58

Please sign in to comment.