AlertMonitoring/LogAnalytics.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
Set-StrictMode -Version Latest
function Set-AzSKMonitoringSettings
{
    <#
    .SYNOPSIS
    This command would help in updating the Log Analytics configuration settings under the current powershell session.
    .DESCRIPTION
    This command will update the Log Analytics settings under the current powershell session. This also remembers the current settings and use them in the subsequent sessions.
     
    .PARAMETER LAWSId
        Workspace ID of your Log Analytics instance. Control scan results get pushed to this instance.
    .PARAMETER LAWSSharedKey
        Shared key of your Log Analytics instance.
    .PARAMETER AltLAWSId
        Workspace ID of your alternate Log Analytics instance. Control scan results get pushed to this instance.
    .PARAMETER AltLAWSSharedKey
        Workspace shared key of your alternate Log Analytics instance.
    .PARAMETER Source
        Provide the source of Log Analytics Events. (e. g. CA,CICD,SDL)
    .PARAMETER Disable
        Use -Disable option to clean the Log Analytics setting under the current instance.
 
    .LINK
    https://aka.ms/azskossdocs
 
    #>

    [Alias("Set-AzSKOMSSettings")]
    param(
        
        [Parameter(Mandatory = $false, HelpMessage="Workspace ID of your Log Analytics instance. Control scan results get pushed to this instance.", ParameterSetName = "Setup")]
        [AllowEmptyString()]
        [string]
        [Alias("wid","OMSWorkspaceID","WorkspaceId")]
        $LAWSId,

        [Parameter(Mandatory = $false, HelpMessage="Shared key of your Log Analytics instance.", ParameterSetName = "Setup")]
        [AllowEmptyString()]
        [string]
        [Alias("wkey","OMSSharedKey","SharedKey")]
        $LAWSSharedKey,

        [Parameter(Mandatory = $false, HelpMessage="Workspace ID of your alternate Log Analytics instance. Control scan results get pushed to this instance.", ParameterSetName = "Setup")]
        [AllowEmptyString()]
        [string]
        [Alias("awid","AltOMSWorkspaceID","AltWorkspaceId")]
        $AltLAWSId,

        [Parameter(Mandatory = $false, HelpMessage="Shared key of your alternate Log Analytics instance.", ParameterSetName = "Setup")]
        [AllowEmptyString()]
        [string]
        [Alias("awkey","AltOMSSharedKey","AltSharedKey")]
        $AltLAWSSharedKey,

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

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

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

function Install-AzSKMonitoringSolution
{
    <#
 
    .SYNOPSIS
    This command would help in creating security dashboard in Log Analytics Workspace
 
    .DESCRIPTION
    This command would help in creating security dashboard in Log Analytics Workspace
 
    .PARAMETER LAWSSubscriptionId
        Id of subscription hosting Log Analytics workspace
    .PARAMETER LAWSResourceGroup
        Resource group hosting Log Analytics workspace
    .PARAMETER LAWSId
        Workspace ID of the Log Analytics workspace which will be used for monitoring.
    .PARAMETER ViewName
        Provide the custom name for your DevOps Kit security view.
    .PARAMETER ValidateOnly
        Provide this debug switch to validate the deployment. It is a predeployment check which validates all the provided params.
    .PARAMETER DoNotOpenOutputFolder
        Switch to specify whether to open output folder.
    .EXAMPLE
     
    .NOTES
    This command helps the application team to check compliance of Azure Subscriptions with the AzSK security guidance.
 
    .LINK
    https://aka.ms/azskossdocs
 
    #>

    [Alias("Install-AzSKOMSSolution")]
    param(
        [Parameter(ParameterSetName="NewModel", HelpMessage="Id of subscription hosting Log Analytics workspace", Mandatory = $true)]
        [string]
        [ValidateNotNullOrEmpty()]
        [Alias("lawssubid","lawssid","OMSSubscriptionId")]
        $LAWSSubscriptionId,  
                
        [Parameter(ParameterSetName="NewModel", HelpMessage="Resource group hosting Log Analytics workspace", Mandatory = $true)]
        [string]
        [ValidateNotNullOrEmpty()]
        [Alias("lawsrg","OMSResourceGroup")]
        $LAWSResourceGroup, 

        [Parameter(ParameterSetName="NewModel", HelpMessage="Workspace ID of the Log Analytics workspace which will be used for monitoring.", Mandatory = $true)]
        [string]
        [Alias("wid","OMSWorkspaceId","WorkspaceId")]
        [ValidateNotNullOrEmpty()]
        $LAWSId, 
        
        [Parameter(ParameterSetName="NewModel", HelpMessage="Provide the custom name for your DevOps Kit security view", Mandatory = $false)]
        [string]
        [Alias("vname")]
        $ViewName = "SecurityCompliance", 
                        
        [switch]
        [Alias("vonly")]
        [Parameter(Mandatory = $False, HelpMessage="Provide this debug switch to validate the deployment. It is a predeployment check which validates all the provided params.")]
        $ValidateOnly,
        
        [switch]
        [Alias("dnof")]
        [Parameter(Mandatory = $false, HelpMessage = "Switch to specify whether to open output folder.")]
        $DoNotOpenOutputFolder
    )
    Begin
    {
        [CommandHelper]::BeginCommand($PSCmdlet.MyInvocation);
        [AzListenerHelper]::RegisterListeners();
    }
    Process
    {
        try
        {
            if($PSCmdlet.MyInvocation.InvocationName.ToUpper().Equals("INSTALL-AZSKOMSSOLUTION"))
            {
                Write-Host "WARNING: The command 'Install-AzSKOMSSolution' will soon be deprecated. It will be replaced by 'Install-AzSKMonitoringSolution'.`n" -ForegroundColor Yellow
            }
            $monitoringInstance = [LogAnalyticsMonitoring]::new($LAWSSubscriptionId, $LAWSResourceGroup, $LAWSId, $PSCmdlet.MyInvocation);
            $monitoringInstance.InvokeFunction($monitoringInstance.ConfigureLAWS, @($ViewName, $ValidateOnly));
        }
        catch
        {
            [EventBase]::PublishGenericException($_);
        }
    }
    End
    {
        [AzListenerHelper]::UnregisterListeners();
    }
}