Read the Heimdall app secret from Heimdall__AppSecretFile; env value deprecated
The file wins, is read once when the options are built, and a set-but-missing, unreadable or empty file stops startup in every environment without naming the secret. Heimdall__AppSecret still works and logs a deprecation warning. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
b80b27da0f
commit
95d2e8d262
5 changed files with 201 additions and 2 deletions
|
|
@ -14,7 +14,11 @@ public sealed class HeimdallOptions
|
|||
|
||||
public string PatreonTierTitle { get; init; } = "Inner Sanctum";
|
||||
|
||||
public string AppSecret { get; init; } = string.Empty;
|
||||
/// <summary>Deprecated: rides in the process environment. Use <see cref="AppSecretFile"/>; the file wins when both are set.</summary>
|
||||
public string AppSecret { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>Path of a file holding the app secret (systemd <c>LoadCredential</c> style: <c>%d/heimdall-app-secret</c>).</summary>
|
||||
public string AppSecretFile { get; init; } = string.Empty;
|
||||
|
||||
public string PatronSupportIntakeSecret { get; init; } = string.Empty;
|
||||
|
||||
|
|
@ -39,4 +43,37 @@ public sealed class HeimdallOptions
|
|||
IsConfigured &&
|
||||
EnablePatronSupportIntake &&
|
||||
!string.IsNullOrWhiteSpace(PatronSupportIntakeSecret);
|
||||
|
||||
/// <summary>
|
||||
/// Resolves <see cref="AppSecret"/> once, when the options are built. A set <see cref="AppSecretFile"/> must be a
|
||||
/// readable, non-empty file, or startup fails; the message names the option and path, never a secret.
|
||||
/// </summary>
|
||||
public static void ResolveAppSecret(HeimdallOptions options, ILogger logger)
|
||||
{
|
||||
if (!string.IsNullOrWhiteSpace(options.AppSecret))
|
||||
{
|
||||
logger.LogWarning("Heimdall:AppSecret is deprecated: it rides in the process environment. Use Heimdall:AppSecretFile, which wins when both are set.");
|
||||
}
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(options.AppSecretFile))
|
||||
{
|
||||
string secret;
|
||||
try
|
||||
{
|
||||
secret = File.ReadAllText(options.AppSecretFile).TrimEnd('\r', '\n');
|
||||
}
|
||||
catch (Exception error) when (error is IOException or UnauthorizedAccessException or ArgumentException or NotSupportedException)
|
||||
{
|
||||
throw new InvalidOperationException(
|
||||
$"Heimdall:AppSecretFile must name a readable file; '{options.AppSecretFile}' could not be read ({error.GetType().Name}).");
|
||||
}
|
||||
|
||||
if (string.IsNullOrWhiteSpace(secret))
|
||||
{
|
||||
throw new InvalidOperationException($"Heimdall:AppSecretFile '{options.AppSecretFile}' is empty.");
|
||||
}
|
||||
|
||||
options.AppSecret = secret;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -22,6 +22,9 @@ public sealed class StartupConfigurationValidator(
|
|||
{
|
||||
var failures = new List<string>();
|
||||
|
||||
// Building the options resolves Heimdall:AppSecretFile, and a bad file stops startup in every environment.
|
||||
_ = heimdallOptions.Value;
|
||||
|
||||
// Checked in every environment: a bad value here would otherwise pass startup and throw on every cast.
|
||||
var conceptVotes = conceptVoteOptions.Value;
|
||||
if (!NetworkPrefix.IsValidLength(conceptVotes.IPv4PrefixLength, NetworkPrefix.IPv4Bits))
|
||||
|
|
|
|||
|
|
@ -64,7 +64,8 @@ builder.Services
|
|||
|
||||
builder.Services
|
||||
.AddOptions<HeimdallOptions>()
|
||||
.BindConfiguration(HeimdallOptions.SectionName);
|
||||
.BindConfiguration(HeimdallOptions.SectionName)
|
||||
.PostConfigure<ILogger<HeimdallOptions>>(HeimdallOptions.ResolveAppSecret);
|
||||
|
||||
builder.Services
|
||||
.AddOptions<StripeOptions>()
|
||||
|
|
|
|||
|
|
@ -28,6 +28,7 @@
|
|||
"BaseUrl": "https://heimdall.gamecult.org",
|
||||
"AppSlug": "bifrost",
|
||||
"AppSecret": "",
|
||||
"AppSecretFile": "",
|
||||
"DiscordGuildId": "",
|
||||
"DiscordAllowedRoleIds": [],
|
||||
"PatreonTierTitle": "Inner Sanctum",
|
||||
|
|
|
|||
Loading…
Reference in a new issue