@bedrock/kms-http 22.1.0 → 23.1.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/lib/BedrockKeystoreConfigStorage.js +24 -0
- package/lib/helpers.js +5 -6
- package/lib/http.js +32 -7
- package/lib/middleware.js +2 -2
- package/package.json +1 -1
- package/schemas/bedrock-kms-http.js +11 -0
|
@@ -33,4 +33,28 @@ export class BedrockKeystoreConfigStorage extends KeystoreConfigStorage {
|
|
|
33
33
|
|
|
34
34
|
return returnRecord ? record : keystoreConfig;
|
|
35
35
|
}
|
|
36
|
+
|
|
37
|
+
async getAll({
|
|
38
|
+
controller, req, query = {}, options = {}, returnRecord = false
|
|
39
|
+
} = {}) {
|
|
40
|
+
const records = await keystores.find({controller, query, options});
|
|
41
|
+
const keystoreConfigs = records.map(r => r.config);
|
|
42
|
+
// skip request checks if specifically requested
|
|
43
|
+
if(req === false) {
|
|
44
|
+
return returnRecord ? records : keystoreConfigs;
|
|
45
|
+
}
|
|
46
|
+
// verify that request is from an IP that is allowed to access the config
|
|
47
|
+
for(let i = 0; i < keystoreConfigs.length; ++i) {
|
|
48
|
+
const keystoreConfig = keystoreConfigs[i];
|
|
49
|
+
const {verified} = helpers.verifyRequestIp({keystoreConfig, req});
|
|
50
|
+
if(!verified) {
|
|
51
|
+
throw new BedrockError(
|
|
52
|
+
'Permission denied. Source IP is not allowed.', 'NotAllowedError', {
|
|
53
|
+
httpStatusCode: 403,
|
|
54
|
+
public: true,
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
return returnRecord ? records : keystoreConfigs;
|
|
59
|
+
}
|
|
36
60
|
}
|
package/lib/helpers.js
CHANGED
|
@@ -1,14 +1,13 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* Copyright (c) 2019-
|
|
2
|
+
* Copyright (c) 2019-2025 Digital Bazaar, Inc. All rights reserved.
|
|
3
3
|
*/
|
|
4
|
+
import * as bedrock from '@bedrock/core';
|
|
4
5
|
import forwarded from 'forwarded';
|
|
5
6
|
import ipaddr from 'ipaddr.js';
|
|
6
7
|
|
|
7
|
-
export function getKeystoreId({
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
}
|
|
11
|
-
return `https://${host}${routes.keystores}/${localId}`;
|
|
8
|
+
export function getKeystoreId({localId, routes}) {
|
|
9
|
+
const {baseUri} = bedrock.config.server;
|
|
10
|
+
return `${baseUri}${routes.keystores}/${localId}`;
|
|
12
11
|
}
|
|
13
12
|
|
|
14
13
|
export function verifyRequestIp({keystoreConfig, req}) {
|
package/lib/http.js
CHANGED
|
@@ -1,19 +1,20 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* Copyright (c) 2019-
|
|
2
|
+
* Copyright (c) 2019-2025 Digital Bazaar, Inc. All rights reserved.
|
|
3
3
|
*/
|
|
4
4
|
import * as bedrock from '@bedrock/core';
|
|
5
5
|
import * as brZCapStorage from '@bedrock/zcap-storage';
|
|
6
6
|
import * as helpers from './helpers.js';
|
|
7
7
|
import * as middleware from './middleware.js';
|
|
8
8
|
import {
|
|
9
|
-
|
|
10
|
-
defaultModuleManager as moduleManager
|
|
11
|
-
} from '@bedrock/kms';
|
|
12
|
-
import {
|
|
9
|
+
getConfigsQuery,
|
|
13
10
|
postKeystoreBody,
|
|
14
11
|
postRevocationBody,
|
|
15
12
|
updateKeystoreConfigBody
|
|
16
13
|
} from '../schemas/bedrock-kms-http.js';
|
|
14
|
+
import {
|
|
15
|
+
keystores,
|
|
16
|
+
defaultModuleManager as moduleManager
|
|
17
|
+
} from '@bedrock/kms';
|
|
17
18
|
import {reportOperationUsage, SERVICE_TYPE} from './metering.js';
|
|
18
19
|
import {asyncHandler} from '@bedrock/express';
|
|
19
20
|
import {generateRandom} from '@digitalbazaar/webkms-switch';
|
|
@@ -21,7 +22,7 @@ import {meters} from '@bedrock/meter-usage-reporter';
|
|
|
21
22
|
import {createValidateMiddleware as validate} from '@bedrock/validation';
|
|
22
23
|
|
|
23
24
|
const {config, util: {BedrockError}} = bedrock;
|
|
24
|
-
const {cors} = middleware;
|
|
25
|
+
const {cors, storage} = middleware;
|
|
25
26
|
|
|
26
27
|
bedrock.events.on('bedrock-express.configure.routes', app => {
|
|
27
28
|
const cfg = config['kms-http'];
|
|
@@ -110,12 +111,36 @@ bedrock.events.on('bedrock-express.configure.routes', app => {
|
|
|
110
111
|
|
|
111
112
|
// create a keystore for the controller
|
|
112
113
|
const random = await generateRandom();
|
|
113
|
-
const id = helpers.getKeystoreId({
|
|
114
|
+
const id = helpers.getKeystoreId({localId: random, routes});
|
|
114
115
|
const config = {id, meterId, ...req.body};
|
|
115
116
|
const record = await keystores.insert({config});
|
|
116
117
|
res.status(201).location(id).json(record.config);
|
|
117
118
|
}));
|
|
118
119
|
|
|
120
|
+
// get all keystores by controller query
|
|
121
|
+
app.get(
|
|
122
|
+
routes.keystores,
|
|
123
|
+
cors(),
|
|
124
|
+
validate({querySchema: getConfigsQuery}),
|
|
125
|
+
middleware.authorizeZcapInvocation({
|
|
126
|
+
async getExpectedValues() {
|
|
127
|
+
const {baseUri, host} = bedrock.config.server;
|
|
128
|
+
return {
|
|
129
|
+
host,
|
|
130
|
+
rootInvocationTarget: `${baseUri}${routes.keystores}`
|
|
131
|
+
};
|
|
132
|
+
},
|
|
133
|
+
async getRootController({req}) {
|
|
134
|
+
return req.query.controller;
|
|
135
|
+
}
|
|
136
|
+
}),
|
|
137
|
+
asyncHandler(async (req, res) => {
|
|
138
|
+
const controller = req.query.controller;
|
|
139
|
+
const options = {projection: {_id: 0, config: 1}, limit: 100};
|
|
140
|
+
const results = await storage.getAll({controller, req, options});
|
|
141
|
+
res.json({results});
|
|
142
|
+
}));
|
|
143
|
+
|
|
119
144
|
// update keystore config
|
|
120
145
|
app.options(routes.keystore, cors());
|
|
121
146
|
app.post(
|
package/lib/middleware.js
CHANGED
|
@@ -24,7 +24,7 @@ const {helpers: {inspectCapabilityChain}} = brZCapStorage;
|
|
|
24
24
|
|
|
25
25
|
const FIVE_MINUTES = 1000 * 60 * 5;
|
|
26
26
|
|
|
27
|
-
const storage = new BedrockKeystoreConfigStorage();
|
|
27
|
+
export const storage = new BedrockKeystoreConfigStorage();
|
|
28
28
|
|
|
29
29
|
// creates middleware to get the keystore config for the current request and
|
|
30
30
|
// caches it in `req.webkms.keystore`
|
|
@@ -32,7 +32,7 @@ export function createGetKeystoreConfig({routes}) {
|
|
|
32
32
|
return asyncHandler(async function _getKeystoreConfig(req, res, next) {
|
|
33
33
|
if(!req.webkms) {
|
|
34
34
|
const keystoreId = helpers.getKeystoreId(
|
|
35
|
-
{
|
|
35
|
+
{localId: req.params.keystoreId, routes});
|
|
36
36
|
const configRecord = await storage.get({
|
|
37
37
|
id: keystoreId, req, returnRecord: true
|
|
38
38
|
});
|
package/package.json
CHANGED
|
@@ -162,7 +162,18 @@ const postRevocationBody = {
|
|
|
162
162
|
...delegatedZcap
|
|
163
163
|
};
|
|
164
164
|
|
|
165
|
+
const getConfigsQuery = {
|
|
166
|
+
title: 'Service Object Configuration Query',
|
|
167
|
+
type: 'object',
|
|
168
|
+
required: ['controller'],
|
|
169
|
+
additionalProperties: false,
|
|
170
|
+
properties: {
|
|
171
|
+
controller
|
|
172
|
+
}
|
|
173
|
+
};
|
|
174
|
+
|
|
165
175
|
export {
|
|
176
|
+
getConfigsQuery,
|
|
166
177
|
postKeystoreBody,
|
|
167
178
|
postRevocationBody,
|
|
168
179
|
updateKeystoreConfigBody
|