Skip to content

Commit

Permalink
#66 wip adding key regeneration
Browse files Browse the repository at this point in the history
  • Loading branch information
cricketthomas committed Jul 12, 2024
1 parent 7ffd3df commit 8c64fa8
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 14 deletions.
10 changes: 5 additions & 5 deletions KeyVaultExplorer/KeyVaultExplorer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,15 @@

<ItemGroup>
<PackageReference Include="Avalonia.Controls.ItemsRepeater" Version="11.1.0-rc2" />
<PackageReference Include="Avalonia.Svg.Skia" Version="11.1.0-rc1" />
<PackageReference Include="DeviceId" Version="6.6.0" />
<PackageReference Include="Avalonia.Svg.Skia" Version="11.1.0-rc2" />
<PackageReference Include="DeviceId" Version="6.7.0" />
<PackageReference Include="FluentAvaloniaUI" Version="2.1.0-preview6" />
<!--Condition below is needed to remove Avalonia.Diagnostics package from build output in Release configuration.-->
<PackageReference Condition="'$(Configuration)' == 'Debug'" Include="Avalonia.Diagnostics" Version="11.1.0-rc2" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="9.0.0-preview.5.24306.7" />
<PackageReference Include="Microsoft.Data.Sqlite.Core" Version="9.0.0-preview.5.24306.3" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="9.0.0-preview.6.24327.7" />
<PackageReference Include="Microsoft.Data.Sqlite.Core" Version="9.0.0-preview.6.24327.4" />
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.2.2" />
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="9.0.0-preview.5.24306.7" />
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="9.0.0-preview.6.24327.7" />
<PackageReference Include="Microsoft.Identity.Client.Extensions.Msal" Version="4.61.3" />
<PackageReference Include="Azure.ResourceManager.KeyVault" Version="1.3.0" />
<PackageReference Include="Azure.Security.KeyVault.Certificates" Version="4.6.0" />
Expand Down
13 changes: 5 additions & 8 deletions KeyVaultExplorer/Services/VaultService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -318,18 +318,15 @@ public async IAsyncEnumerable<KeyVaultResource> GetWithKeyVaultsBySubscriptionAs

public async Task<KeyVaultSecret> CreateSecret(KeyVaultSecret keyVaultSecret, Uri KeyVaultUri)
{
var token = new CustomTokenCredential(await _authService.GetAzureKeyVaultTokenSilent());
SecretClient client = new SecretClient(KeyVaultUri, token);
return await client.SetSecretAsync(keyVaultSecret);

var token = new CustomTokenCredential(await _authService.GetAzureKeyVaultTokenSilent());
SecretClient client = new SecretClient(KeyVaultUri, token);
return await client.SetSecretAsync(keyVaultSecret);
}

public async Task<SecretProperties> UpdateSecret(SecretProperties secretProperties, Uri KeyVaultUri)
{
var token = new CustomTokenCredential(await _authService.GetAzureKeyVaultTokenSilent());

SecretClient client = new SecretClient(KeyVaultUri, token);

return await client.UpdateSecretPropertiesAsync(secretProperties);
SecretClient client = new SecretClient(KeyVaultUri, token);
return await client.UpdateSecretPropertiesAsync(secretProperties);
}
}
91 changes: 91 additions & 0 deletions KeyVaultExplorer/ViewModels/CreateNewKeyVersionViewModel .cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
using Avalonia.Controls;
using Avalonia.Controls.ApplicationLifetimes;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using KeyVaultExplorer.Views;
using KeyVaultExplorer.Services;
using System.Threading.Tasks;
using System.Threading;
using System.Linq;
using Azure.Security.KeyVault.Secrets;
using System;
using Azure.Security.KeyVault.Keys;

namespace KeyVaultExplorer.ViewModels;

public partial class CreateNewKeyVersionViewModel : ViewModelBase
{
[ObservableProperty]
private bool isBusy = false;

[ObservableProperty]
private bool isEdit = false;

public bool HasActivationDate => KeyVaultKeyModel is not null && KeyVaultKeyModel.NotBefore.HasValue;
public bool HasExpirationDate => KeyVaultKeyModel is not null && KeyVaultKeyModel.ExpiresOn.HasValue;

[ObservableProperty]
private string secretValue;

[ObservableProperty]
[NotifyPropertyChangedFor(nameof(Location))]
[NotifyPropertyChangedFor(nameof(HasActivationDate))]
[NotifyPropertyChangedFor(nameof(HasExpirationDate))]
private KeyProperties keyVaultKeyModel;

[ObservableProperty]
private TimeSpan? expiresOnTimespan;

[ObservableProperty]
private TimeSpan? notBeforeTimespan;

public string? Location => KeyVaultKeyModel?.VaultUri.ToString();
public string? Identifier => KeyVaultKeyModel?.Id.ToString();

private readonly AuthService _authService;
private readonly VaultService _vaultService;
private NotificationViewModel _notificationViewModel;

public CreateNewKeyVersionViewModel()
{
_authService = Defaults.Locator.GetRequiredService<AuthService>();
_vaultService = Defaults.Locator.GetRequiredService<VaultService>();
_notificationViewModel = Defaults.Locator.GetRequiredService<NotificationViewModel>();
}

[RelayCommand]
public async Task EditDetails()
{
if (KeyVaultKeyModel.NotBefore.HasValue)
KeyVaultKeyModel.NotBefore = KeyVaultKeyModel.NotBefore.Value.Date + (NotBeforeTimespan.HasValue ? NotBeforeTimespan.Value : TimeSpan.Zero);

if (KeyVaultKeyModel.ExpiresOn.HasValue)
KeyVaultKeyModel.ExpiresOn = KeyVaultKeyModel.ExpiresOn.Value.Date + (ExpiresOnTimespan.HasValue ? ExpiresOnTimespan.Value : TimeSpan.Zero);

//var updatedProps = await _vaultService.UpdateSecret(KeyVaultKeyModel, KeyVaultKeyModel.VaultUri);
//KeyVaultKeyModel = updatedProps;
}

[RelayCommand]
public async Task NewVersion()
{
var newSecret = new KeyVaultSecret(KeyVaultKeyModel.Name, SecretValue);
if (KeyVaultKeyModel.NotBefore.HasValue)
newSecret.Properties.NotBefore = KeyVaultKeyModel.NotBefore.Value.Date + (NotBeforeTimespan.HasValue ? NotBeforeTimespan.Value : TimeSpan.Zero);

if (KeyVaultKeyModel.ExpiresOn.HasValue)
newSecret.Properties.ExpiresOn = KeyVaultKeyModel.ExpiresOn.Value.Date + (ExpiresOnTimespan.HasValue ? ExpiresOnTimespan.Value : TimeSpan.Zero);

newSecret.Properties.ContentType = KeyVaultKeyModel.;

var newVersion = await _vaultService.CreateSecret(newSecret, KeyVaultKeyModel.VaultUri);
var properties = (await _vaultService.GetSecretProperties(newVersion.Properties.VaultUri, newVersion.Name)).First();
//KeyVaultKeyModel = properties;
}

partial void OnKeyVaultKeyModelChanging(KeyProperties model)
{
ExpiresOnTimespan = model is not null && model.ExpiresOn.HasValue ? model?.ExpiresOn.Value.LocalDateTime.TimeOfDay : null;
NotBeforeTimespan = model is not null && model.NotBefore.HasValue ? model?.NotBefore.Value.LocalDateTime.TimeOfDay : null;
}
}
2 changes: 1 addition & 1 deletion KeyVaultExplorer/Views/Pages/PropertiesPage.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@
BorderThickness="1"
CornerRadius="4">
<ScrollViewer VerticalScrollBarVisibility="Visible">
<StackPanel VerticalAlignment="Stretch">
<StackPanel VerticalAlignment="Stretch">
<TextBlock Text="Properties" Theme="{StaticResource BodyStrongTextBlockStyle}" />
<Grid
HorizontalAlignment="Stretch"
Expand Down

0 comments on commit 8c64fa8

Please sign in to comment.