Framework/Listeners/AzResourceInventoryListener.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
class AzResourceInventoryListener: ListenerBase
{
    [Microsoft.ApplicationInsights.TelemetryClient] $TelemetryClient;
    hidden static [AzResourceInventoryListener] $Instance = $null;
    AzResourceInventoryListener():Base()
    {
        $this.TelemetryClient = [Microsoft.ApplicationInsights.TelemetryClient]::new()
    }
    static [AzResourceInventoryListener] GetInstance() {
        if( $null  -eq [AzResourceInventoryListener]::Instance ) {
             [AzResourceInventoryListener]::Instance = [AzResourceInventoryListener]::new();
         }
         return [AzResourceInventoryListener]::Instance
     }
    [void] RegisterEvents() 
    {
        $this.UnregisterEvents();       

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

        $this.RegisterEvent([SVTEvent]::CommandStarted, {
            $currentInstance = [AzResourceInventoryListener]::GetInstance();
        try
        {
            $scanSource = [RemoteReportHelper]::GetScanSource();
            if($scanSource -ne [ScanSource]::Runbook) { return; }
            $SubscriptionId = ([ContextHelper]::GetCurrentRMContext()).Subscription.Id;
            [ResourceInventory]::FetchResources();
            [AzResourceInventoryListener]::PostAzResourceInventory();            
            $resources= [ResourceInventory]::RawResources
            $resourceGroups = Get-AzResourceGroup
            $resourceDetails = @();
            $telemetryEvents = [System.Collections.ArrayList]::new()
                    foreach($res in $resources){
                                    $rgTags = ($resourceGroups | where-object {$_.ResourceGroupName  -eq $res.ResourceGroupName}).Tags;
                        $resourceProperties = @{
                        "Name" = $res.Name;
                        "ResourceId" = $res.ResourceId;
                        "ResourceName" = $res.Name;
                        "ResourceType" = $res.ResourceType;
                        "ResourceGroupName" = $res.ResourceGroupName;
                        "Location" = $res.Location;
                        "SubscriptionId" = $SubscriptionId;
                        "Tags" = [Helpers]::FetchTagsString($res.Tags);
                        "Sku" = $res.Sku
                        "Env" = $res.Tags.Env;
                            "ComponentID" = $res.Tags.ComponentID;
                            "RGComponentID" = $rgTags.ComponentID;
                            "RGEnv" = $rgTags.Env;
                    }
                    $telemetryEvent = "" | Select-Object Name, Properties, Metrics
                    $telemetryEvent.Name = "Resource Inventory"
                    $telemetryEvent.Properties = $resourceProperties
                    $telemetryEvents.Add($telemetryEvent) | Out-Null
                    $resourceDetails+=$resourceProperties;           
                    }
                [AIOrgTelemetryHelper]::TrackEvents($telemetryEvents);
                [RemoteApiHelper]::PostResourceFlatInventory($resourceDetails)
                [ResourceInventory]::SetResourceToTagMapping($resourceDetails)
        }
        catch{
            $currentInstance.PublishException($_);
        }
        });
    }

    
    static [void] PostAzResourceInventory()
    {
        $currentInstance = [AzResourceInventoryListener]::GetInstance();
        try
        {
            $scanSource = [RemoteReportHelper]::GetScanSource();
            if($scanSource -ne [ScanSource]::Runbook) { return; }
            $SubscriptionId = ([ContextHelper]::GetCurrentRMContext()).Subscription.Id;
            if(-not $SubscriptionId) {return;}
            $resources = "" | Select-Object "SubscriptionId", "ResourceGroups"
            $resources.SubscriptionId = $SubscriptionId
            $resources.ResourceGroups = [System.Collections.ArrayList]::new()
            $supportedResourceTypes = [SVTMapping]::GetSupportedResourceMap()
            # Not considering nested resources to reduce complexity
            $filteredResources = [ResourceInventory]::FilteredResources
            $grouped = $filteredResources | Group-Object {$_.ResourceGroupName} | Select-Object Name, Group                
            foreach($group in $grouped){
                $resourceGroup = "" | Select-Object Name, Resources
                $resourceGroup.Name = $group.Name
                $resourceGroup.Resources = [System.Collections.ArrayList]::new()
                foreach($item in $group.Group){
                    $resource = "" | Select-Object Name, ResourceId, Feature
                    if($item.Name.Contains("/")){
                        $splitName = $item.Name.Split("/")
                        $resource.Name = $splitName[$splitName.Length - 1]
                    }
                    else{
                        $resource.Name = $item.Name;
                    }
                    $resource.ResourceId = $item.ResourceId
                    $resource.Feature = $supportedResourceTypes[$item.ResourceType.ToLower()]
                    $resourceGroup.Resources.Add($resource) | Out-Null
                }
                $resources.ResourceGroups.Add($resourceGroup) | Out-Null
            }
            [RemoteApiHelper]::PostResourceInventory($resources)
        }
        catch
        {
            $currentInstance.PublishException($_);
        }
    }
}