Class IntegrityServiceCollectionExtensions
- Namespace
- Microsoft.Extensions.DependencyInjection
- Assembly
- Stratara.Infrastructure.dll
Service-collection extensions that wire HMAC integrity protection for bus envelopes.
public static class IntegrityServiceCollectionExtensions
- Inheritance
-
IntegrityServiceCollectionExtensions
- Inherited Members
Methods
AddBusEnvelopeIntegrity(IServiceCollection, IConfiguration)
Overload that binds BusEnvelopeIntegrityOptions from configuration section
SectionName ("BusEnvelopeIntegrity").
public static IServiceCollection AddBusEnvelopeIntegrity(this IServiceCollection services, IConfiguration configuration)
Parameters
servicesIServiceCollectionThe service collection to mutate.
configurationIConfigurationThe configuration root to bind from.
Returns
- IServiceCollection
The same service collection, to enable chaining.
Examples
Binds Mode from the BusEnvelopeIntegrity section and leaves the key to the caller,
so a host using this overload still has to assign SharedKey:
// appsettings.json: { "BusEnvelopeIntegrity": { "Mode": "Permissive" } }
services.AddBusEnvelopeIntegrity(configuration);
services.Configure<BusEnvelopeIntegrityOptions>(
o => o.SharedKey = Convert.FromBase64String(configuration["BusEnvelopeIntegrity:SharedKey"]!));
Exceptions
- ArgumentNullException
configurationisnull.
AddBusEnvelopeIntegrity(IServiceCollection, Action<BusEnvelopeIntegrityOptions>)
Opt in to HMAC integrity protection on every bus envelope. The framework signs outbound
CommandEnvelope and EventBundle instances and verifies inbound envelopes
according to Mode.
public static IServiceCollection AddBusEnvelopeIntegrity(this IServiceCollection services, Action<BusEnvelopeIntegrityOptions> configure)
Parameters
servicesIServiceCollectionThe service collection to mutate.
configureAction<BusEnvelopeIntegrityOptions>Callback that populates BusEnvelopeIntegrityOptions.
Returns
- IServiceCollection
The same service collection, to enable chaining.
Examples
The key is a byte[] of at least 32 bytes and must be identical on every host that shares
the bus. Read it from a secret store rather than from appsettings.json:
services.AddBusEnvelopeIntegrity(options =>
{
options.Mode = BusEnvelopeIntegrityMode.Strict;
options.SharedKey = Convert.FromBase64String(configuration["BusEnvelopeIntegrity:SharedKey"]!);
});
Remarks
When this method is not called, IBusEnvelopeSigner stays unregistered and
the framework behaves exactly as it did before: envelopes carry no signature, the
dispatcher does not verify, and Mode resolves to
Off. The threat model trade-off is documented in
SECURITY.md.
Every publisher and consumer that share a bus must agree on SharedKey and the active mode. When rolling out integrity to an existing fleet, deploy in two phases:
- Publishers + consumers on Permissive — new envelopes carry a signature, unsigned in-flight envelopes are still accepted with a warning log.
- Once every host is signing, switch both fleets to Strict — unsigned or tampered envelopes are rejected.
Exceptions
- ArgumentNullException
configureisnull.
AddBusEnvelopeIntegrity(IServiceCollection, string, BusEnvelopeIntegrityMode)
Overload that takes the shared key as a base64 string and the enforcement mode directly — the shape a host reaches for first, and the one the start-up probe points at.
public static IServiceCollection AddBusEnvelopeIntegrity(this IServiceCollection services, string base64SharedKey, BusEnvelopeIntegrityMode mode = BusEnvelopeIntegrityMode.Strict)
Parameters
servicesIServiceCollectionThe service collection to mutate.
base64SharedKeystringThe HMAC shared secret, base64-encoded. Decodes to at least 32 bytes and must be identical on every host that shares the bus.
modeBusEnvelopeIntegrityModeEnforcement mode. Defaults to Strict.
Returns
- IServiceCollection
The same service collection, to enable chaining.
Examples
services.AddBusEnvelopeIntegrity(
builder.Configuration["BUS_ENVELOPE_SIGNING_KEY"]!,
BusEnvelopeIntegrityMode.Permissive);
Remarks
Where the key comes from matters more than this overload does. Read it from a secret
store, a key vault or an environment variable injected at deploy time — not from a checked-in
appsettings.json. A signing key in source control signs for anyone who can read the
repository, which is the threat this option exists to close.
The two other overloads remain the right choice when the key is already a byte[], or
when everything but the key is bound from configuration.
Exceptions
- ArgumentNullException
base64SharedKeyisnull.- ArgumentException
base64SharedKeyis not valid base64, or decodes to fewer than 32 bytes. Both are checked here rather than at the first publish, so a mistyped secret fails at registration instead of on a message.