Files
duplicati/Duplicati/WebserverCore/Endpoints/V2/DestinationVerify.cs
T
Kenneth Skovhede b67a73553a Added unified interface for remote browsing.
This PR adds a single API endpoint for listing contents of remote source or backend. With this API, we can greatly simplify the FE as it does not need to know about webmodules for each provider, but can just use the single API.

To support this, the fulldisk module was updated slightly to return enumerable meta entries, similar to ms365 and gws providers.
2026-06-12 11:06:21 +02:00

189 lines
8.3 KiB
C#

// Copyright (C) 2026, The Duplicati Team
// https://duplicati.com, hello@duplicati.com
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using Duplicati.Library.Interface;
using Duplicati.Library.Main.Volumes;
using Duplicati.Server.Database;
using Duplicati.WebserverCore.Abstractions;
using Duplicati.WebserverCore.Dto.V2;
using Duplicati.WebserverCore.Endpoints.Shared;
using Microsoft.AspNetCore.Mvc;
using InvalidCertificateException = Duplicati.Library.Utility.SslCertificateValidator.InvalidCertificateException;
namespace Duplicati.WebserverCore.Endpoints.V2.Backup;
public class DestinationVerify : IEndpointV2
{
public static void Map(RouteGroupBuilder group)
{
group.MapPost("/destination/test", ([FromServices] Connection connection, [FromServices] IApplicationSettings applicationSettings, [FromBody] Dto.V2.DestinationTestRequestDto input, CancellationToken cancelToken)
=> ExecuteTestAsync(connection, applicationSettings, input, cancelToken))
.RequireAuthorization();
}
private static async Task<DestinationTestResponseDto> ExecuteTestAsync(Connection connection, IApplicationSettings applicationSettings, DestinationTestRequestDto input, CancellationToken cancelToken)
{
var destinationType = input.DestinationType ?? RemoteDestinationType.Backend;
try
{
if (destinationType == RemoteDestinationType.SourceProvider)
{
using var wrapper = await SharedRemoteOperation.GetSourceProviderForTestingAsync(connection, applicationSettings, input.DestinationUrl, null, input.BackupId, input.ConnectionStringId ?? -1, input.SourcePrefix, cancelToken);
// Call the specific Test method on the ISourceProvider (read-only test)
await wrapper.SourceProvider.TestAsync(cancelToken);
return DestinationTestResponseDto.Create(
anyFiles: true,
anyBackups: false,
anyEncryptedFiles: false
);
}
else if (destinationType == RemoteDestinationType.RestoreDestinationProvider)
{
using var wrapper = await SharedRemoteOperation.GetRestoreDestinationProviderForTestingAsync(connection, applicationSettings, input.DestinationUrl, null, input.BackupId, input.ConnectionStringId ?? -1, input.SourcePrefix, cancelToken);
// Call the specific Test method on the IRestoreDestinationProvider (should be a write test)
await wrapper.RestoreDestinationProvider.Test(cancelToken);
return DestinationTestResponseDto.Create(
anyFiles: true,
anyBackups: false,
anyEncryptedFiles: false
);
}
else
{
using var wrapper = await SharedRemoteOperation.GetBackendAsync(connection, applicationSettings, input.DestinationUrl, null, input.BackupId, input.ConnectionStringId ?? -1, cancelToken);
using (var b = wrapper.Backend)
{
try { await b.TestAsync(!input.ReadOnlyTest, cancelToken).ConfigureAwait(false); }
catch (Exception ex) when (SharedRemoteOperation.GetInnerException<FolderMissingException>(ex) is FolderMissingException)
{
if (!input.AutoCreate || input.ReadOnlyTest)
throw;
await b.CreateFolderAsync(cancelToken).ConfigureAwait(false);
await b.TestAsync(!input.ReadOnlyTest, cancelToken).ConfigureAwait(false);
}
var anyFiles = false;
var anyBackups = false;
var anyEncryptedFiles = false;
await foreach (var f in b.ListAsync(cancelToken).ConfigureAwait(false))
{
if (f.IsFolder)
continue;
anyFiles = true;
var parsed = VolumeBase.ParseFilename(f.Name);
if (parsed != null)
{
anyBackups = true;
anyEncryptedFiles = !string.IsNullOrWhiteSpace(parsed.EncryptionModule);
break;
}
}
return DestinationTestResponseDto.Create(
anyFiles,
anyBackups,
anyEncryptedFiles
);
}
}
}
catch (Exception ex) when (SharedRemoteOperation.GetInnerException<FolderMissingException>(ex) is FolderMissingException)
{
if (input.AutoCreate)
return DestinationTestResponseDto.Failure(
"Failed to create folder",
"error-creating-folder",
folderExists: false,
afterConnect: ex is TestAfterConnectException
);
return DestinationTestResponseDto.Failure(
"Folder does not exist",
"missing-folder",
folderExists: false,
afterConnect: ex is TestAfterConnectException
);
}
catch (Exception ex) when (SharedRemoteOperation.GetInnerException<InvalidCertificateException>(ex) is InvalidCertificateException icex)
{
if (string.IsNullOrWhiteSpace(icex.Certificate))
return DestinationTestResponseDto.Failure(
icex.Message,
"CertError",
afterConnect: ex is TestAfterConnectException
);
return DestinationTestResponseDto.Failure(
"Host certificate error",
"incorrect-cert",
afterConnect: ex is TestAfterConnectException,
hostCertificate: icex.Certificate
);
}
catch (Exception ex) when (SharedRemoteOperation.GetInnerException<Library.Utility.HostKeyException>(ex) is Library.Utility.HostKeyException hex)
{
if (string.IsNullOrWhiteSpace(hex.ReportedHostKey))
return DestinationTestResponseDto.Failure(
hex.Message,
"KeyError",
afterConnect: ex is TestAfterConnectException
);
return DestinationTestResponseDto.Failure(
"Host key error",
"incorrect-host-key",
afterConnect: ex is TestAfterConnectException,
reportedHostKey: hex.ReportedHostKey,
acceptedHostKey: hex.AcceptedHostKey
);
}
catch (TestAfterConnectException tcex)
{
return DestinationTestResponseDto.Failure(
tcex.Message,
tcex.HelpID,
afterConnect: true
);
}
catch (UserInformationException uex)
{
return DestinationTestResponseDto.Failure(
uex.Message,
uex.HelpID
);
}
catch (Exception ex)
{
return DestinationTestResponseDto.Failure(
ex.Message,
"error"
);
}
}
}