Skip to content

Commit 7bad17f

Browse files
author
sezalchug
committed
resolving comments
1 parent 94cccae commit 7bad17f

File tree

7 files changed

+22
-24
lines changed

7 files changed

+22
-24
lines changed

src/Config/Converters/RuntimeHealthOptionsConvertorFactory.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ internal HealthCheckOptionsConverter(bool replaceEnvVar)
5353
if (reader.TokenType is JsonTokenType.StartObject)
5454
{
5555
bool? enabled = null;
56-
List<string>? roles = null;
56+
HashSet<string>? roles = null;
5757

5858
while (reader.Read())
5959
{
@@ -80,7 +80,7 @@ internal HealthCheckOptionsConverter(bool replaceEnvVar)
8080
// Check if the token type is an array
8181
if (reader.TokenType == JsonTokenType.StartArray)
8282
{
83-
List<string> stringList = new();
83+
HashSet<string> stringList = new();
8484

8585
// Read the array elements one by one
8686
while (reader.Read() && reader.TokenType != JsonTokenType.EndArray)

src/Config/HealthCheck/RuntimeHealthCheckConfig.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ public record RuntimeHealthCheckConfig : HealthCheckConfig
88
// TODO: Add support for caching in upcoming PRs
99
// public int cache-ttl-seconds { get; set; };
1010

11-
public List<string>? Roles { get; set; }
11+
public HashSet<string>? Roles { get; set; }
1212

1313
// TODO: Add support for parallel stream to run the health check query in upcoming PRs
1414
// public int MaxDop { get; set; } = 1; // Parallelized streams to run Health Check (Default: 1)
@@ -17,7 +17,7 @@ public RuntimeHealthCheckConfig() : base()
1717
{
1818
}
1919

20-
public RuntimeHealthCheckConfig(bool? Enabled, List<string>? Roles = null) : base(Enabled)
20+
public RuntimeHealthCheckConfig(bool? Enabled, HashSet<string>? Roles = null) : base(Enabled)
2121
{
2222
this.Roles = Roles;
2323
}

src/Config/ObjectModel/RuntimeConfig.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,8 @@ Runtime.GraphQL is not null &&
151151
Runtime.GraphQL.EnableAggregation;
152152

153153
[JsonIgnore]
154-
public List<string> AllowedRolesForHealth =>
155-
Runtime?.Health?.Roles ?? new List<string>();
154+
public HashSet<string> AllowedRolesForHealth =>
155+
Runtime?.Health?.Roles ?? new HashSet<string>();
156156

157157
private Dictionary<string, DataSource> _dataSourceNameToDataSource;
158158

src/Service.Tests/Configuration/HealthEndpointRolesTests.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ private static void CreateCustomConfigFile(Dictionary<string, Entity> entityMap,
114114
Schema: string.Empty,
115115
DataSource: dataSource,
116116
Runtime: new(
117-
Health: new(Enabled: true, Roles: role != null ? new List<string> { role } : null),
117+
Health: new(Enabled: true, Roles: role != null ? new HashSet<string> { role } : null),
118118
Rest: new(Enabled: true),
119119
GraphQL: new(Enabled: true),
120120
Host: hostOptions

src/Service/HealthCheck/ComprehensiveHealthReportResponseWriter.cs

+4-13
Original file line numberDiff line numberDiff line change
@@ -69,34 +69,25 @@ public Task WriteResponse(HttpContext context)
6969
// Global comprehensive Health Check Enabled
7070
if (config.IsHealthEnabled)
7171
{
72-
_healthCheckHelper.UpdateIncomingRoleHeader(context);
72+
_healthCheckHelper.StoreIncomingRoleHeader(context);
7373
if (!_healthCheckHelper.IsUserAllowedToAccessHealthCheck(context, config.IsDevelopmentMode(), config.AllowedRolesForHealth))
7474
{
75-
LogTrace("Comprehensive Health Check Report is not allowed: 403 Forbidden due to insufficient permissions.");
75+
_logger.LogError("Comprehensive Health Check Report is not allowed: 403 Forbidden due to insufficient permissions.");
7676
context.Response.StatusCode = StatusCodes.Status403Forbidden;
7777
return context.Response.CompleteAsync();
7878
}
7979

8080
ComprehensiveHealthCheckReport dabHealthCheckReport = _healthCheckHelper.GetHealthCheckResponse(context, config);
8181
string response = JsonSerializer.Serialize(dabHealthCheckReport, options: new JsonSerializerOptions { WriteIndented = true, DefaultIgnoreCondition = System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingNull });
82-
LogTrace($"Health check response writer writing status as: {dabHealthCheckReport.Status}");
82+
_logger.LogTrace($"Health check response writer writing status as: {dabHealthCheckReport.Status}");
8383
return context.Response.WriteAsync(response);
8484
}
8585
else
8686
{
87-
LogTrace("Comprehensive Health Check Report Not Found: 404 Not Found.");
87+
_logger.LogError("Comprehensive Health Check Report Not Found: 404 Not Found.");
8888
context.Response.StatusCode = StatusCodes.Status404NotFound;
8989
return context.Response.CompleteAsync();
9090
}
9191
}
92-
93-
/// <summary>
94-
/// Logs a trace message if a logger is present and the logger is enabled for trace events.
95-
/// </summary>
96-
/// <param name="message">Message to emit.</param>
97-
private void LogTrace(string message)
98-
{
99-
_logger.LogTrace(message);
100-
}
10192
}
10293
}

src/Service/HealthCheck/HealthCheckHelper.cs

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT License.
33

4+
using System;
45
using System.Collections.Generic;
56
using System.Diagnostics;
67
using System.Linq;
@@ -62,11 +63,17 @@ public ComprehensiveHealthCheckReport GetHealthCheckResponse(HttpContext context
6263
}
6364

6465
// Updates the incoming role header with the appropriate value from the request headers.
65-
public void UpdateIncomingRoleHeader(HttpContext httpContext)
66+
public void StoreIncomingRoleHeader(HttpContext httpContext)
6667
{
6768
StringValues clientRoleHeader = httpContext.Request.Headers[AuthorizationResolver.CLIENT_ROLE_HEADER];
6869
StringValues clientTokenHeader = httpContext.Request.Headers[AuthenticationOptions.CLIENT_PRINCIPAL_HEADER];
6970

71+
if (clientRoleHeader.Count > 1 || clientTokenHeader.Count > 1)
72+
{
73+
throw new ArgumentException("Multiple values for the client role or token header are not allowed.");
74+
}
75+
76+
// Role Header is not present in the request, set it to anonymous.
7077
if (clientRoleHeader.Count == 1)
7178
{
7279
_incomingRoleHeader = clientRoleHeader.ToString().ToLowerInvariant();
@@ -82,7 +89,7 @@ public void UpdateIncomingRoleHeader(HttpContext httpContext)
8289
/// <param name="hostMode">Compare with the HostMode of DAB</param>
8390
/// <param name="allowedRoles">AllowedRoles in the Runtime.Health config</param>
8491
/// <returns></returns>
85-
public bool IsUserAllowedToAccessHealthCheck(HttpContext httpContext, bool isDevelopmentMode, List<string> allowedRoles)
92+
public bool IsUserAllowedToAccessHealthCheck(HttpContext httpContext, bool isDevelopmentMode, HashSet<string> allowedRoles)
8693
{
8794
if (allowedRoles == null || allowedRoles.Count == 0)
8895
{

src/Service/HealthCheck/HttpUtilities.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ public void ConfigureApiRoute(HttpContext httpContext)
106106
// Send a GET request to the API
107107
apiRoute = $"{apiRoute}{Utilities.CreateHttpRestQuery(entityName, first)}";
108108
HttpRequestMessage message = new(method: HttpMethod.Get, requestUri: apiRoute);
109-
if (string.IsNullOrEmpty(incomingRoleToken) && string.IsNullOrEmpty(incomingRoleHeader))
109+
if (!(string.IsNullOrEmpty(incomingRoleToken) && string.IsNullOrEmpty(incomingRoleHeader)))
110110
{
111111
message.Headers.Add(AuthenticationOptions.CLIENT_PRINCIPAL_HEADER, incomingRoleToken);
112112
message.Headers.Add(AuthorizationResolver.CLIENT_ROLE_HEADER, incomingRoleHeader);
@@ -168,7 +168,7 @@ public void ConfigureApiRoute(HttpContext httpContext)
168168
Content = content
169169
};
170170

171-
if (string.IsNullOrEmpty(incomingRoleToken) && string.IsNullOrEmpty(incomingRoleHeader))
171+
if (!(string.IsNullOrEmpty(incomingRoleToken) && string.IsNullOrEmpty(incomingRoleHeader)))
172172
{
173173
message.Headers.Add(AuthenticationOptions.CLIENT_PRINCIPAL_HEADER, incomingRoleToken);
174174
message.Headers.Add(AuthorizationResolver.CLIENT_ROLE_HEADER, incomingRoleHeader);

0 commit comments

Comments
 (0)