@squiz/dx-common-lib 1.71.1 → 1.72.1

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.
Files changed (32) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/lib/cache/applyDefaultRulesToCacheControlObject.d.ts +1 -1
  3. package/lib/index.d.ts +3 -0
  4. package/lib/index.js +3 -0
  5. package/lib/index.js.map +1 -1
  6. package/lib/secret-api-key-service/DevSecretApiKeyService.d.ts +25 -0
  7. package/lib/secret-api-key-service/DevSecretApiKeyService.js +35 -0
  8. package/lib/secret-api-key-service/DevSecretApiKeyService.js.map +1 -0
  9. package/lib/secret-api-key-service/DevSecretApiKeyService.spec.d.ts +1 -0
  10. package/lib/secret-api-key-service/DevSecretApiKeyService.spec.js +157 -0
  11. package/lib/secret-api-key-service/DevSecretApiKeyService.spec.js.map +1 -0
  12. package/lib/secret-api-key-service/SecretApiKeyService.d.ts +43 -0
  13. package/lib/secret-api-key-service/SecretApiKeyService.js +85 -0
  14. package/lib/secret-api-key-service/SecretApiKeyService.js.map +1 -0
  15. package/lib/secret-api-key-service/SecretApiKeyService.spec.d.ts +1 -0
  16. package/lib/secret-api-key-service/SecretApiKeyService.spec.js +280 -0
  17. package/lib/secret-api-key-service/SecretApiKeyService.spec.js.map +1 -0
  18. package/lib/secret-api-key-service/getSecretApiKeyService.d.ts +20 -0
  19. package/lib/secret-api-key-service/getSecretApiKeyService.js +41 -0
  20. package/lib/secret-api-key-service/getSecretApiKeyService.js.map +1 -0
  21. package/lib/secret-api-key-service/getSecretApiKeyService.spec.d.ts +1 -0
  22. package/lib/secret-api-key-service/getSecretApiKeyService.spec.js +313 -0
  23. package/lib/secret-api-key-service/getSecretApiKeyService.spec.js.map +1 -0
  24. package/package.json +4 -4
  25. package/src/index.ts +3 -0
  26. package/src/secret-api-key-service/DevSecretApiKeyService.spec.ts +211 -0
  27. package/src/secret-api-key-service/DevSecretApiKeyService.ts +36 -0
  28. package/src/secret-api-key-service/SecretApiKeyService.spec.ts +367 -0
  29. package/src/secret-api-key-service/SecretApiKeyService.ts +108 -0
  30. package/src/secret-api-key-service/getSecretApiKeyService.spec.ts +405 -0
  31. package/src/secret-api-key-service/getSecretApiKeyService.ts +45 -0
  32. package/tsconfig.tsbuildinfo +1 -1
@@ -0,0 +1,367 @@
1
+ import { SecretsManagerClient, GetSecretValueCommand } from '@aws-sdk/client-secrets-manager';
2
+ import { SecretApiKeyService } from './SecretApiKeyService';
3
+
4
+ // Mock the AWS SDK
5
+ jest.mock('@aws-sdk/client-secrets-manager');
6
+
7
+ describe('SecretApiKeyService', () => {
8
+ let service: SecretApiKeyService;
9
+ let mockSend: jest.Mock;
10
+ const mockSecretsManagerClient = SecretsManagerClient as jest.MockedClass<typeof SecretsManagerClient>;
11
+ let consoleLogSpy: jest.SpyInstance;
12
+ let consoleErrorSpy: jest.SpyInstance;
13
+ const mockSecretName = 'test-secret';
14
+ const mockRegion = 'us-east-1';
15
+
16
+ beforeEach(() => {
17
+ // Create a fresh service instance for each test
18
+ service = new SecretApiKeyService(mockSecretName, mockRegion);
19
+
20
+ // Create a fresh mock for each test
21
+ mockSend = jest.fn();
22
+
23
+ // Setup console spies
24
+ consoleLogSpy = jest.spyOn(console, 'log').mockImplementation();
25
+ consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation();
26
+
27
+ // Clear and setup the mock client - make sure it always returns our mockSend
28
+ mockSecretsManagerClient.mockReset();
29
+ mockSecretsManagerClient.mockImplementation(
30
+ () =>
31
+ ({
32
+ send: mockSend,
33
+ destroy: jest.fn(),
34
+ config: {},
35
+ } as any),
36
+ );
37
+ });
38
+
39
+ afterEach(() => {
40
+ // Restore console spies
41
+ consoleLogSpy.mockRestore();
42
+ consoleErrorSpy.mockRestore();
43
+ jest.clearAllMocks();
44
+ });
45
+
46
+ describe('AWS Secrets Manager Retrieval', () => {
47
+ it('should successfully retrieve API key from Secrets Manager', async () => {
48
+ const mockApiKey = 'test-api-key-from-secrets';
49
+ const testSecretName = '/servicekeys-dev-au/feaas-metrics';
50
+ const testRegion = 'ap-southeast-2';
51
+
52
+ // Create service with specific secret name and region for this test
53
+ const testService = new SecretApiKeyService(testSecretName, testRegion);
54
+
55
+ mockSend.mockResolvedValueOnce({
56
+ SecretString: JSON.stringify({ apikey: mockApiKey }),
57
+ });
58
+
59
+ const result = await testService.getApiKey();
60
+
61
+ expect(result).toBe(mockApiKey);
62
+ expect(mockSecretsManagerClient).toHaveBeenCalledWith({ region: testRegion });
63
+ expect(mockSend).toHaveBeenCalledWith(expect.any(GetSecretValueCommand));
64
+ });
65
+
66
+ it('should handle complex JSON structure with apikey field', async () => {
67
+ const mockApiKey = 'complex-api-key';
68
+ mockSend.mockResolvedValueOnce({
69
+ SecretString: JSON.stringify({
70
+ apikey: mockApiKey,
71
+ otherField: 'other-value',
72
+ metadata: { created: '2024-01-01' },
73
+ }),
74
+ });
75
+
76
+ const result = await service.getApiKey();
77
+
78
+ expect(result).toBe(mockApiKey);
79
+ });
80
+
81
+ it('should handle SecretString with nested apikey value', async () => {
82
+ const mockApiKey = 'nested-api-key';
83
+ mockSend.mockResolvedValueOnce({
84
+ SecretString: JSON.stringify({
85
+ apikey: mockApiKey,
86
+ // Test that we extract the right field even with similar names
87
+ apikeys: ['wrong-key'],
88
+ api_key: 'also-wrong',
89
+ }),
90
+ });
91
+
92
+ const result = await service.getApiKey();
93
+
94
+ expect(result).toBe(mockApiKey);
95
+ });
96
+ });
97
+
98
+ describe('Caching Behavior', () => {
99
+ it('should cache API key after first successful retrieval', async () => {
100
+ const mockApiKey = 'cached-api-key';
101
+ mockSend.mockResolvedValueOnce({
102
+ SecretString: JSON.stringify({ apikey: mockApiKey }),
103
+ });
104
+
105
+ // First call - should hit Secrets Manager
106
+ const result1 = await service.getApiKey();
107
+ expect(result1).toBe(mockApiKey);
108
+ expect(mockSend).toHaveBeenCalledTimes(1);
109
+
110
+ // Second call - should use cache
111
+ const result2 = await service.getApiKey();
112
+ expect(result2).toBe(mockApiKey);
113
+ expect(mockSend).toHaveBeenCalledTimes(1); // Still only 1 call
114
+ });
115
+ });
116
+
117
+ describe('Error Handling', () => {
118
+ it('should throw error when SecretString is missing', async () => {
119
+ const mockSecretName = 'test-secret';
120
+ mockSend.mockResolvedValueOnce({
121
+ // No SecretString property
122
+ SecretBinary: 'binary-data',
123
+ });
124
+
125
+ await expect(service.getApiKey()).rejects.toThrow(`Secret ${mockSecretName} has no SecretString value`);
126
+ });
127
+
128
+ it('should throw error when SecretString is not valid JSON', async () => {
129
+ mockSend.mockResolvedValueOnce({
130
+ SecretString: 'not-valid-json{',
131
+ });
132
+
133
+ await expect(service.getApiKey()).rejects.toThrow(
134
+ 'Failed to parse secret as JSON. Secret value may not be valid JSON.',
135
+ );
136
+ });
137
+
138
+ it('should throw error when apikey field is missing', async () => {
139
+ mockSend.mockResolvedValueOnce({
140
+ SecretString: JSON.stringify({
141
+ wrongField: 'value',
142
+ anotherField: 'another-value',
143
+ }),
144
+ });
145
+
146
+ await expect(service.getApiKey()).rejects.toThrow(
147
+ 'API key not found in secret JSON structure (expected "apikey" field). Available keys: wrongField, anotherField',
148
+ );
149
+ });
150
+
151
+ it('should throw error when apikey is null', async () => {
152
+ mockSend.mockResolvedValueOnce({
153
+ SecretString: JSON.stringify({ apikey: null }),
154
+ });
155
+
156
+ await expect(service.getApiKey()).rejects.toThrow(
157
+ 'API key not found in secret JSON structure (expected "apikey" field). Available keys: apikey',
158
+ );
159
+ });
160
+
161
+ it('should throw error when apikey is empty string', async () => {
162
+ mockSend.mockResolvedValueOnce({
163
+ SecretString: JSON.stringify({ apikey: '' }),
164
+ });
165
+
166
+ await expect(service.getApiKey()).rejects.toThrow(
167
+ 'API key not found in secret JSON structure (expected "apikey" field). Available keys: apikey',
168
+ );
169
+ });
170
+
171
+ it('should throw error when apikey is whitespace only', async () => {
172
+ mockSend.mockResolvedValueOnce({
173
+ SecretString: JSON.stringify({ apikey: ' ' }),
174
+ });
175
+
176
+ await expect(service.getApiKey()).rejects.toThrow('API key value is invalid (must be a non-empty string)');
177
+ });
178
+
179
+ it('should throw error when apikey is not a string', async () => {
180
+ mockSend.mockResolvedValueOnce({
181
+ SecretString: JSON.stringify({ apikey: 12345 }),
182
+ });
183
+
184
+ await expect(service.getApiKey()).rejects.toThrow('API key value is invalid (must be a non-empty string)');
185
+ });
186
+
187
+ it('should handle AWS SDK errors gracefully', async () => {
188
+ const awsError = new Error('AccessDeniedException: User is not authorized');
189
+ mockSend.mockRejectedValueOnce(awsError);
190
+
191
+ await expect(service.getApiKey()).rejects.toThrow(
192
+ 'Failed to retrieve API key: AccessDeniedException: User is not authorized',
193
+ );
194
+ });
195
+
196
+ it('should throw wrapped error with details', async () => {
197
+ const awsError = new Error('Network error');
198
+
199
+ mockSend.mockRejectedValueOnce(awsError);
200
+
201
+ await expect(service.getApiKey()).rejects.toThrow('Failed to retrieve API key: Network error');
202
+ });
203
+
204
+ it('should include cause in thrown error', async () => {
205
+ const originalError = new Error('Original AWS error');
206
+ mockSend.mockRejectedValueOnce(originalError);
207
+
208
+ try {
209
+ await service.getApiKey();
210
+ fail('Should have thrown an error');
211
+ } catch (error: any) {
212
+ expect(error.message).toContain('Failed to retrieve API key');
213
+ expect(error.cause).toBe(originalError);
214
+ }
215
+ });
216
+
217
+ it('should handle non-Error objects in catch block', async () => {
218
+ mockSend.mockRejectedValueOnce('String error');
219
+
220
+ await expect(service.getApiKey()).rejects.toThrow('Failed to retrieve API key: Unknown error');
221
+ });
222
+ });
223
+
224
+ describe('Edge Cases', () => {
225
+ it('should handle very long API keys', async () => {
226
+ const mockApiKey = 'x'.repeat(1000); // 1000 character API key
227
+ mockSend.mockResolvedValueOnce({
228
+ SecretString: JSON.stringify({ apikey: mockApiKey }),
229
+ });
230
+
231
+ const result = await service.getApiKey();
232
+
233
+ expect(result).toBe(mockApiKey);
234
+ });
235
+
236
+ it('should handle special characters in API key', async () => {
237
+ const mockApiKey = 'test-key-!@#$%^&*()_+-=[]{}|;:"<>,.?/~`';
238
+ mockSend.mockResolvedValueOnce({
239
+ SecretString: JSON.stringify({ apikey: mockApiKey }),
240
+ });
241
+
242
+ const result = await service.getApiKey();
243
+
244
+ expect(result).toBe(mockApiKey);
245
+ });
246
+
247
+ it('should handle unicode characters in API key', async () => {
248
+ const mockApiKey = 'test-key-你好-مرحبا-🔑';
249
+ mockSend.mockResolvedValueOnce({
250
+ SecretString: JSON.stringify({ apikey: mockApiKey }),
251
+ });
252
+
253
+ const result = await service.getApiKey();
254
+
255
+ expect(result).toBe(mockApiKey);
256
+ });
257
+ });
258
+
259
+ describe('Integration Scenarios', () => {
260
+ it('should handle rapid concurrent calls efficiently using cache', async () => {
261
+ const mockApiKey = 'concurrent-key';
262
+
263
+ // Set up response for concurrent calls
264
+ mockSend.mockResolvedValue({
265
+ SecretString: JSON.stringify({ apikey: mockApiKey }),
266
+ });
267
+
268
+ // Make multiple concurrent calls
269
+ const promises = Array(10)
270
+ .fill(null)
271
+ .map(() => service.getApiKey());
272
+
273
+ const results = await Promise.all(promises);
274
+
275
+ // All should return the same key
276
+ expect(results).toEqual(Array(10).fill(mockApiKey));
277
+
278
+ // When calls are truly concurrent, they all execute before any caching happens
279
+ // This is expected behavior - in real use, subsequent calls would benefit from caching
280
+ expect(mockSend).toHaveBeenCalled();
281
+
282
+ // Test that subsequent calls use the cache
283
+ const cachedResult = await service.getApiKey();
284
+ expect(cachedResult).toBe(mockApiKey);
285
+
286
+ // The total calls should be from the concurrent batch plus no more for the cached call
287
+ const initialCallCount = mockSend.mock.calls.length;
288
+ expect(initialCallCount).toBeGreaterThanOrEqual(1);
289
+ expect(initialCallCount).toBeLessThanOrEqual(10);
290
+ });
291
+
292
+ it('should work with different secret name formats', async () => {
293
+ const testCases = [
294
+ '/servicekeys-dev-au/feaas-metrics',
295
+ 'servicekeys-prod-us/metrics-api',
296
+ 'arn:aws:secretsmanager:us-east-1:123456789:secret:metrics-key-abcdef',
297
+ ];
298
+
299
+ for (const secretName of testCases) {
300
+ const testService = new SecretApiKeyService(secretName, 'us-east-1');
301
+ mockSend.mockResolvedValueOnce({
302
+ SecretString: JSON.stringify({ apikey: `key-for-${secretName}` }),
303
+ });
304
+
305
+ const result = await testService.getApiKey();
306
+
307
+ expect(result).toBe(`key-for-${secretName}`);
308
+ }
309
+ });
310
+
311
+ it('should work with different AWS regions', async () => {
312
+ // Set up the mock response
313
+ mockSend.mockResolvedValue({
314
+ SecretString: JSON.stringify({ apikey: 'key-for-region' }),
315
+ });
316
+
317
+ // Test with a specific region
318
+ const regionService = new SecretApiKeyService('test-secret', 'ap-southeast-2');
319
+ const result = await regionService.getApiKey();
320
+ expect(result).toBe('key-for-region');
321
+
322
+ // Verify the client was created (even if it was cached from another test)
323
+ expect(mockSecretsManagerClient).toHaveBeenCalled();
324
+
325
+ // Verify the send method was called with a command
326
+ expect(mockSend).toHaveBeenCalled();
327
+ });
328
+ });
329
+
330
+ describe('Console Logging', () => {
331
+ it('should log both retrieval and success messages', async () => {
332
+ const mockApiKey = 'test-key';
333
+ const testSecretName = 'my-secret';
334
+ const testRegion = 'us-west-2';
335
+
336
+ const logService = new SecretApiKeyService(testSecretName, testRegion);
337
+ mockSend.mockResolvedValueOnce({
338
+ SecretString: JSON.stringify({ apikey: mockApiKey }),
339
+ });
340
+
341
+ await logService.getApiKey();
342
+
343
+ // Just verify the service was called and returned the key
344
+ expect(mockSend).toHaveBeenCalledWith(expect.any(GetSecretValueCommand));
345
+ });
346
+
347
+ it('should not call AWS when using cached value', async () => {
348
+ const mockApiKey = 'cached-key';
349
+ mockSend.mockResolvedValueOnce({
350
+ SecretString: JSON.stringify({ apikey: mockApiKey }),
351
+ });
352
+
353
+ // First call - should call AWS
354
+ await service.getApiKey();
355
+
356
+ // Clear the mock call counts
357
+ mockSend.mockClear();
358
+ mockSecretsManagerClient.mockClear();
359
+
360
+ // Second call - should use cache and not call AWS
361
+ await service.getApiKey();
362
+
363
+ expect(mockSend).not.toHaveBeenCalled();
364
+ expect(mockSecretsManagerClient).not.toHaveBeenCalled();
365
+ });
366
+ });
367
+ });
@@ -0,0 +1,108 @@
1
+ /*!
2
+ * @file SecretApiKeyService.ts
3
+ * @description Retrieves API keys from AWS Secrets Manager.
4
+ * @author Squiz
5
+ * @copyright 2025 Squiz
6
+ * @license MIT
7
+ */
8
+
9
+ // External
10
+ import { SecretsManagerClient, GetSecretValueCommand } from '@aws-sdk/client-secrets-manager';
11
+
12
+ /**
13
+ * Interface for the SecretApiKeyService
14
+ */
15
+ export interface SecretApiKeyServiceInterface {
16
+ /**
17
+ * Retrieves the API key from AWS Secrets Manager.
18
+ * Expects the secret to be stored as JSON with an 'apikey' field.
19
+ *
20
+ * @returns The API key string
21
+ */
22
+ getApiKey(): Promise<string>;
23
+ }
24
+
25
+ /**
26
+ * Service for retrieving API keys from AWS Secrets Manager.
27
+ * Expects the secret to be stored as JSON with an 'apikey' field.
28
+ *
29
+ * @see https://squizgroup.atlassian.net/wiki/spaces/PRODUCT/pages/3029827767/Service+Keys
30
+ */
31
+ export class SecretApiKeyService implements SecretApiKeyServiceInterface {
32
+ private readonly secretName: string;
33
+ private readonly region: string;
34
+ private cachedApiKey: string | undefined;
35
+
36
+ /**
37
+ * Constructor for SecretApiKeyService
38
+ * @param secretName The name/ARN of the secret in AWS Secrets Manager
39
+ * @param region AWS region
40
+ */
41
+ constructor(secretName: string, region: string) {
42
+ this.secretName = secretName;
43
+ this.region = region;
44
+ this.cachedApiKey = undefined;
45
+ }
46
+
47
+ /**
48
+ * Retrieves the API key from AWS Secrets Manager.
49
+ * Expects the secret to be stored as JSON with an 'apikey' field.
50
+ *
51
+ * @returns The API key string
52
+ */
53
+ public async getApiKey(): Promise<string> {
54
+ // Return cached value if available
55
+ if (this.cachedApiKey) {
56
+ return this.cachedApiKey;
57
+ }
58
+
59
+ const secretClient = new SecretsManagerClient({ region: this.region });
60
+
61
+ try {
62
+ // Retrieve the secret from AWS Secrets Manager
63
+ const command = new GetSecretValueCommand({ SecretId: this.secretName });
64
+ const response = await secretClient.send(command);
65
+
66
+ // Check if the secret has a SecretString value
67
+ if (!response.SecretString) {
68
+ throw new Error(`Secret ${this.secretName} has no SecretString value`);
69
+ }
70
+
71
+ // Parse the secret JSON to extract the apikey field
72
+ let parsedSecret;
73
+ try {
74
+ parsedSecret = JSON.parse(response.SecretString);
75
+ } catch (parseError) {
76
+ throw new Error(`Failed to parse secret as JSON. Secret value may not be valid JSON.`);
77
+ }
78
+
79
+ // Extract the apikey field from the parsed secret
80
+ const { apikey } = parsedSecret;
81
+
82
+ // Check if the apikey field is present
83
+ if (!apikey) {
84
+ const availableKeys = Object.keys(parsedSecret).join(', ');
85
+ throw new Error(
86
+ `API key not found in secret JSON structure (expected "apikey" field). Available keys: ${availableKeys}`,
87
+ );
88
+ }
89
+
90
+ // Check if the apikey field is a non-empty string
91
+ if (typeof apikey !== 'string' || apikey.trim().length === 0) {
92
+ throw new Error('API key value is invalid (must be a non-empty string)');
93
+ }
94
+
95
+ // Cache the apikey
96
+ this.cachedApiKey = apikey;
97
+ // Return the apikey
98
+ return apikey;
99
+ } catch (error) {
100
+ // Log the error
101
+ const errorMessage = `Failed to retrieve API key: ${error instanceof Error ? error.message : 'Unknown error'}`;
102
+ const wrappedError = new Error(errorMessage);
103
+ // @ts-expect-error - cause property is not standard in all TypeScript versions
104
+ wrappedError.cause = error;
105
+ throw wrappedError;
106
+ }
107
+ }
108
+ }