Skip to content

Support license key via environment variable fallback#1175

Merged
jbogard merged 2 commits into
mainfrom
feature/license-key-env-var
Jul 2, 2026
Merged

Support license key via environment variable fallback#1175
jbogard merged 2 commits into
mainfrom
feature/license-key-env-var

Conversation

@jbogard

@jbogard jbogard commented Jul 1, 2026

Copy link
Copy Markdown
Collaborator

What Changed

Added support for reading the MediatR license key from an environment variable instead of requiring it to be set in code. Ports LuckyPennySoftware/AutoMapper#4634 to MediatR.

Why

In containerized and cloud environments it's preferable to supply sensitive configuration through environment variables rather than hardcoding it. It also lets enterprises share a single key across many services without code changes.

How It Works

If no license key is set in code, MediatR resolves one from environment variables. The key is resolved using the first non-blank value in this order of precedence:

  1. An explicit value set in code (cfg.LicenseKey or Mediator.LicenseKey).
  2. The MEDIATR_LICENSE_KEY environment variable (product-specific).
  3. The LUCKYPENNY_LICENSE_KEY environment variable (shared across Lucky Penny products; for example, AutoMapper reads the same variable).

Blank/whitespace values are treated as unconfigured and fall through, consistent with #1170.

Notes on adapting from the AutoMapper PR

  • MediatR has two explicit key sources (the config instance and static Mediator.LicenseKey) where AutoMapper has one, so ResolveLicenseKey accepts params string?[] to cover both.
  • Preserved MediatR's blank-as-unconfigured semantics: the resolver skips blank values rather than only null ones, so a blank explicit key correctly falls through to the environment variables.
  • The AutoMapper PR's ci.yml changes were not ported — they're AutoMapper-specific, and MediatR already has fork-safe CI (commits b28cfac, 329ce79).

Tests

New LicenseKeyEnvironmentVariableTests covering explicit-key precedence, config-vs-static precedence, per-variable fallback, blank handling, and the all-unset case. Parallelization is disabled for the class since it mutates process-global environment variables.

Backward Compatibility

100% backward compatible — existing code that sets the license key continues to work unchanged.

🤖 Generated with Claude Code

If no license key is set in code, MediatR now resolves one from
environment variables: the product-specific MEDIATR_LICENSE_KEY, then
the shared LUCKYPENNY_LICENSE_KEY (usable across Lucky Penny products).

Resolution uses the first non-blank value in order of precedence:
explicit config key, static Mediator.LicenseKey, MEDIATR_LICENSE_KEY,
then LUCKYPENNY_LICENSE_KEY. Blank values are treated as unconfigured
and fall through, consistent with #1170.

Ports LuckyPennySoftware/AutoMapper#4634 to MediatR.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings July 1, 2026 18:13

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds environment-variable fallback for MediatR license key resolution so container/cloud deployments can supply the key without code changes, while preserving existing “blank means unconfigured” semantics.

Changes:

  • Introduces LicenseAccessor.ResolveLicenseKey(...) to resolve the first non-blank key from explicit config/static sources, then environment variables.
  • Adds tests covering precedence, blank handling, and the all-unset case (with parallelization intended to be disabled due to process-global env var mutation).
  • Updates README to document MEDIATR_LICENSE_KEY and LUCKYPENNY_LICENSE_KEY discovery and precedence.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
test/MediatR.Tests/Licensing/LicenseKeyEnvironmentVariableTests.cs Adds unit tests for environment-variable license key fallback and precedence rules.
src/MediatR/Licensing/LicenseAccessor.cs Implements env-var fallback via a resolver and applies blank-as-unconfigured behavior in initialization.
README.md Documents environment-variable auto-discovery and precedence order.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread test/MediatR.Tests/Licensing/LicenseKeyEnvironmentVariableTests.cs
Comment thread src/MediatR/Licensing/LicenseAccessor.cs Outdated
- Move DisableParallelization to a dedicated collection-definition class
  and apply only [Collection] to the test class, matching the existing
  ServiceFactoryCollectionBehavior pattern.
- Rewrite ResolveLicenseKey imperatively so environment variables are
  only read when no explicit key is configured, avoiding eager reads.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@jbogard
jbogard merged commit 916ef1b into main Jul 2, 2026
6 checks passed
@jbogard
jbogard deleted the feature/license-key-env-var branch July 2, 2026 13:34
This was referenced Jul 2, 2026
This was referenced Jul 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants