|
| 1 | +''' |
| 2 | +External IDs can be a useful method of keeping track of assets in multiple systems, but it can be time consuming to manually tag each asset. In this example, we'll take a look at how you can, with a map of name/kind/external id, tag each asset in your inventory with an external id automatically using the SDK |
| 3 | +''' |
| 4 | +from azure.identity import InteractiveBrowserCredential |
| 5 | +from azure.defender.easm import EasmClient |
| 6 | + |
| 7 | +#To create an EasmClient, you need your subscription ID, region, and some sort of credential. |
| 8 | +sub_id = '<your subscription ID here>' |
| 9 | +workspace_name = '<your workspace name here>' |
| 10 | +resource_group = '<your resource group here>' |
| 11 | +region = '<your region here>' |
| 12 | + |
| 13 | +endpoint = f'{region}.easm.defender.microsoft.com' |
| 14 | + |
| 15 | +# For the purposes of this demo, I've chosen the InteractiveBrowserCredential but any credential will work. |
| 16 | +browser_credential = InteractiveBrowserCredential() |
| 17 | +client = EasmClient(endpoint, resource_group, sub_id, workspace_name, browser_credential) |
| 18 | + |
| 19 | +# Assets in EASM can be uniquely distinguished by `name` and `kind`, so we can create a simple dictionary containing `name`, `kind`, and `external_id`. In a more realistic case, this could be generated using an export from the external system we're using for tagging, but for our purposes, we can manually write it out |
| 20 | +external_id_mapping = [ |
| 21 | + { |
| 22 | + 'name': 'example.com', |
| 23 | + 'kind': 'host', |
| 24 | + 'external_id': 'EXT040' |
| 25 | + }, |
| 26 | + { |
| 27 | + 'name': 'example.com', |
| 28 | + 'kind': 'domain', |
| 29 | + 'external_id': 'EXT041' |
| 30 | + }, |
| 31 | + { |
| 32 | + 'name': '93.184.216.34', |
| 33 | + 'kind': 'ipAddress', |
| 34 | + 'external_id': 'EXT042' |
| 35 | + }, |
| 36 | + { |
| 37 | + 'name': 'example.org', |
| 38 | + 'kind': 'host', |
| 39 | + 'external_id': 'EXT050' |
| 40 | + }, |
| 41 | +] |
| 42 | +# Using the `assets` client, we can update each asset and append the tracking id of the update to our update ID list, so that we can keep track of the progress on each update later |
| 43 | +update_ids = [] |
| 44 | + |
| 45 | +for asset in external_id_mapping: |
| 46 | + update_request = {'external_id': asset['external_id']} |
| 47 | + asset_filter = f"kind = {asset['kind']} AND name = {asset['name']}" |
| 48 | + update = client.assets.update(body=update_request, filter=asset_filter) |
| 49 | + update_ids.append(update['id']) |
| 50 | + |
| 51 | +# Using the `tasks` client, we can view the progress of each update using the `get` method |
| 52 | +for update_id in update_ids: |
| 53 | + update = client.tasks.get(update_id) |
| 54 | + print(f'{update["id"]}: {update["state"]}') |
| 55 | + |
| 56 | +# The updates can be viewed using the `assets.list` method by creating a filter that matches on each external id using an `in` query |
| 57 | +ids = ', '.join([f'"{asset["external_id"]}"' for asset in external_id_mapping]) |
| 58 | +asset_filter = f'External ID in ({ids})' |
| 59 | + |
| 60 | +for asset in client.assets.list(filter=asset_filter): |
| 61 | + print(f'{asset["externalId"]}, {asset["name"]}') |
| 62 | + |
0 commit comments