aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJP Dillingham <jpdillingham@gmail.com>2021-01-18 22:45:04 -0600
committerGitHub <noreply@github.com>2021-01-18 22:45:04 -0600
commit60c35d5ed0b3946c63783afd2b234a0eeb8d6567 (patch)
treeedc61454899c1eed9137bfae2243359876286eea /src
parent9a56c438f08238fef633a30124d853c18445803e (diff)
parentee41d6e78859cbf498160fddc7d1ffa14d49f4b6 (diff)
downloadslskd-60c35d5ed0b3946c63783afd2b234a0eeb8d6567.tar.gz
Merge pull request #21 from slskd/opt-in-features
Make Swagger and Prometheus opt-in, authentication opt-out
Diffstat (limited to 'src')
-rw-r--r--src/slskd/Controllers/SessionController.cs2
-rw-r--r--src/slskd/Program.cs54
-rw-r--r--src/slskd/Startup.cs66
3 files changed, 82 insertions, 40 deletions
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()