Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add logic + tests for cache invalidation #65

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 33 additions & 3 deletions src/CommonLib/Cache.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.Collections.Concurrent;
using System;
using System.Collections.Concurrent;
using System.ComponentModel;
using System.Runtime.Serialization;
using SharpHoundCommonLib.Enums;

Expand All @@ -18,6 +20,8 @@ public class Cache
//
// [DataMember]private ConcurrentDictionary<string, string> _valueToIDCache;

private static Version defaultVersion = new(1, 0, 0);

private Cache()
{
ValueToIdCache = new ConcurrentDictionary<string, string>();
Expand All @@ -36,6 +40,8 @@ private Cache()
[DataMember] public ConcurrentDictionary<string, string> SIDToDomainCache { get; private set; }

[DataMember] public ConcurrentDictionary<string, string> ValueToIdCache { get; private set; }
[DataMember] public DateTime CacheCreationDate { get; set; }
[DataMember] public Version CacheCreationVersion { get; set; }

[IgnoreDataMember] private static Cache CacheInstance { get; set; }

Expand Down Expand Up @@ -137,9 +143,17 @@ private static string GetPrefixKey(string key, string domain)
/// Creates a new empty cache instance
/// </summary>
/// <returns></returns>
public static Cache CreateNewCache()
public static Cache CreateNewCache(Version version = null)
{
return new Cache();
if (version == null)
{
version = defaultVersion;
}
return new Cache
{
CacheCreationVersion = version,
CacheCreationDate = DateTime.Now.Date
};
}

/// <summary>
Expand All @@ -152,6 +166,22 @@ public static void SetCacheInstance(Cache cache)
CreateMissingDictionaries();
}

public static bool CacheNeedsInvalidation(Cache cache, Version version)
{
superlinkx marked this conversation as resolved.
Show resolved Hide resolved
var threshold = DateTime.Now.Subtract(TimeSpan.FromDays(30));
if (cache.CacheCreationDate < threshold)
{
return true;
}

if (cache.CacheCreationVersion == null || version > cache.CacheCreationVersion)
{
return true;
}

return false;
}

/// <summary>
/// Gets stats from the currently loaded cache
/// </summary>
Expand Down
55 changes: 55 additions & 0 deletions test/unit/CacheTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using System;
using Newtonsoft.Json;
using SharpHoundCommonLib;
using Xunit;
using Xunit.Abstractions;

namespace CommonLibTest
{
public class CacheTest
{
private ITestOutputHelper _testOutputHelper;
public CacheTest(ITestOutputHelper testOutputHelper)
{
_testOutputHelper = testOutputHelper;
}

[Fact]
public void Cache_TestCacheInvalidation()
{
var cache = Cache.CreateNewCache();
var version = new Version(1, 0, 0);
cache.CacheCreationVersion = version;

Assert.True(Cache.CacheNeedsInvalidation(cache, new Version(1,0,1)));
Assert.False(Cache.CacheNeedsInvalidation(cache, new Version(1,0,0)));

var time = DateTime.Now.Subtract(TimeSpan.FromDays(29));
cache.CacheCreationDate = time;
Assert.False(Cache.CacheNeedsInvalidation(cache, version));
cache.CacheCreationDate = DateTime.Now.Subtract(TimeSpan.FromDays(31));
Assert.True(Cache.CacheNeedsInvalidation(cache, version));
}

[Fact]
public void Cache_TestNewCache()
{
var cache = Cache.CreateNewCache();
Assert.Equal(cache.CacheCreationVersion, new Version(1,0,0));
var version = new Version(1, 0, 1);
cache = Cache.CreateNewCache(version);
var time = DateTime.Now.Date;
Assert.Equal(cache.CacheCreationVersion, version);
Assert.Equal(cache.CacheCreationDate, time);
}

[Fact]
public void Cache_OldCacheWillInvalidate()
{
const string json = "{\"GlobalCatalogCache\": {}, \"IdToTypeCache\": {}, \"MachineSidCache\": {}, \"SIDToDomainCache\": {}, \"ValueToIdCache\": {}}";
var cache = JsonConvert.DeserializeObject<Cache>(json);
Assert.Null(cache.CacheCreationVersion);
Assert.True(Cache.CacheNeedsInvalidation(cache, new Version(1,0,0)));
}
}
}
Loading