在KeyVault中检测不存在的密钥的正确方法是什么

时间:2017-08-06 17:45:37

标签: azure azure-keyvault

我使用的是2.3.2 Microsoft.Azure.KeyVault NuGet中的KeyVaultClient。使用GetSecretAsync(,),我注意到如果我尝试访问一个不存在的秘密,就会引发KeyVaultErrorException。

不幸的是,当拒绝访问keyvault或找不到keyvault端点时,也会出现同样的错误。

我目前看到的唯一区别是Message属性。那么检测到没有找到秘密的正确方法是什么?为什么这会抛出异常而不是返回null或其他一些空的'对象

1 个答案:

答案 0 :(得分:2)

要求不存在的秘密:

System.AggregateException occurred
  HResult=0x80131500
  Message=One or more errors occurred.
  Source=mscorlib

Inner Exception 1:
KeyVaultErrorException: Secret not found: secret22222

((Microsoft.Azure.KeyVault.Models.KeyVaultErrorException)($exception).InnerException)
    .Body.Error.Code = "SecretNotFound"
((Microsoft.Azure.KeyVault.Models.KeyVaultErrorException)($exception).InnerException)
    .Body.Error.Message = "Secret not found: secret22222"

没有阅读秘密的权利:

System.AggregateException occurred
  HResult=0x80131500
  Message=One or more errors occurred.
  Source=mscorlib

Inner Exception 1:
KeyVaultErrorException: Access denied

((Microsoft.Azure.KeyVault.Models.KeyVaultErrorException)($exception).InnerException)
    .Body.Error.Code = "Forbidden"
((Microsoft.Azure.KeyVault.Models.KeyVaultErrorException)($exception).InnerException)
    .Body.Error.Message = "Access denied"

尝试阅读已禁用的密码:

System.AggregateException occurred
  HResult=0x80131500
  Message=One or more errors occurred.
  Source=mscorlib

Inner Exception 1:
KeyVaultErrorException: Operation get is not allowed on a disabled secret.

((Microsoft.Azure.KeyVault.Models.KeyVaultErrorException)($exception).InnerException)
    .Body.Error.Code = "Forbidden"
((Microsoft.Azure.KeyVault.Models.KeyVaultErrorException)($exception).InnerException)
    .Body.Error.Message = "Operation get is not allowed on a disabled secret."

无效的保险库端点:

System.AggregateException occurred
  HResult=0x80131500
  Message=One or more errors occurred.
  Source=mscorlib

Inner Exception 1:
HttpRequestException: An error occurred while sending the request.

Inner Exception 2:
WebException: The remote name could not be resolved: 'alicezzzzzz.vault.azure.net'

对我来说不是那么糟糕。如果你期望强烈的错误输入,我认为这不会发生,因为SDK只是一个REST包装器,可能(部分?)由AutoRest生成 - 显然不是在NuGet项目描述(项目网站)中提到但仍然提到:)。