Framework/Core/SVT/SVTStatusReport.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
Set-StrictMode -Version Latest 
class SVTStatusReport : AzSVTCommandBase
{
    [SVTResourceResolver] $ServicesResolver = $null;

    SVTStatusReport([string] $subscriptionId, [InvocationInfo] $invocationContext, [SVTResourceResolver] $resolver): 
        Base($subscriptionId, $invocationContext)
    { 
        if(-not $resolver)
        {
            throw [System.ArgumentException] ("The argument 'resolver' is null");
        }

        $this.ServicesResolver = $resolver;
        $this.ServicesResolver.LoadAzureResources();
    }

    hidden [SVTEventContext[]] RunAllControls()
    {
        [SVTEventContext[]] $result = @();    
        
        # Run all Subscription security controls
        try 
        {
            $this.PublishCustomMessage(" `r`n" + [Constants]::DoubleDashLine + "`r`nStarted Subscription security controls`r`n" + [Constants]::DoubleDashLine);
            $sscore = [SubscriptionSecurityStatus]::new($this.SubscriptionContext.SubscriptionId, $this.InvocationContext);
            if ($sscore) 
            {
                # Just copy all the tags without validation. Validation will be done internally
                $sscore.FilterTags = $this.FilterTags;
                $sscore.ExcludeTags = $this.ExcludeTags;
                $sscore.ControlIdString = $this.ControlIdString;
                $sscore.ExcludeControlIdString = $this.ExcludeControlIdString;
                $sscore.GenerateFixScript = $this.GenerateFixScript;
                $sscore.AttestationOptions = $this.AttestationOptions;
                $sscore.Severity = $this.Severity;

                $result += $sscore.RunAllControls();
                $this.PublishCustomMessage([Constants]::DoubleDashLine + "`r`nCompleted Subscription security controls`r`n" + [Constants]::DoubleDashLine, [MessageType]::Update);
            } 
        }
        catch 
        {
            $this.CommandError($_);
        }   

        # Run all Azure services security controls
        try 
        {
            $this.PublishCustomMessage(" `r`n" + [Constants]::DoubleDashLine + "`r`nStarted Azure services security controls`r`n" + [Constants]::DoubleDashLine);            
            $secStatus = [ServicesSecurityStatus]::new($this.SubscriptionContext.SubscriptionId, $this.InvocationContext, $this.ServicesResolver);
            
            if ($secStatus) 
            {
                # Just copy all the tags without validation. Validation will be done internally
                $secStatus.FilterTags = $this.FilterTags;
                $secStatus.ExcludeTags = $this.ExcludeTags;
                $secStatus.ControlIdString = $this.ControlIdString;
                $secStatus.ExcludeControlIdString = $this.ExcludeControlIdString;
                $secStatus.GenerateFixScript = $this.GenerateFixScript;
                $secStatus.AttestationOptions = $this.AttestationOptions;
                $secStatus.Severity = $this.Severity;

                $result += $secStatus.RunAllControls();
                $this.PublishCustomMessage([Constants]::DoubleDashLine + "`r`nCompleted Azure services security controls`r`n" + [Constants]::DoubleDashLine, [MessageType]::Update);
            }   
        }
        catch 
        {
            $this.CommandError($_);
        }
        
        return $result;
    }

    hidden [SVTEventContext[]] FetchAttestationInfo()
    {
        [SVTEventContext[]] $result = @();    
        
        # Fetch state of all Subscription security controls
        try 
        {
            $this.PublishCustomMessage(" `r`n" + [Constants]::DoubleDashLine + "`r`nGetting attestation info for Subscription level controls`r`n" + [Constants]::DoubleDashLine);
            $sscore = [SubscriptionSecurityStatus]::new($this.SubscriptionContext.SubscriptionId, $this.InvocationContext);
            if ($sscore) 
            {
                # Just copy all the tags without validation. Validation will be done internally
                $sscore.ControlIdString = $this.ControlIdString;
                $sscore.AttestationOptions = $this.AttestationOptions;
                $result += $sscore.FetchAttestationInfo();
                if(($result|Measure-object).count -gt 0)
                {
                    $this.PublishCustomMessage([Constants]::DoubleDashLine + "`r`nCompleted Subscription level controls`r`n" + [Constants]::DoubleDashLine, [MessageType]::Update);
                }
                elseif([string]::IsNullOrWhiteSpace($sscore.ControlIdString))
                {
                    $this.PublishCustomMessage([Constants]::SingleDashLine + "`r`nNo attestation data found for Subscription level controls`r`n" + [Constants]::DoubleDashLine, [MessageType]::Update)
                }
            } 
        }
        catch 
        {
            $this.CommandError($_);
        }   

        # Fetch state of all Azure services security controls
        try 
        {
            $this.PublishCustomMessage(" `r`n" + [Constants]::DoubleDashLine + "`r`nGetting attestation info for Azure services controls`r`n" + [Constants]::DoubleDashLine);            
            $secStatus = [ServicesSecurityStatus]::new($this.SubscriptionContext.SubscriptionId, $this.InvocationContext, $this.ServicesResolver);
            
            if ($secStatus) 
            {
                # Just copy all the tags without validation. Validation will be done internally
            
                $secStatus.ControlIdString = $this.ControlIdString;
                #$secStatus.GenerateFixScript = $this.GenerateFixScript;
                $secStatus.AttestationOptions = $this.AttestationOptions;        
                $secStatusResult = $secStatus.FetchAttestationInfo()
                if(($secStatusResult|Measure-Object).Count -gt 0)
                {
                    $result +=  $secStatusResult 
                    $this.PublishCustomMessage([Constants]::DoubleDashLine + "`r`nCompleted Azure services controls`r`n" + [Constants]::DoubleDashLine, [MessageType]::Update);
                } 
                else
                {
                    $this.PublishCustomMessage([Constants]::SingleDashLine + "`r`nNo attestation data found for Azure services controls`r`n" + [Constants]::DoubleDashLine, [MessageType]::Update)
                }
            }  
        }
        catch 
        {
            $this.CommandError($_);
        }
        #display summary
        if(($result|Measure-Object).Count -gt 0)
        {
            $this.DisplayAttetstationStatistics($result)
        }
        else
        {

        }
        return $result;
    
    }
    hidden [void] DisplayAttetstationStatistics([SVTEventContext[]] $Result)
    {
        $this.PublishCustomMessage("`r`n"+[Constants]::DoubleDashLine+"`r`nSummary of attestation details:`r`n`r`n");
        $this.DisplayAttestationStatusWiseControlsCount($Result);
        $this.DisplaySeverityWiseControlsCount($Result);
        $this.DisplayControlIdWiseCount($Result)
        $this.DisplayExpiryDateWiseControlsCount($Result);
    }
    hidden [void] DisplayAttestationStatusWiseControlsCount([SVTEventContext[]] $Result)
    {
            $subCoreResult = $Result|Where-Object{!$_.IsResource()};
            $resResult = $Result|Where-Object{$_.IsResource()};
            if(($subCoreResult|Measure-Object).Count -gt 0)
            {
                $subCoreGroup = $subCoreResult.ControlResults|Group-Object ActualVerificationResult,AttestationStatus | ForEach{
                [pscustomobject]@{
                   'ActualVerificationResult'=$_.Group[0].ActualVerificationResult
                   'AttestationStatus'=$_.Group[0].AttestationStatus 
                   'ControlsCount'=$_.count}
                }
                $this.PublishCustomMessage([Constants]::SingleDashLine+"`r`nSubscription controls:`r`n"+($subCoreGroup|out-string))
                $this.PublishCustomMessage([Constants]::SingleDashLine)
            }
            if(($resResult|Measure-Object).Count -gt 0)
            {
                $resGroup = $resResult.ControlResults|Group-Object ActualVerificationResult,AttestationStatus | ForEach{
                [pscustomobject]@{
                   'ActualVerificationResult'=$_.Group[0].ActualVerificationResult
                   'AttestationStatus'=$_.Group[0].AttestationStatus 
                   'ControlsCount'=$_.count}
                }  
                $this.PublishCustomMessage("Azure Services controls:`r`n"+($resGroup|out-string))
                $this.PublishCustomMessage([Constants]::DoubleDashLine)
            }
        
    }
    hidden [void] DisplaySeverityWiseControlsCount([SVTEventContext[]] $Result)
    {
        $groupResult = $Result.ControlItem| Group ControlSeverity | ForEach{
                    [pscustomobject]@{
                    'ControlSeverity'=$_.name
                    'ControlsCount'=$_.count}
                    }
        $this.PublishCustomMessage("Distribution of attested controls by severity:`r`n"+($groupResult|out-string))
        $this.PublishCustomMessage([Constants]::DoubleDashLine);
    }
    hidden [void] DisplayControlIdWiseCount([SVTEventContext[]] $Result)
    {
        $groupResult = $Result.ControlItem| Group ControlId | ForEach{
                    [pscustomobject]@{
                    'ControlId'=$_.name
                    'ControlsCount'=$_.count}
                    }
        $this.PublishCustomMessage("Distribution of controls that have been attested:`r`n"+($groupResult|out-string));
        $this.PublishCustomMessage([Constants]::DoubleDashLine);

    }
    hidden [void] DisplayExpiryDateWiseControlsCount([SVTEventContext[]] $Result)
    {
        $subCoreResult = $Result|Where-Object{!$_.IsResource()};
        $resResult = $Result|Where-Object{$_.IsResource()};
        $expiringSubControls = @()
        $expiringStateResources = @()
        if(($subCoreResult|Measure-Object).Count -gt 0)
        {
            $subControlsWithExpDate = $subCoreResult | Where-Object{ $_.ControlResults|Where-Object{![string]::IsNullOrWhiteSpace($_.StateManagement.AttestedStateData.ExpiryDate)}}
            $expiringSubControls= $subControlsWithExpDate | Where-Object{ $_.ControlResults | Where-Object{([datetime]$_.StateManagement.AttestedStateData.ExpiryDate - $(Get-Date).ToUniversalTime()).TotalDays -le 30}}
        }
        if(($resResult|Measure-Object).Count -gt 0)
        {
            $resourcesWithExpDate = $resResult | Where-Object{ $_.ControlResults|Where-Object{![string]::IsNullOrWhiteSpace($_.StateManagement.AttestedStateData.ExpiryDate)}}
            $expiringStateResources = $resourcesWithExpDate | Where-Object{ $_.ControlResults | Where-Object{([datetime]$_.StateManagement.AttestedStateData.ExpiryDate - $(Get-Date).ToUniversalTime()).TotalDays -le 30}}
        }    
        if(($expiringSubControls|Measure-Object).Count -gt 0 -or ($expiringStateResources|Measure-Object).Count -gt 0)
        {
            $expiringSubControls15Days= $expiringSubControls | Where-Object{ $_.ControlResults | Where-Object{([datetime]$_.StateManagement.AttestedStateData.ExpiryDate - $(Get-Date).ToUniversalTime()).TotalDays -le 15}}
            $expiringStateResources15Days = $expiringStateResources | Where-Object{ $_.ControlResults | Where-Object{([datetime]$_.StateManagement.AttestedStateData.ExpiryDate - $(Get-Date).ToUniversalTime()).TotalDays -le 15}}
            $expiringSubControls7Days= $expiringSubControls | Where-Object{ $_.ControlResults | Where-Object{([datetime]$_.StateManagement.AttestedStateData.ExpiryDate - $(Get-Date).ToUniversalTime()).TotalDays -le 7}}
            $expiringStateResources7Days = $expiringStateResources | Where-Object{ $_.ControlResults | Where-Object{([datetime]$_.StateManagement.AttestedStateData.ExpiryDate - $(Get-Date).ToUniversalTime()).TotalDays -le 7}}

            $this.PublishCustomMessage("Summary of controls expiring in near future:`r`n`r`nDays CountOfSubscriptionControls CountOfAzureServicesControls`r`n"+[Constants]::SingleDashLine);
            if(($expiringSubControls7Days|Measure-Object).Count -gt 0 -or ($expiringStateResources7Days|Measure-Object).Count -gt 0)
            {
                $this.PublishCustomMessage("07`t`t$(($expiringSubControls7Days|Measure-Object).Count)`t`t`t`t`t`t`t$(($expiringStateResources7Days|Measure-Object).Count)");
            }
            if(($expiringSubControls15Days|Measure-Object).Count -gt 0 -or ($expiringStateResources15Days|Measure-Object).Count -gt 0)
            {
                $this.PublishCustomMessage("15`t`t$(($expiringSubControls15Days|Measure-Object).Count)`t`t`t`t`t`t`t$(($expiringStateResources15Days|Measure-Object).Count)`t`t`t`t");
            }
            $this.PublishCustomMessage("30`t`t$(($expiringSubControls|Measure-Object).Count)`t`t`t`t`t`t`t$(($expiringStateResources|Measure-Object).Count)`t`t`t`t`r`n`r`n");
            $this.PublishCustomMessage("Recommendation: Check Attestation report to get details of expiring controls and fix/attest them before expiry.",[MessageType]::Warning);
        }
        else
        {
            $this.PublishCustomMessage([Constants]::SingleDashLine+"`r`n`r`nCount of Controls expiring in the next 30 days: 0`r`n");
        }
    }
}