Skip to content

Commit

Permalink
Add Deep Listing option on File Key Store to check all key entries on…
Browse files Browse the repository at this point in the history
… key store opening and retrieve labels
  • Loading branch information
Maxhy committed Apr 11, 2024
1 parent 5a66c27 commit bb6e855
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@
xmlns:properties="clr-namespace:Leosac.KeyManager.Library.KeyStore.File.UI.Properties"
mc:Ignorable="d"
d:DataContext="{d:DesignInstance domain:FileKeyStorePropertiesControlViewModel}"
d:DesignWidth="500" Height="100">
d:DesignWidth="500" Height="130">
<UserControl.Resources>
<kslibdomain:HexStringLengthConverter x:Key="HexStringLengthConverter" />
</UserControl.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<WrapPanel Grid.Row="0" Orientation="Horizontal">
<TextBox x:Name="tbxDirectory" HorizontalAlignment="Left" Margin="5,5,5,5" TextWrapping="Wrap" VerticalAlignment="Top" Width="233" IsReadOnly="True"
Expand All @@ -36,7 +37,11 @@
</TextBox>
<Button x:Name="btnBrowse" Content="{x:Static properties:Resources.Browse}" HorizontalAlignment="Left" Margin="5,5,5,5" VerticalAlignment="Top" Click="BtnBrowse_Click"/>
</WrapPanel>
<DockPanel Grid.Row="1">
<StackPanel Orientation="Horizontal" VerticalAlignment="Center" Margin="5" Grid.Row="1" ToolTip="{x:Static properties:Resources.DeepListingHelper}">
<ToggleButton IsChecked="{Binding FileProperties.DeepListing}" Style="{StaticResource MaterialDesignSwitchLightToggleButton}" />
<TextBlock Text="{x:Static properties:Resources.DeepListing}" Padding="3" />
</StackPanel>
<DockPanel Grid.Row="2">
<StackPanel DockPanel.Dock="Right">
<ToggleButton IsChecked="{Binding Properties.StoreSecret, Mode=TwoWay}"
Style="{StaticResource MaterialDesignSwitchToggleButton}"
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,12 @@
<data name="Browse" xml:space="preserve">
<value>Parcourir...</value>
</data>
<data name="DeepListing" xml:space="preserve">
<value>Liste détaillée</value>
</data>
<data name="DeepListingHelper" xml:space="preserve">
<value>Liste détaillée (peut causer des problèmes de performances sur de larges magasins de clés)</value>
</data>
<data name="Directory" xml:space="preserve">
<value>Répertoire</value>
</data>
Expand Down
6 changes: 6 additions & 0 deletions KeyManager.Library.KeyStore.File.UI/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,12 @@
<data name="Browse" xml:space="preserve">
<value>Browse...</value>
</data>
<data name="DeepListing" xml:space="preserve">
<value>Deep Listing</value>
</data>
<data name="DeepListingHelper" xml:space="preserve">
<value>Deep Listing (may have performance issues on large key store)</value>
</data>
<data name="Directory" xml:space="preserve">
<value>Directory</value>
</data>
Expand Down
19 changes: 14 additions & 5 deletions KeyManager.Library.KeyStore.File/FileKeyStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ public FileKeyStoreProperties GetFileProperties()

public override IEnumerable<KeyEntryClass> SupportedClasses
{
get => new KeyEntryClass[] { KeyEntryClass.Symmetric, KeyEntryClass.Asymmetric };
get => [KeyEntryClass.Symmetric, KeyEntryClass.Asymmetric];
}

public override Task Open()
{
log.Info(String.Format("Opening the key store `{0}`...", GetFileProperties().Fullpath));
log.Info(string.Format("Opening the key store `{0}`...", GetFileProperties().Fullpath));
if (!System.IO.Directory.Exists(GetFileProperties().Fullpath))
{
log.Error(string.Format("Cannot open the key sore `{0}`.", GetFileProperties().Fullpath));
Expand Down Expand Up @@ -166,7 +166,7 @@ private byte[] GetWrappingKey()
return key;
}

public override Task<IList<KeyEntryId>> GetAll(KeyEntryClass? keClass)
public override async Task<IList<KeyEntryId>> GetAll(KeyEntryClass? keClass)
{
log.Info(string.Format("Getting all key entries (class: `{0}`)...", keClass));
IList<KeyEntryId> keyEntries = new List<KeyEntryId>();
Expand All @@ -180,10 +180,19 @@ public override Task<IList<KeyEntryId>> GetAll(KeyEntryClass? keClass)
foreach (var file in files)
{
string identifier = System.IO.Path.GetFileNameWithoutExtension(System.IO.Path.GetFileNameWithoutExtension(file));
keyEntries.Add(new KeyEntryId { Id = identifier });
var keid = new KeyEntryId { Id = identifier };
if (GetFileProperties().DeepListing && keClass != null)
{
var ke = await Get(keid, keClass.Value);
if (ke != null)
{
keid = ke.Identifier;
}
}
keyEntries.Add(keid);
}
log.Info(string.Format("{0} key entries returned.", keyEntries.Count));
return Task.FromResult(keyEntries);
return keyEntries;
}

public override async Task Store(IList<IChangeKeyEntry> changes)
Expand Down
9 changes: 8 additions & 1 deletion KeyManager.Library.KeyStore.File/FileKeyStoreProperties.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,23 @@ public class FileKeyStoreProperties : KeyStoreProperties, IEquatable<FileKeyStor
public FileKeyStoreProperties()
{
_fullpath = string.Empty;
_deepListing = true;
}

private string _fullpath;

public string Fullpath
{
get => _fullpath;
set => SetProperty(ref _fullpath, value);
}

private bool _deepListing;
public bool DeepListing
{
get => _deepListing;
set => SetProperty(ref _deepListing, value);
}

public override int? SecretMaxLength => 32;

public override bool Equals(object? obj)
Expand Down
14 changes: 10 additions & 4 deletions KeyManager.Library.UI/Domain/KeyEntriesControlViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,10 @@ public KeyEntriesControlViewModel(ISnackbarMessageQueue snackbarMessageQueue, Ke
DataContext = model
};
await UpdateKeyEntry(dialog);
if (await UpdateKeyEntry(dialog) && model.KeyEntry != null)
{
identifier.KeyEntryId = model.KeyEntry.Identifier;
}
}
}
catch (KeyStoreException ex)
Expand Down Expand Up @@ -334,7 +337,7 @@ private async Task<bool> EditDefaultKeyEntry(KeyEntryDialog dialog)

public AsyncRelayCommand<SelectableKeyEntryId> CopyKeyEntryCommand { get; }

private async Task UpdateKeyEntry(KeyEntryDialog dialog)
private async Task<bool> UpdateKeyEntry(KeyEntryDialog dialog)
{
try
{
Expand All @@ -344,20 +347,23 @@ private async Task UpdateKeyEntry(KeyEntryDialog dialog)
if (KeyStore != null && model.KeyEntry != null)
{
await KeyStore.Update(model.KeyEntry);
return true;
}
}
}
catch (KeyStoreException ex)
{
SnackbarHelper.EnqueueError(_snackbarMessageQueue, ex, "Key Store Error");
await UpdateKeyEntry(dialog);
return await UpdateKeyEntry(dialog);
}
catch (Exception ex)
{
log.Error("Updating the Key Entry failed unexpected.", ex);
SnackbarHelper.EnqueueError(_snackbarMessageQueue, ex);
await UpdateKeyEntry(dialog);
return await UpdateKeyEntry(dialog);
}

return false;
}

public AsyncRelayCommand<SelectableKeyEntryId> DeleteKeyEntryCommand { get; }
Expand Down
5 changes: 4 additions & 1 deletion KeyManager.Library.UI/KeyEntriesControl.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ public KeyEntriesControl()

private void UserControl_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
{
KeyEntriesDataContext?.RefreshKeyEntries();
if (e.OldValue != null)
{
KeyEntriesDataContext?.RefreshKeyEntries();
}
}

private void KeyEntryEdit_OnDialogClosed(object sender, DialogClosedEventArgs e)
Expand Down

0 comments on commit bb6e855

Please sign in to comment.