@bedrock/kms-http 14.5.0 → 15.0.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.
@@ -1,242 +0,0 @@
1
- /*!
2
- * Copyright (c) 2019-2022 Digital Bazaar, Inc. All rights reserved.
3
- */
4
- import * as helpers from './helpers.js';
5
- import {httpsAgent} from '@bedrock/https-agent';
6
- import {keystores} from '@bedrock/kms';
7
- import {createRequire} from 'node:module';
8
- const require = createRequire(import.meta.url);
9
- const {CapabilityAgent, KmsClient, KeystoreAgent} =
10
- require('@digitalbazaar/webkms-client');
11
-
12
- const ZCAP_ROOT_PREFIX = 'urn:zcap:root:';
13
-
14
- describe('keystore API interactions using webkms-client', () => {
15
- let aliceCapabilityAgent;
16
- let aliceKeystoreConfig;
17
- let bobCapabilityAgent;
18
- let bobKeystoreAgent;
19
-
20
- before(async () => {
21
- const secret = '40762a17-1696-428f-a2b2-ddf9fe9b4987';
22
- const handle = 'alice';
23
- aliceCapabilityAgent = await CapabilityAgent.fromSecret({secret, handle});
24
-
25
- aliceKeystoreConfig = await helpers.createKeystore(
26
- {capabilityAgent: aliceCapabilityAgent});
27
- });
28
-
29
- // generate a keystore for Bob
30
- before(async () => {
31
- const secret = '34f2afd1-34ef-4d46-a998-cdc5462dc0d2';
32
- const handle = 'bob';
33
- bobCapabilityAgent = await CapabilityAgent.fromSecret({secret, handle});
34
- const {id: keystoreId} = await helpers.createKeystore(
35
- {capabilityAgent: bobCapabilityAgent});
36
- try {
37
- const kmsClient = new KmsClient({httpsAgent});
38
- bobKeystoreAgent = new KeystoreAgent(
39
- {capabilityAgent: bobCapabilityAgent, keystoreId, kmsClient});
40
- } catch(e) {
41
- assertNoError(e);
42
- }
43
- });
44
-
45
- it('returns error on attempt to update an invalid config', async () => {
46
- // update Alice's keystore config to include ipAllowList
47
- const config = {...aliceKeystoreConfig};
48
- config.sequence++;
49
- config.ipAllowList = ['8.8.8.8/32'];
50
-
51
- let err;
52
- let result;
53
- try {
54
- result = await bobKeystoreAgent.updateConfig({config});
55
- } catch(e) {
56
- err = e;
57
- }
58
- should.not.exist(result);
59
- should.exist(err);
60
- err.status.should.equal(400);
61
- err.data.should.include.keys('message', 'name', 'type', 'details', 'cause');
62
- err.data.type.should.equal('URLMismatchError');
63
- err.data.details.should.have.keys(
64
- ['expected', 'httpStatusCode', 'actual', 'configId', 'requestUrl']);
65
- err.data.details.actual.should.equal(aliceKeystoreConfig.id);
66
- err.data.details.expected.should.equal(
67
- bobKeystoreAgent.keystoreId);
68
- });
69
-
70
- it('updates config and uses key proving cache busting', async () => {
71
- const keystoreId = aliceKeystoreConfig.id;
72
- const kmsClient = new KmsClient({httpsAgent});
73
-
74
- // create zcap agents to take over the keystore
75
- const agent1 = await CapabilityAgent.fromSecret(
76
- {secret: 'bd926f4c-38e4-4c3a-9a17-0e2608ee0a01', handle: 'primary'});
77
- const agent2 = await CapabilityAgent.fromSecret(
78
- {secret: 'd830baa9-02a6-4bd3-9072-de256b0b6c2d', handle: 'primary'});
79
- const agent3 = await CapabilityAgent.fromSecret(
80
- {secret: '37224754-647e-469a-9639-193a2833c69a', handle: 'primary'});
81
-
82
- const aliceKeystoreAgent = new KeystoreAgent(
83
- {capabilityAgent: aliceCapabilityAgent, keystoreId, kmsClient});
84
- const agent1KeystoreAgent = new KeystoreAgent(
85
- {capabilityAgent: agent1, keystoreId, kmsClient});
86
- // const agent2KeystoreAgent = new KeystoreAgent(
87
- // {capabilityAgent: agent2, keystoreId, kmsClient});
88
- const agent3KeystoreAgent = new KeystoreAgent(
89
- {capabilityAgent: agent3, keystoreId, kmsClient});
90
-
91
- // generate a key for use
92
- const hmacKey = await aliceKeystoreAgent.generateKey({type: 'hmac'});
93
-
94
- // delegate zcap for updating config from agent2 to agent3 to test
95
- // cache busting with delegated zcap
96
- const rootCapability = ZCAP_ROOT_PREFIX + encodeURIComponent(keystoreId);
97
- const agent3Zcap = await helpers.delegate({
98
- parentCapability: rootCapability,
99
- controller: agent3.id,
100
- invocationTarget: keystoreId,
101
- delegator: agent2
102
- });
103
-
104
- // delegate zcap for updating config from agent3 to agent2 to agent1 to
105
- // test cache busting with deep delegated zcap
106
- const agent2Zcap = await helpers.delegate({
107
- parentCapability: rootCapability,
108
- controller: agent2.id,
109
- invocationTarget: keystoreId,
110
- delegator: agent3
111
- });
112
- const agent1Zcap = await helpers.delegate({
113
- parentCapability: agent2Zcap,
114
- controller: agent1.id,
115
- invocationTarget: keystoreId,
116
- delegator: agent2
117
- });
118
-
119
- // update Alice's keystore config controller to agent1
120
- let config = {...aliceKeystoreConfig};
121
-
122
- try {
123
- // disable cache delete functionality on update
124
- keystores._disableClearCacheOnUpdate(true);
125
-
126
- // update controller alice => agent1 (no cache busting required)
127
- {
128
- config.sequence++;
129
- config.controller = agent1.id;
130
- const result = await aliceKeystoreAgent.updateConfig({config});
131
- result.config.id.should.eql(config.id);
132
- result.config.controller.should.eql(agent1.id);
133
- ({config} = result);
134
- }
135
-
136
- // update controller agent1 => agent2 (requires cache busting by
137
- // inspecting the root zcap)
138
- {
139
- config.sequence++;
140
- config.controller = agent2.id;
141
- const result = await agent1KeystoreAgent.updateConfig({config});
142
- result.config.id.should.eql(config.id);
143
- result.config.controller.should.eql(agent2.id);
144
- ({config} = result);
145
- }
146
-
147
- // update controller agent2 => agent3 (requires cache busting by
148
- // inspecting a delegated zcap)
149
- {
150
- config.sequence++;
151
- config.controller = agent3.id;
152
- const result = await agent3KeystoreAgent.updateConfig(
153
- {capability: agent3Zcap, config});
154
- result.config.id.should.eql(config.id);
155
- result.config.controller.should.eql(agent3.id);
156
- ({config} = result);
157
- }
158
-
159
- // update controller agent3 => agent1 (requires cache busting by
160
- // inspecting a deep delegated zcap)
161
- {
162
- config.sequence++;
163
- config.controller = agent1.id;
164
- const result = await agent1KeystoreAgent.updateConfig(
165
- {capability: agent1Zcap, config});
166
- result.config.id.should.eql(config.id);
167
- result.config.controller.should.eql(agent1.id);
168
- ({config} = result);
169
- }
170
-
171
- // update controller back to alice to test key op cache busting
172
- {
173
- config.sequence++;
174
- config.controller = aliceCapabilityAgent.id;
175
- const result = await agent1KeystoreAgent.updateConfig({config});
176
- result.config.id.should.eql(config.id);
177
- result.config.controller.should.eql(aliceCapabilityAgent.id);
178
- ({config} = result);
179
- }
180
-
181
- // use hmac key as alice (requires cache busting by inspecting root
182
- // zcap)
183
- await hmacKey.sign({data: new Uint8Array([0])});
184
-
185
- // update controller alice => agent1 (no cache busting required)
186
- {
187
- config.sequence++;
188
- config.controller = agent1.id;
189
- const result = await aliceKeystoreAgent.updateConfig({config});
190
- result.config.id.should.eql(config.id);
191
- result.config.controller.should.eql(agent1.id);
192
- ({config} = result);
193
- }
194
-
195
- // use hmac key as agent1 (requires cache busting by
196
- // inspecting the root zcap)
197
- hmacKey.invocationSigner = agent1.getSigner();
198
- // use different data each time to avoid hmac caching
199
- await hmacKey.sign({data: new Uint8Array([1])});
200
-
201
- // update controller agent1 => agent2 (requires cache busting by
202
- // inspecting the root zcap)
203
- {
204
- config.sequence++;
205
- config.controller = agent2.id;
206
- const result = await agent1KeystoreAgent.updateConfig({config});
207
- result.config.id.should.eql(config.id);
208
- result.config.controller.should.eql(agent2.id);
209
- ({config} = result);
210
- }
211
-
212
- // use hmac key as agent3 (requires cache busting by
213
- // inspecting a delegated zcap)
214
- hmacKey.capability = agent3Zcap;
215
- hmacKey.invocationSigner = agent3.getSigner();
216
- // use different data each time to avoid hmac caching
217
- await hmacKey.sign({data: new Uint8Array([1])});
218
-
219
- // update controller agent2 => agent3 (cache should already be fresh)
220
- {
221
- config.sequence++;
222
- config.controller = agent3.id;
223
- const result = await agent3KeystoreAgent.updateConfig(
224
- {capability: agent3Zcap, config});
225
- result.config.id.should.eql(config.id);
226
- result.config.controller.should.eql(agent3.id);
227
- ({config} = result);
228
- }
229
-
230
- // use hmac key as agent1 (requires cache busting by
231
- // inspecting a deep delegated zcap)
232
- hmacKey.capability = agent1Zcap;
233
- hmacKey.invocationSigner = agent1.getSigner();
234
- // use different data each time to avoid hmac caching
235
- await hmacKey.sign({data: new Uint8Array([1])});
236
- } catch(e) {
237
- assertNoError(e);
238
- } finally {
239
- keystores._disableClearCacheOnUpdate(false);
240
- }
241
- });
242
- });