Skip to content

Commit

Permalink
#64 wip, adding save functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
cricketthomas committed Jun 29, 2024
1 parent fb15fdd commit 4c9b6a5
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 19 deletions.
17 changes: 17 additions & 0 deletions KeyVaultExplorer/Exceptions/KvExceptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,21 @@ public KeyVaultItemNotFoundException(string message, Exception inner)
: base(message, inner)
{
}
}

public class KeyVaultItemNotFailedToUpdate : Exception
{
public KeyVaultItemNotFailedToUpdate()
{
}

public KeyVaultItemNotFailedToUpdate(string message)
: base(message)
{
}

public KeyVaultItemNotFailedToUpdate(string message, Exception inner)
: base(message, inner)
{
}
}
32 changes: 24 additions & 8 deletions KeyVaultExplorer/Services/VaultService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -312,20 +312,36 @@ public async IAsyncEnumerable<KeyVaultResource> GetWithKeyVaultsBySubscriptionAs
}
}

public async Task CreateOrUpdateSecret(SecretProperties secretProperties, KeyVaultSecret keyVaultSecret, Uri KeyVaultUri)
public async Task CreateSecret(KeyVaultSecret keyVaultSecret, Uri KeyVaultUri)
{
try
{
var token = new CustomTokenCredential(await _authService.GetAzureKeyVaultTokenSilent());
SecretClient client = new SecretClient(KeyVaultUri, token);
await client.SetSecretAsync(keyVaultSecret);
}
catch (Exception ex)
{
throw new KeyVaultItemNotFoundException(ex.Message, ex);
}
}

public async Task UpdateSecret(SecretProperties secretProperties, Uri KeyVaultUri)
{
var token = new CustomTokenCredential(await _authService.GetAzureKeyVaultTokenSilent());
SecretClient client;
if (secretProperties is not null)

try
{
client = new SecretClient(KeyVaultUri, token);
if (secretProperties is not null)
{
SecretClient client = new SecretClient(KeyVaultUri, token);

await client.UpdateSecretPropertiesAsync(secretProperties);
await client.UpdateSecretPropertiesAsync(secretProperties);
}
}
else
catch (Exception ex)
{
client = new SecretClient(KeyVaultUri, token);
await client.SetSecretAsync(keyVaultSecret);
throw new KeyVaultItemNotFoundException(ex.Message, ex);
}
}
}
15 changes: 8 additions & 7 deletions KeyVaultExplorer/ViewModels/CreateNewSecretVersionViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ public partial class CreateNewSecretVersionViewModel : ViewModelBase
[ObservableProperty]
private bool isEdit = false;


[ObservableProperty]
private string secretValue;

Expand All @@ -30,16 +29,13 @@ public partial class CreateNewSecretVersionViewModel : ViewModelBase
[NotifyPropertyChangedFor(nameof(NotBeforeTimespan))]
private SecretProperties keyVaultSecretModel;



public TimeSpan? ExpiresOnTimespan => KeyVaultSecretModel?.ExpiresOn.Value.LocalDateTime.TimeOfDay;

public TimeSpan? NotBeforeTimespan => KeyVaultSecretModel.NotBefore.HasValue ? KeyVaultSecretModel?.NotBefore.Value.LocalDateTime.TimeOfDay : null;

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


private readonly AuthService _authService;
private readonly VaultService _vaultService;
private NotificationViewModel _notificationViewModel;
Expand All @@ -51,9 +47,14 @@ public CreateNewSecretVersionViewModel()
_notificationViewModel = Defaults.Locator.GetRequiredService<NotificationViewModel>();
}

[RelayCommand]
public async Task EditDetails()
{
var properties = KeyVaultSecretModel;
properties.NotBefore = new DateTimeOffset(properties.NotBefore.Value.Date, NotBeforeTimespan.Value); ;
properties.ExpiresOn = new DateTimeOffset(properties.ExpiresOn.Value.Date, ExpiresOnTimespan.Value); ;





await _vaultService.UpdateSecret(KeyVaultSecretModel, KeyVaultSecretModel.VaultUri);
}
}
5 changes: 2 additions & 3 deletions KeyVaultExplorer/ViewModels/PropertiesPageViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ public PropertiesPageViewModel(KeyVaultContentsAmalgamation model)
_storageService = Defaults.Locator.GetRequiredService<IStorageProvider>();

OpenedItem = model;
IsManaged = false;
Dispatcher.UIThread.InvokeAsync(async () =>
{
await GetPropertiesForKeyVaultValue(model);
Expand Down Expand Up @@ -186,7 +185,7 @@ private async Task NewVersion()
// Pass the dialog if you need to hide it from the ViewModel.
var viewModel = new CreateNewSecretVersionViewModel();
await ShouldShowValue(true);
viewModel.KeyVaultSecretModel = OpenedItem.SecretProperties;
viewModel.KeyVaultSecretModel = SecretPropertiesList.First();

// In our case the Content is a UserControl, but can be anything.
dialog.Content = new CreateNewSecretVersion()
Expand Down Expand Up @@ -217,7 +216,7 @@ private async Task EditVersion()
// Pass the dialog if you need to hide it from the ViewModel.
var viewModel = new CreateNewSecretVersionViewModel();
await ShouldShowValue(true);
viewModel.KeyVaultSecretModel = OpenedItem.SecretProperties;
viewModel.KeyVaultSecretModel = SecretPropertiesList.First();
viewModel.IsEdit = true;

// In our case the Content is a UserControl, but can be anything.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@

</Grid>


<Button Command="{Binding EditDetailsCommand}" Content="Apply" />
</StackPanel>
</ScrollViewer>

Expand Down

0 comments on commit 4c9b6a5

Please sign in to comment.