node-opcua-role-set-common 2.174.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +22 -0
- package/README.md +142 -0
- package/dist/archive.d.ts +71 -0
- package/dist/archive.js +189 -0
- package/dist/archive.js.map +1 -0
- package/dist/binary_persistence.d.ts +31 -0
- package/dist/binary_persistence.js +119 -0
- package/dist/binary_persistence.js.map +1 -0
- package/dist/identity_mapping_store.d.ts +36 -0
- package/dist/identity_mapping_store.js +3 -0
- package/dist/identity_mapping_store.js.map +1 -0
- package/dist/in_memory_store.d.ts +20 -0
- package/dist/in_memory_store.js +129 -0
- package/dist/in_memory_store.js.map +1 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.js +41 -0
- package/dist/index.js.map +1 -0
- package/dist/role_restriction_store.d.ts +93 -0
- package/dist/role_restriction_store.js +205 -0
- package/dist/role_restriction_store.js.map +1 -0
- package/dist/user_management_store.d.ts +83 -0
- package/dist/user_management_store.js +208 -0
- package/dist/user_management_store.js.map +1 -0
- package/dist/well_known_role_ids.d.ts +36 -0
- package/dist/well_known_role_ids.js +44 -0
- package/dist/well_known_role_ids.js.map +1 -0
- package/dist/x509_subject.d.ts +44 -0
- package/dist/x509_subject.js +78 -0
- package/dist/x509_subject.js.map +1 -0
- package/package.json +51 -0
- package/source/archive.ts +231 -0
- package/source/binary_persistence.ts +119 -0
- package/source/identity_mapping_store.ts +42 -0
- package/source/in_memory_store.ts +150 -0
- package/source/index.ts +55 -0
- package/source/role_restriction_store.ts +270 -0
- package/source/user_management_store.ts +288 -0
- package/source/well_known_role_ids.ts +42 -0
- package/source/x509_subject.ts +84 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022-2024 Sterfive SAS - 833264583 RCS ORLEANS - France (https://www.sterfive.com)
|
|
4
|
+
|
|
5
|
+
Copyright (c) 2014-2022 Etienne Rossignon
|
|
6
|
+
|
|
7
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
8
|
+
this software and associated documentation files (the "Software"), to deal in
|
|
9
|
+
the Software without restriction, including without limitation the rights to
|
|
10
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
|
11
|
+
the Software, and to permit persons to whom the Software is furnished to do so,
|
|
12
|
+
subject to the following conditions:
|
|
13
|
+
|
|
14
|
+
The above copyright notice and this permission notice shall be included in all
|
|
15
|
+
copies or substantial portions of the Software.
|
|
16
|
+
|
|
17
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
18
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
|
19
|
+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
|
20
|
+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
|
21
|
+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
22
|
+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
# node-opcua-role-set-common
|
|
2
|
+
|
|
3
|
+
Shared types, identity stores and persistence helpers for OPC UA **RoleSet management**
|
|
4
|
+
(OPC 10000-18 — *Role-Based Security*).
|
|
5
|
+
|
|
6
|
+
This package holds the framework-agnostic building blocks used by both the
|
|
7
|
+
server (`node-opcua-role-set-server`) and the client (`node-opcua-role-set-client`)
|
|
8
|
+
role-set packages. It deliberately avoids depending on the heavy
|
|
9
|
+
`node-opcua-address-space` so it can be reused anywhere a `NodeId` and the
|
|
10
|
+
core OPC UA types are available.
|
|
11
|
+
|
|
12
|
+
see http://node-opcua.github.io/
|
|
13
|
+
|
|
14
|
+
## Installation
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
$ npm install node-opcua-role-set-common
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## What's in the box
|
|
21
|
+
|
|
22
|
+
| Export | Description |
|
|
23
|
+
|--------|-------------|
|
|
24
|
+
| `IIdentityMappingStore` | Interface for a store of identity → role mappings (OPC 10000-18 §4.4). |
|
|
25
|
+
| `InMemoryIdentityMappingStore` | In-memory implementation of `IIdentityMappingStore`. |
|
|
26
|
+
| `AnyUserIdentityToken` | Union of `AnonymousIdentityToken \| UserNameIdentityToken \| X509IdentityToken`. |
|
|
27
|
+
| `WellKnownRoles` | Numeric identifiers of the standard OPC UA roles (re-exported from `node-opcua-constants`). |
|
|
28
|
+
| `WellKnownRoleIds` | Pre-built `NodeId` objects for each well-known role. |
|
|
29
|
+
| `saveToBinaryFile` / `loadFromBinaryFile` | Persist / restore a store to / from a binary file. |
|
|
30
|
+
| `encodeIdentityStore` / `decodeIdentityStore` / `identityStoreBinaryStoreSize` | Lower-level binary (de)serialization helpers. |
|
|
31
|
+
|
|
32
|
+
## The identity mapping store
|
|
33
|
+
|
|
34
|
+
A store maps an `IdentityMappingRuleType` (OPC 10000-18 §4.4) to a role `NodeId`,
|
|
35
|
+
and can resolve which roles a given user identity token is granted.
|
|
36
|
+
|
|
37
|
+
```ts
|
|
38
|
+
import {
|
|
39
|
+
InMemoryIdentityMappingStore,
|
|
40
|
+
WellKnownRoleIds
|
|
41
|
+
} from "node-opcua-role-set-common";
|
|
42
|
+
import { IdentityCriteriaType, IdentityMappingRuleType, UserNameIdentityToken } from "node-opcua-types";
|
|
43
|
+
|
|
44
|
+
const store = new InMemoryIdentityMappingStore();
|
|
45
|
+
|
|
46
|
+
// grant the SecurityAdmin role to the user "admin"
|
|
47
|
+
store.addIdentity(
|
|
48
|
+
WellKnownRoleIds.SecurityAdmin,
|
|
49
|
+
new IdentityMappingRuleType({
|
|
50
|
+
criteriaType: IdentityCriteriaType.UserName,
|
|
51
|
+
criteria: "admin"
|
|
52
|
+
})
|
|
53
|
+
);
|
|
54
|
+
|
|
55
|
+
// resolve roles for an incoming user identity token
|
|
56
|
+
const roles = store.resolveRoles(new UserNameIdentityToken({ userName: "admin" }));
|
|
57
|
+
// -> [ NodeId(SecurityAdmin) ]
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
### Supported criteria types
|
|
61
|
+
|
|
62
|
+
`resolveRoles` understands the following `IdentityCriteriaType` values:
|
|
63
|
+
|
|
64
|
+
| Criteria | Matches when |
|
|
65
|
+
|----------|--------------|
|
|
66
|
+
| `Anonymous` | the token is an `AnonymousIdentityToken`. |
|
|
67
|
+
| `AuthenticatedUser` | the token is *not* anonymous. |
|
|
68
|
+
| `UserName` | a `UserNameIdentityToken` whose `userName` equals `criteria`. |
|
|
69
|
+
| `Thumbprint` | an `X509IdentityToken` whose SHA-1 certificate thumbprint equals `criteria`. |
|
|
70
|
+
| `X509Subject` | an `X509IdentityToken` whose certificate subject matches `criteria`. |
|
|
71
|
+
|
|
72
|
+
> **`X509Subject` matching** accepts two formats:
|
|
73
|
+
> - the ordered Distinguished Name format from OPC 10000-18 §4.4.3 (Table 10),
|
|
74
|
+
> e.g. `CN="Jane Doe"/O="ACME"` — every mappable subject attribute present in
|
|
75
|
+
> the certificate (CN, O, OU, L, S, C) must also be present in the criteria;
|
|
76
|
+
> - a plain Common Name string (legacy), compared against the certificate's `commonName` only.
|
|
77
|
+
>
|
|
78
|
+
> The helpers (`matchX509Subject`, `canonicalizeX509Subject`, `parseX509SubjectCriteria`,
|
|
79
|
+
> `certificateSubjectPairs`) are exported for reuse. `DC`, `dnQualifier` and `serialNumber`
|
|
80
|
+
> are not extracted from certificates and are ignored.
|
|
81
|
+
|
|
82
|
+
### Well-known roles
|
|
83
|
+
|
|
84
|
+
`WellKnownRoleIds` saves you from repeatedly calling `resolveNodeId(...)`:
|
|
85
|
+
|
|
86
|
+
```ts
|
|
87
|
+
import { WellKnownRoleIds } from "node-opcua-role-set-common";
|
|
88
|
+
|
|
89
|
+
WellKnownRoleIds.Anonymous;
|
|
90
|
+
WellKnownRoleIds.AuthenticatedUser;
|
|
91
|
+
WellKnownRoleIds.Observer;
|
|
92
|
+
WellKnownRoleIds.Operator;
|
|
93
|
+
WellKnownRoleIds.Engineer;
|
|
94
|
+
WellKnownRoleIds.Supervisor;
|
|
95
|
+
WellKnownRoleIds.ConfigureAdmin;
|
|
96
|
+
WellKnownRoleIds.SecurityAdmin;
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
## Persistence
|
|
100
|
+
|
|
101
|
+
A store can be serialized to a compact binary file using OPC UA binary encoding:
|
|
102
|
+
|
|
103
|
+
```ts
|
|
104
|
+
import { saveToBinaryFile, loadFromBinaryFile } from "node-opcua-role-set-common";
|
|
105
|
+
|
|
106
|
+
await saveToBinaryFile(store, "./config/roles.bin");
|
|
107
|
+
|
|
108
|
+
// later, possibly after a restart
|
|
109
|
+
await loadFromBinaryFile(store, "./config/roles.bin");
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
- `saveToBinaryFile` creates the parent directory if needed.
|
|
113
|
+
- `loadFromBinaryFile` is a no-op when the file is missing or empty.
|
|
114
|
+
- `decodeIdentityStore` **merges** into the existing store (it does not clear it first).
|
|
115
|
+
|
|
116
|
+
### File format
|
|
117
|
+
|
|
118
|
+
```
|
|
119
|
+
UInt32 number of roles
|
|
120
|
+
repeat for each role:
|
|
121
|
+
NodeId role NodeId
|
|
122
|
+
Variant(ExtensionObject[]) identity mapping rules
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
## Related packages
|
|
126
|
+
|
|
127
|
+
- [`node-opcua-role-set-server`](../node-opcua-role-set-server) — installs the RoleSet & User Management Methods on a server.
|
|
128
|
+
- [`node-opcua-role-set-client`](../node-opcua-role-set-client) — client-side helpers to browse roles, read/change identities and manage users.
|
|
129
|
+
- [`node-opcua-role-set-admin`](../node-opcua-role-set-admin) — the `role-set-admin` command-line tool.
|
|
130
|
+
- [Role-Based Security & User Management guide](https://github.com/node-opcua/node-opcua/blob/master/documentation/role_based_security.md) — cross-package overview & getting started.
|
|
131
|
+
|
|
132
|
+
# License
|
|
133
|
+
|
|
134
|
+
Node-OPCUA is made available to you under the MIT open source license.
|
|
135
|
+
|
|
136
|
+
See [LICENSE](./LICENSE) for details.
|
|
137
|
+
|
|
138
|
+
# Copyright
|
|
139
|
+
|
|
140
|
+
Copyright (c) 2022-2026 Sterfive SAS - https://www.sterfive.com
|
|
141
|
+
|
|
142
|
+
Copyright (c) 2014-2022 Etienne Rossignon
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import type { IIdentityMappingStore } from "./identity_mapping_store.js";
|
|
2
|
+
import type { SerializedRoleRestriction } from "./role_restriction_store.js";
|
|
3
|
+
import type { SerializedUserRecord } from "./user_management_store.js";
|
|
4
|
+
export declare const ROLE_SET_ARCHIVE_VERSION = 1;
|
|
5
|
+
/** Persisted definition of a custom Role (so its GUID NodeId survives a restart). */
|
|
6
|
+
export interface PersistedCustomRole {
|
|
7
|
+
nodeId: string;
|
|
8
|
+
roleName: string;
|
|
9
|
+
namespaceUri?: string;
|
|
10
|
+
}
|
|
11
|
+
/** The whole RoleSet configuration, ready to be serialized to one file. */
|
|
12
|
+
export interface RoleSetArchive {
|
|
13
|
+
version: number;
|
|
14
|
+
/** base64 of the OPC UA binary identity-mapping store. */
|
|
15
|
+
identities?: string;
|
|
16
|
+
roles?: PersistedCustomRole[];
|
|
17
|
+
restrictions?: SerializedRoleRestriction[];
|
|
18
|
+
/** UserManagement users with salted scrypt hashes (never clear passwords). */
|
|
19
|
+
users?: SerializedUserRecord[];
|
|
20
|
+
}
|
|
21
|
+
/** Encode the identity store as a base64 string (reuses the binary encoder). */
|
|
22
|
+
export declare function identitiesToBase64(store: IIdentityMappingStore): string;
|
|
23
|
+
/** Decode a base64 identity blob back into the store (merges; empty → no-op). */
|
|
24
|
+
export declare function identitiesFromBase64(store: IIdentityMappingStore, base64: string | undefined): void;
|
|
25
|
+
export interface ArchiveIO {
|
|
26
|
+
/** When set, the archive is encrypted at rest (AES-256-GCM, key derived from this secret via scrypt). */
|
|
27
|
+
secret?: string;
|
|
28
|
+
}
|
|
29
|
+
/** Atomically write the archive (optionally encrypted) to `filePath`. */
|
|
30
|
+
export declare function writeArchive(filePath: string, archive: RoleSetArchive, io?: ArchiveIO): Promise<void>;
|
|
31
|
+
/** Read the archive (decrypting if needed). Missing/empty file → undefined. */
|
|
32
|
+
export declare function readArchive(filePath: string, io?: ArchiveIO): Promise<RoleSetArchive | undefined>;
|
|
33
|
+
/** A snapshot provider for one archive section (returns the current state, or undefined to omit). */
|
|
34
|
+
type SectionProvider<T> = () => T | undefined;
|
|
35
|
+
/**
|
|
36
|
+
* Coordinates a single consolidated archive shared by several stores.
|
|
37
|
+
*
|
|
38
|
+
* `installRoleSet` and `installUserManagement` each register the section(s) they
|
|
39
|
+
* own (identities/roles/restrictions vs. users) and call {@link save} after a
|
|
40
|
+
* mutation; the coordinator gathers **all** registered sections and rewrites the
|
|
41
|
+
* one file atomically — so neither installer clobbers the other's data. Pass the
|
|
42
|
+
* same `ArchiveStore` instance to both to keep everything in one file.
|
|
43
|
+
*
|
|
44
|
+
* A section with **no** registered provider is not dropped: its last loaded value
|
|
45
|
+
* is preserved on save. This makes the result independent of install order — even
|
|
46
|
+
* a mutation between the two installs cannot lose the not-yet-registered sections.
|
|
47
|
+
*/
|
|
48
|
+
export declare class ArchiveStore {
|
|
49
|
+
readonly filePath: string;
|
|
50
|
+
private readonly _io;
|
|
51
|
+
private _identities?;
|
|
52
|
+
private _roles?;
|
|
53
|
+
private _restrictions?;
|
|
54
|
+
private _users?;
|
|
55
|
+
/** Last archive read from disk — used to preserve sections that have no provider yet. */
|
|
56
|
+
private _loaded?;
|
|
57
|
+
constructor(filePath: string, io?: ArchiveIO);
|
|
58
|
+
setIdentitiesProvider(fn: SectionProvider<string>): void;
|
|
59
|
+
setRolesProvider(fn: SectionProvider<PersistedCustomRole[]>): void;
|
|
60
|
+
setRestrictionsProvider(fn: SectionProvider<SerializedRoleRestriction[]>): void;
|
|
61
|
+
setUsersProvider(fn: SectionProvider<SerializedUserRecord[]>): void;
|
|
62
|
+
/** Read the persisted archive (missing file → undefined) and remember it for {@link save}. */
|
|
63
|
+
load(): Promise<RoleSetArchive | undefined>;
|
|
64
|
+
/**
|
|
65
|
+
* Atomically rewrite the archive. A registered provider is authoritative for
|
|
66
|
+
* its section; a section with no provider falls back to the last loaded value
|
|
67
|
+
* so it is never clobbered (install-order independence).
|
|
68
|
+
*/
|
|
69
|
+
save(): Promise<void>;
|
|
70
|
+
}
|
|
71
|
+
export {};
|
package/dist/archive.js
ADDED
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ArchiveStore = exports.ROLE_SET_ARCHIVE_VERSION = void 0;
|
|
4
|
+
exports.identitiesToBase64 = identitiesToBase64;
|
|
5
|
+
exports.identitiesFromBase64 = identitiesFromBase64;
|
|
6
|
+
exports.writeArchive = writeArchive;
|
|
7
|
+
exports.readArchive = readArchive;
|
|
8
|
+
/**
|
|
9
|
+
* @module node-opcua-role-set-common
|
|
10
|
+
*
|
|
11
|
+
* Consolidated, single-file persistence archive for the whole RoleSet
|
|
12
|
+
* configuration (identity mappings, custom Role definitions, application/
|
|
13
|
+
* endpoint restrictions — and, in the future, the UserManagement store).
|
|
14
|
+
*
|
|
15
|
+
* The archive is JSON so most of it stays human-readable and diffable, while
|
|
16
|
+
* the identity mappings — which are OPC UA `ExtensionObject`s with a canonical
|
|
17
|
+
* binary encoding — are embedded as a base64 blob (reusing the proven binary
|
|
18
|
+
* encoder instead of re-inventing a lossy JSON form).
|
|
19
|
+
*
|
|
20
|
+
* Writes are **atomic** (temp file + rename) so a crash can never leave a
|
|
21
|
+
* half-written archive. When an operator-supplied `secret` is given the whole
|
|
22
|
+
* payload is encrypted at rest with AES-256-GCM (key derived via scrypt); the
|
|
23
|
+
* envelope is itself JSON with base64 fields. Without a secret the archive is
|
|
24
|
+
* plain JSON — relying on filesystem permissions / OS-level encryption, which
|
|
25
|
+
* is the appropriate baseline for config that already stores only salted,
|
|
26
|
+
* one-way password hashes.
|
|
27
|
+
*/
|
|
28
|
+
const node_crypto_1 = require("node:crypto");
|
|
29
|
+
const node_fs_1 = require("node:fs");
|
|
30
|
+
const node_path_1 = require("node:path");
|
|
31
|
+
const node_opcua_binary_stream_1 = require("node-opcua-binary-stream");
|
|
32
|
+
const binary_persistence_js_1 = require("./binary_persistence.js");
|
|
33
|
+
exports.ROLE_SET_ARCHIVE_VERSION = 1;
|
|
34
|
+
/** Encode the identity store as a base64 string (reuses the binary encoder). */
|
|
35
|
+
function identitiesToBase64(store) {
|
|
36
|
+
const size = (0, binary_persistence_js_1.identityStoreBinaryStoreSize)(store);
|
|
37
|
+
const stream = new node_opcua_binary_stream_1.BinaryStream(Buffer.alloc(size));
|
|
38
|
+
(0, binary_persistence_js_1.encodeIdentityStore)(store, stream);
|
|
39
|
+
return stream.buffer.subarray(0, stream.length).toString("base64");
|
|
40
|
+
}
|
|
41
|
+
/** Decode a base64 identity blob back into the store (merges; empty → no-op). */
|
|
42
|
+
function identitiesFromBase64(store, base64) {
|
|
43
|
+
if (!base64)
|
|
44
|
+
return;
|
|
45
|
+
const buffer = Buffer.from(base64, "base64");
|
|
46
|
+
if (buffer.length === 0)
|
|
47
|
+
return;
|
|
48
|
+
(0, binary_persistence_js_1.decodeIdentityStore)(store, new node_opcua_binary_stream_1.BinaryStream(buffer));
|
|
49
|
+
}
|
|
50
|
+
function encrypt(plaintext, secret) {
|
|
51
|
+
const salt = (0, node_crypto_1.randomBytes)(16);
|
|
52
|
+
const iv = (0, node_crypto_1.randomBytes)(12);
|
|
53
|
+
const key = (0, node_crypto_1.scryptSync)(secret, salt, 32);
|
|
54
|
+
const cipher = (0, node_crypto_1.createCipheriv)("aes-256-gcm", key, iv);
|
|
55
|
+
const enc = Buffer.concat([cipher.update(plaintext, "utf8"), cipher.final()]);
|
|
56
|
+
return {
|
|
57
|
+
version: exports.ROLE_SET_ARCHIVE_VERSION,
|
|
58
|
+
encrypted: true,
|
|
59
|
+
kdf: "scrypt",
|
|
60
|
+
salt: salt.toString("base64"),
|
|
61
|
+
iv: iv.toString("base64"),
|
|
62
|
+
tag: cipher.getAuthTag().toString("base64"),
|
|
63
|
+
data: enc.toString("base64")
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
function decrypt(env, secret) {
|
|
67
|
+
const key = (0, node_crypto_1.scryptSync)(secret, Buffer.from(env.salt, "base64"), 32);
|
|
68
|
+
const decipher = (0, node_crypto_1.createDecipheriv)("aes-256-gcm", key, Buffer.from(env.iv, "base64"));
|
|
69
|
+
decipher.setAuthTag(Buffer.from(env.tag, "base64"));
|
|
70
|
+
return Buffer.concat([decipher.update(Buffer.from(env.data, "base64")), decipher.final()]).toString("utf8");
|
|
71
|
+
}
|
|
72
|
+
/** Atomically write the archive (optionally encrypted) to `filePath`. */
|
|
73
|
+
async function writeArchive(filePath, archive, io) {
|
|
74
|
+
const json = JSON.stringify(archive, null, 2);
|
|
75
|
+
const payload = io?.secret ? JSON.stringify(encrypt(json, io.secret), null, 2) : json;
|
|
76
|
+
await node_fs_1.promises.mkdir((0, node_path_1.dirname)(filePath), { recursive: true });
|
|
77
|
+
// write to a temp file then rename — rename is atomic and replaces the target
|
|
78
|
+
// (libuv sets MOVEFILE_REPLACE_EXISTING on Windows), so readers never see a
|
|
79
|
+
// partially written archive.
|
|
80
|
+
const tmp = `${filePath}.tmp`;
|
|
81
|
+
await node_fs_1.promises.writeFile(tmp, payload, "utf8");
|
|
82
|
+
await node_fs_1.promises.rename(tmp, filePath);
|
|
83
|
+
}
|
|
84
|
+
/** Read the archive (decrypting if needed). Missing/empty file → undefined. */
|
|
85
|
+
async function readArchive(filePath, io) {
|
|
86
|
+
let text;
|
|
87
|
+
try {
|
|
88
|
+
text = await node_fs_1.promises.readFile(filePath, "utf8");
|
|
89
|
+
}
|
|
90
|
+
catch (err) {
|
|
91
|
+
if (err.code === "ENOENT")
|
|
92
|
+
return undefined;
|
|
93
|
+
throw err;
|
|
94
|
+
}
|
|
95
|
+
if (!text.trim())
|
|
96
|
+
return undefined;
|
|
97
|
+
let parsed;
|
|
98
|
+
try {
|
|
99
|
+
parsed = JSON.parse(text);
|
|
100
|
+
}
|
|
101
|
+
catch (err) {
|
|
102
|
+
throw new Error(`readArchive: '${filePath}' is not a valid archive (JSON parse failed): ${err.message}`);
|
|
103
|
+
}
|
|
104
|
+
if (parsed.encrypted) {
|
|
105
|
+
if (!io?.secret) {
|
|
106
|
+
throw new Error(`readArchive: '${filePath}' is encrypted but no secret was provided`);
|
|
107
|
+
}
|
|
108
|
+
try {
|
|
109
|
+
parsed = JSON.parse(decrypt(parsed, io.secret));
|
|
110
|
+
}
|
|
111
|
+
catch (err) {
|
|
112
|
+
throw new Error(`readArchive: cannot decrypt '${filePath}' (wrong secret or corrupt): ${err.message}`);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
const result = parsed;
|
|
116
|
+
// Future-proofing: refuse an archive written by a newer, incompatible format
|
|
117
|
+
// rather than silently dropping the fields we don't understand. Older minor
|
|
118
|
+
// versions are forward-compatible (unknown fields are simply ignored).
|
|
119
|
+
if (typeof result.version !== "number") {
|
|
120
|
+
throw new Error(`readArchive: '${filePath}' has no version field — not a RoleSet archive`);
|
|
121
|
+
}
|
|
122
|
+
if (result.version > exports.ROLE_SET_ARCHIVE_VERSION) {
|
|
123
|
+
throw new Error(`readArchive: '${filePath}' was written by a newer archive format (version ${result.version} > ${exports.ROLE_SET_ARCHIVE_VERSION}); upgrade node-opcua to read it`);
|
|
124
|
+
}
|
|
125
|
+
return result;
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Coordinates a single consolidated archive shared by several stores.
|
|
129
|
+
*
|
|
130
|
+
* `installRoleSet` and `installUserManagement` each register the section(s) they
|
|
131
|
+
* own (identities/roles/restrictions vs. users) and call {@link save} after a
|
|
132
|
+
* mutation; the coordinator gathers **all** registered sections and rewrites the
|
|
133
|
+
* one file atomically — so neither installer clobbers the other's data. Pass the
|
|
134
|
+
* same `ArchiveStore` instance to both to keep everything in one file.
|
|
135
|
+
*
|
|
136
|
+
* A section with **no** registered provider is not dropped: its last loaded value
|
|
137
|
+
* is preserved on save. This makes the result independent of install order — even
|
|
138
|
+
* a mutation between the two installs cannot lose the not-yet-registered sections.
|
|
139
|
+
*/
|
|
140
|
+
class ArchiveStore {
|
|
141
|
+
filePath;
|
|
142
|
+
_io;
|
|
143
|
+
_identities;
|
|
144
|
+
_roles;
|
|
145
|
+
_restrictions;
|
|
146
|
+
_users;
|
|
147
|
+
/** Last archive read from disk — used to preserve sections that have no provider yet. */
|
|
148
|
+
_loaded;
|
|
149
|
+
constructor(filePath, io) {
|
|
150
|
+
this.filePath = filePath;
|
|
151
|
+
this._io = io ?? {};
|
|
152
|
+
}
|
|
153
|
+
setIdentitiesProvider(fn) {
|
|
154
|
+
this._identities = fn;
|
|
155
|
+
}
|
|
156
|
+
setRolesProvider(fn) {
|
|
157
|
+
this._roles = fn;
|
|
158
|
+
}
|
|
159
|
+
setRestrictionsProvider(fn) {
|
|
160
|
+
this._restrictions = fn;
|
|
161
|
+
}
|
|
162
|
+
setUsersProvider(fn) {
|
|
163
|
+
this._users = fn;
|
|
164
|
+
}
|
|
165
|
+
/** Read the persisted archive (missing file → undefined) and remember it for {@link save}. */
|
|
166
|
+
async load() {
|
|
167
|
+
this._loaded = await readArchive(this.filePath, this._io);
|
|
168
|
+
return this._loaded;
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* Atomically rewrite the archive. A registered provider is authoritative for
|
|
172
|
+
* its section; a section with no provider falls back to the last loaded value
|
|
173
|
+
* so it is never clobbered (install-order independence).
|
|
174
|
+
*/
|
|
175
|
+
async save() {
|
|
176
|
+
const archive = {
|
|
177
|
+
version: exports.ROLE_SET_ARCHIVE_VERSION,
|
|
178
|
+
identities: this._identities ? this._identities() : this._loaded?.identities,
|
|
179
|
+
roles: this._roles ? this._roles() : this._loaded?.roles,
|
|
180
|
+
restrictions: this._restrictions ? this._restrictions() : this._loaded?.restrictions,
|
|
181
|
+
users: this._users ? this._users() : this._loaded?.users
|
|
182
|
+
};
|
|
183
|
+
await writeArchive(this.filePath, archive, this._io);
|
|
184
|
+
// keep the fallback snapshot in sync with what is now on disk
|
|
185
|
+
this._loaded = archive;
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
exports.ArchiveStore = ArchiveStore;
|
|
189
|
+
//# sourceMappingURL=archive.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"archive.js","sourceRoot":"","sources":["../source/archive.ts"],"names":[],"mappings":";;;AAkDA,gDAKC;AAGD,oDAKC;AA2CD,oCAUC;AAGD,kCAyCC;AAhKD;;;;;;;;;;;;;;;;;;;GAmBG;AACH,6CAAwF;AACxF,qCAAyC;AACzC,yCAAoC;AACpC,uEAAwD;AACxD,mEAAiH;AAKpG,QAAA,wBAAwB,GAAG,CAAC,CAAC;AAoB1C,gFAAgF;AAChF,SAAgB,kBAAkB,CAAC,KAA4B;IAC3D,MAAM,IAAI,GAAG,IAAA,oDAA4B,EAAC,KAAK,CAAC,CAAC;IACjD,MAAM,MAAM,GAAG,IAAI,uCAAY,CAAC,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC;IACpD,IAAA,2CAAmB,EAAC,KAAK,EAAE,MAAM,CAAC,CAAC;IACnC,OAAQ,MAAM,CAAC,MAAiB,CAAC,QAAQ,CAAC,CAAC,EAAE,MAAM,CAAC,MAAM,CAAC,CAAC,QAAQ,CAAC,QAAQ,CAAC,CAAC;AACnF,CAAC;AAED,iFAAiF;AACjF,SAAgB,oBAAoB,CAAC,KAA4B,EAAE,MAA0B;IACzF,IAAI,CAAC,MAAM;QAAE,OAAO;IACpB,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CAAC,WAAW,CAAC,CAAC,CAAC,IAAI,CAAC,WAAW,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,OAAO,EAAE,UAAU;YAC5E,KAAK,EAAE,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC,IAAI,CAAC,MAAM,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,OAAO,EAAE,KAAK;YACxD,YAAY,EAAE,IAAI,CAAC,aAAa,CAAC,CAAC,CAAC,IAAI,CAAC,aAAa,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,OAAO,EAAE,YAAY;YACpF,KAAK,EAAE,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC,IAAI,CAAC,MAAM,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,OAAO,EAAE,KAAK;SAC3D,CAAC;QACF,MAAM,YAAY,CAAC,IAAI,CAAC,QAAQ,EAAE,OAAO,EAAE,IAAI,CAAC,GAAG,CAAC,CAAC;QACrD,8DAA8D;QAC9D,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;IAC3B,CAAC;CACJ;AApDD,oCAoDC"}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { BinaryStream, BinaryStreamSizeCalculator } from "node-opcua-binary-stream";
|
|
2
|
+
import type { IIdentityMappingStore } from "./identity_mapping_store.js";
|
|
3
|
+
/**
|
|
4
|
+
* Calculate the exact binary size of the encoded store.
|
|
5
|
+
*/
|
|
6
|
+
export declare function identityStoreBinaryStoreSize(store: IIdentityMappingStore): number;
|
|
7
|
+
/**
|
|
8
|
+
* Encode the full store content into a binary stream.
|
|
9
|
+
*/
|
|
10
|
+
export declare function encodeIdentityStore(store: IIdentityMappingStore, stream: BinaryStream | BinaryStreamSizeCalculator): void;
|
|
11
|
+
/**
|
|
12
|
+
* Decode the full store content from a binary stream.
|
|
13
|
+
* Existing store content is **not** cleared — decoded entries are merged.
|
|
14
|
+
*/
|
|
15
|
+
export declare function decodeIdentityStore(store: IIdentityMappingStore, stream: BinaryStream): void;
|
|
16
|
+
/**
|
|
17
|
+
* Save identity store to a binary file.
|
|
18
|
+
*
|
|
19
|
+
* Uses {@link BinaryStreamSizeCalculator} for exact buffer sizing.
|
|
20
|
+
* Creates the directory if it doesn't exist.
|
|
21
|
+
*/
|
|
22
|
+
export declare function saveToBinaryFile(store: IIdentityMappingStore, filePath: string): Promise<void>;
|
|
23
|
+
/**
|
|
24
|
+
* Load identity store from a binary file.
|
|
25
|
+
*
|
|
26
|
+
* If the file does not exist, the store is left unchanged (no error). A
|
|
27
|
+
* corrupt/truncated file is reported as a clear `Error` (naming the path)
|
|
28
|
+
* rather than a cryptic decoding failure, so the operator can react instead
|
|
29
|
+
* of the Server silently losing the role configuration.
|
|
30
|
+
*/
|
|
31
|
+
export declare function loadFromBinaryFile(store: IIdentityMappingStore, filePath: string): Promise<void>;
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.identityStoreBinaryStoreSize = identityStoreBinaryStoreSize;
|
|
4
|
+
exports.encodeIdentityStore = encodeIdentityStore;
|
|
5
|
+
exports.decodeIdentityStore = decodeIdentityStore;
|
|
6
|
+
exports.saveToBinaryFile = saveToBinaryFile;
|
|
7
|
+
exports.loadFromBinaryFile = loadFromBinaryFile;
|
|
8
|
+
/**
|
|
9
|
+
* @module node-opcua-role-set-common
|
|
10
|
+
*
|
|
11
|
+
* Binary persistence for identity-to-role mappings.
|
|
12
|
+
*
|
|
13
|
+
* File format (all OPC UA binary encoding):
|
|
14
|
+
* UInt32 — number of roles
|
|
15
|
+
* For each role:
|
|
16
|
+
* NodeId — role NodeId
|
|
17
|
+
* Variant(ExtensionObject[]) — identity mapping rules as Variant array
|
|
18
|
+
*/
|
|
19
|
+
const node_fs_1 = require("node:fs");
|
|
20
|
+
const node_path_1 = require("node:path");
|
|
21
|
+
const node_opcua_basic_types_1 = require("node-opcua-basic-types");
|
|
22
|
+
const node_opcua_binary_stream_1 = require("node-opcua-binary-stream");
|
|
23
|
+
const node_opcua_types_1 = require("node-opcua-types");
|
|
24
|
+
const node_opcua_variant_1 = require("node-opcua-variant");
|
|
25
|
+
/**
|
|
26
|
+
* Build a Variant wrapping an array of IdentityMappingRuleType for a role.
|
|
27
|
+
*/
|
|
28
|
+
function makeRulesVariant(rules) {
|
|
29
|
+
return new node_opcua_variant_1.Variant({
|
|
30
|
+
dataType: node_opcua_variant_1.DataType.ExtensionObject,
|
|
31
|
+
arrayType: node_opcua_variant_1.VariantArrayType.Array,
|
|
32
|
+
value: rules
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Calculate the exact binary size of the encoded store.
|
|
37
|
+
*/
|
|
38
|
+
function identityStoreBinaryStoreSize(store) {
|
|
39
|
+
const sizeCalculator = new node_opcua_binary_stream_1.BinaryStreamSizeCalculator();
|
|
40
|
+
encodeIdentityStore(store, sizeCalculator);
|
|
41
|
+
return sizeCalculator.length;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Encode the full store content into a binary stream.
|
|
45
|
+
*/
|
|
46
|
+
function encodeIdentityStore(store, stream) {
|
|
47
|
+
const roleIds = store.getRoleIds();
|
|
48
|
+
(0, node_opcua_basic_types_1.encodeUInt32)(roleIds.length, stream);
|
|
49
|
+
for (const roleId of roleIds) {
|
|
50
|
+
(0, node_opcua_basic_types_1.encodeNodeId)(roleId, stream);
|
|
51
|
+
const rules = store.getIdentitiesForRole(roleId);
|
|
52
|
+
const variant = makeRulesVariant(rules);
|
|
53
|
+
variant.encode(stream);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Decode the full store content from a binary stream.
|
|
58
|
+
* Existing store content is **not** cleared — decoded entries are merged.
|
|
59
|
+
*/
|
|
60
|
+
function decodeIdentityStore(store, stream) {
|
|
61
|
+
const roleCount = (0, node_opcua_basic_types_1.decodeUInt32)(stream);
|
|
62
|
+
for (let i = 0; i < roleCount; i++) {
|
|
63
|
+
const roleId = (0, node_opcua_basic_types_1.decodeNodeId)(stream);
|
|
64
|
+
const variant = new node_opcua_variant_1.Variant();
|
|
65
|
+
variant.decode(stream);
|
|
66
|
+
if (variant.arrayType === node_opcua_variant_1.VariantArrayType.Array && Array.isArray(variant.value)) {
|
|
67
|
+
for (const rule of variant.value) {
|
|
68
|
+
if (rule instanceof node_opcua_types_1.IdentityMappingRuleType) {
|
|
69
|
+
store.addIdentity(roleId, rule);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Save identity store to a binary file.
|
|
77
|
+
*
|
|
78
|
+
* Uses {@link BinaryStreamSizeCalculator} for exact buffer sizing.
|
|
79
|
+
* Creates the directory if it doesn't exist.
|
|
80
|
+
*/
|
|
81
|
+
async function saveToBinaryFile(store, filePath) {
|
|
82
|
+
const size = identityStoreBinaryStoreSize(store);
|
|
83
|
+
const stream = new node_opcua_binary_stream_1.BinaryStream(Buffer.alloc(size));
|
|
84
|
+
encodeIdentityStore(store, stream);
|
|
85
|
+
const buffer = stream.buffer.subarray(0, stream.length);
|
|
86
|
+
await node_fs_1.promises.mkdir((0, node_path_1.dirname)(filePath), { recursive: true });
|
|
87
|
+
await node_fs_1.promises.writeFile(filePath, buffer);
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Load identity store from a binary file.
|
|
91
|
+
*
|
|
92
|
+
* If the file does not exist, the store is left unchanged (no error). A
|
|
93
|
+
* corrupt/truncated file is reported as a clear `Error` (naming the path)
|
|
94
|
+
* rather than a cryptic decoding failure, so the operator can react instead
|
|
95
|
+
* of the Server silently losing the role configuration.
|
|
96
|
+
*/
|
|
97
|
+
async function loadFromBinaryFile(store, filePath) {
|
|
98
|
+
let buffer;
|
|
99
|
+
try {
|
|
100
|
+
buffer = await node_fs_1.promises.readFile(filePath);
|
|
101
|
+
}
|
|
102
|
+
catch (err) {
|
|
103
|
+
if (err.code === "ENOENT") {
|
|
104
|
+
return; // missing file → no-op
|
|
105
|
+
}
|
|
106
|
+
throw err;
|
|
107
|
+
}
|
|
108
|
+
if (buffer.length === 0) {
|
|
109
|
+
return; // empty file → no-op
|
|
110
|
+
}
|
|
111
|
+
const stream = new node_opcua_binary_stream_1.BinaryStream(buffer);
|
|
112
|
+
try {
|
|
113
|
+
decodeIdentityStore(store, stream);
|
|
114
|
+
}
|
|
115
|
+
catch (err) {
|
|
116
|
+
throw new Error(`loadFromBinaryFile: '${filePath}' appears to be corrupt or truncated: ${err.message}`);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
//# sourceMappingURL=binary_persistence.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"binary_persistence.js","sourceRoot":"","sources":["../source/binary_persistence.ts"],"names":[],"mappings":";;AAmCA,oEAIC;AAKD,kDASC;AAMD,kDAcC;AAQD,4CAQC;AAUD,gDAmBC;AAtHD;;;;;;;;;;GAUG;AACH,qCAAyC;AACzC,yCAAoC;AACpC,mEAAgG;AAChG,uEAAoF;AAEpF,uDAA2D;AAC3D,2DAAyE;AAIzE;;GAEG;AACH,SAAS,gBAAgB,CAAC,KAAgC;IACtD,OAAO,IAAI,4BAAO,CAAC;QACf,QAAQ,EAAE,6BAAQ,CAAC,eAAe;QAClC,SAAS,EAAE,qCAAgB,CAAC,KAAK;QACjC,KAAK,EAAE,KAAK;KACf,CAAC,CAAC;AACP,CAAC;AAED;;GAEG;AACH,SAAgB,4BAA4B,CAAC,KAA4B;IACrE,MAAM,cAAc,GAAG,IAAI,qDAA0B,EAAE,CAAC;IACxD,mBAAmB,CAAC,KAAK,EAAE,cAAc,CAAC,CAAC;IAC3C,OAAO,cAAc,CAAC,MAAM,CAAC;AACjC,CAAC;AAED;;GAEG;AACH,SAAgB,mBAAmB,CAAC,KAA4B,EAAE,MAAiD;IAC/G,MAAM,OAAO,GAAG,KAAK,CAAC,UAAU,E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-role-set-common
|
|
3
|
+
*/
|
|
4
|
+
import type { NodeId } from "node-opcua-nodeid";
|
|
5
|
+
import type { AnonymousIdentityToken, IdentityMappingRuleType, UserNameIdentityToken, X509IdentityToken } from "node-opcua-types";
|
|
6
|
+
/**
|
|
7
|
+
* Union of all user identity token types (OPC 10000-4).
|
|
8
|
+
* Mirrors the type from node-opcua-address-space but avoids
|
|
9
|
+
* pulling in the heavy address-space dependency.
|
|
10
|
+
*/
|
|
11
|
+
export type AnyUserIdentityToken = AnonymousIdentityToken | UserNameIdentityToken | X509IdentityToken;
|
|
12
|
+
/**
|
|
13
|
+
* Store for identity-to-role mappings (OPC 10000-18 §4.4).
|
|
14
|
+
*
|
|
15
|
+
* Each mapping associates an {@link IdentityMappingRuleType} with a role NodeId.
|
|
16
|
+
* The store resolves which roles a given user identity token grants.
|
|
17
|
+
*/
|
|
18
|
+
export interface IIdentityMappingStore {
|
|
19
|
+
/**
|
|
20
|
+
* Add an identity mapping rule for a role.
|
|
21
|
+
*
|
|
22
|
+
* @returns `true` if the rule was added, `false` if an equivalent rule
|
|
23
|
+
* already existed (the store is idempotent — duplicates are never stored).
|
|
24
|
+
* Callers binding the OPC UA `AddIdentity` Method use this to return
|
|
25
|
+
* `Bad_AlreadyExists` (OPC 10000-18 §4.4.5).
|
|
26
|
+
*/
|
|
27
|
+
addIdentity(roleId: NodeId, rule: IdentityMappingRuleType): boolean;
|
|
28
|
+
/** Remove an identity mapping rule. Returns true if found and removed. */
|
|
29
|
+
removeIdentity(roleId: NodeId, rule: IdentityMappingRuleType): boolean;
|
|
30
|
+
/** Get all identity mapping rules for a given role. */
|
|
31
|
+
getIdentitiesForRole(roleId: NodeId): IdentityMappingRuleType[];
|
|
32
|
+
/** Get all role NodeIds that have at least one mapping. */
|
|
33
|
+
getRoleIds(): NodeId[];
|
|
34
|
+
/** Resolve which roles are granted by a user identity token. */
|
|
35
|
+
resolveRoles(token: AnyUserIdentityToken): NodeId[];
|
|
36
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"identity_mapping_store.js","sourceRoot":"","sources":["../source/identity_mapping_store.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module node-opcua-role-set-common
|
|
3
|
+
*/
|
|
4
|
+
import { type NodeId } from "node-opcua-nodeid";
|
|
5
|
+
import { IdentityMappingRuleType } from "node-opcua-types";
|
|
6
|
+
import type { AnyUserIdentityToken, IIdentityMappingStore } from "./identity_mapping_store.js";
|
|
7
|
+
/**
|
|
8
|
+
* In-memory implementation of {@link IIdentityMappingStore}.
|
|
9
|
+
*
|
|
10
|
+
* Uses a `Map<string, IdentityMappingRuleType[]>` keyed by
|
|
11
|
+
* the role NodeId string representation.
|
|
12
|
+
*/
|
|
13
|
+
export declare class InMemoryIdentityMappingStore implements IIdentityMappingStore {
|
|
14
|
+
private readonly _map;
|
|
15
|
+
addIdentity(roleId: NodeId, rule: IdentityMappingRuleType): boolean;
|
|
16
|
+
removeIdentity(roleId: NodeId, rule: IdentityMappingRuleType): boolean;
|
|
17
|
+
getIdentitiesForRole(roleId: NodeId): IdentityMappingRuleType[];
|
|
18
|
+
getRoleIds(): NodeId[];
|
|
19
|
+
resolveRoles(token: AnyUserIdentityToken): NodeId[];
|
|
20
|
+
}
|