我正在开发一个应用程序,我想在其中存储将由守护程序服务使用的服务帐户的用户名和密码。
这个想法是为应用程序管理员提供一个仪表板,他/她可以在其中输入服务帐户的凭据,然后可以将其存储在安全的地方。
我可以考虑将其存储在 Azure Vault 之类的安全地方,并在需要时从那里获取。但是,密钥和机密是 Azure Vault 中的不同实体。我不能将它们作为组合存储在某个地方。
有没有人这样做过?或者有没有更好的替代方法来在 Azure 中存储凭据?
我正在开发一个应用程序,我想在其中存储将由守护程序服务使用的服务帐户的用户名和密码。
这个想法是为应用程序管理员提供一个仪表板,他/她可以在其中输入服务帐户的凭据,然后可以将其存储在安全的地方。
我可以考虑将其存储在 Azure Vault 之类的安全地方,并在需要时从那里获取。但是,密钥和机密是 Azure Vault 中的不同实体。我不能将它们作为组合存储在某个地方。
有没有人这样做过?或者有没有更好的替代方法来在 Azure 中存储凭据?
您可以使用 Azure blob 存储用于加密静态数据的技术(信封方法): https ://docs.microsoft.com/en-us/azure/storage/storage-client-side-encryption
KeyVault 具有包装/解包(加密/解密)对称密钥的能力,因此您可以安全地将它们与加密数据一起存储。
以下是一般步骤:
您将需要这些 nuget 包:
Install-Package Microsoft.Azure.KeyVault
Install-Package Microsoft.Azure.KeyVault.Extensions
Install-Package Microsoft.IdentityModel.Clients.ActiveDirectory -Version 2.16.204221202
获取 KeyVaultKeyResolver 的引用
KeyVaultKeyResolver cloudResolver = new KeyVaultKeyResolver(Utils.GetToken);
// Example GetToken implementation
public class Utils {
// Retrive JWT token to be used for KeyVault access.
internal async static Task<string> GetToken(string authority, string resource, string scope)
{
var authContext = new AuthenticationContext(authority);
// Could use pfx instead
ClientCredential clientCred = new ClientCredential(
ConfigurationManager.AppSettings["clientId"],
ConfigurationManager.AppSettings["clientSecret"]);
AuthenticationResult result = await authContext.AcquireTokenAsync(resource, clientCred);
if (result == null)
throw new InvalidOperationException("Failed to obtain the JWT token.");
return result.AccessToken;
}
}
拥有 KeyResolver 后,您可以获得一个 IKey 来包装/解包您的 AES 对称密钥,如下所示...
包装/加密 AES 密钥
keyID 是来自 Key Vault 的 URI,aesKey 是要加密的 AES 密钥的字节 []:
// Resolve an IKey by Key ID from URI in KeyVault
var keyEncryptionKey = cloudResolver.ResolveKeyAsync(keyId, CancellationToken.None).GetAwaiter().GetResult();
// Take our gen'ed AES Key and wrap (encrypt) it.
Tuple<byte[], string> wrappedKey = keyEncryptionKey.WrapKeyAsync(aeskey, null /* algorithm */, CancellationToken.None).GetAwaiter().GetResult();
Tuple 中的 byte[] 包含对称密钥的加密字节和使用的算法的名称。将这些与您的密文一起保存为元数据。
解包/解密 AES 密钥
使用相同的密钥调用(密钥版本很重要),algoName 是用于包装密钥的算法的名称(例如“RSA-OAEP”)。
// Retrieve the IKey by Key ID
// Unwrap Key
byte[] aesKey = rsa.UnwrapKeyAsync(wrappedKeyBytes, algoName, CancellationToken.None).GetAwaiter().GetResult();
其他需要考虑的细节是密钥备份/恢复和密钥轮换。