diff options
| author | JP Dillingham <jpdillingham@gmail.com> | 2021-01-18 22:45:04 -0600 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-01-18 22:45:04 -0600 |
| commit | 60c35d5ed0b3946c63783afd2b234a0eeb8d6567 (patch) | |
| tree | edc61454899c1eed9137bfae2243359876286eea | |
| parent | 9a56c438f08238fef633a30124d853c18445803e (diff) | |
| parent | ee41d6e78859cbf498160fddc7d1ffa14d49f4b6 (diff) | |
| download | slskd-60c35d5ed0b3946c63783afd2b234a0eeb8d6567.tar.gz | |
Merge pull request #21 from slskd/opt-in-features
Make Swagger and Prometheus opt-in, authentication opt-out
| -rw-r--r-- | .github/configuration/changelog.json | 10 | ||||
| -rw-r--r-- | slskd.sln | 6 | ||||
| -rw-r--r-- | src/slskd/Controllers/SessionController.cs | 2 | ||||
| -rw-r--r-- | src/slskd/Program.cs | 54 | ||||
| -rw-r--r-- | src/slskd/Startup.cs | 66 |
5 files changed, 97 insertions, 41 deletions
diff --git a/.github/configuration/changelog.json b/.github/configuration/changelog.json index c7072363..d48dbbf6 100644 --- a/.github/configuration/changelog.json +++ b/.github/configuration/changelog.json @@ -5,14 +5,22 @@ "labels": ["feature"] }, { + "title": "### Enhancements", + "labels": ["enhancement"] + }, + { "title": "### Bug Fixes", - "labels": ["fix"] + "labels": ["bug", "fix"] }, { "title": "### Refactoring", "labels": ["refactor"] }, { + "title": "### Documentation", + "labels": ["documentation"] + }, + { "title": "### Tests", "labels": ["tests"] } @@ -37,6 +37,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "slskd.Tests.Unit", "tests\s EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "slskd.Tests.Integration", "tests\slskd.Tests.Integration\slskd.Tests.Integration.csproj", "{6BF0549C-F88D-43F9-85EE-388BA5FC38D0}" EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "configuration", "configuration", "{CFC23637-4439-4C7B-ACF4-AF04B2E65315}" + ProjectSection(SolutionItems) = preProject + .github\configuration\changelog.json = .github\configuration\changelog.json + EndProjectSection +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -64,6 +69,7 @@ Global {ED2A1CCD-9C47-4AB5-AF00-84EF507E2402} = {6186A99C-FE43-4C97-8A6A-F27432F44282} {2777623C-3DBE-4EB4-A092-DFEA7BD8EA3A} = {137724E1-1613-4EFC-BD67-399A620AEABF} {6BF0549C-F88D-43F9-85EE-388BA5FC38D0} = {137724E1-1613-4EFC-BD67-399A620AEABF} + {CFC23637-4439-4C7B-ACF4-AF04B2E65315} = {6186A99C-FE43-4C97-8A6A-F27432F44282} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {A793ED59-76F4-4874-846B-2A199C92A48E} diff --git a/src/slskd/Controllers/SessionController.cs b/src/slskd/Controllers/SessionController.cs index 0f849a2a..5c162708 100644 --- a/src/slskd/Controllers/SessionController.cs +++ b/src/slskd/Controllers/SessionController.cs @@ -34,7 +34,7 @@ [ProducesResponseType(typeof(bool), 200)] public IActionResult Enabled() { - return Ok(Startup.EnableSecurity); + return Ok(!Program.DisableAuthentication); } /// <summary> diff --git a/src/slskd/Program.cs b/src/slskd/Program.cs index 82dacf5c..ad401abc 100644 --- a/src/slskd/Program.cs +++ b/src/slskd/Program.cs @@ -25,11 +25,20 @@ public static bool Debug { get; private set; } [Argument('n', "no-logo", "suppress logo on startup")] - public static bool NoLogo { get; private set; } + public static bool DisableLogo { get; private set; } + [Argument('x', "no-auth", "disable authentication for web requests")] + public static bool DisableAuthentication { get; private set; } + [Argument('i', "instance-name", "optional; a unique name for this instance")] public static string InstanceName { get; private set; } - + + [Argument('p', "prometheus", "enable collection and publish of prometheus metrics")] + public static bool EnablePrometheus { get; private set; } + + [Argument('s', "swagger", "enable swagger documentation")] + public static bool EnableSwagger { get; private set; } + [Argument(default, "loki", "the url to a Grafana Loki instance to log to")] public static string LoggerLokiUrl { get; private set; } @@ -65,17 +74,27 @@ if (ShowHelp) { - if (!NoLogo) PrintBanner(Version); + if (!DisableLogo) + { + PrintBanner(Version); + } + PrintHelp(); return; } - Debug = Debugger.IsAttached - || Debug - || Environment.GetEnvironmentVariable("SLSKD_DEBUG") != null; + static bool IsSet(string envar) + => (Environment.GetEnvironmentVariable(envar)?.Equals("true", StringComparison.InvariantCultureIgnoreCase) ?? false); + + Debug = Debugger.IsAttached || Debug || IsSet("SLSKD_DEBUG"); + DisableLogo = DisableLogo || IsSet("SLSKD_NO_LOGO"); + DisableAuthentication = DisableAuthentication || IsSet("SLSKD_NO_AUTH"); InstanceName ??= Environment.GetEnvironmentVariable("SLSKD_INSTANCE_NAME") ?? "default"; + EnableSwagger = EnableSwagger || IsSet("SLSKD_SWAGGER"); + EnablePrometheus = EnablePrometheus || IsSet("SLSKD_PROMETHEUS"); + LoggerLokiUrl ??= Environment.GetEnvironmentVariable("SLSKD_LOGGER_LOKI_URL"); LoggerLokiEnabled = !string.IsNullOrEmpty(LoggerLokiUrl); @@ -129,7 +148,7 @@ .CreateLogger(); } - if (!NoLogo) + if (!DisableLogo) { PrintBanner(Version); } @@ -141,6 +160,21 @@ logger.Information("Invocation ID: {InvocationId}", InvocationId); logger.Information("Process ID: {ProcessId}", ProcessId); + if (DisableAuthentication) + { + logger.Warning("Authentication of web requests is DISABLED"); + } + + if (EnablePrometheus) + { + logger.Information("Publishing Prometheus metrics to /metrics"); + } + + if (EnableSwagger) + { + logger.Information("Publishing Swagger documentation to /swagger"); + } + if (LoggerLokiEnabled) { logger.Information("Logging to Loki instance at {LoggerLokiUrl}", LoggerLokiUrl); @@ -148,7 +182,11 @@ try { - using var runtimeMetrics = DotNetRuntimeStatsBuilder.Default().StartCollecting(); + if (EnablePrometheus) + { + using var runtimeMetrics = DotNetRuntimeStatsBuilder.Default().StartCollecting(); + } + CreateWebHostBuilder(args).Build().Run(); } catch (Exception ex) diff --git a/src/slskd/Startup.cs b/src/slskd/Startup.cs index b6ef0788..37a3c7cd 100644 --- a/src/slskd/Startup.cs +++ b/src/slskd/Startup.cs @@ -48,12 +48,10 @@ internal static DiagnosticLevel DiagnosticLevel { get; set; } internal static int ConnectTimeout { get; set; } internal static int InactivityTimeout { get; set; } - internal static bool EnableSecurity { get; set; } internal static int SecurityTokenTTL { get; set; } internal static int RoomMessageLimit { get; set; } internal static int ReadBufferSize { get; set; } internal static int WriteBufferSize { get; set; } - internal static bool EnableSwagger { get; set; } internal static string XmlDocFile { get; set; } internal static SymmetricSecurityKey JwtSigningKey { get; set; } @@ -79,12 +77,10 @@ DiagnosticLevel = Configuration.GetValue<DiagnosticLevel>("DIAGNOSTIC", DiagnosticLevel.Info); ConnectTimeout = Configuration.GetValue<int>("CONNECT_TIMEOUT", 5000); InactivityTimeout = Configuration.GetValue<int>("INACTIVITY_TIMEOUT", 15000); - EnableSecurity = Configuration.GetValue<bool>("ENABLE_SECURITY", true); SecurityTokenTTL = Configuration.GetValue<int>("SECURITY_TOKEN_TTL", 604800000); // 7 days RoomMessageLimit = Configuration.GetValue<int>("ROOM_MESSAGE_LIMIT", 250); ReadBufferSize = Configuration.GetValue<int>("READ_BUFFER_SIZE", 16384); WriteBufferSize = Configuration.GetValue<int>("WRITE_BUFFER_SIZE", 16384); - EnableSwagger = Configuration.GetValue<bool>("ENABLE_SWAGGER", true); XmlDocFile = Configuration.GetValue<string>("XML_DOC_FILE", Path.Combine(AppContext.BaseDirectory, typeof(Startup).GetTypeInfo().Assembly.GetName().Name + ".xml")); JwtSigningKey = new SymmetricSecurityKey(PBKDF2.GetKey(Password)); @@ -107,7 +103,7 @@ { services.AddCors(options => options.AddPolicy("AllowAll", builder => builder.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader())); - if (EnableSecurity) + if (!Program.DisableAuthentication) { services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) .AddJwtBearer(options => @@ -143,34 +139,34 @@ options.JsonSerializerOptions.IgnoreNullValues = true; }); - if (EnableSwagger) + services.AddApiVersioning(options => options.ReportApiVersions = true); + services.AddVersionedApiExplorer(options => { - services.AddApiVersioning(options => options.ReportApiVersions = true); - services.AddVersionedApiExplorer(options => - { - options.GroupNameFormat = "'v'VVV"; - options.SubstituteApiVersionInUrl = true; - }); - - services.AddSwaggerGen(options => - { - options.DescribeAllParametersInCamelCase(); - options.SwaggerDoc("v0", - new OpenApiInfo - { - Title = "slskd", - Version = "v0" - } - ); + options.GroupNameFormat = "'v'VVV"; + options.SubstituteApiVersionInUrl = true; + }); - if (System.IO.File.Exists(XmlDocFile)) + services.AddSwaggerGen(options => + { + options.DescribeAllParametersInCamelCase(); + options.SwaggerDoc("v0", + new OpenApiInfo { - options.IncludeXmlComments(XmlDocFile); + Title = "slskd", + Version = "v0" } - }); - } + ); - services.AddSystemMetrics(); + if (System.IO.File.Exists(XmlDocFile)) + { + options.IncludeXmlComments(XmlDocFile); + } + }); + + if (Program.EnablePrometheus) + { + services.AddSystemMetrics(); + } services.AddSingleton<ISoulseekClient, SoulseekClient>(serviceProvider => Client); services.AddSingleton<ITransferTracker, TransferTracker>(); @@ -223,7 +219,11 @@ app.UseFileServer(fileServerOptions); app.UseSerilogRequestLogging(); - app.UseHttpMetrics(); + + if (Program.EnablePrometheus) + { + app.UseHttpMetrics(); + } app.UseAuthentication(); @@ -232,10 +232,14 @@ app.UseEndpoints(endpoints => { endpoints.MapControllers(); - endpoints.MapMetrics(); + + if (Program.EnablePrometheus) + { + endpoints.MapMetrics(); + } }); - if (EnableSwagger) + if (Program.EnableSwagger) { app.UseSwagger(); app.UseSwaggerUI(options => provider.ApiVersionDescriptions.ToList() |
