AzSK.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
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
Set-StrictMode -Version Latest
Write-Host "Importing Az modules. This may take a while..." -ForegroundColor Yellow
Import-Module Az.Accounts -RequiredVersion 1.2.1 -WarningAction SilentlyContinue
Enable-AzureRMAlias

. $PSScriptRoot\Framework\Framework.ps1

@("$PSScriptRoot\SVT", "$PSScriptRoot\AlertMonitoring", "$PSScriptRoot\SubscriptionSecurity", "$PSScriptRoot\ContinuousAssurance" , "$PSScriptRoot\AzSKInfo", "$PSScriptRoot\PolicySetup", "$PSScriptRoot\ARMChecker","$PSScriptRoot\PIM") |
    ForEach-Object {
    (Get-ChildItem -Path $_ -Recurse -File -Include "*.ps1") |
        ForEach-Object {
        . $_.FullName
    }
}
#create aliases
function Get-AzSKAccessToken {
    <#
    .SYNOPSIS
    This command would help in generating the access token from the current login context for the specified resource URI.
    .DESCRIPTION
    This command would help in generating the access token from the current login context for the specified resource URI.
 
    .PARAMETER ResourceAppIdURI
        Provide the Resource App ID URI
    .PARAMETER TenantId
        Current logged in user tenant id.
 
    .LINK
    https://aka.ms/azskossdocs
 
    #>

    Param(
        [Parameter(Mandatory = $true, HelpMessage = "Provide the Resource App ID URI")]
        [string]
        [Alias("rau")]
        $ResourceAppIdURI,

        [Parameter(Mandatory = $false, HelpMessage = "Current logged in user tenant id.")]
        [string]
        [Alias("ti")]
        $TenantId
    )
    Begin 
    {
        [CommandHelper]::BeginCommand($MyInvocation);
    }
    Process 
    {
        try 
        {
            [Helpers]::GetAccessToken($ResourceAppIdURI, $TenantId);        
        }
        catch 
        {
            [EventBase]::PublishGenericException($_);
        }
    } 
}

function Get-AzSKSupportedResourceTypes {
    <#
    .SYNOPSIS
    This command would list all the resource types of Azure Services which are currently supported by AzSK
    .DESCRIPTION
    This command would list all the resource types of Azure Services which are currently supported by AzSK
 
    .LINK
    https://aka.ms/azskossdocs
 
    #>

    Param()
    Begin 
    {
        [CommandHelper]::BeginCommand($MyInvocation);
    }
    Process 
    {
        try 
        {
            [SVTMapping]::Mapping | Select-Object -Property ResourceTypeName, ResourceType | Sort-Object -Property ResourceTypeName;
        }
        catch 
        {
            [EventBase]::PublishGenericException($_);
        }
    }
}

function Set-AzSKPolicySettings {
    <#
    .SYNOPSIS
    This command would help to set online policy store URL.
    .DESCRIPTION
    This command would help to set online policy store URL.
 
    .PARAMETER OnlinePolicyStoreURL
        Provide the online policy URL
    .PARAMETER DisableOnlinePolicy
        Flag to disable online policy.
    .PARAMETER EnableAADAuthForOnlinePolicyStore
        Enables AAD authentication for online policy store.
    .PARAMETER EnableOnlinePolicy
        Provide the flag to enable online policy
    .PARAMETER AutoUpdateCommand
            Provide org install URL
    .PARAMETER AutoUpdate
            Toggle the auto-update feature
     
    .LINK
    https://aka.ms/azskossdocs
 
    #>

    Param(
        [Parameter(Mandatory = $false, HelpMessage = "Provide the Online Policy Store URI")]
        [Alias("LocalOrgPolicyFolderPath")]
        [string]
        [Alias("opu")]
        $OnlinePolicyStoreUrl,

        [Parameter(Mandatory = $false, HelpMessage = "Provide the flag to enable online policy")]
        [switch]
        [Alias("eop")]
        $EnableOnlinePolicy,

        [Parameter(Mandatory = $false, HelpMessage = "Provide your Azure Environment")]
        [string]
        [Alias("ae")]
        $AzureEnvironment,

        [Parameter(Mandatory = $false, HelpMessage = "Provide the flag to disable online policy")]
        [switch]
        [Alias("dop")]
        $DisableOnlinePolicy,

        [Parameter(Mandatory = $false, HelpMessage = "Provide the flag to enable auth for online policy")]
        [switch]
        [Alias("eaop")]
        $EnableAADAuthForOnlinePolicyStore,

        [Parameter(Mandatory = $false, HelpMessage = "Provide org install URL")]
        [string]
        [Alias("auc")]
        $AutoUpdateCommand,

        [Parameter(Mandatory = $false, ParameterSetName = "AutoUpdatePolicy", HelpMessage = "Toggle the auto-update feature")]
        [ValidateSet("On", "Off", "NotSet")]
        [Alias("au")]
        $AutoUpdate,

        [Parameter(Mandatory = $true, ParameterSetName = "CACentralMode")]
        [switch]
        $EnableCentralScanMode
    )
    Begin {
        [CommandHelper]::BeginCommand($PSCmdlet.MyInvocation);
        [ListenerHelper]::RegisterListeners();
    }
    Process {
        try {

            $azskSettings = [ConfigurationManager]::GetLocalAzSKSettings();
            if (-not [string]::IsNullOrWhiteSpace($OnlinePolicyStoreUrl)) {
                try {
                    $url = [System.Net.WebRequest]::Create($OnlinePolicyStoreUrl)
                }
                catch {
                    [EventBase]::PublishGenericCustomMessage("Enter valid URL : $OnlinePolicyStoreUrl", [MessageType]::Error);
                    return
                }

                $azskSettings.OnlinePolicyStoreUrl = $OnlinePolicyStoreUrl

                if ($EnableAADAuthForOnlinePolicyStore) {
                    $azskSettings.EnableAADAuthForOnlinePolicyStore = $true
                }
                else {
                    $azskSettings.EnableAADAuthForOnlinePolicyStore = $false
                }
            }

            if ($EnableAADAuthForOnlinePolicyStore) {
                $azskSettings.EnableAADAuthForOnlinePolicyStore = $true
            }

            if ($DisableOnlinePolicy) {
                $azskSettings.UseOnlinePolicyStore = $false
            }

            if ($EnableOnlinePolicy) {
                $azskSettings.UseOnlinePolicyStore = $true
            }

            if (-not [string]::IsNullOrWhiteSpace($AutoUpdateCommand)) {
                $azskSettings.AutoUpdateCommand = $AutoUpdateCommand;
            }

            if ($AutoUpdate) {
                $azskSettings.AutoUpdateSwitch = $AutoUpdate
            }
            if($EnableCentralScanMode)
            {
                 $azskSettings.IsCentralScanModeOn = $EnableCentralScanMode
            }
            if ($AzureEnvironment) {
                $azskSettings.AzureEnvironment = $AzureEnvironment
            }
            else {
                $azskSettings.AzureEnvironment = [Constants]::DefaultAzureEnvironment
            }
            [ConfigurationManager]::UpdateAzSKSettings($azskSettings);            
            [EventBase]::PublishGenericCustomMessage("Successfully configured policy settings. `nStart a fresh PS console/session to ensure any policy updates are (re-)loaded.", [MessageType]::Warning);
        }
        catch {
            [EventBase]::PublishGenericException($_);
        }
    }
    End {
        [ListenerHelper]::UnregisterListeners();
    }
}

function Set-AzSKLocalAIOrgTelemetrySettings {
    <#
    .SYNOPSIS
    This command would help to set local control telemetry settings.
    .DESCRIPTION
    This command would help to set local control telemetry settings.
 
    .PARAMETER LocalAIOrgTelemetryKey
        Provide local telemetry key.
    .PARAMETER EnableLocalAIOrgTelemetry
        Enables local control telemetry.
    .LINK
    https://aka.ms/azskossdocs
 
    #>

    Param(
        [Parameter(Mandatory = $true, HelpMessage = "Provide the local control telemetry key")]
        [string]
        [Alias("lotk")]
        $LocalAIOrgTelemetryKey,

        [Parameter(Mandatory = $true, HelpMessage = "Provide the flag to enable local control telemetry")]
        [bool]
        [Alias("elot")]
        $EnableLocalAIOrgTelemetry
    )
    Begin {
        [CommandHelper]::BeginCommand($PSCmdlet.MyInvocation);
        [ListenerHelper]::RegisterListeners();
    }
    Process {
        try { 
            $azskSettings = [ConfigurationManager]::GetLocalAzSKSettings();
            $azskSettings.LocalControlTelemetryKey = $LocalAIOrgTelemetryKey
            $azskSettings.LocalEnableControlTelemetry = $EnableLocalAIOrgTelemetry
            [ConfigurationManager]::UpdateAzSKSettings($azskSettings);
            [EventBase]::PublishGenericCustomMessage("Successfully set control telemetry settings");
        }
        catch {
            [EventBase]::PublishGenericException($_);
        }
    }
    End {
        [ListenerHelper]::UnregisterListeners();
    }
}

function Set-AzSKUsageTelemetryLevel {
    <#
    .SYNOPSIS
    This command would help to set telemetry level.
    .DESCRIPTION
    This command would help to set telemetry level.
 
    .PARAMETER Level
        Provide the telemetry level
    .LINK
    https://aka.ms/azskossdocs
 
    #>

    Param(
        [Parameter(Mandatory = $true, HelpMessage = "Provide the telemetry level")]
        [ValidateSet("None", "Anonymous")]
        [string]
        [Alias("lvl")]
        $Level
    )
    Begin {
        [CommandHelper]::BeginCommand($PSCmdlet.MyInvocation);
        [ListenerHelper]::RegisterListeners();
    }
    Process {
        try {
            $azskSettings = [ConfigurationManager]::GetLocalAzSKSettings();
            $azskSettings.UsageTelemetryLevel = $Level
            [ConfigurationManager]::UpdateAzSKSettings($azskSettings);
            [EventBase]::PublishGenericCustomMessage("Successfully set usage telemetry level");
        }
        catch {
            [EventBase]::PublishGenericException($_);
        }
    }
    End {
        [ListenerHelper]::UnregisterListeners();
    }
}

function Set-AzSKUserPreference {
    <#
    .SYNOPSIS
    This command would help to set user preferences for AzSK.
    .DESCRIPTION
    This command would help to set user preferences for AzSK.
 
    .PARAMETER OutputFolderPath
    Provide the custom folder path for output files generated from AzSK
    .PARAMETER ResetOutputFolderPath
    Reset the output folder path to default value
    .PARAMETER DoNotOpenOutputFolder
    Switch to specify whether to open output folder.
    .LINK
    https://aka.ms/azskossdocs
 
    #>

    Param
    (
        [Parameter(Mandatory = $true, ParameterSetName = "Set OutputFolderPath", HelpMessage = "Provide the custom folder path for output files generated from AzSK")]
        [string]
        [Alias("ofp")]
        $OutputFolderPath,

        [Parameter(Mandatory = $true, ParameterSetName = "Reset OutputFolderPath", HelpMessage = "Reset the output folder path to default value")]
        [switch]
        [Alias("rofp")]
        $ResetOutputFolderPath,

        [switch]
        [Parameter(Mandatory = $false, HelpMessage = "Switch to specify whether to open output folder.")]
        [Alias("dnof")]
        $DoNotOpenOutputFolder,

        [switch]
        [Parameter(Mandatory = $true, ParameterSetName = "EnableComplianceStorage", HelpMessage = "Switch to enable storage of compliance report data at subscription.")]
        [Alias("scus")]
        $StoreComplianceSummaryInUserSubscriptions,

        [switch]
        [Parameter(Mandatory = $false, ParameterSetName = "DisableComplianceStorage", HelpMessage = "Switch to disable storage of compliance report data at subscription.")]
        [Alias("dcsus")]
        $DisableComplianceSummaryStorageInUserSubscriptions
    )
    Begin {
        [CommandHelper]::BeginCommand($PSCmdlet.MyInvocation);
        [ListenerHelper]::RegisterListeners();
    }
    Process {
        try {
            $azskSettings = [ConfigurationManager]::GetLocalAzSKSettings();
            if ($ResetOutputFolderPath) {
                
                $azskSettings.OutputFolderPath = "";
                [EventBase]::PublishGenericCustomMessage("Output folder path has been reset successfully");
            }
            elseif (-not [string]::IsNullOrWhiteSpace($OutputFolderPath)) {
                if (Test-Path -Path $OutputFolderPath) {                    
                    $azskSettings.OutputFolderPath = $OutputFolderPath;
                    [EventBase]::PublishGenericCustomMessage("Output folder path has been changed successfully");
                }
                else {
                    [EventBase]::PublishGenericCustomMessage("The specified path does not exist", [MessageType]::Error);
                }
            }
            
            if($StoreComplianceSummaryInUserSubscriptions)
            {
                $azskSettings.StoreComplianceSummaryInUserSubscriptions = $true;
            }
            if($DisableComplianceSummaryStorageInUserSubscriptions)
            {
                $azskSettings.StoreComplianceSummaryInUserSubscriptions = $false;
            }
            
            [ConfigurationManager]::UpdateAzSKSettings($azskSettings);
            [EventBase]::PublishGenericCustomMessage("Successfully set user preference");
        }
        catch {
            [EventBase]::PublishGenericException($_);
        }
    }
    End {
        [ListenerHelper]::UnregisterListeners();
    }
}

function Send-AzSKInternalData {
    <#
    .SYNOPSIS
    This command is for internal use. Not recommended to run until advised by support team.
    .DESCRIPTION
    This command is for internal use. Not recommended to run until advised by support team.
 
    .PARAMETER SubscriptionId
        Subscription id for which the data to be sent.
 
    .LINK
    https://aka.ms/azskossdocs
 
    #>

    Param
    (
        [string]
        [Parameter(Mandatory = $true, HelpMessage="Subscription id for which the data to be sent.")]
        [ValidateNotNullOrEmpty()]
        [Alias("sid","s")]
        $SubscriptionId
    )
    Begin {
        [CommandHelper]::BeginCommand($PSCmdlet.MyInvocation);
        [ListenerHelper]::RegisterListeners();
    }
    Process {
        try {
            $resolver = [SVTResourceResolver]::new($SubscriptionId);
            $secStatus = [ServicesSecurityStatus]::new($SubscriptionId, $PSCmdlet.MyInvocation, $resolver);
            if ($secStatus) {                
            return $secStatus.InvokeFunction($secStatus.ComputeApplicableControls)               
            }
        }
        catch {
            [EventBase]::PublishGenericException($_);
        }
    }
    End {
        [ListenerHelper]::UnregisterListeners();
    }
}

function Set-AzSKPrivacyNoticeResponse {
    <#
    .SYNOPSIS
    This command would help to set user preferences for EULA and Privacy.
    .DESCRIPTION
    This command would help to set user preferences for EULA and Privacy.
 
    .PARAMETER AcceptPrivacyNotice
        Provide the flag to suppress the Privacy notice prompt and submit the acceptance. (Yes/No)
 
    .LINK
    https://aka.ms/azskossdocs
 
    #>

    Param
    (
        [Parameter(Mandatory = $true, HelpMessage = "Provide the flag to suppress the Privacy notice prompt and submit the acceptance. (Yes/No)")]
        [string]
        [ValidateSet("Yes", "No")]
        [Alias("apn")]
        $AcceptPrivacyNotice
    )
    Begin {
        [CommandHelper]::BeginCommand($PSCmdlet.MyInvocation);
        [ListenerHelper]::RegisterListeners();
    }
    Process {
        try {
            $azskSettings = [ConfigurationManager]::GetLocalAzSKSettings();

            if ($AcceptPrivacyNotice -eq "yes") {
                $azskSettings.PrivacyNoticeAccepted = $true
                $azskSettings.UsageTelemetryLevel = "Anonymous"
            }

            if ($AcceptPrivacyNotice -eq "no") {
                $azskSettings.PrivacyNoticeAccepted = $false
                $azskSettings.UsageTelemetryLevel = "None"
            }
            [ConfigurationManager]::UpdateAzSKSettings($azskSettings)
            [EventBase]::PublishGenericCustomMessage("Successfully updated privacy settings.");
        }
        catch {
            [EventBase]::PublishGenericException($_);
        }

    }
    End {
        [ListenerHelper]::UnregisterListeners();
    }
}

function Clear-AzSKSessionState {

    Write-Host "Clearing AzSK session state..." -ForegroundColor Yellow
    [ConfigOverride]::ClearConfigInstance()
    Write-Host "Session state cleared." -ForegroundColor Yellow

}


$FrameworkPath = $PSScriptRoot

. $FrameworkPath\Framework\Helpers\AliasHelper.ps1