AlertMonitoring/OMS.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
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
Set-StrictMode -Version Latest
function Set-AzSKOMSSettings
{
    <#
    .SYNOPSIS
    This command would help in updating the OMS configuration settings under the current powershell session.
    .DESCRIPTION
    This command will update the OMS Settings under the current powershell session. This also remembers the current settings and use them in the subsequent sessions.
     
    .PARAMETER OMSWorkspaceID
        Workspace ID of your OMS instance. AzSK data would be pushed to this instance.
    .PARAMETER OMSSharedKey
        Shared key of your OMS instance.
    .PARAMETER AltOMSWorkspaceID
        Alternate workspaceId of your OMS instance. AzSK data would be pushed to this instance.
    .PARAMETER AltOMSSharedKey
        Workspace shared key of your alternate OMS instance.
    .PARAMETER Source
        Provide the source of OMS Events. (e. g. CA,CICD,SDL)
    .PARAMETER Disable
        Use -Disable option to clean the OMS setting under the current instance.
 
    .LINK
    https://aka.ms/azskossdocs
 
    #>

    param(
        
        [Parameter(Mandatory = $false, HelpMessage="Workspace ID of your OMS instance. AzSK data would be pushed to this instance.", ParameterSetName = "Setup")]
        [AllowEmptyString()]
        [string]
        $OMSWorkspaceID,

        [Parameter(Mandatory = $false, HelpMessage="Shared key of your OMS instance.", ParameterSetName = "Setup")]
        [AllowEmptyString()]
        [string]
        $OMSSharedKey,

        [Parameter(Mandatory = $false, HelpMessage="Alternate Workspace ID of your OMS instance. AzSK data would be pushed to this instance.", ParameterSetName = "Setup")]
        [AllowEmptyString()]
        [string]
        $AltOMSWorkspaceID,

        [Parameter(Mandatory = $false, HelpMessage="Shared key of your alternate OMS instance.", ParameterSetName = "Setup")]
        [AllowEmptyString()]
        [string]
        $AltOMSSharedKey,

        [Parameter(Mandatory = $false, HelpMessage="Provide the source of OMS Events.(e.g. CC,CICD,SDL)", ParameterSetName = "Setup")]
        [AllowEmptyString()]
        [string]
        $Source,

        [Parameter(Mandatory = $true, HelpMessage="Use -Disable option to clean the OMS setting under the current instance.", ParameterSetName = "Disable")]
        [switch]
        $Disable

    )
    Begin
    {
        [CommandHelper]::BeginCommand($PSCmdlet.MyInvocation);
        [ListenerHelper]::RegisterListeners();
    }
    Process
    {
        try
        {
            $appSettings = [ConfigurationManager]::GetLocalAzSKSettings();
            if(-not $Disable) 
            {
                if(-not [string]::IsNullOrWhiteSpace($OMSWorkspaceID) -and -not [string]::IsNullOrWhiteSpace($OMSSharedKey))
                {
                    $appSettings.OMSWorkspaceId = $OMSWorkspaceID
                    $appSettings.OMSSharedKey = $OMSSharedKey
                }
                elseif(([string]::IsNullOrWhiteSpace($OMSWorkspaceID) -and -not [string]::IsNullOrWhiteSpace($OMSSharedKey)) `
                        -and (-not [string]::IsNullOrWhiteSpace($OMSWorkspaceID) -and [string]::IsNullOrWhiteSpace($OMSSharedKey)))
                {                    
                    [EventBase]::PublishGenericCustomMessage("You need to send both the OMSWorkspaceId and OMSSharedKey", [MessageType]::Error);
                    return;
                }
                if(-not [string]::IsNullOrWhiteSpace($AltOMSWorkspaceID) -and -not [string]::IsNullOrWhiteSpace($AltOMSSharedKey))
                {
                    $appSettings.AltOMSWorkspaceId = $AltOMSWorkspaceID
                    $appSettings.AltOMSSharedKey = $AltOMSSharedKey
                }
                elseif(([string]::IsNullOrWhiteSpace($AltOMSWorkspaceID) -and -not [string]::IsNullOrWhiteSpace($AltOMSSharedKey)) `
                        -and (-not [string]::IsNullOrWhiteSpace($AltOMSWorkspaceID) -and [string]::IsNullOrWhiteSpace($AltOMSSharedKey)))
                {                    
                    [EventBase]::PublishGenericCustomMessage("You need to send both the AltOMSWorkspaceId and AltOMSSharedKey", [MessageType]::Error);
                    return;
                }
            }
            else {
                $appSettings.OMSWorkspaceId = ""
                $appSettings.OMSSharedKey = ""
                $appSettings.AltOMSWorkspaceId = ""
                $appSettings.AltOMSSharedKey = ""
            }
            if(-not [string]::IsNullOrWhiteSpace($Source))
            {                
                $appSettings.OMSSource = $Source
            }
            else
            {
                $appSettings.OMSSource = "SDL"
            }
            $appSettings.OMSType = "AzSK"
            [ConfigurationManager]::UpdateAzSKSettings($appSettings);
            [EventBase]::PublishGenericCustomMessage([Constants]::SingleDashLine + "`r`nWe have added new queries for the OMS solution. These will help reflect the aggregate control pass/fail status more accurately. Please go here to get them: https://aka.ms/azsk/omsqueries `r`n",[MessageType]::Warning);
            [EventBase]::PublishGenericCustomMessage("Successfully changed policy settings");
        }
        catch
        {
            [EventBase]::PublishGenericException($_);
        }
    }
    End
    {
        [ListenerHelper]::UnregisterListeners();
    }
}

function Install-AzSKOMSSolution
{
    <#
 
    .SYNOPSIS
    This command would help in creating security dashboard for applications
 
    .DESCRIPTION
    This command would help in creating security dashboard for applications
 
    .PARAMETER OMSSubscriptionId
        Id of subscription hosting OMS workspace
    .PARAMETER OMSResourceGroup
        Resource group hosting OMS workspace
    .PARAMETER OMSWorkspaceId
        Provide the OMS workspaceId
    .PARAMETER ViewName
        Provide the custom name for your devopskit security view
    .PARAMETER ValidateOnly
        Provide this debug switch to validate the deployment. It is a predeployment check which validates all the provided params
 
    .EXAMPLE
     
    .NOTES
    This command helps the application team to check compliance of Azure Subscriptions with the AzSK security guidance.
 
    .LINK
    https://aka.ms/azskossdocs
 
    #>

    param(
        [Parameter(ParameterSetName="NewModel", Mandatory = $true)]
        [string]
        [ValidateNotNullOrEmpty()]
        $OMSSubscriptionId,  
                
        [Parameter(ParameterSetName="NewModel", Mandatory = $true)]
        [string]
        [ValidateNotNullOrEmpty()]
        $OMSResourceGroup, 

        [Parameter(ParameterSetName="NewModel", Mandatory = $true)]
        [string]
        [ValidateNotNullOrEmpty()]
        $OMSWorkspaceId, 
        
        [Parameter(ParameterSetName="NewModel", Mandatory = $false)]
        [string]
        $ViewName = "SecurityCompliance", 
                        
        [switch]
        [Parameter(Mandatory = $False)]
        $ValidateOnly
    )
    Begin
    {
        [CommandHelper]::BeginCommand($PSCmdlet.MyInvocation);
        [ListenerHelper]::RegisterListeners();
    }
    Process
    {
        try
        {
            $OMSMonitoringInstance = [OMSMonitoring]::new($OMSSubscriptionId, $OMSResourceGroup, $OMSWorkspaceId, $PSCmdlet.MyInvocation);
            $OMSMonitoringInstance.InvokeFunction($OMSMonitoringInstance.ConfigureOMS, @($ViewName, $ValidateOnly));
        }
        catch
        {
            [EventBase]::PublishGenericException($_);
        }
    }
    End
    {
        [ListenerHelper]::UnregisterListeners();
    }
}