Skip to content

Commit

Permalink
xIisLogging and xWebsite: Add Ensure property to LogCustomFields (#576)
Browse files Browse the repository at this point in the history
- Website
  - Add Ensure to LogCustomFieldInformation. (issue #571)
- IisLogging
  - Can now remove all LogCustomFields using Ensure. (issue #571)
  • Loading branch information
Clay10J committed Dec 7, 2023
1 parent 1f5a356 commit 3798530
Show file tree
Hide file tree
Showing 17 changed files with 653 additions and 32 deletions.
20 changes: 15 additions & 5 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@ For older change log history see the [historic changelog](HISTORIC_CHANGELOG.md)
present in the module DscResource.Common.
- Removed the function `Get-CurrentUser` since no code were using it.

### Changed

- Website
- Add Ensure to LogCustomFieldInformation. ([issue #571](https://github.com/dsccommunity/WebAdministrationDsc/issues/571))

### Fixed

- IisLogging
- Can now remove all LogCustomFields using Ensure. ([issue #571](https://github.com/dsccommunity/WebAdministrationDsc/issues/571))

## [4.1.0] - 2023-01-03

### Fixed
Expand All @@ -32,10 +42,10 @@ For older change log history see the [historic changelog](HISTORIC_CHANGELOG.md)
- CommonTestHelper
Added `Invoke-UnitTestCleanup` to get consistent cleanup of stubs.
Gives correct execution of integration tests when run in same PowerShell session as unit tests (no longer calling stubs).
Gives correct `Restore-WebConfiguration` after integration tests when run in same PowerShell session as unit tests (no longer calling stub).
Gives correct `Restore-WebConfiguration` after integration tests when run in same PowerShell session as unit tests (no longer calling stub).
- MockWebAdministrationWindowsFeature
[Issue #351](https://github.com/dsccommunity/WebAdministrationDsc/issues/351)
Stubs now throw StubNotImplemented when they are called in order to show when a cmdlet is not mocked correctly.
Stubs now throw StubNotImplemented when they are called in order to show when a cmdlet is not mocked correctly.

## [4.0.0] - 2022-09-17

Expand Down Expand Up @@ -174,9 +184,9 @@ For older change log history see the [historic changelog](HISTORIC_CHANGELOG.md)

- xWebAdministration.Common
- Added new helper function `Get-WebConfigurationPropertyValue` to
help return a value of a `WebConfigurationProperty`. *This helper*
*function is unable to be unit tested because it is using a type*
*that cannot be mocked.*
help return a value of a `WebConfigurationProperty`. _This helper_
_function is unable to be unit tested because it is using a type_
_that cannot be mocked._
- xWebAppPoolDefaults
- Changed to use the new helper function `Get-WebConfigurationPropertyValue`
so that the resource can be properly unit tested.
Expand Down
10 changes: 8 additions & 2 deletions source/DSCResources/DSC_IisLogging/DSC_IisLogging.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -500,8 +500,14 @@ function Set-LogCustomField
<#
Set-WebConfigurationProperty updates logfile.customFields.
#>

Set-WebConfigurationProperty -PSPath 'MACHINE/WEBROOT/APPHOST' -Filter "system.applicationHost/Sites/SiteDefaults/logFile/customFields" -Name "." -Value $setCustomFields
if ($setCustomFields.Count -gt 0)
{
Set-WebConfigurationProperty -PSPath 'MACHINE/WEBROOT/APPHOST' -Filter "system.applicationHost/Sites/SiteDefaults/logFile/customFields" -Name "." -Value $setCustomFields
}
else
{
Remove-WebConfigurationProperty -PSPath 'MACHINE/WEBROOT/APPHOST' -Filter "system.applicationHost/Sites/SiteDefaults/logFile/customFields" -Name "."
}
}

<#
Expand Down
36 changes: 26 additions & 10 deletions source/DSCResources/DSC_WebSite/DSC_WebSite.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -1793,17 +1793,27 @@ function Set-LogCustomField
$setCustomFields = @()
foreach ($customField in $LogCustomField)
{
$setCustomFields += @{
logFieldName = $customField.LogFieldName
sourceName = $customField.SourceName
sourceType = $customField.SourceType
if ($customField.Ensure -ne 'Absent')
{
$setCustomFields += @{
logFieldName = $customField.LogFieldName
sourceName = $customField.SourceName
sourceType = $customField.SourceType
}
}
}

# The second Set-WebConfigurationProperty is to handle an edge case where logfile.customFields is not updated correctly. May be caused by a possible bug in the IIS provider
for ($i = 1; $i -le 2; $i++)
{
Set-WebConfigurationProperty -PSPath 'MACHINE/WEBROOT/APPHOST' -Filter "system.applicationHost/sites/site[@name='$Site']/logFile/customFields" -Name "." -Value $setCustomFields
if ($setCustomFields.Count -gt 0)
{
Set-WebConfigurationProperty -PSPath 'MACHINE/WEBROOT/APPHOST' -Filter "system.applicationHost/sites/site[@name='$Site']/logFile/customFields" -Name "." -Value $setCustomFields
}
else
{
Remove-WebConfigurationProperty -PSPath 'MACHINE/WEBROOT/APPHOST' -Filter "system.applicationHost/sites/site[@name='$Site']/logFile/customFields" -Name "."
}
}
}

Expand Down Expand Up @@ -2087,29 +2097,35 @@ function Test-LogCustomField
$LogCustomField
)

$inDesiredSate = $true
$inDesiredState = $true

foreach ($customField in $LogCustomField)
{
$filterString = "/system.applicationHost/sites/site[@name='{0}']/logFile/customFields/add[@logFieldName='{1}']" -f $Site, $customField.LogFieldName
$presentCustomField = Get-WebConfigurationProperty -Filter $filterString -Name "."

$shouldBePresent = $customField.Ensure -ne 'Absent'

if ($presentCustomField)
{
$sourceNameMatch = $customField.SourceName -eq $presentCustomField.SourceName
$sourceTypeMatch = $customField.SourceType -eq $presentCustomField.sourceType
if (-not ($sourceNameMatch -and $sourceTypeMatch))
$doesNotMatchEnsure = (-not ($sourceNameMatch -and $sourceTypeMatch)) -eq $shouldBePresent
if ($doesNotMatchEnsure)
{
$inDesiredSate = $false
$inDesiredState = $false
}
}
else
{
$inDesiredSate = $false
if ($shouldBePresent)
{
$inDesiredState = $false
}
}
}

return $inDesiredSate
return $inDesiredState
}

<#
Expand Down
1 change: 1 addition & 0 deletions source/DSCResources/DSC_WebSite/DSC_WebSite.schema.mof
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,5 @@ class DSC_LogCustomFieldInformation
[Write, Description("Field name to identify the custom field within the log file. Please note that the field name cannot contain spaces.")] String LogFieldName;
[Write, Description("The acceptable values for this property are: RequestHeader, ResponseHeader or ServerVariable (note that enhanced logging cannot log a server variable with a name that contains lower-case characters - to include a server variable in the event log just make sure that its name consists of all upper-case characters)."),ValueMap{"RequestHeader","ResponseHeader","ServerVariable"},Values{"RequestHeader","ResponseHeader","ServerVariable"}] String SourceType;
[Write, Description("Name of the HTTP header or server variable (depending on the Source Type you selected) that contains a value that you want to log.")] String SourceName;
[Write, Description("Indicates if the custom log field should be present or absent. Defaults to Present."), ValueMap{"Present","Absent"}, Values{"Present","Absent"}] String Ensure;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
configuration Sample_IisLogging_LogCustomFields_EnsureAbsent
{
param
(
# Target nodes to apply the configuration
[String[]] $NodeName = 'localhost'
)

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

Node $NodeName
{
IisLogging Logging
{
LogPath = 'C:\IISLogFiles'
Logflags = @('Date','Time','ClientIP','ServerIP','UserAgent')
LogFormat = 'W3C'
LogTargetW3C = 'File,ETW'
LogCustomFields = @(
DSC_LogCustomField
{
LogFieldName = 'ClientEncoding'
SourceName = 'Accept-Encoding'
SourceType = 'RequestHeader'
Ensure = 'Absent'
}
)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
configuration Sample_IisLogging_LogCustomFields_EnsurePresentDefault
{
param
(
# Target nodes to apply the configuration
[String[]] $NodeName = 'localhost'
)

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

Node $NodeName
{
IisLogging Logging
{
LogPath = 'C:\IISLogFiles'
Logflags = @('Date','Time','ClientIP','ServerIP','UserAgent')
LogFormat = 'W3C'
LogTargetW3C = 'File,ETW'
LogCustomFields = @(
DSC_LogCustomField
{
LogFieldName = 'ClientEncoding'
SourceName = 'Accept-Encoding'
SourceType = 'RequestHeader'
}
)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
configuration Sample_IisLogging_LogCustomFields_EnsurePresentExplicitly
{
param
(
# Target nodes to apply the configuration
[String[]] $NodeName = 'localhost'
)

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

Node $NodeName
{
IisLogging Logging
{
LogPath = 'C:\IISLogFiles'
Logflags = @('Date','Time','ClientIP','ServerIP','UserAgent')
LogFormat = 'W3C'
LogTargetW3C = 'File,ETW'
LogCustomFields = @(
DSC_LogCustomField
{
LogFieldName = 'ClientEncoding'
SourceName = 'Accept-Encoding'
SourceType = 'RequestHeader'
Ensure = 'Present'
}
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ configuration Sample_WebSite_NewWebsite
{
Ensure = 'Present'
Name = $WebSiteName
SiteId = $SiteId
SiteId = $SiteId
State = 'Started'
ServerAutoStart = $true
PhysicalPath = $DestinationPath
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
configuration Sample_WebSite_WithCustomLogFields_EnsureAbsent
{
param
(
# Target nodes to apply the configuration
[String[]]
$NodeName = 'localhost',

# Name of the website to create
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[String]
$WebSiteName,

# Optional Site Id for the website
[Parameter()]
[UInt32]
$SiteId,

# Source Path for Website content
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[String]
$SourcePath,

# Destination path for Website content
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[String]
$DestinationPath
)

# Import the module that defines custom resources
Import-DscResource -Module WebAdministrationDsc, PSDesiredStateConfiguration

Node $NodeName
{
# Install the IIS role
WindowsFeature IIS
{
Ensure = 'Present'
Name = 'Web-Server'
}

# Install the ASP .NET 4.5 role
WindowsFeature AspNet45
{
Ensure = 'Present'
Name = 'Web-Asp-Net45'
}

# Stop the default website
WebSite DefaultSite
{
Ensure = 'Present'
Name = 'Default Web Site'
State = 'Stopped'
ServerAutoStart = $false
PhysicalPath = 'C:\inetpub\wwwroot'
DependsOn = '[WindowsFeature]IIS'
}

# Copy the website content
File WebContent
{
Ensure = 'Present'
SourcePath = $SourcePath
DestinationPath = $DestinationPath
Recurse = $true
Type = 'Directory'
DependsOn = '[WindowsFeature]AspNet45'
}

# Create the new Website
WebSite NewWebsite
{
Ensure = 'Present'
Name = $WebSiteName
SiteId = $SiteId
State = 'Started'
ServerAutoStart = $true
PhysicalPath = $DestinationPath
DependsOn = '[File]WebContent'
LogFlags = @('Date','Time','ClientIP','ServerIP','UserAgent')
LogFormat = 'W3C'
LogCustomFields = @(
DSC_LogCustomFieldInformation
{
LogFieldName = 'ClientEncoding'
SourceName = 'Accept-Encoding'
SourceType = 'RequestHeader'
Ensure = 'Absent'
}
)
}
}
}
Loading

0 comments on commit 3798530

Please sign in to comment.