diff --git a/CHANGELOG.md b/CHANGELOG.md index bad923b84..5a14a91b2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - SPFarm - Updated to run cmdlet `Update-SPFlightsConfigFile` on SharePoint Subscription. +### Fixed + +- SPCertificateSettings + - Fixed an error where the command failed to add + SPCertificateNotificationContacts when there are currently none set. + ## [5.4.0] - 2023-04-04 ### Fixed diff --git a/SharePointDsc/DSCResources/MSFT_SPCertificateSettings/MSFT_SPCertificateSettings.psm1 b/SharePointDsc/DSCResources/MSFT_SPCertificateSettings/MSFT_SPCertificateSettings.psm1 index 5748ab2ea..f24766620 100644 --- a/SharePointDsc/DSCResources/MSFT_SPCertificateSettings/MSFT_SPCertificateSettings.psm1 +++ b/SharePointDsc/DSCResources/MSFT_SPCertificateSettings/MSFT_SPCertificateSettings.psm1 @@ -285,22 +285,35 @@ function Set-TargetResource if ($contactsProvided) { Write-Verbose "Checking Certificate Notification Contacts" - $currentContacts = [array](Get-SPCertificateNotificationContact).Address + [array]$currentContacts = Get-SPCertificateNotificationContact - $diffs = Compare-Object -ReferenceObject $desiredContacts -DifferenceObject $currentContacts - foreach ($diff in $diffs) + # If there aren't any current Contacts we'll add them all. Also Compare-Object does not like $null Objects. + # This fixes Issue "SPCertificateSettings: Unable to set contacts when previously blank" https://github.com/dsccommunity/SharePointDsc/issues/1430 + if ($currentContacts.Count -eq 0) { - switch ($diff.SideIndicator) + foreach ($contact in $desiredContacts) { - "<=" - { - Write-Verbose "Adding $($diff.InputObject)" - $null = Add-SPCertificateNotificationContact -EmailAddress $diff.InputObject - } - "=>" + Write-Verbose "Adding $($diff.InputObject)" + $null = Add-SPCertificateNotificationContact -EmailAddress $contact + } + } + else + { + $diffs = Compare-Object -ReferenceObject $desiredContacts -DifferenceObject ($currentContacts | Select-Object -ExpandProperty Address) + foreach ($diff in $diffs) + { + switch ($diff.SideIndicator) { - Write-Verbose "Removing $($diff.InputObject)" - $null = Remove-SPCertificateNotificationContact -EmailAddress $diff.InputObject -Confirm:$false + "<=" + { + Write-Verbose "Adding $($diff.InputObject)" + $null = Add-SPCertificateNotificationContact -EmailAddress $diff.InputObject + } + "=>" + { + Write-Verbose "Removing $($diff.InputObject)" + $null = Remove-SPCertificateNotificationContact -EmailAddress $diff.InputObject -Confirm:$false + } } } } diff --git a/tests/Unit/SharePointDsc/SharePointDsc.SPCertificateSettings.Tests.ps1 b/tests/Unit/SharePointDsc/SharePointDsc.SPCertificateSettings.Tests.ps1 index d633bc560..e1fc07088 100644 --- a/tests/Unit/SharePointDsc/SharePointDsc.SPCertificateSettings.Tests.ps1 +++ b/tests/Unit/SharePointDsc/SharePointDsc.SPCertificateSettings.Tests.ps1 @@ -33,7 +33,7 @@ function Invoke-TestSetup $script:testEnvironment = Initialize-TestEnvironment ` -DSCModuleName $script:DSCModuleName ` - -DSCResourceName $script:DSCResourceFullName ` + -DscResourceName $script:DSCResourceFullName ` -ResourceType 'Mof' ` -TestType 'Unit' } @@ -260,7 +260,7 @@ try CertificateExpirationWarningThresholdDays = 15 CertificateExpirationErrorThresholdDays = 15 CertificateNotificationContacts = @( - @{ + [PSCustomObject]@{ Address = 'wrong@contoso.com' } ) @@ -270,7 +270,7 @@ try Mock -CommandName Get-SPFarm -MockWith { return @{ } } Mock -CommandName Get-SPCertificateNotificationContact -MockWith { return @( - @{ + [PSCustomObject]@{ Address = 'wrong@contoso.com' } ) @@ -295,6 +295,55 @@ try } } + Context -Name "The server is in a farm and zero contacts have been applied" -Fixture { + BeforeAll { + $testParams = @{ + IsSingleInstance = 'Yes' + CertificateNotificationContacts = 'admin@contoso.com' + } + + Mock -CommandName Get-SPCertificateSettings -MockWith { + $returnVal = @{ + DefaultOrganizationalUnit = '' + DefaultOrganization = '' + DefaultLocality = '' + DefaultState = '' + DefaultCountry = '' + DefaultKeyAlgorithm = 'RSA' + DefaultRsaKeySize = 2048 + DefaultEllipticCurve = 'nistP256' + DefaultHashAlgorithm = 'SHA256' + DefaultRsaSignaturePadding = 'Pkcs1' + CertificateExpirationAttentionThresholdDays = 60 + CertificateExpirationWarningThresholdDays = 15 + CertificateExpirationErrorThresholdDays = 15 + CertificateNotificationContacts = [System.Net.Mail.MailAddressCollection]::new() + } + return $returnVal + } + Mock -CommandName Get-SPFarm -MockWith { return @{ } } + Mock -CommandName Get-SPCertificateNotificationContact -MockWith { + return [System.Net.Mail.MailAddressCollection]::new() + } + Mock -CommandName Add-SPCertificateNotificationContact -MockWith {} + Mock -CommandName Remove-SPCertificateNotificationContact -MockWith {} + } + + It "Should return values from the get method" { + $result = Get-TargetResource @testParams + $result.CertificateNotificationContacts.Count | Should -Be 0 + } + + It "Should return false from the test method" { + Test-TargetResource @testParams | Should -Be $false + } + + It "Should update the certificate settings" { + Set-TargetResource @testParams + Assert-MockCalled Add-SPCertificateNotificationContact + } + } + Context -Name "The server is in a farm and the correct settings have been applied" -Fixture { BeforeAll { $testParams = @{