Skip to content

Commit

Permalink
Basic implementation of Key Store diff
Browse files Browse the repository at this point in the history
  • Loading branch information
Maxhy committed Mar 27, 2024
1 parent 9f6cb03 commit 7df744e
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 11 deletions.
90 changes: 81 additions & 9 deletions KeyManager.Library/KeyStore/KeyStore.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using Leosac.KeyManager.Library.DivInput;
using Newtonsoft.Json;
using System.Text;
using static System.Formats.Asn1.AsnWriter;
using System.Threading;

namespace Leosac.KeyManager.Library.KeyStore
{
Expand Down Expand Up @@ -256,7 +258,7 @@ public virtual async Task Publish(KeyStore store, Func<string, KeyStore?> getFav
await Publish(store, getFavoriteKeyStore, askForKeyStoreSecretIfRequired, keClass, null, initCallback);
}

public virtual async Task Publish(KeyStore store, Func<string, KeyStore?> getFavoriteKeyStore, Func<KeyStore, Task<bool>>? askForKeyStoreSecretIfRequired, KeyEntryClass keClass, IEnumerable<KeyEntryId>? ids, Action<KeyStore, KeyEntryClass, int>? initCallback)
protected virtual async Task KeyEntriesAction(KeyStore store, Func<string, KeyStore?> getFavoriteKeyStore, Func<KeyStore, Task<bool>>? askForKeyStoreSecretIfRequired, KeyEntryClass keClass, IEnumerable<KeyEntryId>? ids, Action<KeyStore, KeyEntryClass, int>? initCallback, Func<KeyStore, List<IChangeKeyEntry>, Task> action)
{
var changes = new List<IChangeKeyEntry>();
if (ids == null)
Expand All @@ -267,7 +269,7 @@ public virtual async Task Publish(KeyStore store, Func<string, KeyStore?> getFav
if (!string.IsNullOrEmpty(Options?.PublishVariable))
{
Attributes[ATTRIBUTE_PUBVAR] = Options.PublishVariable;
Attributes[ATTRIBUTE_HEXPUBVAR] = Convert.ToHexString(Encoding.UTF8.GetBytes(Options.PublishVariable));
Attributes[ATTRIBUTE_HEXPUBVAR] = Convert.ToHexString(Encoding.UTF8.GetBytes(Options.PublishVariable));
}

foreach (var id in ids)
Expand Down Expand Up @@ -382,25 +384,95 @@ public virtual async Task Publish(KeyStore store, Func<string, KeyStore?> getFav

await store.Open();
try
{
await action(store, changes);
}
finally
{
await store.Close();
}
}

public virtual Task Publish(KeyStore store, Func<string, KeyStore?> getFavoriteKeyStore, Func<KeyStore, Task<bool>>? askForKeyStoreSecretIfRequired, KeyEntryClass keClass, IEnumerable<KeyEntryId>? ids, Action<KeyStore, KeyEntryClass, int>? initCallback)
{
return KeyEntriesAction(store, getFavoriteKeyStore, askForKeyStoreSecretIfRequired, keClass, ids, initCallback, new Func<KeyStore, List<IChangeKeyEntry>, Task>(async (s, changes) =>
{
if (!(Options?.DryRun).GetValueOrDefault(false))
{
await store.Store(changes);
await s.Store(changes);
}
else
{
log.Info("Dry Run, skipping the storage of key entries.");
}
}
finally
{
await store.Close();
}
}));
}

public virtual Task Diff(KeyStore store, Func<string, KeyStore?> getFavoriteKeyStore, Func<KeyStore, Task<bool>>? askForKeyStoreSecretIfRequired, KeyEntryClass keClass, IEnumerable<KeyEntryId>? ids, Action<KeyStore, KeyEntryClass, int>? initCallback)
{
throw new NotImplementedException();
return KeyEntriesAction(store, getFavoriteKeyStore, askForKeyStoreSecretIfRequired, keClass, ids, initCallback, new Func<KeyStore, List<IChangeKeyEntry>, Task>(async (s, changes) =>
{
uint missings = 0, diffs = 0;
var details = string.Empty;
foreach (KeyEntry c in changes)
{
if (await store.CheckKeyEntryExists(c.Identifier, keClass))
{
var ke = await store.Get(c.Identifier, keClass);
if (ke != null)
{
if (JsonConvert.SerializeObject(ke.Properties) == JsonConvert.SerializeObject(c.Properties))
{
if (c.Variant?.Name == ke.Variant?.Name)
{
if (c.Variant != null)
{
for(int i = 0; i < c.Variant.KeyContainers.Count; i++)
{
if (c.Variant.KeyContainers[i].Key.GetAggregatedValueAsString() != ke.Variant!.KeyContainers[i].Key.GetAggregatedValueAsString())
{
diffs++;
details += string.Format("Key Container `{0}` (#{1}) of {2} doesn't match.", c.Variant.KeyContainers[i].Name, i, c.Identifier) + Environment.NewLine;
break;
}
}
}
}
else
{
diffs++;
details += string.Format("Variant of {0} doesn't match.", c.Identifier) + Environment.NewLine;
}
}
else
{
diffs++;
details += string.Format("Properties of {0} doesn't match.", c.Identifier) + Environment.NewLine;
}
}
else
{
diffs++;
details += string.Format("Cannot load details of {0}.", c.Identifier) + Environment.NewLine;
}
}
else
{
missings++;
details += string.Format("{0} is missing.", c.Identifier) + Environment.NewLine;
}
}
if (missings > 0 || diffs > 0)
{
var differror = "Key Entries on both Key Store doesn't match." + Environment.NewLine
+ string.Format("Missing: {0} - Differences: {1}", missings, diffs) + Environment.NewLine
+ details;
log.Info(differror);
throw new KeyStoreException(differror);
}
}));
}

private static string? ComputeDivInput(DivInputContext divContext, IList<DivInputFragment> divInput)
Expand Down
4 changes: 2 additions & 2 deletions KeyManager/Domain/EditKeyStoreControlViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -318,8 +318,6 @@ public async Task RunOnKeyStore(UserControl dialog, Func<KeyStore, Func<string,
initCallback
);
}

SnackbarHelper.EnqueueMessage(_snackbarMessageQueue, "Key Entries have been successfully published.");
}
finally
{
Expand All @@ -337,6 +335,7 @@ public async Task Publish()
try
{
await RunOnKeyStore(new PublishKeyStoreDialog(), KeyStore.Publish);
SnackbarHelper.EnqueueMessage(_snackbarMessageQueue, "Key Entries have been successfully published.");
}
catch (KeyStoreException ex)
{
Expand All @@ -357,6 +356,7 @@ public async Task Diff()
try
{
await RunOnKeyStore(new DiffKeyStoreDialog(), KeyStore.Diff);
SnackbarHelper.EnqueueMessage(_snackbarMessageQueue, "No differences found.");
}
catch (KeyStoreException ex)
{
Expand Down

0 comments on commit 7df744e

Please sign in to comment.