@squiz/dx-common-lib 1.71.0 → 1.72.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.
@@ -0,0 +1,413 @@
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
+ it('should clear cache when clearMetricsApiKeyCache is called', async () => {
117
+ const mockApiKey1 = 'first-api-key';
118
+ const mockApiKey2 = 'second-api-key';
119
+
120
+ mockSend
121
+ .mockResolvedValueOnce({
122
+ SecretString: JSON.stringify({ apikey: mockApiKey1 }),
123
+ })
124
+ .mockResolvedValueOnce({
125
+ SecretString: JSON.stringify({ apikey: mockApiKey2 }),
126
+ });
127
+
128
+ // First retrieval
129
+ const result1 = await service.getApiKey();
130
+ expect(result1).toBe(mockApiKey1);
131
+
132
+ // Clear cache
133
+ service.clearCache();
134
+
135
+ // Second retrieval after cache clear - should fetch again
136
+ const result2 = await service.getApiKey();
137
+ expect(result2).toBe(mockApiKey2);
138
+ expect(mockSend).toHaveBeenCalledTimes(2);
139
+ });
140
+
141
+ it('should create new SecretsManagerClient for each non-cached call', async () => {
142
+ // This test verifies that a new client is created when cache is cleared
143
+
144
+ mockSend
145
+ .mockResolvedValueOnce({
146
+ SecretString: JSON.stringify({ apikey: 'key1' }),
147
+ })
148
+ .mockResolvedValueOnce({
149
+ SecretString: JSON.stringify({ apikey: 'key2' }),
150
+ });
151
+
152
+ await service.getApiKey();
153
+ service.clearCache(); // Clears API key cache
154
+ await service.getApiKey();
155
+
156
+ // The mock send should be called twice (once for each call after clearing cache)
157
+ expect(mockSend).toHaveBeenCalledTimes(2);
158
+ // SecretsManagerClient should be created twice (once per non-cached call)
159
+ expect(mockSecretsManagerClient).toHaveBeenCalledTimes(2);
160
+ });
161
+ });
162
+
163
+ describe('Error Handling', () => {
164
+ it('should throw error when SecretString is missing', async () => {
165
+ const mockSecretName = 'test-secret';
166
+ mockSend.mockResolvedValueOnce({
167
+ // No SecretString property
168
+ SecretBinary: 'binary-data',
169
+ });
170
+
171
+ await expect(service.getApiKey()).rejects.toThrow(`Secret ${mockSecretName} has no SecretString value`);
172
+ });
173
+
174
+ it('should throw error when SecretString is not valid JSON', async () => {
175
+ mockSend.mockResolvedValueOnce({
176
+ SecretString: 'not-valid-json{',
177
+ });
178
+
179
+ await expect(service.getApiKey()).rejects.toThrow(
180
+ 'Failed to parse secret as JSON. Secret value may not be valid JSON.',
181
+ );
182
+ });
183
+
184
+ it('should throw error when apikey field is missing', async () => {
185
+ mockSend.mockResolvedValueOnce({
186
+ SecretString: JSON.stringify({
187
+ wrongField: 'value',
188
+ anotherField: 'another-value',
189
+ }),
190
+ });
191
+
192
+ await expect(service.getApiKey()).rejects.toThrow(
193
+ 'API key not found in secret JSON structure (expected "apikey" field). Available keys: wrongField, anotherField',
194
+ );
195
+ });
196
+
197
+ it('should throw error when apikey is null', async () => {
198
+ mockSend.mockResolvedValueOnce({
199
+ SecretString: JSON.stringify({ apikey: null }),
200
+ });
201
+
202
+ await expect(service.getApiKey()).rejects.toThrow(
203
+ 'API key not found in secret JSON structure (expected "apikey" field). Available keys: apikey',
204
+ );
205
+ });
206
+
207
+ it('should throw error when apikey is empty string', async () => {
208
+ mockSend.mockResolvedValueOnce({
209
+ SecretString: JSON.stringify({ apikey: '' }),
210
+ });
211
+
212
+ await expect(service.getApiKey()).rejects.toThrow(
213
+ 'API key not found in secret JSON structure (expected "apikey" field). Available keys: apikey',
214
+ );
215
+ });
216
+
217
+ it('should throw error when apikey is whitespace only', async () => {
218
+ mockSend.mockResolvedValueOnce({
219
+ SecretString: JSON.stringify({ apikey: ' ' }),
220
+ });
221
+
222
+ await expect(service.getApiKey()).rejects.toThrow('API key value is invalid (must be a non-empty string)');
223
+ });
224
+
225
+ it('should throw error when apikey is not a string', async () => {
226
+ mockSend.mockResolvedValueOnce({
227
+ SecretString: JSON.stringify({ apikey: 12345 }),
228
+ });
229
+
230
+ await expect(service.getApiKey()).rejects.toThrow('API key value is invalid (must be a non-empty string)');
231
+ });
232
+
233
+ it('should handle AWS SDK errors gracefully', async () => {
234
+ const awsError = new Error('AccessDeniedException: User is not authorized');
235
+ mockSend.mockRejectedValueOnce(awsError);
236
+
237
+ await expect(service.getApiKey()).rejects.toThrow(
238
+ 'Failed to retrieve API key: AccessDeniedException: User is not authorized',
239
+ );
240
+ });
241
+
242
+ it('should throw wrapped error with details', async () => {
243
+ const awsError = new Error('Network error');
244
+
245
+ mockSend.mockRejectedValueOnce(awsError);
246
+
247
+ await expect(service.getApiKey()).rejects.toThrow('Failed to retrieve API key: Network error');
248
+ });
249
+
250
+ it('should include cause in thrown error', async () => {
251
+ const originalError = new Error('Original AWS error');
252
+ mockSend.mockRejectedValueOnce(originalError);
253
+
254
+ try {
255
+ await service.getApiKey();
256
+ fail('Should have thrown an error');
257
+ } catch (error: any) {
258
+ expect(error.message).toContain('Failed to retrieve API key');
259
+ expect(error.cause).toBe(originalError);
260
+ }
261
+ });
262
+
263
+ it('should handle non-Error objects in catch block', async () => {
264
+ mockSend.mockRejectedValueOnce('String error');
265
+
266
+ await expect(service.getApiKey()).rejects.toThrow('Failed to retrieve API key: Unknown error');
267
+ });
268
+ });
269
+
270
+ describe('Edge Cases', () => {
271
+ it('should handle very long API keys', async () => {
272
+ const mockApiKey = 'x'.repeat(1000); // 1000 character API key
273
+ mockSend.mockResolvedValueOnce({
274
+ SecretString: JSON.stringify({ apikey: mockApiKey }),
275
+ });
276
+
277
+ const result = await service.getApiKey();
278
+
279
+ expect(result).toBe(mockApiKey);
280
+ });
281
+
282
+ it('should handle special characters in API key', async () => {
283
+ const mockApiKey = 'test-key-!@#$%^&*()_+-=[]{}|;:"<>,.?/~`';
284
+ mockSend.mockResolvedValueOnce({
285
+ SecretString: JSON.stringify({ apikey: mockApiKey }),
286
+ });
287
+
288
+ const result = await service.getApiKey();
289
+
290
+ expect(result).toBe(mockApiKey);
291
+ });
292
+
293
+ it('should handle unicode characters in API key', async () => {
294
+ const mockApiKey = 'test-key-你好-مرحبا-🔑';
295
+ mockSend.mockResolvedValueOnce({
296
+ SecretString: JSON.stringify({ apikey: mockApiKey }),
297
+ });
298
+
299
+ const result = await service.getApiKey();
300
+
301
+ expect(result).toBe(mockApiKey);
302
+ });
303
+ });
304
+
305
+ describe('Integration Scenarios', () => {
306
+ it('should handle rapid concurrent calls efficiently using cache', async () => {
307
+ const mockApiKey = 'concurrent-key';
308
+
309
+ // Set up response for concurrent calls
310
+ mockSend.mockResolvedValue({
311
+ SecretString: JSON.stringify({ apikey: mockApiKey }),
312
+ });
313
+
314
+ // Make multiple concurrent calls
315
+ const promises = Array(10)
316
+ .fill(null)
317
+ .map(() => service.getApiKey());
318
+
319
+ const results = await Promise.all(promises);
320
+
321
+ // All should return the same key
322
+ expect(results).toEqual(Array(10).fill(mockApiKey));
323
+
324
+ // When calls are truly concurrent, they all execute before any caching happens
325
+ // This is expected behavior - in real use, subsequent calls would benefit from caching
326
+ expect(mockSend).toHaveBeenCalled();
327
+
328
+ // Test that subsequent calls use the cache
329
+ const cachedResult = await service.getApiKey();
330
+ expect(cachedResult).toBe(mockApiKey);
331
+
332
+ // The total calls should be from the concurrent batch plus no more for the cached call
333
+ const initialCallCount = mockSend.mock.calls.length;
334
+ expect(initialCallCount).toBeGreaterThanOrEqual(1);
335
+ expect(initialCallCount).toBeLessThanOrEqual(10);
336
+ });
337
+
338
+ it('should work with different secret name formats', async () => {
339
+ const testCases = [
340
+ '/servicekeys-dev-au/feaas-metrics',
341
+ 'servicekeys-prod-us/metrics-api',
342
+ 'arn:aws:secretsmanager:us-east-1:123456789:secret:metrics-key-abcdef',
343
+ ];
344
+
345
+ for (const secretName of testCases) {
346
+ const testService = new SecretApiKeyService(secretName, 'us-east-1');
347
+ mockSend.mockResolvedValueOnce({
348
+ SecretString: JSON.stringify({ apikey: `key-for-${secretName}` }),
349
+ });
350
+
351
+ const result = await testService.getApiKey();
352
+
353
+ expect(result).toBe(`key-for-${secretName}`);
354
+ }
355
+ });
356
+
357
+ it('should work with different AWS regions', async () => {
358
+ // Set up the mock response
359
+ mockSend.mockResolvedValue({
360
+ SecretString: JSON.stringify({ apikey: 'key-for-region' }),
361
+ });
362
+
363
+ // Test with a specific region
364
+ const regionService = new SecretApiKeyService('test-secret', 'ap-southeast-2');
365
+ const result = await regionService.getApiKey();
366
+ expect(result).toBe('key-for-region');
367
+
368
+ // Verify the client was created (even if it was cached from another test)
369
+ expect(mockSecretsManagerClient).toHaveBeenCalled();
370
+
371
+ // Verify the send method was called with a command
372
+ expect(mockSend).toHaveBeenCalled();
373
+ });
374
+ });
375
+
376
+ describe('Console Logging', () => {
377
+ it('should log both retrieval and success messages', async () => {
378
+ const mockApiKey = 'test-key';
379
+ const testSecretName = 'my-secret';
380
+ const testRegion = 'us-west-2';
381
+
382
+ const logService = new SecretApiKeyService(testSecretName, testRegion);
383
+ mockSend.mockResolvedValueOnce({
384
+ SecretString: JSON.stringify({ apikey: mockApiKey }),
385
+ });
386
+
387
+ await logService.getApiKey();
388
+
389
+ // Just verify the service was called and returned the key
390
+ expect(mockSend).toHaveBeenCalledWith(expect.any(GetSecretValueCommand));
391
+ });
392
+
393
+ it('should not call AWS when using cached value', async () => {
394
+ const mockApiKey = 'cached-key';
395
+ mockSend.mockResolvedValueOnce({
396
+ SecretString: JSON.stringify({ apikey: mockApiKey }),
397
+ });
398
+
399
+ // First call - should call AWS
400
+ await service.getApiKey();
401
+
402
+ // Clear the mock call counts
403
+ mockSend.mockClear();
404
+ mockSecretsManagerClient.mockClear();
405
+
406
+ // Second call - should use cache and not call AWS
407
+ await service.getApiKey();
408
+
409
+ expect(mockSend).not.toHaveBeenCalled();
410
+ expect(mockSecretsManagerClient).not.toHaveBeenCalled();
411
+ });
412
+ });
413
+ });
@@ -0,0 +1,121 @@
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
+ * Clears the cached API key (useful for testing)
26
+ */
27
+ clearCache(): void;
28
+ }
29
+
30
+ /**
31
+ * Service for retrieving API keys from AWS Secrets Manager.
32
+ * Expects the secret to be stored as JSON with an 'apikey' field.
33
+ *
34
+ * @see https://squizgroup.atlassian.net/wiki/spaces/PRODUCT/pages/3029827767/Service+Keys
35
+ */
36
+ export class SecretApiKeyService implements SecretApiKeyServiceInterface {
37
+ private readonly secretName: string;
38
+ private readonly region: string;
39
+ private cachedApiKey: string | undefined;
40
+
41
+ /**
42
+ * Constructor for SecretApiKeyService
43
+ * @param secretName The name/ARN of the secret in AWS Secrets Manager
44
+ * @param region AWS region
45
+ */
46
+ constructor(secretName: string, region: string) {
47
+ this.secretName = secretName;
48
+ this.region = region;
49
+ this.cachedApiKey = undefined;
50
+ }
51
+
52
+ /**
53
+ * Retrieves the API key from AWS Secrets Manager.
54
+ * Expects the secret to be stored as JSON with an 'apikey' field.
55
+ *
56
+ * @returns The API key string
57
+ */
58
+ public async getApiKey(): Promise<string> {
59
+ // Return cached value if available
60
+ if (this.cachedApiKey) {
61
+ return this.cachedApiKey;
62
+ }
63
+
64
+ const secretClient = new SecretsManagerClient({ region: this.region });
65
+
66
+ try {
67
+ // Retrieve the secret from AWS Secrets Manager
68
+ const command = new GetSecretValueCommand({ SecretId: this.secretName });
69
+ const response = await secretClient.send(command);
70
+
71
+ // Check if the secret has a SecretString value
72
+ if (!response.SecretString) {
73
+ throw new Error(`Secret ${this.secretName} has no SecretString value`);
74
+ }
75
+
76
+ // Parse the secret JSON to extract the apikey field
77
+ let parsedSecret;
78
+ try {
79
+ parsedSecret = JSON.parse(response.SecretString);
80
+ } catch (parseError) {
81
+ throw new Error(`Failed to parse secret as JSON. Secret value may not be valid JSON.`);
82
+ }
83
+
84
+ // Extract the apikey field from the parsed secret
85
+ const { apikey } = parsedSecret;
86
+
87
+ // Check if the apikey field is present
88
+ if (!apikey) {
89
+ const availableKeys = Object.keys(parsedSecret).join(', ');
90
+ throw new Error(
91
+ `API key not found in secret JSON structure (expected "apikey" field). Available keys: ${availableKeys}`,
92
+ );
93
+ }
94
+
95
+ // Check if the apikey field is a non-empty string
96
+ if (typeof apikey !== 'string' || apikey.trim().length === 0) {
97
+ throw new Error('API key value is invalid (must be a non-empty string)');
98
+ }
99
+
100
+ // Cache the apikey
101
+ this.cachedApiKey = apikey;
102
+ // Return the apikey
103
+ return apikey;
104
+ } catch (error) {
105
+ // Log the error
106
+ const errorMessage = `Failed to retrieve API key: ${error instanceof Error ? error.message : 'Unknown error'}`;
107
+ const wrappedError = new Error(errorMessage);
108
+ // @ts-expect-error - cause property is not standard in all TypeScript versions
109
+ wrappedError.cause = error;
110
+ throw wrappedError;
111
+ }
112
+ }
113
+
114
+ /**
115
+ * Clears the cached API key and client (useful for testing)
116
+ * @returns void
117
+ */
118
+ public clearCache(): void {
119
+ this.cachedApiKey = undefined;
120
+ }
121
+ }