Phase 6.2 Stream A — LdapGroupRoleMapping entity + EF migration + CRUD service
Stream A.1-A.2 per docs/v2/implementation/phase-6-2-authorization-runtime.md.
Seed-data migration (A.3) is a separate follow-up once production LDAP group
DNs are finalised; until then CRUD via the Admin UI handles the fleet set up.
Configuration:
- New AdminRole enum {ConfigViewer, ConfigEditor, FleetAdmin} — string-stored.
- New LdapGroupRoleMapping entity with Id (surrogate PK), LdapGroup (512 chars),
Role (AdminRole enum), ClusterId (nullable, FK to ServerCluster), IsSystemWide,
CreatedAtUtc, Notes.
- EF config: UX_LdapGroupRoleMapping_Group_Cluster unique index on
(LdapGroup, ClusterId) + IX_LdapGroupRoleMapping_Group hot-path index on
LdapGroup for sign-in lookups. Cluster FK cascades on cluster delete.
- Migration 20260419_..._AddLdapGroupRoleMapping generated via `dotnet ef`.
Configuration.Services:
- ILdapGroupRoleMappingService — CRUD surface. Declared as control-plane only
per decision #150; the OPC UA data-path evaluator must NOT depend on this
interface (Phase 6.2 compliance check on control/data-plane separation).
GetByGroupsAsync is the hot-path sign-in lookup.
- LdapGroupRoleMappingService (EF Core impl) enforces the write-time invariant
"exactly one of (ClusterId populated, IsSystemWide=true)" and surfaces
InvalidLdapGroupRoleMappingException on violation. Create auto-populates Id
+ CreatedAtUtc when omitted.
Tests (9 new, all pass) in Configuration.Tests:
- Create sets Id + CreatedAtUtc.
- Create rejects empty LdapGroup.
- Create rejects IsSystemWide=true with populated ClusterId.
- Create rejects IsSystemWide=false with null ClusterId.
- GetByGroupsAsync returns matching rows only.
- GetByGroupsAsync with empty input returns empty (no full-table scan).
- ListAllAsync orders by group then cluster.
- Delete removes the target row.
- Delete of unknown id is a no-op.
Microsoft.EntityFrameworkCore.InMemory 10.0.0 added to Configuration.Tests for
the service-level tests (schema-compliance tests still use the live SQL
fixture).
SchemaComplianceTests updated to expect the new LdapGroupRoleMapping table.
Full solution dotnet test: 1051 passing (baseline 906, Phase 6.1 shipped at
1042, Phase 6.2 Stream A adds 9 = 1051). Pre-existing Client.CLI Subscribe
flake unchanged.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,56 @@
|
|||||||
|
using ZB.MOM.WW.OtOpcUa.Configuration.Enums;
|
||||||
|
|
||||||
|
namespace ZB.MOM.WW.OtOpcUa.Configuration.Entities;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Maps an LDAP group to an <see cref="AdminRole"/> for Admin UI access. Optionally scoped
|
||||||
|
/// to one <see cref="ClusterId"/>; when <see cref="IsSystemWide"/> is true, the grant
|
||||||
|
/// applies fleet-wide.
|
||||||
|
/// </summary>
|
||||||
|
/// <remarks>
|
||||||
|
/// <para>Per <c>docs/v2/plan.md</c> decisions #105 and #150 — this entity is <b>control-plane
|
||||||
|
/// only</b>. The OPC UA data-path evaluator does not read these rows; it reads
|
||||||
|
/// <see cref="NodeAcl"/> joined directly against the session's resolved LDAP group
|
||||||
|
/// memberships. Collapsing the two would let a user inherit tag permissions via an
|
||||||
|
/// admin-role claim path never intended as a data-path grant.</para>
|
||||||
|
///
|
||||||
|
/// <para>Uniqueness: <c>(LdapGroup, ClusterId)</c> — the same LDAP group may hold
|
||||||
|
/// different roles on different clusters, but only one row per cluster. A system-wide row
|
||||||
|
/// (<c>IsSystemWide = true</c>, <c>ClusterId = null</c>) stacks additively with any
|
||||||
|
/// cluster-scoped rows for the same group.</para>
|
||||||
|
/// </remarks>
|
||||||
|
public sealed class LdapGroupRoleMapping
|
||||||
|
{
|
||||||
|
/// <summary>Surrogate primary key.</summary>
|
||||||
|
public Guid Id { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// LDAP group DN the membership query returns (e.g. <c>cn=fleet-admin,ou=groups,dc=corp,dc=example</c>).
|
||||||
|
/// Comparison is case-insensitive per LDAP conventions.
|
||||||
|
/// </summary>
|
||||||
|
public required string LdapGroup { get; set; }
|
||||||
|
|
||||||
|
/// <summary>Admin role this group grants.</summary>
|
||||||
|
public required AdminRole Role { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Cluster the grant applies to; <c>null</c> when <see cref="IsSystemWide"/> is true.
|
||||||
|
/// Foreign key to <see cref="ServerCluster.ClusterId"/>.
|
||||||
|
/// </summary>
|
||||||
|
public string? ClusterId { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// <c>true</c> = grant applies across every cluster in the fleet; <c>ClusterId</c> must be null.
|
||||||
|
/// <c>false</c> = grant is cluster-scoped; <c>ClusterId</c> must be populated.
|
||||||
|
/// </summary>
|
||||||
|
public required bool IsSystemWide { get; set; }
|
||||||
|
|
||||||
|
/// <summary>Row creation timestamp (UTC).</summary>
|
||||||
|
public DateTime CreatedAtUtc { get; set; }
|
||||||
|
|
||||||
|
/// <summary>Optional human-readable note (e.g. "added 2026-04-19 for Warsaw fleet admin handoff").</summary>
|
||||||
|
public string? Notes { get; set; }
|
||||||
|
|
||||||
|
/// <summary>Navigation for EF core when the row is cluster-scoped.</summary>
|
||||||
|
public ServerCluster? Cluster { get; set; }
|
||||||
|
}
|
||||||
26
src/ZB.MOM.WW.OtOpcUa.Configuration/Enums/AdminRole.cs
Normal file
26
src/ZB.MOM.WW.OtOpcUa.Configuration/Enums/AdminRole.cs
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
namespace ZB.MOM.WW.OtOpcUa.Configuration.Enums;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Admin UI roles per <c>admin-ui.md</c> §"Admin Roles" and Phase 6.2 Stream A.
|
||||||
|
/// These govern Admin UI capabilities (cluster CRUD, draft → publish, fleet-wide admin
|
||||||
|
/// actions) — they do NOT govern OPC UA data-path authorization, which reads
|
||||||
|
/// <see cref="Entities.NodeAcl"/> joined against LDAP group memberships directly.
|
||||||
|
/// </summary>
|
||||||
|
/// <remarks>
|
||||||
|
/// Per <c>docs/v2/plan.md</c> decision #150 the two concerns share zero runtime code path:
|
||||||
|
/// the control plane (Admin UI) consumes <see cref="Entities.LdapGroupRoleMapping"/>; the
|
||||||
|
/// data plane consumes <see cref="Entities.NodeAcl"/> rows directly. Having them in one
|
||||||
|
/// table would collapse the distinction + let a user inherit tag permissions via their
|
||||||
|
/// admin-role claim path.
|
||||||
|
/// </remarks>
|
||||||
|
public enum AdminRole
|
||||||
|
{
|
||||||
|
/// <summary>Read-only Admin UI access — can view cluster state, drafts, publish history.</summary>
|
||||||
|
ConfigViewer,
|
||||||
|
|
||||||
|
/// <summary>Can author drafts + submit for publish.</summary>
|
||||||
|
ConfigEditor,
|
||||||
|
|
||||||
|
/// <summary>Full Admin UI privileges including publish + fleet-admin actions.</summary>
|
||||||
|
FleetAdmin,
|
||||||
|
}
|
||||||
1342
src/ZB.MOM.WW.OtOpcUa.Configuration/Migrations/20260419131444_AddLdapGroupRoleMapping.Designer.cs
generated
Normal file
1342
src/ZB.MOM.WW.OtOpcUa.Configuration/Migrations/20260419131444_AddLdapGroupRoleMapping.Designer.cs
generated
Normal file
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,62 @@
|
|||||||
|
using System;
|
||||||
|
using Microsoft.EntityFrameworkCore.Migrations;
|
||||||
|
|
||||||
|
#nullable disable
|
||||||
|
|
||||||
|
namespace ZB.MOM.WW.OtOpcUa.Configuration.Migrations
|
||||||
|
{
|
||||||
|
/// <inheritdoc />
|
||||||
|
public partial class AddLdapGroupRoleMapping : Migration
|
||||||
|
{
|
||||||
|
/// <inheritdoc />
|
||||||
|
protected override void Up(MigrationBuilder migrationBuilder)
|
||||||
|
{
|
||||||
|
migrationBuilder.CreateTable(
|
||||||
|
name: "LdapGroupRoleMapping",
|
||||||
|
columns: table => new
|
||||||
|
{
|
||||||
|
Id = table.Column<Guid>(type: "uniqueidentifier", nullable: false),
|
||||||
|
LdapGroup = table.Column<string>(type: "nvarchar(512)", maxLength: 512, nullable: false),
|
||||||
|
Role = table.Column<string>(type: "nvarchar(32)", maxLength: 32, nullable: false),
|
||||||
|
ClusterId = table.Column<string>(type: "nvarchar(64)", maxLength: 64, nullable: true),
|
||||||
|
IsSystemWide = table.Column<bool>(type: "bit", nullable: false),
|
||||||
|
CreatedAtUtc = table.Column<DateTime>(type: "datetime2(3)", nullable: false),
|
||||||
|
Notes = table.Column<string>(type: "nvarchar(512)", maxLength: 512, nullable: true)
|
||||||
|
},
|
||||||
|
constraints: table =>
|
||||||
|
{
|
||||||
|
table.PrimaryKey("PK_LdapGroupRoleMapping", x => x.Id);
|
||||||
|
table.ForeignKey(
|
||||||
|
name: "FK_LdapGroupRoleMapping_ServerCluster_ClusterId",
|
||||||
|
column: x => x.ClusterId,
|
||||||
|
principalTable: "ServerCluster",
|
||||||
|
principalColumn: "ClusterId",
|
||||||
|
onDelete: ReferentialAction.Cascade);
|
||||||
|
});
|
||||||
|
|
||||||
|
migrationBuilder.CreateIndex(
|
||||||
|
name: "IX_LdapGroupRoleMapping_ClusterId",
|
||||||
|
table: "LdapGroupRoleMapping",
|
||||||
|
column: "ClusterId");
|
||||||
|
|
||||||
|
migrationBuilder.CreateIndex(
|
||||||
|
name: "IX_LdapGroupRoleMapping_Group",
|
||||||
|
table: "LdapGroupRoleMapping",
|
||||||
|
column: "LdapGroup");
|
||||||
|
|
||||||
|
migrationBuilder.CreateIndex(
|
||||||
|
name: "UX_LdapGroupRoleMapping_Group_Cluster",
|
||||||
|
table: "LdapGroupRoleMapping",
|
||||||
|
columns: new[] { "LdapGroup", "ClusterId" },
|
||||||
|
unique: true,
|
||||||
|
filter: "[ClusterId] IS NOT NULL");
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
protected override void Down(MigrationBuilder migrationBuilder)
|
||||||
|
{
|
||||||
|
migrationBuilder.DropTable(
|
||||||
|
name: "LdapGroupRoleMapping");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -663,6 +663,51 @@ namespace ZB.MOM.WW.OtOpcUa.Configuration.Migrations
|
|||||||
b.ToTable("ExternalIdReservation", (string)null);
|
b.ToTable("ExternalIdReservation", (string)null);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
modelBuilder.Entity("ZB.MOM.WW.OtOpcUa.Configuration.Entities.LdapGroupRoleMapping", b =>
|
||||||
|
{
|
||||||
|
b.Property<Guid>("Id")
|
||||||
|
.ValueGeneratedOnAdd()
|
||||||
|
.HasColumnType("uniqueidentifier");
|
||||||
|
|
||||||
|
b.Property<string>("ClusterId")
|
||||||
|
.HasMaxLength(64)
|
||||||
|
.HasColumnType("nvarchar(64)");
|
||||||
|
|
||||||
|
b.Property<DateTime>("CreatedAtUtc")
|
||||||
|
.HasColumnType("datetime2(3)");
|
||||||
|
|
||||||
|
b.Property<bool>("IsSystemWide")
|
||||||
|
.HasColumnType("bit");
|
||||||
|
|
||||||
|
b.Property<string>("LdapGroup")
|
||||||
|
.IsRequired()
|
||||||
|
.HasMaxLength(512)
|
||||||
|
.HasColumnType("nvarchar(512)");
|
||||||
|
|
||||||
|
b.Property<string>("Notes")
|
||||||
|
.HasMaxLength(512)
|
||||||
|
.HasColumnType("nvarchar(512)");
|
||||||
|
|
||||||
|
b.Property<string>("Role")
|
||||||
|
.IsRequired()
|
||||||
|
.HasMaxLength(32)
|
||||||
|
.HasColumnType("nvarchar(32)");
|
||||||
|
|
||||||
|
b.HasKey("Id");
|
||||||
|
|
||||||
|
b.HasIndex("ClusterId");
|
||||||
|
|
||||||
|
b.HasIndex("LdapGroup")
|
||||||
|
.HasDatabaseName("IX_LdapGroupRoleMapping_Group");
|
||||||
|
|
||||||
|
b.HasIndex("LdapGroup", "ClusterId")
|
||||||
|
.IsUnique()
|
||||||
|
.HasDatabaseName("UX_LdapGroupRoleMapping_Group_Cluster")
|
||||||
|
.HasFilter("[ClusterId] IS NOT NULL");
|
||||||
|
|
||||||
|
b.ToTable("LdapGroupRoleMapping", (string)null);
|
||||||
|
});
|
||||||
|
|
||||||
modelBuilder.Entity("ZB.MOM.WW.OtOpcUa.Configuration.Entities.Namespace", b =>
|
modelBuilder.Entity("ZB.MOM.WW.OtOpcUa.Configuration.Entities.Namespace", b =>
|
||||||
{
|
{
|
||||||
b.Property<Guid>("NamespaceRowId")
|
b.Property<Guid>("NamespaceRowId")
|
||||||
@@ -1181,6 +1226,16 @@ namespace ZB.MOM.WW.OtOpcUa.Configuration.Migrations
|
|||||||
b.Navigation("Generation");
|
b.Navigation("Generation");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
modelBuilder.Entity("ZB.MOM.WW.OtOpcUa.Configuration.Entities.LdapGroupRoleMapping", b =>
|
||||||
|
{
|
||||||
|
b.HasOne("ZB.MOM.WW.OtOpcUa.Configuration.Entities.ServerCluster", "Cluster")
|
||||||
|
.WithMany()
|
||||||
|
.HasForeignKey("ClusterId")
|
||||||
|
.OnDelete(DeleteBehavior.Cascade);
|
||||||
|
|
||||||
|
b.Navigation("Cluster");
|
||||||
|
});
|
||||||
|
|
||||||
modelBuilder.Entity("ZB.MOM.WW.OtOpcUa.Configuration.Entities.Namespace", b =>
|
modelBuilder.Entity("ZB.MOM.WW.OtOpcUa.Configuration.Entities.Namespace", b =>
|
||||||
{
|
{
|
||||||
b.HasOne("ZB.MOM.WW.OtOpcUa.Configuration.Entities.ServerCluster", "Cluster")
|
b.HasOne("ZB.MOM.WW.OtOpcUa.Configuration.Entities.ServerCluster", "Cluster")
|
||||||
|
|||||||
@@ -29,6 +29,7 @@ public sealed class OtOpcUaConfigDbContext(DbContextOptions<OtOpcUaConfigDbConte
|
|||||||
public DbSet<ExternalIdReservation> ExternalIdReservations => Set<ExternalIdReservation>();
|
public DbSet<ExternalIdReservation> ExternalIdReservations => Set<ExternalIdReservation>();
|
||||||
public DbSet<DriverHostStatus> DriverHostStatuses => Set<DriverHostStatus>();
|
public DbSet<DriverHostStatus> DriverHostStatuses => Set<DriverHostStatus>();
|
||||||
public DbSet<DriverInstanceResilienceStatus> DriverInstanceResilienceStatuses => Set<DriverInstanceResilienceStatus>();
|
public DbSet<DriverInstanceResilienceStatus> DriverInstanceResilienceStatuses => Set<DriverInstanceResilienceStatus>();
|
||||||
|
public DbSet<LdapGroupRoleMapping> LdapGroupRoleMappings => Set<LdapGroupRoleMapping>();
|
||||||
|
|
||||||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||||
{
|
{
|
||||||
@@ -51,6 +52,7 @@ public sealed class OtOpcUaConfigDbContext(DbContextOptions<OtOpcUaConfigDbConte
|
|||||||
ConfigureExternalIdReservation(modelBuilder);
|
ConfigureExternalIdReservation(modelBuilder);
|
||||||
ConfigureDriverHostStatus(modelBuilder);
|
ConfigureDriverHostStatus(modelBuilder);
|
||||||
ConfigureDriverInstanceResilienceStatus(modelBuilder);
|
ConfigureDriverInstanceResilienceStatus(modelBuilder);
|
||||||
|
ConfigureLdapGroupRoleMapping(modelBuilder);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void ConfigureServerCluster(ModelBuilder modelBuilder)
|
private static void ConfigureServerCluster(ModelBuilder modelBuilder)
|
||||||
@@ -531,4 +533,36 @@ public sealed class OtOpcUaConfigDbContext(DbContextOptions<OtOpcUaConfigDbConte
|
|||||||
e.HasIndex(x => x.LastSampledUtc).HasDatabaseName("IX_DriverResilience_LastSampled");
|
e.HasIndex(x => x.LastSampledUtc).HasDatabaseName("IX_DriverResilience_LastSampled");
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static void ConfigureLdapGroupRoleMapping(ModelBuilder modelBuilder)
|
||||||
|
{
|
||||||
|
modelBuilder.Entity<LdapGroupRoleMapping>(e =>
|
||||||
|
{
|
||||||
|
e.ToTable("LdapGroupRoleMapping");
|
||||||
|
e.HasKey(x => x.Id);
|
||||||
|
e.Property(x => x.LdapGroup).HasMaxLength(512).IsRequired();
|
||||||
|
e.Property(x => x.Role).HasConversion<string>().HasMaxLength(32);
|
||||||
|
e.Property(x => x.ClusterId).HasMaxLength(64);
|
||||||
|
e.Property(x => x.CreatedAtUtc).HasColumnType("datetime2(3)");
|
||||||
|
e.Property(x => x.Notes).HasMaxLength(512);
|
||||||
|
|
||||||
|
// FK to ServerCluster when cluster-scoped; null for system-wide grants.
|
||||||
|
e.HasOne(x => x.Cluster)
|
||||||
|
.WithMany()
|
||||||
|
.HasForeignKey(x => x.ClusterId)
|
||||||
|
.OnDelete(DeleteBehavior.Cascade);
|
||||||
|
|
||||||
|
// Uniqueness: one row per (LdapGroup, ClusterId). Null ClusterId is treated as its own
|
||||||
|
// "bucket" so a system-wide row coexists with cluster-scoped rows for the same group.
|
||||||
|
// SQL Server treats NULL as a distinct value in unique-index comparisons by default
|
||||||
|
// since 2008 SP1 onwards under the session setting we use — tested in SchemaCompliance.
|
||||||
|
e.HasIndex(x => new { x.LdapGroup, x.ClusterId })
|
||||||
|
.IsUnique()
|
||||||
|
.HasDatabaseName("UX_LdapGroupRoleMapping_Group_Cluster");
|
||||||
|
|
||||||
|
// Hot-path lookup during cookie auth: "what grants does this user's set of LDAP
|
||||||
|
// groups carry?". Fires on every sign-in so the index earns its keep.
|
||||||
|
e.HasIndex(x => x.LdapGroup).HasDatabaseName("IX_LdapGroupRoleMapping_Group");
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,47 @@
|
|||||||
|
using ZB.MOM.WW.OtOpcUa.Configuration.Entities;
|
||||||
|
using ZB.MOM.WW.OtOpcUa.Configuration.Enums;
|
||||||
|
|
||||||
|
namespace ZB.MOM.WW.OtOpcUa.Configuration.Services;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// CRUD surface for <see cref="LdapGroupRoleMapping"/> — the control-plane mapping from
|
||||||
|
/// LDAP groups to Admin UI roles. Consumed only by Admin UI code paths; the OPC UA
|
||||||
|
/// data-path evaluator MUST NOT depend on this interface (see decision #150 and the
|
||||||
|
/// Phase 6.2 compliance check on control/data-plane separation).
|
||||||
|
/// </summary>
|
||||||
|
/// <remarks>
|
||||||
|
/// Per Phase 6.2 Stream A.2 this service is expected to run behind the Phase 6.1
|
||||||
|
/// <c>ResilientConfigReader</c> pipeline (timeout → retry → fallback-to-cache) so a
|
||||||
|
/// transient DB outage during sign-in falls back to the sealed snapshot rather than
|
||||||
|
/// denying every login.
|
||||||
|
/// </remarks>
|
||||||
|
public interface ILdapGroupRoleMappingService
|
||||||
|
{
|
||||||
|
/// <summary>List every mapping whose LDAP group matches one of <paramref name="ldapGroups"/>.</summary>
|
||||||
|
/// <remarks>
|
||||||
|
/// Hot path — fires on every sign-in. The default EF implementation relies on the
|
||||||
|
/// <c>IX_LdapGroupRoleMapping_Group</c> index. Case-insensitive per LDAP conventions.
|
||||||
|
/// </remarks>
|
||||||
|
Task<IReadOnlyList<LdapGroupRoleMapping>> GetByGroupsAsync(
|
||||||
|
IEnumerable<string> ldapGroups, CancellationToken cancellationToken);
|
||||||
|
|
||||||
|
/// <summary>Enumerate every mapping; Admin UI listing only.</summary>
|
||||||
|
Task<IReadOnlyList<LdapGroupRoleMapping>> ListAllAsync(CancellationToken cancellationToken);
|
||||||
|
|
||||||
|
/// <summary>Create a new grant.</summary>
|
||||||
|
/// <exception cref="InvalidLdapGroupRoleMappingException">
|
||||||
|
/// Thrown when the proposed row violates an invariant (IsSystemWide inconsistent with
|
||||||
|
/// ClusterId, duplicate (group, cluster) pair, etc.) — ValidatedLdapGroupRoleMappingService
|
||||||
|
/// is the write surface that enforces these; the raw service here surfaces DB-level violations.
|
||||||
|
/// </exception>
|
||||||
|
Task<LdapGroupRoleMapping> CreateAsync(LdapGroupRoleMapping row, CancellationToken cancellationToken);
|
||||||
|
|
||||||
|
/// <summary>Delete a mapping by its surrogate key.</summary>
|
||||||
|
Task DeleteAsync(Guid id, CancellationToken cancellationToken);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>Thrown when <see cref="LdapGroupRoleMapping"/> authoring violates an invariant.</summary>
|
||||||
|
public sealed class InvalidLdapGroupRoleMappingException : Exception
|
||||||
|
{
|
||||||
|
public InvalidLdapGroupRoleMappingException(string message) : base(message) { }
|
||||||
|
}
|
||||||
@@ -0,0 +1,69 @@
|
|||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using ZB.MOM.WW.OtOpcUa.Configuration.Entities;
|
||||||
|
|
||||||
|
namespace ZB.MOM.WW.OtOpcUa.Configuration.Services;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// EF Core implementation of <see cref="ILdapGroupRoleMappingService"/>. Enforces the
|
||||||
|
/// "exactly one of (ClusterId, IsSystemWide)" invariant at the write surface so a
|
||||||
|
/// malformed row can't land in the DB.
|
||||||
|
/// </summary>
|
||||||
|
public sealed class LdapGroupRoleMappingService(OtOpcUaConfigDbContext db) : ILdapGroupRoleMappingService
|
||||||
|
{
|
||||||
|
public async Task<IReadOnlyList<LdapGroupRoleMapping>> GetByGroupsAsync(
|
||||||
|
IEnumerable<string> ldapGroups, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
ArgumentNullException.ThrowIfNull(ldapGroups);
|
||||||
|
var groupSet = ldapGroups.ToList();
|
||||||
|
if (groupSet.Count == 0) return [];
|
||||||
|
|
||||||
|
return await db.LdapGroupRoleMappings
|
||||||
|
.AsNoTracking()
|
||||||
|
.Where(m => groupSet.Contains(m.LdapGroup))
|
||||||
|
.ToListAsync(cancellationToken)
|
||||||
|
.ConfigureAwait(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<IReadOnlyList<LdapGroupRoleMapping>> ListAllAsync(CancellationToken cancellationToken)
|
||||||
|
=> await db.LdapGroupRoleMappings
|
||||||
|
.AsNoTracking()
|
||||||
|
.OrderBy(m => m.LdapGroup)
|
||||||
|
.ThenBy(m => m.ClusterId)
|
||||||
|
.ToListAsync(cancellationToken)
|
||||||
|
.ConfigureAwait(false);
|
||||||
|
|
||||||
|
public async Task<LdapGroupRoleMapping> CreateAsync(LdapGroupRoleMapping row, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
ArgumentNullException.ThrowIfNull(row);
|
||||||
|
ValidateInvariants(row);
|
||||||
|
|
||||||
|
if (row.Id == Guid.Empty) row.Id = Guid.NewGuid();
|
||||||
|
if (row.CreatedAtUtc == default) row.CreatedAtUtc = DateTime.UtcNow;
|
||||||
|
|
||||||
|
db.LdapGroupRoleMappings.Add(row);
|
||||||
|
await db.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
|
||||||
|
return row;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task DeleteAsync(Guid id, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
var existing = await db.LdapGroupRoleMappings.FindAsync([id], cancellationToken).ConfigureAwait(false);
|
||||||
|
if (existing is null) return;
|
||||||
|
db.LdapGroupRoleMappings.Remove(existing);
|
||||||
|
await db.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void ValidateInvariants(LdapGroupRoleMapping row)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrWhiteSpace(row.LdapGroup))
|
||||||
|
throw new InvalidLdapGroupRoleMappingException("LdapGroup must not be empty.");
|
||||||
|
|
||||||
|
if (row.IsSystemWide && !string.IsNullOrEmpty(row.ClusterId))
|
||||||
|
throw new InvalidLdapGroupRoleMappingException(
|
||||||
|
"IsSystemWide=true requires ClusterId to be null. A fleet-wide grant cannot also be cluster-scoped.");
|
||||||
|
|
||||||
|
if (!row.IsSystemWide && string.IsNullOrEmpty(row.ClusterId))
|
||||||
|
throw new InvalidLdapGroupRoleMappingException(
|
||||||
|
"IsSystemWide=false requires a populated ClusterId. A cluster-scoped grant needs its target cluster.");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,138 @@
|
|||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using Shouldly;
|
||||||
|
using Xunit;
|
||||||
|
using ZB.MOM.WW.OtOpcUa.Configuration.Entities;
|
||||||
|
using ZB.MOM.WW.OtOpcUa.Configuration.Enums;
|
||||||
|
using ZB.MOM.WW.OtOpcUa.Configuration.Services;
|
||||||
|
|
||||||
|
namespace ZB.MOM.WW.OtOpcUa.Configuration.Tests;
|
||||||
|
|
||||||
|
[Trait("Category", "Unit")]
|
||||||
|
public sealed class LdapGroupRoleMappingServiceTests : IDisposable
|
||||||
|
{
|
||||||
|
private readonly OtOpcUaConfigDbContext _db;
|
||||||
|
|
||||||
|
public LdapGroupRoleMappingServiceTests()
|
||||||
|
{
|
||||||
|
var options = new DbContextOptionsBuilder<OtOpcUaConfigDbContext>()
|
||||||
|
.UseInMemoryDatabase($"ldap-grm-{Guid.NewGuid():N}")
|
||||||
|
.Options;
|
||||||
|
_db = new OtOpcUaConfigDbContext(options);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Dispose() => _db.Dispose();
|
||||||
|
|
||||||
|
private LdapGroupRoleMapping Make(string group, AdminRole role, string? clusterId = null, bool? isSystemWide = null) =>
|
||||||
|
new()
|
||||||
|
{
|
||||||
|
LdapGroup = group,
|
||||||
|
Role = role,
|
||||||
|
ClusterId = clusterId,
|
||||||
|
IsSystemWide = isSystemWide ?? (clusterId is null),
|
||||||
|
};
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Create_SetsId_AndCreatedAtUtc()
|
||||||
|
{
|
||||||
|
var svc = new LdapGroupRoleMappingService(_db);
|
||||||
|
var row = Make("cn=fleet,dc=x", AdminRole.FleetAdmin);
|
||||||
|
|
||||||
|
var saved = await svc.CreateAsync(row, CancellationToken.None);
|
||||||
|
|
||||||
|
saved.Id.ShouldNotBe(Guid.Empty);
|
||||||
|
saved.CreatedAtUtc.ShouldBeGreaterThan(DateTime.UtcNow.AddMinutes(-1));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Create_Rejects_EmptyLdapGroup()
|
||||||
|
{
|
||||||
|
var svc = new LdapGroupRoleMappingService(_db);
|
||||||
|
var row = Make("", AdminRole.FleetAdmin);
|
||||||
|
|
||||||
|
await Should.ThrowAsync<InvalidLdapGroupRoleMappingException>(
|
||||||
|
() => svc.CreateAsync(row, CancellationToken.None));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Create_Rejects_SystemWide_With_ClusterId()
|
||||||
|
{
|
||||||
|
var svc = new LdapGroupRoleMappingService(_db);
|
||||||
|
var row = Make("cn=g", AdminRole.ConfigViewer, clusterId: "c1", isSystemWide: true);
|
||||||
|
|
||||||
|
await Should.ThrowAsync<InvalidLdapGroupRoleMappingException>(
|
||||||
|
() => svc.CreateAsync(row, CancellationToken.None));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Create_Rejects_NonSystemWide_WithoutClusterId()
|
||||||
|
{
|
||||||
|
var svc = new LdapGroupRoleMappingService(_db);
|
||||||
|
var row = Make("cn=g", AdminRole.ConfigViewer, clusterId: null, isSystemWide: false);
|
||||||
|
|
||||||
|
await Should.ThrowAsync<InvalidLdapGroupRoleMappingException>(
|
||||||
|
() => svc.CreateAsync(row, CancellationToken.None));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task GetByGroups_Returns_MatchingGrants_Only()
|
||||||
|
{
|
||||||
|
var svc = new LdapGroupRoleMappingService(_db);
|
||||||
|
await svc.CreateAsync(Make("cn=fleet,dc=x", AdminRole.FleetAdmin), CancellationToken.None);
|
||||||
|
await svc.CreateAsync(Make("cn=editor,dc=x", AdminRole.ConfigEditor), CancellationToken.None);
|
||||||
|
await svc.CreateAsync(Make("cn=viewer,dc=x", AdminRole.ConfigViewer), CancellationToken.None);
|
||||||
|
|
||||||
|
var results = await svc.GetByGroupsAsync(
|
||||||
|
["cn=fleet,dc=x", "cn=viewer,dc=x"], CancellationToken.None);
|
||||||
|
|
||||||
|
results.Count.ShouldBe(2);
|
||||||
|
results.Select(r => r.Role).ShouldBe([AdminRole.FleetAdmin, AdminRole.ConfigViewer], ignoreOrder: true);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task GetByGroups_Empty_Input_ReturnsEmpty()
|
||||||
|
{
|
||||||
|
var svc = new LdapGroupRoleMappingService(_db);
|
||||||
|
await svc.CreateAsync(Make("cn=fleet,dc=x", AdminRole.FleetAdmin), CancellationToken.None);
|
||||||
|
|
||||||
|
var results = await svc.GetByGroupsAsync([], CancellationToken.None);
|
||||||
|
|
||||||
|
results.ShouldBeEmpty();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task ListAll_Orders_ByGroupThenCluster()
|
||||||
|
{
|
||||||
|
var svc = new LdapGroupRoleMappingService(_db);
|
||||||
|
await svc.CreateAsync(Make("cn=b,dc=x", AdminRole.FleetAdmin), CancellationToken.None);
|
||||||
|
await svc.CreateAsync(Make("cn=a,dc=x", AdminRole.ConfigEditor, clusterId: "c2", isSystemWide: false), CancellationToken.None);
|
||||||
|
await svc.CreateAsync(Make("cn=a,dc=x", AdminRole.ConfigEditor, clusterId: "c1", isSystemWide: false), CancellationToken.None);
|
||||||
|
|
||||||
|
var results = await svc.ListAllAsync(CancellationToken.None);
|
||||||
|
|
||||||
|
results[0].LdapGroup.ShouldBe("cn=a,dc=x");
|
||||||
|
results[0].ClusterId.ShouldBe("c1");
|
||||||
|
results[1].ClusterId.ShouldBe("c2");
|
||||||
|
results[2].LdapGroup.ShouldBe("cn=b,dc=x");
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Delete_Removes_Matching_Row()
|
||||||
|
{
|
||||||
|
var svc = new LdapGroupRoleMappingService(_db);
|
||||||
|
var saved = await svc.CreateAsync(Make("cn=fleet,dc=x", AdminRole.FleetAdmin), CancellationToken.None);
|
||||||
|
|
||||||
|
await svc.DeleteAsync(saved.Id, CancellationToken.None);
|
||||||
|
|
||||||
|
var after = await svc.ListAllAsync(CancellationToken.None);
|
||||||
|
after.ShouldBeEmpty();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Delete_Unknown_Id_IsNoOp()
|
||||||
|
{
|
||||||
|
var svc = new LdapGroupRoleMappingService(_db);
|
||||||
|
|
||||||
|
await svc.DeleteAsync(Guid.NewGuid(), CancellationToken.None);
|
||||||
|
// no exception
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -30,6 +30,7 @@ public sealed class SchemaComplianceTests
|
|||||||
"NodeAcl", "ExternalIdReservation",
|
"NodeAcl", "ExternalIdReservation",
|
||||||
"DriverHostStatus",
|
"DriverHostStatus",
|
||||||
"DriverInstanceResilienceStatus",
|
"DriverInstanceResilienceStatus",
|
||||||
|
"LdapGroupRoleMapping",
|
||||||
};
|
};
|
||||||
|
|
||||||
var actual = QueryStrings(@"
|
var actual = QueryStrings(@"
|
||||||
|
|||||||
@@ -14,6 +14,7 @@
|
|||||||
<PackageReference Include="Shouldly" Version="4.3.0"/>
|
<PackageReference Include="Shouldly" Version="4.3.0"/>
|
||||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0"/>
|
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0"/>
|
||||||
<PackageReference Include="Microsoft.Data.SqlClient" Version="6.1.1"/>
|
<PackageReference Include="Microsoft.Data.SqlClient" Version="6.1.1"/>
|
||||||
|
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="10.0.0"/>
|
||||||
<PackageReference Include="xunit.runner.visualstudio" Version="3.0.2">
|
<PackageReference Include="xunit.runner.visualstudio" Version="3.0.2">
|
||||||
<PrivateAssets>all</PrivateAssets>
|
<PrivateAssets>all</PrivateAssets>
|
||||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||||
|
|||||||
Reference in New Issue
Block a user