@defra/lis-hubs-infra-core 0.13.0-lreg-563.alpha.2 → 0.13.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@defra/lis-hubs-infra-core",
3
- "version": "0.13.0-lreg-563.alpha.2",
3
+ "version": "0.13.0",
4
4
  "description": "Shared hub mechanics for livestock platform.",
5
5
  "main": "src/index.js",
6
6
  "repository": {
@@ -2,6 +2,7 @@ import h2o2 from '@hapi/h2o2'
2
2
 
3
3
  import { getModulesForHub } from '@defra/lis-hubs-infra-registry'
4
4
  import { getServiceBaseUrl } from './get-service-base-url.js'
5
+ import { getServiceToken } from './service-token/issuer.js'
5
6
 
6
7
  /**
7
8
  * @param {string | undefined} cookieHeader
@@ -35,9 +36,10 @@ export function createProxyPlugin({ hubId, environment, hubJwtCookieName }) {
35
36
  await server.register(h2o2)
36
37
 
37
38
  for (const { id: moduleName, path, port } of modules) {
39
+ const serviceName = `lis-apps-${moduleName}`
38
40
  const baseUri = getServiceBaseUrl(
39
41
  environment,
40
- `lis-apps-${moduleName}`,
42
+ serviceName,
41
43
  port
42
44
  ).origin
43
45
 
@@ -48,7 +50,7 @@ export function createProxyPlugin({ hubId, environment, hubJwtCookieName }) {
48
50
  proxy: {
49
51
  passThrough: true,
50
52
  xforward: true,
51
- mapUri(request) {
53
+ async mapUri(request) {
52
54
  const subPath = request.params.path ?? ''
53
55
  const uri =
54
56
  (subPath ? `${baseUri}/${subPath}` : baseUri) +
@@ -69,6 +71,7 @@ export function createProxyPlugin({ hubId, environment, hubJwtCookieName }) {
69
71
  uri,
70
72
  headers: {
71
73
  'x-forwarded-prefix': path,
74
+ authorization: `Bearer ${await getServiceToken(serviceName)}`,
72
75
  ...(hubJwtCookie && { cookie: hubJwtCookie })
73
76
  }
74
77
  }
@@ -2,11 +2,14 @@ import { beforeEach, describe, expect, test, vi } from 'vitest'
2
2
 
3
3
  import { getModulesForHub } from '@defra/lis-hubs-infra-registry'
4
4
  import { createProxyPlugin } from './proxy-plugin.js'
5
+ import { getServiceToken } from './service-token/issuer.js'
5
6
 
6
7
  vi.mock('@defra/lis-hubs-infra-registry')
8
+ vi.mock('./service-token/issuer.js')
7
9
 
8
10
  const mocks = {
9
- getModulesForHub: vi.mocked(getModulesForHub)
11
+ getModulesForHub: vi.mocked(getModulesForHub),
12
+ getServiceToken: vi.mocked(getServiceToken)
10
13
  }
11
14
 
12
15
  describe('createProxyPlugin()', () => {
@@ -15,6 +18,7 @@ describe('createProxyPlugin()', () => {
15
18
  mocks.getModulesForHub.mockReturnValue([
16
19
  { id: 'cattle-home', path: '/cattle', port: 3222 }
17
20
  ])
21
+ mocks.getServiceToken.mockResolvedValue('service-token')
18
22
  })
19
23
 
20
24
  test.each([
@@ -43,7 +47,7 @@ describe('createProxyPlugin()', () => {
43
47
  const route = server.route.mock.calls[0][0]
44
48
  expect(route).toMatchObject({ method: '*', path: '/cattle/{path*}' })
45
49
  expect(
46
- route.handler.proxy.mapUri({
50
+ await route.handler.proxy.mapUri({
47
51
  params: { path: 'summary-data' },
48
52
  url: new URL('http://hub.test/cattle/summary-data'),
49
53
  headers: {
@@ -55,11 +59,12 @@ describe('createProxyPlugin()', () => {
55
59
  uri: `${expectedBaseUri}/summary-data`,
56
60
  headers: {
57
61
  'x-forwarded-prefix': '/cattle',
62
+ authorization: 'Bearer service-token',
58
63
  cookie: 'livestock_hub_jwt=hub.jwt.value'
59
64
  }
60
65
  })
61
66
  expect(
62
- route.handler.proxy.mapUri({
67
+ await route.handler.proxy.mapUri({
63
68
  params: {},
64
69
  url: new URL('http://hub.test/cattle'),
65
70
  headers: {}
@@ -67,11 +72,63 @@ describe('createProxyPlugin()', () => {
67
72
  ).toEqual({
68
73
  uri: expectedBaseUri,
69
74
  headers: {
70
- 'x-forwarded-prefix': '/cattle'
75
+ 'x-forwarded-prefix': '/cattle',
76
+ authorization: 'Bearer service-token'
71
77
  }
72
78
  })
73
79
  })
74
80
 
81
+ test('sends a service token for the target spoke in place of the inbound authorization', async () => {
82
+ // Arrange
83
+ const server = { route: vi.fn(), register: vi.fn() }
84
+ const proxy = createProxyPlugin({
85
+ hubId: 'back-office',
86
+ environment: 'local',
87
+ hubJwtCookieName: 'livestock_hub_jwt'
88
+ })
89
+ const request = {
90
+ params: {},
91
+ url: new URL('http://hub.test/cattle'),
92
+ headers: { authorization: 'Bearer browser-token' }
93
+ }
94
+
95
+ // Act
96
+ await proxy.plugin.register(server)
97
+ const result =
98
+ await server.route.mock.calls[0][0].handler.proxy.mapUri(request)
99
+
100
+ // Assert
101
+ expect(mocks.getServiceToken).toHaveBeenCalledWith('lis-apps-cattle-home')
102
+ expect(result.headers.authorization).toBe('Bearer service-token')
103
+ })
104
+
105
+ test('rejects when the service token cannot be obtained', async () => {
106
+ // Arrange
107
+ mocks.getServiceToken.mockRejectedValue(new Error('sts down'))
108
+ const server = { route: vi.fn(), register: vi.fn() }
109
+ const proxy = createProxyPlugin({
110
+ hubId: 'back-office',
111
+ environment: 'local',
112
+ hubJwtCookieName: 'livestock_hub_jwt'
113
+ })
114
+ await proxy.plugin.register(server)
115
+
116
+ // Act
117
+ let error
118
+ try {
119
+ await server.route.mock.calls[0][0].handler.proxy.mapUri({
120
+ params: {},
121
+ url: new URL('http://hub.test/cattle'),
122
+ headers: {}
123
+ })
124
+ } catch (e) {
125
+ error = e
126
+ }
127
+
128
+ // Assert
129
+ expect(error.message).toBe('sts down')
130
+ })
131
+
75
132
  test('forwards only the hub JWT cookie', async () => {
76
133
  // Arrange
77
134
  const server = { route: vi.fn(), register: vi.fn() }
@@ -91,7 +148,8 @@ describe('createProxyPlugin()', () => {
91
148
 
92
149
  // Act
93
150
  await proxy.plugin.register(server)
94
- const result = server.route.mock.calls[0][0].handler.proxy.mapUri(request)
151
+ const result =
152
+ await server.route.mock.calls[0][0].handler.proxy.mapUri(request)
95
153
 
96
154
  // Assert
97
155
  expect(result.headers.cookie).toBe('livestock_hub_jwt=hub.jwt.value')
@@ -114,10 +172,14 @@ describe('createProxyPlugin()', () => {
114
172
 
115
173
  // Act
116
174
  await proxy.plugin.register(server)
117
- const result = server.route.mock.calls[0][0].handler.proxy.mapUri(request)
175
+ const result =
176
+ await server.route.mock.calls[0][0].handler.proxy.mapUri(request)
118
177
 
119
178
  // Assert
120
- expect(result.headers).toEqual({ 'x-forwarded-prefix': '/cattle' })
179
+ expect(result.headers).toEqual({
180
+ 'x-forwarded-prefix': '/cattle',
181
+ authorization: 'Bearer service-token'
182
+ })
121
183
  expect(request.headers).toEqual({})
122
184
  })
123
185
 
@@ -150,7 +212,7 @@ describe('createProxyPlugin()', () => {
150
212
 
151
213
  // Act
152
214
  await proxy.plugin.register(server)
153
- server.route.mock.calls[0][0].handler.proxy.mapUri(request)
215
+ await server.route.mock.calls[0][0].handler.proxy.mapUri(request)
154
216
 
155
217
  // Assert
156
218
  expect(request.headers).toEqual({ accept: 'text/html' })
@@ -171,7 +233,7 @@ describe('createProxyPlugin()', () => {
171
233
  // Assert
172
234
  const route = server.route.mock.calls[0][0]
173
235
  expect(
174
- route.handler.proxy.mapUri({
236
+ await route.handler.proxy.mapUri({
175
237
  params: { path: 'animals' },
176
238
  url: new URL(
177
239
  'http://hub.test/cattle/animals?sort=sex&direction=asc&page=1'