-
Notifications
You must be signed in to change notification settings - Fork 492
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
Added new audit event sub-types #2170
Changes from 9 commits
2423c46
4018dc5
30b2f12
5372a01
1e80e62
02b878b
b8048ef
ceb9283
0b1bbaa
ecd093e
88c93ce
dd200ae
25f372a
9ee34a0
30b2ade
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,10 +3,15 @@ | |
// Licensed under the MIT License (MIT). See LICENSE in the repo root for license information. | ||
// ------------------------------------------------------------------------------------------------- | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Net; | ||
using EnsureThat; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.Extensions.Options; | ||
using Microsoft.Health.Api.Features.Audit; | ||
using Microsoft.Health.Core.Configs; | ||
using Microsoft.Health.Core.Features.Context; | ||
using Microsoft.Health.Core.Features.Security; | ||
using Microsoft.Health.Fhir.Core.Features.Context; | ||
|
@@ -21,19 +26,23 @@ public class AuditHelper : IAuditHelper | |
private readonly RequestContextAccessor<IFhirRequestContext> _fhirRequestContextAccessor; | ||
private readonly IAuditLogger _auditLogger; | ||
private readonly IAuditHeaderReader _auditHeaderReader; | ||
private readonly IOptions<AuditConfiguration> _auditConfiguration; | ||
|
||
public AuditHelper( | ||
RequestContextAccessor<IFhirRequestContext> fhirRequestContextAccessor, | ||
IAuditLogger auditLogger, | ||
IAuditHeaderReader auditHeaderReader) | ||
IAuditHeaderReader auditHeaderReader, | ||
IOptions<AuditConfiguration> auditConfiguration) | ||
{ | ||
EnsureArg.IsNotNull(fhirRequestContextAccessor, nameof(fhirRequestContextAccessor)); | ||
EnsureArg.IsNotNull(auditLogger, nameof(auditLogger)); | ||
EnsureArg.IsNotNull(auditHeaderReader, nameof(auditHeaderReader)); | ||
EnsureArg.IsNotNull(auditConfiguration, nameof(auditConfiguration)); | ||
|
||
_fhirRequestContextAccessor = fhirRequestContextAccessor; | ||
_auditLogger = auditLogger; | ||
_auditHeaderReader = auditHeaderReader; | ||
_auditConfiguration = auditConfiguration; | ||
} | ||
|
||
/// <inheritdoc /> | ||
|
@@ -68,6 +77,13 @@ private void Log(AuditAction auditAction, HttpStatusCode? statusCode, HttpContex | |
IFhirRequestContext fhirRequestContext = _fhirRequestContextAccessor.RequestContext; | ||
|
||
string auditEventType = fhirRequestContext.AuditEventType; | ||
var customHeaders = _auditHeaderReader.Read(httpContext) as Dictionary<string, string>; | ||
|
||
// check for custom audit headers in context-response | ||
if (auditAction == AuditAction.Executed) | ||
{ | ||
CheckForCustomAuditHeadersInResponse(httpContext, customHeaders); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Put it under if statement below. |
||
|
||
// Audit the call if an audit event type is associated with the action. | ||
if (!string.IsNullOrEmpty(auditEventType)) | ||
|
@@ -81,7 +97,39 @@ private void Log(AuditAction auditAction, HttpStatusCode? statusCode, HttpContex | |
correlationId: fhirRequestContext.CorrelationId, | ||
callerIpAddress: httpContext.Connection?.RemoteIpAddress?.ToString(), | ||
callerClaims: claimsExtractor.Extract(), | ||
customHeaders: _auditHeaderReader.Read(httpContext)); | ||
customHeaders: customHeaders); | ||
} | ||
} | ||
|
||
private void CheckForCustomAuditHeadersInResponse(HttpContext httpContext, Dictionary<string, string> customHeaders) | ||
{ | ||
var responseCustomHeaders = httpContext.Response.Headers.Where(x => x.Key.StartsWith(_auditConfiguration.Value.CustomAuditHeaderPrefix, StringComparison.OrdinalIgnoreCase)).ToDictionary(a => a.Key, a => a.Value.ToString()); | ||
if (responseCustomHeaders.Any()) | ||
{ | ||
var largeHeaders = responseCustomHeaders.Where(x => x.Value.Length > AuditConstants.MaximumLengthOfCustomHeader).ToDictionary(a => a.Key, a => a.Value.ToString()); | ||
if (largeHeaders.Any()) | ||
{ | ||
throw new AuditHeaderTooLargeException(largeHeaders.First().Key, largeHeaders.First().Value.Length); | ||
} | ||
|
||
foreach (var header in responseCustomHeaders) | ||
{ | ||
var headerValue = header.Value.ToString(); | ||
if (headerValue.Length > AuditConstants.MaximumLengthOfCustomHeader) | ||
{ | ||
throw new AuditHeaderTooLargeException(header.Key, headerValue.Length); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
} | ||
|
||
if (!customHeaders.ContainsKey(header.Key)) | ||
{ | ||
customHeaders.Add(header.Key, headerValue); | ||
} | ||
} | ||
|
||
if (customHeaders.Count > AuditConstants.MaximumNumberOfCustomHeaders) | ||
{ | ||
throw new AuditHeaderCountExceededException(customHeaders.Count); | ||
} | ||
} | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,8 +13,10 @@ | |
using MediatR; | ||
using Microsoft.Azure.Cosmos; | ||
using Microsoft.Extensions.Logging; | ||
using Microsoft.Extensions.Options; | ||
using Microsoft.Extensions.Primitives; | ||
using Microsoft.Health.Abstractions.Exceptions; | ||
using Microsoft.Health.Core.Configs; | ||
using Microsoft.Health.Core.Features.Context; | ||
using Microsoft.Health.Fhir.Core.Features.Context; | ||
using Microsoft.Health.Fhir.CosmosDb.Features.Metrics; | ||
|
@@ -27,18 +29,21 @@ public class CosmosResponseProcessor : ICosmosResponseProcessor | |
private readonly RequestContextAccessor<IFhirRequestContext> _fhirRequestContextAccessor; | ||
private readonly IMediator _mediator; | ||
private readonly ICosmosQueryLogger _queryLogger; | ||
private readonly IOptions<AuditConfiguration> _auditConfiguration; | ||
private readonly ILogger<CosmosResponseProcessor> _logger; | ||
|
||
public CosmosResponseProcessor(RequestContextAccessor<IFhirRequestContext> fhirRequestContextAccessor, IMediator mediator, ICosmosQueryLogger queryLogger, ILogger<CosmosResponseProcessor> logger) | ||
public CosmosResponseProcessor(RequestContextAccessor<IFhirRequestContext> fhirRequestContextAccessor, IMediator mediator, ICosmosQueryLogger queryLogger, IOptions<AuditConfiguration> auditConfiguration, ILogger<CosmosResponseProcessor> logger) | ||
{ | ||
EnsureArg.IsNotNull(fhirRequestContextAccessor, nameof(fhirRequestContextAccessor)); | ||
EnsureArg.IsNotNull(mediator, nameof(mediator)); | ||
EnsureArg.IsNotNull(queryLogger, nameof(queryLogger)); | ||
EnsureArg.IsNotNull(queryLogger, nameof(queryLogger)); | ||
EnsureArg.IsNotNull(logger, nameof(logger)); | ||
|
||
_fhirRequestContextAccessor = fhirRequestContextAccessor; | ||
_mediator = mediator; | ||
_queryLogger = queryLogger; | ||
_auditConfiguration = auditConfiguration; | ||
_logger = logger; | ||
} | ||
|
||
|
@@ -145,6 +150,7 @@ private async Task AddRequestChargeToFhirRequestContext(double responseRequestCh | |
} | ||
|
||
requestContext.ResponseHeaders[CosmosDbHeaders.RequestCharge] = responseRequestCharge.ToString(CultureInfo.InvariantCulture); | ||
requestContext.ResponseHeaders[_auditConfiguration.Value.CustomAuditHeaderPrefix + CosmosDbHeaders.RequestCharge] = responseRequestCharge.ToString(CultureInfo.InvariantCulture); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Probably should ask that question few iterations before, but why we use custom header mechanism to report database consumption? First should it even go through custom header section of the audit i.e. AuditLogger.AuditMessageFormat If that's ok we can extend IFhirRequestContext to have float/integer to represent consumption charge (with default to 0) and set it up respectfully for sql/cosmos. Also how it's gonna be used? |
||
} | ||
|
||
var cosmosMetrics = new CosmosStorageRequestMetricsNotification(requestContext.AuditEventType, requestContext.ResourceType) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is dangerous.
Not every
IReadOnlyDictionary
is aDictionary
, so this can become null if implementation ofRead
would change.You can do
var newDictionary = Dictionary<string,string>(_auditHeaderReader.Read(httpContext))
to make it editable.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You change it to casting it to type, it's a same semantic.
Don't cast it, create a new one.