diff --git a/src/BL/Services/MinioHelper.cs b/src/BL/Services/MinioHelper.cs index 2218848b9..da4ed1652 100644 --- a/src/BL/Services/MinioHelper.cs +++ b/src/BL/Services/MinioHelper.cs @@ -408,7 +408,7 @@ public async Task GetCopyObject(string sourceBucketName, stri Key = sourceObjectKey }; - var getObjectResponse = amazonS3Client.GetObjectAsync(getObjectRequest).Result; + var getObjectResponse = await amazonS3Client.GetObjectAsync(getObjectRequest); return getObjectResponse; diff --git a/src/DARE-API/Controllers/SubmissionController.cs b/src/DARE-API/Controllers/SubmissionController.cs index dad62d8dc..669b47c26 100644 --- a/src/DARE-API/Controllers/SubmissionController.cs +++ b/src/DARE-API/Controllers/SubmissionController.cs @@ -381,21 +381,16 @@ public async Task 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) { diff --git a/src/DARE-API/Controllers/TESController.cs b/src/DARE-API/Controllers/TESController.cs index 60ae5f457..b52a9a7d4 100644 --- a/src/DARE-API/Controllers/TESController.cs +++ b/src/DARE-API/Controllers/TESController.cs @@ -157,6 +157,8 @@ private string SetTesTaskStateToCancelled(string testaskstr, int subid) public virtual async Task 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(); diff --git a/src/DARE-API/Program.cs b/src/DARE-API/Program.cs index 0d3562f64..71bb969c9 100644 --- a/src/DARE-API/Program.cs +++ b/src/DARE-API/Program.cs @@ -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); @@ -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(); @@ -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; + } + }; }); diff --git a/src/DARE-API/Services/KeyclockTokenAPIHelper.cs b/src/DARE-API/Services/KeyclockTokenAPIHelper.cs index 1fb632c3d..b9f54db08 100644 --- a/src/DARE-API/Services/KeyclockTokenAPIHelper.cs +++ b/src/DARE-API/Services/KeyclockTokenAPIHelper.cs @@ -24,7 +24,7 @@ public async Task 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); } diff --git a/src/DARE-FrontEnd/Controllers/SubmissionController.cs b/src/DARE-FrontEnd/Controllers/SubmissionController.cs index 243a42305..984b485ec 100644 --- a/src/DARE-FrontEnd/Controllers/SubmissionController.cs +++ b/src/DARE-FrontEnd/Controllers/SubmissionController.cs @@ -125,7 +125,7 @@ public async Task DownloadFileAsync(int subId) { "submissionId", subId.ToString() } }; - var submission = _clientHelper.CallAPIWithoutModel("/api/Submission/GetASubmission/", paramlist).Result; + var submission = _clientHelper.CallAPIWithoutModel($"/api/Submission/GetASubmission/{subId}").Result; var file = await _clientHelper.CallAPIToGetFile( "/api/Submission/DownloadFile", paramlist); diff --git a/src/Data-Egress-API/Controllers/DataEgressController.cs b/src/Data-Egress-API/Controllers/DataEgressController.cs index 411ef1111..619db7cb1 100644 --- a/src/Data-Egress-API/Controllers/DataEgressController.cs +++ b/src/Data-Egress-API/Controllers/DataEgressController.cs @@ -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 @@ -344,19 +344,12 @@ public async Task 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) { diff --git a/src/Data-Egress-UI/Controllers/DataController.cs b/src/Data-Egress-UI/Controllers/DataController.cs index 98768dcce..b1a70e7bb 100644 --- a/src/Data-Egress-UI/Controllers/DataController.cs +++ b/src/Data-Egress-UI/Controllers/DataController.cs @@ -104,7 +104,7 @@ public IActionResult DownloadFile(int? fileId) { "id", fileId.ToString() } }; - var egressFile = _dataClientHelper.CallAPIWithoutModel("/api/DataEgress/GetEgressFile", paramlist).Result; + var egressFile = _dataClientHelper.CallAPIWithoutModel($"/api/DataEgress/GetEgressFile/{fileId}").Result; var file = _dataClientHelper.CallAPIToGetFile( "/api/DataEgress/DownloadFile", paramlist).Result; return File(file, GetContentType(egressFile.Name), egressFile.Name); diff --git a/src/TRE-API/Controllers/SubmissionController.cs b/src/TRE-API/Controllers/SubmissionController.cs index d7fa3a780..2b2e7d3c1 100644 --- a/src/TRE-API/Controllers/SubmissionController.cs +++ b/src/TRE-API/Controllers/SubmissionController.cs @@ -299,8 +299,8 @@ await _hutchHelper.CallAPI($"/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); } }