Skip to content

Commit

Permalink
exception fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
cammj committed May 9, 2024
1 parent 9617f72 commit 53c370a
Showing 1 changed file with 36 additions and 10 deletions.
46 changes: 36 additions & 10 deletions Sync.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
Expand All @@ -25,9 +26,6 @@ public static class Sync
private static bool _pullEntraUsers =
bool.Parse(Environment.GetEnvironmentVariable("SyncEntra", EnvironmentVariableTarget.Process) ?? "false");

// If to pull entra users
private static string _pullEntraProperties = (Environment.GetEnvironmentVariable("SyncEntraProperties", EnvironmentVariableTarget.Process) ?? "id,mail,displayName,givenName,surname,department,companyName,city,country,JobTitle");

/// <summary>
/// How many table rows to send up in a batch
/// </summary>
Expand Down Expand Up @@ -91,6 +89,11 @@ public static async Task RunAsync([TimerTrigger("0 */15 * * * *")] TimerInfo myT
// Sync Tenant Simulation Users
foreach (string id in simulationIds)
{
// Throw if taskBatchQueueProcessor is faulted
if (taskBatchQueueProcessor.IsFaulted)
throw new Exception($"taskBatchQueueProcessor has faulted: {taskBatchQueueProcessor.Exception}");

// Run get simulation users
await GetTenantSimulationUsers(GraphClient, id);
}

Expand Down Expand Up @@ -234,13 +237,23 @@ private static async Task<HashSet<string>> GetTenantSimulations(GraphServiceClie
{
// Get the table row item for this simulation
var SimulationExistingTableItem = await tableClient.GetEntityIfExistsAsync<TableEntity>("Simulations", sim.Id);

// For determining the last user sync
DateTime? LastUserSync = null;
if (SimulationExistingTableItem.HasValue && SimulationExistingTableItem.Value.ContainsKey("LastUserSync"))
LastUserSync = DateTime.SpecifyKind(DateTime.Parse(SimulationExistingTableItem.Value["LastUserSync"].ToString()), DateTimeKind.Utc);

// Perform a user sync (if)
// - We have never performed a sync
// - The simulation finished within the past 7 days
// - Or the simulation is running
// - Last user sync is more than a month ago

if (!SimulationExistingTableItem.HasValue || sim.Status == SimulationStatus.Running || sim.CompletionDateTime > DateTime.UtcNow.AddDays(-7))
if (!SimulationExistingTableItem.HasValue ||
sim.Status == SimulationStatus.Running ||
sim.CompletionDateTime > DateTime.UtcNow.AddDays(-7) ||
LastUserSync is null ||
(LastUserSync.HasValue && LastUserSync.Value < DateTime.UtcNow.AddMonths(-1)))
{
_log.LogInformation($"Perform full synchronisation of simulation '{sim.DisplayName}' status {SimulationStatus.Running}");
SimulationIds.Add(sim.Id);
Expand Down Expand Up @@ -269,7 +282,8 @@ private static async Task<HashSet<string>> GetTenantSimulations(GraphServiceClie
{"Payload_DisplayName", sim.Payload?.DisplayName},
{"Payload_Platform", sim.Payload?.Platform?.ToString()},
{"Status", sim.Status.ToString()},
{"AutomationId", sim.AutomationId}
{"AutomationId", sim.AutomationId},
{"LastUserSync", LastUserSync}
}));

return true;
Expand All @@ -286,6 +300,8 @@ private static async Task<HashSet<string>> GetTenantSimulations(GraphServiceClie
/// <param name="GraphClient"></param>
private static async Task GetTenantSimulationUsers(GraphServiceClient GraphClient, string SimulationId)
{
Stopwatch sw = Stopwatch.StartNew();
_log.LogInformation($"Performing full user synchronisation of {SimulationId}");

var requestInformation =
GraphClient.Security.AttackSimulation.Simulations[SimulationId].ToGetRequestInformation();
Expand All @@ -306,6 +322,10 @@ private static async Task GetTenantSimulationUsers(GraphServiceClient GraphClien
{"SimulationUser_Email", userSimDetail.SimulationUser?.Email},
{"CompromisedDateTime", userSimDetail.CompromisedDateTime},
{"ReportedPhishDateTime", userSimDetail.ReportedPhishDateTime},
{"AssignedTrainingsCount", userSimDetail.AssignedTrainingsCount},
{"CompletedTrainingsCount", userSimDetail.CompletedTrainingsCount},
{"InProgressTrainingsCount", userSimDetail.InProgressTrainingsCount},
{"IsCompromised", userSimDetail.IsCompromised},
}));

// Determine if should sync user
Expand Down Expand Up @@ -345,6 +365,11 @@ private static async Task GetTenantSimulationUsers(GraphServiceClient GraphClien
{"LastUserSync", DateTime.UtcNow},
}));

_log.LogInformation($"Full user synchronisation of {SimulationId} completed in {sw.Elapsed}");

// Perform a task delay here to allow other threads to complete
await Task.Delay(1000);

}

/// <summary>
Expand Down Expand Up @@ -382,8 +407,6 @@ private static async Task SyncUser(GraphServiceClient GraphClient, string id)
{
// Set in dictionary
userListSynced[id] = true;

string[] syncProperties = _pullEntraProperties.Split(",");

try
{
Expand Down Expand Up @@ -449,11 +472,14 @@ private static async Task<bool> ShouldSyncUser(string id)
// Get last sync time
DateTime? LastUserSync = null;
if (UserTableItem.HasValue && UserTableItem.Value.ContainsKey("LastUserSync"))
LastUserSync = DateTime.Parse(UserTableItem.Value["LastUserSync"].ToString());
LastUserSync = DateTime.SpecifyKind(DateTime.Parse(UserTableItem.Value["LastUserSync"].ToString()), DateTimeKind.Utc);

// If no sync or days is older than a year
if (LastUserSync is null || LastUserSync.Value < DateTime.UtcNow.AddDays(-1))
// If no sync or days is older than a week
if (LastUserSync is null || LastUserSync.Value < DateTime.UtcNow.AddDays(-7))
return true;

// Add to userSyncList so we don't need to check again
userListSynced[id] = true;

return false;

Expand Down

0 comments on commit 53c370a

Please sign in to comment.