aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJP Dillingham <jp@dillingham.me>2023-03-21 17:26:05 -0500
committerGitHub <noreply@github.com>2023-03-21 17:26:05 -0500
commit664b01cfc85d7c71ad40049bd2f2066916de456c (patch)
tree063f9f4d5e2b13899aac7faccc03d5fea5c0b479 /src
parentbd4c10a559e9347be1313c544c4a590e41edca19 (diff)
parent388e567bdf78eb7aade2db7187e1e806d172d932 (diff)
downloadslskd-664b01cfc85d7c71ad40049bd2f2066916de456c.tar.gz
Merge pull request #892 from slskd/underscores
Use `instance_name` from Relay Agent config instead of map key
Diffstat (limited to 'src')
-rw-r--r--src/slskd/Common/Authentication/AuthRole.cs33
-rw-r--r--src/slskd/Common/Authentication/Role.cs3
-rw-r--r--src/slskd/Core/API/Controllers/ApplicationController.cs4
-rw-r--r--src/slskd/Core/API/Controllers/OptionsController.cs8
-rw-r--r--src/slskd/Core/Options.cs17
-rw-r--r--src/slskd/Program.cs2
-rw-r--r--src/slskd/Relay/API/Controllers/RelayController.cs10
-rw-r--r--src/slskd/Relay/API/Hubs/RelayHub.cs21
-rw-r--r--src/slskd/Relay/RelayService.cs18
9 files changed, 95 insertions, 21 deletions
diff --git a/src/slskd/Common/Authentication/AuthRole.cs b/src/slskd/Common/Authentication/AuthRole.cs
new file mode 100644
index 00000000..0550c085
--- /dev/null
+++ b/src/slskd/Common/Authentication/AuthRole.cs
@@ -0,0 +1,33 @@
+// <copyright file="AuthRole.cs" company="slskd Team">
+// Copyright (c) slskd Team. All rights reserved.
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Affero General Public License as published
+// by the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Affero General Public License for more details.
+//
+// You should have received a copy of the GNU Affero General Public License
+// along with this program. If not, see https://www.gnu.org/licenses/.
+// </copyright>
+
+namespace slskd
+{
+ /// <summary>
+ /// Authentication roles.
+ /// </summary>
+ /// <remarks>
+ /// For use in the <see cref="Microsoft.AspNetCore.Authorization.AuthorizeAttribute"/> attribute, specifically.
+ /// </remarks>
+ public static class AuthRole
+ {
+ public const string AdministratorOnly = "Administrator";
+ public const string ReadWriteOrAdministrator = "ReadWrite,Administrator";
+ public const string GuestOrAdministrator = "Guest,Administrator";
+ public const string Any = "ReadOnly,ReadWrite,Guest,Administrator";
+ }
+}
diff --git a/src/slskd/Common/Authentication/Role.cs b/src/slskd/Common/Authentication/Role.cs
index 2f88283c..87ba3466 100644
--- a/src/slskd/Common/Authentication/Role.cs
+++ b/src/slskd/Common/Authentication/Role.cs
@@ -21,6 +21,7 @@ namespace slskd.Authentication
{
ReadOnly = 0,
ReadWrite = 1,
- Administrator = 2,
+ Guest = 2,
+ Administrator = 3,
}
}
diff --git a/src/slskd/Core/API/Controllers/ApplicationController.cs b/src/slskd/Core/API/Controllers/ApplicationController.cs
index c09b5513..0fd542db 100644
--- a/src/slskd/Core/API/Controllers/ApplicationController.cs
+++ b/src/slskd/Core/API/Controllers/ApplicationController.cs
@@ -69,7 +69,7 @@ namespace slskd.Core.API
/// </summary>
/// <returns></returns>
[HttpDelete]
- [Authorize(Policy = AuthPolicy.JwtOnly)]
+ [Authorize(Policy = AuthPolicy.JwtOnly, Roles = AuthRole.AdministratorOnly)]
public IActionResult Shutdown()
{
Program.MasterCancellationTokenSource.Cancel();
@@ -89,7 +89,7 @@ namespace slskd.Core.API
/// </summary>
/// <returns></returns>
[HttpPut]
- [Authorize(Policy = AuthPolicy.JwtOnly)]
+ [Authorize(Policy = AuthPolicy.JwtOnly, Roles = AuthRole.AdministratorOnly)]
public IActionResult Restart()
{
Program.MasterCancellationTokenSource.Cancel();
diff --git a/src/slskd/Core/API/Controllers/OptionsController.cs b/src/slskd/Core/API/Controllers/OptionsController.cs
index 961571a0..da8fb5f0 100644
--- a/src/slskd/Core/API/Controllers/OptionsController.cs
+++ b/src/slskd/Core/API/Controllers/OptionsController.cs
@@ -82,7 +82,7 @@ namespace slskd.Core.API
/// <returns></returns>
[HttpGet]
[Route("debug")]
- [Authorize(Policy = AuthPolicy.JwtOnly)]
+ [Authorize(Policy = AuthPolicy.JwtOnly, Roles = AuthRole.AdministratorOnly)]
[ProducesResponseType(typeof(string), 200)]
public IActionResult Debug()
{
@@ -100,7 +100,7 @@ namespace slskd.Core.API
}
[HttpGet]
- [Authorize(Policy = AuthPolicy.JwtOnly)]
+ [Authorize(Policy = AuthPolicy.JwtOnly, Roles = AuthRole.AdministratorOnly)]
[Route("yaml/location")]
public IActionResult GetYamlFileLocation()
{
@@ -113,7 +113,7 @@ namespace slskd.Core.API
}
[HttpGet]
- [Authorize(Policy = AuthPolicy.JwtOnly)]
+ [Authorize(Policy = AuthPolicy.JwtOnly, Roles = AuthRole.AdministratorOnly)]
[Route("yaml")]
public IActionResult GetYamlFile()
{
@@ -127,7 +127,7 @@ namespace slskd.Core.API
}
[HttpPost]
- [Authorize(Policy = AuthPolicy.JwtOnly)]
+ [Authorize(Policy = AuthPolicy.JwtOnly, Roles = AuthRole.AdministratorOnly)]
[Route("yaml")]
public IActionResult UpdateYamlFile([FromBody] string yaml)
{
diff --git a/src/slskd/Core/Options.cs b/src/slskd/Core/Options.cs
index c9b46239..1d9f814c 100644
--- a/src/slskd/Core/Options.cs
+++ b/src/slskd/Core/Options.cs
@@ -438,6 +438,14 @@ namespace slskd
}
else
{
+ // determine whether any InstanceName is used more than once
+ var instanceNames = Agents.Values.Select(a => a.InstanceName);
+
+ if (instanceNames.Count() != instanceNames.Distinct().Count())
+ {
+ modeResults.Add(new ValidationResult("One or more Agent instance names are duplicated. Ensure instance names are unique."));
+ }
+
foreach (var (name, agent) in Agents)
{
var res = new List<ValidationResult>();
@@ -516,6 +524,15 @@ namespace slskd
public class RelayAgentConfigurationOptions : IValidatableObject
{
/// <summary>
+ /// Gets the agent instance name.
+ /// </summary>
+ [Description("the name for this agent")]
+ [StringLength(255, MinimumLength = 1)]
+ [NotNullOrWhiteSpace]
+ [Secret]
+ public string InstanceName { get; init; }
+
+ /// <summary>
/// Gets the agent secret.
/// </summary>
[Description("shared secret for this agent")]
diff --git a/src/slskd/Program.cs b/src/slskd/Program.cs
index 5674aa2b..1236865f 100644
--- a/src/slskd/Program.cs
+++ b/src/slskd/Program.cs
@@ -618,14 +618,12 @@ namespace slskd
{
options.AddPolicy(AuthPolicy.JwtOnly, policy =>
{
- policy.RequireRole(Role.Administrator.ToString());
policy.AuthenticationSchemes.Add(JwtBearerDefaults.AuthenticationScheme);
policy.RequireAuthenticatedUser();
});
options.AddPolicy(AuthPolicy.ApiKeyOnly, policy =>
{
- policy.RequireRole(Role.ReadWrite.ToString(), Role.Administrator.ToString());
policy.AuthenticationSchemes.Add(ApiKeyAuthentication.AuthenticationScheme);
policy.RequireAuthenticatedUser();
});
diff --git a/src/slskd/Relay/API/Controllers/RelayController.cs b/src/slskd/Relay/API/Controllers/RelayController.cs
index 586cbbb2..49c76e2d 100644
--- a/src/slskd/Relay/API/Controllers/RelayController.cs
+++ b/src/slskd/Relay/API/Controllers/RelayController.cs
@@ -68,7 +68,7 @@ namespace slskd.Relay
/// </summary>
/// <returns></returns>
[HttpPut("agent")]
- [Authorize(Policy = AuthPolicy.JwtOnly)]
+ [Authorize(Policy = AuthPolicy.JwtOnly, Roles = AuthRole.AdministratorOnly)]
public async Task<IActionResult> Connect()
{
if (!OptionsAtStartup.Relay.Enabled || !new[] { RelayMode.Agent, RelayMode.Debug }.Contains(OperationMode))
@@ -85,7 +85,7 @@ namespace slskd.Relay
/// </summary>
/// <returns></returns>
[HttpDelete("agent")]
- [Authorize(Policy = AuthPolicy.JwtOnly)]
+ [Authorize(Policy = AuthPolicy.JwtOnly, Roles = AuthRole.AdministratorOnly)]
public async Task<IActionResult> Disconnect()
{
if (!OptionsAtStartup.Relay.Enabled || !new[] { RelayMode.Agent, RelayMode.Debug }.Contains(OperationMode))
@@ -103,7 +103,7 @@ namespace slskd.Relay
/// <param name="token">The unique identifier for the request.</param>
/// <returns></returns>
[HttpGet("controller/downloads/{token}")]
- [Authorize(Policy = AuthPolicy.ApiKeyOnly)]
+ [Authorize(Policy = AuthPolicy.ApiKeyOnly, Roles = AuthRole.Any)]
public IActionResult DownloadFile([FromRoute]string token)
{
if (!OptionsAtStartup.Relay.Enabled || !new[] { RelayMode.Controller, RelayMode.Debug }.Contains(OperationMode))
@@ -157,7 +157,7 @@ namespace slskd.Relay
[RequestSizeLimit(10L * 1024L * 1024L * 1024L)]
[RequestFormLimits(MultipartBodyLengthLimit = 10L * 1024L * 1024L * 1024L)]
[DisableFormValueModelBinding]
- [Authorize(Policy = AuthPolicy.ApiKeyOnly)]
+ [Authorize(Policy = AuthPolicy.ApiKeyOnly, Roles = AuthRole.ReadWriteOrAdministrator)]
public async Task<IActionResult> UploadFile(string token)
{
if (!OptionsAtStartup.Relay.Enabled || !new[] { RelayMode.Controller, RelayMode.Debug }.Contains(OperationMode))
@@ -252,7 +252,7 @@ namespace slskd.Relay
/// <param name="token">The unique identifier for the request.</param>
/// <returns></returns>
[HttpPost("controller/shares/{token}")]
- [Authorize(Policy = AuthPolicy.ApiKeyOnly)]
+ [Authorize(Policy = AuthPolicy.ApiKeyOnly, Roles = AuthRole.ReadWriteOrAdministrator)]
public async Task<IActionResult> UploadShares(string token)
{
if (!OptionsAtStartup.Relay.Enabled || !new[] { RelayMode.Controller, RelayMode.Debug }.Contains(OperationMode))
diff --git a/src/slskd/Relay/API/Hubs/RelayHub.cs b/src/slskd/Relay/API/Hubs/RelayHub.cs
index e246711e..333bd529 100644
--- a/src/slskd/Relay/API/Hubs/RelayHub.cs
+++ b/src/slskd/Relay/API/Hubs/RelayHub.cs
@@ -89,7 +89,7 @@ namespace slskd.Relay
OptionsAtStartup = optionsAtStartup;
}
- private ILogger Log { get; } = Serilog.Log.ForContext<RelayService>();
+ private ILogger Log { get; } = Serilog.Log.ForContext<RelayHub>();
private IRelayService Relay { get; }
private IOptionsMonitor<Options> OptionsMonitor { get; }
private OptionsAtStartup OptionsAtStartup { get; }
@@ -138,9 +138,24 @@ namespace slskd.Relay
/// <exception cref="UnauthorizedAccessException">Thrown when the challenge response is invalid.</exception>
public void Login(string agent, string challengeResponse)
{
- OptionsMonitor.CurrentValue.Relay.Agents.TryGetValue(agent, out var agentOptions);
+ bool TryGetAgentConfig(string agent, out Options.RelayOptions.RelayAgentConfigurationOptions options)
+ {
+ var allAgents = OptionsMonitor.CurrentValue.Relay.Agents;
+ var found = allAgents.Values.SingleOrDefault(a => a.InstanceName == agent);
+
+ if (found != default)
+ {
+ options = found;
+ return true;
+ }
+
+ Log.Warning("Unable to locate Agent config for '{Agent}' (configured Agents: {Agents})", agent, string.Join(", ", allAgents.Values.Select(a => a.InstanceName)));
+
+ options = null;
+ return false;
+ }
- if (agentOptions == default)
+ if (!TryGetAgentConfig(agent, out var agentOptions))
{
Log.Warning("Unauthorized login attempt from unknown Agent {Agent} (connection {Id}) from {IP}", agent, Context.ConnectionId, RemoteIpAddress);
throw new UnauthorizedAccessException();
diff --git a/src/slskd/Relay/RelayService.cs b/src/slskd/Relay/RelayService.cs
index c7056a0c..6cbbb0a2 100644
--- a/src/slskd/Relay/RelayService.cs
+++ b/src/slskd/Relay/RelayService.cs
@@ -743,7 +743,9 @@ namespace slskd.Relay
return false;
}
- if (!OptionsMonitor.CurrentValue.Relay.Agents.TryGetValue(agentName, out var agentOptions))
+ var agentOptions = OptionsMonitor.CurrentValue.Relay.Agents.Values.SingleOrDefault(a => a.InstanceName == agentName);
+
+ if (agentOptions == default)
{
Log.Debug("Auth challenge for {Id} failed: no configuration for agent '{Agent}'", connectionId, agentName);
return false;
@@ -751,9 +753,15 @@ namespace slskd.Relay
var key = Pbkdf2.GetKey(password: agentOptions.Secret, salt: agentName, length: 48);
var tokenBytes = System.Text.Encoding.UTF8.GetBytes((string)challengeToken);
- var expectedResponse = Aes.Encrypt(tokenBytes, key).ToBase62();
+ var expectedCredential = Aes.Encrypt(tokenBytes, key).ToBase62();
+
+ if (expectedCredential != credential)
+ {
+ Log.Debug("Validation failed: Supplied credential {Credential} does not match expected credential {Expected}", credential, expectedCredential);
+ return false;
+ }
- return expectedResponse == credential;
+ return true;
}
/// <summary>
@@ -860,7 +868,9 @@ namespace slskd.Relay
{
try
{
- if (!OptionsMonitor.CurrentValue.Relay.Agents.TryGetValue(agentName, out var agentOptions))
+ var agentOptions = OptionsMonitor.CurrentValue.Relay.Agents.Values.SingleOrDefault(a => a.InstanceName == agentName);
+
+ if (agentOptions == default)
{
Log.Debug("Validation failed: Agent {Agent} not configured", agentName);
return false;