Restructure solution layout by module
This commit is contained in:
@@ -0,0 +1,148 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace StellaOps.Notify.WebService.Options;
|
||||
|
||||
/// <summary>
|
||||
/// Strongly typed configuration for the Notify WebService host.
|
||||
/// </summary>
|
||||
public sealed class NotifyWebServiceOptions
|
||||
{
|
||||
public const string SectionName = "notify";
|
||||
|
||||
/// <summary>
|
||||
/// Schema version that downstream consumers can use to detect breaking changes.
|
||||
/// </summary>
|
||||
public int SchemaVersion { get; set; } = 1;
|
||||
|
||||
/// <summary>
|
||||
/// Authority / authentication configuration.
|
||||
/// </summary>
|
||||
public AuthorityOptions Authority { get; set; } = new();
|
||||
|
||||
/// <summary>
|
||||
/// Mongo storage configuration for configuration state and audit logs.
|
||||
/// </summary>
|
||||
public StorageOptions Storage { get; set; } = new();
|
||||
|
||||
/// <summary>
|
||||
/// Plug-in loader configuration.
|
||||
/// </summary>
|
||||
public PluginOptions Plugins { get; set; } = new();
|
||||
|
||||
/// <summary>
|
||||
/// HTTP API behaviour.
|
||||
/// </summary>
|
||||
public ApiOptions Api { get; set; } = new();
|
||||
|
||||
/// <summary>
|
||||
/// Telemetry configuration toggles.
|
||||
/// </summary>
|
||||
public TelemetryOptions Telemetry { get; set; } = new();
|
||||
|
||||
public sealed class AuthorityOptions
|
||||
{
|
||||
public bool Enabled { get; set; } = true;
|
||||
|
||||
public bool AllowAnonymousFallback { get; set; }
|
||||
|
||||
public string Issuer { get; set; } = "https://authority.local";
|
||||
|
||||
public string? MetadataAddress { get; set; }
|
||||
|
||||
public bool RequireHttpsMetadata { get; set; } = true;
|
||||
|
||||
public int BackchannelTimeoutSeconds { get; set; } = 30;
|
||||
|
||||
public int TokenClockSkewSeconds { get; set; } = 60;
|
||||
|
||||
public IList<string> Audiences { get; set; } = new List<string> { "notify" };
|
||||
|
||||
public string ReadScope { get; set; } = "notify.read";
|
||||
|
||||
public string AdminScope { get; set; } = "notify.admin";
|
||||
|
||||
/// <summary>
|
||||
/// Optional development signing key for symmetric JWT validation when Authority is disabled.
|
||||
/// </summary>
|
||||
public string? DevelopmentSigningKey { get; set; }
|
||||
}
|
||||
|
||||
public sealed class StorageOptions
|
||||
{
|
||||
public string Driver { get; set; } = "mongo";
|
||||
|
||||
public string ConnectionString { get; set; } = string.Empty;
|
||||
|
||||
public string Database { get; set; } = "notify";
|
||||
|
||||
public int CommandTimeoutSeconds { get; set; } = 30;
|
||||
}
|
||||
|
||||
public sealed class PluginOptions
|
||||
{
|
||||
public string? BaseDirectory { get; set; }
|
||||
|
||||
public string? Directory { get; set; }
|
||||
|
||||
public IList<string> SearchPatterns { get; set; } = new List<string>();
|
||||
|
||||
public IList<string> OrderedPlugins { get; set; } = new List<string>();
|
||||
}
|
||||
|
||||
public sealed class ApiOptions
|
||||
{
|
||||
public string BasePath { get; set; } = "/api/v1/notify";
|
||||
|
||||
public string InternalBasePath { get; set; } = "/internal/notify";
|
||||
|
||||
public string TenantHeader { get; set; } = "X-StellaOps-Tenant";
|
||||
|
||||
public RateLimitOptions RateLimits { get; set; } = new();
|
||||
}
|
||||
|
||||
public sealed class RateLimitOptions
|
||||
{
|
||||
public RateLimitPolicyOptions DeliveryHistory { get; set; } = RateLimitPolicyOptions.CreateDefault(
|
||||
tokenLimit: 60,
|
||||
tokensPerPeriod: 30,
|
||||
replenishmentPeriodSeconds: 60,
|
||||
queueLimit: 20);
|
||||
|
||||
public RateLimitPolicyOptions TestSend { get; set; } = RateLimitPolicyOptions.CreateDefault(
|
||||
tokenLimit: 5,
|
||||
tokensPerPeriod: 5,
|
||||
replenishmentPeriodSeconds: 60,
|
||||
queueLimit: 2);
|
||||
}
|
||||
|
||||
public sealed class RateLimitPolicyOptions
|
||||
{
|
||||
public bool Enabled { get; set; } = true;
|
||||
|
||||
public int TokenLimit { get; set; } = 10;
|
||||
|
||||
public int TokensPerPeriod { get; set; } = 10;
|
||||
|
||||
public int ReplenishmentPeriodSeconds { get; set; } = 60;
|
||||
|
||||
public int QueueLimit { get; set; } = 0;
|
||||
|
||||
public static RateLimitPolicyOptions CreateDefault(int tokenLimit, int tokensPerPeriod, int replenishmentPeriodSeconds, int queueLimit)
|
||||
{
|
||||
return new RateLimitPolicyOptions
|
||||
{
|
||||
TokenLimit = tokenLimit,
|
||||
TokensPerPeriod = tokensPerPeriod,
|
||||
ReplenishmentPeriodSeconds = replenishmentPeriodSeconds,
|
||||
QueueLimit = queueLimit
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class TelemetryOptions
|
||||
{
|
||||
public bool EnableRequestLogging { get; set; } = true;
|
||||
|
||||
public string MinimumLogLevel { get; set; } = "Information";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
|
||||
namespace StellaOps.Notify.WebService.Options;
|
||||
|
||||
internal static class NotifyWebServiceOptionsPostConfigure
|
||||
{
|
||||
public static void Apply(NotifyWebServiceOptions options, string contentRootPath)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(options);
|
||||
ArgumentNullException.ThrowIfNull(contentRootPath);
|
||||
|
||||
NormalizePluginOptions(options.Plugins, contentRootPath);
|
||||
}
|
||||
|
||||
private static void NormalizePluginOptions(NotifyWebServiceOptions.PluginOptions plugins, string contentRootPath)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(plugins);
|
||||
|
||||
var baseDirectory = plugins.BaseDirectory;
|
||||
if (string.IsNullOrWhiteSpace(baseDirectory))
|
||||
{
|
||||
baseDirectory = Path.Combine(contentRootPath, "..");
|
||||
}
|
||||
else if (!Path.IsPathRooted(baseDirectory))
|
||||
{
|
||||
baseDirectory = Path.GetFullPath(Path.Combine(contentRootPath, baseDirectory));
|
||||
}
|
||||
|
||||
plugins.BaseDirectory = baseDirectory;
|
||||
|
||||
if (string.IsNullOrWhiteSpace(plugins.Directory))
|
||||
{
|
||||
plugins.Directory = Path.Combine("plugins", "notify");
|
||||
}
|
||||
|
||||
if (!Path.IsPathRooted(plugins.Directory))
|
||||
{
|
||||
plugins.Directory = Path.Combine(baseDirectory, plugins.Directory);
|
||||
}
|
||||
|
||||
if (plugins.SearchPatterns.Count == 0)
|
||||
{
|
||||
plugins.SearchPatterns.Add("StellaOps.Notify.Connectors.*.dll");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,136 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
|
||||
namespace StellaOps.Notify.WebService.Options;
|
||||
|
||||
internal static class NotifyWebServiceOptionsValidator
|
||||
{
|
||||
public static void Validate(NotifyWebServiceOptions options)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(options);
|
||||
|
||||
ValidateStorage(options.Storage);
|
||||
ValidateAuthority(options.Authority);
|
||||
ValidateApi(options.Api);
|
||||
}
|
||||
|
||||
private static void ValidateStorage(NotifyWebServiceOptions.StorageOptions storage)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(storage);
|
||||
|
||||
var driver = storage.Driver ?? string.Empty;
|
||||
if (!string.Equals(driver, "mongo", StringComparison.OrdinalIgnoreCase) &&
|
||||
!string.Equals(driver, "memory", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
throw new InvalidOperationException($"Unsupported storage driver '{storage.Driver}'.");
|
||||
}
|
||||
|
||||
if (string.Equals(driver, "mongo", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(storage.ConnectionString))
|
||||
{
|
||||
throw new InvalidOperationException("notify:storage:connectionString must be provided.");
|
||||
}
|
||||
|
||||
if (string.IsNullOrWhiteSpace(storage.Database))
|
||||
{
|
||||
throw new InvalidOperationException("notify:storage:database must be provided.");
|
||||
}
|
||||
|
||||
if (storage.CommandTimeoutSeconds <= 0)
|
||||
{
|
||||
throw new InvalidOperationException("notify:storage:commandTimeoutSeconds must be positive.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void ValidateAuthority(NotifyWebServiceOptions.AuthorityOptions authority)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(authority);
|
||||
|
||||
if (authority.Enabled)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(authority.Issuer))
|
||||
{
|
||||
throw new InvalidOperationException("notify:authority:issuer must be provided when authority is enabled.");
|
||||
}
|
||||
|
||||
if (authority.Audiences is null || authority.Audiences.Count == 0)
|
||||
{
|
||||
throw new InvalidOperationException("notify:authority:audiences must include at least one value.");
|
||||
}
|
||||
|
||||
if (string.IsNullOrWhiteSpace(authority.AdminScope) || string.IsNullOrWhiteSpace(authority.ReadScope))
|
||||
{
|
||||
throw new InvalidOperationException("notify:authority admin and read scopes must be configured.");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(authority.DevelopmentSigningKey) || authority.DevelopmentSigningKey.Length < 32)
|
||||
{
|
||||
throw new InvalidOperationException("notify:authority:developmentSigningKey must be at least 32 characters when authority is disabled.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void ValidateApi(NotifyWebServiceOptions.ApiOptions api)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(api);
|
||||
|
||||
if (!api.BasePath.StartsWith("/", StringComparison.Ordinal))
|
||||
{
|
||||
throw new InvalidOperationException("notify:api:basePath must start with '/'.");
|
||||
}
|
||||
|
||||
if (!api.InternalBasePath.StartsWith("/", StringComparison.Ordinal))
|
||||
{
|
||||
throw new InvalidOperationException("notify:api:internalBasePath must start with '/'.");
|
||||
}
|
||||
|
||||
if (string.IsNullOrWhiteSpace(api.TenantHeader))
|
||||
{
|
||||
throw new InvalidOperationException("notify:api:tenantHeader must be provided.");
|
||||
}
|
||||
|
||||
ValidateRateLimits(api.RateLimits);
|
||||
}
|
||||
|
||||
private static void ValidateRateLimits(NotifyWebServiceOptions.RateLimitOptions rateLimits)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(rateLimits);
|
||||
|
||||
ValidatePolicy(rateLimits.DeliveryHistory, "notify:api:rateLimits:deliveryHistory");
|
||||
ValidatePolicy(rateLimits.TestSend, "notify:api:rateLimits:testSend");
|
||||
|
||||
static void ValidatePolicy(NotifyWebServiceOptions.RateLimitPolicyOptions options, string prefix)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(options);
|
||||
|
||||
if (!options.Enabled)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (options.TokenLimit <= 0)
|
||||
{
|
||||
throw new InvalidOperationException($"{prefix}:tokenLimit must be positive when enabled.");
|
||||
}
|
||||
|
||||
if (options.TokensPerPeriod <= 0)
|
||||
{
|
||||
throw new InvalidOperationException($"{prefix}:tokensPerPeriod must be positive when enabled.");
|
||||
}
|
||||
|
||||
if (options.ReplenishmentPeriodSeconds <= 0)
|
||||
{
|
||||
throw new InvalidOperationException($"{prefix}:replenishmentPeriodSeconds must be positive when enabled.");
|
||||
}
|
||||
|
||||
if (options.QueueLimit < 0)
|
||||
{
|
||||
throw new InvalidOperationException($"{prefix}:queueLimit cannot be negative.");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user