diff --git a/sdk/dotNet/SecretManagement.Keeper/SecretManagement.Keeper.Extension/SecretManagement.Keeper.Extension.psd1 b/sdk/dotNet/SecretManagement.Keeper/SecretManagement.Keeper.Extension/SecretManagement.Keeper.Extension.psd1 index b469eb72..5aaf1f53 100644 --- a/sdk/dotNet/SecretManagement.Keeper/SecretManagement.Keeper.Extension/SecretManagement.Keeper.Extension.psd1 +++ b/sdk/dotNet/SecretManagement.Keeper/SecretManagement.Keeper.Extension/SecretManagement.Keeper.Extension.psd1 @@ -1,5 +1,5 @@ @{ - ModuleVersion = '16.0.5' + ModuleVersion = '16.2.0' RootModule = 'SecretManagement.Keeper.Extension.psm1' RequiredAssemblies = '../SecretManagement.Keeper.dll' CompatiblePSEditions = @('Core') diff --git a/sdk/dotNet/SecretManagement.Keeper/SecretManagement.Keeper.csproj b/sdk/dotNet/SecretManagement.Keeper/SecretManagement.Keeper.csproj index df45d36e..ec8c79c2 100644 --- a/sdk/dotNet/SecretManagement.Keeper/SecretManagement.Keeper.csproj +++ b/sdk/dotNet/SecretManagement.Keeper/SecretManagement.Keeper.csproj @@ -4,8 +4,8 @@ netstandard2.0 SecretManagement.Keeper true - 16.0.5 - 16.0.5 + 16.2.0 + 16.2.0 diff --git a/sdk/dotNet/SecretManagement.Keeper/SecretManagement.Keeper.psd1 b/sdk/dotNet/SecretManagement.Keeper/SecretManagement.Keeper.psd1 index 4f9df8ce..05d112da 100644 --- a/sdk/dotNet/SecretManagement.Keeper/SecretManagement.Keeper.psd1 +++ b/sdk/dotNet/SecretManagement.Keeper/SecretManagement.Keeper.psd1 @@ -1,5 +1,5 @@ @{ - ModuleVersion = '16.0.5' + ModuleVersion = '16.2.0' CompatiblePSEditions = @('Core') GUID = '20ab89cb-f0dd-4e8e-b276-f3a7708c1eb2' Author = 'Sergey Aldoukhov' diff --git a/sdk/dotNet/SecretManagement.Keeper/SecretManagement.Keeper.psm1 b/sdk/dotNet/SecretManagement.Keeper/SecretManagement.Keeper.psm1 index ecb3c9ab..3685291c 100644 --- a/sdk/dotNet/SecretManagement.Keeper/SecretManagement.Keeper.psm1 +++ b/sdk/dotNet/SecretManagement.Keeper/SecretManagement.Keeper.psm1 @@ -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 @@ -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 diff --git a/sdk/dotNet/SecretManagement.Keeper/src/SecretManagement.cs b/sdk/dotNet/SecretManagement.Keeper/src/SecretManagement.cs index 23e05f09..6f183b98 100644 --- a/sdk/dotNet/SecretManagement.Keeper/src/SecretManagement.cs +++ b/sdk/dotNet/SecretManagement.Keeper/src/SecretManagement.cs @@ -13,10 +13,25 @@ namespace SecretManagement.Keeper { public static class Client { - public static async Task GetVaultConfig(string oneTimeToken) + public static async Task 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 GetVaultConfigFromConfigString(string config) + { + var storage = new InMemoryStorage(config); try { await SecretsManagerClient.GetSecrets(new SecretsManagerOptions(storage)); @@ -73,6 +88,9 @@ public static async Task GetSecret(string name, Hashtable config) var dict = new Dictionary(); if (parts.Length > 1) { + if (parts[1] == "Notes") { + return found.Data.notes; + } if (parts[1].StartsWith("Files[", true, CultureInfo.InvariantCulture)) { if (found.Files == null) @@ -86,11 +104,13 @@ public static async Task 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) { @@ -100,6 +120,10 @@ public static async Task 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(); @@ -108,6 +132,19 @@ public static async Task GetSecret(string name, Hashtable config) return new Hashtable(dict); } + private class RecordComparer : IEqualityComparer + { + 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 GetSecretsInfo(string filter, Hashtable config) { var (records, _) = await GetKeeperSecrets(config); @@ -115,7 +152,8 @@ public static async Task GetSecretsInfo(string filter, Hashtable confi 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(); }