-
Notifications
You must be signed in to change notification settings - Fork 2
feat: Participant Audit Log Table #1881
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
Open
Warren-Pitterson
wants to merge
53
commits into
main
Choose a base branch
from
feat/DTOSS-12573-Audit-Log-Table
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
53 commits
Select commit
Hold shift + click to select a range
b5d5b42
feat: Audit Source Enum
Warren-Pitterson 52b9524
feat: add queues package
Warren-Pitterson d3b9991
feat: docker
Warren-Pitterson 8f469bf
feat: sln
Warren-Pitterson 596c412
feat: audit writer function
Warren-Pitterson debc070
feat: migration
Warren-Pitterson 24af8be
feat: EF model
Warren-Pitterson 18ced9d
feat: queue
Warren-Pitterson 03adaef
feat: ParticipantAuditMessage model
Warren-Pitterson 104628b
feat: receive function queue implementation
Warren-Pitterson d9e8677
test: tests
Warren-Pitterson 87079a5
chore: removed unnecessary usings
Warren-Pitterson d59f6b7
Merge branch 'main' into feat/DTOSS-12573-Audit-Log-Table
Warren-Pitterson 2e7ba48
chore: docker sonarqube security fix
Warren-Pitterson 70ba022
Merge branch 'feat/DTOSS-12573-Audit-Log-Table' of https://github.com…
Warren-Pitterson 68af007
Update application/CohortManager/src/Functions/Shared/Common/Extensio…
Warren-Pitterson 980fa1c
chore: PR comment reviews
Warren-Pitterson 8ad27e2
Merge branch 'feat/DTOSS-12573-Audit-Log-Table' of https://github.com…
Warren-Pitterson f39519b
chore: removed PII
Warren-Pitterson ba0f97d
chore: removed PII
Warren-Pitterson 94018b9
chore: PII removed
Warren-Pitterson 690a667
feat: return changed to throw
Warren-Pitterson 6dab401
feat: lazy initiation
Warren-Pitterson 5300254
test: updated tests
Warren-Pitterson 1ef4bfd
test: test covering the audit failure path.
Warren-Pitterson b8d15db
feat: removed redundant Function suffix and changed raw_data_ref to b…
Warren-Pitterson a05315b
test: added using, date change
Warren-Pitterson ba5a60f
Merge branch 'main' into feat/DTOSS-12573-Audit-Log-Table
Warren-Pitterson dbe9ee5
feat: write to blob container, rename container, fallback
Warren-Pitterson 3182d28
test: test added
Warren-Pitterson a19584d
chore: changed from CREATED_DATETIME to DATE_CREATED to match other s…
Warren-Pitterson 99bd4dc
chore: removed source from blobPath,
Warren-Pitterson 055ed8d
fix: sonarqube error handling
Warren-Pitterson ef85585
fix: sonarqube - dockerfile change
Warren-Pitterson 6f90778
test: update test to new implementation
Warren-Pitterson bb6770d
refactor: changed from queue to service bus trigger
Warren-Pitterson f415474
feat: createdDatetime model changes
Warren-Pitterson 75fe27f
feat: participantAuditLogs made public
Warren-Pitterson e5e97d0
feat: index changes
Warren-Pitterson 275a03f
test: unit tests
Warren-Pitterson d46b2d6
test: unit test
Warren-Pitterson 0544439
Merge branch 'main' into feat/DTOSS-12573-Audit-Log-Table
Warren-Pitterson 57a12b2
chore: remove unnecessary using
Warren-Pitterson 2e1abe8
feat: replaced auditQueueSender for IQueueClient
Warren-Pitterson 05e1c8e
chore: queue renamed
Warren-Pitterson 0dda248
feat: removed lazy loading and added BlobStorageHelper Method
Warren-Pitterson 36ac055
chore: typo
Warren-Pitterson ba0a743
test: updated tests
Warren-Pitterson 4e05854
chore: whitespace and hardcoded value changed to enum
Warren-Pitterson 7d4d91f
chore: whitespace
Warren-Pitterson 73bd965
feat: add environment variables to dockerfile
Warren-Pitterson 7c4f69b
feat: Sequential audit sends in batches
Warren-Pitterson ddd338b
feat: Queues added to service-bus config, graceful fallback on durabl…
Warren-Pitterson File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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
107 changes: 107 additions & 0 deletions
107
application/CohortManager/src/Functions/AuditServices/AuditWriter/AuditWriter.cs
This file contains hidden or 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,107 @@ | ||
| namespace NHS.CohortManager.AuditServices; | ||
|
|
||
| using System.Text.Json; | ||
| using DataServices.Database; | ||
| using Microsoft.Azure.Functions.Worker; | ||
| using Microsoft.Extensions.Logging; | ||
| using Model; | ||
| using Common; | ||
|
|
||
| public class AuditWriter | ||
| { | ||
| private const string AuditBlobContainer = "participant-audit"; | ||
| private readonly DataServicesContext _dbContext; | ||
| private readonly ILogger<AuditWriter> _logger; | ||
| private readonly IBlobStorageHelper _blobStorageHelper; | ||
| private readonly AuditWriterConfig _config; | ||
|
|
||
| private static readonly JsonSerializerOptions JsonOptions = new() | ||
| { | ||
| PropertyNameCaseInsensitive = true | ||
| }; | ||
|
|
||
| public AuditWriter(DataServicesContext dbContext, ILogger<AuditWriter> logger, IBlobStorageHelper blobStorageHelper, AuditWriterConfig config) | ||
| { | ||
| _dbContext = dbContext; | ||
| _logger = logger; | ||
| _blobStorageHelper = blobStorageHelper; | ||
| _config = config; | ||
| } | ||
|
|
||
| [Function(nameof(AuditWriter))] | ||
| public async Task Run( | ||
| [ServiceBusTrigger("%AuditQueueName%", Connection = "ServiceBusConnectionString")] string messageText, FunctionContext context) | ||
| { | ||
| ParticipantAuditMessage? audit; | ||
| try | ||
| { | ||
| audit = JsonSerializer.Deserialize<ParticipantAuditMessage>(messageText, JsonOptions); | ||
| } | ||
| catch (JsonException ex) | ||
| { | ||
| throw new InvalidOperationException("Failed to deserialise audit message.", ex); | ||
| } | ||
|
|
||
| if (audit is null) | ||
| { | ||
| _logger.LogError("Failed to deserialise audit message"); | ||
| throw new JsonException("Audit message deserialised to null."); | ||
| } | ||
|
|
||
| audit.RawDataRef = await WriteSnapshotToBlobAsync(audit); | ||
|
|
||
| var auditLog = new ParticipantAuditLog | ||
| { | ||
| CorrelationId = audit.CorrelationId, | ||
| NhsNumber = audit.NhsNumber, | ||
| BatchId = audit.BatchId, | ||
| CreatedDatetime = audit.CreatedDatetime, | ||
| RecordSource = (int)audit.Source, | ||
| RecordSourceDesc = audit.RecordSourceDesc, | ||
| CreatedBy = audit.CreatedBy, | ||
| ScreeningId = audit.ScreeningId, | ||
| RawDataRef = audit.RawDataRef | ||
| }; | ||
|
|
||
| _dbContext.Set<ParticipantAuditLog>().Add(auditLog); | ||
| await _dbContext.SaveChangesAsync(); | ||
|
|
||
| _logger.LogInformation("Audit written | Source: {Source} | Correlation: {CorrelationId}", | ||
| audit.Source, audit.CorrelationId); | ||
| } | ||
|
|
||
| private async Task<string?> WriteSnapshotToBlobAsync(ParticipantAuditMessage message) | ||
| { | ||
| try | ||
| { | ||
| var blobPath = $"{message.CreatedDatetime:dd-MM-yyyy}/{message.CorrelationId}.json"; | ||
|
|
||
| var payload = message.RequestSnapshot is not null | ||
| ? JsonSerializer.SerializeToUtf8Bytes(message.RequestSnapshot) | ||
| : JsonSerializer.SerializeToUtf8Bytes(message); | ||
|
|
||
| var blobFile = new BlobFile(payload, blobPath); | ||
| var uri = await _blobStorageHelper.UploadFileToBlobStorageAndGetUri( | ||
| _config.AzureWebJobsStorage, AuditBlobContainer, blobFile, overwrite: true); | ||
|
|
||
| if (uri is null) | ||
| { | ||
| _logger.LogError( | ||
| "Failed to write audit snapshot to blob for CorrelationId {CorrelationId}. " + | ||
| "Audit will be persisted without a blob reference.", | ||
| message.CorrelationId); | ||
| return null; | ||
| } | ||
|
|
||
| return uri; | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| _logger.LogError(ex, | ||
| "Failed to write audit snapshot to blob for CorrelationId {CorrelationId}. " + | ||
| "Audit will be persisted without a blob reference.", | ||
| message.CorrelationId); | ||
| return null; | ||
| } | ||
| } | ||
| } | ||
36 changes: 36 additions & 0 deletions
36
application/CohortManager/src/Functions/AuditServices/AuditWriter/AuditWriter.csproj
This file contains hidden or 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,36 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
| <PropertyGroup> | ||
| <ProjectGuid>{A1B2C3D4-E5F6-7890-ABCD-EF1234567890}</ProjectGuid> | ||
| <TargetFramework>net8.0</TargetFramework> | ||
| <AzureFunctionsVersion>v4</AzureFunctionsVersion> | ||
| <OutputType>Exe</OutputType> | ||
| <ImplicitUsings>enable</ImplicitUsings> | ||
| <Nullable>enable</Nullable> | ||
| </PropertyGroup> | ||
| <ItemGroup> | ||
| <PackageReference Include="Microsoft.Azure.Functions.Worker" /> | ||
| <PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.ServiceBus" /> | ||
| <PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" /> | ||
| <PackageReference Include="Microsoft.Extensions.Diagnostics.HealthChecks" /> | ||
| <PackageReference Include="Azure.Storage.Blobs" /> | ||
| <PackageReference Include="Azure.Messaging.ServiceBus" /> | ||
| </ItemGroup> | ||
| <ItemGroup> | ||
| <None Update="host.json"> | ||
| <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> | ||
| </None> | ||
| <None Update="local.settings.json"> | ||
| <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> | ||
| <CopyToPublishDirectory>Never</CopyToPublishDirectory> | ||
| </None> | ||
| </ItemGroup> | ||
| <ItemGroup> | ||
| <Using Include="System.Threading.ExecutionContext" Alias="ExecutionContext" /> | ||
| </ItemGroup> | ||
| <ItemGroup> | ||
| <ProjectReference Include="..\..\Shared\DataServices.Database\DataServices.Database.csproj" /> | ||
| <ProjectReference Include="..\..\Shared\DataServices.Core\DataServices.Core.csproj" /> | ||
| <ProjectReference Include="..\..\Shared\Common\Common.csproj" /> | ||
| <ProjectReference Include="..\..\Shared\HealthChecks\HealthChecks.csproj" /> | ||
| </ItemGroup> | ||
| </Project> |
11 changes: 11 additions & 0 deletions
11
application/CohortManager/src/Functions/AuditServices/AuditWriter/AuditWriterConfig.cs
This file contains hidden or 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,11 @@ | ||
| namespace NHS.CohortManager.AuditServices; | ||
|
|
||
| using System.ComponentModel.DataAnnotations; | ||
|
|
||
| public class AuditWriterConfig | ||
| { | ||
| [Required] | ||
| public required string ServiceBusConnectionString { get; set; } | ||
| [Required] | ||
| public required string AzureWebJobsStorage { get; set; } | ||
| } |
16 changes: 16 additions & 0 deletions
16
application/CohortManager/src/Functions/AuditServices/AuditWriter/Dockerfile
This file contains hidden or 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 @@ | ||
| ARG BASE_IMAGE | ||
| FROM $BASE_IMAGE AS function | ||
|
|
||
| COPY ./AuditServices/AuditWriter /app/src/dotnet-function-app | ||
| WORKDIR /app/src/dotnet-function-app | ||
|
|
||
| RUN --mount=type=cache,target=/root/.nuget/packages \ | ||
| dotnet publish ./*.csproj --output /home/site/wwwroot | ||
|
|
||
| # To enable ssh & remote debugging on app service change the base image to the one below | ||
| # FROM mcr.microsoft.com/azure-functions/dotnet-isolated:4-dotnet-isolated8.0-appservice | ||
| FROM mcr.microsoft.com/azure-functions/dotnet-isolated:4-dotnet-isolated8.0 | ||
| ENV AzureWebJobsScriptRoot=/home/site/wwwroot \ | ||
| AzureFunctionsJobHost__Logging__Console__IsEnabled=true | ||
|
|
||
| COPY --from=function ["/home/site/wwwroot", "/home/site/wwwroot"] |
23 changes: 23 additions & 0 deletions
23
application/CohortManager/src/Functions/AuditServices/AuditWriter/Program.cs
This file contains hidden or 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,23 @@ | ||
| using Microsoft.Extensions.Hosting; | ||
| using Microsoft.Extensions.DependencyInjection; | ||
| using DataServices.Core; | ||
| using DataServices.Database; | ||
| using HealthChecks.Extensions; | ||
| using Common; | ||
| using NHS.CohortManager.AuditServices; | ||
|
|
||
| var host = new HostBuilder() | ||
| .ConfigureFunctionsWorkerDefaults() | ||
| .AddConfiguration<AuditWriterConfig>(out AuditWriterConfig config) | ||
| .AddDataServicesHandler<DataServicesContext>() | ||
| .AddServiceBusClient(config.ServiceBusConnectionString) | ||
| .ConfigureServices(services => | ||
| { | ||
| services.AddDatabaseHealthCheck("AuditWriter"); | ||
| services.AddSingleton(config); | ||
| services.AddTransient<IBlobStorageHelper, BlobStorageHelper>(); | ||
| }) | ||
| .AddTelemetry() | ||
| .Build(); | ||
|
|
||
| await host.RunAsync(); |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.