Framework/Core/SVT/SVTResourceResolver.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
246
247
248
249
250
Set-StrictMode -Version Latest

class SVTResourceResolver: AzSKRoot
{
    [string[]] $ResourceNames = @();
    [string] $ResourceType = "";
    [ResourceTypeName] $ResourceTypeName = [ResourceTypeName]::All;
    [Hashtable] $Tag = $null;
    [string] $TagName = "";
    [string] $TagValue = "";
    hidden [string[]] $ResourceGroups = @();

    [SVTResource[]] $SVTResources = @();
    
    # Indicates to fetch all resource groups
    SVTResourceResolver([string] $subscriptionId):
        Base($subscriptionId)
    { }

    SVTResourceResolver([string] $subscriptionId, [string] $resourceGroupNames, [string] $resourceNames, [string] $resourceType, [ResourceTypeName] $resourceTypeName):
        Base($subscriptionId)
    {
        $this.ResourceType = $resourceType;
        $this.ResourceTypeName = $resourceTypeName;

        #throw if user has set params for ResourceTypeName and ResourceType
        #Default value of ResourceTypeName is All.
        if($this.ResourceTypeName -ne [ResourceTypeName]::All -and -not [string]::IsNullOrWhiteSpace($this.ResourceType)){
            throw [SuppressedException] "Both the parameters 'ResourceTypeName' and 'ResourceType' contains values. You should use only one of these parameters."
        }

        if(-not [string]::IsNullOrEmpty($resourceGroupNames))
        {
            $this.ResourceGroups += $this.ConvertToStringArray($resourceGroupNames);

            if ($this.ResourceGroups.Count -eq 0)
            {
                throw [SuppressedException] "The parameter 'ResourceGroupNames' does not contain any string."
            }
        }        
        if(-not [string]::IsNullOrEmpty($resourceNames))
        {
            $this.ResourceNames += $this.ConvertToStringArray($resourceNames)
            if ($this.ResourceNames.Count -eq 0)
            {
                throw [SuppressedException] "The parameter 'ResourceNames' does not contain any string."
            }
        }
    }

    [void] LoadAzureResources()
    {
        #Lazy load the SVT resource array
        if($this.SVTResources.Count -eq 0)
        {
            $resources = @();
            $EnableDevOpsKitSetupCheck = [ConfigurationManager]::GetAzSKConfigData().EnableDevOpsKitSetupCheck;
            $AzSKCfgResource=$null
            if($EnableDevOpsKitSetupCheck)
            {
                $settings = [ConfigurationManager]::GetAzSKSettings();
                [string] $omsSource = $settings.OMSSource;
                if([string]::IsNullOrWhiteSpace($omsSource) -or $omsSource -eq "SDL"){                    
                    $AzSKCfgResource= [SVTResource]::new();
                    $AzSKCfgResource.ResourceId = 'AzSKCfg';
                    $AzSKCfgResource.ResourceGroupName = 'AzSKCfg';
                    $AzSKCfgResource.ResourceName = 'AzSKCfg';
                    $AzSKCfgResource.ResourceType = 'AzSKCfg';
                    $AzSKCfgResource.Location = 'CentralUS';
                    $AzSKCfgResource.ResourceTypeMapping = ([SVTMapping]::Mapping |
                                                Where-Object { $_.ResourceType -eq 'AzSKCfg' } |
                                                Select-Object -First 1);
                    #This is to handle the case where user passes ResourceTypeName as 'AzSKCfg'
                    if($this.ResourceTypeName -eq 'AzSKCfg')
                    {
                        $resources+=$AzSKCfgResource
                        $AzSKCfgResource=$null 
                    }
                }
                else
                {
                    $AzSKCfgResource=$null 
                }
            }
            
            #Fetch Resources from Azure
            if($this.ResourceGroups.Count -eq 0 -or ($this.ResourceGroups.Count -eq 1 -and $this.ResourceGroups[0].Trim() -eq "*"))
            {
                #Target resource group param is not passed. Pass appropriate input params
                $resources += $this.FindAzureRmResource("");
            }
            else
            {
                #Fetch resources for all resource groups
                $this.ResourceGroups |
                ForEach-Object {
                    $resouresFound = $this.FindAzureRmResource($_);

                    if(($resouresFound | Measure-Object).Count -eq 0)
                    {
                        $this.PublishCustomMessage("Could not find any resources to scan under Resource Group [$_]", [MessageType]::Warning);
                    }
                    else
                    {
                        $resources += $resouresFound;
                    }
                }
            }

            if($resources.Count -eq 0)
            {
                throw ([SuppressedException]::new(("Could not find any resources that match the specified criteria."), [SuppressedExceptionType]::InvalidOperation))
            }

            $allResourceTypes = [string[]] [ConfigurationManager]::LoadServerConfigFile("AllResourceTypes.json");

            $erVnetResourceGroups = $null;
            if($null -ne $AzSKCfgResource)
            {
                $this.SVTResources+=$AzSKCfgResource
            }        

            $resources | Where-Object { $this.ResourceNames.Count -le 1 -or $this.ResourceNames -contains $_.Name} | ForEach-Object {
                $resource = $_
                $svtResource = [SVTResource]::new();
                $svtResource.ResourceId = $resource.ResourceId;
                $svtResource.ResourceGroupName = $resource.ResourceGroupName;
                $svtResource.ResourceName = $resource.Name;
                $svtResource.ResourceType = $resource.ResourceType;
                $svtResource.Location = $resource.Location;

                if($this.ResourceTypeName -ne [ResourceTypeName]::All)
                {
                    $svtResource.ResourceTypeMapping = ([SVTMapping]::Mapping |
                                            Where-Object { $_.ResourceTypeName -eq $this.ResourceTypeName } |
                                            Select-Object -First 1);
                }
                else
                {
                    $svtResource.ResourceTypeMapping = ([SVTMapping]::Mapping |
                                            Where-Object { $_.ResourceType -eq $resource.ResourceType } |
                                            Select-Object -First 1);
                }

                # Checking if Vnet is ErVNet or not
                if($svtResource.ResourceTypeMapping -and $svtResource.ResourceTypeMapping.ResourceTypeName -eq [SVTMapping]::VirtualNetworkTypeName)
                {
                    if(-not $erVnetResourceGroups)
                    {
                        $erVnetResourceGroups = $this.ConvertToStringArray([ConfigurationManager]::GetAzSKConfigData().ERvNetResourceGroupNames);
                    }

                    # Check if the resource group name corresponds to ERvNet
                    if(($erVnetResourceGroups -contains $svtResource.ResourceGroupName) -or ([Helpers]::IsvNetExpressRouteConnected($svtResource.ResourceName, $svtResource.ResourceGroupName)))
                    {
                        # Set the ERvNet type
                        $svtResource.ResourceTypeMapping = ([SVTMapping]::Mapping |
                                            Where-Object { $_.ResourceTypeName -eq [SVTMapping]::ERvNetTypeName } |
                                            Select-Object -First 1);
                    }
                }
                $ignoredType = $false;
                # Added APIConnectionTypeName in condition to explicitly exclude APIConnection (LogicApp connector) type while scanning
                # APIConnection (LogicApp connector) will be only scanned when logic app is being scanned.
                if(-not $svtResource.ResourceTypeMapping -or $svtResource.ResourceTypeMapping.ResourceTypeName -eq [SVTMapping]::APIConnectionTypeName)
                {
                    $ignoredType = [string]::IsNullOrEmpty(($allResourceTypes | Where-Object { $_ -eq $resource.ResourceType } | Select-Object -First 1));
                }

                if(-not $ignoredType)
                {
                    $this.SVTResources += $svtResource;
                }
            }
        }
    }

    hidden [SVTResource] CreateSVTResource([string] $ConnectionResourceId,[string] $ResourceGroupName, [string] $ConnectionResourceName, [string] $ResourceType, [string] $Location, [string] $MappingName)
    {
        $svtResource = [SVTResource]::new();
        $svtResource.ResourceId = $ConnectionResourceId; 
        $svtResource.ResourceGroupName = $ResourceGroupName;
        $svtResource.ResourceName = $ConnectionResourceName
        $svtResource.ResourceType = $ResourceType; #
        $svtResource.Location = $Location;
        $svtResource.ResourceTypeMapping = ([SVTMapping]::Mapping |
                        Where-Object { $_.ResourceTypeName -eq $MappingName } |
                        Select-Object -First 1);

        return $svtResource;
    }

    hidden [System.Object[]] FindAzureRmResource([string] $resourceGroupName)
    {
        $expression = "Get-AzureRmResource";

        if(-not [string]::IsNullOrEmpty($resourceGroupName))
        {
            $expression += " -ResourceGroupName '$resourceGroupName' " ;
        }

        if([string]::IsNullOrEmpty($this.ResourceType) -and ($this.ResourceTypeName -ne [ResourceTypeName]::All))
        {
            $mapping = ([SVTMapping]::Mapping |
                                    Where-Object { $_.ResourceTypeName -eq $this.ResourceTypeName.ToString() } |
                                    Select-Object -First 1);
            if($mapping)
            {
                $this.ResourceType = $mapping.ResourceType;
            }
        }

        if(-not [string]::IsNullOrEmpty($this.ResourceType))
        {
            $expression += " -ResourceType '$($this.ResourceType)' " ;
        }

        if($this.ResourceNames.Count -eq 1)
        {
            $expression += " -Name '$($this.ResourceNames[0])' " ;
        }
        
        if($this.Tag -and $this.Tag.Count -ne 0)
        {
            $tagValues = "";
            $this.Tag.GetEnumerator() |
            ForEach-Object {
                $tagValues += "'$($_.Name)'='$($_.Value)'; "
            };

            $expression += " -Tag @{ $tagValues }" ;

        }
        elseif((-not [string]::IsNullOrEmpty($this.TagName)) -and (-not [string]::IsNullOrEmpty($this.TagValue)))
        {
            $expression += " -TagName '$($this.TagName)' -TagValue '$($this.TagValue)'" ;
        }

        $result = @();
        $expressionResult = Invoke-Expression $expression
        if($expressionResult)
        {
            $result += $expressionResult
        }
        return $result;
    }
    
}