Skip to content

Commit

Permalink
#64 add ability to create new secrets
Browse files Browse the repository at this point in the history
  • Loading branch information
cricketthomas committed Jul 27, 2024
1 parent 7afc9f1 commit ee76df3
Show file tree
Hide file tree
Showing 24 changed files with 606 additions and 325 deletions.
Binary file added KeyVaultExplorer/Assets/Add.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added KeyVaultExplorer/Assets/NewKey.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 3 additions & 3 deletions KeyVaultExplorer/Database/KvExplorerDb.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ CREATE INDEX IF NOT EXISTS IX_QuickAccess_KeyVaultId ON QuickAccess (
await createTableCommand.ExecuteNonQueryAsync();
}

public IEnumerable<QuickAccess> GetQuickAccessItems()
public List<QuickAccess> GetQuickAccessItems()
{
var command = _connection.CreateCommand();
command.CommandText = "SELECT Id, Name, VaultUri, KeyVaultId, SubscriptionDisplayName, SubscriptionId, TenantId, Location FROM QuickAccess;";
Expand Down Expand Up @@ -128,7 +128,7 @@ public async IAsyncEnumerable<QuickAccess> GetQuickAccessItemsAsyncEnumerable()
}
}

public async Task<bool> QuickAccessItemByKeyVaultIdExists(string keyVaultId)
public async Task<bool> QuickAccessItemByKeyVaultIdExists(string? keyVaultId)
{
var command = _connection.CreateCommand();
command.CommandText = "SELECT 1 FROM QuickAccess WHERE KeyVaultId = @KeyVaultId LIMIT 1;";
Expand Down Expand Up @@ -193,7 +193,7 @@ public async Task<AppSettings> GetToggleSettings()
return settings;
}

public async Task<IEnumerable<Subscriptions>> GetStoredSubscriptions()
public async Task<List<Subscriptions>> GetStoredSubscriptions()
{
var command = _connection.CreateCommand();
command.CommandText = "SELECT DisplayName, SubscriptionId, TenantId FROM Subscriptions;";
Expand Down
6 changes: 4 additions & 2 deletions KeyVaultExplorer/KeyVaultExplorer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@


<ItemGroup>
<None Remove="Assets\Add.png" />
<None Remove="Assets\AppIcon.ico" />
<None Remove="Assets\Certificate.png" />
<None Remove="Assets\Certificate.svg" />
Expand All @@ -40,6 +41,7 @@
<None Remove="Assets\kv-gray.png" />
<None Remove="Assets\Lock.png" />
<None Remove="Assets\Lock.svg" />
<None Remove="Assets\NewKey.png" />
<None Remove="Assets\Pin.png" />
<None Remove="Assets\Pin.svg" />
<None Remove="Assets\Refresh.svg" />
Expand All @@ -48,12 +50,12 @@


<ItemGroup>
<PackageReference Include="Avalonia.Controls.ItemsRepeater" Version="11.1.0" />
<PackageReference Include="Avalonia.Controls.ItemsRepeater" Version="11.1.1" />
<PackageReference Include="Avalonia.Svg.Skia" Version="11.1.0" />
<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" />
<PackageReference Condition="'$(Configuration)' == 'Debug'" Include="Avalonia.Diagnostics" Version="11.1.1" />
<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" />
Expand Down
2 changes: 2 additions & 0 deletions KeyVaultExplorer/Models/SubscriptionDataItems.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ public partial class SubscriptionDataItem : ObservableObject
{
public SubscriptionData Data { get; set; } = null!;

public SubscriptionResource Resource { get; set; } = null!;

[ObservableProperty]
private bool isPinned;

Expand Down
64 changes: 61 additions & 3 deletions KeyVaultExplorer/Resources/Resources.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@



<SolidColorBrush x:Key="DynamicActiveBackgroundFAColor" Color="{DynamicResource SolidBackgroundFillColorBase}" />
<SolidColorBrush x:Key="DynamicActiveBackgroundFAColor" Color="{DynamicResource SolidBackgroundFillColorBase}" />
<!--<StaticResource x:Key="TabViewItemHeaderBackgroundSelected" ResourceKey="SelectedTabViewColorKv" />
<SolidColorBrush x:Key="SelectedTabViewColorKv" Color="Transparent" />-->

Expand Down Expand Up @@ -102,7 +102,7 @@

<Grid>
<!-- Blurred border -->
<Border Margin="25" BoxShadow="0 6 10 0 #4F000000">
<Border Margin="25" BoxShadow="0 4 10 0 #44000000">
<Border.Effect>
<BlurEffect Radius="25" />
</Border.Effect>
Expand All @@ -122,7 +122,7 @@
Background="{TemplateBinding Background}"
BackgroundSizing="CenterBorder"
BorderBrush="{DynamicResource SurfaceStrokeColorFlyoutBrush}"
BorderThickness="2"
BorderThickness="1"
CornerRadius="{TemplateBinding CornerRadius}">
<Border.RenderTransform>
<TranslateTransform X="-10" Y="-20" />
Expand All @@ -146,6 +146,64 @@



<ControlTheme x:Key="{x:Type MenuFlyoutPresenter}" TargetType="MenuFlyoutPresenter">
<Setter Property="Background" Value="{DynamicResource MenuFlyoutPresenterBackground}" />
<Setter Property="BorderBrush" Value="{DynamicResource MenuFlyoutPresenterBorderBrush}" />
<Setter Property="BorderThickness" Value="{DynamicResource MenuFlyoutPresenterBorderThemeThickness}" />
<Setter Property="Padding" Value="{DynamicResource MenuFlyoutPresenterThemePadding}" />
<Setter Property="MaxWidth" Value="{DynamicResource FlyoutThemeMaxWidth}" />
<Setter Property="MinHeight" Value="{DynamicResource MenuFlyoutThemeMinHeight}" />
<Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto" />
<Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto" />
<Setter Property="CornerRadius" Value="{DynamicResource OverlayCornerRadius}" />
<Setter Property="Template">
<ControlTemplate>

<Grid>
<!-- Blurred border -->
<Border Margin="25" BoxShadow="0 4 10 0 #44000000">
<Border.Effect>
<BlurEffect Radius="25" />
</Border.Effect>
<Border.RenderTransform>
<TranslateTransform X="-15" Y="-25" />
</Border.RenderTransform>
</Border>

<!-- Content Y="-20" -->

<!-- BoxShadow="0 2 10 0 #56000000" -->
<Border
Name="LayoutRoot"
Margin="25"
Padding="{DynamicResource FlyoutBorderThemePadding}"
VerticalAlignment="Top"
Background="{TemplateBinding Background}"
BackgroundSizing="CenterBorder"
BorderBrush="{DynamicResource SurfaceStrokeColorFlyoutBrush}"
BorderThickness="1"
CornerRadius="{TemplateBinding CornerRadius}">
<Border.RenderTransform>
<TranslateTransform X="-10" Y="-25" />
</Border.RenderTransform>
<ScrollViewer
Margin="{TemplateBinding Padding}"
HorizontalScrollBarVisibility="{TemplateBinding ScrollViewer.HorizontalScrollBarVisibility}"
VerticalScrollBarVisibility="{TemplateBinding ScrollViewer.VerticalScrollBarVisibility}">
<ItemsPresenter
Name="PART_ItemsPresenter"
Grid.IsSharedSizeScope="True"
ItemsPanel="{TemplateBinding ItemsPanel}"
KeyboardNavigation.TabNavigation="Continue" />
</ScrollViewer>
</Border>
</Grid>

</ControlTemplate>
</Setter>
</ControlTheme>




</ResourceDictionary>
64 changes: 63 additions & 1 deletion KeyVaultExplorer/Resources/Styles.axaml
Original file line number Diff line number Diff line change
@@ -1,9 +1,40 @@
<Styles
xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:effects="using:Avalonia.Media"
xmlns:ui="using:FluentAvalonia.UI.Controls"
xmlns:uip="using:FluentAvalonia.UI.Controls.Primitives">
<Design.PreviewWith />
<Design.PreviewWith>

<StackPanel Width="310" Height="300">


<DropDownButton
Name="CreateDropDownButton"
Margin="50"
Content="Create"
FontSize="{DynamicResource FontSizeSmall}"
ToolTip.Tip="Create new item"
ToolTip.VerticalOffset="10">
<DropDownButton.Flyout>
<MenuFlyout>
<MenuItem Header="Create Secret.." />
<MenuItem Header="Create Key.." IsEnabled="False" />
<MenuItem Header="Create Certificate.." IsEnabled="False" />
</MenuFlyout>
</DropDownButton.Flyout>
</DropDownButton>
</StackPanel>
</Design.PreviewWith>
<!--
<Style Selector="MenuFlyoutPresenter">
<Setter Property="BorderBrush" Value="Red" />
<Setter Property="BorderThickness" Value="1" />
<Setter Property="BorderBrush" />
<Setter Property="BoxShadow" Value="0 2 10 0 #34000000" />
</Style>-->



<Style Selector="TextBox.IsSmall">
<Setter Property="MinHeight" Value="30" />
Expand All @@ -13,6 +44,37 @@
<Setter Property="Padding" Value="8,2,2,2" />
</Style>

<!-- <Style Selector="MenuFlyoutPresenter">
<Setter Property="Template">
<ControlTemplate>
<Border
Name="LayoutRoot"
Margin="25"
Padding="4,2"
Background="{DynamicResource SolidBackgroundFillColorTertiaryBrush}"
BorderBrush="{DynamicResource SolidBackgroundFillColorTertiaryBrush}"
BoxShadow="0 6 10 0 #4F000000"
CornerRadius="5">
<Border.Effect>
<BlurEffect Radius="25" />
</Border.Effect>
<Border.RenderTransform>
<TranslateTransform X="-10" Y="-25" />
</Border.RenderTransform>
<ScrollViewer>
<ItemsPresenter Name="PART_ItemsPresenter" />
</ScrollViewer>
</Border>
</ControlTemplate>
</Setter>
</Style>-->

<!-- <Style Selector="DropDownButton /template/ Border#LayoutRoot">
<Setter Property="Margin" Value="10" />
<Setter Property="BoxShadow" Value="0 4 10 0 #34000000" />
</Style>-->

<Style Selector="TextBox.isCompact">
<Setter Property="MinHeight" Value="25" />
<Setter Property="Padding" Value="5,0,0,0" />
Expand Down
54 changes: 27 additions & 27 deletions KeyVaultExplorer/Services/VaultService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Threading;
using System.Threading.Tasks;

Expand All @@ -20,16 +21,32 @@ namespace KeyVaultExplorer.Services;

public partial class VaultService
{
#pragma warning disable IDE0290 // Use primary constructor
public VaultService(AuthService authService, IMemoryCache memoryCache, KvExplorerDb dbContext)
#pragma warning restore IDE0290 // Use primary constructor
{
_authService = authService;
_memoryCache = memoryCache;
_dbContext = dbContext;
}

public AuthService _authService { get; set; }
public IMemoryCache _memoryCache { get; set; }
public KvExplorerDb _dbContext { get; set; }
private AuthService _authService { get; set; }
private KvExplorerDb _dbContext { get; set; }
private IMemoryCache _memoryCache { get; set; }

public async Task<KeyVaultKey> CreateKey(KeyVaultKey key, Uri KeyVaultUri)
{
var token = new CustomTokenCredential(await _authService.GetAzureKeyVaultTokenSilent());
var client = new KeyClient(KeyVaultUri, token);
return await client.CreateKeyAsync(key.Name, key.KeyType);
}

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

public async IAsyncEnumerable<SubscriptionResourceWithNextPageToken> GetAllSubscriptions(CancellationToken cancellationToken = default, string continuationToken = null)
{
Expand Down Expand Up @@ -257,9 +274,9 @@ public async Task<List<SecretProperties>> GetSecretProperties(Uri keyVaultUri, s
}
}

public async Task<Dictionary<string, KeyVaultResource>> GetStoredSelectedSubscriptions(string subsriptionId)
public async Task<Dictionary<string, KeyVaultResource>> GetStoredSelectedSubscriptions(string subscriptionId)
{
var resource = new ResourceIdentifier(subsriptionId);
var resource = new ResourceIdentifier(subscriptionId);
var armClient = new ArmClient(new CustomTokenCredential(await _authService.GetAzureArmTokenSilent()));
SubscriptionResource subscription = armClient.GetSubscriptionResource(resource);

Expand Down Expand Up @@ -308,42 +325,25 @@ public async IAsyncEnumerable<SecretProperties> GetVaultAssociatedSecrets(Uri kv
}
}

public async IAsyncEnumerable<KeyVaultResource> GetWithKeyVaultsBySubscriptionAsync(KvSubscriptionModel resource)
public static async IAsyncEnumerable<KeyVaultResource> GetWithKeyVaultsBySubscriptionAsync(KvSubscriptionModel resource)
{
await foreach (var kvResource in resource.Subscription.GetKeyVaultsAsync())
{
yield return kvResource;
}
}

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

public async Task<SecretProperties> UpdateSecret(SecretProperties properties, Uri KeyVaultUri)
{
var token = new CustomTokenCredential(await _authService.GetAzureKeyVaultTokenSilent());
SecretClient client = new SecretClient(KeyVaultUri, token);
var client = new SecretClient(KeyVaultUri, token);
return await client.UpdateSecretPropertiesAsync(properties);
}



public async Task<KeyVaultKey> CreateKey(KeyVaultKey key, Uri KeyVaultUri)
{
var token = new CustomTokenCredential(await _authService.GetAzureKeyVaultTokenSilent());
var client = new KeyClient(KeyVaultUri, token);
return await client.CreateKeyAsync(key.Name, key.KeyType);
}

public async Task<KeyVaultKey> UpdateKey(KeyProperties properties, Uri KeyVaultUri)
{
var token = new CustomTokenCredential(await _authService.GetAzureKeyVaultTokenSilent());
var client = new KeyClient(KeyVaultUri, token);
return await client.UpdateKeyPropertiesAsync(properties);
}

}
Loading

0 comments on commit ee76df3

Please sign in to comment.