Repository navigation
Server: audit in the service envelope, specific audits for user management and KeyCredential push #5226
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
Merged
Merged
Server: audit in the service envelope, specific audits for user management and KeyCredential push #5226
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
d4dd34c
Server: audit failed services in the service envelope (SS-6)
romanett 46b8e08
Server: specific audit events for user management and KeyCredential p…
romanett af8b7ff
Server: address SS-6 review (async sink, fan-out, identity, timestamp…
romanett d452d21
Server: session fault audit keeps an anonymous request's ClientUserId…
romanett 3ec7274
Server: audit sink logging via LoggerMessage, async test, Lock in tes…
romanett 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
145 changes: 145 additions & 0 deletions
145
src/Opc.Ua.Server/KeyCredential/KeyCredentialPushSubject.Audit.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,145 @@ | ||
| // SPDX-FileCopyrightText: Copyright (c) 2005-2026, OPC Federation AISBL | ||
| // SPDX-License-Identifier: MIT | ||
|
|
||
| using System.Collections.Generic; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using Microsoft.Extensions.Logging; | ||
| using Microsoft.Extensions.Logging.Abstractions; | ||
|
|
||
| namespace Opc.Ua.Server | ||
| { | ||
| /// <summary> | ||
| /// The audit events of the KeyCredential push methods (OPC 10000-12 §8.6). | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// <para> | ||
| /// Each audited method reports one event when it returns, on success and on failure | ||
| /// (including a denied call), as OPC 10000-4 §6.5.9 requires for a method that changes | ||
| /// the state of the system: | ||
| /// </para> | ||
| /// <list type="bullet"> | ||
| /// <item><c>UpdateCredential</c>: KeyCredentialUpdatedAuditEventType (§8.6.9), with the | ||
| /// ResourceUri of the credential. The CredentialSecret is not copied into the event.</item> | ||
| /// <item><c>DeleteCredential</c>: KeyCredentialDeletedAuditEventType (§8.6.10), with the | ||
| /// ResourceUri of the credential.</item> | ||
| /// <item><c>CreateCredential</c>: Part 12 defines no type for it, so it raises an | ||
| /// AuditUpdateMethodEventType event.</item> | ||
| /// <item><c>GetEncryptingKey</c> only reads, so it is not audited (§6.5.9 makes that | ||
| /// optional).</item> | ||
| /// </list> | ||
| /// </remarks> | ||
| public sealed partial class KeyCredentialPushSubject | ||
| { | ||
| private async ValueTask<CreateCredentialMethodStateResult> OnAuditedCreateCredentialAsync( | ||
| ISystemContext context, | ||
| MethodState method, | ||
| NodeId objectId, | ||
| string name, | ||
| string resourceUri, | ||
| string profileUri, | ||
| ArrayOf<string> endpointUrls, | ||
| CancellationToken ct = default) | ||
| { | ||
| CreateCredentialMethodStateResult result = await OnCreateCredentialAsync( | ||
| context, method, objectId, name, resourceUri, profileUri, endpointUrls, ct).ConfigureAwait(false); | ||
| if (GetAuditServer(context) is { Auditing: true } server) | ||
| { | ||
| server.ReportAuditMethodCallEvent( | ||
| context, | ||
| objectId, | ||
| method.NodeId, | ||
| ArrayOf.Wrapped(new[] | ||
| { | ||
| Variant.From(name), | ||
| Variant.From(resourceUri), | ||
| Variant.From(profileUri), | ||
| Variant.From(endpointUrls) | ||
| }), | ||
| result.ServiceResult, | ||
| static () => new AuditUpdateMethodEventState(null), | ||
| GetAuditLogger(context)); | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| private async ValueTask<KeyCredentialUpdateMethodStateResult> OnAuditedUpdateCredentialAsync( | ||
| ISystemContext context, | ||
| MethodState method, | ||
| NodeId objectId, | ||
| string credentialId, | ||
| ByteString credentialSecret, | ||
| string certificateThumbprint, | ||
| string securityPolicyUri, | ||
| CancellationToken ct = default) | ||
| { | ||
| KeyCredentialUpdateMethodStateResult result = await OnUpdateCredentialAsync( | ||
| context, method, objectId, credentialId, credentialSecret, certificateThumbprint, securityPolicyUri, ct) | ||
| .ConfigureAwait(false); | ||
|
romanett marked this conversation as resolved.
Resolved
|
||
| if (GetAuditServer(context) is { Auditing: true } server) | ||
| { | ||
| string resourceUri = GetResourceUri(method); | ||
| server.ReportAuditMethodCallEvent( | ||
| context, | ||
| objectId, | ||
| method.NodeId, | ||
| ArrayOf.Wrapped(new[] | ||
| { | ||
| Variant.From(credentialId), | ||
| // the (possibly encrypted) secret never goes into an audit event | ||
| Variant.From(AuditEvents.RedactedPrivateKey), | ||
| Variant.From(certificateThumbprint), | ||
| Variant.From(securityPolicyUri) | ||
| }), | ||
| result.ServiceResult, | ||
| static () => new KeyCredentialUpdatedAuditEventState(null), | ||
| GetAuditLogger(context), | ||
| (auditContext, e) => e.SetChildValue(auditContext, BrowseNames.ResourceUri, resourceUri, false)); | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| private async ValueTask<ServiceResult> OnAuditedDeleteCredentialAsync( | ||
| ISystemContext context, | ||
| MethodState method, | ||
| NodeId objectId, | ||
| ArrayOf<Variant> inputArguments, | ||
| List<Variant> outputArguments, | ||
| CancellationToken ct = default) | ||
| { | ||
| // read before the call: a successful delete removes the credential node | ||
| string resourceUri = GetResourceUri(method); | ||
| ServiceResult result = await OnDeleteCredentialAsync( | ||
| context, method, objectId, inputArguments, outputArguments, ct).ConfigureAwait(false); | ||
| if (GetAuditServer(context) is { Auditing: true } server) | ||
| { | ||
| server.ReportAuditMethodCallEvent( | ||
| context, | ||
| objectId, | ||
| method.NodeId, | ||
| inputArguments, | ||
| result, | ||
| static () => new KeyCredentialDeletedAuditEventState(null), | ||
| GetAuditLogger(context), | ||
| (auditContext, e) => e.SetChildValue(auditContext, BrowseNames.ResourceUri, resourceUri, false)); | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| private static IAuditEventServer? GetAuditServer(ISystemContext context) | ||
| { | ||
| return (context as ServerSystemContext)?.Server; | ||
| } | ||
|
|
||
| private static ILogger GetAuditLogger(ISystemContext context) | ||
| { | ||
| return context.Telemetry?.CreateLogger<KeyCredentialPushSubject>() | ||
| ?? NullLogger<KeyCredentialPushSubject>.Instance; | ||
| } | ||
|
|
||
| private static string GetResourceUri(MethodState method) | ||
| { | ||
| return (method.Parent as KeyCredentialConfigurationState)?.ResourceUri?.Value ?? string.Empty; | ||
| } | ||
| } | ||
| } | ||
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.