fix(configuration-database): resolve ConfigurationDatabase-012 — store inbound-API keys as HMAC-SHA256 hashes
Inbound-API bearer credentials are no longer persisted in plaintext. ApiKey now holds a KeyHash (peppered HMAC-SHA256); the key is shown once at creation and only its hash is stored. Lookup and validation hash the presented candidate. Cross-module: Commons (ApiKey, ApiKeyHasher), ConfigurationDatabase (mapping + HashApiKeyValue migration), InboundAPI (ApiKeyValidator), ManagementService (key creation), CentralUI (ApiKeys.razor). Existing keys must be re-issued.
This commit is contained in:
49
tests/ScadaLink.Commons.Tests/Entities/ApiKeyTests.cs
Normal file
49
tests/ScadaLink.Commons.Tests/Entities/ApiKeyTests.cs
Normal file
@@ -0,0 +1,49 @@
|
||||
using ScadaLink.Commons.Entities.InboundApi;
|
||||
using ScadaLink.Commons.Types.InboundApi;
|
||||
|
||||
namespace ScadaLink.Commons.Tests.Entities;
|
||||
|
||||
/// <summary>
|
||||
/// ConfigurationDatabase-012: the <see cref="ApiKey"/> entity must never carry the
|
||||
/// plaintext bearer credential as a persisted field — only its deterministic hash.
|
||||
/// </summary>
|
||||
public class ApiKeyTests
|
||||
{
|
||||
[Fact]
|
||||
public void ApiKey_HasNoPlaintextKeyValueProperty()
|
||||
{
|
||||
// The plaintext key is shown to the operator once at creation and is never
|
||||
// persisted. The entity must therefore expose KeyHash, not KeyValue.
|
||||
var properties = typeof(ApiKey).GetProperties().Select(p => p.Name).ToArray();
|
||||
|
||||
Assert.DoesNotContain("KeyValue", properties);
|
||||
Assert.Contains("KeyHash", properties);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Constructor_FromPlaintext_StoresHashNotPlaintext()
|
||||
{
|
||||
var key = new ApiKey("MES-Production", "the-secret-key-value");
|
||||
|
||||
Assert.NotEqual("the-secret-key-value", key.KeyHash);
|
||||
Assert.Equal(ApiKeyHasher.Default.Hash("the-secret-key-value"), key.KeyHash);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void FromHash_StoresHashVerbatim()
|
||||
{
|
||||
var key = ApiKey.FromHash("RecipeManager-Dev", "precomputed-hash-value");
|
||||
|
||||
Assert.Equal("RecipeManager-Dev", key.Name);
|
||||
Assert.Equal("precomputed-hash-value", key.KeyHash);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Constructor_NullArguments_Throw()
|
||||
{
|
||||
Assert.Throws<ArgumentNullException>(() => new ApiKey(null!, "value"));
|
||||
Assert.Throws<ArgumentNullException>(() => new ApiKey("name", (string)null!));
|
||||
Assert.Throws<ArgumentNullException>(() => ApiKey.FromHash(null!, "hash"));
|
||||
Assert.Throws<ArgumentNullException>(() => ApiKey.FromHash("name", null!));
|
||||
}
|
||||
}
|
||||
84
tests/ScadaLink.Commons.Tests/Types/ApiKeyHasherTests.cs
Normal file
84
tests/ScadaLink.Commons.Tests/Types/ApiKeyHasherTests.cs
Normal file
@@ -0,0 +1,84 @@
|
||||
using ScadaLink.Commons.Types.InboundApi;
|
||||
|
||||
namespace ScadaLink.Commons.Tests.Types;
|
||||
|
||||
/// <summary>
|
||||
/// ConfigurationDatabase-012: the inbound-API bearer credential is stored as a
|
||||
/// deterministic keyed hash (HMAC-SHA256 with a server-side pepper) rather than
|
||||
/// plaintext. These tests pin the hasher contract that the entity, the validator,
|
||||
/// and the management create-path all depend on.
|
||||
/// </summary>
|
||||
public class ApiKeyHasherTests
|
||||
{
|
||||
[Fact]
|
||||
public void Hash_IsDeterministic_SameInputSameOutput()
|
||||
{
|
||||
var hasher = new ApiKeyHasher("a-sufficiently-long-server-pepper-value");
|
||||
|
||||
var first = hasher.Hash("some-api-key-value");
|
||||
var second = hasher.Hash("some-api-key-value");
|
||||
|
||||
Assert.Equal(first, second);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Hash_DoesNotEqualPlaintext()
|
||||
{
|
||||
var hasher = new ApiKeyHasher("a-sufficiently-long-server-pepper-value");
|
||||
|
||||
var hash = hasher.Hash("some-api-key-value");
|
||||
|
||||
Assert.NotEqual("some-api-key-value", hash);
|
||||
Assert.DoesNotContain("some-api-key-value", hash);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Hash_DifferentInputs_ProduceDifferentHashes()
|
||||
{
|
||||
var hasher = new ApiKeyHasher("a-sufficiently-long-server-pepper-value");
|
||||
|
||||
Assert.NotEqual(hasher.Hash("key-one"), hasher.Hash("key-two"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Hash_DifferentPeppers_ProduceDifferentHashes()
|
||||
{
|
||||
var a = new ApiKeyHasher("a-sufficiently-long-server-pepper-value");
|
||||
var b = new ApiKeyHasher("a-different-but-equally-long-pepper-val");
|
||||
|
||||
// The pepper binds the hash to the server: a stolen DB dump is useless
|
||||
// without the pepper because the same key hashes differently under it.
|
||||
Assert.NotEqual(a.Hash("same-api-key"), b.Hash("same-api-key"));
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(null)]
|
||||
[InlineData("")]
|
||||
[InlineData(" ")]
|
||||
[InlineData("too-short")]
|
||||
public void Constructor_MissingOrWeakPepper_FailsFast(string? pepper)
|
||||
{
|
||||
// The pepper must be present and of meaningful length; a missing or weak
|
||||
// pepper is a deployment misconfiguration and must fail loudly.
|
||||
Assert.Throws<ArgumentException>(() => new ApiKeyHasher(pepper!));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Hash_NullInput_Throws()
|
||||
{
|
||||
var hasher = new ApiKeyHasher("a-sufficiently-long-server-pepper-value");
|
||||
|
||||
Assert.Throws<ArgumentNullException>(() => hasher.Hash(null!));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Default_IsUsableWithoutAPepper()
|
||||
{
|
||||
// The unpeppered default exists for tests and non-production wiring; it is
|
||||
// still a one-way HMAC-SHA256, just without the server-binding pepper.
|
||||
var hash = ApiKeyHasher.Default.Hash("some-api-key-value");
|
||||
|
||||
Assert.NotEqual("some-api-key-value", hash);
|
||||
Assert.Equal(ApiKeyHasher.Default.Hash("some-api-key-value"), hash);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user