Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Opt-in to Localization String tests and Merge Common Modules - Fixes #195 #200

Merged
merged 5 commits into from
May 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .MetaTestOptIn.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@
"Common Tests - Custom Script Analyzer Rules",
"Common Tests - Validate Example Files To Be Published",
"Common Tests - Validate Markdown Links",
"Common Tests - Relative Path Length"
"Common Tests - Relative Path Length",
"Common Tests - Validate Localization"
]
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

## Unreleased

- Opted into Common Tests 'Common Tests - Validate Localization' -
fixes [Issue #195](https://github.com/PowerShell/CertificateDsc/issues/195).
- Combined all `CertificateDsc.ResourceHelper` module functions into
`CertificateDsc.Common` module and renamed to `CertificateDsc.CommonHelper`
module.

## 4.6.0.0

- CertReq:
Expand Down
67 changes: 30 additions & 37 deletions DSCResources/MSFT_CertReq/MSFT_CertReq.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,18 @@

$modulePath = Join-Path -Path (Split-Path -Path (Split-Path -Path $PSScriptRoot -Parent) -Parent) -ChildPath 'Modules'

# Import the Certificate Common Modules
Import-Module -Name (Join-Path -Path $modulePath `
-ChildPath (Join-Path -Path 'CertificateDsc.Common' `
-ChildPath 'CertificateDsc.Common.psm1'))

# Import the Certificate Resource Helper Module
Import-Module -Name (Join-Path -Path $modulePath `
-ChildPath (Join-Path -Path 'CertificateDsc.ResourceHelper' `
-ChildPath 'CertificateDsc.ResourceHelper.psm1'))

# Import the Certificate PDT Helper Module
Import-Module -Name (Join-Path -Path $modulePath `
-ChildPath (Join-Path -Path 'CertificateDsc.PDT' `
-ChildPath 'CertificateDsc.PDT.psm1'))

# Import Localization Strings
$localizedData = Get-LocalizedData `
-ResourceName 'MSFT_CertReq' `
-ResourcePath (Split-Path -Parent $Script:MyInvocation.MyCommand.Path)
# Import the Certificate Resource Common Module.
Import-Module -Name (Join-Path -Path $modulePath `
-ChildPath (Join-Path -Path 'CertificateDsc.Common' `
-ChildPath 'CertificateDsc.Common.psm1'))

# Import Localization Strings.
$script:localizedData = Get-LocalizedData -ResourceName 'MSFT_CertReq'

<#
.SYNOPSIS
Expand Down Expand Up @@ -193,7 +186,7 @@ function Get-TargetResource

Write-Verbose -Message ( @(
"$($MyInvocation.MyCommand): "
$($LocalizedData.GettingCertReqStatusMessage -f $Subject, $CA)
$($script:localizedData.GettingCertReqStatusMessage -f $Subject, $CA)
) -join '' )

$cert = Get-Childitem -Path Cert:\LocalMachine\My |
Expand All @@ -211,7 +204,7 @@ function Get-TargetResource
{
Write-Verbose -Message ( @(
"$($MyInvocation.MyCommand): "
$($LocalizedData.CertificateExistsMessage -f $Subject, $ca, $cert.Thumbprint)
$($script:localizedData.CertificateExistsMessage -f $Subject, $ca, $cert.Thumbprint)
) -join '' )

$returnValue = @{
Expand Down Expand Up @@ -406,7 +399,7 @@ function Set-TargetResource

Write-Verbose -Message ( @(
"$($MyInvocation.MyCommand): "
$($LocalizedData.StartingCertReqMessage -f $Subject, $ca)
$($script:localizedData.StartingCertReqMessage -f $Subject, $ca)
) -join '' )

# If the Subject does not contain a full X500 path, construct just the CN
Expand Down Expand Up @@ -525,7 +518,7 @@ CertificateTemplate = "$CertificateTemplate"
# NEW: Create a new request as directed by PolicyFileIn
Write-Verbose -Message ( @(
"$($MyInvocation.MyCommand): "
$($LocalizedData.CreateRequestCertificateMessage -f $infPath, $reqPath)
$($script:localizedData.CreateRequestCertificateMessage -f $infPath, $reqPath)
) -join '' )

<#
Expand All @@ -552,7 +545,7 @@ CertificateTemplate = "$CertificateTemplate"

Write-Verbose -Message ( @(
"$($MyInvocation.MyCommand): "
$($LocalizedData.CreateRequestResultCertificateMessage -f ($createRequest | Out-String))
$($script:localizedData.CreateRequestResultCertificateMessage -f ($createRequest | Out-String))
) -join '' )

<#
Expand All @@ -565,7 +558,7 @@ CertificateTemplate = "$CertificateTemplate"
{
Write-Verbose -Message ( @(
"$($MyInvocation.MyCommand): "
$($LocalizedData.SubmittingRequestCertificateMessage -f $reqPath, $cerPath, $ca)
$($script:localizedData.SubmittingRequestCertificateMessage -f $reqPath, $cerPath, $ca)
) -join '' )

if ($Credential)
Expand Down Expand Up @@ -621,7 +614,7 @@ CertificateTemplate = "$CertificateTemplate"

Write-Verbose -Message ( @(
"$($MyInvocation.MyCommand): "
$($LocalizedData.SubmittingRequestProcessCertificateMessage)
$($script:localizedData.SubmittingRequestProcessCertificateMessage)
) -join '' )

$null = Wait-Win32ProcessStop `
Expand All @@ -637,7 +630,7 @@ CertificateTemplate = "$CertificateTemplate"
else
{
New-InvalidOperationException `
-Message ($LocalizedData.CertReqOutNotFoundError -f $certReqOutPath)
-Message ($script:localizedData.CertReqOutNotFoundError -f $certReqOutPath)
} # if
} # if
}
Expand All @@ -648,39 +641,39 @@ CertificateTemplate = "$CertificateTemplate"

Write-Verbose -Message ( @(
"$($MyInvocation.MyCommand): "
$($LocalizedData.SubmittingRequestResultCertificateMessage -f ($submitRequest | Out-String))
$($script:localizedData.SubmittingRequestResultCertificateMessage -f ($submitRequest | Out-String))
) -join '' )
}
else
{
New-InvalidOperationException `
-Message ($LocalizedData.CertificateReqNotFoundError -f $reqPath)
-Message ($script:localizedData.CertificateReqNotFoundError -f $reqPath)
} # if

# ACCEPT: Accept the request
if (Test-Path -Path $cerPath)
{
Write-Verbose -Message ( @(
"$($MyInvocation.MyCommand): "
$($LocalizedData.AcceptingRequestCertificateMessage -f $cerPath, $ca)
$($script:localizedData.AcceptingRequestCertificateMessage -f $cerPath, $ca)
) -join '' )

$acceptRequest = & certreq.exe @('-accept', '-machine', '-q', $cerPath)

Write-Verbose -Message ( @(
"$($MyInvocation.MyCommand): "
$($LocalizedData.AcceptingRequestResultCertificateMessage -f ($acceptRequest | Out-String))
$($script:localizedData.AcceptingRequestResultCertificateMessage -f ($acceptRequest | Out-String))
) -join '' )
}
else
{
New-InvalidOperationException `
-Message ($LocalizedData.CertificateCerNotFoundError -f $cerPath)
-Message ($script:localizedData.CertificateCerNotFoundError -f $cerPath)
} # if

Write-Verbose -Message ( @(
"$($MyInvocation.MyCommand): "
$($LocalizedData.CleaningUpRequestFilesMessage -f "$($workingPath).*")
$($script:localizedData.CleaningUpRequestFilesMessage -f "$($workingPath).*")
) -join '' )
Remove-Item -Path "$($workingPath).*" -Force
} # end function Set-TargetResource
Expand Down Expand Up @@ -862,7 +855,7 @@ function Test-TargetResource

Write-Verbose -Message ( @(
"$($MyInvocation.MyCommand): "
$($LocalizedData.TestingCertReqStatusMessage -f $Subject, $ca)
$($script:localizedData.TestingCertReqStatusMessage -f $Subject, $ca)
) -join '' )

$certificate = Get-Childitem -Path Cert:\LocalMachine\My |
Expand Down Expand Up @@ -890,7 +883,7 @@ function Test-TargetResource
{
Write-Verbose -Message ( @(
"$($MyInvocation.MyCommand): "
$($LocalizedData.CertificateExistsMessage -f $Subject, $ca, $certificate.Thumbprint)
$($script:localizedData.CertificateExistsMessage -f $Subject, $ca, $certificate.Thumbprint)
) -join '' )

if ($AutoRenew)
Expand All @@ -900,7 +893,7 @@ function Test-TargetResource
# The certificate was found but it is expiring within 30 days or has expired
Write-Verbose -Message ( @(
"$($MyInvocation.MyCommand): "
$($LocalizedData.ExpiringCertificateMessage -f $Subject, $ca, $certificate.Thumbprint)
$($script:localizedData.ExpiringCertificateMessage -f $Subject, $ca, $certificate.Thumbprint)
) -join '' )
return $false
} # if
Expand All @@ -912,7 +905,7 @@ function Test-TargetResource
# The certificate has expired
Write-Verbose -Message ( @(
"$($MyInvocation.MyCommand): "
$($LocalizedData.ExpiredCertificateMessage -f $Subject, $ca, $certificate.Thumbprint)
$($script:localizedData.ExpiredCertificateMessage -f $Subject, $ca, $certificate.Thumbprint)
) -join '' )
return $false
} # if
Expand Down Expand Up @@ -967,7 +960,7 @@ function Test-TargetResource
{
Write-Verbose -Message ( @(
"$($MyInvocation.MyCommand): "
$($LocalizedData.CertTemplateMismatch -f $Subject, $ca, $certificate.Thumbprint, $currentCertificateTemplateName)
$($script:localizedData.CertTemplateMismatch -f $Subject, $ca, $certificate.Thumbprint, $currentCertificateTemplateName)
) -join '' )
return $false
} # if
Expand All @@ -977,23 +970,23 @@ function Test-TargetResource
{
Write-Verbose -Message ( @(
"$($MyInvocation.MyCommand): "
$($LocalizedData.CertFriendlyNameMismatch -f $Subject, $ca, $certificate.Thumbprint, $certificate.FriendlyName)
$($script:localizedData.CertFriendlyNameMismatch -f $Subject, $ca, $certificate.Thumbprint, $certificate.FriendlyName)
) -join '' )
return $false
} # if

# The certificate was found and is OK - so no change required.
Write-Verbose -Message ( @(
"$($MyInvocation.MyCommand): "
$($LocalizedData.ValidCertificateExistsMessage -f $Subject, $ca, $certificate.Thumbprint)
$($script:localizedData.ValidCertificateExistsMessage -f $Subject, $ca, $certificate.Thumbprint)
) -join '' )
return $true
} # if

# A valid certificate was not found
Write-Verbose -Message ( @(
"$($MyInvocation.MyCommand): "
$($LocalizedData.NoValidCertificateMessage -f $Subject, $ca)
$($script:localizedData.NoValidCertificateMessage -f $Subject, $ca)
) -join '' )
return $false
} # end function Test-TargetResource
Expand Down Expand Up @@ -1027,7 +1020,7 @@ function Assert-ResourceProperty
if ((($KeyType -eq 'RSA') -and ($KeyLength -notin '1024', '2048', '4096', '8192')) -or `
(($KeyType -eq 'ECDH') -and ($KeyLength -notin '192', '224', '256', '384', '521')))
{
New-InvalidArgumentException -Message (($LocalizedData.InvalidKeySize) -f $KeyLength,$KeyType) -ArgumentName 'KeyLength'
New-InvalidArgumentException -Message (($script:localizedData.InvalidKeySize) -f $KeyLength,$KeyType) -ArgumentName 'KeyLength'
}
}# end function Assert-ResourceProperty

Expand Down
1 change: 0 additions & 1 deletion DSCResources/MSFT_CertReq/en-US/MSFT_CertReq.strings.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ ConvertFrom-StringData @'
ExpiringCertificateMessage = The certificate found with subject '{0}' issued by {1} with thumbprint '{2}' is about to expire.
NoValidCertificateMessage = No valid certificate found with subject '{0}' issued by {1}.
ExpiredCertificateMessage = The certificate found with subject '{0}' issued by {1} with thumbprint '{2}' has expired.
InvalidCertificateMessage = The certificate found with subject '{0}' issued by {1} with thumbprint '{2}' is inavlid.
ValidCertificateExistsMessage = Valid certificate '{2}' found with subject '{0}' issued by {1}.
CertificateReqNotFoundError = Certificate Request file '{0}' not found.
CertificateCerNotFoundError = Certificate file '{0}' not found.
Expand Down
37 changes: 15 additions & 22 deletions DSCResources/MSFT_CertificateExport/MSFT_CertificateExport.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,13 @@

$modulePath = Join-Path -Path (Split-Path -Path (Split-Path -Path $PSScriptRoot -Parent) -Parent) -ChildPath 'Modules'

# Import the Certificate Common Modules
# Import the Certificate Resource Common Module.
Import-Module -Name (Join-Path -Path $modulePath `
-ChildPath (Join-Path -Path 'CertificateDsc.Common' `
-ChildPath 'CertificateDsc.Common.psm1'))

# Import the Certificate Resource Helper Module
Import-Module -Name (Join-Path -Path $modulePath `
-ChildPath (Join-Path -Path 'CertificateDsc.ResourceHelper' `
-ChildPath 'CertificateDsc.ResourceHelper.psm1'))

# Import Localization Strings
$localizedData = Get-LocalizedData `
-ResourceName 'MSFT_CertificateExport' `
-ResourcePath (Split-Path -Parent $Script:MyInvocation.MyCommand.Path)
# Import Localization Strings.
$script:localizedData = Get-LocalizedData -ResourceName 'MSFT_CertificateExport'

<#
.SYNOPSIS
Expand All @@ -38,7 +31,7 @@ function Get-TargetResource
Write-Verbose -Message (
@(
"$($MyInvocation.MyCommand): ",
$($LocalizedData.GettingCertificateExportMessage -f $Path)
$($script:localizedData.GettingCertificateExportMessage -f $Path)
) -join '' )

$result = @{
Expand Down Expand Up @@ -184,7 +177,7 @@ function Set-TargetResource
Write-Verbose -Message (
@(
"$($MyInvocation.MyCommand): ",
$($LocalizedData.SettingCertificateExportMessage -f $Path)
$($script:localizedData.SettingCertificateExportMessage -f $Path)
) -join '' )

$findCertificateParameters = @{} + $PSBoundParameters
Expand All @@ -202,7 +195,7 @@ function Set-TargetResource
Write-Verbose -Message (
@(
"$($MyInvocation.MyCommand): ",
$($LocalizedData.CertificateToExportNotFound -f $Path, $Type, $Store)
$($script:localizedData.CertificateToExportNotFound -f $Path, $Type, $Store)
) -join '' )
}
else
Expand All @@ -213,7 +206,7 @@ function Set-TargetResource
Write-Verbose -Message (
@(
"$($MyInvocation.MyCommand): ",
$($LocalizedData.CertificateToExportFound -f $certificateThumbprintToExport, $Path)
$($script:localizedData.CertificateToExportFound -f $certificateThumbprintToExport, $Path)
) -join '' )

# Export the certificate
Expand Down Expand Up @@ -249,7 +242,7 @@ function Set-TargetResource
Write-Verbose -Message (
@(
"$($MyInvocation.MyCommand): ",
$($LocalizedData.CertificateExported -f $certificateThumbprintToExport, $Path, $Type)
$($script:localizedData.CertificateExported -f $certificateThumbprintToExport, $Path, $Type)
) -join '' )
} # if
} # end function Set-TargetResource
Expand Down Expand Up @@ -390,7 +383,7 @@ function Test-TargetResource
Write-Verbose -Message (
@(
"$($MyInvocation.MyCommand): ",
$($LocalizedData.TestingCertificateExportMessage -f $Path)
$($script:localizedData.TestingCertificateExportMessage -f $Path)
) -join '' )

$findCertificateParameters = @{} + $PSBoundParameters
Expand All @@ -408,7 +401,7 @@ function Test-TargetResource
Write-Verbose -Message (
@(
"$($MyInvocation.MyCommand): ",
$($LocalizedData.CertificateToExportNotFound -f $Path, $Type, $Store)
$($script:localizedData.CertificateToExportNotFound -f $Path, $Type, $Store)
) -join '' )

return $true
Expand All @@ -421,7 +414,7 @@ function Test-TargetResource
Write-Verbose -Message (
@(
"$($MyInvocation.MyCommand): ",
$($LocalizedData.CertificateToExportFound -f $certificateThumbprintToExport, $Path)
$($script:localizedData.CertificateToExportFound -f $certificateThumbprintToExport, $Path)
) -join '' )

if (Test-Path -Path $Path)
Expand All @@ -432,7 +425,7 @@ function Test-TargetResource
Write-Verbose -Message (
@(
"$($MyInvocation.MyCommand): ",
$($LocalizedData.CertificateAlreadyExportedMatchSource -f $certificateThumbprintToExport, $Path)
$($script:localizedData.CertificateAlreadyExportedMatchSource -f $certificateThumbprintToExport, $Path)
) -join '' )

# Need to now compare the existing exported cert content with the found cert
Expand All @@ -451,7 +444,7 @@ function Test-TargetResource
Write-Verbose -Message (
@(
"$($MyInvocation.MyCommand): ",
$($LocalizedData.CertificateAlreadyExportedNotMatchSource -f $certificateThumbprintToExport, $Path)
$($script:localizedData.CertificateAlreadyExportedNotMatchSource -f $certificateThumbprintToExport, $Path)
) -join '' )

return $false
Expand All @@ -464,7 +457,7 @@ function Test-TargetResource
Write-Verbose -Message (
@(
"$($MyInvocation.MyCommand): ",
$($LocalizedData.CertificateAlreadyExported -f $certificateThumbprintToExport, $Path)
$($script:localizedData.CertificateAlreadyExported -f $certificateThumbprintToExport, $Path)
) -join '' )
} # if

Expand All @@ -476,7 +469,7 @@ function Test-TargetResource
Write-Verbose -Message (
@(
"$($MyInvocation.MyCommand): ",
$($LocalizedData.CertificateNotExported -f $certificateThumbprintToExport, $Path)
$($script:localizedData.CertificateNotExported -f $certificateThumbprintToExport, $Path)
) -join '' )

return $false
Expand Down
Loading