Skip to content

Commit

Permalink
Merge pull request #1437 from ChristophHannappel/fix/SPCertificateSet…
Browse files Browse the repository at this point in the history
…tings

Fixes Compare-Object Exception if there are currently no CertificateN…
  • Loading branch information
ykuijs authored Apr 20, 2024
2 parents c8281f1 + b310e7d commit 1a0587e
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 15 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ function Invoke-TestSetup

$script:testEnvironment = Initialize-TestEnvironment `
-DSCModuleName $script:DSCModuleName `
-DSCResourceName $script:DSCResourceFullName `
-DscResourceName $script:DSCResourceFullName `
-ResourceType 'Mof' `
-TestType 'Unit'
}
Expand Down Expand Up @@ -260,7 +260,7 @@ try
CertificateExpirationWarningThresholdDays = 15
CertificateExpirationErrorThresholdDays = 15
CertificateNotificationContacts = @(
@{
[PSCustomObject]@{
Address = '[email protected]'
}
)
Expand All @@ -270,7 +270,7 @@ try
Mock -CommandName Get-SPFarm -MockWith { return @{ } }
Mock -CommandName Get-SPCertificateNotificationContact -MockWith {
return @(
@{
[PSCustomObject]@{
Address = '[email protected]'
}
)
Expand All @@ -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 = '[email protected]'
}

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 = @{
Expand Down

0 comments on commit 1a0587e

Please sign in to comment.