Framework/Core/SVT/Services/Databricks.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
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
Set-StrictMode -Version Latest 
class Databricks: SVTBase
{       
    hidden [PSObject] $ResourceObject;
    hidden [string] $ManagedResourceGroupName;
    hidden [string] $WorkSpaceLoction;
    hidden [string] $WorkSpaceBaseUrl = "https://{0}.azuredatabricks.net/api/2.0/";
    hidden [string] $PersonalAccessToken =""; 
    hidden [bool] $HasAdminAccess = $false;

    Databricks([string] $subscriptionId, [string] $resourceGroupName, [string] $resourceName): 
                 Base($subscriptionId, $resourceGroupName, $resourceName) 
    { 
     
        $this.GetResourceObject();
    }

    Databricks([string] $subscriptionId, [SVTResource] $svtResource): 
        Base($subscriptionId, $svtResource) 
    { 
       
        $this.GetResourceObject();
    }

     hidden [PSObject] GetResourceObject()
    {
        if (-not $this.ResourceObject)
        {
        
            $this.ResourceObject = Get-AzureRmResource -Name $this.ResourceContext.ResourceName  `
                                        -ResourceType $this.ResourceContext.ResourceType `
                                        -ResourceGroupName $this.ResourceContext.ResourceGroupName

            if(-not $this.ResourceObject)
            {
                throw ([SuppressedException]::new(("Resource '$($this.ResourceContext.ResourceName)' not found under Resource Group '$($this.ResourceContext.ResourceGroupName)'"), [SuppressedExceptionType]::InvalidOperation))
            }
            else
            {
               $this.InitializeRequiredVariables();
            }
            
        }

        return $this.ResourceObject;
    }

    
    hidden [ControlResult] CheckVnetPeering([ControlResult] $controlResult)
    {
        
        $vnetPeerings = Get-AzureRmVirtualNetworkPeering -VirtualNetworkName "workers-vnet" -ResourceGroupName $this.ManagedResourceGroupName
        if($null -ne $vnetPeerings  -and ($vnetPeerings|Measure-Object).count -gt 0)
        {
            $controlResult.AddMessage([VerificationResult]::Verify, [MessageData]::new("Verify below peering found on VNet", $vnetPeerings));
            $controlResult.SetStateData("Peering found on VNet", $vnetPeerings);

        }
        else
        {
            $controlResult.AddMessage([VerificationResult]::Passed, [MessageData]::new("No VNet peering found on VNet", $vnetPeerings));
        }

        return $controlResult;
    }

     hidden [ControlResult] CheckSecretScope([ControlResult] $controlResult)
    {
        if(-not [string]::IsNullOrEmpty($this.PersonalAccessToken) -and $this.IsUserAdmin())
        {
             $SecretScopes    = $this.InvokeRestAPICall("GET","secrets/scopes/list","")
             if($null -ne  $SecretScopes  -and ( $SecretScopes|Measure-Object).count -gt 0)
             {
               $controlResult.AddMessage([VerificationResult]::Verify, [MessageData]::new("Verify secrets and keys must not be as plain text in notebook"));
             }
             else
             {
               $controlResult.AddMessage([VerificationResult]::Failed, [MessageData]::new("No secret scope found in your workspace."));
             }
        }else
        {
           $controlResult.CurrentSessionContext.Permissions.HasRequiredAccess = $false;
           $controlResult.AddMessage([VerificationResult]::Manual, "Not able to fetch secret scope details. This has to be manually verified.");
        }
       

        return $controlResult;
    }

     hidden [ControlResult] CheckSecretScopeBackend([ControlResult] $controlResult)
    {
        if(-not [string]::IsNullOrEmpty($this.PersonalAccessToken) -and $this.IsUserAdmin())
        {
             $SecretScopes    = $this.InvokeRestAPICall("GET","secrets/scopes/list","")
             if($null -ne  $SecretScopes  -and (( $SecretScopes|Measure-Object).count -gt 0) -and [Helpers]::CheckMember($SecretScopes,"scopes"))
             {
                  $DatabricksBackedSecret = $SecretScopes.scopes | where {$_.backend_type -ne "AZURE_KEYVAULT"}
                  if($null -ne $DatabricksBackedSecret -and ( $SecretScopes|Measure-Object).count -gt 0)
                  {
                    $controlResult.AddMessage([VerificationResult]::Verify, [MessageData]::new("Following Databricks backed secret scopes found:", $DatabricksBackedSecret));
                    $controlResult.SetStateData("Following Databricks backed secret scope found:", $DatabricksBackedSecret);
                  }
                  else
                  {
                    $controlResult.AddMessage([VerificationResult]::Passed, [MessageData]::new("All secret scopes in the workspace are Key Vault backed."));
                  }
             }
             else
             {
               $controlResult.AddMessage([VerificationResult]::Verify, [MessageData]::new("No secret scope found in your workspace."));
             }
        }else
        {
           $controlResult.CurrentSessionContext.Permissions.HasRequiredAccess = $false;
           $controlResult.AddMessage([VerificationResult]::Manual, "Not able to fetch secret scope details. This has to be manually verified.");
        }
       

        return $controlResult;
    }

    hidden [ControlResult] CheckKeyVaultReference([ControlResult] $controlResult)
    {
        if(-not [string]::IsNullOrEmpty($this.PersonalAccessToken) -and $this.IsUserAdmin())
        {
            $KeyVaultScopeMapping = @()
            $KeyVaultWithMultipleReference = @() 
            $SecretScopes    = $this.InvokeRestAPICall("GET","secrets/scopes/list","")
            if($null -ne  $SecretScopes  -and (( $SecretScopes|Measure-Object).count -gt 0) -and [Helpers]::CheckMember($SecretScopes,"scopes"))
            {
              $KeyVaultBackedSecretScope = $SecretScopes.scopes | where {$_.backend_type -eq "AZURE_KEYVAULT"}
              if($null -ne $KeyVaultBackedSecretScope -and ( $KeyVaultBackedSecretScope | Measure-Object).count -gt 0)
              {
                $KeyVaultBackedSecretScope | ForEach-Object {
                    $KeyVaultScopeMappingObject = "" | Select-Object "ScopeName", "KeyVaultResourceId"
                    $KeyVaultScopeMappingObject.ScopeName = $_.name
                    $KeyVaultScopeMappingObject.KeyVaultResourceId = $_.keyvault_metadata.resource_id
                    $KeyVaultScopeMapping += $KeyVaultScopeMappingObject
                }
                # Check if same keyvault is referenced by multiple secret scopes
                $KeyVaultWithManyReference = $KeyVaultScopeMapping | Group-object -Property KeyVaultResourceId | Where-Object {$_.Count -gt 1} 
                if($null -ne $KeyVaultWithManyReference -and ($KeyVaultWithManyReference | Measure-Object).Count -gt 0)
                {
                    $KeyVaultWithManyReference | ForEach-Object { $KeyVaultWithMultipleReference += $_.Name }
                    $controlResult.AddMessage([VerificationResult]::Verify, [MessageData]::new("Following KeyVault(s) are referenced by multiple secret scope:", $KeyVaultWithMultipleReference));
                    $controlResult.SetStateData("Following KeyVault(s) are referenced by multiple secret scope:", $KeyVaultWithMultipleReference);

                }else
                {
                    $controlResult.AddMessage([VerificationResult]::Passed, [MessageData]::new("All KeyVault backed secret scope are linked with independent KeyVault.", $KeyVaultWithMultipleReference));
                }
              }
              else
              {
                $controlResult.AddMessage([VerificationResult]::Verify, [MessageData]::new("No KeyVault backed secret scope found in your workspace."));
              }
            }
            else
            {
                $controlResult.AddMessage([VerificationResult]::Verify, [MessageData]::new("No secret scope is found in your workspace."));
            }

        }else
        {
            $controlResult.CurrentSessionContext.Permissions.HasRequiredAccess = $false;
            $controlResult.AddMessage([VerificationResult]::Manual, "Not able to fetch secret scope details. This has to be manually verified.");
        }
       

        return $controlResult;
    }

    hidden [ControlResult] CheckAccessTokenExpiry([ControlResult] $controlResult)
    {   
        if(-not [string]::IsNullOrEmpty($this.PersonalAccessToken))
        {
             $AccessTokens = $this.InvokeRestAPICall("GET","token/list","")
            if($null -ne $AccessTokens -and ($AccessTokens.token_infos| Measure-Object).Count -gt 0)
            {   
                $PATwithInfiniteValidity =@()
                $AccessTokensList =@()
                $AccessTokens.token_infos | ForEach-Object {
                    $currentObject = "" | Select-Object "comment","token_id","expiry_in_days"
                    if($_.expiry_time -eq "-1")
                    {
                        $currentObject.comment = $_.comment
                        $currentObject.token_id = $_.token_id
                        $currentObject.expiry_in_days = "Never"
                    }
                    else{
                     
                        $currentObject.comment = $_.comment
                        $currentObject.token_id = $_.token_id
                        $currentObject.expiry_in_days = (New-TimeSpan -Seconds (($_.expiry_time - $_.creation_time)/1000)).Days
                    }
                    $AccessTokensList += $currentObject

                }
                $PATwithInfiniteValidity += $AccessTokensList | Where-Object {$_.expiry_in_days -eq "Never" }
                $PATwithInfiniteValidity += $AccessTokensList | Where-Object {$_.expiry_in_days -ne "Never"} | Where-Object {$_.expiry_in_days -gt 180} 
            
                $PATwithFiniteValidity = $AccessTokensList | Where-Object {$_.expiry_in_days -ne "Never" -and $_.expiry_in_days -le 180}

                if($null -ne $PATwithInfiniteValidity -and ($PATwithInfiniteValidity| Measure-Object).Count -gt 0)
                {
                    $controlResult.AddMessage([VerificationResult]::Failed, [MessageData]::new("Following personal access tokens have validity more than 180 days:", $PATwithInfiniteValidity));
                    #$controlResult.SetStateData("Following personal access tokens have validity more than 180 days:", $PATwithInfiniteValidity);

                }
                else
                {
                    $controlResult.AddMessage([VerificationResult]::Passed, [MessageData]::new("Following personal access tokens have validity less than 180 days:", $PATwithFiniteValidity));
                }

            }
            else
            {
                $controlResult.AddMessage([VerificationResult]::Manual, [MessageData]::new("No personal access token found in your workspace."));
            }    

        }else
        {
           $controlResult.CurrentSessionContext.Permissions.HasRequiredAccess = $false;
           $controlResult.AddMessage([VerificationResult]::Manual, "Not able to fetch PAT (personal access token) details. This has to be manually verified.");
        } 
       
        return $controlResult;
    }

    hidden [ControlResult] CheckAdminAccess([ControlResult] $controlResult)
    {   
       if(-not [string]::IsNullOrEmpty($this.PersonalAccessToken) -and $this.IsUserAdmin())
       {    
            $controlResult.VerificationResult = [VerificationResult]::Verify;
            $accessList = [RoleAssignmentHelper]::GetAzSKRoleAssignmentByScope($this.GetResourceId(), $false, $true);
            $adminAccessList = $accessList | Where-Object { $_.RoleDefinitionName -eq 'Owner' -or $_.RoleDefinitionName -eq 'Contributor'}
            # Add check for User Type
            $potentialAdminUsers = @()
            $activeAdminUsers =@()
            $adminAccessList | ForEach-Object {
                if([Helpers]::CheckMember($_, "SignInName"))
                {
                   $potentialAdminUsers += $_.SignInName
                }
            }    
            # Get All Active Users
            $requestBody = "group_name=admins"
            $activeAdmins = $this.InvokeRestAPICall("GET","groups/list-members",$requestBody);
            if($null -ne $activeAdmins -and ($activeAdmins | Measure-Object).Count -gt 0)
            {
                $activeAdminUsers += $activeAdmins.members
            }
            if(($potentialAdminUsers|Measure-Object).Count -gt 0)
            {
                $controlResult.AddMessage("`r`nValidate that the following identities have potential admin access to resource - [$($this.ResourceContext.ResourceName)]");
                $controlResult.AddMessage("Note: Users that have been explicitly added in the 'admins' group in the workspace are considered 'active' admins");
                $controlResult.AddMessage([MessageData]::new("", $potentialAdminUsers));
            }
            if(($activeAdminUsers|Measure-Object).Count -gt 0)
            {
                $controlResult.AddMessage("`r`nValidate that the following identities have active admin access to resource - [$($this.ResourceContext.ResourceName)]");
                $controlResult.AddMessage("Note: Users that have been explicitly added in the 'admins' group in the workspace are considered 'active' admins");
                $controlResult.AddMessage([MessageData]::new("", $activeAdminUsers));
            }
       }
       else
       {
            $controlResult.CurrentSessionContext.Permissions.HasRequiredAccess = $false;
            $controlResult.AddMessage([VerificationResult]::Manual, "Not able to fetch admin details. This has to be manually verified.");
       }
        
        return $controlResult;
    }

    hidden [ControlResult] CheckGuestAdminAccess([ControlResult] $controlResult)
    {   
       if(-not [string]::IsNullOrEmpty($this.PersonalAccessToken) -and $this.IsUserAdmin())
       {    
            # Get All Active Users
            $guestAdminUsers =@()
            $requestBody = "group_name=admins"
            $activeAdmins = $this.InvokeRestAPICall("GET","groups/list-members",$requestBody);
            if($null -ne $activeAdmins -and ($activeAdmins.members | Measure-Object).Count -gt 0)
            { 
                if(($null -ne $this.ControlSettings) -and [Helpers]::CheckMember($this.ControlSettings,"Databricks.Tenant_Domain"))
                {    
                     $tenantDomain = $this.ControlSettings.Databricks.Tenant_Domain
                     $activeAdmins.members | ForEach-Object{
                     if($_.user_name.Split('@')[1] -ne $tenantDomain)
                     {
                        $guestAdminUsers +=$_
                     }
                    }
                }
                
            }
            if($null -ne $guestAdminUsers -and ($guestAdminUsers | Measure-Object).Count -gt 0)
            {
                $controlResult.AddMessage([VerificationResult]::Failed, [MessageData]::new("Following guest accounts have admin access on workspace:", $guestAdminUsers));
            }
            else{
                $controlResult.AddMessage([VerificationResult]::Verify, [MessageData]::new("Manually verify that guest accounts should not have admin access on workspace."));
            }
            
       }
       else
       {
            $controlResult.CurrentSessionContext.Permissions.HasRequiredAccess = $false;
            $controlResult.AddMessage([VerificationResult]::Manual, "Not able to fetch admin details. This has to be manually verified.");
       }
        
        return $controlResult;
    }

    hidden [PSObject] InvokeRestAPICall([string] $method, [string] $operation , [string] $queryString)
    {   
         $ResponseObject = $null;
         try
         {
             $uri = $this.WorkSpaceBaseUrl + $operation 
             if(-not [string]::IsNullOrWhiteSpace($queryString))
             {
              $uri =  $uri +'?'+ $queryString
             }
             $ResponseObject = Invoke-RestMethod -Method $method -Uri $uri `
                               -Headers @{"Authorization" = "Bearer "+$this.PersonalAccessToken} `
                               -ContentType 'application/json' -UseBasicParsing
         }
         catch
         {
            $this.PublishCustomMessage("Could not evaluate control due to Databricks API call failure. Token may be invalid.", [MessageType]::Error);
         } 
        return  $ResponseObject 
    }

    hidden [string] ReadAccessToken()
    { 
         $scanSource = [RemoteReportHelper]::GetScanSource();
         if($scanSource -eq [ScanSource]::SpotCheck)
         { 
           $input = Read-Host "Enter PAT (personal access token) for '$($this.ResourceContext.ResourceName)' Databricks workspace"
           $input = $input.Trim()
           return $input;
         }
         else
         {
            return $null;
         }
       
    }

    hidden InitializeRequiredVariables()
    {
        $this.WorkSpaceLoction = $this.ResourceObject.Location
        $count = $this.ResourceObject.Properties.managedResourceGroupId.Split("/").Count
        $this.ManagedResourceGroupName = $this.ResourceObject.Properties.managedResourceGroupId.Split("/")[$count-1]
        $this.WorkSpaceBaseUrl=[system.string]::Format($this.WorkSpaceBaseUrl,$this.WorkSpaceLoction)
        $this.PersonalAccessToken = $this.ReadAccessToken()
        #$this.HasAdminAccess = $this.IsUserAdmin()
    }

    hidden [bool] IsUserAdmin()
    {
      try
      {
          $currentContext = [Helpers]::GetCurrentRMContext()
          $userId = $currentContext.Account.Id;
          $requestBody = "user_name="+$userId
          $parentGroups = $this.InvokeRestAPICall("GET","groups/list-parents",$requestBody)
          if($parentGroups.group_names.Contains("admins"))
          {
              return $true;
          }else
          {
             return $false;
          }
      }
      catch{
        return $false;
      }
      
    }

}