powershell - 安全地访问 Key Vault secret

标签 powershell security azure-keyvault azure-function-app

我在 Powershell 中编写了一个程序,该程序按计划在 Azure Functions 应用程序中运行。为了避免硬编码凭据,我创建了一个 Azure Key Vault 来存储 secret 。我在 Azure Function 中创建了托管标识,在 Azure Key Vault 中创建了 secret ,然后在 Azure Function 中创建了应用程序设置,并使用 URL 指向 Azure Key Vault 中的 secret 。该程序引用应用程序 secret (APPSETTING) 并按预期运行:

    $uSecret = $ENV:APPSETTING_SecretUsername 
    $pSecret = $ENV:APPSETTING_SecretPassword 
    $sasSecret = $ENV:APPSETTING_SecretSAS
    $securePassword = ConvertTo-SecureString -String $pSecret -AsPlainText -Force
    $UserCredential = New-Object -TypeName "System.Management.Automation.PSCredential" -ArgumentList $uSecret, $securePassword
    
    $session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $UserCredential -Authentication Basic -AllowRedirection

但是我注意到,如果我通过 Windows Powershell(以管理员身份运行)在本地计算机上运行相同的程序,并且将上述行修改如下,则该程序运行良好 - 这意味着它可以访问 Office 365 和数据湖存储:

    $uSecret = (Get-AzKeyVaultSecret -VaultName 'auditkeyvault' -Name 'SecretUsername').SecretValueText 
    $pSecret = (Get-AzKeyVaultSecret -VaultName 'auditkeyvault' -Name 'SecretPassword').SecretValueText 
    $sasSecret = (Get-AzKeyVaultSecret -VaultName 'auditkeyvault' -Name 'SecretSAS').SecretValueText 
    $securePassword = ConvertTo-SecureString -String $pSecret -AsPlainText -Force
    $UserCredential = New-Object -TypeName "System.Management.Automation.PSCredential" -ArgumentList $uSecret, $securePassword
    
    $session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $UserCredential -Authentication Basic -AllowRedirection

为什么我能够在我的计算机上本地运行它?我本以为只有 Azure Functions 应用程序能够从 Azure Key Vault 检索 secret ,并且任何其他资源(例如我的本地计算机)都会被阻止?

这不是为具有特定 URL 的 Azure Functions 创建托管标识的全部目的,以便它可以将自己标识为经过身份验证/授权的资源来访问 key 吗?然而,当我使用 (Get-AzKeyVaultSecret -VaultName 'auditkeyvault' -Name 'SecretUsername').SecretValueText 在本地运行程序时,我的程序仍然能够检索 key 并运行!

enter image description here

有人可以解释一下为什么会发生这种情况,或者我是否误解了什么?

非常感谢!

(PS。这都是在带有示例数据的试用实例上运行的,因此目前没有实际数据受到损害)

最佳答案

keystore 的目的是安全地保守您的 secret 。

任何授权凭据(通过 Keyvault 访问策略)都可以通过 REST API 访问这些 secret 。

要访问 secret ,您需要:

  • key 保管库中允许您进行足够访问的访问策略
  • 通过授权帐户进行身份验证

Get-AzKeyVaultSecret 只是检索 secret 的另一种方法。

它可以在您的计算机上运行,​​因为您的 session 仍然经过身份验证,并且您的 AzureAd 帐户具有对该 keyvault key 的读取访问权限。

您可以有效地使用任何 Az 命令,而无需每次都重新进行身份验证。 调用 Get-AzContext 获取当前上下文详细信息。

Connect-AzAccount 在以下位置自动使用时,请务必保存访问 token 和其他相关信息:C:\Users\MAK\.Azure\AzureRmContext.json

如果您首先断开连接 Disconnect-AzAccount 并尝试再次获取 key 而不重新进行身份验证,那么它将失败。

注意 如果您对 Az 模块在磁盘上保存 token 感到不满意,可以通过 Disable-AzContextAutosave 禁用默认行为

关于powershell - 安全地访问 Key Vault secret ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63751380/

相关文章:

azure - 访问 Azure Active Directory 中的 Key Vault 应用程序注册

c# - 用户主体安全。禁用不需要的智能卡提示?

用于从 Azure Key Vault 读取 secret 的 Python 脚本

bash - 在 Azure DevOps 中通过 bash 访问 Azure KV 变量

.net - 作为服务运行时找不到 PowerShell 模块

android - 如何在 Android 上保护数据库(加密)密码?

sql-server - 如何在 SQL 中检查特定登录名下是否存在某个用户?

powershell - 在其他用户的当前用户存储上安装证书

PowerShellscript,错误的文件编码对话

windows - 从 Console2 复制到剪贴板