b3bee84b9e
Scripts can bind entries of a secrets dictionary to values held in an external vault. The backend resolves every binding at dispatch time — manual runs and triggered runs alike — and ships only the values, so agents and browsers never hold vault credentials. The agent injects them as a case-insensitive OrderedDictionary session variable (name configurable per script, default Secrets), giving $Secrets.ApiKey in the script body with declaration order preserved. Infisical is the first driver (universal-auth machine identity, v3 raw API); the ISecretsProviderDriver interface keeps the rest pluggable and self-describing so the settings form renders from driver metadata. Provider connection settings are a Data Protection-encrypted blob, secret config fields never return to the browser, and the script editor's picker lists secret names only. Resolution failures fail the job with a value-free message naming the binding and provider. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
115 lines
4.6 KiB
C#
115 lines
4.6 KiB
C#
using Backend.Data;
|
|
using Backend.Secrets;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Shared.Contracts.Api;
|
|
using Shared.Contracts.Models;
|
|
using Xunit;
|
|
|
|
namespace Backend.Tests;
|
|
|
|
public class SecretsProviderTests : IClassFixture<BackendTestFixture>
|
|
{
|
|
private readonly BackendTestFixture fixture;
|
|
|
|
public SecretsProviderTests(BackendTestFixture fixture) => this.fixture = fixture;
|
|
|
|
private async Task<T> WithServiceAsync<T>(Func<SecretsProviderService, AppDbContext, Task<T>> action)
|
|
{
|
|
using var scope = fixture.Services.CreateScope();
|
|
var service = scope.ServiceProvider.GetRequiredService<SecretsProviderService>();
|
|
var db = scope.ServiceProvider.GetRequiredService<AppDbContext>();
|
|
return await action(service, db);
|
|
}
|
|
|
|
private static SaveSecretsProviderRequest Request(string name, string? clientSecret = "super-secret") => new(
|
|
name, "infisical", new Dictionary<string, string?>
|
|
{
|
|
["baseUrl"] = "https://infisical.example.com",
|
|
["clientId"] = "machine-id",
|
|
["clientSecret"] = clientSecret,
|
|
["projectId"] = "proj-1",
|
|
["environment"] = "prod",
|
|
});
|
|
|
|
[Fact]
|
|
public async Task ConfigIsEncryptedAtRestAndSanitizedInDtos()
|
|
{
|
|
var dto = await WithServiceAsync(async (service, _) =>
|
|
await service.CreateAsync(Request("enc-test"), "tester", CancellationToken.None));
|
|
|
|
// The DTO must not leak the secret, but must report that it is set.
|
|
Assert.False(dto.Config.ContainsKey("clientSecret"));
|
|
Assert.Equal("https://infisical.example.com", dto.Config["baseUrl"]);
|
|
Assert.Contains("clientSecret", dto.SecretFieldsSet);
|
|
|
|
// The database row must not contain the plaintext secret anywhere.
|
|
var stored = await fixture.WithDbAsync(async db =>
|
|
await Task.FromResult(db.SecretsProviders.Single(p => p.Id == dto.Id)));
|
|
Assert.DoesNotContain("super-secret", stored.ConfigProtected);
|
|
Assert.DoesNotContain("infisical.example.com", stored.ConfigProtected);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task UpdateWithEmptySecretKeepsTheStoredValue()
|
|
{
|
|
var created = await WithServiceAsync(async (service, _) =>
|
|
await service.CreateAsync(Request("keep-test"), "tester", CancellationToken.None));
|
|
|
|
await WithServiceAsync<object?>(async (service, _) =>
|
|
{
|
|
var entity = await service.FindAsync(created.Id, CancellationToken.None);
|
|
// A masked form submits the secret as empty — the credential survives.
|
|
await service.UpdateAsync(entity!, Request("keep-test", clientSecret: null), "tester", CancellationToken.None);
|
|
return null;
|
|
});
|
|
|
|
var configJson = await WithServiceAsync(async (service, _) =>
|
|
{
|
|
var entity = await service.FindAsync(created.Id, CancellationToken.None);
|
|
return service.DecryptConfigJson(entity!);
|
|
});
|
|
|
|
Assert.Contains("super-secret", configJson);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ResolvingAgainstAMissingProviderNamesTheBinding()
|
|
{
|
|
var bindings = new List<SecretBinding> { new("ApiKey", Guid.NewGuid(), "API_KEY") };
|
|
|
|
var ex = await Assert.ThrowsAsync<SecretResolutionException>(() =>
|
|
WithServiceAsync(async (service, _) =>
|
|
await service.ResolveAsync(bindings, CancellationToken.None)));
|
|
|
|
Assert.Contains("ApiKey", ex.Message);
|
|
// The message must never contain a secret value — only names.
|
|
}
|
|
|
|
[Fact]
|
|
public async Task UnreachableVaultFailsResolutionWithProviderName()
|
|
{
|
|
var dto = await WithServiceAsync(async (service, _) =>
|
|
await service.CreateAsync(Request("offline-vault"), "tester", CancellationToken.None));
|
|
|
|
var bindings = new List<SecretBinding> { new("DbPassword", dto.Id, "DB_PASSWORD") };
|
|
|
|
var ex = await Assert.ThrowsAsync<SecretResolutionException>(() =>
|
|
WithServiceAsync(async (service, _) =>
|
|
await service.ResolveAsync(bindings, CancellationToken.None)));
|
|
|
|
Assert.Contains("DbPassword", ex.Message);
|
|
Assert.Contains("offline-vault", ex.Message);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task DriverCatalogListsInfisical()
|
|
{
|
|
var drivers = await WithServiceAsync((service, _) => Task.FromResult(service.Drivers));
|
|
|
|
var infisical = Assert.Single(drivers, d => d.Kind == "infisical");
|
|
Assert.Contains("clientSecret", infisical.SecretFields);
|
|
// Every secret field must also be a declared UI field, so the form can render it.
|
|
Assert.All(infisical.SecretFields, f => Assert.Contains(infisical.Fields, cf => cf.Name == f && cf.Secret));
|
|
}
|
|
}
|