@tdxvolt/volt-client-grpc 0.1.0 → 0.1.2
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/lib/index.cjs +1645 -1463
- package/package.json +1 -1
- package/protobuf/tdx/volt_api/data/v1/sqlite_database_api.proto +8 -0
- package/protobuf/tdx/volt_api/data/v1/sqlite_server_api.proto +5 -0
- package/protobuf/tdx/volt_api/{tunnel → relay}/v1/proxy_api.proto +2 -2
- package/protobuf/tdx/volt_api/{tunnel/v1/tunnel_api.proto → relay/v1/relay_api.proto} +6 -5
- package/protobuf/tdx/volt_api/sync/v1/sync.proto +49 -6
- package/protobuf/tdx/volt_api/volt/v1/discovery_api.proto +3 -3
- package/protobuf/tdx/volt_api/volt/v1/file_api.proto +47 -1
- package/protobuf/tdx/volt_api/volt/v1/spark_api.proto +121 -0
- package/protobuf/tdx/volt_api/volt/v1/volt.proto +30 -25
- package/protobuf/tdx/volt_api/volt/v1/volt_api.proto +78 -45
- package/protobuf/tdx/volt_api/volt/v1/wire_api.proto +8 -6
- package/src/constants.js +72 -72
- package/src/grpc-call.js +31 -23
- package/src/grpc-utils.js +212 -176
- package/src/index.js +1 -1
- package/src/proto-utils.js +68 -51
- package/src/utils.js +66 -66
- package/src/volt-client-internal.js +281 -252
- package/src/volt-client.js +544 -479
- package/src/volt-connection.js +144 -135
- package/src/volt-credential.js +237 -0
- package/src/tunnel.pb.js +0 -0
- package/src/volt-crypto.js +0 -213
|
@@ -5,284 +5,313 @@ import bent from "bent";
|
|
|
5
5
|
import forge from "node-forge";
|
|
6
6
|
import lodash from "lodash";
|
|
7
7
|
import VoltConnection from "./volt-connection.js";
|
|
8
|
-
import {getServiceDescriptors} from "./proto-utils.js";
|
|
9
|
-
import {createClient as createGrpcClient} from "./grpc-utils.js";
|
|
8
|
+
import { getServiceDescriptors } from "./proto-utils.js";
|
|
9
|
+
import { createClient as createGrpcClient } from "./grpc-utils.js";
|
|
10
10
|
import * as voltUtils from "./utils.js";
|
|
11
|
-
import {constants} from "./constants.js";
|
|
11
|
+
import { constants } from "./constants.js";
|
|
12
12
|
import GRPCCall from "./grpc-call.js";
|
|
13
13
|
|
|
14
|
-
const {pki} = forge;
|
|
15
|
-
const {filter, pick} = lodash;
|
|
14
|
+
const { pki } = forge;
|
|
15
|
+
const { filter, pick } = lodash;
|
|
16
16
|
|
|
17
17
|
const log = debug("volt-client-grpc:volt-client-internal");
|
|
18
18
|
const voltServices = [
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
19
|
+
constants.serviceType.voltAPI,
|
|
20
|
+
constants.serviceType.fileAPI,
|
|
21
|
+
constants.serviceType.sqliteDatabaseAPI,
|
|
22
|
+
constants.serviceType.sqliteServerAPI,
|
|
23
|
+
constants.serviceType.ssiAPI,
|
|
24
|
+
constants.serviceType.relayAPI,
|
|
25
|
+
constants.serviceType.wireAPI,
|
|
25
26
|
];
|
|
26
27
|
|
|
27
28
|
function issueBind(bindRequest, ttl) {
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
29
|
+
// eslint-disable-next-line no-use-before-define
|
|
30
|
+
return unaryCall.call(this, "Bind", bindRequest).then((bindResponse) => {
|
|
31
|
+
log("got binding request response %j", bindResponse);
|
|
32
|
+
if (bindResponse.status?.code) {
|
|
33
|
+
log("error in bind response: %s", bindResponse.status.message);
|
|
34
|
+
return Promise.reject(new Error(bindResponse.status.message));
|
|
35
|
+
} else {
|
|
36
|
+
switch (bindResponse.decision) {
|
|
37
|
+
case "POLICY_DECISION_PERMIT": {
|
|
38
|
+
//
|
|
39
|
+
// The request status is permit => cache the bind response info.
|
|
40
|
+
//
|
|
41
|
+
|
|
42
|
+
// This is the certificate assigned to us by the volt.
|
|
43
|
+
this._credential.cache.cert = bindResponse.cert;
|
|
44
|
+
|
|
45
|
+
// This is the signing CA used by the volt (don't ov)
|
|
46
|
+
this._credential.cache.ca = bindResponse.chain;
|
|
47
|
+
|
|
48
|
+
// Resource id is assigned by the volt.
|
|
49
|
+
this._credential.cache.client_id = bindResponse.identity_id;
|
|
50
|
+
this._credential.saveCache();
|
|
51
|
+
break;
|
|
52
|
+
}
|
|
53
|
+
case "POLICY_DECISION_DENY": {
|
|
54
|
+
log(">>>>>>>>>>>>>>> access request is DENIED <<<<<<<<<<<<<<<<<<<");
|
|
55
|
+
break;
|
|
56
|
+
}
|
|
57
|
+
case "POLICY_DECISION_PROMPT":
|
|
58
|
+
case "POLICY_DECISION_PENDING": {
|
|
59
|
+
log("access pending approval - waiting...");
|
|
60
|
+
break;
|
|
61
|
+
}
|
|
62
|
+
default:
|
|
63
|
+
log("ignoring unknown bind descision %s", bindResponse.status);
|
|
64
|
+
break;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
return bindResponse.decision;
|
|
69
|
+
});
|
|
66
70
|
}
|
|
67
71
|
|
|
68
72
|
function findDIDDocumentService(document, serviceType) {
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
73
|
+
if (!document.service) {
|
|
74
|
+
return [];
|
|
75
|
+
}
|
|
72
76
|
|
|
73
|
-
|
|
74
|
-
|
|
77
|
+
const matches = filter(document.service, (svc) => svc.type === serviceType);
|
|
78
|
+
return matches;
|
|
75
79
|
}
|
|
76
80
|
|
|
77
81
|
export async function bindInternal() {
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
82
|
+
try {
|
|
83
|
+
// Check if we need to resolve a Relay volt.
|
|
84
|
+
if (
|
|
85
|
+
this._config?.relay?.discovery_url ||
|
|
86
|
+
typeof this._config?.relay === "string"
|
|
87
|
+
) {
|
|
88
|
+
const relayURL = this._config.relay?.discovery_url || this._config.relay;
|
|
89
|
+
log("attempting to retrieve Relay information from %s", relayURL);
|
|
90
|
+
const voltConfig = await fetchVoltConfig.call(this, relayURL);
|
|
91
|
+
this._config.relay = voltConfig.volt;
|
|
92
|
+
this._config.relay.discovery_url = relayURL;
|
|
93
|
+
if (this._config.relay.ca_pem) {
|
|
94
|
+
log(
|
|
95
|
+
"Auto-fetched Relay CA %s, remote address %s",
|
|
96
|
+
this._config.relay.ca_pem,
|
|
97
|
+
this._config.relay.address,
|
|
98
|
+
);
|
|
99
|
+
} else {
|
|
100
|
+
throw new Error("Unable to fetch Relay CA - cannot securely connect.");
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
if (!this._credential.cache.ca) {
|
|
105
|
+
this._credential.cache.ca = this._voltConfig.ca_pem;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
if (!this._credential.cache.ca) {
|
|
109
|
+
throw new Error(
|
|
110
|
+
"No certificate authority found in configuration for target Volt - check configuration",
|
|
111
|
+
);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
const keyPair = this._credential.getKey();
|
|
115
|
+
const publicKeyPem = pki.publicKeyToPem(keyPair.publicKey);
|
|
116
|
+
|
|
117
|
+
if (!this._credential.cache.bindIp) {
|
|
118
|
+
// Default the IP address if none is specified. This is used to set the SAN in the
|
|
119
|
+
// certificate issued by the Volt.
|
|
120
|
+
this._credential.cache.bindIp = ip.address();
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
this._credential.saveCache();
|
|
124
|
+
|
|
125
|
+
// Form the request message.
|
|
126
|
+
const bindRequest = {
|
|
127
|
+
binding_name: this._config.client_name,
|
|
128
|
+
public_key: publicKeyPem,
|
|
129
|
+
host: this._credential.cache.bindIp,
|
|
130
|
+
x509_credential: [],
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
if (this._credential.cache.cert) {
|
|
134
|
+
bindRequest.x509_credential = [
|
|
135
|
+
this._credential.cache.cert + this._credential.cache.ca,
|
|
136
|
+
];
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
if (this._voltConfig.challenge_code) {
|
|
140
|
+
bindRequest.challenge = voltUtils.signBase64(
|
|
141
|
+
keyPair.privateKey,
|
|
142
|
+
this._voltConfig.challenge_code,
|
|
143
|
+
);
|
|
144
|
+
} else {
|
|
145
|
+
log(
|
|
146
|
+
"****no Volt challenge code available**** => not sending challenge signature",
|
|
147
|
+
);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
// For remote connections, add our cloud-issued certificate as an additional credential.
|
|
151
|
+
if (this._config?.relay?.ca_pem && this._credential.cache.cloud_cert) {
|
|
152
|
+
bindRequest.x509_credential = bindRequest.x509_credential.concat(
|
|
153
|
+
this._credential.cache.cloud_cert + this._config.relay.ca_pem,
|
|
154
|
+
);
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
log("sending %d x509 credentials", bindRequest.x509_credential.length);
|
|
158
|
+
log("bind TTL is ", this._config.bindRequestTTL);
|
|
159
|
+
|
|
160
|
+
// This will block while the request is pending.
|
|
161
|
+
const pollInterval = 10000;
|
|
162
|
+
let decision = "";
|
|
163
|
+
do {
|
|
164
|
+
if (decision) {
|
|
165
|
+
// Wait a while before retrying.
|
|
166
|
+
// eslint-disable-next-line no-await-in-loop
|
|
167
|
+
await new Promise((resolve) => setTimeout(resolve, pollInterval));
|
|
168
|
+
}
|
|
169
|
+
// eslint-disable-next-line no-await-in-loop
|
|
170
|
+
decision = await issueBind.call(
|
|
171
|
+
this,
|
|
172
|
+
bindRequest,
|
|
173
|
+
this._voltConfig.bindRequestTTL,
|
|
174
|
+
);
|
|
175
|
+
log("binding decision: %s", decision);
|
|
176
|
+
} while (
|
|
177
|
+
decision === "POLICY_DECISION_PROMPT" ||
|
|
178
|
+
decision === "POLICY_DECISION_PENDING"
|
|
179
|
+
);
|
|
180
|
+
|
|
181
|
+
return decision === "POLICY_DECISION_PERMIT";
|
|
182
|
+
} catch (err) {
|
|
183
|
+
log("failure binding to Volt [%s]", err.message);
|
|
184
|
+
throw err;
|
|
185
|
+
}
|
|
165
186
|
}
|
|
166
187
|
|
|
167
188
|
export function connectInternal() {
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
189
|
+
try {
|
|
190
|
+
if (this._voltConnection) {
|
|
191
|
+
log("connectInternal - Volt connection already exists");
|
|
192
|
+
return Promise.resolve();
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
const onDisconnected = () => {
|
|
196
|
+
this._connected = false;
|
|
197
|
+
this._session = null;
|
|
198
|
+
|
|
199
|
+
if (this._cachedClient) {
|
|
200
|
+
this._grpc.closeClient(this._cachedClient);
|
|
201
|
+
this._cachedClient = null;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
if (this._cachedRemoteClient) {
|
|
205
|
+
this._grpc.closeClient(this._cachedRemoteClient);
|
|
206
|
+
this._cachedRemoteClient = null;
|
|
207
|
+
}
|
|
208
|
+
};
|
|
209
|
+
|
|
210
|
+
this._voltConnection = new VoltConnection(this, this._grpc);
|
|
211
|
+
|
|
212
|
+
this._voltConnection.on("connected", (session) => {
|
|
213
|
+
this._session = session;
|
|
214
|
+
this._connected = !!session;
|
|
215
|
+
if (!this._connected) {
|
|
216
|
+
onDisconnected();
|
|
217
|
+
}
|
|
218
|
+
this.emit("connected", !!session);
|
|
219
|
+
});
|
|
220
|
+
|
|
221
|
+
this._voltConnection.on("error", (err) => {
|
|
222
|
+
// Should auto-reconnect.
|
|
223
|
+
log("Volt connection error [%s]", err.message);
|
|
224
|
+
if (this._connected) {
|
|
225
|
+
onDisconnected();
|
|
226
|
+
this.emit("connected", false);
|
|
227
|
+
}
|
|
228
|
+
});
|
|
229
|
+
|
|
230
|
+
this._voltConnection.on("evt", (evt) => {
|
|
231
|
+
this.emit("evt", evt);
|
|
232
|
+
});
|
|
233
|
+
|
|
234
|
+
return this._voltConnection.connect();
|
|
235
|
+
} catch (err) {
|
|
236
|
+
log("connect - error [%s]", err.message);
|
|
237
|
+
throw err;
|
|
238
|
+
}
|
|
218
239
|
}
|
|
219
240
|
|
|
220
241
|
export function getVoltAPIClientInternal() {
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
+
if (!this._cachedClient) {
|
|
243
|
+
const serviceAddress = this.isRemote
|
|
244
|
+
? this._config.relay.address
|
|
245
|
+
: this._voltConfig.address;
|
|
246
|
+
log("creating cached VoltAPI service client on %s", serviceAddress);
|
|
247
|
+
let serviceDescriptors = {};
|
|
248
|
+
voltServices.forEach((pkg) => {
|
|
249
|
+
const packageDescriptors = getServiceDescriptors(this._grpc, pkg);
|
|
250
|
+
serviceDescriptors = { ...serviceDescriptors, ...packageDescriptors };
|
|
251
|
+
});
|
|
252
|
+
|
|
253
|
+
this._cachedClient = createGrpcClient(
|
|
254
|
+
this._grpc,
|
|
255
|
+
serviceDescriptors,
|
|
256
|
+
serviceAddress,
|
|
257
|
+
this._credential,
|
|
258
|
+
this.isRemote && !this.isVoltRelay,
|
|
259
|
+
this._config?.relay?.cloud_relay ? this._voltConfig.id : "",
|
|
260
|
+
this._config?.relay?.cloud_relay ? this._voltConfig.id : "",
|
|
261
|
+
);
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
return this._cachedClient;
|
|
242
265
|
}
|
|
243
266
|
|
|
244
267
|
export function unaryCall(method, request) {
|
|
245
|
-
|
|
268
|
+
const grpcClient = this.getVoltAPIClient();
|
|
246
269
|
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
270
|
+
const call = new GRPCCall(this, method, "METHOD_TYPE_UNARY");
|
|
271
|
+
call.start(grpcClient, request);
|
|
272
|
+
return call.wait();
|
|
250
273
|
}
|
|
251
274
|
|
|
252
|
-
export async function fetchVoltConfig(
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
275
|
+
export async function fetchVoltConfig(disovery_url) {
|
|
276
|
+
const getJSON = bent("json");
|
|
277
|
+
const resolvedConfig = await getJSON(disovery_url);
|
|
278
|
+
|
|
279
|
+
const voltConfig = pick(
|
|
280
|
+
resolvedConfig,
|
|
281
|
+
"id",
|
|
282
|
+
"display_name",
|
|
283
|
+
"public_key",
|
|
284
|
+
"fingerprint",
|
|
285
|
+
"address",
|
|
286
|
+
"ca_pem",
|
|
287
|
+
"challenge_code",
|
|
288
|
+
"cloud_relay",
|
|
289
|
+
);
|
|
290
|
+
|
|
291
|
+
const relayConfig = pick(resolvedConfig, "relay");
|
|
292
|
+
|
|
293
|
+
return { volt: voltConfig, ...relayConfig };
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
export async function fetchVoltConfigFromDID(volt_did) {
|
|
297
|
+
const didResolution = voltUtils.getDIDResolutionURL(volt_did);
|
|
298
|
+
if (!didResolution) {
|
|
299
|
+
throw new Error(`Invalid Volt DID: ${volt_did}`);
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
const getJSON = bent("json");
|
|
303
|
+
const didDocument = await getJSON(didResolution);
|
|
304
|
+
const voltConfigServices = findDIDDocumentService(
|
|
305
|
+
didDocument,
|
|
306
|
+
constants.didServiceType.voltConfig,
|
|
307
|
+
);
|
|
308
|
+
if (voltConfigServices.length !== 1) {
|
|
309
|
+
log(
|
|
310
|
+
"Unexpected number of Volt configuration endpoints found in DID document for %s, services found: %d",
|
|
311
|
+
volt_did,
|
|
312
|
+
voltConfigServices.length,
|
|
313
|
+
);
|
|
314
|
+
}
|
|
315
|
+
const serviceEndpoint = voltConfigServices[0].serviceEndpoint;
|
|
316
|
+
return await fetchVoltConfig.call(this, serviceEndpoint);
|
|
288
317
|
}
|