#!/usr/bin/env python # coding: utf-8 # # Resource Graph Explorer Data Provider # # ## Description # This data provider allows for a connection to the [Azure Resource Graph](https://docs.microsoft.com/azure/governance/resource-graph/overview) and a way to query against the [Azure Resource Graph Explorer](https://docs.microsoft.com/azure/governance/resource-graph/first-query-portal). The data connector functions in the same way as other data connectors and uses the Kusto Query Language (KQL) and has with some subtle differences to other connectors in they way that authentication is handled. # # You would use this data connector to flexibly and quickly get details on deployed Azure resources within a subscription. It allows for bulk queries on various aspects of resources and returns data in a very structured format. This makes it much more effective and efficient than getting resource specific details via the resource API. # # More details about data providers in MSTICPy can be found in [the documentation](https://msticpy.readthedocs.io/en/latest/data_acquisition/DataProviders.html) # # ### Installation # Installation of this data connector requires that MSTICPy be installed with the Azure extras: # `pip install msticpy['azure']` # # ### Initialization # The provider for the Azure Resource Graph is named `ResourceGraph` # # In[1]: from msticpy.data.data_providers import QueryProvider qry_prov = QueryProvider("ResourceGraph") # ### Authentication # Once initialized the first step in using the data provider is to authenticate. The Resource Graph provider uses MSTICPy's [Azure authentication features](https://msticpy.readthedocs.io/en/latest/data_acquisition/AzureData.html?highlight=azure#instantiating-and-connecting-with-an-azure-data-connector) and you can provide a set of authentication methods when connecting. By default the provider will attempt to authenticate using credentials stored in msticpyconfig.yaml (or as environment variables) and an Azure CLI connection but this can be customized with the 'auth_methods' keyword.
# # If storing details in msticpyconfig.yaml they must be under the `AzureCLI` DataProviders section - for more details see [this documentation](https://msticpy.readthedocs.io/en/latest/getting_started/msticpyconfig.html). # # Once successfully connected you will be presented with a "Connected" message. # # In[2]: qry_prov.connect(auth_methods=["cli"]) # ## Listing available queries # As with other data providers there are a number of built-in queries with this provider. Once connected you can view the available queries with `QUERY_PROVIDER.list_queries()`. # # Alternatively you can view query details in an interactive widget with `QUERY_PROVIDER.browse_queries()` # # For more information, refer documentation : [Listing available queries](https://msticpy.readthedocs.io/en/latest/data_acquisition/DataProviders.html#listing-available-queries). # In[19]: qry_prov.browse_queries() # ## Running pre-defined query # In order to run pre-defined query, execute with the query name, e.g. `QUERY_PROVIDER.ResoruceGraph.QUERY_NAME()`. You can pass parameters to these queries to customize them, however they will also run with default parameters if none as provider. The query browser will provide details as to what parameters are avaliable with each query. # # As with other data providers data is returned to you in a Pandas DataFrame. # # For more information , refer documentation - [Running an pre-defined query](https://msticpy.readthedocs.io/en/latest/data_acquisition/DataProviders.html#running-an-pre-defined-query) # In[20]: qry_prov.ResourceGraph.list_resources_by_api_version() # ### Running an ad-hoc query # You can also define a your own KQL query for the Resource Graph and run with `QUERY_PROVIDER.exec_query(QUERY)` # # For more information, see the documentation on [Running an Ad-hoc Query](https://msticpy.readthedocs.io/en/latest/data_acquisition/DataProviders.html#running-an-ad-hoc-query) # In[33]: query = "Resources | where type =~ 'Microsoft.Compute/virtualMachines' | take 3" qry_prov.exec_query(query) #
# Note: Resource Graph queries are limited to 1000 rows of output each.
# If your query returns 1000 rows it is likely it has hit this limit, consider re-writing the query to return a smaller subset of data.
# This applies to both built in queries and ad-hoc queries.
#
# ## End-to-end Example # # In this example we want to take a look at all of the virtual machines we have in our environment and they get specific details including public IP on one of them: # In[22]: from msticpy.data.data_providers import QueryProvider # Initialize and connect to provider qry_prov = QueryProvider("ResourceGraph") qry_prov.connect() # In[36]: # Get list of VMs and see how many we have vms = qry_prov.ResourceGraph.list_virtual_machines() print(f"Number of VMs found : {len(vms.index)}") # Filter the query to get a smaller dataset vms = qry_prov.ResourceGraph.list_virtual_machines(add_query_items="| where resourceGroup contains 'msticpy'") display(vms) # Set hostname for our next query hostname = vms.iloc[0]['name'] # Now we can get details on the specific VM using its hostname. # In[37]: df = qry_prov.ResourceGraph.list_detailed_virtual_machines(host_name=hostname) print(tabulate(df.head(), df.columns, tablefmt="rst", showindex=False)) # copy table to clipboard and paste in RST doc clip.copy(tabulate(df.head(), df.columns, tablefmt="rst", showindex=False)) # ## References # # - Azure Resource Graph: https://docs.microsoft.com/azure/governance/resource-graph/overview # - Resource Graph Query Language: https://docs.microsoft.com/azure/governance/resource-graph/concepts/query-language