From ac5fe8981773d668862f9717e5e0ff037d949514 Mon Sep 17 00:00:00 2001 From: Kaare Date: Wed, 6 Oct 2021 12:20:38 +0200 Subject: [PATCH] added authentication for rclone api requests if user pass is specified --- SARotate.cs | 33 +++++++++++++++++++++++++++------ 1 file changed, 27 insertions(+), 6 deletions(-) diff --git a/SARotate.cs b/SARotate.cs index 373453a..45339f2 100644 --- a/SARotate.cs +++ b/SARotate.cs @@ -98,12 +98,12 @@ public Task StopAsync(CancellationToken cancellationToken) } var remoteVersionUri = new Uri($"{remoteRcloneHost}/core/version"); - bool validRcloneVersion = await CheckValidRcloneVersion(remoteVersionUri, remote); + bool validRcloneVersion = await CheckValidRcloneVersion(remoteVersionUri, remote, remotes[remote]); if (!validRcloneVersion) { - Console.WriteLine("Ignoring remote: " + remote); - Console.WriteLine("Rclone versions below " + _minimumVersionString + " are unsupported."); + LogMessage("Ignoring remote: " + remote); + LogMessage("Rclone versions below " + _minimumVersionString + " are unsupported."); remotes.Remove(remote); } @@ -122,7 +122,7 @@ public Task StopAsync(CancellationToken cancellationToken) foreach (string remote in remotes.Keys) { - string? previousServiceAccountUsed = await FindPreviousServiceAccountUsedForRemote(remote, remotes[remote].Address); + string? previousServiceAccountUsed = await FindPreviousServiceAccountUsedForRemote(remote, remotes[remote]); if (string.IsNullOrEmpty(previousServiceAccountUsed)) { @@ -184,16 +184,24 @@ public Task StopAsync(CancellationToken cancellationToken) return serviceAccountUsageOrderByGroup; } - private async Task CheckValidRcloneVersion(Uri rcloneVersionEndpoint, string remote) + private async Task CheckValidRcloneVersion(Uri rcloneVersionEndpoint, string remote, RemoteInfo remoteInfo) { var request = new HttpRequestMessage(HttpMethod.Post, rcloneVersionEndpoint); + if (!string.IsNullOrEmpty(remoteInfo.User) && !string.IsNullOrEmpty(remoteInfo.Pass)) + { + request.Headers.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(Encoding.ASCII.GetBytes(remoteInfo.User + ":" + remoteInfo.Pass))); + LogMessage("Adding user and password to RClone api request"); + } + HttpClient client = _httpClientFactory.CreateClient(); HttpResponseMessage response = await client.SendAsync(request); string resultContent = await response.Content.ReadAsStringAsync(); + LogMessage("resultFromVersion:::: " + resultContent); + dynamic? versionResponse = JsonConvert.DeserializeObject(resultContent); dynamic? decomposed = versionResponse?.decomposed; int majorVersion = decomposed != null ? decomposed[0] : -1; @@ -205,12 +213,19 @@ private async Task CheckValidRcloneVersion(Uri rcloneVersionEndpoint, stri return majorVersion == _minimumMajorVersion && minorVersion >= _minimumMinorVersion && patchVersion >= _minimumPatchVersion; } - private async Task FindPreviousServiceAccountUsedForRemote(string remote, string rcloneApiUri) + private async Task FindPreviousServiceAccountUsedForRemote(string remote, RemoteInfo remoteInfo) { + string rcloneApiUri = remoteInfo.Address; rcloneApiUri += rcloneApiUri.EndsWith("/") ? "backend/command" : "/backend/command"; var request = new HttpRequestMessage(HttpMethod.Post, rcloneApiUri); + if (!string.IsNullOrEmpty(remoteInfo.User) && !string.IsNullOrEmpty(remoteInfo.Pass)) + { + request.Headers.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(Encoding.ASCII.GetBytes(remoteInfo.User + ":" + remoteInfo.Pass))); + LogMessage("Adding user and password to RClone api request"); + } + var command = new RCloneServiceAccountCommand { command = "get", @@ -365,6 +380,12 @@ private async Task RunSwappingService( var request = new HttpRequestMessage(HttpMethod.Post, rcloneApiUri); + if (!string.IsNullOrEmpty(remoteConfig[remote].User) && !string.IsNullOrEmpty(remoteConfig[remote].Pass)) + { + request.Headers.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(Encoding.ASCII.GetBytes(remoteConfig[remote].User + ":" + remoteConfig[remote].Pass))); + LogMessage("Adding user and password to RClone api request"); + } + var command = new RCloneServiceAccountCommand { command = "set",