From bd365b2d856d271b68b38661f999719547caefea Mon Sep 17 00:00:00 2001 From: jaybeeelsdon Date: Thu, 12 Dec 2024 18:34:41 +0000 Subject: [PATCH] fix: finalerrors (#897) * Get more details * It's wrong * fix: put it all back --- .../Controllers/DataEgressController.cs | 2 +- .../DbContexts/DataInitializer.cs | 15 +++++- .../DataEgressCredentialsController.cs | 47 +++++++++++++++++++ .../UpdateCredentials.cshtml | 40 ++++++++++++++++ .../Views/Shared/_Layout.cshtml | 4 +- .../Controllers/TRECredentialsController.cs | 40 ++++++++++++++++ .../DbContexts/DataInitializer.cs | 13 +++++ src/TRE-API/Services/SubmissionHelper.cs | 16 +++---- .../Controllers/TRECredentialsController.cs | 43 +++++++++++++++++ src/TRE-UI/Views/Shared/_Layout.cshtml | 4 +- .../TRECredentials/UpdateCredentials.cshtml | 44 +++++++++++++++++ 11 files changed, 256 insertions(+), 12 deletions(-) create mode 100644 src/Data-Egress-UI/Controllers/DataEgressCredentialsController.cs create mode 100644 src/Data-Egress-UI/Views/DataEgressCredentials/UpdateCredentials.cshtml create mode 100644 src/TRE-API/Controllers/TRECredentialsController.cs create mode 100644 src/TRE-UI/Controllers/TRECredentialsController.cs create mode 100644 src/TRE-UI/Views/TRECredentials/UpdateCredentials.cshtml diff --git a/src/Data-Egress-API/Controllers/DataEgressController.cs b/src/Data-Egress-API/Controllers/DataEgressController.cs index bca85bf01..a8df11c4d 100644 --- a/src/Data-Egress-API/Controllers/DataEgressController.cs +++ b/src/Data-Egress-API/Controllers/DataEgressController.cs @@ -86,7 +86,7 @@ public async Task AddNewDataEgress(EgressSubmission submission) } catch (Exception ex) { - Log.Error(ex.ToString()); + Log.Error(ex,"{Function} Sending email error", "AddNewDataEgress"); } return new BoolReturn() { Result = true }; diff --git a/src/Data-Egress-API/Repositories/DbContexts/DataInitializer.cs b/src/Data-Egress-API/Repositories/DbContexts/DataInitializer.cs index 31d443260..1cd41aee7 100644 --- a/src/Data-Egress-API/Repositories/DbContexts/DataInitializer.cs +++ b/src/Data-Egress-API/Repositories/DbContexts/DataInitializer.cs @@ -41,7 +41,20 @@ public void SeedAllInOneData(string password) _dbContext.SaveChanges(); } - + if (!_dbContext.KeycloakCredentials.Any(x => x.CredentialType == CredentialType.Egress)) + { + + + _dbContext.KeycloakCredentials.Add(new KeycloakCredentials() + { + UserName = "globaladminuser", + CredentialType = CredentialType.Egress, + PasswordEnc = _encDecHelper.Encrypt(password) + }); + _dbContext.SaveChanges(); + } + + } diff --git a/src/Data-Egress-UI/Controllers/DataEgressCredentialsController.cs b/src/Data-Egress-UI/Controllers/DataEgressCredentialsController.cs new file mode 100644 index 000000000..f7a12ad74 --- /dev/null +++ b/src/Data-Egress-UI/Controllers/DataEgressCredentialsController.cs @@ -0,0 +1,47 @@ +using BL.Models; +using BL.Services; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; +using BL.Models.APISimpleTypeReturns; + +namespace Data_Egress_UI.Controllers +{ + [Authorize(Roles = "data-egress-admin")] + public class DataEgressCredentialsController : Controller + { + private readonly IDataEgressClientHelper _clientHelper; + public DataEgressCredentialsController(IDataEgressClientHelper client) + { + _clientHelper = client; + } + [HttpGet] + public async Task UpdateCredentialsAsync() + { + var valid = await _clientHelper.CallAPIWithoutModel("/api/TreCredentials/EgressCheckCredentialsAreValid"); + return View(new KeycloakCredentials() + { Valid = valid.Result }); + } + [HttpPost] + public async Task UpdateCredentials(KeycloakCredentials credentials) + { + if (ModelState.IsValid) + { + var result = + await _clientHelper.CallAPI( + "/api/TreCredentials/EgressUpdateCredentials", credentials); + if (result.Valid) + { + return RedirectToAction("Index", "Home"); + } + else + { + return View(credentials); + } + } + else + { + return View(credentials); + } + } + } +} \ No newline at end of file diff --git a/src/Data-Egress-UI/Views/DataEgressCredentials/UpdateCredentials.cshtml b/src/Data-Egress-UI/Views/DataEgressCredentials/UpdateCredentials.cshtml new file mode 100644 index 000000000..55b0ed210 --- /dev/null +++ b/src/Data-Egress-UI/Views/DataEgressCredentials/UpdateCredentials.cshtml @@ -0,0 +1,40 @@ +@model BL.Models.KeycloakCredentials +
+

Update Tre Credentials

+
+
+
+ @if (!Model.Valid) + { +

Egress Credentials are missing or invalid. Please update

+ } +
+
+
+
+ @using (Html.BeginForm("UpdateCredentials", "DataEgressCredentials", FormMethod.Post, new { id = "frmMain" })) + { + @Html.AntiForgeryToken() + @Html.HiddenFor(m => m.Id) +
+ @Html.LabelFor(m => m.UserName) + @Html.TextBoxFor(x => x.UserName, new { @class = "form-control show-tick noBottomMargin" }) + @Html.ValidationMessageFor(m => m.UserName) +
+
+ @Html.LabelFor(m => m.PasswordEnc) + @Html.PasswordFor(x => x.PasswordEnc, new { @class = "form-control show-tick noBottomMargin" }) + @Html.ValidationMessageFor(m => m.PasswordEnc) +
+
+ @Html.LabelFor(m => m.ConfirmPassword) + @Html.PasswordFor(x => x.ConfirmPassword, new { @class = "form-control show-tick noBottomMargin" }) + @Html.ValidationMessageFor(m => m.ConfirmPassword) +
+
+ +
+ } +
+
+
\ No newline at end of file diff --git a/src/Data-Egress-UI/Views/Shared/_Layout.cshtml b/src/Data-Egress-UI/Views/Shared/_Layout.cshtml index 835a3952d..b299c8fdf 100644 --- a/src/Data-Egress-UI/Views/Shared/_Layout.cshtml +++ b/src/Data-Egress-UI/Views/Shared/_Layout.cshtml @@ -35,7 +35,9 @@ - + diff --git a/src/TRE-API/Controllers/TRECredentialsController.cs b/src/TRE-API/Controllers/TRECredentialsController.cs new file mode 100644 index 000000000..8d9c5752a --- /dev/null +++ b/src/TRE-API/Controllers/TRECredentialsController.cs @@ -0,0 +1,40 @@ +using BL.Models; +using BL.Models.APISimpleTypeReturns; +using BL.Models.Settings; +using BL.Services; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; +using Serilog; +using TRE_API.Repositories.DbContexts; +using TRE_API.Services; +namespace TRE_API.Controllers +{ + [Route("api/[controller]")] + [ApiController] + public class TRECredentialsController : Controller + { + private readonly ApplicationDbContext _DbContext; + private readonly IEncDecHelper _encDecHelper; + private readonly KeycloakTokenHelper _keycloakTokenHelper; + public TRECredentialsController(ApplicationDbContext applicationDbContext, IEncDecHelper encDec, SubmissionKeyCloakSettings keycloakSettings) + { + _encDecHelper = encDec; + _DbContext = applicationDbContext; + _keycloakTokenHelper = new KeycloakTokenHelper(keycloakSettings.BaseUrl, keycloakSettings.ClientId, + keycloakSettings.ClientSecret, keycloakSettings.Proxy, keycloakSettings.ProxyAddresURL, keycloakSettings.DemoMode); + } + [Authorize(Roles = "dare-tre-admin")] + [HttpGet("CheckCredentialsAreValid")] + public async Task CheckCredentialsAreValidAsync() + { + return await ControllerHelpers.CheckCredentialsAreValid(_keycloakTokenHelper, _encDecHelper, _DbContext, CredentialType.Tre); + } + [Authorize(Roles = "dare-tre-admin")] + [HttpPost("UpdateCredentials")] + public async Task UpdateCredentials(KeycloakCredentials creds) + { + creds = await ControllerHelpers.UpdateCredentials(creds, _keycloakTokenHelper, _DbContext, _encDecHelper, CredentialType.Tre, "dare-tre-admin"); + return creds; + } + } +} \ No newline at end of file diff --git a/src/TRE-API/Repositories/DbContexts/DataInitializer.cs b/src/TRE-API/Repositories/DbContexts/DataInitializer.cs index 792424292..605b9a0f5 100644 --- a/src/TRE-API/Repositories/DbContexts/DataInitializer.cs +++ b/src/TRE-API/Repositories/DbContexts/DataInitializer.cs @@ -41,6 +41,19 @@ public void SeedAllInOneData(string password) _dbContext.SaveChanges(); } + if (!_dbContext.KeycloakCredentials.Any(x => x.CredentialType == CredentialType.Tre)) + { + + + _dbContext.KeycloakCredentials.Add(new KeycloakCredentials() + { + UserName = "globaladminuser", + CredentialType = CredentialType.Tre, + PasswordEnc = _encDecHelper.Encrypt(password) + }); + _dbContext.SaveChanges(); + } + if (!_dbContext.KeycloakCredentials.Any(x => x.CredentialType == CredentialType.Egress)) { diff --git a/src/TRE-API/Services/SubmissionHelper.cs b/src/TRE-API/Services/SubmissionHelper.cs index 71a0c7831..e21a5060a 100644 --- a/src/TRE-API/Services/SubmissionHelper.cs +++ b/src/TRE-API/Services/SubmissionHelper.cs @@ -240,14 +240,14 @@ public void SimulateSubmissionProcessing(Submission submission) { - UpdateStatusForTre(submission.ToString(), StatusType.SendingSubmissionToHutch, ""); - UpdateStatusForTre(submission.ToString(), StatusType.WaitingForCrate, ""); - UpdateStatusForTre(submission.ToString(), StatusType.ValidatingCrate, ""); - UpdateStatusForTre(submission.ToString(), StatusType.FetchingWorkflow, ""); - UpdateStatusForTre(submission.ToString(), StatusType.StagingWorkflow, ""); - UpdateStatusForTre(submission.ToString(), StatusType.ExecutingWorkflow, ""); - UpdateStatusForTre(submission.ToString(), StatusType.PreparingOutputs, ""); - UpdateStatusForTre(submission.ToString(), StatusType.TransferredForDataOut, ""); + UpdateStatusForTre(submission.Id.ToString(), StatusType.SendingSubmissionToHutch, ""); + UpdateStatusForTre(submission.Id.ToString(), StatusType.WaitingForCrate, ""); + UpdateStatusForTre(submission.Id.ToString(), StatusType.ValidatingCrate, ""); + UpdateStatusForTre(submission.Id.ToString(), StatusType.FetchingWorkflow, ""); + UpdateStatusForTre(submission.Id.ToString(), StatusType.StagingWorkflow, ""); + UpdateStatusForTre(submission.Id.ToString(), StatusType.ExecutingWorkflow, ""); + UpdateStatusForTre(submission.Id.ToString(), StatusType.PreparingOutputs, ""); + UpdateStatusForTre(submission.Id.ToString(), StatusType.TransferredForDataOut, ""); Uri uri = new Uri(submission.DockerInputLocation); string fileName = Path.GetFileName(uri.LocalPath); diff --git a/src/TRE-UI/Controllers/TRECredentialsController.cs b/src/TRE-UI/Controllers/TRECredentialsController.cs new file mode 100644 index 000000000..b187f905c --- /dev/null +++ b/src/TRE-UI/Controllers/TRECredentialsController.cs @@ -0,0 +1,43 @@ +using BL.Models; +using BL.Services; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; +using BL.Models.APISimpleTypeReturns; +using TRE_UI.Services; +namespace TRE_UI.Controllers +{ + [Authorize(Roles = "dare-tre-admin")] + public class TRECredentialsController : Controller + { + private readonly ITREClientHelper _clientHelper; + public TRECredentialsController(ITREClientHelper client) + { + _clientHelper = client; + } + [HttpGet] + public async Task UpdateCredentialsAsync() + { + return View(await ControllerHelpers.CheckCredentialsAreValid("TRECredentials", _clientHelper)); + + } + [HttpPost] + + public async Task UpdateCredentials(KeycloakCredentials credentials) + { + if (await ControllerHelpers.UpdateCredentials("TRECredentials", _clientHelper, ModelState, + credentials)) + { + return RedirectToAction("Index", "Home"); + } + else + { + return View(credentials); + } + + } + + + + + } +} \ No newline at end of file diff --git a/src/TRE-UI/Views/Shared/_Layout.cshtml b/src/TRE-UI/Views/Shared/_Layout.cshtml index 7fa481b47..795058b92 100644 --- a/src/TRE-UI/Views/Shared/_Layout.cshtml +++ b/src/TRE-UI/Views/Shared/_Layout.cshtml @@ -45,7 +45,9 @@ - + diff --git a/src/TRE-UI/Views/TRECredentials/UpdateCredentials.cshtml b/src/TRE-UI/Views/TRECredentials/UpdateCredentials.cshtml new file mode 100644 index 000000000..70ca1bb7f --- /dev/null +++ b/src/TRE-UI/Views/TRECredentials/UpdateCredentials.cshtml @@ -0,0 +1,44 @@ +@model BL.Models.KeycloakCredentials +@{ + ViewData["Title"] = "TRE Credentials"; +} +
+

TRE Credentials

+
+
+
+ @if (!Model.Valid) + { +

TRE Credentials are missing or invalid. Please update

+ } +
+
+
+
+ @using (Html.BeginForm("UpdateCredentials", "TRECredentials", FormMethod.Post, new { id = "frmMain" })) + { + @Html.AntiForgeryToken() + @Html.HiddenFor(m => m.Id) + +
+ @Html.LabelFor(m => m.UserName, new { @class = "form-label" }) + @Html.TextBoxFor(x => x.UserName, new { @class = "form-control show-tick" }) + @Html.ValidationMessageFor(m => m.UserName) +
+
+ @Html.LabelFor(m => m.PasswordEnc, new { @class = "form-label" }) + @Html.PasswordFor(x => x.PasswordEnc, new { @class = "form-control show-tick noBottomMargin" }) + @Html.ValidationMessageFor(m => m.PasswordEnc) +
+
+ @Html.LabelFor(m => m.ConfirmPassword, new { @class = "form-label" }) + @Html.PasswordFor(x => x.ConfirmPassword, new { @class = "form-control show-tick noBottomMargin" }) + @Html.ValidationMessageFor(m => m.ConfirmPassword) +
+
+ +
+ } +
+
+
\ No newline at end of file