Function1.cs is an Azure Function designed to automate the synchronization of simulation data between Microsoft Graph API and Microsoft Dataverse. It processes simulation data, user coverage, and training user coverage, ensuring efficient and reliable data handling.
The function is triggered periodically using a TimerTrigger. The current schedule is set to run every hour:
The function retrieves configuration values from environment variables to connect to external services. Examples include:
TenantIdClientIdDataverseConnectionSimulationTableCoverageUsersTableSimulationUsersTableTrainingUserTable
The function interacts with Microsoft Graph API to:
- Fetch simulations (
/security/attackSimulation/simulations). - Retrieve user coverage and training user coverage data.
The function uses the ServiceClient to interact with Microsoft Dataverse, performing CRUD operations on tables like:
SimulationTableSimulationUsersTableCoverageUsersTableTrainingUserTable
The function determines the appropriate Microsoft Graph base URL based on the environment (e.g., AzureUSDoD, AzureGov, or default graph.microsoft.com).
The GetAccessToken method retrieves an OAuth token for authenticating requests to Microsoft Graph API. It caches the token and refreshes it before expiration.
The GetSimulations method retrieves a list of simulations from Microsoft Graph API. It handles pagination and rate-limiting (HTTP 429).
The WriteSimulationToDataverse method writes simulation data to the SimulationTable in Dataverse. It checks if a record already exists and either updates or creates it.
The function retrieves the sync status of simulations from Dataverse and filters simulations based on their status and sync rules. It processes simulations that:
- Are completed but not yet marked as "Completed."
- Are running but haven't been synced in the last 24 hours.
- Are canceled or excluded but not yet marked as "Completed."
For each simulation, the function:
- Fetches associated users using
GetSimulationUsers. - Writes user data to the
SimulationUsersTablein Dataverse usingWriteSimulationUsersToDataverse.
The function retrieves training user coverage data from Microsoft Graph API and writes it to the TrainingUserTable in Dataverse using WriteTrainingUserCoverageToDataverse.
The code includes commented-out sections for fetching and writing user coverage data to Dataverse. These operations are marked as long-running processes.
The function includes robust error handling:
- Logs errors using
ILogger. - Retries requests on rate-limiting (HTTP 429) or token expiration (HTTP 401).
- Catches and logs exceptions during data processing.
The file includes several helper methods for specific tasks:
RetrieveExistingRecord: Checks if a record exists in Dataverse.MarkSimulationAsProcessed: Updates the sync status of a simulation.AddUserCountToSimulation: Updates the user count for a simulation.RetrieveSyncStatusesForSimulations: Retrieves sync statuses for multiple simulations.
The file defines data models for deserializing API responses:
Simulation: Represents a simulation.UserCoverage: Represents user coverage data.TrainingUserCoverage: Represents training user coverage data.SimulationUsers: Represents users associated with a simulation.
- Concurrency: The function uses
Task.WhenAllto process simulations and users concurrently. - Rate Limiting: The function handles rate-limiting by respecting the
Retry-Afterheader or applying exponential backoff. - Pagination: The function handles paginated responses from Microsoft Graph API using the
@odata.nextLinkproperty.
The function automates the synchronization of simulation data between Microsoft Graph API and Dataverse. It is designed to handle large datasets, rate-limiting, and token expiration gracefully, ensuring reliable and efficient data processing.