Files
duplicati/Duplicati/Library/SecretProvider/LibSecretLinuxProvider.cs
T
Kenneth Skovhede 34468d352c Update LibSecret support
This PR updates the LibSecret support to work correctly on KDE which uses a slightly different default collection.

With this PR the "default" alias is resolved and used by default. If no such collection is found, the collection `login` is used.

This was the intended logic before, but the alias was not resolved, so the code looked for a literal `default` collection and then fell back to `login`. On KDE systems this could cause issues by creating a new collection literally named `default` which would then fail to resolve.

With this update the libSecret works correctly on Plasma 6 (`kwalletd6`).

Support for Plasma 5 is not complete as the libsecret implementation there is different and deprecated. Here, installing Gnome Keyring is a workaround.

This fixes #6891
2026-06-23 23:54:54 +02:00

183 lines
8.8 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 System.Reflection;
using System.Runtime.Versioning;
using System.Web;
using Duplicati.Library.Interface;
using Duplicati.Library.SecretProvider.LibSecret;
using Duplicati.Library.Utility;
namespace Duplicati.Library.SecretProvider;
/// <summary>
/// Secret provider that reads secrets from libsecret on Linux
/// </summary>
[SupportedOSPlatform("linux")]
public class LibSecretLinuxProvider : ISecretProvider
{
/// <summary>
/// The default D-Bus service name implementing the freedesktop Secret Service API.
/// </summary>
public const string DefaultServiceName = "org.freedesktop.secrets";
/// <inheritdoc />
public string Key => "libsecret";
/// <inheritdoc />
public string DisplayName => Strings.LibSecretLinuxProvider.DisplayName;
/// <inheritdoc />
public string Description => Strings.LibSecretLinuxProvider.Description;
/// <summary>
/// Cached support check for libsecret so we only evaluate availability once.
/// </summary>
private static readonly Lazy<Task<bool>> _isSupported = new(static () =>
{
if (!OperatingSystem.IsLinux())
return Task.FromResult(false);
return SecretCollection.IsSupported(DefaultServiceName, CancellationToken.None);
});
/// <inheritdoc />
public Task<bool> IsSupported(CancellationToken cancellationToken) => _isSupported.Value;
/// <inheritdoc />
public bool IsSetSupported => true;
/// <summary>
/// The configuration for the secret provider
/// </summary>
private class LibSecretConfig : ICommandLineArgumentMapper
{
/// <summary>
/// The collection to use. When left empty, the canonical default collection is
/// resolved via the Secret Service "default" alias. When set to an explicit value
/// (including the literal "default"), that collection name is matched literally.
/// </summary>
public string Collection { get; set; } = string.Empty;
/// <summary>
/// Whether the collection name is case sensitive
/// </summary>
public bool CaseSensitive { get; set; }
/// <summary>
/// Whether the collection should automatically be created when storing secrets if it does not exist.
/// </summary>
public bool NoAutoCreateCollection { get; set; }
/// <summary>
/// The D-Bus service name implementing the freedesktop Secret Service API.
/// </summary>
public string ServiceName { get; set; } = DefaultServiceName;
/// <summary>
/// Gets the command line argument description for the given name
/// </summary>
/// <param name="name">The name of the argument</param>
/// <returns>The description or null if not found</returns>
public static CommandLineArgumentDescriptionAttribute? GetCommandLineArgumentDescription(string name)
=> name switch
{
nameof(Collection) => new CommandLineArgumentDescriptionAttribute() { Name = "collection", Type = CommandLineArgument.ArgumentType.String, ShortDescription = Strings.LibSecretLinuxProvider.CollectionDescriptionShort, LongDescription = Strings.LibSecretLinuxProvider.CollectionDescriptionLong },
nameof(CaseSensitive) => new CommandLineArgumentDescriptionAttribute() { Name = "case-sensitive", Type = CommandLineArgument.ArgumentType.Boolean, ShortDescription = Strings.LibSecretLinuxProvider.CaseSensitiveDescriptionShort, LongDescription = Strings.LibSecretLinuxProvider.CaseSensitiveDescriptionLong },
nameof(NoAutoCreateCollection) => new CommandLineArgumentDescriptionAttribute() { Name = "no-autocreate-collection", Type = CommandLineArgument.ArgumentType.Boolean, ShortDescription = Strings.LibSecretLinuxProvider.NoAutoCreateCollectionDescriptionShort, LongDescription = Strings.LibSecretLinuxProvider.NoAutoCreateCollectionDescriptionLong },
nameof(ServiceName) => new CommandLineArgumentDescriptionAttribute() { Name = "service-name", Type = CommandLineArgument.ArgumentType.String, ShortDescription = Strings.LibSecretLinuxProvider.ServiceNameDescriptionShort, LongDescription = Strings.LibSecretLinuxProvider.ServiceNameDescriptionLong, DefaultValue = DefaultServiceName },
_ => null,
};
/// <inheritdoc />
CommandLineArgumentDescriptionAttribute? ICommandLineArgumentMapper.GetCommandLineArgumentDescription(MemberInfo mi)
=> GetCommandLineArgumentDescription(mi.Name);
}
/// <summary>
/// The configuration for the secret provider; null if not initialized
/// </summary>
private LibSecretConfig? _cfg;
/// <summary>
/// The secret collection
/// </summary>
private SecretCollection? _collection;
/// <inheritdoc />
public IList<ICommandLineArgument> SupportedCommands
=> CommandLineArgumentMapper.MapArguments(new LibSecretConfig())
.ToList();
/// <inheritdoc />
public async Task InitializeAsync(System.Uri config, CancellationToken cancellationToken)
{
if (!OperatingSystem.IsLinux())
throw new PlatformNotSupportedException("LibSecret is only supported on Linux");
var args = HttpUtility.ParseQueryString(config.Query);
_cfg = CommandLineArgumentMapper.ApplyArguments(new LibSecretConfig(), args);
if (string.IsNullOrWhiteSpace(_cfg.ServiceName))
throw new UserInformationException("The service name must be specified", "ServiceNameRequired");
// An empty collection means "resolve the canonical default collection via the alias".
_collection = await SecretCollection.CreateAsync(_cfg.Collection, !_cfg.NoAutoCreateCollection, _cfg.ServiceName, cancellationToken).ConfigureAwait(false);
await _collection.UnlockAsync().ConfigureAwait(false);
}
/// <inheritdoc />
public Task<Dictionary<string, string>> ResolveSecretsAsync(IEnumerable<string> keys, CancellationToken cancellationToken)
{
if (_cfg is null || _collection is null)
throw new InvalidOperationException("The secret provider has not been initialized");
if (!OperatingSystem.IsLinux())
throw new PlatformNotSupportedException("LibSecret is only supported on Linux");
return _collection.GetSecretsAsync(keys, _cfg.CaseSensitive ? StringComparer.Ordinal : StringComparer.OrdinalIgnoreCase, cancellationToken);
}
/// <inheritdoc />
public async Task SetSecretAsync(string key, string value, bool overwrite, CancellationToken cancellationToken)
{
if (_cfg is null || _collection is null)
throw new InvalidOperationException("The secret provider has not been initialized");
if (!OperatingSystem.IsLinux())
throw new PlatformNotSupportedException("LibSecret is only supported on Linux");
var comparer = _cfg.CaseSensitive ? StringComparer.Ordinal : StringComparer.OrdinalIgnoreCase;
await _collection.StoreSecretAsync(key, value, overwrite, comparer, cancellationToken).ConfigureAwait(false);
}
/// <summary>
/// Checks whether the specified collection exists
/// </summary>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>>True if the collection exists; otherwise false</returns>
public async Task<bool> DoesCollectionExist(CancellationToken cancellationToken)
{
if (_cfg is null)
throw new InvalidOperationException("The secret provider has not been initialized");
if (!OperatingSystem.IsLinux())
throw new PlatformNotSupportedException("LibSecret is only supported on Linux");
return await SecretCollection.CollectionExists(_cfg.Collection, _cfg.ServiceName, cancellationToken);
}
}