Skip to content

Commit 76d82ae

Browse files
authored
WebConfigPropertyCollection: Allow deleting single item property collections, update examples, remove duplicate resource documentation (#644)
1 parent c3efe11 commit 76d82ae

File tree

9 files changed

+240
-327
lines changed

9 files changed

+240
-327
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ For older change log history see the [historic changelog](HISTORIC_CHANGELOG.md)
1111

1212
- WebConfigPropertyCollection
1313
- Allowed different property collection key types to be added beyond the default.
14+
- Allowed control over single item property collection key types, including examples - fixes ([issue #379](https://github.com/dsccommunity/WebAdministrationDsc/issues/379)), ([issue #631](https://github.com/dsccommunity/WebAdministrationDsc/issues/631)).
1415

1516
### Changed
1617

@@ -24,6 +25,10 @@ For older change log history see the [historic changelog](HISTORIC_CHANGELOG.md)
2425
- WebAdministrationDsc
2526
- Fixed CertificateStoreName default value from `MY` to `My` ([issue #642](https://github.com/dsccommunity/WebAdministrationDsc/issues/642))
2627

28+
### Removed
29+
30+
- Removed outdated resources documentation from README.md.
31+
2732
## [4.2.0] - 2024-08-26
2833

2934
### Removed

README.md

Lines changed: 31 additions & 326 deletions
Large diffs are not rendered by default.

source/DSCResources/DSC_WebConfigPropertyCollection/DSC_WebConfigPropertyCollection.psm1

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,12 @@ function Set-TargetResource
274274
Write-Verbose `
275275
-Message ($script:localizedData.VerboseSetTargetRemoveItem -f $ItemPropertyName )
276276

277+
# If we are removing a single collection item with the wildcard syntax for the key, set key name to match the property name
278+
if ($ItemKeyName -eq '*')
279+
{
280+
$ItemKeyName = $ItemPropertyName
281+
}
282+
277283
$filter = "$($Filter)/$($CollectionName)"
278284
Remove-WebConfigurationProperty `
279285
-PSPath $WebsitePath `

source/Examples/Resources/WebConfigPropertyCollection/Sample_WebConfigPropertyCollection_Remove.ps1

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ Configuration Sample_WebConfigPropertyCollection_Remove
3030
ItemKeyName = 'verb'
3131
ItemKeyValue = 'TRACE'
3232
ItemPropertyName = 'allowed'
33-
ItemPropertyValue = 'false'
3433
Ensure = 'Absent'
3534
}
3635
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<#
2+
.SYNOPSIS
3+
Make appsettings.json inaccessible to clients.
4+
5+
.DESCRIPTION
6+
This example shows how to use the WebConfigPropertyCollection DSC resource for adding a single item configuration element.
7+
It will add an "add" element to the system.webServer/security/requestFiltering/hiddenSegments collection to block appsettings.json.
8+
#>
9+
Configuration Sample_WebConfigPropertyCollection_SingleItemAdd
10+
{
11+
param
12+
(
13+
# Target nodes to apply the configuration.
14+
[Parameter()]
15+
[String[]]
16+
$NodeName = 'localhost'
17+
)
18+
19+
# Import the modules that define custom resources
20+
Import-DscResource -ModuleName WebAdministrationDsc
21+
22+
Node $NodeName
23+
{
24+
WebConfigPropertyCollection "$($NodeName) - Block appsettings.json"
25+
{
26+
WebsitePath = 'MACHINE/WEBROOT/APPHOST'
27+
Filter = 'system.webServer/security/requestFiltering'
28+
CollectionName = 'hiddenSegments'
29+
ItemName = 'add'
30+
ItemKeyName = '*'
31+
ItemKeyValue = 'appsettings.json'
32+
ItemPropertyName = 'segment'
33+
ItemPropertyValue = 'appsettings.json'
34+
Ensure = 'Present'
35+
}
36+
}
37+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<#
2+
.SYNOPSIS
3+
Removes making appsettings.json inaccessible to clients.
4+
5+
.DESCRIPTION
6+
This example shows how to use the WebConfigPropertyCollection DSC resource for removing a single item configuration element.
7+
It will remove the "add" element from the system.webServer/security/requestFiltering/hiddenSegments collection (if present) for blocking appsettings.json.
8+
#>
9+
Configuration Sample_WebConfigPropertyCollection_SingleItemRemove
10+
{
11+
param
12+
(
13+
# Target nodes to apply the configuration.
14+
[Parameter()]
15+
[String[]]
16+
$NodeName = 'localhost'
17+
)
18+
19+
# Import the modules that define custom resources
20+
Import-DscResource -ModuleName WebAdministrationDsc
21+
22+
Node $NodeName
23+
{
24+
WebConfigPropertyCollection "$($NodeName) - Remove blocking appsettings.json"
25+
{
26+
WebsitePath = 'MACHINE/WEBROOT/APPHOST'
27+
Filter = 'system.webServer/security/requestFiltering'
28+
CollectionName = 'hiddenSegments'
29+
ItemName = 'add'
30+
ItemKeyName = '*'
31+
ItemKeyValue = 'appsettings.json'
32+
ItemPropertyName = 'segment'
33+
Ensure = 'Absent'
34+
}
35+
}
36+
}

tests/Integration/DSC_WebConfigPropertyCollection.Integration.Tests.ps1

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,12 @@ try
5959
IntegerItemKeyValue = 'Content-Type'
6060
IntegerItemPropertyName = 'Sizelimit'
6161
IntegerItemPropertyValue = [string](Get-Random -Minimum 11 -Maximum 100)
62+
SingleItemFilter = 'system.webServer/security/requestFiltering'
63+
SingleItemCollectionName = 'hiddenSegments'
64+
SingleItemKeyName = '*'
65+
SingleItemKeyValue = 'appsettings.json'
66+
SingleItemPropertyName = 'segment'
67+
SingleItemPropertyValue = 'appsettings.json'
6268
}
6369
)
6470
}
@@ -78,6 +84,12 @@ try
7884
$integerItemKeyValue = $ConfigurationData.AllNodes.IntegerItemKeyValue
7985
$integerItemPropertyName = $ConfigurationData.AllNodes.IntegerItemPropertyName
8086
$integerItemPropertyValue = $ConfigurationData.AllNodes.IntegerItemPropertyValue
87+
$singleItemFilter = $ConfigurationData.AllNodes.SingleItemFilter
88+
$singleItemCollectionName = $ConfigurationData.AllNodes.SingleItemCollectionName
89+
$singleItemKeyName = $ConfigurationData.AllNodes.SingleItemKeyName
90+
$singleItemKeyValue = $ConfigurationData.AllNodes.SingleItemKeyValue
91+
$singleItemPropertyName = $ConfigurationData.AllNodes.SingleItemPropertyName
92+
$singleItemPropertyValue = $ConfigurationData.AllNodes.SingleItemPropertyValue
8193

8294
$startDscConfigurationParameters = @{
8395
Path = $TestDrive
@@ -89,6 +101,7 @@ try
89101

90102
$filterValue = "$($filter)/$($collectionName)/$($itemName)[@$($itemKeyName)='$($itemKeyValue)']/@$itemPropertyName"
91103
$integerFilterValue = "$($integerFilter)/$($integerCollectionName)/$($itemName)[@$($integerItemKeyName)='$($integerItemKeyValue)']/@$integerItemPropertyName"
104+
$singleItemFilterValue = "$($singleItemFilter)/$($singleItemCollectionName)/$($itemName)[@$($singleItemKeyName)='$($singleItemKeyValue)']/@$singleItemPropertyName"
92105

93106
Context 'When Adding Collection item' {
94107
It 'Should compile and apply the MOF without throwing' {
@@ -183,6 +196,53 @@ try
183196
}
184197
}
185198

199+
Context 'When Adding Single Collection item' {
200+
It 'Should compile and apply the MOF without throwing' {
201+
{
202+
& "$($script:dscResourceName)_SingleItemAdd" -OutputPath $TestDrive -ConfigurationData $configurationData
203+
Start-DscConfiguration @startDscConfigurationParameters
204+
} | Should -Not -Throw
205+
}
206+
207+
It 'Should be able to call Get-DscConfiguration without throwing' {
208+
{ Get-DscConfiguration -Verbose -ErrorAction Stop } | Should -Not -Throw
209+
}
210+
211+
It 'Should return $true for Test-DscConfiguration' {
212+
Test-DscConfiguration | Should Be $true
213+
}
214+
215+
It 'Should have the correct value of the configuration property collection item' {
216+
# Get the new value.
217+
$value = (Get-WebConfigurationProperty -PSPath $websitePath -Filter $singleItemFilterValue -Name "." -ErrorAction SilentlyContinue).Value
218+
219+
$value | Should -Be $singleItemPropertyValue
220+
}
221+
}
222+
223+
Context 'When Removing Single Collection item' {
224+
It 'Should compile and apply the MOF without throwing' {
225+
{
226+
& "$($script:dscResourceName)_SingleItemRemove" -OutputPath $TestDrive -ConfigurationData $configurationData
227+
Start-DscConfiguration @startDscConfigurationParameters
228+
} | Should -Not -Throw
229+
}
230+
231+
It 'Should be able to call Get-DscConfiguration without throwing' {
232+
{ Get-DscConfiguration -Verbose -ErrorAction Stop } | Should -Not -Throw
233+
}
234+
235+
It 'Should return $true for Test-DscConfiguration' {
236+
Test-DscConfiguration | Should Be $true
237+
}
238+
239+
It 'Should remove configuration property' {
240+
$value = (Get-WebConfigurationProperty -PSPath $websitePath -Filter $singleItemFilterValue -Name "." -ErrorAction SilentlyContinue).Value
241+
242+
$value | Should -BeNullOrEmpty
243+
}
244+
}
245+
186246
# Remove the website we created for testing purposes.
187247
if (Get-Website -Name $websiteName)
188248
{

tests/Integration/DSC_WebConfigPropertyCollection.config.ps1

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,44 @@ Configuration DSC_WebConfigPropertyCollection_Integer
8181
}
8282
}
8383
}
84+
85+
Configuration DSC_WebConfigPropertyCollection_SingleItemAdd
86+
{
87+
Import-DscResource -ModuleName WebAdministrationDsc
88+
89+
node localhost
90+
{
91+
WebConfigPropertyCollection IntegrationTest
92+
{
93+
WebsitePath = $Node.WebsitePath
94+
Filter = $Node.SingleItemFilter
95+
CollectionName = $Node.SingleItemCollectionName
96+
ItemName = $Node.ItemName
97+
ItemKeyName = $Node.SingleItemKeyName
98+
ItemKeyValue = $Node.SingleItemKeyValue
99+
ItemPropertyName = $Node.SingleItemPropertyName
100+
ItemPropertyValue = $Node.SingleItemPropertyValue
101+
Ensure = 'Present'
102+
}
103+
}
104+
}
105+
106+
Configuration DSC_WebConfigPropertyCollection_SingleItemRemove
107+
{
108+
Import-DscResource -ModuleName WebAdministrationDsc
109+
110+
node localhost
111+
{
112+
WebConfigPropertyCollection IntegrationTest
113+
{
114+
WebsitePath = $Node.WebsitePath
115+
Filter = $Node.SingleItemFilter
116+
CollectionName = $Node.SingleItemCollectionName
117+
ItemName = $Node.ItemName
118+
ItemKeyName = $Node.SingleItemKeyName
119+
ItemKeyValue = $Node.SingleItemKeyValue
120+
ItemPropertyName = $Node.SingleItemPropertyName
121+
Ensure = 'Absent'
122+
}
123+
}
124+
}

tests/Unit/DSC_WebConfigPropertyCollection.Tests.ps1

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,18 @@ try
6060
Ensure = 'Absent'
6161
}
6262

63+
$script:absentSingleItemParameters = @{
64+
WebsitePath = 'MACHINE/WEBROOT/APPHOST'
65+
Filter = 'system.webServer/security/requestFiltering'
66+
CollectionName = 'hiddenSegments'
67+
ItemName = 'add'
68+
ItemKeyName = '*'
69+
ItemKeyValue = 'appsettings.json'
70+
ItemPropertyName = 'segment'
71+
ItemPropertyValue = 'appsettings.json'
72+
Ensure = 'Absent'
73+
}
74+
6375
#region Function Get-TargetResource
6476
Describe "$($script:dscResourceName)\Get-TargetResource" {
6577
$parameters = @{
@@ -310,6 +322,18 @@ try
310322
Assert-MockCalled -CommandName Remove-WebConfigurationProperty -Times 1 -Exactly
311323
}
312324
}
325+
326+
Context 'Ensure is absent and single collection item element' {
327+
Mock -CommandName Remove-WebConfigurationProperty -MockWith {}
328+
329+
It 'Should call the right Mocks' {
330+
Set-TargetResource @script:absentSingleItemParameters
331+
332+
Assert-MockCalled -CommandName Remove-WebConfigurationProperty -Times 1 -Exactly -ParameterFilter {
333+
$AtElement[$script:absentSingleItemParameters.ItemPropertyName] -eq $script:absentSingleItemParameters.ItemKeyValue
334+
}
335+
}
336+
}
313337
}
314338
#endregion Function Set-TargetResource
315339

0 commit comments

Comments
 (0)