0

I have just begun using AzGraph and I am learning how to use its queries, I am running into an issue when attempting to pull key vault whitelisted IP addresses, below is the query that I am currently running:

Search-AzGraph -Query "resources
|where type == 'microsoft.keyvault/vaults'
|where properties.publicNetworkAccess == 'Enabled'
|mv-expand properties.networkAcls.ipRules
|project kvName = name, kvRule = properties.networkAcls.ipRules"

The output instead of providing a list of addresses per vault returns a bunch of duplicated lines for the same vaults, this only occurs after a certain number of whitelisted addresses, I am not sure on the number:

kvName              kvRule                                                                                                           
------              ------                                                                                                           
Vault1                                                                                                                          
Vault2                                                                                                                  
Vault3              {@{value=1.1.1.1/32}}                                                                                     
Vault4              {@{value=1.1.1.1/32}, @{value=2.2.2.2/32}}                                                             
Vault5              {@{value=1.1.1.1/32}, @{value=2.2.2.2/32}}                                                             
Vault6              {@{value=1.1.1.1/32}}                                                                                      
Vault7                                                                                                                               
Vault8                                                                                                                               
Vault9  <--         {@{value=1.1.1.1/32}, @{value=2.2.2.2/32}, @{value=3.3.3.3/32}, @{value=4.4.4.4/32}...}
Vault9  <--         {@{value=1.1.1.1/32}, @{value=2.2.2.2/32}, @{value=3.3.3.3/32}, @{value=4.4.4.4/32}...}
Vault9  <--         {@{value=1.1.1.1/32}, @{value=2.2.2.2/32}, @{value=3.3.3.3/32}, @{value=4.4.4.4/32}...}
Vault9  <--         {@{value=1.1.1.1/32}, @{value=2.2.2.2/32}, @{value=3.3.3.3/32}, @{value=4.4.4.4/32}...}
Vault9  <--         {@{value=1.1.1.1/32}, @{value=2.2.2.2/32}, @{value=3.3.3.3/32}, @{value=4.4.4.4/32}...}   

I have also tried extending the property values to see if that helped, but instead, the format changed to below

Code:

Search-AzGraph -Query "resources
|where type == 'microsoft.keyvault/vaults'
|where properties.publicNetworkAccess == 'Enabled'
|extend kvRule=parsejson(tostring(properties.networkAcls.ipRules))
|mv-expand kvRule 
|project kvName = name, kvRule.value"

Output:

kvName              kvRule            
------              ------                                     
Vault1                           
Vault2             1.1.1.1/32
Vault3             1.2.3.4/32   
Vault4             5.6.7.8/32   
Vault5             1.2.3.3/32 
Vault6                      
Vault7
Vault8                 
Vault9   <--       1.1.1.1/32   
Vault9   <--       2.2.2.2/32  
Vault9   <--       3.3.3.3/32 
Vault9   <--       4.4.4.4/32   

I came across the join operator, and attempted to use the examples against my queries but failed, the output was always similar to the above output, or I received an error:

This query outputs similar to the second example:

Search-AzGraph -Query "Resources 
| join kind=leftouter (resources | where type=='microsoft.keyvault/vaults' | where properties.publicNetworkAccess == 'Enabled' | extend kvRule=parsejson(tostring(properties.networkAcls.ipRules)) | mv-expand kvRule | project id, kvName = name, kvURI = properties.vaultUri, kvRule) on id
| where type == 'microsoft.keyvault/vaults' 
| project id, name, kvType = type, kvLoc = location, kvSub = subscriptionId, kvURI, kvRule= properties.networkAcls.ipRules"

I also attempted the below query, which errored out that the ipRules are dynamic:

Search-AzGraph -Query "Resources
|where type == 'microsoft.keyvault/vaults'
|where properties.publicNetworkAccess == 'Enabled'
|extend kvRule=parsejson(tostring(properties.networkAcls.ipRules))
|project kvID = id, name, kvLoc = location, kvSub = subscriptionId, kvURI = properties.vaultUri, kvRule
| join kind=leftouter (
    Resources
    |where type == 'microsoft.keyvault/vaults'
    |where properties.publicNetworkAccess == 'Enabled'
    |project name, kvRule = tolower(id))
on kvRule
| summarize by name" 

Error:

"code": "InvalidQuery",
"message": "Query is invalid. Please refer to the documentation for the Azure Resource Graph service and fix the error before retrying."

"code": "Default",
"message": "join key 'kvRule' is of a 'dynamic' type. Please use an explicit cast using extend operator in the join legs (for example, '... | extend kvRule = tostring(kvRule) | join (... | extend kvRule = tostring(kvRule)) on kvRule') as join on a 'dynamic' type is not supported."

I am struggling to understand how I can make this query work, I really believe that I need to use the join operator to get this query right, but I do not have enough understanding of KQL/DB queries to do so, looking to be educated on how I can correctly perform this query.

My goal is to have the output be a single vault name, with kvRule including a full list of the addresses in its whitelist if there are any all in a single line:

kvName              kvRule                                                                                                           
------              ------                                                                                                           
Vault1                                                                                                                          
Vault2               1.1.1.1/32, 2.2.2.2/32, 3.3.3.3/32                                                                                                    
Vault3               1.1.1.1/32, 2.2.2.2/32, 3.3.3.3/32, 1.1.1.3/32, 2.2.2.4/32, 3.3.3.1/32 
David דודו Markovitz
  • 42,900
  • 6
  • 64
  • 88

1 Answers1

0

to fix the query, instead of

|extend kvRule=parsejson(tostring(properties.networkAcls.ipRules))

make it

|extend kvRule=tostring(parsejson(properties.networkAcls.ipRules))

and for better filtering, you may consider using

| where isnotempty(kvRule)

and for clear results

kind=inner