Skip to content

Commit

Permalink
added authentication for rclone api requests if user pass is specified
Browse files Browse the repository at this point in the history
  • Loading branch information
toutas committed Oct 6, 2021
1 parent d3d8b22 commit ac5fe89
Showing 1 changed file with 27 additions and 6 deletions.
33 changes: 27 additions & 6 deletions SARotate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -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))
{
Expand Down Expand Up @@ -184,16 +184,24 @@ public Task StopAsync(CancellationToken cancellationToken)
return serviceAccountUsageOrderByGroup;
}

private async Task<bool> CheckValidRcloneVersion(Uri rcloneVersionEndpoint, string remote)
private async Task<bool> 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;
Expand All @@ -205,12 +213,19 @@ private async Task<bool> CheckValidRcloneVersion(Uri rcloneVersionEndpoint, stri
return majorVersion == _minimumMajorVersion && minorVersion >= _minimumMinorVersion && patchVersion >= _minimumPatchVersion;
}

private async Task<string?> FindPreviousServiceAccountUsedForRemote(string remote, string rcloneApiUri)
private async Task<string?> 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",
Expand Down Expand Up @@ -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",
Expand Down

0 comments on commit ac5fe89

Please sign in to comment.