Wire opt-in write-only Garage S3 mirror after authoritative FSAssets commits with SHA-256 guards

This commit is contained in:
OpenGenesis (MPL 2.0) committed 2026-11-16 18:04:08 +01:00
1 parent e634d67d77
commit b4b4a70293
1 file changed
+57
@@ -1,6 +1,8 @@
using System;
using System.IO;
using System.Threading;
using System.Security.Cryptography;
using OpenGenesisLINK.Storage.S3;
using OpenMetaverse;
using Nini.Config;
using OpenSim.Services.FSAssetService;
@@ -28,6 +30,12 @@ namespace OpenGenesisLINK.Addons.AssetService
private readonly bool localCacheEnabled;
private readonly bool atomicUpdatesEnabled;
private readonly OGLParallelAssetStore parallelStore;
private readonly OglS3ObjectMirror s3Mirror;
private long s3MirrorSucceeded;
private long s3MirrorFailed;
public bool S3MirrorEnabled => s3Mirror != null;
public long S3MirrorSucceeded => Interlocked.Read(ref s3MirrorSucceeded);
public long S3MirrorFailed => Interlocked.Read(ref s3MirrorFailed);
private readonly bool parallelReadPreferred;
private long parallelMirrored;
private long parallelReadHits;
@@ -133,6 +141,15 @@ namespace OpenGenesisLINK.Addons.AssetService
parallelStore = new OGLParallelAssetStore(objectPath, indexPath);
}
// Write-only S3 shadow mirror: no authoritative reads or cutover.
// Environment-only secrets; disabled unless explicitly opted in.
if (settings.GetBoolean("S3MirrorEnabled", false))
{
if (settings.GetBoolean("S3ReadPreferred", false))
throw new InvalidOperationException("S3 reads are not enabled until authoritative SQL hash checks are implemented.");
s3Mirror = OglS3ObjectMirror.FromEnvironment();
}
string mode = settings.GetString("Mode", "compatibility");
if (!String.Equals(mode, "compatibility", StringComparison.OrdinalIgnoreCase))
throw new NotSupportedException(
@@ -297,6 +314,8 @@ namespace OpenGenesisLINK.Addons.AssetService
}
if (updated && parallelStore != null)
TryMirrorCommitted(id);
if (updated && s3Mirror != null)
TryMirrorS3Committed(id);
return updated;
}
catch (Exception)
@@ -350,6 +369,8 @@ namespace OpenGenesisLINK.Addons.AssetService
if (parallelStore != null &&
Guid.TryParse(id, out Guid committedId) && committedId != Guid.Empty)
TryMirrorCommitted(id);
if (s3Mirror != null && Guid.TryParse(id, out Guid s3CommittedId) && s3CommittedId != Guid.Empty)
TryMirrorS3Committed(id);
return id;
}
finally
@@ -432,6 +453,42 @@ namespace OpenGenesisLINK.Addons.AssetService
}
}
// S3 is an advisory, write-only mirror. All errors are isolated from
// the committed FSAssets write and do not change viewer behavior.
private void TryMirrorS3Committed(string id)
{
try
{
string expectedHash;
if (m_DataConnector.Get(id, out expectedHash) == null)
return;
byte[] payload = base.GetData(id);
if (payload == null ||
!String.Equals(Convert.ToHexString(SHA256.HashData(payload)),
expectedHash, StringComparison.OrdinalIgnoreCase))
{
Interlocked.Increment(ref s3MirrorFailed);
return;
}
string storedHash = s3Mirror.MirrorAsync(payload).GetAwaiter().GetResult();
string freshHash;
if (!String.Equals(storedHash, expectedHash, StringComparison.OrdinalIgnoreCase) ||
m_DataConnector.Get(id, out freshHash) == null ||
!String.Equals(expectedHash, freshHash, StringComparison.OrdinalIgnoreCase))
{
Interlocked.Increment(ref s3MirrorFailed);
return;
}
Interlocked.Increment(ref s3MirrorSucceeded);
}
catch (Exception)
{
// No bucket names, endpoint, object keys or credentials in logs.
Interlocked.Increment(ref s3MirrorFailed);
OglLog.Warn("[OGL-ASSETS]: S3 shadow mirror failed; authoritative FSAssets write preserved.");
}
}
private void InvalidateLocal(string id)
{
if (String.IsNullOrWhiteSpace(id)) return;