Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed downloads of files #567

Merged
merged 14 commits into from
Feb 26, 2024
2 changes: 1 addition & 1 deletion src/BL/Services/MinioHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ public async Task<GetObjectResponse> GetCopyObject(string sourceBucketName, stri
Key = sourceObjectKey
};

var getObjectResponse = amazonS3Client.GetObjectAsync(getObjectRequest).Result;
var getObjectResponse = await amazonS3Client.GetObjectAsync(getObjectRequest);

return getObjectResponse;

Expand Down
15 changes: 5 additions & 10 deletions src/DARE-API/Controllers/SubmissionController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -381,21 +381,16 @@ public async Task<IActionResult> DownloadFileAsync(int submissionId)
{
try
{

Log.Debug($"DownloadFileAsync submissionId > {submissionId}");
var submission = _DbContext.Submissions.First(x => x.Id == submissionId);



Log.Debug($"DownloadFileAsync submission.Project.OutputBucket > {submission.Project.OutputBucket} submission.FinalOutputFile > {submission.FinalOutputFile} " );
var response = await _minioHelper.GetCopyObject(submission.Project.OutputBucket, submission.FinalOutputFile);

using (var responseStream = response.ResponseStream)
{
var fileBytes = new byte[responseStream.Length];
await responseStream.ReadAsync(fileBytes, 0, (int)responseStream.Length);

// Create a FileContentResult and return it as the response
return File(fileBytes, GetContentType(submission.FinalOutputFile), submission.FinalOutputFile);
}

var responseStream = response.ResponseStream;
return File(responseStream, GetContentType(submission.FinalOutputFile), submission.FinalOutputFile);
}
catch (Exception ex)
{
Expand Down
2 changes: 2 additions & 0 deletions src/DARE-API/Controllers/TESController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,8 @@ private string SetTesTaskStateToCancelled(string testaskstr, int subid)
public virtual async Task<IActionResult> CreateTaskAsync([FromBody] TesTask tesTask,
CancellationToken cancellationToken)
{
Log.Information($"/v1/tasks route successfully entered");

try
{
var usersName = (from x in User.Claims where x.Type == "preferred_username" select x.Value).First();
Expand Down
133 changes: 132 additions & 1 deletion src/DARE-API/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
using System.Security.Claims;
using System.Runtime;
using Microsoft.AspNetCore.Server.Kestrel.Core;
using Microsoft.AspNetCore.Authentication.OpenIdConnect;

var builder = WebApplication.CreateBuilder(args);

Expand Down Expand Up @@ -87,9 +88,10 @@
ValidAudiences = submissionKeyCloakSettings.ValidAudiences.Trim().Split(',').ToList(),
ValidIssuer = submissionKeyCloakSettings.Authority,
ValidateIssuerSigningKey = true,
ValidateIssuer = true,
ValidateIssuer = false,
ValidateLifetime = true
};
Log.Information($"Check TokenValidationParams for Issuer {submissionKeyCloakSettings.Authority}");

builder.Services.AddTransient<IClaimsTransformation, ClaimsTransformerBL>();

Expand Down Expand Up @@ -124,8 +126,137 @@
options.IncludeErrorDetails = true;

options.TokenValidationParameters = TVP;
options.Events = new JwtBearerEvents
{
OnForbidden = context =>
{
Log.Information("ONFORBIDDEN START");
Log.Information("HttpContext.Connection.RemoteIpAddress : {RemoteIpAddress}",
context.HttpContext.Connection.RemoteIpAddress);
Log.Information("HttpContext.Connection.RemotePort : {RemotePort}",
context.HttpContext.Connection.RemotePort);
Log.Information("HttpContext.Request.Scheme : {Scheme}", context.HttpContext.Request.Scheme);
Log.Information("HttpContext.Request.Host : {Host}", context.HttpContext.Request.Host);

foreach (var header in context.HttpContext.Request.Headers)
{
Log.Information("Request Header {key} - {value}", header.Key, header.Value);
}

foreach (var header in context.HttpContext.Response.Headers)
{
Log.Information("Response Header {key} - {value}", header.Key, header.Value);
}
Log.Information("ONFORBIDDEN END");
return context.Response.CompleteAsync();
},
OnTokenValidated = context =>
{
Log.Information("ONTOKENVALIDATED START");
Log.Information("HttpContext.Connection.RemoteIpAddress : {RemoteIpAddress}",
context.HttpContext.Connection.RemoteIpAddress);
Log.Information("HttpContext.Connection.RemotePort : {RemotePort}",
context.HttpContext.Connection.RemotePort);
Log.Information("HttpContext.Request.Scheme : {Scheme}", context.HttpContext.Request.Scheme);
Log.Information("HttpContext.Request.Host : {Host}", context.HttpContext.Request.Host);

foreach (var header in context.HttpContext.Request.Headers)
{
Log.Information("Request Header {key} - {value}", header.Key, header.Value);
}

foreach (var header in context.HttpContext.Response.Headers)
{
Log.Information("Response Header {key} - {value}", header.Key, header.Value);
}
Log.Information("ONTOKENVALIDATED END");
// Log the issuer claim from the token
var issuer = context.Principal.FindFirst("iss")?.Value;
Log.Information("Token Issuer: {Issuer}", issuer);
var audience = context.Principal.FindFirst("aud")?.Value;
Log.Information("Token Audience: {Audience}", audience);
return Task.CompletedTask;
},
OnAuthenticationFailed = context =>
{
Log.Information("ONAUTHFAILED START");
Log.Information("HttpContext.Connection.RemoteIpAddress : {RemoteIpAddress}",
context.HttpContext.Connection.RemoteIpAddress);
Log.Information("HttpContext.Connection.RemotePort : {RemotePort}",
context.HttpContext.Connection.RemotePort);
Log.Information("HttpContext.Request.Scheme : {Scheme}", context.HttpContext.Request.Scheme);
Log.Information("HttpContext.Request.Host : {Host}", context.HttpContext.Request.Host);

foreach (var header in context.HttpContext.Request.Headers)
{
Log.Information("Request Header {key} - {value}", header.Key, header.Value);
}

foreach (var header in context.HttpContext.Response.Headers)
{
Log.Information("Response Header {key} - {value}", header.Key, header.Value);
}
Log.Information("ONAUTHFAILED END");
Log.Error("{Function}: {ex}", "OnAuthFailed", context.Exception.Message);
Log.Error("Auth failed event: {event}", context.Request.Headers);
return context.Response.CompleteAsync();
},
OnMessageReceived = context =>
{
Log.Information("ONMESSAGERECEIVED START");
Log.Information("HttpContext.Connection.RemoteIpAddress : {RemoteIpAddress}",
context.HttpContext.Connection.RemoteIpAddress);
Log.Information("HttpContext.Connection.RemotePort : {RemotePort}",
context.HttpContext.Connection.RemotePort);
Log.Information("HttpContext.Request.Scheme : {Scheme}", context.HttpContext.Request.Scheme);
Log.Information("HttpContext.Request.Host : {Host}", context.HttpContext.Request.Host);

foreach (var header in context.HttpContext.Request.Headers)
{
Log.Information("Request Header {key} - {value}", header.Key, header.Value);
}

foreach (var header in context.HttpContext.Response.Headers)
{
Log.Information("Response Header {key} - {value}", header.Key, header.Value);
}
Log.Information("ONMESSAGERECEVIED END");
string accessToken = context.Request.Query["access_token"];
PathString path = context.HttpContext.Request.Path;

if (
!string.IsNullOrEmpty(accessToken) &&
path.StartsWithSegments("/api/SignalRHub")
)
{
context.Token = accessToken;
}

return Task.CompletedTask;
},
OnChallenge = context =>
{
Log.Information("ONCHALLENGE START");
Log.Information("HttpContext.Connection.RemoteIpAddress : {RemoteIpAddress}",
context.HttpContext.Connection.RemoteIpAddress);
Log.Information("HttpContext.Connection.RemotePort : {RemotePort}",
context.HttpContext.Connection.RemotePort);
Log.Information("HttpContext.Request.Scheme : {Scheme}", context.HttpContext.Request.Scheme);
Log.Information("HttpContext.Request.Host : {Host}", context.HttpContext.Request.Host);

foreach (var header in context.HttpContext.Request.Headers)
{
Log.Information("Request Header {key} - {value}", header.Key, header.Value);
}

foreach (var header in context.HttpContext.Response.Headers)
{
Log.Information("Response Header {key} - {value}", header.Key, header.Value);
}
Log.Information("ONCHALLENGE END");
return Task.CompletedTask;
}
};
});


Expand Down
2 changes: 1 addition & 1 deletion src/DARE-API/Services/KeyclockTokenAPIHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public async Task<string> GetTokenForUser(string username, string password, stri
string clientId = _settings.ClientId;
string clientSecret = _settings.ClientSecret;
var proxyhandler = _settings.getProxyHandler;
Log.Information($"GetTokenForUser uesing proxyhandler ");
Log.Information($"GetTokenForUser uesing proxyhandler _settings.Authority > {_settings.Authority}");

return await KeycloakCommon.GetTokenForUserGuts(username, password, requiredRole, proxyhandler, keycloakBaseUrl, clientId, clientSecret);
}
Expand Down
2 changes: 1 addition & 1 deletion src/DARE-FrontEnd/Controllers/SubmissionController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public async Task<IActionResult> DownloadFileAsync(int subId)
{ "submissionId", subId.ToString() }
};

var submission = _clientHelper.CallAPIWithoutModel<Submission>("/api/Submission/GetASubmission/", paramlist).Result;
var submission = _clientHelper.CallAPIWithoutModel<Submission>($"/api/Submission/GetASubmission/{subId}").Result;
var file = await _clientHelper.CallAPIToGetFile(
"/api/Submission/DownloadFile", paramlist);

Expand Down
17 changes: 5 additions & 12 deletions src/Data-Egress-API/Controllers/DataEgressController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public EgressSubmission GetEgress(int id)
}

[Authorize(Roles = "data-egress-admin")]
[HttpGet("GetEgressFile")]
[HttpGet("GetEgressFile/{id}")]
public EgressFile GetEgressFile(int id)
{
try
Expand Down Expand Up @@ -344,19 +344,12 @@ public async Task<IActionResult> DownloadFileAsync(int id)

var egressFile = _DbContext.EgressFiles.First(x => x.Id == id);

var response = await _minioHelper.GetCopyObject(egressFile.EgressSubmission.OutputBucket, egressFile.Name);

var responseStream = response.ResponseStream;

var response =
await _minioHelper.GetCopyObject(egressFile.EgressSubmission.OutputBucket, egressFile.Name);

using (var responseStream = response.ResponseStream)
{
var fileBytes = new byte[responseStream.Length];
await responseStream.ReadAsync(fileBytes, 0, (int)responseStream.Length);

// Create a FileContentResult and return it as the response
return File(fileBytes, GetContentType(egressFile.Name), egressFile.Name);
}
return File(responseStream, GetContentType(egressFile.Name), egressFile.Name);

}
catch (Exception ex)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Data-Egress-UI/Controllers/DataController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public IActionResult DownloadFile(int? fileId)
{ "id", fileId.ToString() }
};

var egressFile = _dataClientHelper.CallAPIWithoutModel<EgressFile>("/api/DataEgress/GetEgressFile", paramlist).Result;
var egressFile = _dataClientHelper.CallAPIWithoutModel<EgressFile>($"/api/DataEgress/GetEgressFile/{fileId}").Result;
var file = _dataClientHelper.CallAPIToGetFile(
"/api/DataEgress/DownloadFile", paramlist).Result;
return File(file, GetContentType(egressFile.Name), egressFile.Name);
Expand Down
4 changes: 2 additions & 2 deletions src/TRE-API/Controllers/SubmissionController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -299,8 +299,8 @@ await _hutchHelper.CallAPI<ApprovalResult, APIReturn>($"/api/jobs/{review.SubId}
Log.Information($"EgressResults with File.Approved > {File.Approved} File.FileName > {File.FileName} ");
if (File.Approved)
{
var source = _minioTreHelper.GetCopyObject(review.OutputBucket, File.FileName);
var resultcopy = _minioSubHelper.CopyObjectToDestination(bucket.Bucket, File.FileName, source.Result).Result;
var source = await _minioTreHelper.GetCopyObject(review.OutputBucket, File.FileName);
var resultcopy = await _minioSubHelper.CopyObjectToDestination(bucket.Bucket, File.FileName, source);
}
}

Expand Down
Loading