Skip to content

Commit f6dd161

Browse files
authored
AdcsAuthorityInformationAccess: Convert to Class-Based Resource (#149)
1 parent 00e1b42 commit f6dd161

23 files changed

Lines changed: 1796 additions & 1694 deletions

.vscode/settings.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@
3636
"keepachangelog",
3737
"notin",
3838
"pscmdlet",
39-
"steppable"
39+
"steppable",
40+
"adcs",
41+
"ocsp"
4042
],
4143
"cSpell.ignorePaths": [
4244
".git"

CHANGELOG.md

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99

1010
- ActiveDirectoryCSDsc
1111
- Automatically publish documentation to GitHub Wiki - Fixes [Issue #122](https://github.com/dsccommunity/ActiveDirectoryCSDsc/issues/122).
12+
- Revert Pester to non-prerelease.
13+
- Use DscResource.Base pre-release.
1214
- Renamed `master` branch to `main` - Fixes [Issue #124](https://github.com/dsccommunity/ActiveDirectoryCSDsc/issues/124).
1315
- Updated `GitVersion.yml` to latest pattern - Fixes [Issue #126](https://github.com/dsccommunity/ActiveDirectoryCSDsc/issues/126).
1416
- Updated build to use `Sampler.GitHubTasks` - Fixes [Issue #129](https://github.com/dsccommunity/ActiveDirectoryCSDsc/issues/129).
@@ -17,12 +19,29 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1719
- Azure pipelines - Remove deprecated images, make the build work
1820
- Azure Pipelines - Reverted `PublishCodeCoverageResults` back to v1 to display line level coverage in ADO.
1921
- `Tests` - Added tests for catch blocks in `AdcsCertificationAuthority`, `AcdsOnlineResponder`, `AdcsWebEnrollment`.
20-
- `Tests` - Migrate all tests to Pester 5
22+
- `Tests` - Migrate all tests to Pester 5.
23+
- Include module file in code coverage.
24+
- Add RootModule to Module psd1.
2125
- AdcsAuthorityInformationAccess
2226
- Removed `AllowRestartService` parameter from compared settings, force `Get-CaAiaUriList`
2327
to return unmangled `System.String[]` with single values.
2428
Fixes [Issue #128](https://github.com/dsccommunity/ActiveDirectoryCSDsc/issues/128)
2529
Fixes [Issue #138](https://github.com/dsccommunity/ActiveDirectoryCSDsc/issues/138).
30+
- Convert to class-based resource.
31+
- `Get-CaAiaUriList`
32+
- Clean up tests.
33+
- Change Write-Verbose to Write-Debug.
34+
- `Get-CAAuthorityInformationAccess` force Verbose to false.
35+
- Move to Private function.
36+
- `Restart-ServiceIfExists`
37+
- Change Write-Verbose to Write-Debug.
38+
39+
### Added
40+
41+
- AdcsReason
42+
- Used in class-based resources.
43+
- Testing
44+
- WebAdministration Stub, to allow AdcsEnrollmentPolicyWebService to pass without the module being installed.
2645

2746
## [5.0.0] - 2020-06-20
2847

RequiredModules.psd1

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,26 @@
1818
Pester = @{
1919
Version = 'latest'
2020
Parameters = @{
21-
AllowPrerelease = $true
21+
AllowPrerelease = $false
2222
}
2323
}
2424

25-
Plaster = 'latest'
26-
ModuleBuilder = 'latest'
27-
ChangelogManagement = 'latest'
28-
Sampler = 'latest'
29-
'Sampler.GitHubTasks' = 'latest'
30-
MarkdownLinkCheck = 'latest'
31-
'DscResource.Test' = 'latest'
32-
xDscResourceDesigner = 'latest'
25+
Plaster = 'latest'
26+
ModuleBuilder = 'latest'
27+
ChangelogManagement = 'latest'
28+
Sampler = 'latest'
29+
'Sampler.GitHubTasks' = 'latest'
30+
MarkdownLinkCheck = 'latest'
31+
'DscResource.Test' = 'latest'
32+
xDscResourceDesigner = 'latest'
3333

3434
# Build dependencies needed for using the module
35-
'DscResource.Base' = 'latest'
35+
'DscResource.Base' = @{
36+
Version = 'latest'
37+
Parameters = @{
38+
AllowPrerelease = $true
39+
}
40+
}
3641
'DscResource.Common' = 'latest'
3742

3843
# Analyzer rules

build.yaml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,6 @@ Pester:
110110
ExcludeFromCodeCoverage:
111111
- Modules/DscResource.Common
112112
- Modules/DscResource.Base
113-
- prefix.ps1
114-
- ActiveDirectoryCSDsc.psm1
115113

116114
####################################################
117115
# Pester Configuration (DscResource.Test) #

source/ActiveDirectoryCSDsc.psd1

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
@{
2+
# Script module or binary module file associated with this manifest.
3+
RootModule = 'ActiveDirectoryCSDsc.psm1'
4+
25
# Version number of this module.
36
moduleVersion = '0.0.1'
47

source/Build.psd1

Lines changed: 0 additions & 5 deletions
This file was deleted.

source/Classes/001.AdcsReason.ps1

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<#
2+
.SYNOPSIS
3+
The reason a property of a DSC resource is not in desired state.
4+
5+
.DESCRIPTION
6+
A DSC resource can have a read-only property `Reasons` that the compliance
7+
part (audit via Azure Policy) of Azure AutoManage Machine Configuration
8+
uses. The property Reasons holds an array of AdcsReason. Each AdcsReason
9+
explains why a property of a DSC resource is not in desired state.
10+
#>
11+
12+
class AdcsReason
13+
{
14+
[DscProperty()]
15+
[System.String]
16+
$Code
17+
18+
[DscProperty()]
19+
[System.String]
20+
$Phrase
21+
}
Lines changed: 209 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,209 @@
1+
<#
2+
.SYNOPSIS
3+
The `AdcsAuthorityInformationAccess` DSC resource is used to configure the
4+
URIs in the Authority Information Access and Online Responder OCSP extensions.
5+
6+
.DESCRIPTION
7+
This resource can be used to configure the URIs in the Authority Information
8+
Access and Online Responder OCSP extensions of certificates issued by an
9+
Active Directory Certificate Authority.
10+
11+
.PARAMETER IsSingleInstance
12+
Specifies the resource is a single instance, the value must be 'Yes'.
13+
14+
.PARAMETER AiaUri
15+
Specifies the list of URIs that should be included in the AIA extension of
16+
the issued certificate.
17+
18+
.PARAMETER OcspUri
19+
Specifies the list of URIs that should be included in the Online Responder
20+
OCSP extension of the issued certificate.
21+
22+
.PARAMETER AllowRestartService
23+
Allows the Certificate Authority service to be restarted if changes are made.
24+
25+
.PARAMETER Reasons
26+
Returns the reason a property is not in desired state.
27+
28+
.NOTES
29+
Used Functions:
30+
Name | Module
31+
--------------------------------- |--------------------------
32+
Add-CAAuthorityInformationAccess | AdcsAdministration
33+
Remove-CAAuthorityInformationAccess | AdcsAdministration
34+
Assert-Module | DscResource.Common
35+
Assert-BoundParameter | DscResource.Common
36+
Get-CaAiaUriList | ActiveDirectoryCSDsc
37+
Restart-ServiceIfExists | ActiveDirectoryCSDsc.Common
38+
#>
39+
40+
[DscResource()]
41+
class AdcsAuthorityInformationAccess : ResourceBase
42+
{
43+
[DscProperty(Key)]
44+
[System.String]
45+
$IsSingleInstance = 'Yes'
46+
47+
[DscProperty()]
48+
[System.String[]]
49+
$AiaUri
50+
51+
[DscProperty()]
52+
[System.String[]]
53+
$OcspUri
54+
55+
[DscProperty()]
56+
[Nullable[System.Boolean]]
57+
$AllowRestartService
58+
59+
[DscProperty(NotConfigurable)]
60+
[AdcsReason[]]
61+
$Reasons
62+
63+
AdcsAuthorityInformationAccess () : base ($PSScriptRoot)
64+
{
65+
# These properties will not be enforced.
66+
$this.ExcludeDscProperties = @(
67+
'IsSingleInstance'
68+
'AllowRestartService'
69+
)
70+
}
71+
72+
[AdcsAuthorityInformationAccess] Get()
73+
{
74+
# Call the base method to return the properties.
75+
return ([ResourceBase] $this).Get()
76+
}
77+
78+
# Base method Get() calls this method to get the current state as a Hashtable.
79+
[System.Collections.Hashtable] GetCurrentState([System.Collections.Hashtable] $properties)
80+
{
81+
$state = @{}
82+
83+
$AiaList = [System.String[]] (Get-CaAiaUriList -ExtensionType 'AddToCertificateAia')
84+
$OcspList = [System.String[]] (Get-CaAiaUriList -ExtensionType 'AddToCertificateOcsp')
85+
86+
if ($AiaList -or $OcspList)
87+
{
88+
$state = @{
89+
IsSingleInstance = $properties.IsSingleInstance
90+
AiaUri = $AiaList
91+
OcspUri = $OcspList
92+
}
93+
}
94+
95+
return $state
96+
}
97+
98+
[void] Set()
99+
{
100+
# Call the base method to enforce the properties.
101+
([ResourceBase] $this).Set()
102+
}
103+
104+
<#
105+
Base method Set() call this method with the properties that should be
106+
enforced and that are not in desired state.
107+
#>
108+
hidden [void] Modify([System.Collections.Hashtable] $properties)
109+
{
110+
$RestartRequired = $false
111+
112+
if ($properties.ContainsKey('AiaUri'))
113+
{
114+
# Get the array number of the entry
115+
$index = [System.Array]::IndexOf($this.PropertiesNotInDesiredState.Property, 'AiaUri')
116+
117+
# Add any missing AIA URIs
118+
foreach ($desiredAiaUri in $this.PropertiesNotInDesiredState[$index].ExpectedValue)
119+
{
120+
if ($desiredAiaUri -notin $this.PropertiesNotInDesiredState[$index].ActualValue)
121+
{
122+
Write-Debug -Message ($this.localizedData.AddingAdcsAiaUriMessage -f 'AIA', $desiredAiaUri)
123+
124+
Add-CAAuthorityInformationAccess -Uri $desiredAiaUri -AddToCertificateAia -Force -Verbose:$false
125+
126+
$RestartRequired = $true
127+
}
128+
}
129+
130+
# Remove any AIA URIs that aren't required
131+
foreach ($currentAiaUri in $this.PropertiesNotInDesiredState[$index].ActualValue)
132+
{
133+
if ($currentAiaUri -notin $this.PropertiesNotInDesiredState[$index].ExpectedValue)
134+
{
135+
Write-Debug -Message ($this.localizedData.RemovingAdcsAiaUriMessage -f 'AIA', $currentAiaUri)
136+
137+
Remove-CAAuthorityInformationAccess -Uri $currentAiaUri -AddToCertificateAia -Force -Verbose:$false
138+
139+
$RestartRequired = $true
140+
}
141+
}
142+
}
143+
144+
if ($properties.ContainsKey('OcspUri'))
145+
{
146+
# Get the array number of the entry
147+
$index = [System.Array]::IndexOf($this.PropertiesNotInDesiredState.Property, 'OcspUri')
148+
149+
# Add any missing OCSP URIs
150+
foreach ($desiredOcspUri in $this.PropertiesNotInDesiredState[$index].ExpectedValue)
151+
{
152+
if ($desiredOcspUri -notin $this.PropertiesNotInDesiredState[$index].ActualValue)
153+
{
154+
Write-Debug -Message ($this.localizedData.AddingAdcsAiaUriMessage -f 'OCSP', $desiredOcspUri)
155+
156+
Add-CAAuthorityInformationAccess -Uri $desiredOcspUri -AddToCertificateOcsp -Force -Verbose:$false
157+
158+
$RestartRequired = $true
159+
}
160+
}
161+
162+
# Remove any OCSP URIs that aren't required
163+
foreach ($currentOcspUri in $this.PropertiesNotInDesiredState[$index].ActualValue)
164+
{
165+
if ($currentOcspUri -notin $this.PropertiesNotInDesiredState[$index].ExpectedValue)
166+
{
167+
Write-Debug -Message ($this.localizedData.RemovingAdcsAiaUriMessage -f 'OCSP', $currentOcspUri)
168+
169+
Remove-CAAuthorityInformationAccess -Uri $currentOcspUri -AddToCertificateOcsp -Force -Verbose:$false
170+
171+
$RestartRequired = $true
172+
}
173+
}
174+
}
175+
176+
if ($RestartRequired -and $this.AllowRestartService)
177+
{
178+
Write-Debug -Message $this.localizedData.RestartingCertSvcMessage
179+
180+
$null = Restart-ServiceIfExists -Name 'CertSvc' -Verbose:$false
181+
}
182+
}
183+
184+
[System.Boolean] Test()
185+
{
186+
# Call the base method to test all of the properties that should be enforced.
187+
return ([ResourceBase] $this).Test()
188+
}
189+
190+
<#
191+
Base method Assert() call this method with the properties that was assigned
192+
a value.
193+
#>
194+
hidden [void] AssertProperties([System.Collections.Hashtable] $properties)
195+
{
196+
Assert-Module -ModuleName 'ADCSAdministration'
197+
198+
$assertBoundParameterParameters = @{
199+
BoundParameterList = $properties
200+
RequiredParameter = @(
201+
'AiaUri'
202+
'OcspUri'
203+
)
204+
RequiredBehavior = 'Any'
205+
}
206+
207+
Assert-BoundParameter @assertBoundParameterParameters
208+
}
209+
}

0 commit comments

Comments
 (0)