Skip to content

Commit

Permalink
Composite: Centralized SkipRule functionality (#83)
Browse files Browse the repository at this point in the history
* moved to dot source file

* renamed test

* consolidated tests into composite.tests

* added test module

* updated readme

* Updated common blocks

* fixed SuppressMessageAttribute

* fixed helper name

* changed PSBoundParameters path

* fixed spelling
  • Loading branch information
athaynes authored Aug 21, 2018
1 parent 3f2a382 commit ef79f64
Show file tree
Hide file tree
Showing 16 changed files with 250 additions and 418 deletions.
65 changes: 23 additions & 42 deletions DSCResources/Browser/Browser.schema.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ Configuration Browser
$BrowserVersion,

[Parameter()]
[ValidateSet('1.13','1.15','1.16')]
[ValidateSet('1.13', '1.15', '1.16')]
[ValidateNotNullOrEmpty()]
[version]
$StigVersion,
Expand All @@ -66,54 +66,35 @@ Configuration Browser
$SkipRuleType
)

#region Add required data to XML
if ( $Exception )
{
$exceptionsObject = [StigException]::ConvertFrom( $Exception )
}
else
{
$exceptionsObject = $null
}

if ( $SkipRule )
{
$skipRuleObject = [SkippedRule]::ConvertFrom( $SkipRule )
}
else
{
$skipRuleObject = $null
}

if ( $SkipRuleType )
{
$skipRuleTypeObject = [SkippedRuleType]::ConvertFrom( $SkipRuleType )
}
else
{
$skipRuleTypeObject = $null
}

if ( $OrgSettings )
{
$orgSettingsObject = Get-OrgSettingsObject -OrgSettings $OrgSettings
}
else
{
$orgSettingsObject = $null
}
#endregion
##### BEGIN DO NOT MODIFY #####
<#
The exception, skipped rule, and organizational settings functionality
is universal across all composites, so the code to process it is in a
central file that is dot sourced into each composite.
#>
$dscResourcesPath = Split-Path -Path $PSScriptRoot -Parent
$userSettingsPath = Join-Path -Path $dscResourcesPath -ChildPath 'stigdata.usersettings.ps1'
. $userSettingsPath
##### END DO NOT MODIFY #####

$technology = [Technology]::Windows
$technologyVersion = [TechnologyVersion]::New( 'All', $technology )
$technologyRole = [TechnologyRole]::New( $BrowserVersion, $technologyVersion )
$StigDataObject = [StigData]::New( $StigVersion, $orgSettingsObject, $technology,
$technologyRole, $technologyVersion, $exceptionsObject,
$skipRuleTypeObject, $skipRuleObject )

$stigDataObject = [StigData]::New( $StigVersion, $OrgSettings, $technology,
$technologyRole, $technologyVersion, $Exception,
$SkipRuleType, $SkipRule )
#### BEGIN DO NOT MODIFY ####
# $StigData is used in the resources that are dot sourced below
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseDeclaredVarsMoreThanAssignments",'')]
$StigData = $StigDataObject.StigXml

# $resourcePath is exported from the helper module in the header

# This is required to process Skipped rules
Import-DscResource -ModuleName PSDesiredStateConfiguration -ModuleVersion 1.1
. "$resourcePath\windows.Script.skip.ps1"
##### END DO NOT MODIFY #####

Import-DscResource -ModuleName PSDesiredStateConfiguration -ModuleVersion 1.1
. "$resourcePath\windows.Registry.ps1"
}
75 changes: 30 additions & 45 deletions DSCResources/DotNetFramework/DotNetFramework.schema.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -69,50 +69,35 @@ Configuration DotNetFramework
$SkipRuleType
)

if ( $Exception )
{
$exceptionsObject = [StigException]::ConvertFrom( $Exception )
}
else
{
$exceptionsObject = $null
}

if ( $SkipRule )
{
$skipRuleObject = [SkippedRule]::ConvertFrom( $SkipRule )
}
else
{
$skipRuleObject = $null
}

if ( $SkipRuleType )
{
$skipRuleTypeObject = [SkippedRuleType]::ConvertFrom( $SkipRuleType )
}
else
{
$skipRuleTypeObject = $null
}

if ( $OrgSettings )
{
$orgSettingsObject = Get-OrgSettingsObject -OrgSettings $OrgSettings
}
else
{
$orgSettingsObject = $null
}

$technology = [Technology]::Windows
$technologyVersion = [TechnologyVersion]::New( "All", $technology )
$technologyRole = [TechnologyRole]::New( $FrameworkVersion, $technologyVersion )
$StigDataObject = [StigData]::New( $StigVersion, $orgSettingsObject, $technology, $technologyRole, $technologyVersion, $exceptionsObject , $skipRuleTypeObject, $skipRuleObject )

##### BEGIN DO NOT MODIFY #####
<#
The exception, skipped rule, and organizational settings functionality
is universal across all composites, so the code to process it is in a
central file that is dot sourced into each composite.
#>
$dscResourcesPath = Split-Path -Path $PSScriptRoot -Parent
$userSettingsPath = Join-Path -Path $dscResourcesPath -ChildPath 'stigdata.usersettings.ps1'
. $userSettingsPath
##### END DO NOT MODIFY #####

$technology = [Technology]::Windows
$technologyVersion = [TechnologyVersion]::New( "All", $technology )
$technologyRole = [TechnologyRole]::New( $FrameworkVersion, $technologyVersion )
$stigDataObject = [StigData]::New( $StigVersion, $orgSettingsObject, $technology,
$technologyRole, $technologyVersion, $Exception,
$SkipRuleType, $SkipRule )
#### BEGIN DO NOT MODIFY ####
# $StigData is used in the resources that are dot sourced below
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseDeclaredVarsMoreThanAssignments",'')]
$StigData = $StigDataObject.StigXml

# $resourcePath is exported from the helper module in the header
Import-DscResource -ModuleName PSDesiredStateConfiguration
. "$resourcePath\windows.Registry.ps1"
}
# $resourcePath is exported from the helper module in the header

# This is required to process Skipped rules
Import-DscResource -ModuleName PSDesiredStateConfiguration -ModuleVersion 1.1
. "$resourcePath\windows.Script.skip.ps1"
##### END DO NOT MODIFY #####

Import-DscResource -ModuleName PSDesiredStateConfiguration
. "$resourcePath\windows.Registry.ps1"
}
38 changes: 38 additions & 0 deletions DSCResources/Resources/SqlServer.ScriptQuery.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.

$rules = Get-RuleClassData -StigData $StigData -Name SqlScriptQueryRule

foreach ($instance in $ServerInstance)
{
if ($null -ne $Database)
{
foreach ($db in $Database)
{
foreach ( $rule in $rules )
{
SqlScriptQuery "$(Get-ResourceTitle -Rule $rule)$instance"
{
ServerInstance = $Instance
GetQuery = $rule.GetScript
TestQuery = $rule.TestScript
SetQuery = $rule.SetScript
Variable = $db
}
}
}
}
else
{
foreach ($rule in $rules)
{
SqlScriptQuery "$(Get-ResourceTitle -Rule $rule)$instance"
{
ServerInstance = $instance
GetQuery = $rule.GetScript
TestQuery = $rule.TestScript
SetQuery = $rule.SetScript
}
}
}
}
4 changes: 2 additions & 2 deletions DSCResources/Resources/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
The file naming convention in this directory indicates the target platform and sub-component DSC resource.
Many of the STIG's define registry settings so it will be reused the most, but all resources are set up and implemented in the same manner.

For example 'windows.registry.ps1' indicates that it contains the DSC resource to manage the registry on the Windows platform.
For example 'Windows.Registry.ps1' indicates that it contains the DSC resource to manage the registry on the Windows platform.

Any composite resource in the PowerStigDsc module can dot-source this file without having to do any additional work.
Any composite resource in the PowerStig module can dot-source this file without having to do any additional work.
85 changes: 26 additions & 59 deletions DSCResources/SqlServer/SqlServer.schema.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -44,22 +44,6 @@ using module ..\..\PowerStig.psm1
.PARAMETER SkipRuleType
All STIG rule IDs of the specified type are collected in an array and passed to the Skip-Rule
function. Each rule follows the same process as the SkipRule parameter.
.EXAMPLE
In this example the 1.16 of the Windows SQLServer2012 Instance STIG is applied to a specific instance
Import-DscResource -ModuleName PowerStigDsc
Node localhost
{
SqlServer BaseLine
{
SqlVersion = Server2012
SqlRole = Instance
StigVersion = '1.16'
ServerInstance = 'ServerX\TestInstance'
}
}
#>
Configuration SqlServer
{
Expand Down Expand Up @@ -113,52 +97,35 @@ Configuration SqlServer
$SkipRuleType
)

if ( $Exception )
{
$exceptionsObject = [StigException]::ConvertFrom( $Exception )
}
else
{
$exceptionsObject = $null
}

if ( $SkipRule )
{
$skipRuleObject = [SkippedRule]::ConvertFrom( $SkipRule )
}
else
{
$skipRuleObject = $null
}

if ( $SkipRuleType )
{
$skipRuleTypeObject = [SkippedRuleType]::ConvertFrom( $SkipRuleType )
}
else
{
$skipRuleTypeObject = $null
}

if ( $OrgSettings )
{
$orgSettingsObject = Get-OrgSettingsObject -OrgSettings $OrgSettings
}
else
{
$orgSettingsObject = $null
}

$technology = [Technology]::SqlServer
##### BEGIN DO NOT MODIFY #####
<#
The exception, skipped rule, and organizational settings functionality
is universal across all composites, so the code to process it is in a
central file that is dot sourced into each composite.
#>
$dscResourcesPath = Split-Path -Path $PSScriptRoot -Parent
$userSettingsPath = Join-Path -Path $dscResourcesPath -ChildPath 'stigdata.usersettings.ps1'
. $userSettingsPath
##### END DO NOT MODIFY #####

$technology = [Technology]::SqlServer
$technologyVersion = [TechnologyVersion]::New( $SqlVersion, $technology )
$technologyRole = [TechnologyRole]::New( $SqlRole, $technologyVersion )
$StigDataObject = [StigData]::New( $StigVersion, $orgSettingsObject, $technology,
$technologyRole, $technologyVersion, $exceptionsObject,
$skipRuleTypeObject, $skipRuleObject )

$technologyRole = [TechnologyRole]::New( $SqlRole, $technologyVersion )
$stigDataObject = [StigData]::New( $StigVersion, $OrgSettings, $technology,
$technologyRole, $technologyVersion, $Exception,
$SkipRuleType, $SkipRule )
#### BEGIN DO NOT MODIFY ####
# $StigData is used in the resources that are dot sourced below
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseDeclaredVarsMoreThanAssignments",'')]
$StigData = $StigDataObject.StigXml

# $resourcePath is exported from the helper module in the header

# This is required to process Skipped rules
Import-DscResource -ModuleName PSDesiredStateConfiguration -ModuleVersion 1.1
. "$resourcePath\windows.Script.skip.ps1"
##### END DO NOT MODIFY #####

Import-DscResource -ModuleName SqlServerDsc -ModuleVersion '11.4.0.0'
. "$resourcePath\windows.SqlScriptQuery.ps1"
. "$resourcePath\SqlServer.ScriptQuery.ps1"
}
Loading

0 comments on commit ef79f64

Please sign in to comment.