Skip to content

Commit

Permalink
xWebSite: Revert back to Set-ItemProperty for LogFlags (dsccommunity#560
Browse files Browse the repository at this point in the history
)

- xWebsite
  - Fix an issue where changes to LogFlags would fail to apply.  Set LogFlags using 
    Set-ItemProperty to fix error when updating flags.
  • Loading branch information
harveyrendell authored and Gregory Storme committed Feb 14, 2020
1 parent bbc6aea commit 0d012cb
Show file tree
Hide file tree
Showing 9 changed files with 174 additions and 28 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ For older change log history see the [historic changelog](HISTORIC_CHANGELOG.md)
- xWebsite
- Fixed HTTPS binding issue causing failure when CertificateSubject matches
multiple certificates.
- Fix an issue where changes to LogFlags would fail to apply.


## [3.1.0] - 2019-12-30

Expand Down
9 changes: 4 additions & 5 deletions source/DSCResources/MSFT_xWebSite/MSFT_xWebSite.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -585,11 +585,10 @@ function Set-TargetResource
Write-Verbose -Message ($script:localizedData.VerboseSetTargetUpdateLogFlags `
-f $Name)

# Set-ItemProperty has no effect with the LogFile.LogExtFileFlags property
$site = Get-Item "IIS:\Sites\$Name"
$site.LogFile.LogFormat = 'W3C'
$site.LogFile.LogExtFileFlags = $LogFlags -join ','
$site | Set-Item
Set-ItemProperty "IIS:\Sites\$Name" `
-Name logFile.logFormat -value 'W3C'
Set-ItemProperty "IIS:\Sites\$Name" `
-Name logFile.logExtFileFlags -value ($LogFlags -join ',')
}

# Update LogPath if required
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
configuration Sample_xIisLogging_LogFlags
{
param
(
# Target nodes to apply the configuration
[String[]] $NodeName = 'localhost'
)

# Import the module that defines custom resources
Import-DscResource -Module xWebAdministration

Node $NodeName
{
xIisLogging Logging
{
LogPath = 'C:\IISLogFiles'
Logflags = @('Date', 'Time', 'ClientIP', 'ServerIP', 'UserAgent')
LogFormat = 'W3C'
}
}
}
37 changes: 36 additions & 1 deletion tests/Integration/MSFT_XIISLogging.Integration.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ try
{ Get-DscConfiguration -Verbose -ErrorAction Stop } | Should Not throw
}
#endregion
It 'Changing Loggging Truncate Settings ' -test {
It 'Changing Logging Truncate Settings ' -test {

Invoke-Expression -Command "$($script:dscResourceName)_Truncate -OutputPath `$TestDrive"
Start-DscConfiguration -Path $TestDrive -ComputerName localhost -Wait -Verbose -Force
Expand All @@ -97,6 +97,41 @@ try
$currentLogSettings.customFields.Collection[1].SourceType | Should Be 'ResponseHeader'
}
}

Describe "$($script:dscResourceName)_LogFlags" {
#region DEFAULT TESTS
It 'Should compile without throwing' {
{
Invoke-Expression -Command "$($script:dscResourceName)_LogFlags -OutputPath `$TestDrive"
Start-DscConfiguration -Path $TestDrive -ComputerName localhost -Wait -Verbose -Force
} | Should not throw
}

It 'should be able to call Get-DscConfiguration without throwing' {
{ Get-DscConfiguration -Verbose -ErrorAction Stop } | Should Not throw
}
#endregion
It 'Changing enabled LogFlags ' -test {

Invoke-Expression -Command "$($script:dscResourceName)_LogFlags -OutputPath `$TestDrive"
Start-DscConfiguration -Path $TestDrive -ComputerName localhost -Wait -Verbose -Force

$currentLogSettings = Get-WebConfiguration -filter '/system.applicationHost/sites/siteDefaults/Logfile'

$currentLogSettings.directory | Should Be 'C:\IISLogFiles'
$currentLogSettings.logExtFileFlags | Should Be 'Date,Time,ClientIP,ServerIP,UserAgent'
$currentLogSettings.logformat | Should Be 'W3C'
$currentLogSettings.logTargetW3C | Should Be 'File,ETW'
$currentLogSettings.TruncateSize | Should Be '2097152'
$currentLogSettings.localTimeRollover | Should Be 'True'
$currentLogSettings.customFields.Collection[0].LogFieldName | Should Be 'ClientEncoding'
$currentLogSettings.customFields.Collection[0].SourceName | Should Be 'Accept-Encoding'
$currentLogSettings.customFields.Collection[0].SourceType | Should Be 'RequestHeader'
$currentLogSettings.customFields.Collection[1].LogFieldName | Should Be 'X-Powered-By'
$currentLogSettings.customFields.Collection[1].SourceName | Should Be 'ASP.NET'
$currentLogSettings.customFields.Collection[1].SourceType | Should Be 'ResponseHeader'
}
}
}
finally
{
Expand Down
29 changes: 29 additions & 0 deletions tests/Integration/MSFT_XIISLogging.config.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,32 @@ configuration MSFT_xIisLogging_Truncate
)
}
}

configuration MSFT_xIisLogging_LogFlags
{
Import-DscResource -ModuleName xWebAdministration

xIisLogging Logging
{
LogPath = 'C:\IISLogFiles'
Logflags = @('Date','Time','ClientIP','ServerIP','UserAgent')
LoglocalTimeRollover = $true
LogTruncateSize = '2097152'
LogFormat = 'W3C'
LogTargetW3C = 'File,ETW'
LogCustomFields = @(
MSFT_xLogCustomField
{
LogFieldName = 'ClientEncoding'
SourceName = 'Accept-Encoding'
SourceType = 'RequestHeader'
}
MSFT_xLogCustomField
{
LogFieldName = 'X-Powered-By'
SourceName = 'ASP.NET'
SourceType = 'ResponseHeader'
}
)
}
}
35 changes: 33 additions & 2 deletions tests/Integration/MSFT_xWebSite.Integration.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -156,10 +156,41 @@ try
$result.logFile.customFields.Collection[1].LogFieldName | Should Be $dscConfig.AllNodes.LogFieldName2
$result.logFile.customFields.Collection[1].SourceName | Should Be $dscConfig.AllNodes.SourceName2
$result.logFile.customFields.Collection[1].SourceType | Should Be $dscConfig.AllNodes.SourceType2

#Test LogFlags is correct
$result.logFile.LogExtFileFlags | Should Be 'Date,Time,ClientIP,UserName,ServerIP'
$result.logFile.LogFormat | Should Be $dscConfig.AllNodes.LogFormat
}

}

Describe "$($script:dscResourceName)_Logging_Configured" {
#region DEFAULT TESTS
It 'Should compile without throwing' {
{
Invoke-Expression -Command "$($script:dscResourceName)_Logging_Configured -ConfigurationData `$dscConfig -OutputPath `$TestDrive -CertificateThumbprint `$selfSignedCert.Thumbprint"
Start-DscConfiguration -Path $TestDrive -ComputerName localhost -Wait -Verbose -Force
} | Should not throw
}

It 'Should be able to call Get-DscConfiguration without throwing' {
{ Get-DscConfiguration -Verbose -ErrorAction Stop } | Should Not throw
}
#endregion

It 'Should update the enabled LogFlags' -test {

Invoke-Expression -Command "$($script:dscResourceName)_Logging_Configured -ConfigurationData `$dscConfig -OutputPath `$TestDrive -CertificateThumbprint `$selfSignedCert.Thumbprint"

# Build results to test
$result = Get-Website -Name $dscConfig.AllNodes.Website

# Test Website has updated LogFlags
$result.logFile.LogExtFileFlags | Should Be 'Date,Time,ClientIP,ServerIP,UserAgent'
$result.logFile.LogFormat | Should Be $dscConfig.AllNodes.LogFormat
}
}

Describe "$($script:dscResourceName)_Present_Stopped" {
#region DEFAULT TESTS
It 'Should compile without throwing' {
Expand All @@ -176,7 +207,7 @@ try

It 'Should Create a Stopped Website with correct settings' -test {

Invoke-Expression -Command "$($script:dscResourceName)_Present_Stopped -ConfigurationData `$dscConfg -OutputPath `$TestDrive -CertificateThumbprint `$selfSignedCert.Thumbprint"
Invoke-Expression -Command "$($script:dscResourceName)_Present_Stopped -ConfigurationData `$dscConfig -OutputPath `$TestDrive -CertificateThumbprint `$selfSignedCert.Thumbprint"

# Build results to test
$result = Get-Website -Name $dscConfig.AllNodes.Website
Expand Down Expand Up @@ -257,7 +288,7 @@ try

It 'Should remove the Website' -test {

Invoke-Expression -Command "$($script:dscResourceName)_Absent -ConfigurationData `$dscConfg -OutputPath `$TestDrive"
Invoke-Expression -Command "$($script:dscResourceName)_Absent -ConfigurationData `$dscConfig -OutputPath `$TestDrive"

# Build results to test
$result = Get-Website -Name $dscConfig.AllNodes.Website
Expand Down
26 changes: 26 additions & 0 deletions tests/Integration/MSFT_xWebSite.config.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ configuration MSFT_xWebSite_Present_Started
ServiceAutoStartProvider = $Node.ServiceAutoStartProvider
State = 'Started'
ServerAutoStart = $true
LogFlags = $Node.LogFlags1
LogFormat = $Node.LogFormat
LogTargetW3C = 'ETW'
LogCustomFields = @(
MSFT_xLogCustomFieldInformation
Expand Down Expand Up @@ -288,6 +290,8 @@ configuration MSFT_xWebSite_Webconfig_Get_Test_Set
ServiceAutoStartEnabled = $Node.ServiceAutoStartEnabled
ServiceAutoStartProvider = $Node.ServiceAutoStartProvider
State = 'Started'
LogFlags = $Node.LogFlags1
LogFormat = $Node.LogFormat
LogCustomFields = @(
MSFT_xLogCustomFieldInformation
{
Expand All @@ -305,3 +309,25 @@ configuration MSFT_xWebSite_Webconfig_Get_Test_Set
}
}
}

configuration MSFT_xWebSite_Logging_Configured
{
param(

[Parameter(Mandatory = $true)]
[String] $CertificateThumbprint

)

Import-DscResource -ModuleName xWebAdministration

Node $AllNodes.NodeName
{
xWebSite Website
{
Name = $Node.Website
LogFlags = $Node.LogFlags2
LogFormat = $Node.LogFormat
}
}
}
17 changes: 10 additions & 7 deletions tests/Integration/MSFT_xWebSite.config.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,16 @@
HTTPSHostname = 'https.website'
CertificateStoreName = 'MY'
SslFlags = '1'
LogFieldName1 = 'CustomField1'
SourceName1 = 'Accept-Encoding'
SourceType1 = 'RequestHeader'
LogFieldName2 = 'CustomField2'
SourceName2 = 'Warning'
SourceType2 = 'ResponseHeader'
LogTargetW3C = 'ETW'
LogFieldName1 = 'CustomField1'
SourceName1 = 'Accept-Encoding'
SourceType1 = 'RequestHeader'
LogFieldName2 = 'CustomField2'
SourceName2 = 'Warning'
SourceType2 = 'ResponseHeader'
LogTargetW3C = 'ETW'
LogFormat = 'W3C'
Logflags1 = @('Date','Time','ClientIP','UserName','ServerIP')
Logflags2 = @('Date','Time','ClientIP','ServerIP','UserAgent')
}
)
}
26 changes: 13 additions & 13 deletions tests/Unit/MSFT_xWebsite.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -1124,9 +1124,9 @@ try
Assert-MockCalled -CommandName Update-WebsiteBinding -Exactly 1
Assert-MockCalled -CommandName Update-DefaultPage -Exactly 1
Assert-MockCalled -CommandName Set-Authentication -Exactly 4
Assert-MockCalled -CommandName Get-Item -Exactly 3
Assert-MockCalled -CommandName Set-Item -Exactly 3
Assert-MockCalled -CommandName Set-ItemProperty -Exactly 11
Assert-MockCalled -CommandName Get-Item -Exactly 2
Assert-MockCalled -CommandName Set-Item -Exactly 2
Assert-MockCalled -CommandName Set-ItemProperty -Exactly 13
Assert-MockCalled -CommandName Start-Website -Exactly 1
Assert-MockCalled -CommandName Set-WebConfigurationProperty -Exactly 2
Assert-MockCalled -CommandName Test-LogCustomField -Exactly 1
Expand Down Expand Up @@ -1370,7 +1370,7 @@ try
Set-TargetResource @MockParameters

It 'Should call all the mocks' {
Assert-MockCalled -CommandName Set-ItemProperty -Exactly 11
Assert-MockCalled -CommandName Set-ItemProperty -Exactly 13
Assert-MockCalled -CommandName Add-WebConfiguration -Exactly 1
Assert-MockCalled -CommandName Test-WebsiteBinding -Exactly 1
Assert-MockCalled -CommandName Update-WebsiteBinding -Exactly 1
Expand Down Expand Up @@ -1452,9 +1452,9 @@ try
Assert-MockCalled -CommandName Stop-Website -Exactly 1
Assert-MockCalled -CommandName Test-WebsiteBinding -Exactly 1
Assert-MockCalled -CommandName Update-WebsiteBinding -Exactly 1
Assert-MockCalled -CommandName Get-Item -Exactly 1
Assert-MockCalled -CommandName Set-Item -Exactly 1
Assert-MockCalled -CommandName Set-ItemProperty -Exactly 8
Assert-MockCalled -CommandName Get-Item -Exactly 0
Assert-MockCalled -CommandName Set-Item -Exactly 0
Assert-MockCalled -CommandName Set-ItemProperty -Exactly 10
Assert-MockCalled -CommandName Add-WebConfiguration -Exactly 1
Assert-MockCalled -CommandName Update-DefaultPage -Exactly 1
Assert-MockCalled -CommandName Confirm-UniqueBinding -Exactly 1
Expand Down Expand Up @@ -1535,9 +1535,9 @@ try
It 'Should call all the mocks' {
Assert-MockCalled -CommandName Test-WebsiteBinding -Exactly 1
Assert-MockCalled -CommandName Update-WebsiteBinding -Exactly 1
Assert-MockCalled -CommandName Get-Item -Exactly 2
Assert-MockCalled -CommandName Set-Item -Exactly 2
Assert-MockCalled -CommandName Set-ItemProperty -Exactly 6
Assert-MockCalled -CommandName Get-Item -Exactly 1
Assert-MockCalled -CommandName Set-Item -Exactly 1
Assert-MockCalled -CommandName Set-ItemProperty -Exactly 8
Assert-MockCalled -CommandName Set-ItemProperty -ParameterFilter { $Name -eq 'LogFile.directory' } -Exactly 0
Assert-MockCalled -CommandName Add-WebConfiguration -Exactly 1
Assert-MockCalled -CommandName Update-DefaultPage -Exactly 1
Expand Down Expand Up @@ -1617,9 +1617,9 @@ try
It 'Should call all the mocks' {
Assert-MockCalled -CommandName Test-WebsiteBinding -Exactly 1
Assert-MockCalled -CommandName Update-WebsiteBinding -Exactly 1
Assert-MockCalled -CommandName Get-Item -Exactly 2
Assert-MockCalled -CommandName Set-Item -Exactly 2
Assert-MockCalled -CommandName Set-ItemProperty -Exactly 7
Assert-MockCalled -CommandName Get-Item -Exactly 1
Assert-MockCalled -CommandName Set-Item -Exactly 1
Assert-MockCalled -CommandName Set-ItemProperty -Exactly 9
Assert-MockCalled -CommandName Set-ItemProperty -ParameterFilter { $Name -eq 'LogFile.directory' } -Exactly 1
Assert-MockCalled -CommandName Add-WebConfiguration -Exactly 1
Assert-MockCalled -CommandName Update-DefaultPage -Exactly 1
Expand Down

0 comments on commit 0d012cb

Please sign in to comment.