diff --git a/docs/security.md b/docs/security.md index 5845bd3e5..8ae3b33d7 100644 --- a/docs/security.md +++ b/docs/security.md @@ -591,7 +591,10 @@ without one and anonymous sessions use the Galaxy driver's `AlarmAck` fallback a neither the ack is refused before it reaches the gateway. Deleting a key on `/apikeys` removes its identity (if the Secrets store is down at that moment the list still refreshes and the error names the two leftover secrets to remove by hand). Re-saving an existing identity with the password box **blank keeps the -stored password** (the editor never renders it back); a first-time save needs a password (#997 A-4/A-9). See [docs/drivers/Galaxy.md § Alarm acknowledge credential](drivers/Galaxy.md#alarm-acknowledge-credential). +stored password** (the editor never renders it back); a first-time save needs a password (#997 A-4/A-9). A Galaxy +account whose password is genuinely empty is stored with the editor's explicit **Empty password** checkbox +(`IApiKeyGalaxyAckIdentityService.SetWithEmptyPasswordAsync`, #1050) — it writes an empty password secret +(the driver sends an empty `operator_password`; an *absent* password secret is still a refusal). See [docs/drivers/Galaxy.md § Alarm acknowledge credential](drivers/Galaxy.md#alarm-acknowledge-credential). ### Anonymous is a normal principal diff --git a/src/Server/ZB.MOM.WW.OtOpcUa.AdminUI/ApiKeyAck/ApiKeyGalaxyAckIdentityService.cs b/src/Server/ZB.MOM.WW.OtOpcUa.AdminUI/ApiKeyAck/ApiKeyGalaxyAckIdentityService.cs index 6130d6018..94aac4f94 100644 --- a/src/Server/ZB.MOM.WW.OtOpcUa.AdminUI/ApiKeyAck/ApiKeyGalaxyAckIdentityService.cs +++ b/src/Server/ZB.MOM.WW.OtOpcUa.AdminUI/ApiKeyAck/ApiKeyGalaxyAckIdentityService.cs @@ -35,6 +35,19 @@ public interface IApiKeyGalaxyAckIdentityService /// A task representing the operation. Task SetAsync(string apiKeyId, string user, string? domain, string? password, string actor, CancellationToken ct); + /// + /// Sets (or replaces) a key's Galaxy ack identity with an EMPTY password (#1050). Galaxy accounts can have + /// empty passwords, but reads a blank password as "keep the stored one" (and refuses it + /// first time), so storing an empty password needs this explicit call. + /// + /// The public key id. + /// Galaxy user; required. + /// Optional domain. + /// The admin performing the change, for audit. + /// The cancellation token. + /// A task representing the operation. + Task SetWithEmptyPasswordAsync(string apiKeyId, string user, string? domain, string actor, CancellationToken ct); + /// Removes a key's Galaxy ack identity (no-op when none). /// The public key id. /// The admin performing the change, for audit. @@ -97,8 +110,16 @@ public sealed class ApiKeyGalaxyAckIdentityService : IApiKeyGalaxyAckIdentitySer } /// - public async Task SetAsync( + public Task SetAsync( string apiKeyId, string user, string? domain, string? password, string actor, CancellationToken ct) + => SetCoreAsync(apiKeyId, user, domain, password, emptyPassword: false, actor, ct); + + /// + public Task SetWithEmptyPasswordAsync(string apiKeyId, string user, string? domain, string actor, CancellationToken ct) + => SetCoreAsync(apiKeyId, user, domain, password: null, emptyPassword: true, actor, ct); + + private async Task SetCoreAsync( + string apiKeyId, string user, string? domain, string? password, bool emptyPassword, string actor, CancellationToken ct) { user = (user ?? string.Empty).Trim(); if (user.Length == 0) throw new ArgumentException("A Galaxy user is required.", nameof(user)); @@ -108,14 +129,23 @@ public sealed class ApiKeyGalaxyAckIdentityService : IApiKeyGalaxyAckIdentitySer var userName = new SecretName(GalaxyAckIdentitySecrets.UserSecretName(apiKeyId)); var passwordName = new SecretName(GalaxyAckIdentitySecrets.PasswordSecretName(apiKeyId)); - if (string.IsNullOrEmpty(password)) + if (emptyPassword) + { + // #1050: the explicit "this account has an empty password" option. Written like any new password — + // password FIRST, so a concurrent ack never pairs the new user with a missing password. + await UpsertAsync(passwordName, string.Empty, $"Galaxy ack password (empty) for API key {apiKeyId}", actor, ct) + .ConfigureAwait(false); + } + else if (string.IsNullOrEmpty(password)) { // The editor never renders a stored password back, so a blank box means "keep the current one" - // overwriting it with an empty value would silently break every ack on this key (#997 A-4). With no // existing identity there is nothing to keep, so a blank password is refused instead (as is a half-configured identity whose password secret is gone). var existing = await _store.GetAsync(passwordName, ct).ConfigureAwait(false); if (existing is null || existing.IsDeleted) - throw new ArgumentException("A password is required when setting a Galaxy ack identity.", nameof(password)); + throw new ArgumentException( + "A password is required when setting a Galaxy ack identity (for an account whose password is empty, " + + "tick \"Empty password\").", nameof(password)); } else { diff --git a/src/Server/ZB.MOM.WW.OtOpcUa.AdminUI/Components/Pages/ApiKeys.razor b/src/Server/ZB.MOM.WW.OtOpcUa.AdminUI/Components/Pages/ApiKeys.razor index 9ef9630d5..fa07106d6 100644 --- a/src/Server/ZB.MOM.WW.OtOpcUa.AdminUI/Components/Pages/ApiKeys.razor +++ b/src/Server/ZB.MOM.WW.OtOpcUa.AdminUI/Components/Pages/ApiKeys.razor @@ -78,8 +78,13 @@
+ placeholder="@(_ackEmptyPassword ? "(empty password)" : _ackIdentities.ContainsKey(_ackEditKeyId) ? "leave blank to keep the current password" : null)" + disabled="@_ackEmptyPassword" autocomplete="new-password" /> +
+ + +
+
The account's password is genuinely empty (a blank box keeps the stored password).
@@ -191,6 +196,8 @@ private string _ackUser = ""; private string _ackDomain = ""; private string _ackPassword = ""; + // #1050: explicit "the account's password is empty" — a blank box alone means "keep the stored password". + private bool _ackEmptyPassword; private string _newName = ""; private string _newPrincipal = ""; private DateTime? _newExpires; @@ -320,6 +327,7 @@ { _ackEditKeyId = apiKeyId; _ackPassword = ""; + _ackEmptyPassword = false; if (_ackIdentities.TryGetValue(apiKeyId, out var current)) { var slash = current.IndexOf('\\'); @@ -339,6 +347,13 @@ _ackUser = ""; _ackDomain = ""; _ackPassword = ""; + _ackEmptyPassword = false; + } + + // A typed password and "Empty password" are mutually exclusive: ticking the box discards what was typed. + private void OnAckEmptyPasswordChanged() + { + if (_ackEmptyPassword) _ackPassword = ""; } private async Task SaveAckIdentityAsync() @@ -348,7 +363,10 @@ _error = null; try { - await AckIdentities.SetAsync(id, _ackUser, _ackDomain, _ackPassword, await ActorAsync(), default); + if (_ackEmptyPassword) + await AckIdentities.SetWithEmptyPasswordAsync(id, _ackUser, _ackDomain, await ActorAsync(), default); + else + await AckIdentities.SetAsync(id, _ackUser, _ackDomain, _ackPassword, await ActorAsync(), default); CloseAckEditor(); await ReloadAsync(); } diff --git a/tests/Server/ZB.MOM.WW.OtOpcUa.AdminUI.Tests/ApiKeyAck/ApiKeyGalaxyAckIdentityServiceTests.cs b/tests/Server/ZB.MOM.WW.OtOpcUa.AdminUI.Tests/ApiKeyAck/ApiKeyGalaxyAckIdentityServiceTests.cs index e75c16cb0..29289caf3 100644 --- a/tests/Server/ZB.MOM.WW.OtOpcUa.AdminUI.Tests/ApiKeyAck/ApiKeyGalaxyAckIdentityServiceTests.cs +++ b/tests/Server/ZB.MOM.WW.OtOpcUa.AdminUI.Tests/ApiKeyAck/ApiKeyGalaxyAckIdentityServiceTests.cs @@ -124,6 +124,49 @@ public sealed class ApiKeyGalaxyAckIdentityServiceTests _store.Rows.ShouldBeEmpty(); } + /// + /// #1050: Galaxy accounts can have empty passwords, and a blank box means "keep", so storing an EMPTY password + /// needs its own explicit option — first-time (where blank is refused) and over an existing password. + /// + [Fact] + public async Task SetWithEmptyPassword_stores_an_empty_password_first_time() + { + var ct = TestContext.Current.CancellationToken; + + await Service().SetWithEmptyPasswordAsync(KeyId, "alice", "CORP", "admin", ct); + + _cipher.Decrypt(_store.Rows[GalaxyAckIdentitySecrets.PasswordSecretName(KeyId)]).ShouldBe(string.Empty); + _cipher.Decrypt(_store.Rows[GalaxyAckIdentitySecrets.UserSecretName(KeyId)]).ShouldBe("CORP\\alice"); + _store.UpsertOrder.ShouldBe([GalaxyAckIdentitySecrets.PasswordSecretName(KeyId), GalaxyAckIdentitySecrets.UserSecretName(KeyId)]); + _invalidator.Invalidated.ShouldContain(GalaxyAckIdentitySecrets.PasswordSecretName(KeyId)); + _audit.Events.ShouldHaveSingleItem().Action.ShouldBe("apikey.galaxy-ack-identity.set"); + } + + [Fact] + public async Task SetWithEmptyPassword_replaces_a_stored_password_and_a_later_blank_keeps_it_empty() + { + var svc = Service(); + var ct = TestContext.Current.CancellationToken; + await svc.SetAsync(KeyId, "alice", "CORP", Password, "admin", ct); + + await svc.SetWithEmptyPasswordAsync(KeyId, "alice", "CORP", "admin", ct); + _cipher.Decrypt(_store.Rows[GalaxyAckIdentitySecrets.PasswordSecretName(KeyId)]).ShouldBe(string.Empty); + + // A later edit with a blank box keeps the (empty) password rather than refusing — it exists. + await svc.SetAsync(KeyId, "alice", "NEWDOM", null, "admin", ct); + _cipher.Decrypt(_store.Rows[GalaxyAckIdentitySecrets.PasswordSecretName(KeyId)]).ShouldBe(string.Empty); + _cipher.Decrypt(_store.Rows[GalaxyAckIdentitySecrets.UserSecretName(KeyId)]).ShouldBe("NEWDOM\\alice"); + } + + [Theory] + [InlineData("")] + [InlineData("CORP\\alice")] + public async Task SetWithEmptyPassword_validates_the_user_like_SetAsync(string user) + { + await Should.ThrowAsync(() => Service().SetWithEmptyPasswordAsync(KeyId, user, null, "admin", default)); + _store.Rows.ShouldBeEmpty(); + } + [Fact] public async Task Set_rejects_a_malformed_key_id() { diff --git a/tests/Server/ZB.MOM.WW.OtOpcUa.AdminUI.Tests/Pages/ApiKeysDeleteTests.cs b/tests/Server/ZB.MOM.WW.OtOpcUa.AdminUI.Tests/Pages/ApiKeysDeleteTests.cs index df9f8d425..07d4ad855 100644 --- a/tests/Server/ZB.MOM.WW.OtOpcUa.AdminUI.Tests/Pages/ApiKeysDeleteTests.cs +++ b/tests/Server/ZB.MOM.WW.OtOpcUa.AdminUI.Tests/Pages/ApiKeysDeleteTests.cs @@ -108,6 +108,9 @@ public sealed class ApiKeysDeleteTests : BunitContext public Task SetAsync(string apiKeyId, string user, string? domain, string? password, string actor, CancellationToken ct) => throw new NotSupportedException(); + public Task SetWithEmptyPasswordAsync(string apiKeyId, string user, string? domain, string actor, CancellationToken ct) => + throw new NotSupportedException(); + public Task ClearAsync(string apiKeyId, string actor, CancellationToken ct) { ClearCalls++;