-
Notifications
You must be signed in to change notification settings - Fork 95
feat(binding-opcua): Add channel security support #1401 #1415
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
relu91
merged 9 commits into
eclipse-thingweb:master
from
node-opcua:feat/binding_opcua_set_security_1401
Sep 22, 2025
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
8ae1c34
feat(binding-opcua): Add channel security support #1401
erossignon 7a2546e
feat(binding-opcua): add method to reflect on current connected user …
erossignon 8d979c4
feat(binding-opcua): improve channel security test
erossignon 82b0299
chore(binding-opcua): relax settings in .mocharc.yml
erossignon 8158bb8
chore(binding-opcua) #1401 use uav: prefix for new opcua security sch…
erossignon 250be6e
chore(core): #1401 use uav: prefix for new opcua security schemes use…
erossignon 72f1da1
feat(binding-opcua): move OPCUA security schema to binding-opcua #1401
erossignon 17f9447
chore(core): make the tests about the combo scheme generic.
erossignon 269ea39
refactor(binding-opcua): move cert mgt and security resolution
erossignon 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
54 changes: 54 additions & 0 deletions
54
packages/binding-opcua/src/certificate-manager-singleton.ts
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,54 @@ | ||
| /******************************************************************************** | ||
| * Copyright (c) 2025 Contributors to the Eclipse Foundation | ||
| * | ||
| * See the NOTICE file(s) distributed with this work for additional | ||
| * information regarding copyright ownership. | ||
| * | ||
| * This program and the accompanying materials are made available under the | ||
| * terms of the Eclipse Public License v. 2.0 which is available at | ||
| * http://www.eclipse.org/legal/epl-2.0, or the W3C Software Notice and | ||
| * Document License (2015-05-13) which is available at | ||
| * https://www.w3.org/Consortium/Legal/2015/copyright-software-and-document. | ||
| * | ||
| * SPDX-License-Identifier: EPL-2.0 OR W3C-20150513 | ||
| ********************************************************************************/ | ||
| import path from "node:path"; | ||
| import { OPCUACertificateManager } from "node-opcua-certificate-manager"; | ||
| import envPath from "env-paths"; | ||
| import { createLoggers } from "@node-wot/core"; | ||
|
|
||
| const { debug } = createLoggers("binding-opcua", "opcua-protocol-client"); | ||
|
|
||
| const env = envPath("binding-opcua", { suffix: "node-wot" }); | ||
|
|
||
| /** | ||
| * Certificate Manager Singleton for OPCUA Binding in the WoT context. | ||
| * | ||
| */ | ||
| export class CertificateManagerSingleton { | ||
| private static _certificateManager: OPCUACertificateManager | null = null; | ||
|
|
||
| public static async getCertificateManager(): Promise<OPCUACertificateManager> { | ||
| if (CertificateManagerSingleton._certificateManager) { | ||
| return CertificateManagerSingleton._certificateManager; | ||
| } | ||
| const rootFolder = path.join(env.config, "PKI"); | ||
| debug("OPCUA PKI folder", rootFolder); | ||
| const certificateManager = new OPCUACertificateManager({ | ||
| rootFolder, | ||
| }); | ||
| await certificateManager.initialize(); | ||
| certificateManager.referenceCounter++; | ||
| CertificateManagerSingleton._certificateManager = certificateManager; | ||
| return certificateManager; | ||
| } | ||
|
|
||
| public static releaseCertificateManager(): void { | ||
| if (CertificateManagerSingleton._certificateManager) { | ||
| CertificateManagerSingleton._certificateManager.referenceCounter--; | ||
| // dispose is degined to free resources if referenceCounter==0; | ||
| CertificateManagerSingleton._certificateManager.dispose(); | ||
| CertificateManagerSingleton._certificateManager = null; | ||
| } | ||
| } | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| /******************************************************************************** | ||
| * Copyright (c) 2025 Contributors to the Eclipse Foundation | ||
| * | ||
| * See the NOTICE file(s) distributed with this work for additional | ||
| * information regarding copyright ownership. | ||
| * | ||
| * This program and the accompanying materials are made available under the | ||
| * terms of the Eclipse Public License v. 2.0 which is available at | ||
| * http://www.eclipse.org/legal/epl-2.0, or the W3C Software Notice and | ||
| * Document License (2015-05-13) which is available at | ||
| * https://www.w3.org/Consortium/Legal/2015/copyright-software-and-document. | ||
| * | ||
| * SPDX-License-Identifier: EPL-2.0 OR W3C-20150513 | ||
| ********************************************************************************/ | ||
| import { | ||
| MessageSecurityMode, | ||
| SecurityPolicy, | ||
| UserIdentityInfo, | ||
| UserIdentityInfoUserName, | ||
| UserIdentityInfoX509, | ||
| UserTokenType, | ||
| } from "node-opcua-client"; | ||
| import { convertPEMtoDER } from "node-opcua-crypto"; | ||
| import { OPCUACAuthenticationScheme, OPCUAChannelSecurityScheme } from "./security_scheme"; | ||
|
|
||
| export interface OPCUAChannelSecuritySettings { | ||
| securityPolicy: SecurityPolicy; | ||
| messageSecurityMode: MessageSecurityMode; | ||
| } | ||
|
|
||
| /** | ||
| * Resolves the channel security settings from the given security scheme. | ||
| * Will throw an error if the policy or message mode is invalid. | ||
| * @param security The OPC UA channel security scheme. | ||
| * @returns The resolved channel security settings. | ||
| */ | ||
| export function resolveChannelSecurity(security: OPCUAChannelSecurityScheme): OPCUAChannelSecuritySettings { | ||
| if (security.scheme === "uav:channel-security" && security.messageMode !== "none") { | ||
| const securityPolicy: SecurityPolicy = SecurityPolicy[security.policy as keyof typeof SecurityPolicy]; | ||
|
|
||
| if (securityPolicy === undefined) { | ||
| throw new Error(`Invalid security policy '${security.policy}'`); | ||
| } | ||
|
|
||
| let messageSecurityMode: MessageSecurityMode = MessageSecurityMode.Invalid; | ||
| switch (security.messageMode) { | ||
| case "sign": | ||
| messageSecurityMode = MessageSecurityMode.Sign; | ||
| break; | ||
| case "sign_encrypt": | ||
| messageSecurityMode = MessageSecurityMode.SignAndEncrypt; | ||
| break; | ||
| default: | ||
| messageSecurityMode = MessageSecurityMode.None; | ||
| break; | ||
| } | ||
|
|
||
| return { | ||
| securityPolicy, | ||
| messageSecurityMode, | ||
| }; | ||
| } else { | ||
| return { | ||
| securityPolicy: SecurityPolicy.None, | ||
| messageSecurityMode: MessageSecurityMode.None, | ||
| }; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Resolves the user identity information from the given authentication scheme. | ||
| * Will throw an error if the token type is invalid. | ||
| * @param security The OPC UA authentication scheme. | ||
| * @returns The resolved user identity information. | ||
| */ | ||
| export function resolvedUserIdentity(security: OPCUACAuthenticationScheme) { | ||
| let userIdentity: UserIdentityInfo; | ||
| switch (security.tokenType) { | ||
| case "username": | ||
| userIdentity = <UserIdentityInfoUserName>{ | ||
| type: UserTokenType.UserName, | ||
| password: security.password, | ||
| userName: security.userName, | ||
| }; | ||
| break; | ||
| case "certificate": | ||
| userIdentity = <UserIdentityInfoX509>{ | ||
| type: UserTokenType.Certificate, | ||
| certificateData: convertPEMtoDER(security.certificate), | ||
| privateKey: security.privateKey, | ||
| }; | ||
| break; | ||
| case "anonymous": | ||
| default: | ||
| // it is OK to use anonymous as default, | ||
| // as it provides the lowest privileges | ||
| userIdentity = <UserIdentityInfo>{ | ||
| type: UserTokenType.Anonymous, | ||
| }; | ||
| break; | ||
| } | ||
|
|
||
| return userIdentity; | ||
| } |
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.