-
Notifications
You must be signed in to change notification settings - Fork 526
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add customer request headers to audit log (#637)
Add a feature to allow end users to add data to the audit logs by way of a custom HTTP header.
- Loading branch information
Showing
25 changed files
with
636 additions
and
174 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
src/Microsoft.Health.Fhir.Api/Features/Audit/AuditConstants.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// ------------------------------------------------------------------------------------------------- | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License (MIT). See LICENSE in the repo root for license information. | ||
// ------------------------------------------------------------------------------------------------- | ||
|
||
namespace Microsoft.Health.Fhir.Api.Features.Audit | ||
{ | ||
public static class AuditConstants | ||
{ | ||
public const string CustomAuditHeaderKeyValue = "CustomAuditHeaderCollectionKeyValue"; | ||
|
||
public const int MaximumNumberOfCustomHeaders = 10; | ||
|
||
public const int MaximumLengthOfCustomHeader = 2048; | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
src/Microsoft.Health.Fhir.Api/Features/Audit/AuditHeaderException.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// ------------------------------------------------------------------------------------------------- | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License (MIT). See LICENSE in the repo root for license information. | ||
// ------------------------------------------------------------------------------------------------- | ||
|
||
using Microsoft.Health.Fhir.Core.Exceptions; | ||
|
||
namespace Microsoft.Health.Fhir.Api.Features.Audit | ||
{ | ||
public class AuditHeaderException : FhirException | ||
{ | ||
public AuditHeaderException(string headerName, int size) | ||
: base(string.Format(Resources.CustomAuditHeaderTooLarge, AuditConstants.MaximumLengthOfCustomHeader, headerName, size)) | ||
{ | ||
} | ||
|
||
public AuditHeaderException(int size) | ||
: base(string.Format(Resources.TooManyCustomAuditHeaders, AuditConstants.MaximumNumberOfCustomHeaders, size)) | ||
{ | ||
} | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
src/Microsoft.Health.Fhir.Api/Features/Audit/AuditHeaderReader.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// ------------------------------------------------------------------------------------------------- | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License (MIT). See LICENSE in the repo root for license information. | ||
// ------------------------------------------------------------------------------------------------- | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using EnsureThat; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.Extensions.Options; | ||
using Microsoft.Extensions.Primitives; | ||
using Microsoft.Health.Fhir.Core.Configs; | ||
|
||
namespace Microsoft.Health.Fhir.Api.Features.Audit | ||
{ | ||
public class AuditHeaderReader : IAuditHeaderReader | ||
{ | ||
private readonly AuditConfiguration _auditConfiguration; | ||
|
||
public AuditHeaderReader(IOptions<AuditConfiguration> auditConfiguration) | ||
{ | ||
EnsureArg.IsNotNull(auditConfiguration?.Value, nameof(auditConfiguration)); | ||
|
||
_auditConfiguration = auditConfiguration.Value; | ||
} | ||
|
||
public IReadOnlyDictionary<string, string> Read(HttpContext httpContext) | ||
{ | ||
EnsureArg.IsNotNull(httpContext, nameof(httpContext)); | ||
|
||
object cachedCustomHeaders; | ||
|
||
if (httpContext.Items.TryGetValue(AuditConstants.CustomAuditHeaderKeyValue, out cachedCustomHeaders)) | ||
{ | ||
return cachedCustomHeaders as IReadOnlyDictionary<string, string>; | ||
} | ||
|
||
var customHeaders = new Dictionary<string, string>(); | ||
|
||
foreach (KeyValuePair<string, StringValues> header in httpContext.Request.Headers) | ||
{ | ||
if (header.Key.StartsWith(_auditConfiguration.CustomAuditHeaderPrefix, StringComparison.OrdinalIgnoreCase)) | ||
{ | ||
var headerValue = header.Value.ToString(); | ||
if (headerValue.Length > AuditConstants.MaximumLengthOfCustomHeader) | ||
{ | ||
throw new AuditHeaderException(header.Key, headerValue.Length); | ||
} | ||
|
||
customHeaders[header.Key] = headerValue; | ||
} | ||
} | ||
|
||
if (customHeaders.Count > AuditConstants.MaximumNumberOfCustomHeaders) | ||
{ | ||
throw new AuditHeaderException(customHeaders.Count); | ||
} | ||
|
||
httpContext.Items[AuditConstants.CustomAuditHeaderKeyValue] = customHeaders; | ||
return customHeaders; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
src/Microsoft.Health.Fhir.Api/Features/Audit/IAuditHeaderReader.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// ------------------------------------------------------------------------------------------------- | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License (MIT). See LICENSE in the repo root for license information. | ||
// ------------------------------------------------------------------------------------------------- | ||
|
||
using System.Collections.Generic; | ||
using Microsoft.AspNetCore.Http; | ||
|
||
namespace Microsoft.Health.Fhir.Api.Features.Audit | ||
{ | ||
public interface IAuditHeaderReader | ||
{ | ||
IReadOnlyDictionary<string, string> Read(HttpContext httpContext); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
src/Microsoft.Health.Fhir.Core/Configs/AuditConfiguration.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// ------------------------------------------------------------------------------------------------- | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License (MIT). See LICENSE in the repo root for license information. | ||
// ------------------------------------------------------------------------------------------------- | ||
|
||
using Microsoft.Health.Fhir.Core.Exceptions; | ||
|
||
namespace Microsoft.Health.Fhir.Core.Configs | ||
{ | ||
public class AuditConfiguration | ||
{ | ||
private string _customAuditHeaderPrefix = "X-MS-AZUREFHIR-AUDIT-"; | ||
|
||
public string CustomAuditHeaderPrefix | ||
{ | ||
get | ||
{ | ||
return _customAuditHeaderPrefix; | ||
} | ||
|
||
set | ||
{ | ||
if (string.IsNullOrEmpty(value)) | ||
{ | ||
throw new InvalidDefinitionException(Resources.CustomHeaderPrefixCannotBeEmpty); | ||
} | ||
|
||
_customAuditHeaderPrefix = value; | ||
} | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.