Skip to content

Commit

Permalink
Powershell multiple fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
saldoukhov committed Feb 25, 2022
1 parent e5d76f6 commit 4645630
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
@{
ModuleVersion = '16.0.5'
ModuleVersion = '16.2.0'
RootModule = 'SecretManagement.Keeper.Extension.psm1'
RequiredAssemblies = '../SecretManagement.Keeper.dll'
CompatiblePSEditions = @('Core')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
<TargetFramework>netstandard2.0</TargetFramework>
<AssemblyName>SecretManagement.Keeper</AssemblyName>
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
<AssemblyVersion>16.0.5</AssemblyVersion>
<FileVersion>16.0.5</FileVersion>
<AssemblyVersion>16.2.0</AssemblyVersion>
<FileVersion>16.2.0</FileVersion>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
@{
ModuleVersion = '16.0.5'
ModuleVersion = '16.2.0'
CompatiblePSEditions = @('Core')
GUID = '20ab89cb-f0dd-4e8e-b276-f3a7708c1eb2'
Author = 'Sergey Aldoukhov'
Expand Down
19 changes: 16 additions & 3 deletions sdk/dotNet/SecretManagement.Keeper/SecretManagement.Keeper.psm1
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
function Register-KeeperVault {
[CmdletBinding()]
[CmdletBinding(DefaultParameterSetName = 'Token')]
param (
[Parameter(Mandatory = $true)]
[string] $Name,
[Parameter(Mandatory = $true)]
[Parameter(Mandatory = $true, ParameterSetName = 'Token')]
[string] $OneTimeToken,
[Parameter(Mandatory = $true, ParameterSetName = 'Config')]
[string] $Config,
[string] $LocalVaultName
)
if ($PSVersionTable.PSVersion.Major -lt 6) {
Write-Error "Keeper Secrets Manager: this version of Powershell ($($PSVersionTable.PSVersion.ToString())) is not supported"
return
}
$vaults = Microsoft.Powershell.SecretManagement\Get-SecretVault
if ($LocalVaultName) {
$localVaultModuleName = $vaults.Where( { $_.Name -eq $LocalVaultName } ) | Select-Object -ExpandProperty ModuleName
Expand All @@ -26,7 +32,14 @@ function Register-KeeperVault {
$configSecretName = 'KeeperVault.' + $Name
Write-Host "Storing Keeper Vault config $($configSecretName) in $($localVaultModuleName) Vault named $($LocalVaultName)"
$moduleInstance = Import-Module -Name $localVaultModuleName -PassThru -ErrorAction Stop
$result = [SecretManagement.Keeper.Client]::GetVaultConfig($OneTimeToken).GetAwaiter().GetResult()
switch ($PSCmdlet.ParameterSetName) {
'Token' {
$result = [SecretManagement.Keeper.Client]::GetVaultConfigFromToken($OneTimeToken).GetAwaiter().GetResult()
}
'Config' {
$result = [SecretManagement.Keeper.Client]::GetVaultConfigFromConfigString($Config).GetAwaiter().GetResult()
}
}
if ($result.IsFailure) {
Write-Error $result.ErrorMessage
return
Expand Down
48 changes: 43 additions & 5 deletions sdk/dotNet/SecretManagement.Keeper/src/SecretManagement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,25 @@ namespace SecretManagement.Keeper
{
public static class Client
{
public static async Task<KeeperResult> GetVaultConfig(string oneTimeToken)
public static async Task<KeeperResult> GetVaultConfigFromToken(string oneTimeToken)
{
var storage = new InMemoryStorage();
SecretsManagerClient.InitializeStorage(storage, oneTimeToken, "keepersecurity.com");
SecretsManagerClient.InitializeStorage(storage, oneTimeToken);
try
{
await SecretsManagerClient.GetSecrets(new SecretsManagerOptions(storage));
}
catch (Exception e)
{
return KeeperResult.Error(e.Message);
}

return KeeperResult.Ok(storage.AsHashTable());
}

public static async Task<KeeperResult> GetVaultConfigFromConfigString(string config)
{
var storage = new InMemoryStorage(config);
try
{
await SecretsManagerClient.GetSecrets(new SecretsManagerOptions(storage));
Expand Down Expand Up @@ -73,6 +88,9 @@ public static async Task<object> GetSecret(string name, Hashtable config)
var dict = new Dictionary<string, object>();
if (parts.Length > 1)
{
if (parts[1] == "Notes") {
return found.Data.notes;
}
if (parts[1].StartsWith("Files[", true, CultureInfo.InvariantCulture))
{
if (found.Files == null)
Expand All @@ -86,11 +104,13 @@ public static async Task<object> GetSecret(string name, Hashtable config)
: SecretsManagerClient.DownloadFile(file);
}

var field = found.Data.fields.FirstOrDefault(x => (x.label ?? x.type).Equals(parts[1], StringComparison.OrdinalIgnoreCase));
var field = found.Data.fields
.Concat(found.Data.custom ?? new KeeperRecordField[] { })
.FirstOrDefault(x => (x.label ?? x.type).Equals(parts[1], StringComparison.OrdinalIgnoreCase));
return field?.value[0].ToString();
}

foreach (var field in found.Data.fields)
foreach (var field in found.Data.fields.Concat(found.Data.custom ?? new KeeperRecordField[] { }))
{
if (field.type == "fileRef" || field.value.Length == 0)
{
Expand All @@ -100,6 +120,10 @@ public static async Task<object> GetSecret(string name, Hashtable config)
dict[field.label ?? field.type] = field.value[0].ToString();
}

if (found.Data.notes != null) {
dict["Notes"] = found.Data.notes;
}

if (found.Files != null && found.Files.Length > 0)
{
dict["Files"] = found.Files.Select(x => x.Data.title).ToArray();
Expand All @@ -108,14 +132,28 @@ public static async Task<object> GetSecret(string name, Hashtable config)
return new Hashtable(dict);
}

private class RecordComparer : IEqualityComparer<KeeperRecord>
{
public bool Equals(KeeperRecord x, KeeperRecord y)
{
return x.RecordUid == y.RecordUid;
}

public int GetHashCode(KeeperRecord obj)
{
return obj.RecordUid.GetHashCode();
}
}

public static async Task<string[]> GetSecretsInfo(string filter, Hashtable config)
{
var (records, _) = await GetKeeperSecrets(config);
var filterPattern = new WildcardPattern(
pattern: filter,
options: WildcardOptions.IgnoreCase);
return records
.Where(x => filterPattern.IsMatch(x.Data.title))
.Where(x => x.RecordUid == filter || filterPattern.IsMatch(x.Data.title))
.Distinct(new RecordComparer())
.Select(x => $"{x.RecordUid} {x.Data.title}").ToArray();
}

Expand Down

0 comments on commit 4645630

Please sign in to comment.