@defra/lis-hubs-infra-core 0.22.0 → 0.23.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
@@ -102,7 +102,9 @@ class CattleHomeBe4FeClient extends BaseClient {
102
102
 
103
103
  `_get`, `_post`, `_put`, `_patch` and `_delete` take a path and
104
104
  [Wreck](https://hapi.dev/module/wreck/) options. If a service token can't be obtained, they throw
105
- `ServiceTokenError`.
105
+ `ServiceTokenError`. Other thrown errors carry `statusCode`, `serviceName`, `method`, `path` and `cause`
106
+ for code that catches the error. The shared logger emits only the error's type, message and stack,
107
+ so a caller that wants them in a log line must put them in the message or `logger.context`.
106
108
 
107
109
  Locally, lis-dev's `lis-fake-sts` service stands in for AWS STS. See
108
110
  [Service-to-service auth](https://github.com/DEFRA/lis-dev#service-to-service-auth) in lis-dev.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@defra/lis-hubs-infra-core",
3
- "version": "0.22.0",
3
+ "version": "0.23.0",
4
4
  "description": "Shared hub mechanics for livestock platform.",
5
5
  "main": "src/index.js",
6
6
  "repository": {
@@ -33,6 +33,7 @@
33
33
  "dependencies": {
34
34
  "@aws-sdk/client-sts": "3.1143.0",
35
35
  "@elastic/ecs-pino-format": "1.5.0",
36
+ "@hapi/boom": "10.0.1",
36
37
  "@hapi/h2o2": "10.0.4",
37
38
  "@hapi/wreck": "^18.1.2",
38
39
  "hapi-pino": "13.0.0",
@@ -178,25 +178,35 @@ export class BaseClient {
178
178
  if (error instanceof ServiceTokenError) {
179
179
  throw error
180
180
  }
181
- throw this.#parseError(error.output?.statusCode, error.data?.payload)
181
+ throw this.#parseError(
182
+ { method, path, cause: error },
183
+ error.output?.statusCode,
184
+ error.data?.payload
185
+ )
182
186
  }
183
187
 
184
188
  let payload
185
189
 
186
190
  try {
187
191
  payload = await Wreck.read(res, options)
188
- } catch {
189
- throw this.#parseError(res.statusCode)
192
+ } catch (error) {
193
+ throw this.#parseError({ method, path, cause: error }, res.statusCode)
190
194
  }
191
195
 
192
196
  if (res.statusCode >= BAD_REQUEST) {
193
- throw this.#parseError(res.statusCode, payload)
197
+ throw this.#parseError({ method, path }, res.statusCode, payload)
194
198
  }
195
199
 
196
200
  return { res, payload }
197
201
  }
198
202
 
199
- #parseError(statusCode, payload) {
203
+ /**
204
+ * @param {{ method: string, path: string, cause?: Error }} request the failed call, and the underlying error if there was one
205
+ * @param {number} [statusCode]
206
+ * @param {object} [payload] the upstream error body
207
+ * @returns {Error} an error carrying statusCode, serviceName, method, path and cause, for whoever logs it
208
+ */
209
+ #parseError({ method, path, cause }, statusCode, payload) {
200
210
  let message
201
211
 
202
212
  if (payload?.error?.code) {
@@ -209,8 +219,11 @@ export class BaseClient {
209
219
  message = `Request failed - ${statusCode}`
210
220
  }
211
221
 
212
- const error = new Error(message)
222
+ const error = new Error(message, cause && { cause })
213
223
  error.statusCode = statusCode
224
+ error.serviceName = this._serviceName
225
+ error.method = method
226
+ error.path = path
214
227
  return error
215
228
  }
216
229
  }
@@ -415,6 +415,13 @@ describe('error handling', () => {
415
415
  // Assert
416
416
  expect(error).toBeInstanceOf(Error)
417
417
  expect(error.message).toBe('404 - CPH not found')
418
+ expect(error).toMatchObject({
419
+ statusCode: 404,
420
+ serviceName: 'lis-be4fe-cattle-home',
421
+ method: 'GET',
422
+ path: 'api/cphs/missing/cattle'
423
+ })
424
+ expect(error.cause).toBeUndefined()
418
425
  })
419
426
 
420
427
  test('it throws a generic validation error for a 422 response', async () => {
@@ -476,13 +483,21 @@ describe('error handling', () => {
476
483
  // Assert
477
484
  expect(error).toBeInstanceOf(Error)
478
485
  expect(error.message).toBe('Request failed - 503')
486
+ expect(error).toMatchObject({
487
+ statusCode: 503,
488
+ serviceName: 'lis-be4fe-cattle-home',
489
+ method: 'GET',
490
+ path: 'api/users/user-1/cphs'
491
+ })
492
+ expect(error.cause).toBe(wreckError)
479
493
  })
480
494
 
481
495
  test('it throws when the response body cannot be read', async () => {
482
496
  // Arrange
483
497
  const client = createClient()
484
498
  vi.spyOn(Wreck, 'request').mockResolvedValue({ statusCode: 200 })
485
- vi.spyOn(Wreck, 'read').mockRejectedValue(new Error('Invalid JSON'))
499
+ const readError = new Error('Invalid JSON')
500
+ vi.spyOn(Wreck, 'read').mockRejectedValue(readError)
486
501
 
487
502
  // Act
488
503
  let error
@@ -495,5 +510,12 @@ describe('error handling', () => {
495
510
  // Assert
496
511
  expect(error).toBeInstanceOf(Error)
497
512
  expect(error.message).toBe('Request failed - 200')
513
+ expect(error).toMatchObject({
514
+ statusCode: 200,
515
+ serviceName: 'lis-be4fe-cattle-home',
516
+ method: 'GET',
517
+ path: 'api/users/user-1/cphs'
518
+ })
519
+ expect(error.cause).toBe(readError)
498
520
  })
499
521
  })
@@ -12,11 +12,13 @@ const defaultIgnorePath = (_, request) =>
12
12
 
13
13
  const validFormats = new Set(['ecs', 'pretty-print'])
14
14
 
15
- // Standardised, non-configurable across every consumer - these headers
16
- // must never reach logs.
15
+ // Standardised, non-configurable across every consumer - these must never
16
+ // reach logs. error_description is free text an identity provider returns on
17
+ // its callback, so it is kept out of the request line.
17
18
  const redactPaths = [
18
19
  'req.headers.authorization',
19
20
  'req.headers.cookie',
21
+ 'req.query.error_description',
20
22
  'res.headers'
21
23
  ]
22
24
 
@@ -86,6 +86,27 @@ describe('Logger', () => {
86
86
  expect(logLine.toString()).not.toContain('secret-token')
87
87
  expect(logLine.toString()).not.toContain('secret-session')
88
88
  })
89
+
90
+ test('it removes an identity provider error_description from the request query', () => {
91
+ // Arrange
92
+ const freshLogging = new logger.constructor()
93
+ const writeSpy = vi
94
+ .spyOn(process.stdout, 'write')
95
+ .mockImplementation(() => true)
96
+
97
+ // Act
98
+ freshLogging.info({
99
+ req: {
100
+ query: { error: 'access_denied', error_description: 'free text' }
101
+ }
102
+ })
103
+ const [logLine] = writeSpy.mock.calls[0]
104
+ writeSpy.mockRestore()
105
+
106
+ // Assert
107
+ expect(logLine.toString()).toContain('access_denied')
108
+ expect(logLine.toString()).not.toContain('free text')
109
+ })
89
110
  })
90
111
  describe.each(['fatal', 'error', 'warn', 'info', 'debug', 'trace'])(
91
112
  '%s',
@@ -296,4 +317,31 @@ describe('Logger', () => {
296
317
  expect(parsed.tenant).toBeUndefined()
297
318
  })
298
319
  })
320
+
321
+ describe('errors', () => {
322
+ test('drops non-standard properties from logged errors', () => {
323
+ // Arrange
324
+ const freshLogging = new logger.constructor()
325
+ const writeSpy = vi
326
+ .spyOn(process.stdout, 'write')
327
+ .mockImplementation(() => true)
328
+ const err = new Error('outer', { cause: new Error('inner') })
329
+ err.command = { name: 'set', args: ['secret-payload'] }
330
+ err.code = 'X'
331
+
332
+ // Act
333
+ freshLogging.error({ err }, 'command failed')
334
+ const [logLine] = writeSpy.mock.calls[0]
335
+ writeSpy.mockRestore()
336
+
337
+ // Assert
338
+ const serialised = logLine.toString()
339
+ expect(Object.keys(JSON.parse(serialised).error).toSorted()).toEqual([
340
+ 'message',
341
+ 'stack_trace',
342
+ 'type'
343
+ ])
344
+ expect(serialised).not.toContain('secret-payload')
345
+ })
346
+ })
299
347
  })
@@ -1,8 +1,13 @@
1
+ import Boom from '@hapi/boom'
1
2
  import h2o2 from '@hapi/h2o2'
2
3
 
3
4
  import { getModulesForHub } from '@defra/lis-hubs-infra-registry'
4
5
  import { getServiceBaseUrl } from './get-service-base-url.js'
5
6
  import { getServiceToken } from './service-token/issuer.js'
7
+ import { ServiceTokenError } from './base-client.js'
8
+
9
+ const BAD_GATEWAY = 502
10
+ const GENERIC_ERROR_MESSAGE = 'An internal server error occurred'
6
11
 
7
12
  /**
8
13
  * @param {string | undefined} cookieHeader
@@ -18,6 +23,50 @@ function pickCookies(cookieHeader, names) {
18
23
  return picked?.length ? picked.join('; ') : undefined
19
24
  }
20
25
 
26
+ /**
27
+ * @param {string} serviceName
28
+ * @returns {Promise<string>} a service token for the spoke
29
+ * @throws {ServiceTokenError} naming the spoke, so the hub's error log says which one
30
+ */
31
+ async function getSpokeServiceToken(serviceName) {
32
+ try {
33
+ return await getServiceToken(serviceName)
34
+ } catch (err) {
35
+ throw new ServiceTokenError(
36
+ `Service token unavailable for spoke ${serviceName}`,
37
+ { cause: err }
38
+ )
39
+ }
40
+ }
41
+
42
+ /**
43
+ * h2o2 onResponse. An upstream failure becomes a Boom error naming the spoke,
44
+ * keeping the upstream status, so the hub's error handler logs it once with
45
+ * that context, while the response body stays generic. A response is passed
46
+ * through as h2o2 does by default.
47
+ *
48
+ * @param {string} serviceName
49
+ * @returns {Function} an h2o2 onResponse handler
50
+ */
51
+ function wrapSpokeFailure(serviceName) {
52
+ return function onResponse(err, res, _request, h) {
53
+ if (err) {
54
+ const wrapped = new Error(
55
+ `Spoke ${serviceName} request failed: ${err.message}`,
56
+ { cause: err }
57
+ )
58
+ const boom = Boom.boomify(wrapped, {
59
+ statusCode: err.output?.statusCode ?? BAD_GATEWAY
60
+ })
61
+ // Boom only hides the message of a 500; this one names the spoke and
62
+ // the upstream address, which the browser must not see.
63
+ boom.output.payload.message = GENERIC_ERROR_MESSAGE
64
+ return boom
65
+ }
66
+ return h.response(res).code(res.statusCode).passThrough(true)
67
+ }
68
+ }
69
+
21
70
  /**
22
71
  * @param {object} options
23
72
  * @param {string} options.hubId
@@ -69,6 +118,7 @@ export function createProxyPlugin({
69
118
  proxy: {
70
119
  passThrough: true,
71
120
  xforward: true,
121
+ onResponse: wrapSpokeFailure(serviceName),
72
122
  async mapUri(request) {
73
123
  const subPath = request.params.path ?? ''
74
124
  const uri =
@@ -91,7 +141,7 @@ export function createProxyPlugin({
91
141
  uri,
92
142
  headers: {
93
143
  'x-forwarded-prefix': path,
94
- authorization: `Bearer ${await getServiceToken(serviceName)}`,
144
+ authorization: `Bearer ${await getSpokeServiceToken(serviceName)}`,
95
145
  ...(forwardedCookies && { cookie: forwardedCookies })
96
146
  }
97
147
  }
@@ -1,8 +1,11 @@
1
- import { beforeEach, describe, expect, test, vi } from 'vitest'
1
+ import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest'
2
+ import Boom from '@hapi/boom'
2
3
 
3
4
  import { getModulesForHub } from '@defra/lis-hubs-infra-registry'
4
5
  import { createProxyPlugin } from './proxy-plugin.js'
5
6
  import { getServiceToken } from './service-token/issuer.js'
7
+ import { logger } from './logging/index.js'
8
+ import { ServiceTokenError } from './base-client.js'
6
9
 
7
10
  vi.mock('@defra/lis-hubs-infra-registry')
8
11
  vi.mock('./service-token/issuer.js')
@@ -21,6 +24,10 @@ describe('createProxyPlugin()', () => {
21
24
  mocks.getServiceToken.mockResolvedValue('service-token')
22
25
  })
23
26
 
27
+ afterEach(() => {
28
+ vi.restoreAllMocks()
29
+ })
30
+
24
31
  test.each([
25
32
  // Local proxying intentionally uses HTTP because the services run locally.
26
33
  ['local', 'http://localhost:3222'],
@@ -102,9 +109,12 @@ describe('createProxyPlugin()', () => {
102
109
  expect(result.headers.authorization).toBe('Bearer service-token')
103
110
  })
104
111
 
105
- test('rejects when the service token cannot be obtained', async () => {
112
+ test('rejects with an error naming the spoke when the service token cannot be obtained', async () => {
106
113
  // Arrange
107
- mocks.getServiceToken.mockRejectedValue(new Error('sts down'))
114
+ const stsError = new Error('sts down')
115
+ mocks.getServiceToken.mockRejectedValue(stsError)
116
+ const errorSpy = vi.fn()
117
+ vi.spyOn(logger, 'error', 'get').mockReturnValue(errorSpy)
108
118
  const server = { route: vi.fn(), register: vi.fn() }
109
119
  const proxy = createProxyPlugin({
110
120
  hubId: 'back-office',
@@ -126,7 +136,113 @@ describe('createProxyPlugin()', () => {
126
136
  }
127
137
 
128
138
  // Assert
129
- expect(error.message).toBe('sts down')
139
+ expect(error).toBeInstanceOf(ServiceTokenError)
140
+ expect(error.message).toBe(
141
+ 'Service token unavailable for spoke lis-apps-cattle-home'
142
+ )
143
+ expect(error.statusCode).toBe(503)
144
+ expect(error.cause).toBe(stsError)
145
+ expect(errorSpy).not.toHaveBeenCalled()
146
+ })
147
+
148
+ test.each([502, 504])(
149
+ 'turns a failed spoke request into a %i Boom error naming the spoke, without logging',
150
+ async (statusCode) => {
151
+ // Arrange
152
+ const errorSpy = vi.fn()
153
+ vi.spyOn(logger, 'error', 'get').mockReturnValue(errorSpy)
154
+ const server = { route: vi.fn(), register: vi.fn() }
155
+ const proxy = createProxyPlugin({
156
+ hubId: 'back-office',
157
+ environment: 'local',
158
+ hubJwtCookieName: 'livestock_hub_jwt'
159
+ })
160
+ await proxy.plugin.register(server)
161
+ const upstreamError = Boom.boomify(new Error('Client request error'), {
162
+ statusCode
163
+ })
164
+
165
+ // Act
166
+ const result = server.route.mock.calls[0][0].handler.proxy.onResponse(
167
+ upstreamError,
168
+ undefined,
169
+ { method: 'get', path: '/cattle/animals' },
170
+ {}
171
+ )
172
+
173
+ // Assert
174
+ expect(Boom.isBoom(result)).toBe(true)
175
+ expect(result.output.statusCode).toBe(statusCode)
176
+ expect(result.message).toBe(
177
+ 'Spoke lis-apps-cattle-home request failed: Client request error'
178
+ )
179
+ expect(result.cause).toBe(upstreamError)
180
+ expect(result.output.payload).toEqual({
181
+ statusCode,
182
+ error: statusCode === 502 ? 'Bad Gateway' : 'Gateway Time-out',
183
+ message: 'An internal server error occurred'
184
+ })
185
+ expect(errorSpy).not.toHaveBeenCalled()
186
+ }
187
+ )
188
+
189
+ test('defaults a failed spoke request without a status to 502', async () => {
190
+ // Arrange
191
+ const server = { route: vi.fn(), register: vi.fn() }
192
+ const proxy = createProxyPlugin({
193
+ hubId: 'back-office',
194
+ environment: 'local',
195
+ hubJwtCookieName: 'livestock_hub_jwt'
196
+ })
197
+ await proxy.plugin.register(server)
198
+ const upstreamError = new Error('socket hang up')
199
+
200
+ // Act
201
+ const result = server.route.mock.calls[0][0].handler.proxy.onResponse(
202
+ upstreamError,
203
+ undefined,
204
+ { method: 'get', path: '/cattle' },
205
+ {}
206
+ )
207
+
208
+ // Assert
209
+ expect(result.output.statusCode).toBe(502)
210
+ expect(result.cause).toBe(upstreamError)
211
+ })
212
+
213
+ test('passes the spoke response through when the request succeeds', async () => {
214
+ // Arrange
215
+ const errorSpy = vi.fn()
216
+ vi.spyOn(logger, 'error', 'get').mockReturnValue(errorSpy)
217
+ const server = { route: vi.fn(), register: vi.fn() }
218
+ const proxy = createProxyPlugin({
219
+ hubId: 'back-office',
220
+ environment: 'local',
221
+ hubJwtCookieName: 'livestock_hub_jwt'
222
+ })
223
+ await proxy.plugin.register(server)
224
+ const response = { statusCode: 302 }
225
+ const passedThrough = { passedThrough: true }
226
+ const toolkitResponse = {
227
+ code: vi.fn().mockReturnThis(),
228
+ passThrough: vi.fn().mockReturnValue(passedThrough)
229
+ }
230
+ const h = { response: vi.fn().mockReturnValue(toolkitResponse) }
231
+
232
+ // Act
233
+ const result = server.route.mock.calls[0][0].handler.proxy.onResponse(
234
+ null,
235
+ response,
236
+ { method: 'get', path: '/cattle' },
237
+ h
238
+ )
239
+
240
+ // Assert
241
+ expect(h.response).toHaveBeenCalledWith(response)
242
+ expect(toolkitResponse.code).toHaveBeenCalledWith(302)
243
+ expect(toolkitResponse.passThrough).toHaveBeenCalledWith(true)
244
+ expect(result).toBe(passedThrough)
245
+ expect(errorSpy).not.toHaveBeenCalled()
130
246
  })
131
247
 
132
248
  test('forwards only the hub JWT cookie', async () => {
@@ -1,6 +1,7 @@
1
1
  import { GetWebIdentityTokenCommand, STSClient } from '@aws-sdk/client-sts'
2
2
 
3
3
  import { SIGNING_ALGORITHM } from './constants.js'
4
+ import { logger } from '../logging/index.js'
4
5
 
5
6
  const MAX_DURATION_SECONDS = 900
6
7
  const MIN_DURATION_SECONDS = 60
@@ -83,7 +84,12 @@ export async function getServiceToken(audience) {
83
84
  if (now >= refreshFrom) {
84
85
  // Callers keep the still-valid token; a failure leaves it cached so a
85
86
  // later call retries.
86
- refreshToken(audience).catch(() => undefined)
87
+ refreshToken(audience).catch((err) =>
88
+ logger.warn(
89
+ { err },
90
+ `Background service token refresh failed for ${audience}; keeping cached token`
91
+ )
92
+ )
87
93
  }
88
94
  return cached.token
89
95
  }
@@ -39,6 +39,7 @@ describe('getServiceToken()', () => {
39
39
  afterEach(() => {
40
40
  vi.useRealTimers()
41
41
  vi.clearAllMocks()
42
+ vi.restoreAllMocks()
42
43
  })
43
44
 
44
45
  test('builds one STS client, with request timeouts and limited attempts', async () => {
@@ -205,6 +206,9 @@ describe('getServiceToken()', () => {
205
206
 
206
207
  test('keeps serving the cached token when a background refresh fails and retries on the next call', async () => {
207
208
  // Arrange
209
+ const { logger } = await import('../logging/index.js')
210
+ const warnSpy = vi.fn()
211
+ vi.spyOn(logger, 'warn', 'get').mockReturnValue(warnSpy)
208
212
  send
209
213
  .mockResolvedValueOnce(tokenResponse('t1'))
210
214
  .mockRejectedValueOnce(new Error('sts down'))
@@ -224,6 +228,30 @@ describe('getServiceToken()', () => {
224
228
  expect(afterFailure).toBe('t1')
225
229
  expect(afterRetry).toBe('t2')
226
230
  expect(send).toHaveBeenCalledTimes(3)
231
+ expect(warnSpy).toHaveBeenCalledTimes(1)
232
+ })
233
+
234
+ test('logs a warning with the audience when a background refresh fails', async () => {
235
+ // Arrange
236
+ const { logger } = await import('../logging/index.js')
237
+ const warnSpy = vi.fn()
238
+ vi.spyOn(logger, 'warn', 'get').mockReturnValue(warnSpy)
239
+ const stsError = new Error('sts down')
240
+ send
241
+ .mockResolvedValueOnce(tokenResponse('t1'))
242
+ .mockRejectedValueOnce(stsError)
243
+ await getServiceToken('aud')
244
+ vi.setSystemTime(new Date(NOW.getTime() + 840 * 1000))
245
+
246
+ // Act
247
+ await getServiceToken('aud')
248
+ await vi.advanceTimersByTimeAsync(0)
249
+
250
+ // Assert
251
+ expect(warnSpy).toHaveBeenCalledWith(
252
+ { err: stsError },
253
+ 'Background service token refresh failed for aud; keeping cached token'
254
+ )
227
255
  })
228
256
 
229
257
  test('caches tokens separately per audience', async () => {