/// <summary> /// ProcessRecord /// </summary> protected override void ProcessRecord() { Data.KeyVault KeyVault = KeyVaultHelper.GetItemThrow(null, VaultName, true); if (!(String.IsNullOrEmpty(KeyVault.Thumbprint))) { WriteError( (new PSAdminException(PSAdminExceptionType.ParameterDefined, String.Format("in KeyVault '{0}' with the name of '{1}'", VaultName, Thumbprint))).GetErrorRecord() ); return; } Data.KeyVaultCertificate[] SearchCertificate = KeyVaultCertificateHelper.GetItemsThrow(null, KeyVault.VaultName, null, Thumbprint, null, false, true); X509Certificate2 x509 = (X509Certificate2)SearchCertificate[0].Certificate; if ((x509.HasPrivateKey == false) || (x509.PrivateKey == null)) { WriteError( (new PSAdminException(PSAdminExceptionType.CertificatePrivateKey, Thumbprint)).GetErrorRecord() ); return; } Hashtable filter = new Hashtable { { "Id", KeyVault.Id }, { "VaultName", KeyVault.VaultName }, }; Hashtable row = new Hashtable { { "Thumbprint", x509.Thumbprint }, { "VaultKey", ((RSACryptoServiceProvider)x509.PublicKey.Key).Encrypt(KeyVault.VaultKey, true) } }; bool issuccessful = KeyVaultHelper.SetItemsThrow(row, filter, true); }
/// <summary> /// Process Record /// </summary> protected override void ProcessRecord() { Data.KeyVault KeyVault = KeyVaultHelper.GetItemThrow(null, VaultName, true); Data.KeyVaultCertificate[] SearchCertificate = KeyVaultCertificateHelper.GetItemsThrow(null, VaultName, Name, null, null, false, true); // Note: This will only ever return one item foreach (Data.KeyVaultCertificate Certificate in SearchCertificate) { X509Certificate2 x509 = (X509Certificate2)Certificate.Certificate; byte[] CertificateByteArray = x509.Export(X509ContentType.Pkcs12, Password); x509.Dispose(); switch (ParameterSetName) { case ImportFromFileParameterSetName: File.WriteAllBytes(FileName, CertificateByteArray); break; case ImportFromStringParameterSetName: WriteObject( Convert.ToBase64String(CertificateByteArray) ); break; default: WriteError( (new PSAdminException(PSAdminExceptionType.ParameterSetNotFound, Name, "Name")).GetErrorRecord() ); return; } } }
public static bool RemoveItem(string Id, string VaultName, bool Exact) { Data.KeyVault result = GetItem(Id, VaultName, Exact); if (result == null) { return false; } return RemoveItems(Id, VaultName, Exact); }
internal static bool SetItemThrow(string Id, string VaultName, string Location, string VaultURI, bool SoftDeleteEnabled, string[] Tags, bool Exact) { Data.KeyVault result = GetItemThrow(Id, VaultName, Exact); if (result == null) { return false; } return SetItemsThrow(Id, VaultName, Location, VaultURI, SoftDeleteEnabled, Tags, Exact); }
/// <summary> /// Process Record /// </summary> protected override void ProcessRecord() { String Id = Guid.NewGuid().ToString().Replace("-", ""); KeyVaultHelper.NewItemThrow(Id, VaultName, Location, VaultURI, SoftDeleteEnabled, Tags); if (Passthru) { Data.KeyVault result = KeyVaultHelper.GetItem(null, VaultName, true); WriteObject(result); } }
internal static byte[] GetVaultKey(string VaultName) { Data.KeyVault KeyVault = KeyVaultHelper.GetItemThrow(null, VaultName, true); if ( String.IsNullOrEmpty(KeyVault.Thumbprint) ) return KeyVault.VaultKey; Data.KeyVaultCertificate Certificate = KeyVaultCertificateHelper.GetItemThrow(null, VaultName, null, KeyVault.Thumbprint, null, true, true); // Decrypt the Key X509Certificate2 x509 = (X509Certificate2)Certificate.Certificate; if ((x509.HasPrivateKey == false) || (x509.PrivateKey == null)) { throw new InvalidOperationException("Certificate does not contain PrivateKey"); } return ((RSACryptoServiceProvider)x509.PrivateKey).Decrypt(KeyVault.VaultKey, true); }
public static bool SetItems(string Id, string VaultName, string Name, string Version, string Enabled, Nullable <DateTime> Expires, Nullable <DateTime> NotBefore, string ContentType, string[] Tags, string SecretValue, bool Exact) { Data.KeyVault KeyVault = KeyVaultHelper.GetItem(null, VaultName, true); if (KeyVault == null) { return(false); } // Build the Key byte[] Key = KeyVaultHelper.GetVaultKey(KeyVault.VaultName); byte[] SecretData = null; if (!String.IsNullOrEmpty(SecretValue)) { SecretData = Crypto.ConvertToKeyVaultSecret(SecretValue, Key); } Hashtable filter = new Hashtable { { "Id", Id }, { "VaultName", VaultName }, { "Name", Name } }; Hashtable table = new Hashtable { { "Version", Version }, { "Enabled", Enabled }, { "Expires", Expires }, { "NotBefore", NotBefore }, { "ContentType", ContentType }, { "Tags", Tags }, { "SecretValue", SecretData } }; if (Tags != null) { table.Add("Tags", String.Join(";", Tags)); } return(SQLiteDB.UpdateRow(TableName, table, filter, Exact)); }
/// <summary> /// Process Record /// </summary> protected override void ProcessRecord() { Data.KeyVault KeyVault = KeyVaultHelper.GetItem(null, VaultName, true); Data.Computer[] searchcomputer = GetPSAdminComputer.Call(null, VaultName, ComputerName, null, true); if (searchcomputer.Length > 0) { WriteError( (new PSAdminException(PSAdminExceptionType.ItemExists, ComputerName, "ComputerName")).GetErrorRecord() ); return; } Hashtable item = new Hashtable { { "Id", Guid.NewGuid().ToString().Replace("-", "") }, { "VaultName", VaultName }, { "ComputerName", ComputerName }, { "Description", Description }, { "Created", DateTime.UtcNow }, { "Updated", DateTime.UtcNow }, { "LastOnline", LastOnline }, { "AssetNumber", AssetNumber }, { "SerialNumber", SerialNumber }, { "DeviceSKU", DeviceSKU }, { "OSVersion", OSVersion }, { "Location", Location }, { "Building", Building }, { "Room", Room }, { "Rack", Rack }, { "Slot", Slot }, { "VMHost", VMHost }, { "BuildDefinition", BuildDefinition }, { "BuildState", BuildState }, { "BuildDesiredVersion", BuildDesiredVersion }, { "BuildActualVersion", BuildActualVersion }, { "Domain", Domain }, { "Forest", Forest }, { "PublicFQDN", PublicFQDN }, { "LoadBalancer", LoadBalancer }, { "PublicIP", PublicIP }, { "LocalIP", LocalIP }, { "MACAddress", MACAddress }, { "Notes", Notes } }; if (Tags != null) { item.Add("Tags", String.Join(";", Tags)); } bool issuccessful = Call(item); if (!issuccessful) { WriteError( (new PSAdminException(PSAdminExceptionType.RowCreate)).GetErrorRecord() ); } if (Passthru) { Data.Computer[] results = GetPSAdminComputer.Call(null, VaultName, ComputerName, null, true); // Unroll the object foreach (Data.Computer result in results) { WriteObject(result); } } }