Module/Rule.FileContent/Convert/FileContentRule.Convert.psm1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
using module .\..\..\Common\Common.psm1
using module .\..\FileContentRule.psm1
using module .\FileContentType\FileContentType.psm1

$exclude = @($MyInvocation.MyCommand.Name,'Template.*.txt', '*.psm1')
$supportFileList = Get-ChildItem -Path $PSScriptRoot -File -Recurse -Exclude $exclude
foreach ($supportFile in $supportFileList)
{
    Write-Verbose "Loading $($supportFile.FullName)"
    . $supportFile.FullName
}
# Header

<#
    .SYNOPSIS
        Convert the contents of an xccdf check-content element into a fileContent object
    .DESCRIPTION
        The FileContentRule class is used to manage STIGs for applications that utilize a
        configuration file to manage security settings
#>

class FileContentRuleConvert : FileContentRule
{
    <#
        .SYNOPSIS
            Empty constructor for SplitFactory
    #>

    FileContentRuleConvert ()
    {
    }

    <#
        .SYNOPSIS
            Converts a xccdf stig rule element into a File Content Rule
        .PARAMETER XccdfRule
            The STIG rule to convert
    #>

    FileContentRuleConvert ([xml.xmlelement] $XccdfRule) : base ($XccdfRule, $true)
    {
        $this.SetKeyName()
        $this.SetValue()
        if ($this.conversionstatus -eq 'pass')
        {
            $this.SetDuplicateRule()
        }
        $this.SetDscResource()
    }

    #region Methods

    <#
        .SYNOPSIS
            Extracts the key name from the check-content and sets the value
        .DESCRIPTION
            Gets the key name from the xccdf content and sets the
            value. If the key name that is returned is not valid,
            the parser status is set to fail
    #>

    [void] SetKeyName ()
    {
        $thisKeyName = (Get-KeyValuePair $this.SplitCheckContent).Key

        if (-not $this.SetStatus($thisKeyName))
        {
            $this.set_Key($thisKeyName)
        }
    }

    <#
        .SYNOPSIS
            Extracts the key value from the check-content and sets the value
        .DESCRIPTION
            Gets the key value from the xccdf content and sets the
            value. If the key value that is returned is not valid,
            the parser status is set to fail
    #>

    [void] SetValue ()
    {
        $thisValue = (Get-KeyValuePair $this.SplitCheckContent).Value

        if (-not $this.SetStatus($thisValue))
        {
            $this.set_Value($thisValue)
        }
    }

    hidden [void] SetDscResource ()
    {
        if ($null -eq $this.DuplicateOf)
        {
            if ($this.Key -match 'deployment.')
            {
                $this.DscResource = 'KeyValuePairFile'
            }
            else
            {
                $this.DscResource = 'ReplaceText'
            }
        }
        else
        {
            $this.DscResource = 'None'
        }
    }

    static [bool] Match ([string] $CheckContent)
    {
        $result = $false
        switch ( $true )
        {
            {
                (
                    $CheckContent -Match 'app.update.enabled' -and
                    $CheckContent -NotMatch 'Mozilla.cfg' -and
                    $CheckContent -NotMatch 'locked or'
                )
            }
            {
                $result = $false
                break
            }
            {
                (
                    $CheckContent -Match 'deployment.properties' -and
                    $CheckContent -Match '=' -and
                    $CheckContent -NotMatch 'exception.sites'
                ) -or
                (
                    $CheckContent -Match 'about:config' -and
                    $CheckContent -NotMatch 'Mozilla.cfg'
                )
            }
            {
                $result = $true
                break
            }
            default
            {
                $result = $false
                break
            }
        }
        return $result
    }

    <#
        .SYNOPSIS
            Tests if a rules contains more than one check
        .DESCRIPTION
            Gets the policy setting in the rule from the xccdf content and then
            checks for the existance of multiple entries.
        .PARAMETER CheckContent
            The rule text from the check-content element in the xccdf
    #>

    static [bool] HasMultipleRules ([string] $CheckContent)
    {
        $keyValuePairs = Get-KeyValuePair -CheckContent ([FileContentRule]::SplitCheckContent($CheckContent))
        return (Test-MultipleFileContentRule -KeyValuePair $keyValuePairs)
    }

    <#
        .SYNOPSIS
            Splits the CheckContent into multiple CheckContent strings
        .DESCRIPTION
            When CheckContent is identified as containing multiple rules
            this method will break the CheckContent out into multiple
            CheckContent strings that contain single rules.
        .PARAMETER CheckContent
            The rule text from the check-content element in the xccdf
    #>

    static [string[]] SplitMultipleRules ([string] $CheckContent)
    {
        return (Get-KeyValuePair -SplitCheckContent -CheckContent ([FileContentRule]::SplitCheckContent($CheckContent)))
    }
}