How pala46/azure-inventory works
Purpose and Entry Point
This is a one-shot Azure inventory exporter. Its sole executable entry point, azure_inventory.py, authenticates through Azure SDK credential discovery, queries Azure Resource Graph (ARG) across visible or selected subscriptions, enriches resources locally, and writes CSV and optionally Excel output.
• azure_inventory.py
• requirements.txt
CLI and Run Configuration
azure_inventory.py owns argument parsing and controls the run scope: subscription filters, output format/path, required-tag policy, ARG page size, fast mode, and policy-query suppression. Constants near the top define organization-specific tag aliases, resource categories, environment name hints, and region display mappings.
The script is intentionally stateless: each invocation creates a timestamped inventory snapshot in the current directory or caller-specified output base path.
• azure_inventory.py
Azure Discovery Layer
Azure Resource Graph is the primary data plane, avoiding subscription-by-subscription ARM enumeration. The script queries resources plus supporting ARG datasets such as resource containers, policy states, diagnostic settings, and recovery/protection data. It pages graph results and reduces page size when payload limits are encountered.
Management-group ancestry, subscription names, tenant information, direct ARM resource fields, and several governance/operations signals are obtained in this layer.
• azure_inventory.py
Resource Normalization and Core Inventory Records
Raw ARG rows are transformed into a common record schema. This stage derives category from provider namespace, parses ARM IDs for parent/resource-group relationships, normalizes region names, extracts SKU/state/zone/identity details from properties, and emits N/A where a value cannot be determined.
The common schema is the boundary between Azure-specific query payloads and tabular export formats.
• azure_inventory.py
Governance Enrichment
Tag processing normalizes tag keys and scans them against configurable regex patterns to populate business metadata such as application, owner, environment, criticality, lifecycle, and classification. Environment can fall back to resource-group or resource-name hints. Required-tag compliance is alias-aware rather than relying solely on literal tag names.
Policy compliance is an optional separate ARG enrichment; policy-state results are consolidated so the worst observed state is assigned to each resource.
• azure_inventory.py
Network, Security, and Operations Correlation
The script builds in-memory relationships rather than issuing per-resource API calls. It cross-references public IPs, NICs, VMs, load balancers, VNets, subnets, and NSGs to derive public exposure, VNet/subnet placement, and network controls. Type-specific property inspection determines storage, disk, and CMK-related encryption status.
Additional graph-derived lookup sets associate resources with ARM diagnostic settings and Recovery Services protected-item sources. --fast bypasses property-driven network, access, and encryption enrichment.
• azure_inventory.py
Export Boundary and Dependencies
Final normalized/enriched records are serialized to CSV and, when openpyxl is installed, XLSX. Azure authentication supports Azure CLI login, service-principal environment variables, and managed identity through Azure SDK credential chaining. Required Azure permissions are Reader for inventory scope and Policy Insights read access for policy status.
• azure_inventory.py
• requirements.txt
Main entry points: azure_inventory.py Raw resource and lookup rows · requirements.txt Dependencies
Components 13
Local execution
- CLI and run config · Controls scope, output, paging, fast mode, and policy queries.
azure_inventory.py - Org mappings and policies · Tag aliases, categories, environment hints, and region mappings.
azure_inventory.py - Dependencies · Azure SDK and optional Excel dependency declarations.
requirements.txt
Azure discovery
- Azure credential chain · Uses CLI login, environment service principal, or managed identity.
azure_inventory.py - Azure Resource Graph · Paged cross-subscription resource and supporting dataset queries.
azure_inventory.py - ARM and tenant metadata · Management ancestry, subscription names, tenant, and direct resource fields.
azure_inventory.py - Raw resource and lookup rows · Resources, containers, policy, diagnostics, and recovery data.
azure_inventory.py
Inventory processing
- Common record normalization · Parses ARM IDs and derives category, region, SKU, state, zones, and identity.
azure_inventory.py - Tag and policy enrichment · Applies alias-aware tag compliance and worst policy state.
azure_inventory.py - Network, security, and ops correlation · Derives exposure, placement, controls, encryption, diagnostics, and protection.
azure_inventory.py - Enriched inventory records · Azure-independent records ready for serialization.
azure_inventory.py
Export
- Timestamped CSV snapshot · Written to the current directory or requested output base path.
azure_inventory.py - Optional XLSX snapshot · Produced when openpyxl is available.
azure_inventory.py
Connections 19
- CLI and run config → Org mappings and policies: uses
- CLI and run config → Azure credential chain: initializes
- Azure credential chain → Azure Resource Graph: authenticates queries
- CLI and run config → Azure Resource Graph: sets subscriptions and page size
- Azure Resource Graph → Raw resource and lookup rows: returns paged datasets
- Azure credential chain → ARM and tenant metadata: authenticates lookup
- ARM and tenant metadata → Raw resource and lookup rows: adds context
- Raw resource and lookup rows → Common record normalization: resource rows
- Org mappings and policies → Common record normalization: category and region mappings
- Common record normalization → Tag and policy enrichment: common records
- Org mappings and policies → Tag and policy enrichment: tag aliases and required policy
- Raw resource and lookup rows → Tag and policy enrichment: policy states
- Tag and policy enrichment → Network, security, and ops correlation: governed records
- Raw resource and lookup rows → Network, security, and ops correlation: relationship lookup sets
- CLI and run config → Network, security, and ops correlation: fast mode may bypass
- Network, security, and ops correlation → Enriched inventory records: enriches records
- Enriched inventory records → Timestamped CSV snapshot: serializes
- Enriched inventory records → Optional XLSX snapshot: serializes
- Dependencies → Optional XLSX snapshot: openpyxl enables