@@ -17,8 +17,6 @@ import { Subscription } from "rxjs/Subscription";
17
17
import { promisify } from "util" ;
18
18
import { Readable } from "stream" ;
19
19
import { URL } from "url" ;
20
- import path from "path" ;
21
- import envPath from "env-paths" ;
22
20
import {
23
21
ProtocolClient ,
24
22
Content ,
@@ -54,25 +52,23 @@ import {
54
52
getBuiltInDataType ,
55
53
readNamespaceArray ,
56
54
UserIdentityInfo ,
57
- UserIdentityInfoUserName ,
58
- UserIdentityInfoX509 ,
59
55
} from "node-opcua-pseudo-session" ;
60
56
import { makeNodeId , NodeId , NodeIdLike , NodeIdType , resolveNodeId } from "node-opcua-nodeid" ;
61
57
import { AttributeIds , BrowseDirection , makeResultMask } from "node-opcua-data-model" ;
62
58
import { makeBrowsePath } from "node-opcua-service-translate-browse-path" ;
63
59
import { StatusCodes } from "node-opcua-status-code" ;
64
- import { coercePrivateKeyPem , convertPEMtoDER , readPrivateKey } from "node-opcua-crypto" ;
60
+ import { coercePrivateKeyPem , readPrivateKey } from "node-opcua-crypto" ;
65
61
import { opcuaJsonEncodeVariant } from "node-opcua-json" ;
66
62
import { Argument , BrowseDescription , BrowseResult , MessageSecurityMode , UserTokenType } from "node-opcua-types" ;
67
- import { isGoodish2 , OPCUACertificateManager , ReferenceTypeIds } from "node-opcua" ;
63
+ import { isGoodish2 , ReferenceTypeIds } from "node-opcua" ;
68
64
69
65
import { schemaDataValue } from "./codec" ;
70
66
import { OPCUACAuthenticationScheme , OPCUAChannelSecurityScheme } from "./security_scheme" ;
67
+ import { CertificateManagerSingleton } from "./certificate-manager-singleton" ;
68
+ import { resolveChannelSecurity , resolvedUserIdentity } from "./opcua-security-resolver" ;
71
69
72
70
const { debug } = createLoggers ( "binding-opcua" , "opcua-protocol-client" ) ;
73
71
74
- const env = envPath ( "binding-opcua" , { suffix : "node-wot" } ) ;
75
-
76
72
export type Command = "Read" | "Write" | "Subscribe" ;
77
73
78
74
export interface NodeByBrowsePath {
@@ -167,32 +163,6 @@ export class OPCUAProtocolClient implements ProtocolClient {
167
163
private _securityPolicy : SecurityPolicy = SecurityPolicy . None ;
168
164
private _userIdentity : UserIdentityInfo = < AnonymousIdentity > { type : UserTokenType . Anonymous } ;
169
165
170
- private static _certificateManager : OPCUACertificateManager | null = null ;
171
-
172
- public static async getCertificateManager ( ) : Promise < OPCUACertificateManager > {
173
- if ( OPCUAProtocolClient . _certificateManager ) {
174
- return OPCUAProtocolClient . _certificateManager ;
175
- }
176
- const rootFolder = path . join ( env . config , "PKI" ) ;
177
- debug ( "OPCUA PKI folder" , rootFolder ) ;
178
- const certificateManager = new OPCUACertificateManager ( {
179
- rootFolder,
180
- } ) ;
181
- await certificateManager . initialize ( ) ;
182
- certificateManager . referenceCounter ++ ;
183
- OPCUAProtocolClient . _certificateManager = certificateManager ;
184
- return certificateManager ;
185
- }
186
-
187
- public static releaseCertificateManager ( ) : void {
188
- if ( OPCUAProtocolClient . _certificateManager ) {
189
- OPCUAProtocolClient . _certificateManager . referenceCounter -- ;
190
- // dispose is degined to free resources if referenceCounter==0;
191
- OPCUAProtocolClient . _certificateManager . dispose ( ) ;
192
- OPCUAProtocolClient . _certificateManager = null ;
193
- }
194
- }
195
-
196
166
private async _withConnection < T > ( form : OPCUAForm , next : ( connection : OPCUAConnection ) => Promise < T > ) : Promise < T > {
197
167
const endpoint = form . href ;
198
168
const matchesScheme : boolean = endpoint ?. match ( / ^ o p c .t c p : \/ \/ / ) != null ;
@@ -202,7 +172,7 @@ export class OPCUAProtocolClient implements ProtocolClient {
202
172
}
203
173
let c : OPCUAConnectionEx | undefined = this . _connections . get ( endpoint ) ;
204
174
if ( ! c ) {
205
- const clientCertificateManager = await OPCUAProtocolClient . getCertificateManager ( ) ;
175
+ const clientCertificateManager = await CertificateManagerSingleton . getCertificateManager ( ) ;
206
176
const client = OPCUAClient . create ( {
207
177
endpointMustExist : false ,
208
178
connectionStrategy : {
@@ -540,57 +510,18 @@ export class OPCUAProtocolClient implements ProtocolClient {
540
510
await connection . session . close ( ) ;
541
511
await connection . client . disconnect ( ) ;
542
512
}
543
- await OPCUAProtocolClient . _certificateManager ?. dispose ( ) ;
513
+ CertificateManagerSingleton . releaseCertificateManager ( ) ;
544
514
}
545
515
546
- private setChannelSecurity ( security : OPCUAChannelSecurityScheme ) : boolean {
547
- const foundSecurity = SecurityPolicy [ security . policy as keyof typeof SecurityPolicy ] ;
548
-
549
- if ( foundSecurity === undefined ) {
550
- return false ;
551
- }
552
-
553
- this . _securityPolicy = foundSecurity ;
554
-
555
- switch ( security . messageMode ) {
556
- case "sign" :
557
- this . _securityMode = MessageSecurityMode . Sign ;
558
- break ;
559
- case "sign_encrypt" :
560
- this . _securityMode = MessageSecurityMode . SignAndEncrypt ;
561
- break ;
562
- default :
563
- this . _securityMode = MessageSecurityMode . None ;
564
- break ;
565
- }
566
-
516
+ #setChannelSecurity( security : OPCUAChannelSecurityScheme ) : boolean {
517
+ const { messageSecurityMode, securityPolicy } = resolveChannelSecurity ( security ) ;
518
+ this . _securityMode = messageSecurityMode ;
519
+ this . _securityPolicy = securityPolicy ;
567
520
return true ;
568
521
}
569
522
570
- private setAuthentication ( security : OPCUACAuthenticationScheme ) : boolean {
571
- switch ( security . tokenType ) {
572
- case "username" :
573
- this . _userIdentity = < UserIdentityInfoUserName > {
574
- type : UserTokenType . UserName ,
575
- password : security . password ,
576
- userName : security . userName ,
577
- } ;
578
- break ;
579
- case "certificate" :
580
- this . _userIdentity = < UserIdentityInfoX509 > {
581
- type : UserTokenType . Certificate ,
582
- certificateData : convertPEMtoDER ( security . certificate ) ,
583
- privateKey : security . privateKey ,
584
- } ;
585
- break ;
586
- case "anonymous" :
587
- this . _userIdentity = < UserIdentityInfo > {
588
- type : UserTokenType . Anonymous ,
589
- } ;
590
- break ;
591
- default :
592
- return false ;
593
- }
523
+ #setAuthentication( security : OPCUACAuthenticationScheme ) : boolean {
524
+ this . _userIdentity = resolvedUserIdentity ( security ) ;
594
525
return true ;
595
526
}
596
527
@@ -599,10 +530,10 @@ export class OPCUAProtocolClient implements ProtocolClient {
599
530
let success = true ;
600
531
switch ( securityScheme . scheme ) {
601
532
case "uav:channel-security" :
602
- success = this . setChannelSecurity ( securityScheme as OPCUAChannelSecurityScheme ) ;
533
+ success = this . # setChannelSecurity( securityScheme as OPCUAChannelSecurityScheme ) ;
603
534
break ;
604
535
case "uav:authentication" :
605
- success = this . setAuthentication ( securityScheme as OPCUACAuthenticationScheme ) ;
536
+ success = this . # setAuthentication( securityScheme as OPCUACAuthenticationScheme ) ;
606
537
break ;
607
538
case "combo" : {
608
539
const combo = securityScheme as AllOfSecurityScheme | OneOfSecurityScheme ;
0 commit comments