fix(transport): import diff compares secrets and bindings; preview honours the name map; CI lookups; area paths; no Rename where unsupported (TRN-2, TRN-3, TRN-6, TRN-7, TRN-8, TRN-9, WP2 tasks 2.4+2.5)

2.4 — ArtifactDiff:
- TRN-2: secret-bearing values (data-connection primary/backup config, DB
  connection string, external-system auth, SMTP credentials, SMS token) are
  compared by SHA-256 and reported value-free as <present> -> <changed>, so a
  changed endpoint no longer reads Identical and is force-skipped.
- TRN-6: the instance diff resolves each existing binding's DataConnectionId to
  its name and compares it (a rebind OPC1 -> OPC2 is Modified); State is no
  longer compared (the import never carries a live state).

2.5 — BundleImporter / ManagementActor / Central UI:
- TRN-3: PreviewAsync takes an optional BundleNameMap (additive optional param);
  under a MapToExisting redirect the connection/instance diffs, connection
  auto-match and unresolved-connection blocker run against the mapped target.
  The CLI import path passes its explicit --map-site/--map-connection redirects
  to the preview before the blocker gate; the Central UI re-runs the preview
  under the Map-step choices at Confirm. Apply binds a CreateNew site/connection
  whose identifier/name already exists to that row instead of inserting a
  duplicate the unique index rejects.
- TRN-7: connection, instance, instance-template and composition/base-template
  lookups match exact spelling first, then case-insensitively.
- TRN-8: InstanceDto gains the additive AreaPath (root->leaf); the exporter
  fills it and the importer resolves/creates areas level by level under their
  real parent. A legacy leaf-only bundle with an ambiguous leaf resolves
  deterministically and adds an ImportResult warning.
- TRN-9: Rename is rejected by ApplyAsync for Site, DataConnection,
  SmtpConfiguration and SmsConfiguration (ImportRenameSupport); the UI does not
  offer it there and the CLI rename policy falls back to Skip with a warning.

Also moves a doc comment misplaced by the 2.6 commit. Reflection round-trip
guard (RoundTripEquivalenceTests) re-run: green.
This commit is contained in:
Joseph Doherty
2026-12-03 10:11:07 -05:00
parent cbdd9fc40b
commit baecf10fea
16 changed files with 1391 additions and 169 deletions
@@ -237,15 +237,10 @@ public sealed class ArtifactDiff
// target's existing timeout untouched in that case.
AddIfDifferent(changes, "TimeoutSeconds", existing.TimeoutSeconds, incoming.TimeoutSeconds ?? existing.TimeoutSeconds);
// Secrets: presence-only comparison (we never echo the value in the diff).
var existingHasSecret = !string.IsNullOrEmpty(existing.AuthConfiguration);
var incomingHasSecret = incoming.Secrets is not null && incoming.Secrets.Values.ContainsKey("AuthConfiguration");
if (existingHasSecret != incomingHasSecret)
{
changes.Add(new FieldChange("Secrets.AuthConfiguration",
existingHasSecret ? "<present>" : null,
incomingHasSecret ? "<present>" : null));
}
// Secrets: compared by hash, reported presence-only (TRN-2 — the value is never
// echoed in the diff, but a changed value is no longer read as Identical).
AddSecretChange(changes, "Secrets.AuthConfiguration",
existing.AuthConfiguration, SecretValue(incoming.Secrets, "AuthConfiguration"));
// Methods are name-keyed children.
var existingForCompare = existingMethods ?? Array.Empty<ExternalSystemMethod>();
@@ -276,15 +271,10 @@ public sealed class ArtifactDiff
AddIfDifferent(changes, "MaxRetries", existing.MaxRetries, incoming.MaxRetries);
AddIfDifferent(changes, "RetryDelay", existing.RetryDelay.ToString(), incoming.RetryDelay.ToString());
// ConnectionString lives in Secrets only — presence-only comparison.
var existingHasSecret = !string.IsNullOrEmpty(existing.ConnectionString);
var incomingHasSecret = incoming.Secrets is not null && incoming.Secrets.Values.ContainsKey("ConnectionString");
if (existingHasSecret != incomingHasSecret)
{
changes.Add(new FieldChange("Secrets.ConnectionString",
existingHasSecret ? "<present>" : null,
incomingHasSecret ? "<present>" : null));
}
// ConnectionString lives in Secrets only — compared by hash, reported
// presence-only (TRN-2).
AddSecretChange(changes, "Secrets.ConnectionString",
existing.ConnectionString, SecretValue(incoming.Secrets, "ConnectionString"));
return BuildItem("DatabaseConnection", incoming.Name, changes);
}
@@ -345,14 +335,9 @@ public sealed class ArtifactDiff
AddIfDifferent(changes, "OAuth2Scope", existing.OAuth2Scope, incoming.OAuth2Scope);
AddIfDifferent(changes, "Transport", existing.Transport, incoming.Transport);
var existingHasSecret = !string.IsNullOrEmpty(existing.Credentials);
var incomingHasSecret = incoming.Secrets is not null && incoming.Secrets.Values.ContainsKey("Credentials");
if (existingHasSecret != incomingHasSecret)
{
changes.Add(new FieldChange("Secrets.Credentials",
existingHasSecret ? "<present>" : null,
incomingHasSecret ? "<present>" : null));
}
// TRN-2: compared by hash, reported presence-only.
AddSecretChange(changes, "Secrets.Credentials",
existing.Credentials, SecretValue(incoming.Secrets, "Credentials"));
return BuildItem("SmtpConfiguration", incoming.Host, changes);
}
@@ -379,14 +364,9 @@ public sealed class ArtifactDiff
AddIfDifferent(changes, "MaxRetries", existing.MaxRetries, incoming.MaxRetries);
AddIfDifferent(changes, "RetryDelay", existing.RetryDelay.ToString(), incoming.RetryDelay.ToString());
var existingHasSecret = !string.IsNullOrEmpty(existing.AuthToken);
var incomingHasSecret = incoming.Secrets is not null && incoming.Secrets.Values.ContainsKey("AuthToken");
if (existingHasSecret != incomingHasSecret)
{
changes.Add(new FieldChange("Secrets.AuthToken",
existingHasSecret ? "<present>" : null,
incomingHasSecret ? "<present>" : null));
}
// TRN-2: compared by hash, reported presence-only.
AddSecretChange(changes, "Secrets.AuthToken",
existing.AuthToken, SecretValue(incoming.Secrets, "AuthToken"));
return BuildItem("SmsConfiguration", incoming.AccountSid, changes);
}
@@ -481,13 +461,12 @@ public sealed class ArtifactDiff
AddIfDifferent(changes, "FailoverRetryCount", existing.FailoverRetryCount, incoming.FailoverRetryCount);
// Protocol config rides in Secrets (PrimaryConfiguration / BackupConfiguration).
// Presence-only comparison — never echo the config value.
AddSecretPresenceChange(changes, "Secrets.PrimaryConfiguration",
!string.IsNullOrEmpty(existing.PrimaryConfiguration),
HasSecretKey(incoming.Secrets, "PrimaryConfiguration"));
AddSecretPresenceChange(changes, "Secrets.BackupConfiguration",
!string.IsNullOrEmpty(existing.BackupConfiguration),
HasSecretKey(incoming.Secrets, "BackupConfiguration"));
// Compared by hash, reported presence-only — never echo the config value
// (TRN-2: a changed endpoint URL used to read Identical and be force-skipped).
AddSecretChange(changes, "Secrets.PrimaryConfiguration",
existing.PrimaryConfiguration, SecretValue(incoming.Secrets, "PrimaryConfiguration"));
AddSecretChange(changes, "Secrets.BackupConfiguration",
existing.BackupConfiguration, SecretValue(incoming.Secrets, "BackupConfiguration"));
return BuildItem("DataConnection", incoming.Name, changes);
}
@@ -510,13 +489,19 @@ public sealed class ArtifactDiff
/// <param name="existingTemplateName">The existing instance's template name (the entity carries only a FK), or null if unknown.</param>
/// <param name="existingSiteIdentifier">The existing instance's site identifier (the entity carries only a FK), or null if unknown.</param>
/// <param name="existingAreaName">The existing instance's area name (the entity carries only a FK), or null if unknown / unset.</param>
/// <param name="existingConnectionNameById">TRN-6: id→name of the existing instance's site connections, so a
/// binding's bound connection (stored as a FK) is compared by name. Null skips the connection comparison.</param>
/// <param name="existingAreaPath">TRN-8: the existing instance's root→leaf area path; compared instead of the
/// leaf name when the incoming instance carries an <see cref="InstanceDto.AreaPath"/>.</param>
/// <returns>An import preview item describing the conflict type and differences.</returns>
public ImportPreviewItem CompareInstance(
InstanceDto incoming,
Instance? existing,
string? existingTemplateName = null,
string? existingSiteIdentifier = null,
string? existingAreaName = null)
string? existingAreaName = null,
IReadOnlyDictionary<int, string>? existingConnectionNameById = null,
IReadOnlyList<string>? existingAreaPath = null)
{
ArgumentNullException.ThrowIfNull(incoming);
if (existing is null) return New("Instance", incoming.UniqueName);
@@ -533,8 +518,21 @@ public sealed class ArtifactDiff
{
AddIfDifferent(changes, "SiteIdentifier", existingSiteIdentifier, incoming.SiteIdentifier);
}
AddIfDifferent(changes, "AreaName", existingAreaName, incoming.AreaName);
AddIfDifferent(changes, "State", existing.State.ToString(), incoming.State.ToString());
if (incoming.AreaPath is { Count: > 0 } incomingPath && (existingAreaPath is not null || existing.AreaId is null))
{
// TRN-8: the same leaf name can sit under two parents — compare the full path.
AddIfDifferent(changes, "AreaPath",
existingAreaPath is null ? null : string.Join("/", existingAreaPath),
string.Join("/", incomingPath));
}
else
{
AddIfDifferent(changes, "AreaName", existingAreaName, incoming.AreaName);
}
// TRN-6: State is not compared — an import always writes NotDeployed for a new
// instance and never carries a live state across environments, so a State-only
// difference used to read Modified and an Overwrite then reset a live instance
// to NotDeployed for no configuration change.
DiffChildren(
existing.AttributeOverrides,
@@ -573,10 +571,14 @@ public sealed class ArtifactDiff
incoming.ConnectionBindings,
e => e.AttributeName,
i => i.AttributeName,
// ConnectionName resolves to a FK on the entity (DataConnectionId) that
// can't be compared cross-environment, so the binding diff compares only
// the per-attribute DataSourceReference override.
(e, i) => e.DataSourceReferenceOverride == i.DataSourceReferenceOverride,
// TRN-6: the entity binds by FK (DataConnectionId); the caller resolves it to
// the target connection's name, which is compared (case-insensitively, like
// the unique (SiteId, Name) index) together with the per-attribute
// DataSourceReference override. A rebind OPC1 -> OPC2 used to read Identical.
(e, i) => e.DataSourceReferenceOverride == i.DataSourceReferenceOverride
&& (existingConnectionNameById is null
|| (existingConnectionNameById.TryGetValue(e.DataConnectionId, out var boundName)
&& string.Equals(boundName, i.ConnectionName, StringComparison.OrdinalIgnoreCase))),
"ConnectionBindings",
changes);
@@ -657,24 +659,37 @@ public sealed class ArtifactDiff
};
/// <summary>True if <paramref name="secrets"/> carries the named key.</summary>
private static bool HasSecretKey(SecretsBlock? secrets, string key) =>
secrets is not null && secrets.Values.ContainsKey(key);
private static string? SecretValue(SecretsBlock? secrets, string key) =>
secrets is not null && secrets.Values.TryGetValue(key, out var value) ? value : null;
/// <summary>
/// Records a presence-only secret change (a flip in whether a value is set on
/// each side) using the <c>&lt;present&gt;</c> / null marker convention. The
/// secret value itself is NEVER read into the diff — only its presence.
/// TRN-2: compares a secret-bearing value by SHA-256 hash and reports the result
/// presence-only. A flip in presence uses the <c>&lt;present&gt;</c> / null markers;
/// a value present on both sides that DIFFERS is reported as
/// <c>&lt;present&gt;</c> → <c>&lt;changed&gt;</c>, so the row classifies Modified
/// (it used to read Identical and be force-skipped) while the value itself never
/// reaches the diff.
/// </summary>
private static void AddSecretPresenceChange(List<FieldChange> changes, string field, bool existingHasSecret, bool incomingHasSecret)
private static void AddSecretChange(List<FieldChange> changes, string field, string? existingValue, string? incomingValue)
{
if (existingHasSecret != incomingHasSecret)
var existingHas = !string.IsNullOrEmpty(existingValue);
var incomingHas = !string.IsNullOrEmpty(incomingValue);
if (existingHas != incomingHas)
{
changes.Add(new FieldChange(field,
existingHasSecret ? "<present>" : null,
incomingHasSecret ? "<present>" : null));
existingHas ? "<present>" : null,
incomingHas ? "<present>" : null));
return;
}
if (existingHas && !SecretHash(existingValue!).AsSpan().SequenceEqual(SecretHash(incomingValue!)))
{
changes.Add(new FieldChange(field, "<present>", "<changed>"));
}
}
private static byte[] SecretHash(string value) =>
System.Security.Cryptography.SHA256.HashData(System.Text.Encoding.UTF8.GetBytes(value));
private static void DiffChildren<TExisting, TIncoming>(
IEnumerable<TExisting> existing,
IEnumerable<TIncoming> incoming,