Skip to content

Commit

Permalink
fix: improve RequestFailedException error message (#150)
Browse files Browse the repository at this point in the history
Improve the error message:

Previous:
```
notation sign notationreg.azurecr.io/hello-app:v2 --plugin azure-kv --id https://acrci-test-kv.vault.azure.net/keys/self-signed-pkcs13/70747b2064c0488e936eba7a29acc4c6
Warning: Always sign the artifact using digest(@sha256:...) rather than a tag(:v2) because tags are mutable and a tag reference can point to a different artifact than the one signed.
Error: describe-key command failed: failed to execute the describe-key command for plugin azure-kv: ERROR: A certificate with (name/id) self-signed-pkcs13/versions/70747b2064c0488e936eba7a29acc4c6 was not found in this key vault. If you recently deleted this certificate you may be able to recover it using the correct recovery command. For help resolving this issue, please see https://go.microsoft.com/fwlink/?linkid=2125182
Status: 404 (Not Found)
ErrorCode: CertificateNotFound

Content:
{"error":{"code":"CertificateNotFound","message":"A certificate with (name/id) self-signed-pkcs13/versions/70747b2064c0488e936eba7a29acc4c6 was not found in this key vault. If you recently deleted this certificate you may be able to recover it using the correct recovery command. For help resolving this issue, please see https://go.microsoft.com/fwlink/?linkid=2125182"}}

Headers:
Cache-Control: no-cache
Pragma: no-cache
x-ms-keyvault-region: eastus
x-ms-client-request-id: 94abafcf-db8e-4046-be8a-573360b627eb
x-ms-request-id: b6c9de19-768c-4187-90d3-17342827af20
x-ms-keyvault-service-version: 1.9.1222.3
x-ms-keyvault-network-info: conn_type=Ipv4;addr=167.220.255.20;act_addr_fam=InterNetwork;
X-Content-Type-Options: REDACTED
Strict-Transport-Security: REDACTED
Date: Mon, 29 Jan 2024 06:52:00 GMT
Content-Length: 372
Content-Type: application/json; charset=utf-8
Expires: -1

```
Current:
```
notation sign notationreg.azurecr.io/hello-app:v2 --plugin azure-kv --id https://acrci-test-kv.vault.azure.net/keys/self-signed-pkcs13/70747b2064c0488e936eba7a29acc4c6   
Warning: Always sign the artifact using digest(@sha256:...) rather than a tag(:v2) because tags are mutable and a tag reference can point to a different artifact than the one signed.
Error: describe-key command failed: failed to execute the describe-key command for plugin azure-kv: CertificateNotFound: A certificate with (name/id) self-signed-pkcs13/versions/70747b2064c0488e936eba7a29acc4c6 was not found in this key vault. If you recently deleted this certificate you may be able to recover it using the correct recovery command. For help resolving this issue, please see https://go.microsoft.com/fwlink/?linkid=2125182
```

Resolves part of notaryproject/notation#868
Signed-off-by: Junjie Gao <[email protected]>

Signed-off-by: Junjie Gao <[email protected]>
  • Loading branch information
JeyJeyGao authored Jan 29, 2024
1 parent e1442cd commit 0e675c8
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion Notation.Plugin.AzureKeyVault/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Notation.Plugin.AzureKeyVault.Command;
using System.Text.Json;
using Notation.Plugin.AzureKeyVault.Command;
using Notation.Plugin.Protocol;

namespace Notation.Plugin.AzureKeyVault
Expand All @@ -16,6 +17,31 @@ public static async Task Main(string[] args)
Error.PrintError(e.Code, e.Message);
Environment.Exit(1);
}
catch (Azure.RequestFailedException e)
{
// wrap azure exception to notation plugin error response
var rawResponse = e.GetRawResponse();
if (rawResponse != null)
{
var content = JsonDocument.Parse(rawResponse.Content);
if (content.RootElement.TryGetProperty("error", out var errorInfo) &&
errorInfo.TryGetProperty("message", out var errMsg))
{
var errorMessage = errMsg.GetString();
if (!string.IsNullOrEmpty(errorMessage))
{
Error.PrintError(
errorCode: e.ErrorCode ?? Error.ERROR,
errorMessage: errorMessage);
Environment.Exit(1);
}
}
}

// fallback to default error message
Error.PrintError(Error.ERROR, e.Message);
Environment.Exit(1);
}
catch (Exception e)
{
Error.PrintError(Error.ERROR, e.Message);
Expand Down

0 comments on commit 0e675c8

Please sign in to comment.