Module/Rule.SqlDatabase/Convert/Methods.ps1

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
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
#region Method Functions

# This is required to keep track of the split rules progress during xml creation
$script:databasearray = @()

<#
    .SYNOPSIS
        Retrieves the SqlDatabase name from the check-content element in the xccdf
 
    .PARAMETER CheckContent
        Specifies the check-content element in the xccdf
#>

function Get-DatabaseName
{
    [CmdletBinding()]
    [OutputType([string])]
    param
    (
        [Parameter(Mandatory = $true)]
        [string]
        $CheckContent
    )

    switch ($checkcontent)
    {
        {$PSItem -Match "((?i)Pubs)"}
        {
            if ($databasearray.Contains('Pubs') -eq $false)
            {
                $name = 'Pubs'
                $script:databasearray += $name
                break
            }
        }
        {$PSItem -Match "((?i)Northwind)"}
        {
            if ($databasearray.Contains('Northwind') -eq $false)
            {
                $name = 'Northwind'
                $script:databasearray += $name
                break
            }
        }
        {$PSItem -Match "((?i)AdventureWorks)"}
        {
            if ($databasearray.Contains('AdventureWorks') -eq $false)
            {
                $name = 'AdventureWorks'
                $script:databasearray += $name
                break
            }
        }
        {$PSItem -Match "((?i)WorldwideImporters)"}
        {
            if ($databasearray.Contains('WorldwideImporters') -eq $false)
            {
                $name = 'WorldwideImporters'
                $script:databasearray += $name
                break
            }
        }
    }

    return $name
}

<#
    .SYNOPSIS
        Sets the SqlDatabase ensure status rules from the check-content element in the xccdf
 
    .PARAMETER CheckContent
        Specifies the check-content element in the xccdf
#>

function Set-Ensure
{
    [CmdletBinding()]
    [OutputType([string])]
    param
    (
        [Parameter(Mandatory = $true)]
        [string]
        $CheckContent
    )

    switch ($checkContent)
    {
        {$PSItem -Match 'If this system is identified as production' -or $PSItem -Match 'the existance of the publicly available'}
        {
            $ensure = 'Absent'
        }
    }

    return $ensure
}

<#
    .SYNOPSIS
        Check if the string (MitigationTarget) contains a comma. If so the rule needs to be split
#>


function Test-MultipleSqlDatabase
{
    [CmdletBinding()]
    [OutputType([bool])]
    param
    (
        [Parameter(Mandatory = $true)]
        [psobject]
        $CheckContent
    )

    $matchDatabase = ($checkContent | Select-String -Pattern "(Pubs|Northwind|AdventureWorks|WorldwideImporters)" -AllMatches).Matches.Value

    if ($matchDatabase -gt 1)
    {
        return $true
    }

    return $false
}

function Split-MultipleSqlDatabase
{
    [CmdletBinding()]
    [OutputType([bool])]
    param
    (
        [Parameter(Mandatory = $true)]
        [psobject]
        $CheckContent
    )

    $result = @()
    $matchDatabase = ($checkContent | Select-String -Pattern "(Pubs|Northwind|AdventureWorks|WorldwideImporters)" -AllMatches).Matches.Value

    foreach ($database in $matchDatabase)
    {
        $result += $CheckContent
    }

    return $result
}