@bedrock/kms-http 14.3.1 → 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.
package/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # Bedrock Key Management System API module _(bedrock-kms-http)_
1
+ # Bedrock Key Management System API module _(@bedrock/kms-http)_
2
2
  > HTTP APIs for Bedrock Key Management.
3
3
 
4
4
  ## Table of Contents
@@ -11,14 +11,14 @@
11
11
 
12
12
  ## Install
13
13
 
14
- - Node.js 12+ is required.
14
+ - Node.js 16+ is required.
15
15
 
16
16
  ### NPM
17
17
 
18
18
  To install via NPM:
19
19
 
20
20
  ```
21
- npm install --save bedrock-kms-http
21
+ npm install @bedrock/kms-http
22
22
  ```
23
23
 
24
24
  ### Development
@@ -3,10 +3,8 @@
3
3
  */
4
4
  import * as bedrock from '@bedrock/core';
5
5
  import * as helpers from './helpers.js';
6
- import {createRequire} from 'node:module';
6
+ import {KeystoreConfigStorage} from '@digitalbazaar/webkms-switch';
7
7
  import {keystores} from '@bedrock/kms';
8
- const require = createRequire(import.meta.url);
9
- const {KeystoreConfigStorage} = require('@digitalbazaar/webkms-switch');
10
8
 
11
9
  const {util: {BedrockError}} = bedrock;
12
10
 
package/lib/helpers.js CHANGED
@@ -2,7 +2,7 @@
2
2
  * Copyright (c) 2019-2022 Digital Bazaar, Inc. All rights reserved.
3
3
  */
4
4
  import forwarded from 'forwarded';
5
- import {Netmask} from 'netmask';
5
+ import ipaddr from 'ipaddr.js';
6
6
 
7
7
  export function getKeystoreId({host, req, localId, routes}) {
8
8
  if(!host) {
@@ -12,6 +12,7 @@ export function getKeystoreId({host, req, localId, routes}) {
12
12
  }
13
13
 
14
14
  export function verifyRequestIp({keystoreConfig, req}) {
15
+ // skip check if no IP allow list configured
15
16
  const {ipAllowList} = keystoreConfig;
16
17
  if(!ipAllowList) {
17
18
  return {verified: true};
@@ -22,15 +23,17 @@ export function verifyRequestIp({keystoreConfig, req}) {
22
23
  // IPs will be from the `x-forwarded-for` header.
23
24
  const sourceAddresses = forwarded(req);
24
25
 
25
- // ipAllowList is an array of CIDRs
26
- for(const cidr of ipAllowList) {
27
- const netmask = new Netmask(cidr);
28
- for(const address of sourceAddresses) {
29
- if(netmask.contains(address)) {
30
- return {verified: true};
31
- }
32
- }
33
- }
26
+ // build list of allowed IP ranges from IPv4/IPv6 CIDRs
27
+ const ipAllowRangeList = {
28
+ allow: ipAllowList.map(cidr => ipaddr.parseCIDR(cidr))
29
+ };
30
+
31
+ // check if any source address allowed
32
+ const verified = sourceAddresses.some(address => {
33
+ const ip = ipaddr.parse(address);
34
+ // check if in allow list, else deny
35
+ return ipaddr.subnetMatch(ip, ipAllowRangeList, 'deny') === 'allow';
36
+ });
34
37
 
35
- return {verified: false};
38
+ return {verified};
36
39
  }
package/lib/http.js CHANGED
@@ -5,25 +5,23 @@ 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
- import {asyncHandler} from '@bedrock/express';
9
- import cors from 'cors';
10
- import {createRequire} from 'node:module';
11
- import {createValidateMiddleware as validate} from '@bedrock/validation';
12
8
  import {
13
- defaultModuleManager as moduleManager,
14
- keystores
9
+ keystores,
10
+ defaultModuleManager as moduleManager
15
11
  } from '@bedrock/kms';
16
- import {meters} from '@bedrock/meter-usage-reporter';
17
12
  import {
18
13
  postKeystoreBody,
19
- updateKeystoreConfigBody,
20
- postRevocationBody
14
+ postRevocationBody,
15
+ updateKeystoreConfigBody
21
16
  } from '../schemas/bedrock-kms-http.js';
22
17
  import {reportOperationUsage, SERVICE_TYPE} from './metering.js';
23
- const require = createRequire(import.meta.url);
24
- const {generateRandom} = require('@digitalbazaar/webkms-switch');
18
+ import {asyncHandler} from '@bedrock/express';
19
+ import {generateRandom} from '@digitalbazaar/webkms-switch';
20
+ import {meters} from '@bedrock/meter-usage-reporter';
21
+ import {createValidateMiddleware as validate} from '@bedrock/validation';
25
22
 
26
23
  const {config, util: {BedrockError}} = bedrock;
24
+ const {cors} = middleware;
27
25
 
28
26
  bedrock.events.on('bedrock-express.configure.routes', app => {
29
27
  const cfg = config['kms-http'];
package/lib/metering.js CHANGED
@@ -4,11 +4,11 @@
4
4
  import * as bedrock from '@bedrock/core';
5
5
  import * as brZCapStorage from '@bedrock/zcap-storage';
6
6
  import {
7
- defaultModuleManager as moduleManager,
8
- keystores
7
+ keystores,
8
+ defaultModuleManager as moduleManager
9
9
  } from '@bedrock/kms';
10
- import {meters} from '@bedrock/meter-usage-reporter';
11
10
  import {logger} from './logger.js';
11
+ import {meters} from '@bedrock/meter-usage-reporter';
12
12
 
13
13
  // configure usage aggregator for webkms meters
14
14
  export const SERVICE_TYPE = 'webkms';
package/lib/middleware.js CHANGED
@@ -4,26 +4,25 @@
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
- import {asyncHandler} from '@bedrock/express';
8
- import {BedrockKeystoreConfigStorage} from './BedrockKeystoreConfigStorage.js';
9
- import {createRequire} from 'node:module';
7
+ import {
8
+ authorizeZcapInvocation as _authorizeZcapInvocation,
9
+ authorizeZcapRevocation as _authorizeZcapRevocation
10
+ } from '@digitalbazaar/ezcap-express';
10
11
  import {
11
12
  defaultDocumentLoader as documentLoader,
12
13
  defaultModuleManager as moduleManager
13
14
  } from '@bedrock/kms';
15
+ import {default as _cors} from 'cors';
16
+ import {asyncHandler} from '@bedrock/express';
17
+ import {BedrockKeystoreConfigStorage} from './BedrockKeystoreConfigStorage.js';
18
+ import {createOperationMiddleware} from '@digitalbazaar/webkms-switch';
19
+ import {CryptoLD} from 'crypto-ld';
20
+ import {Ed25519Signature2020} from '@digitalbazaar/ed25519-signature-2020';
21
+ import {
22
+ Ed25519VerificationKey2020
23
+ } from '@digitalbazaar/ed25519-verification-key-2020';
14
24
  import {logger} from './logger.js';
15
25
  import {reportOperationUsage} from './metering.js';
16
- const require = createRequire(import.meta.url);
17
- const {
18
- authorizeZcapInvocation: _authorizeZcapInvocation,
19
- authorizeZcapRevocation: _authorizeZcapRevocation
20
- } = require('@digitalbazaar/ezcap-express');
21
- const {createOperationMiddleware} = require('@digitalbazaar/webkms-switch');
22
- const {CryptoLD} = require('crypto-ld');
23
- const {Ed25519Signature2020} =
24
- require('@digitalbazaar/ed25519-signature-2020');
25
- const {Ed25519VerificationKey2020} =
26
- require('@digitalbazaar/ed25519-verification-key-2020');
27
26
 
28
27
  const {config, util: {BedrockError}} = bedrock;
29
28
 
@@ -107,6 +106,11 @@ export function authorizeZcapRevocation() {
107
106
  });
108
107
  }
109
108
 
109
+ export function cors() {
110
+ // `86400` is the max acceptable cache age for modern browsers
111
+ return _cors({maxAge: 86400});
112
+ }
113
+
110
114
  async function getKeystoreController({req}) {
111
115
  /* The following code is to prevent false-negative authz errors that are the
112
116
  result of stale cached keystore configs.
package/package.json CHANGED
@@ -1,9 +1,13 @@
1
1
  {
2
2
  "name": "@bedrock/kms-http",
3
- "version": "14.3.1",
3
+ "version": "15.0.0",
4
4
  "type": "module",
5
5
  "description": "Bedrock KMS HTTP API",
6
6
  "main": "./lib/index.js",
7
+ "files": [
8
+ "lib/**/*.js",
9
+ "schemas/**/*.js"
10
+ ],
7
11
  "scripts": {
8
12
  "lint": "eslint ."
9
13
  },
@@ -24,35 +28,36 @@
24
28
  },
25
29
  "homepage": "https://github.com/digitalbazaar/bedrock-kms-http",
26
30
  "dependencies": {
27
- "@digitalbazaar/ed25519-signature-2020": "^3.0.0",
28
- "@digitalbazaar/ed25519-verification-key-2020": "^3.2.0",
29
- "@digitalbazaar/ezcap-express": "^6.2.0",
30
- "@digitalbazaar/webkms-switch": "^10.2.0",
31
+ "@digitalbazaar/ed25519-signature-2020": "^4.0.1",
32
+ "@digitalbazaar/ed25519-verification-key-2020": "^4.0.0",
33
+ "@digitalbazaar/ezcap-express": "^7.0.0",
34
+ "@digitalbazaar/webkms-switch": "^11.0.0",
31
35
  "cidr-regex": "^3.1.1",
32
36
  "cors": "^2.8.5",
33
- "crypto-ld": "^6.0.0",
37
+ "crypto-ld": "^7.0.0",
34
38
  "forwarded": "^0.2.0",
35
- "netmask": "^2.0.2"
39
+ "ipaddr.js": "^2.0.1"
36
40
  },
37
41
  "peerDependencies": {
38
- "@bedrock/app-identity": "^3.0.0",
39
- "@bedrock/core": "^6.0.0",
42
+ "@bedrock/app-identity": "^4.0.0",
43
+ "@bedrock/core": "^6.0.1",
40
44
  "@bedrock/express": "^8.0.0",
41
- "@bedrock/kms": "^10.2.0",
42
- "@bedrock/meter-usage-reporter": "^7.0.0",
45
+ "@bedrock/kms": "^11.0.0",
46
+ "@bedrock/meter-usage-reporter": "^8.0.0",
43
47
  "@bedrock/validation": "^7.0.0",
44
- "@bedrock/zcap-storage": "^7.0.0"
48
+ "@bedrock/zcap-storage": "^8.0.0"
45
49
  },
46
50
  "directories": {
47
51
  "lib": "./lib"
48
52
  },
49
53
  "devDependencies": {
50
- "eslint": "^7.32.0",
51
- "eslint-config-digitalbazaar": "^2.8.0",
52
- "eslint-plugin-jsdoc": "^39.2.9",
54
+ "eslint": "^8.18.0",
55
+ "eslint-config-digitalbazaar": "^4.0.1",
56
+ "eslint-plugin-jsdoc": "^39.3.3",
57
+ "eslint-plugin-unicorn": "^43.0.0",
53
58
  "jsdoc-to-markdown": "^7.1.1"
54
59
  },
55
60
  "engines": {
56
- "node": ">=14"
61
+ "node": ">=16"
57
62
  }
58
63
  }
@@ -114,7 +114,7 @@ const ipAllowList = {
114
114
  items: {
115
115
  type: 'string',
116
116
  // leading and trailing slashes in regex must be removed
117
- pattern: cidrRegex.v4({exact: true}).toString().slice(1, -1),
117
+ pattern: cidrRegex({exact: true}).toString().slice(1, -1),
118
118
  }
119
119
  };
120
120
 
package/.eslintrc.cjs DELETED
@@ -1,12 +0,0 @@
1
- module.exports = {
2
- root: true,
3
- parserOptions: {
4
- // this is required for dynamic import()
5
- ecmaVersion: 2020
6
- },
7
- env: {
8
- node: true
9
- },
10
- extends: ['digitalbazaar', 'digitalbazaar/jsdoc'],
11
- ignorePatterns: ['node_modules/']
12
- };
@@ -1,77 +0,0 @@
1
- name: Bedrock Node.js CI
2
-
3
- on: [push]
4
-
5
- jobs:
6
- lint:
7
- runs-on: ubuntu-latest
8
- timeout-minutes: 10
9
- strategy:
10
- matrix:
11
- node-version: [16.x]
12
- steps:
13
- - uses: actions/checkout@v2
14
- - name: Use Node.js ${{ matrix.node-version }}
15
- uses: actions/setup-node@v1
16
- with:
17
- node-version: ${{ matrix.node-version }}
18
- - run: npm install
19
- - name: Run eslint
20
- run: npm run lint
21
- test-node:
22
- needs: [lint]
23
- runs-on: ubuntu-latest
24
- timeout-minutes: 10
25
- services:
26
- mongodb:
27
- image: mongo:4.4
28
- ports:
29
- - 27017:27017
30
- strategy:
31
- matrix:
32
- node-version: [14.x, 16.x]
33
- steps:
34
- - uses: actions/checkout@v2
35
- - name: Use Node.js ${{ matrix.node-version }}
36
- uses: actions/setup-node@v1
37
- with:
38
- node-version: ${{ matrix.node-version }}
39
- - run: |
40
- npm install
41
- cd test
42
- npm install
43
- - name: Run test with Node.js ${{ matrix.node-version }}
44
- run: |
45
- cd test
46
- npm test
47
- coverage:
48
- needs: [test-node]
49
- runs-on: ubuntu-latest
50
- timeout-minutes: 10
51
- services:
52
- mongodb:
53
- image: mongo:4.4
54
- ports:
55
- - 27017:27017
56
- strategy:
57
- matrix:
58
- node-version: [16.x]
59
- steps:
60
- - uses: actions/checkout@v2
61
- - name: Use Node.js ${{ matrix.node-version }}
62
- uses: actions/setup-node@v1
63
- with:
64
- node-version: ${{ matrix.node-version }}
65
- - run: |
66
- npm install
67
- cd test
68
- npm install
69
- - name: Generate coverage report
70
- run: |
71
- cd test
72
- npm run coverage-ci
73
- - name: Upload coverage to Codecov
74
- uses: codecov/codecov-action@v2
75
- with:
76
- file: ./test/coverage/lcov.info
77
- fail_ci_if_error: true