@squiz/dx-common-lib 1.39.1-alpha.11 → 1.39.1-alpha.14
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/.npm/_logs/{2023-06-27T01_19_15_613Z-debug-0.log → 2023-06-27T07_36_54_939Z-debug-0.log} +15 -15
- package/lib/api-key-validation/ApiKeyValidationService.d.ts +8 -3
- package/lib/api-key-validation/CloudflareApiKeyService.d.ts +9 -4
- package/lib/api-key-validation/CloudflareApiKeyService.js +24 -12
- package/lib/api-key-validation/CloudflareApiKeyService.js.map +1 -1
- package/lib/api-key-validation/CloudflareApiKeyService.spec.js +26 -23
- package/lib/api-key-validation/CloudflareApiKeyService.spec.js.map +1 -1
- package/lib/api-key-validation/DevelopmentApiKeyService.d.ts +8 -3
- package/lib/api-key-validation/DevelopmentApiKeyService.js +14 -5
- package/lib/api-key-validation/DevelopmentApiKeyService.js.map +1 -1
- package/lib/api-key-validation/DevelopmentApiKeyService.spec.js +10 -6
- package/lib/api-key-validation/DevelopmentApiKeyService.spec.js.map +1 -1
- package/lib/index.d.ts +0 -1
- package/lib/index.js +0 -1
- package/lib/index.js.map +1 -1
- package/package.json +4 -4
- package/src/api-key-validation/ApiKeyValidationService.ts +9 -3
- package/src/api-key-validation/CloudflareApiKeyService.spec.ts +38 -24
- package/src/api-key-validation/CloudflareApiKeyService.ts +27 -13
- package/src/api-key-validation/DevelopmentApiKeyService.spec.ts +12 -6
- package/src/api-key-validation/DevelopmentApiKeyService.ts +16 -5
- package/src/index.ts +0 -1
- package/tsconfig.tsbuildinfo +1 -1
- package/lib/server-utils/apiKeyMiddleware.d.ts +0 -6
- package/lib/server-utils/apiKeyMiddleware.js +0 -23
- package/lib/server-utils/apiKeyMiddleware.js.map +0 -1
- package/lib/server-utils/apiKeyMiddleware.spec.d.ts +0 -1
- package/lib/server-utils/apiKeyMiddleware.spec.js +0 -40
- package/lib/server-utils/apiKeyMiddleware.spec.js.map +0 -1
- package/src/server-utils/apiKeyMiddleware.spec.ts +0 -51
- package/src/server-utils/apiKeyMiddleware.ts +0 -26
package/.npm/_logs/{2023-06-27T01_19_15_613Z-debug-0.log → 2023-06-27T07_36_54_939Z-debug-0.log}
RENAMED
@@ -2,11 +2,11 @@
|
|
2
2
|
1 info using npm@9.5.0
|
3
3
|
2 info using node@v18.15.0
|
4
4
|
3 timing npm:load:whichnode Completed in 1ms
|
5
|
-
4 timing config:load:defaults Completed in
|
6
|
-
5 timing config:load:file:/usr/local/lib/node_modules/npm/npmrc Completed in
|
7
|
-
6 timing config:load:builtin Completed in
|
5
|
+
4 timing config:load:defaults Completed in 2ms
|
6
|
+
5 timing config:load:file:/usr/local/lib/node_modules/npm/npmrc Completed in 1ms
|
7
|
+
6 timing config:load:builtin Completed in 1ms
|
8
8
|
7 timing config:load:cli Completed in 2ms
|
9
|
-
8 timing config:load:env Completed in
|
9
|
+
8 timing config:load:env Completed in 1ms
|
10
10
|
9 info found workspace root at /builds/developer-experience/cmp
|
11
11
|
10 timing config:load:file:/builds/developer-experience/cmp/.npmrc Completed in 0ms
|
12
12
|
11 timing config:load:project Completed in 24ms
|
@@ -15,23 +15,23 @@
|
|
15
15
|
14 timing config:load:file:/usr/local/etc/npmrc Completed in 0ms
|
16
16
|
15 timing config:load:global Completed in 0ms
|
17
17
|
16 timing config:load:setEnvs Completed in 1ms
|
18
|
-
17 timing config:load Completed in
|
19
|
-
18 timing npm:load:configload Completed in
|
18
|
+
17 timing config:load Completed in 33ms
|
19
|
+
18 timing npm:load:configload Completed in 33ms
|
20
20
|
19 timing npm:load:mkdirpcache Completed in 0ms
|
21
21
|
20 timing npm:load:mkdirplogs Completed in 0ms
|
22
22
|
21 verbose title npm run compile
|
23
23
|
22 verbose argv "run" "compile" "--"
|
24
|
-
23 timing npm:load:setTitle Completed in
|
25
|
-
24 timing config:load:flatten Completed in
|
26
|
-
25 timing npm:load:display Completed in
|
27
|
-
26 verbose logfile logs-max:10 dir:/builds/developer-experience/cmp/packages/dx-common-lib/.npm/_logs/2023-06-
|
28
|
-
27 verbose logfile /builds/developer-experience/cmp/packages/dx-common-lib/.npm/_logs/2023-06-
|
24
|
+
23 timing npm:load:setTitle Completed in 2ms
|
25
|
+
24 timing config:load:flatten Completed in 4ms
|
26
|
+
25 timing npm:load:display Completed in 5ms
|
27
|
+
26 verbose logfile logs-max:10 dir:/builds/developer-experience/cmp/packages/dx-common-lib/.npm/_logs/2023-06-27T07_36_54_939Z-
|
28
|
+
27 verbose logfile /builds/developer-experience/cmp/packages/dx-common-lib/.npm/_logs/2023-06-27T07_36_54_939Z-debug-0.log
|
29
29
|
28 timing npm:load:logFile Completed in 3ms
|
30
|
-
29 timing npm:load:timers Completed in
|
30
|
+
29 timing npm:load:timers Completed in 0ms
|
31
31
|
30 timing npm:load:configScope Completed in 0ms
|
32
|
-
31 timing npm:load Completed in
|
32
|
+
31 timing npm:load Completed in 46ms
|
33
33
|
32 silly logfile done cleaning log files
|
34
|
-
33 timing command:run Completed in
|
34
|
+
33 timing command:run Completed in 3895ms
|
35
35
|
34 verbose exit 0
|
36
|
-
35 timing npm Completed in
|
36
|
+
35 timing npm Completed in 3952ms
|
37
37
|
36 info ok
|
@@ -1,5 +1,10 @@
|
|
1
|
+
/// <reference types="node" />
|
2
|
+
import { IncomingMessage } from 'http';
|
1
3
|
export interface ApiKeyValidationService {
|
2
|
-
|
3
|
-
|
4
|
-
|
4
|
+
interServiceKeyIsValid(key: string | undefined): boolean;
|
5
|
+
/**
|
6
|
+
* @deprecated Suggest moving towards using JWT authentication.
|
7
|
+
*/
|
8
|
+
matrixKeyIsValid(key: string | undefined, request: IncomingMessage): boolean;
|
9
|
+
getInterServiceKeys(): string[];
|
5
10
|
}
|
@@ -1,18 +1,23 @@
|
|
1
|
+
/// <reference types="node" />
|
1
2
|
import { SecretsManagerClient } from '@aws-sdk/client-secrets-manager';
|
2
3
|
import { ApiKeyValidationService } from './ApiKeyValidationService';
|
3
4
|
import { Logger } from '@squiz/dx-logger-lib';
|
5
|
+
import { IncomingMessage } from 'http';
|
4
6
|
export interface CloudFlareKeys {
|
5
|
-
keys: string[];
|
6
7
|
'matrix-keys': string[];
|
8
|
+
'inter-service-keys': string[];
|
7
9
|
}
|
8
10
|
export declare class CloudflareApiKeyService implements ApiKeyValidationService {
|
9
11
|
protected secretName: string;
|
10
12
|
protected secretsClient: SecretsManagerClient;
|
11
13
|
protected logger: Logger;
|
12
14
|
constructor(secretName: string, logger?: Logger);
|
13
|
-
|
14
|
-
|
15
|
+
/**
|
16
|
+
* @deprecated Suggest moving towards using JWT authentication.
|
17
|
+
*/
|
18
|
+
matrixKeyIsValid(key: string, request: IncomingMessage): boolean;
|
19
|
+
interServiceKeyIsValid(key: string): boolean;
|
15
20
|
protected getValidApiKeys(): Promise<CloudFlareKeys>;
|
16
21
|
refreshApiKeys(): Promise<void>;
|
17
|
-
|
22
|
+
getInterServiceKeys(): string[];
|
18
23
|
}
|
@@ -5,8 +5,8 @@ const client_secrets_manager_1 = require("@aws-sdk/client-secrets-manager");
|
|
5
5
|
const UnAuthenticatedRequestError_1 = require("../error/UnAuthenticatedRequestError");
|
6
6
|
const dx_logger_lib_1 = require("@squiz/dx-logger-lib");
|
7
7
|
let validKeys = {
|
8
|
-
keys: [],
|
9
8
|
'matrix-keys': [],
|
9
|
+
'inter-service-keys': [],
|
10
10
|
};
|
11
11
|
let refreshInterval;
|
12
12
|
// number must be smaller than 24 days.
|
@@ -23,17 +23,28 @@ class CloudflareApiKeyService {
|
|
23
23
|
}
|
24
24
|
this.logger = logger;
|
25
25
|
}
|
26
|
-
|
27
|
-
|
26
|
+
/**
|
27
|
+
* @deprecated Suggest moving towards using JWT authentication.
|
28
|
+
*/
|
29
|
+
matrixKeyIsValid(key, request) {
|
30
|
+
var _a;
|
31
|
+
if (validKeys['matrix-keys'].length == 0) {
|
28
32
|
throw new UnAuthenticatedRequestError_1.UnAuthenticatedRequestError('No api keys to check against');
|
29
33
|
}
|
30
|
-
|
34
|
+
if (validKeys['matrix-keys'].includes(key)) {
|
35
|
+
this.logger.info('Request authenticated using legacy Matrix API key', {
|
36
|
+
url: request.url,
|
37
|
+
userAgent: (_a = request.headers) === null || _a === void 0 ? void 0 : _a['user-agent'],
|
38
|
+
});
|
39
|
+
return true;
|
40
|
+
}
|
41
|
+
return false;
|
31
42
|
}
|
32
|
-
|
33
|
-
if (validKeys['
|
34
|
-
throw new UnAuthenticatedRequestError_1.UnAuthenticatedRequestError('No api keys to check against');
|
43
|
+
interServiceKeyIsValid(key) {
|
44
|
+
if (validKeys['inter-service-keys'].length == 0) {
|
45
|
+
throw new UnAuthenticatedRequestError_1.UnAuthenticatedRequestError('No inter service api keys to check against');
|
35
46
|
}
|
36
|
-
return validKeys['
|
47
|
+
return validKeys['inter-service-keys'].includes(key);
|
37
48
|
}
|
38
49
|
async getValidApiKeys() {
|
39
50
|
let secretValue;
|
@@ -51,7 +62,7 @@ class CloudflareApiKeyService {
|
|
51
62
|
throw new Error('cloudflare api key SecretString undefined');
|
52
63
|
}
|
53
64
|
const secret = JSON.parse(secretValue.SecretString);
|
54
|
-
if (secret
|
65
|
+
if (secret['matrix-keys'] && secret['inter-service-keys']) {
|
55
66
|
return secret;
|
56
67
|
}
|
57
68
|
throw new Error('api keys retrieved and decoded successfully but contained no values');
|
@@ -63,7 +74,8 @@ class CloudflareApiKeyService {
|
|
63
74
|
async refreshApiKeys() {
|
64
75
|
this.logger.info('refreshing keys');
|
65
76
|
validKeys = await this.getValidApiKeys();
|
66
|
-
this.logger.info(`
|
77
|
+
this.logger.info(`found ${validKeys['matrix-keys'].length} valid matrix keys`);
|
78
|
+
this.logger.info(`found ${validKeys['inter-service-keys'].length} valid inter service keys`);
|
67
79
|
if (!refreshInterval) {
|
68
80
|
refreshInterval = setInterval(async () => {
|
69
81
|
await this.refreshApiKeys();
|
@@ -71,8 +83,8 @@ class CloudflareApiKeyService {
|
|
71
83
|
}
|
72
84
|
}
|
73
85
|
// temporary method, to be removed when DXP auth is properly implemented
|
74
|
-
|
75
|
-
return validKeys['
|
86
|
+
getInterServiceKeys() {
|
87
|
+
return validKeys['inter-service-keys'];
|
76
88
|
}
|
77
89
|
}
|
78
90
|
exports.CloudflareApiKeyService = CloudflareApiKeyService;
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"CloudflareApiKeyService.js","sourceRoot":"","sources":["../../src/api-key-validation/CloudflareApiKeyService.ts"],"names":[],"mappings":";;;AAAA,4EAIyC;AACzC,sFAAmF;AAEnF,wDAAyD;
|
1
|
+
{"version":3,"file":"CloudflareApiKeyService.js","sourceRoot":"","sources":["../../src/api-key-validation/CloudflareApiKeyService.ts"],"names":[],"mappings":";;;AAAA,4EAIyC;AACzC,sFAAmF;AAEnF,wDAAyD;AAQzD,IAAI,SAAS,GAAmB;IAC9B,aAAa,EAAE,EAAE;IACjB,oBAAoB,EAAE,EAAE;CACzB,CAAC;AAEF,IAAI,eAA+C,CAAC;AACpD,uCAAuC;AACvC,2EAA2E;AAC3E,MAAM,aAAa,GAAG,CAAC,GAAG,EAAE,GAAG,IAAI,GAAG,IAAI,CAAC,CAAC,qCAAqC;AAEjF,MAAa,uBAAuB;IAIlC,YAAsB,UAAkB,EAAE,MAAe;QAAnC,eAAU,GAAV,UAAU,CAAQ;QACtC,IAAI,CAAC,aAAa,GAAG,IAAI,6CAAoB,CAAC;YAC5C,MAAM,EAAE,gBAAgB;SACzB,CAAC,CAAC;QAEH,IAAI,CAAC,MAAM,EAAE;YACX,MAAM,GAAG,IAAA,yBAAS,EAAC,EAAE,IAAI,EAAE,yBAAyB,EAAE,CAAC,CAAC;SACzD;QAED,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;IACvB,CAAC;IAED;;OAEG;IACI,gBAAgB,CAAC,GAAW,EAAE,OAAwB;;QAC3D,IAAI,SAAS,CAAC,aAAa,CAAC,CAAC,MAAM,IAAI,CAAC,EAAE;YACxC,MAAM,IAAI,yDAA2B,CAAC,8BAA8B,CAAC,CAAC;SACvE;QAED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C,qEAAqE,CAAC,CAAC;SACxF;QAAC,OAAO,CAAC,EAAE;YACV,MAAM,IAAI,KAAK,CAAC,4CAA4C,CAAC,CAAC;SAC/D;IACH,CAAC;IAEM,KAAK,CAAC,cAAc;QACzB,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,iBAAiB,CAAC,CAAC;QACpC,SAAS,GAAG,MAAM,IAAI,CAAC,eAAe,EAAE,CAAC;QAEzC,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,SAAS,SAAS,CAAC,aAAa,CAAC,CAAC,MAAM,oBAAoB,CAAC,CAAC;QAC/E,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,SAAS,SAAS,CAAC,oBAAoB,CAAC,CAAC,MAAM,2BAA2B,CAAC,CAAC;QAE7F,IAAI,CAAC,eAAe,EAAE;YACpB,eAAe,GAAG,WAAW,CAAC,KAAK,IAAI,EAAE;gBACvC,MAAM,IAAI,CAAC,cAAc,EAAE,CAAC;YAC9B,CAAC,EAAE,aAAa,CAAC,CAAC;SACnB;IACH,CAAC;IAED,wEAAwE;IACjE,mBAAmB;QACxB,OAAO,SAAS,CAAC,oBAAoB,CAAC,CAAC;IACzC,CAAC;CACF;AA5FD,0DA4FC"}
|
@@ -15,14 +15,14 @@ describe('CloudflareApiKeyService', () => {
|
|
15
15
|
});
|
16
16
|
it('should make a request to the aws secrets manager when refreshing the keys', async () => {
|
17
17
|
sendSpy.mockClear();
|
18
|
-
sendSpy.mockImplementationOnce(() => Promise.resolve({ SecretString: '{"keys":[]}' }));
|
18
|
+
sendSpy.mockImplementationOnce(() => Promise.resolve({ SecretString: '{"matrix-keys":[], "inter-service-keys":[]}' }));
|
19
19
|
await service.refreshApiKeys();
|
20
20
|
expect(sendSpy.mock.lastCall[0].input).toEqual({ SecretId: 'my-secret-name' });
|
21
21
|
expect(sendSpy.mock.lastCall[0]).toBeInstanceOf(client_secrets_manager_1.GetSecretValueCommand);
|
22
22
|
});
|
23
23
|
it('should start a refresh timer once called, but only once', (done) => {
|
24
24
|
sendSpy.mockClear();
|
25
|
-
sendSpy.mockImplementationOnce(() => Promise.resolve({ SecretString: '{"keys":["my-new-key"]}' }));
|
25
|
+
sendSpy.mockImplementationOnce(() => Promise.resolve({ SecretString: '{"matrix-keys":[], "inter-service-keys":["my-new-key"]}' }));
|
26
26
|
const spy = jest.spyOn(service, 'refreshApiKeys');
|
27
27
|
expect(spy).not.toBeCalled();
|
28
28
|
expect(setInterval).toHaveBeenCalledTimes(1); // for the above 2 tests
|
@@ -30,62 +30,65 @@ describe('CloudflareApiKeyService', () => {
|
|
30
30
|
expect(spy).toHaveBeenCalled();
|
31
31
|
jest.useRealTimers();
|
32
32
|
setTimeout(() => {
|
33
|
-
expect(service.
|
33
|
+
expect(service.interServiceKeyIsValid('my-new-key')).toEqual(true);
|
34
34
|
done();
|
35
35
|
}, 5);
|
36
36
|
});
|
37
37
|
});
|
38
|
-
describe('
|
38
|
+
describe('matrixKeyIsValid', () => {
|
39
39
|
describe('when there are no api keys', () => {
|
40
40
|
beforeAll(async () => {
|
41
|
-
sendSpy.mockImplementation(() => Promise.resolve({ SecretString: '{"keys":[]}' }));
|
41
|
+
sendSpy.mockImplementation(() => Promise.resolve({ SecretString: '{"matrix-keys":[], "inter-service-keys":[]}' }));
|
42
42
|
await service.refreshApiKeys();
|
43
43
|
});
|
44
44
|
it('should throw an unauthenticated request error when validating a key if there are no keys to validate against', () => {
|
45
|
-
expect(() => service.
|
45
|
+
expect(() => service.matrixKeyIsValid('anything', {})).toThrowError(new UnAuthenticatedRequestError_1.UnAuthenticatedRequestError('No api keys to check against'));
|
46
46
|
});
|
47
47
|
});
|
48
48
|
describe('when there are api keys', () => {
|
49
49
|
beforeAll(async () => {
|
50
|
-
sendSpy.mockImplementation(() => Promise.resolve({
|
50
|
+
sendSpy.mockImplementation(() => Promise.resolve({
|
51
|
+
SecretString: '{"matrix-keys":["my-matrix-key"], "inter-service-keys":["my-inter-service-key"]}',
|
52
|
+
}));
|
51
53
|
await service.refreshApiKeys();
|
52
54
|
});
|
53
|
-
it('should return true when there are keys loaded and the input key is in the list of valid keys', () => {
|
54
|
-
expect(service.
|
55
|
-
expect(service.keyIsValid('my-other-key')).toEqual(true);
|
55
|
+
it('should return true when there are keys loaded and the input key is in the list of valid matrix keys', () => {
|
56
|
+
expect(service.matrixKeyIsValid('my-matrix-key', {})).toEqual(true);
|
56
57
|
});
|
57
|
-
it('should return false when there are keys loaded and the input key is not in the list of valid keys', async () => {
|
58
|
-
expect(service.
|
58
|
+
it('should return false when there are keys loaded and the input key is not in the list of valid matrix keys', async () => {
|
59
|
+
expect(service.matrixKeyIsValid('not-my-key', {})).toEqual(false);
|
59
60
|
});
|
60
|
-
it('should return false
|
61
|
-
expect(service.
|
61
|
+
it('should return false when using non matrix keys', async () => {
|
62
|
+
expect(service.matrixKeyIsValid('my-inter-service-key', {})).toEqual(false);
|
62
63
|
});
|
63
64
|
});
|
64
65
|
});
|
65
|
-
describe('
|
66
|
+
describe('interServiceKeyIsValid', () => {
|
66
67
|
describe('when there are no api keys', () => {
|
67
68
|
beforeAll(async () => {
|
68
|
-
sendSpy.mockImplementation(() => Promise.resolve({ SecretString: '{"keys":[], "
|
69
|
+
sendSpy.mockImplementation(() => Promise.resolve({ SecretString: '{"matrix-keys":[], "inter-service-keys":[]}' }));
|
69
70
|
await service.refreshApiKeys();
|
70
71
|
});
|
71
72
|
it('should throw an unauthenticated request error when validating a key if there are no keys to validate against', () => {
|
72
|
-
expect(() => service.
|
73
|
+
expect(() => service.interServiceKeyIsValid('anything')).toThrowError(new UnAuthenticatedRequestError_1.UnAuthenticatedRequestError('No inter service api keys to check against'));
|
73
74
|
});
|
74
75
|
});
|
75
76
|
describe('when there are api keys', () => {
|
76
77
|
beforeAll(async () => {
|
77
|
-
sendSpy.mockImplementation(() => Promise.resolve({
|
78
|
+
sendSpy.mockImplementation(() => Promise.resolve({
|
79
|
+
SecretString: '{"keys":["my-key", "my-other-key"], "matrix-keys":["my-matrix-key"], "inter-service-keys":["my-inter-service-key"]}',
|
80
|
+
}));
|
78
81
|
await service.refreshApiKeys();
|
79
82
|
});
|
80
83
|
it('should return true when there are keys loaded and the input key is in the list of valid matrix keys', () => {
|
81
|
-
expect(service.
|
84
|
+
expect(service.interServiceKeyIsValid('my-inter-service-key')).toEqual(true);
|
82
85
|
});
|
83
86
|
it('should return false when there are keys loaded and the input key is not in the list of valid matrix keys', async () => {
|
84
|
-
expect(service.
|
87
|
+
expect(service.interServiceKeyIsValid('not-my-key')).toEqual(false);
|
85
88
|
});
|
86
|
-
it('should return false when using non
|
87
|
-
expect(service.
|
88
|
-
expect(service.
|
89
|
+
it('should return false when using non inter-service keys', async () => {
|
90
|
+
expect(service.interServiceKeyIsValid('my-key')).toEqual(false);
|
91
|
+
expect(service.interServiceKeyIsValid('my-matrix-key')).toEqual(false);
|
89
92
|
});
|
90
93
|
});
|
91
94
|
});
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"CloudflareApiKeyService.spec.js","sourceRoot":"","sources":["../../src/api-key-validation/CloudflareApiKeyService.spec.ts"],"names":[],"mappings":";;AAAA,4EAA8F;AAC9F,sFAAmF;AACnF,uEAAoE;
|
1
|
+
{"version":3,"file":"CloudflareApiKeyService.spec.js","sourceRoot":"","sources":["../../src/api-key-validation/CloudflareApiKeyService.spec.ts"],"names":[],"mappings":";;AAAA,4EAA8F;AAC9F,sFAAmF;AACnF,uEAAoE;AAGpE,MAAM,OAAO,GAAG,IAAI,CAAC,KAAK,CAAC,6CAAoB,CAAC,SAAS,EAAE,MAAM,CAAC,CAAC;AAEnE,IAAI,CAAC,aAAa,EAAE,CAAC;AACrB,IAAI,CAAC,KAAK,CAAC,MAAM,EAAE,aAAa,CAAC,CAAC;AAElC,QAAQ,CAAC,yBAAyB,EAAE,GAAG,EAAE;IACvC,MAAM,OAAO,GAAG,IAAI,iDAAuB,CAAC,gBAAgB,CAAC,CAAC;IAE9D,QAAQ,CAAC,gBAAgB,EAAE,GAAG,EAA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AAC,CAAC;QAEH,QAAQ,CAAC,yBAAyB,EAAE,GAAG,EAAE;YACvC,SAAS,CAAC,KAAK,IAAI,EAAE;gBACnB,OAAO,CAAC,kBAAkB,CAAC,GAAG,EAAE,CAC9B,OAAO,CAAC,OAAO,CAAC;oBACd,YAAY,EACV,qHAAqH;iBACxH,CAAC,CACH,CAAC;gBACF,MAAM,OAAO,CAAC,cAAc,EAAE,CAAC;YACjC,CAAC,CAAC,CAAC;YAEH,EAAE,CAAC,qGAAqG,EAAE,GAAG,EAAE;gBAC7G,MAAM,CAAC,OAAO,CAAC,sBAAsB,CAAC,sBAAsB,CAAC,CAAC,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;YAC/E,CAAC,CAAC,CAAC;YAEH,EAAE,CAAC,0GAA0G,EAAE,KAAK,IAAI,EAAE;gBACxH,MAAM,CAAC,OAAO,CAAC,sBAAsB,CAAC,YAAY,CAAC,CAAC,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;YACtE,CAAC,CAAC,CAAC;YAEH,EAAE,CAAC,uDAAuD,EAAE,KAAK,IAAI,EAAE;gBACrE,MAAM,CAAC,OAAO,CAAC,sBAAsB,CAAC,QAAQ,CAAC,CAAC,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;gBAChE,MAAM,CAAC,OAAO,CAAC,sBAAsB,CAAC,eAAe,CAAC,CAAC,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;YACzE,CAAC,CAAC,CAAC;QACL,CAAC,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"}
|
@@ -1,6 +1,11 @@
|
|
1
|
+
/// <reference types="node" />
|
2
|
+
import { Logger } from '@squiz/dx-logger-lib';
|
1
3
|
import { ApiKeyValidationService } from './ApiKeyValidationService';
|
4
|
+
import { IncomingMessage } from 'http';
|
2
5
|
export declare class DevelopmentApiKeyService implements ApiKeyValidationService {
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
+
private logger;
|
7
|
+
constructor(logger?: Logger);
|
8
|
+
matrixKeyIsValid(key: string | undefined, request: IncomingMessage): boolean;
|
9
|
+
interServiceKeyIsValid(key: string | undefined): boolean;
|
10
|
+
getInterServiceKeys(): string[];
|
6
11
|
}
|
@@ -1,16 +1,25 @@
|
|
1
1
|
"use strict";
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
3
|
exports.DevelopmentApiKeyService = void 0;
|
4
|
+
const dx_logger_lib_1 = require("@squiz/dx-logger-lib");
|
4
5
|
class DevelopmentApiKeyService {
|
5
|
-
|
6
|
-
|
6
|
+
constructor(logger) {
|
7
|
+
this.logger = logger || (0, dx_logger_lib_1.getLogger)({ name: 'DevelopmentApiKeyService' });
|
7
8
|
}
|
8
|
-
matrixKeyIsValid() {
|
9
|
+
matrixKeyIsValid(key, request) {
|
10
|
+
var _a;
|
11
|
+
this.logger.info('Request authenticated using legacy Matrix API key', {
|
12
|
+
url: request.url,
|
13
|
+
userAgent: (_a = request.headers) === null || _a === void 0 ? void 0 : _a['user-agent'],
|
14
|
+
});
|
9
15
|
return true;
|
10
16
|
}
|
17
|
+
interServiceKeyIsValid(key) {
|
18
|
+
return Boolean(key && this.getInterServiceKeys().includes(key));
|
19
|
+
}
|
11
20
|
// temporary method, to be removed when DXP auth is properly implemented
|
12
|
-
|
13
|
-
return ['
|
21
|
+
getInterServiceKeys() {
|
22
|
+
return ['inter-service-api-key'];
|
14
23
|
}
|
15
24
|
}
|
16
25
|
exports.DevelopmentApiKeyService = DevelopmentApiKeyService;
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"DevelopmentApiKeyService.js","sourceRoot":"","sources":["../../src/api-key-validation/DevelopmentApiKeyService.ts"],"names":[],"mappings":";;;
|
1
|
+
{"version":3,"file":"DevelopmentApiKeyService.js","sourceRoot":"","sources":["../../src/api-key-validation/DevelopmentApiKeyService.ts"],"names":[],"mappings":";;;AAAA,wDAAyD;AAIzD,MAAa,wBAAwB;IAGnC,YAAmB,MAAe;QAChC,IAAI,CAAC,MAAM,GAAG,MAAM,IAAI,IAAA,yBAAS,EAAC,EAAE,IAAI,EAAE,0BAA0B,EAAE,CAAC,CAAC;IAC1E,CAAC;IACM,gBAAgB,CAAC,GAAuB,EAAE,OAAwB;;QACvE,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,mDAAmD,EAAE;YACpE,GAAG,EAAE,OAAO,CAAC,GAAG;YAChB,SAAS,EAAE,MAAA,OAAO,CAAC,OAAO,0CAAG,YAAY,CAAC;SAC3C,CAAC,CAAC;QACH,OAAO,IAAI,CAAC;IACd,CAAC;IACM,sBAAsB,CAAC,GAAuB;QACnD,OAAO,OAAO,CAAC,GAAG,IAAI,IAAI,CAAC,mBAAmB,EAAE,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC,CAAC;IAClE,CAAC;IACD,wEAAwE;IACjE,mBAAmB;QACxB,OAAO,CAAC,uBAAuB,CAAC,CAAC;IACnC,CAAC;CACF;AApBD,4DAoBC"}
|
@@ -1,16 +1,20 @@
|
|
1
1
|
"use strict";
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
3
|
const DevelopmentApiKeyService_1 = require("./DevelopmentApiKeyService");
|
4
|
+
const dx_logger_lib_1 = require("@squiz/dx-logger-lib");
|
4
5
|
describe('DevelopmentApiKeyService', () => {
|
5
|
-
const service = new DevelopmentApiKeyService_1.DevelopmentApiKeyService();
|
6
|
-
describe('
|
6
|
+
const service = new DevelopmentApiKeyService_1.DevelopmentApiKeyService((0, dx_logger_lib_1.getLogger)({ name: 'test', silent: true }));
|
7
|
+
describe('matrixKeyIsValid', () => {
|
7
8
|
it('should always return true', () => {
|
8
|
-
expect(service.
|
9
|
+
expect(service.matrixKeyIsValid(undefined, {})).toEqual(true);
|
9
10
|
});
|
10
11
|
});
|
11
|
-
describe('
|
12
|
-
it('should
|
13
|
-
expect(service.
|
12
|
+
describe('interServiceKeyIsValid', () => {
|
13
|
+
it('should return true if key is "inter-service-api-key"', () => {
|
14
|
+
expect(service.interServiceKeyIsValid('inter-service-api-key')).toEqual(true);
|
15
|
+
});
|
16
|
+
it('should return false if key is not "inter-service-api-key"', () => {
|
17
|
+
expect(service.interServiceKeyIsValid('invalid-api-key')).toEqual(false);
|
14
18
|
});
|
15
19
|
});
|
16
20
|
});
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"DevelopmentApiKeyService.spec.js","sourceRoot":"","sources":["../../src/api-key-validation/DevelopmentApiKeyService.spec.ts"],"names":[],"mappings":";;AAAA,yEAAsE;AAEtE,QAAQ,CAAC,0BAA0B,EAAE,GAAG,EAAE;IACxC,MAAM,OAAO,GAAG,IAAI,mDAAwB,EAAE,CAAC;
|
1
|
+
{"version":3,"file":"DevelopmentApiKeyService.spec.js","sourceRoot":"","sources":["../../src/api-key-validation/DevelopmentApiKeyService.spec.ts"],"names":[],"mappings":";;AAAA,yEAAsE;AAEtE,wDAAiD;AAEjD,QAAQ,CAAC,0BAA0B,EAAE,GAAG,EAAE;IACxC,MAAM,OAAO,GAAG,IAAI,mDAAwB,CAAC,IAAA,yBAAS,EAAC,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,CAAC,CAAC,CAAC;IAExF,QAAQ,CAAC,kBAAkB,EAAE,GAAG,EAAE;QAChC,EAAE,CAAC,2BAA2B,EAAE,GAAG,EAAE;YACnC,MAAM,CAAC,OAAO,CAAC,gBAAgB,CAAC,SAAS,EAAE,EAAqB,CAAC,CAAC,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;QACnF,CAAC,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;IAEH,QAAQ,CAAC,wBAAwB,EAAE,GAAG,EAAE;QACtC,EAAE,CAAC,sDAAsD,EAAE,GAAG,EAAE;YAC9D,MAAM,CAAC,OAAO,CAAC,sBAAsB,CAAC,uBAAuB,CAAC,CAAC,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;QAChF,CAAC,CAAC,CAAC;QAEH,EAAE,CAAC,2DAA2D,EAAE,GAAG,EAAE;YACnE,MAAM,CAAC,OAAO,CAAC,sBAAsB,CAAC,iBAAiB,CAAC,CAAC,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;QAC3E,CAAC,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"}
|
package/lib/index.d.ts
CHANGED
@@ -8,7 +8,6 @@ export * from './api-key-validation/ApiKeyValidationService';
|
|
8
8
|
export * from './api-key-validation/CloudflareApiKeyService';
|
9
9
|
export * from './api-key-validation/DevelopmentApiKeyService';
|
10
10
|
export * from './api-key-validation/getApiKeyService';
|
11
|
-
export * from './server-utils/apiKeyMiddleware';
|
12
11
|
export * from './cache';
|
13
12
|
export * from './formatted-text/formattedTextToHtmlString';
|
14
13
|
export * from './json-order';
|
package/lib/index.js
CHANGED
@@ -24,7 +24,6 @@ __exportStar(require("./api-key-validation/ApiKeyValidationService"), exports);
|
|
24
24
|
__exportStar(require("./api-key-validation/CloudflareApiKeyService"), exports);
|
25
25
|
__exportStar(require("./api-key-validation/DevelopmentApiKeyService"), exports);
|
26
26
|
__exportStar(require("./api-key-validation/getApiKeyService"), exports);
|
27
|
-
__exportStar(require("./server-utils/apiKeyMiddleware"), exports);
|
28
27
|
__exportStar(require("./cache"), exports);
|
29
28
|
__exportStar(require("./formatted-text/formattedTextToHtmlString"), exports);
|
30
29
|
__exportStar(require("./json-order"), exports);
|
package/lib/index.js.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,gDAA8B;AAC9B,0CAAwB;AACxB,yCAAuB;AACvB,qDAAmC;AACnC,iEAA+C;AAC/C,+DAA6C;AAC7C,+EAA6D;AAC7D,+EAA6D;AAC7D,gFAA8D;AAC9D,wEAAsD;AACtD,
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,gDAA8B;AAC9B,0CAAwB;AACxB,yCAAuB;AACvB,qDAAmC;AACnC,iEAA+C;AAC/C,+DAA6C;AAC7C,+EAA6D;AAC7D,+EAA6D;AAC7D,gFAA8D;AAC9D,wEAAsD;AACtD,0CAAwB;AACxB,6EAA2D;AAC3D,+CAA6B"}
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@squiz/dx-common-lib",
|
3
|
-
"version": "1.39.1-alpha.
|
3
|
+
"version": "1.39.1-alpha.14",
|
4
4
|
"description": "",
|
5
5
|
"main": "lib/index.js",
|
6
6
|
"scripts": {
|
@@ -13,8 +13,8 @@
|
|
13
13
|
"license": "ISC",
|
14
14
|
"dependencies": {
|
15
15
|
"@aws-sdk/client-secrets-manager": "3.218.0",
|
16
|
-
"@squiz/dx-json-schema-lib": "1.39.1-alpha.
|
17
|
-
"@squiz/dx-logger-lib": "1.39.1-alpha.
|
16
|
+
"@squiz/dx-json-schema-lib": "1.39.1-alpha.14",
|
17
|
+
"@squiz/dx-logger-lib": "1.39.1-alpha.14",
|
18
18
|
"archiver": "5.3.1",
|
19
19
|
"escape-string-regexp": "4.0.0",
|
20
20
|
"fs-extra": "11.1.0",
|
@@ -35,5 +35,5 @@
|
|
35
35
|
"ts-node": "10.9.1",
|
36
36
|
"typescript": "4.9.4"
|
37
37
|
},
|
38
|
-
"gitHead": "
|
38
|
+
"gitHead": "258c9a26d3df69659115b7c74a79aed405e4bf5a"
|
39
39
|
}
|
@@ -1,5 +1,11 @@
|
|
1
|
+
import { IncomingMessage } from 'http';
|
2
|
+
|
1
3
|
export interface ApiKeyValidationService {
|
2
|
-
|
3
|
-
|
4
|
-
|
4
|
+
interServiceKeyIsValid(key: string | undefined): boolean;
|
5
|
+
|
6
|
+
/**
|
7
|
+
* @deprecated Suggest moving towards using JWT authentication.
|
8
|
+
*/
|
9
|
+
matrixKeyIsValid(key: string | undefined, request: IncomingMessage): boolean;
|
10
|
+
getInterServiceKeys(): string[];
|
5
11
|
}
|
@@ -1,6 +1,7 @@
|
|
1
1
|
import { GetSecretValueCommand, SecretsManagerClient } from '@aws-sdk/client-secrets-manager';
|
2
2
|
import { UnAuthenticatedRequestError } from '../error/UnAuthenticatedRequestError';
|
3
3
|
import { CloudflareApiKeyService } from './CloudflareApiKeyService';
|
4
|
+
import { IncomingMessage } from 'http';
|
4
5
|
|
5
6
|
const sendSpy = jest.spyOn(SecretsManagerClient.prototype, 'send');
|
6
7
|
|
@@ -21,7 +22,9 @@ describe('CloudflareApiKeyService', () => {
|
|
21
22
|
|
22
23
|
it('should make a request to the aws secrets manager when refreshing the keys', async () => {
|
23
24
|
sendSpy.mockClear();
|
24
|
-
sendSpy.mockImplementationOnce(() =>
|
25
|
+
sendSpy.mockImplementationOnce(() =>
|
26
|
+
Promise.resolve({ SecretString: '{"matrix-keys":[], "inter-service-keys":[]}' }),
|
27
|
+
);
|
25
28
|
await service.refreshApiKeys();
|
26
29
|
|
27
30
|
expect(sendSpy.mock.lastCall[0].input).toEqual({ SecretId: 'my-secret-name' });
|
@@ -30,7 +33,9 @@ describe('CloudflareApiKeyService', () => {
|
|
30
33
|
|
31
34
|
it('should start a refresh timer once called, but only once', (done) => {
|
32
35
|
sendSpy.mockClear();
|
33
|
-
sendSpy.mockImplementationOnce(() =>
|
36
|
+
sendSpy.mockImplementationOnce(() =>
|
37
|
+
Promise.resolve({ SecretString: '{"matrix-keys":[], "inter-service-keys":["my-new-key"]}' }),
|
38
|
+
);
|
34
39
|
const spy = jest.spyOn(service, 'refreshApiKeys');
|
35
40
|
|
36
41
|
expect(spy).not.toBeCalled();
|
@@ -41,21 +46,23 @@ describe('CloudflareApiKeyService', () => {
|
|
41
46
|
|
42
47
|
jest.useRealTimers();
|
43
48
|
setTimeout(() => {
|
44
|
-
expect(service.
|
49
|
+
expect(service.interServiceKeyIsValid('my-new-key')).toEqual(true);
|
45
50
|
done();
|
46
51
|
}, 5);
|
47
52
|
});
|
48
53
|
});
|
49
54
|
|
50
|
-
describe('
|
55
|
+
describe('matrixKeyIsValid', () => {
|
51
56
|
describe('when there are no api keys', () => {
|
52
57
|
beforeAll(async () => {
|
53
|
-
sendSpy.mockImplementation(() =>
|
58
|
+
sendSpy.mockImplementation(() =>
|
59
|
+
Promise.resolve({ SecretString: '{"matrix-keys":[], "inter-service-keys":[]}' }),
|
60
|
+
);
|
54
61
|
await service.refreshApiKeys();
|
55
62
|
});
|
56
63
|
|
57
64
|
it('should throw an unauthenticated request error when validating a key if there are no keys to validate against', () => {
|
58
|
-
expect(() => service.
|
65
|
+
expect(() => service.matrixKeyIsValid('anything', {} as IncomingMessage)).toThrowError(
|
59
66
|
new UnAuthenticatedRequestError('No api keys to check against'),
|
60
67
|
);
|
61
68
|
});
|
@@ -64,35 +71,39 @@ describe('CloudflareApiKeyService', () => {
|
|
64
71
|
describe('when there are api keys', () => {
|
65
72
|
beforeAll(async () => {
|
66
73
|
sendSpy.mockImplementation(() =>
|
67
|
-
Promise.resolve({
|
74
|
+
Promise.resolve({
|
75
|
+
SecretString: '{"matrix-keys":["my-matrix-key"], "inter-service-keys":["my-inter-service-key"]}',
|
76
|
+
}),
|
68
77
|
);
|
69
78
|
await service.refreshApiKeys();
|
70
79
|
});
|
71
|
-
|
72
|
-
|
73
|
-
expect(service.
|
80
|
+
|
81
|
+
it('should return true when there are keys loaded and the input key is in the list of valid matrix keys', () => {
|
82
|
+
expect(service.matrixKeyIsValid('my-matrix-key', {} as IncomingMessage)).toEqual(true);
|
74
83
|
});
|
75
84
|
|
76
|
-
it('should return false when there are keys loaded and the input key is not in the list of valid keys', async () => {
|
77
|
-
expect(service.
|
85
|
+
it('should return false when there are keys loaded and the input key is not in the list of valid matrix keys', async () => {
|
86
|
+
expect(service.matrixKeyIsValid('not-my-key', {} as IncomingMessage)).toEqual(false);
|
78
87
|
});
|
79
88
|
|
80
|
-
it('should return false
|
81
|
-
expect(service.
|
89
|
+
it('should return false when using non matrix keys', async () => {
|
90
|
+
expect(service.matrixKeyIsValid('my-inter-service-key', {} as IncomingMessage)).toEqual(false);
|
82
91
|
});
|
83
92
|
});
|
84
93
|
});
|
85
94
|
|
86
|
-
describe('
|
95
|
+
describe('interServiceKeyIsValid', () => {
|
87
96
|
describe('when there are no api keys', () => {
|
88
97
|
beforeAll(async () => {
|
89
|
-
sendSpy.mockImplementation(() =>
|
98
|
+
sendSpy.mockImplementation(() =>
|
99
|
+
Promise.resolve({ SecretString: '{"matrix-keys":[], "inter-service-keys":[]}' }),
|
100
|
+
);
|
90
101
|
await service.refreshApiKeys();
|
91
102
|
});
|
92
103
|
|
93
104
|
it('should throw an unauthenticated request error when validating a key if there are no keys to validate against', () => {
|
94
|
-
expect(() => service.
|
95
|
-
new UnAuthenticatedRequestError('No api keys to check against'),
|
105
|
+
expect(() => service.interServiceKeyIsValid('anything')).toThrowError(
|
106
|
+
new UnAuthenticatedRequestError('No inter service api keys to check against'),
|
96
107
|
);
|
97
108
|
});
|
98
109
|
});
|
@@ -100,22 +111,25 @@ describe('CloudflareApiKeyService', () => {
|
|
100
111
|
describe('when there are api keys', () => {
|
101
112
|
beforeAll(async () => {
|
102
113
|
sendSpy.mockImplementation(() =>
|
103
|
-
Promise.resolve({
|
114
|
+
Promise.resolve({
|
115
|
+
SecretString:
|
116
|
+
'{"keys":["my-key", "my-other-key"], "matrix-keys":["my-matrix-key"], "inter-service-keys":["my-inter-service-key"]}',
|
117
|
+
}),
|
104
118
|
);
|
105
119
|
await service.refreshApiKeys();
|
106
120
|
});
|
107
121
|
|
108
122
|
it('should return true when there are keys loaded and the input key is in the list of valid matrix keys', () => {
|
109
|
-
expect(service.
|
123
|
+
expect(service.interServiceKeyIsValid('my-inter-service-key')).toEqual(true);
|
110
124
|
});
|
111
125
|
|
112
126
|
it('should return false when there are keys loaded and the input key is not in the list of valid matrix keys', async () => {
|
113
|
-
expect(service.
|
127
|
+
expect(service.interServiceKeyIsValid('not-my-key')).toEqual(false);
|
114
128
|
});
|
115
129
|
|
116
|
-
it('should return false when using non
|
117
|
-
expect(service.
|
118
|
-
expect(service.
|
130
|
+
it('should return false when using non inter-service keys', async () => {
|
131
|
+
expect(service.interServiceKeyIsValid('my-key')).toEqual(false);
|
132
|
+
expect(service.interServiceKeyIsValid('my-matrix-key')).toEqual(false);
|
119
133
|
});
|
120
134
|
});
|
121
135
|
});
|