Skip to content

Commit

Permalink
remove mocked auth code.
Browse files Browse the repository at this point in the history
  • Loading branch information
devinleighsmith committed May 17, 2024
1 parent 2f884e6 commit 98b3140
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 42 deletions.
24 changes: 6 additions & 18 deletions source/backend/api/Helpers/Healthchecks/PimsCdogsHealthCheck.cs
Original file line number Diff line number Diff line change
@@ -1,38 +1,26 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using Pims.Api.Services;
using Pims.Api.Repositories.Cdogs;

namespace Pims.Api.Helpers.Healthchecks
{
public class PimsCdogsHealthcheck : IHealthCheck
{
private readonly IDocumentGenerationService _generationService;
private readonly IConfiguration _configuration;
private readonly ClaimsPrincipal _user;
private readonly IDocumentGenerationRepository _generationRepository;

public PimsCdogsHealthcheck(IDocumentGenerationService generationService, IConfiguration configuration, ClaimsPrincipal user)
public PimsCdogsHealthcheck(IDocumentGenerationRepository generationRepository)
{
_generationService = generationService;
_configuration = configuration;
_user = user;
_generationRepository = generationRepository;
}

public async Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default)
{
try
{
// for the purposes of this health check, grant the user in context the claim necessary to be treated as a service account.
if (!this._user.Claims.Any())
{
this._user.AddIdentity(new ClaimsIdentity(new List<Claim>() { new Claim("clientId", this._configuration.GetValue<string>("Keycloak:Client")) }));
}
var fileTypes = await _generationService.GetSupportedFileTypes();

var fileTypes = await _generationRepository.TryGetFileTypesAsync();
if (fileTypes.HttpStatusCode != System.Net.HttpStatusCode.OK || fileTypes.Payload == null || fileTypes.Payload.Dictionary.Count == 0)
{
return new HealthCheckResult(HealthStatus.Unhealthy, $"received invalid file types response from CDOGS");
Expand Down
23 changes: 5 additions & 18 deletions source/backend/api/Helpers/Healthchecks/PimsMayanHealthCheck.cs
Original file line number Diff line number Diff line change
@@ -1,38 +1,25 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using Pims.Api.Services;
using Pims.Api.Repositories.Mayan;

namespace Pims.Api.Helpers.Healthchecks
{
public class PimsMayanHealthcheck : IHealthCheck
{
private readonly IDocumentService _documentService;
private readonly ClaimsPrincipal _user;
private readonly IConfiguration _configuration;
private readonly IEdmsDocumentRepository _documentRepository;

public PimsMayanHealthcheck(IConfiguration configuration, IDocumentService documentService, ClaimsPrincipal user)
public PimsMayanHealthcheck(IEdmsDocumentRepository documentRepository)
{
_documentService = documentService;
_user = user;
_configuration = configuration;
_documentRepository = documentRepository;
}

public async Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default)
{
try
{
if (!this._user.Claims.Any())
{
// for the purposes of this health check, grant the user in context the claim necessary to be treated as a service account.
this._user.AddIdentity(new ClaimsIdentity(new List<Claim>() { new Claim("clientId", this._configuration.GetValue<string>("Keycloak:Client")) }));
}
var documentTypes = await _documentService.GetStorageDocumentTypes(page: 1);
var documentTypes = await _documentRepository.TryGetDocumentTypesAsync(string.Empty, 1, 1);
if (documentTypes.HttpStatusCode != System.Net.HttpStatusCode.OK || documentTypes.Payload == null || documentTypes.Payload.Count == 0)
{
return new HealthCheckResult(HealthStatus.Unhealthy, $"received invalid mayan response for document types");
Expand Down
7 changes: 1 addition & 6 deletions source/backend/api/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using System.Globalization;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Reflection;
using System.Security.Claims;
using System.Text;
Expand All @@ -27,8 +26,6 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Microsoft.IdentityModel.Tokens;
using Microsoft.OpenApi.Models;
using Pims.Api.Handlers;
Expand All @@ -54,7 +51,6 @@
using Pims.Dal.Keycloak;
using Pims.Geocoder;
using Pims.Ltsa;
using Pims.Ltsa.Configuration;
using Prometheus;

namespace Pims.Api
Expand Down Expand Up @@ -223,8 +219,7 @@ public void ConfigureServices(IServiceCollection services)
services.AddClamAvService(this.Configuration.GetSection("Av"));
services.AddHttpContextAccessor();

ClaimsPrincipal systemPrincipal = new ClaimsPrincipal();
services.AddTransient<ClaimsPrincipal>(s => s.GetService<IHttpContextAccessor>()?.HttpContext == null ? systemPrincipal : s.GetService<IHttpContextAccessor>().HttpContext.User);
services.AddTransient<ClaimsPrincipal>(s => s.GetService<IHttpContextAccessor>()?.HttpContext?.User);
services.AddScoped<IProxyRequestClient, ProxyRequestClient>();
services.AddScoped<IOpenIdConnectRequestClient, OpenIdConnectRequestClient>();
services.AddResponseCaching();
Expand Down

0 comments on commit 98b3140

Please sign in to comment.