Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Throw RequestFailedException for failed backup/restore #41919

Merged
merged 2 commits into from
Feb 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Release History

## 4.4.0 (2024-02-13)
## 4.4.0 (2024-02-14)

Changes from both the last release and the last beta include:

Expand All @@ -10,6 +10,10 @@ Changes from both the last release and the last beta include:
- The `sasToken` parameter is now optional in `KeyVaultBackupClient.StartRestore` and `StartRestoreAsync`. Managed Identity will be used instead if `sasToken` is null.
- The `sasToken` parameter is now optional in `KeyVaultBackupClient.StartSelectiveKeyRestore` and `StartSelectiveKeyRestoreAsync`. Managed Identity will be used instead if `sasToken` is null.

### Breaking Changes

- `KeyVaultBackupOperation`, `KeyVaultRestoreOperation`, and `KeyVaultSelectiveKeyRestoreOperation` now throw a `RequestFailedException` with a different error message - but a raw `Response` with more details - when the service returns an error response. ([#41855](https://github.com/Azure/azure-sdk-for-net/issues/41855))

### Bugs Fixed

- When a Key Vault is moved to another tenant, the client is reauthenticated.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,12 @@ await _client.GetBackupDetailsAsync(Id, cancellationToken).ConfigureAwait(false)
_requestFailedException = new RequestFailedException("Unexpected failure", ex);
throw _requestFailedException;
}

if (_value != null && _value.EndTime.HasValue && _value.Error != null)
{
_requestFailedException = new RequestFailedException($"{_value.Error.Message}\nInnerError: {_value.Error.InnerError}\nCode: {_value.Error.Code}");
_requestFailedException = _response != null ?
new RequestFailedException(_response)
: new RequestFailedException($"{_value.Error.Message}\nInnerError: {_value.Error.InnerError}\nCode: {_value.Error.Code}");
throw _requestFailedException;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ internal RestoreOperationInternal(TResponseType value, Response response, KeyVau
};

/// <summary>
/// The error value resturned by the service call.
/// The error value returned by the service call.
/// </summary>
internal KeyVaultServiceError Error => _value switch
{
Expand Down Expand Up @@ -215,9 +215,12 @@ await _client.GetSelectiveKeyRestoreDetailsAsync(Id, cancellationToken).Configur
_requestFailedException = new RequestFailedException("Unexpected Failure.", ex);
throw _requestFailedException;
}

if (_value != null && EndTime.HasValue && Error?.Code != null)
{
_requestFailedException = new RequestFailedException($"{Error.Message}\nInnerError: {Error.InnerError}\nCode: {Error.Code}");
_requestFailedException = _response != null ?
new RequestFailedException(_response)
: new RequestFailedException($"{Error.Message}\nInnerError: {Error.InnerError}\nCode: {Error.Code}");
throw _requestFailedException;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
// Licensed under the MIT License.

using System;
using System.Text.Json;
using System.Threading;
using Azure.Core.Serialization;
using Azure.Core.TestFramework;
using Azure.Security.KeyVault.Administration.Models;
using Moq;
using NUnit.Framework;
Expand Down Expand Up @@ -120,5 +123,79 @@ public void ValueThrowsWhenOperationIsNotComplete()
Assert.That(operation.StartTime, Is.EqualTo(incompleteBackup.StartTime));
Assert.That(operation.EndTime, Is.EqualTo(incompleteBackup.EndTime));
}

[Test(Description = "https://github.com/Azure/azure-sdk-for-net/issues/41855")]
public void BackupOperationThrowsOnBadRequest()
{
const string jobId = "c79735efd41d4b8a8ef23634b7f3dd0d";
var response = new MockResponse(200, "OK").WithJson($$"""
{
"endTime": 1704409031,
"error": {
"code": "BadRequest",
"innererror": null,
"message": "Invalid backup: Reason: Cannot read backup status document"
},
"jobId": "c79735efd41d4b8a8ef23634b7f3dd0d",
"startTime": 1704409030,
"status": "Failed",
"statusDetails": "Invalid backup: Reason: Cannot read backup status document"
}
""");

using var doc = JsonDocument.Parse(response.Content.ToStream());
var detail = FullBackupDetailsInternal.DeserializeFullBackupDetailsInternal(doc.RootElement);

var mockClient = new Mock<KeyVaultBackupClient>();
mockClient
.Setup(m => m.GetBackupDetails(It.IsAny<string>(), It.IsAny<CancellationToken>()))
.Returns(Response.FromValue(detail, response));

var operation = new KeyVaultBackupOperation(mockClient.Object, jobId);
var ex = Assert.Throws<RequestFailedException>(() => operation.WaitForCompletion());
Assert.AreEqual(ex.Status, 200);

dynamic error = ex.GetRawResponse()?.Content.ToDynamicFromJson(JsonPropertyNames.UseExact).error;
Assert.NotNull(error);
Assert.AreEqual("BadRequest", (string)error.code);
Assert.AreEqual("Invalid backup: Reason: Cannot read backup status document", (string)error.message);
}

[Test(Description = "https://github.com/Azure/azure-sdk-for-net/issues/41855")]
public void RestoreOperationThrowsOnBadRequest()
{
const string jobId = "c79735efd41d4b8a8ef23634b7f3dd0d";
var response = new MockResponse(200, "OK").WithJson($$"""
{
"endTime": 1704409031,
"error": {
"code": "BadRequest",
"innererror": null,
"message": "Invalid backup: Reason: Cannot read backup status document"
},
"jobId": "c79735efd41d4b8a8ef23634b7f3dd0d",
"startTime": 1704409030,
"status": "Failed",
"statusDetails": "Invalid backup: Reason: Cannot read backup status document"
}
""");

using var doc = JsonDocument.Parse(response.Content.ToStream());
var detail = RestoreDetailsInternal.DeserializeRestoreDetailsInternal(doc.RootElement);

var mockClient = new Mock<KeyVaultBackupClient>();
mockClient
.Setup(m => m.GetRestoreDetails(It.IsAny<string>(), It.IsAny<CancellationToken>()))
.Returns(Response.FromValue(detail, response));

var operation = new KeyVaultRestoreOperation(mockClient.Object, jobId);
var ex = Assert.Throws<RequestFailedException>(() => operation.WaitForCompletion());
Assert.AreEqual(ex.Status, 200);

dynamic error = ex.GetRawResponse()?.Content.ToDynamicFromJson(JsonPropertyNames.UseExact).error;
Assert.NotNull(error);
Assert.AreEqual("BadRequest", (string)error.code);
Assert.AreEqual("Invalid backup: Reason: Cannot read backup status document", (string)error.message);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Release History

## 4.6.0 (2024-02-13)
## 4.6.0 (2024-02-14)

Changes from both the last release and the last beta include:

Expand Down
2 changes: 1 addition & 1 deletion sdk/keyvault/Azure.Security.KeyVault.Keys/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Release History

## 4.6.0 (2024-02-13)
## 4.6.0 (2024-02-14)

Changes from both the last release and the last beta include:

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Release History

## 4.6.0 (2024-02-13)
## 4.6.0 (2024-02-14)

Changes from both the last release and the last beta include:

Expand Down