Framework/Listeners/CA/WriteCAStatus.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
Set-StrictMode -Version Latest 
#Listner to write CA scan status on completion of resource scan
class WriteCAStatus: ListenerBase
{
    hidden static [WriteCAStatus] $Instance = $null;
    static [WriteCAStatus] GetInstance()
    {
        if ( $null -eq  [WriteCAStatus]::Instance)
        {
            [WriteCAStatus]::Instance = [WriteCAStatus]::new();
        }    
        return [WriteCAStatus]::Instance
    }


    [void] RegisterEvents()
    {
        $this.UnregisterEvents();       

        $this.RegisterEvent([AzSKRootEvent]::GenerateRunIdentifier, {
            $currentInstance = [WriteCAStatus]::GetInstance();
            try 
            {
                $currentInstance.SetRunIdentifier([AzSKRootEventArgument] ($Event.SourceArgs | Select-Object -First 1));                         
            }
            catch 
            {
                $currentInstance.PublishException($_);
            }
        });

        $this.RegisterEvent([SVTEvent]::EvaluationCompleted, {
            $currentInstance = [WriteCAStatus]::GetInstance();
            try 
            {
                $props = $Event.SourceArgs[0];
                if($props)
                {
                    if($props.IsResource())
                    {
                        #Update resource scan completion in CA storage account
                        $currentInstance = [WriteCAStatus]::GetInstance();
                        $scanSource = [AzSKSettings]::GetInstance().GetScanSource();
                        [ControlBaselineManager] $controlBaselineMngr = [ControlBaselineManager]::GetInstance();
                        $baselineControlsDetails = $controlBaselineMngr.GetBaselineControlDetails()
                        #If Scan source is in suported sources or UsePartialCommits switch is available
                        if ($currentInstance.InvocationContext.BoundParameters["UsePartialCommits"] -or ($baselineControlsDetails.SupportedSources -contains $scanSource))
                        {
                            $controlBaselineMngr.UpdateResourceStatus( $props.ResourceContext.ResourceId,"COMP");
                        }
                        #$currentInstance.AddOutputLog(([Constants]::CompletedAnalysis -f $props.FeatureName, $props.ResourceContext.ResourceGroupName, $props.ResourceContext.ResourceName));
                    }
                    else
                    {
                        
                    }
                }            
            }
            catch 
            {
                $currentInstance.PublishException($_);
            }
        });

         $this.RegisterEvent([SVTEvent]::CommandCompleted, {
            $currentInstance = [ControlBaselineManager]::GetInstance();
            try 
            {
                $currentInstance = [WriteCAStatus]::GetInstance();
                $scanSource = [AzSKSettings]::GetInstance().GetScanSource();
                [ControlBaselineManager] $controlBaselineMngr = [ControlBaselineManager]::GetInstance();
                $baselineControlsDetails = $controlBaselineMngr.GetBaselineControlDetails()
                #If Scan source is in suported sources or UsePartialCommits switch is available
                if ($currentInstance.InvocationContext.BoundParameters["UsePartialCommits"] -or ($baselineControlsDetails.SupportedSources -contains $scanSource))
                {
                    $controlBaselineMngr.RemoveControlBaseline();   
                }
            }
            catch 
            {
                $currentInstance.PublishException($_);
            }
        });
    }


}