From eec110675bcefd36a347a944e348ec2f3c7c750d Mon Sep 17 00:00:00 2001 From: Ruben Vallejo Date: Tue, 31 Jan 2023 17:27:15 -0500 Subject: [PATCH 1/3] Add unit tests for post /proxy endpoint Signed-off-by: Ruben Vallejo --- .../src/service/KubernetesBuilder.test.ts | 78 +++++++++++++++++++ 1 file changed, 78 insertions(+) diff --git a/plugins/kubernetes-backend/src/service/KubernetesBuilder.test.ts b/plugins/kubernetes-backend/src/service/KubernetesBuilder.test.ts index 2766f9abfc..5e0caaf6a5 100644 --- a/plugins/kubernetes-backend/src/service/KubernetesBuilder.test.ts +++ b/plugins/kubernetes-backend/src/service/KubernetesBuilder.test.ts @@ -31,6 +31,10 @@ import { import { KubernetesBuilder } from './KubernetesBuilder'; import { KubernetesFanOutHandler } from './KubernetesFanOutHandler'; import { CatalogApi } from '@backstage/catalog-client'; +import { HEADER_KUBERNETES_CLUSTER } from './KubernetesProxy'; +import { setupServer } from 'msw/node'; +import { setupRequestMockHandlers } from '@backstage/backend-test-utils'; +import { rest } from 'msw'; describe('KubernetesBuilder', () => { let app: express.Express; @@ -267,4 +271,78 @@ describe('KubernetesBuilder', () => { expect(response.status).toEqual(200); }); }); + + describe('post /proxy', () => { + const worker = setupServer(); + setupRequestMockHandlers(worker); + + it('returns the given request body', async () => { + const requestBody = { + kind: 'Namespace', + apiVersion: 'v1', + metadata: { + name: 'new-ns', + }, + }; + + const proxyEndpointRequest = request(app) + .post('/proxy/api/v1/namespaces') + .set(HEADER_KUBERNETES_CLUSTER, 'some-cluster') + .send(requestBody); + + worker.use( + rest.post('https://localhost:1234/api/v1/namespaces', (req, res, ctx) => + req + .arrayBuffer() + .then(body => + res( + ctx.set('content-type', `${req.headers.get('content-type')}`), + ctx.body(body), + ), + ), + ), + rest.all(proxyEndpointRequest.url, (req, _res, _ctx) => + req.passthrough(), + ), + ); + + const response = await proxyEndpointRequest; + + expect(response.body).toStrictEqual(requestBody); + }); + + it('supports yaml content type', async () => { + const requestBody = `--- +kind: Namespace +apiVersion: v1 +metadata: + name: new-ns +`; + + const proxyEndpointRequest = request(app) + .post('/proxy/api/v1/namespaces') + .set(HEADER_KUBERNETES_CLUSTER, 'some-cluster') + .set('content-type', 'application/yaml') + .send(requestBody); + + worker.use( + rest.post('https://localhost:1234/api/v1/namespaces', (req, res, ctx) => + req + .arrayBuffer() + .then(body => + res( + ctx.set('content-type', `${req.headers.get('content-type')}`), + ctx.body(body), + ), + ), + ), + rest.all(proxyEndpointRequest.url, (req, _res, _ctx) => + req.passthrough(), + ), + ); + + const response = await proxyEndpointRequest; + expect(response.text).toEqual(requestBody); + }); + }); }); From 81c5dca2ff5e3dac321f04995d1c402c2dded00a Mon Sep 17 00:00:00 2001 From: Ruben Vallejo Date: Tue, 31 Jan 2023 17:29:32 -0500 Subject: [PATCH 2/3] Passing post proxy unit tests, proxy now supports write operations Signed-off-by: Ruben Vallejo --- .../src/service/KubernetesBuilder.test.ts | 47 +++++++------------ .../src/service/KubernetesBuilder.ts | 3 +- 2 files changed, 18 insertions(+), 32 deletions(-) diff --git a/plugins/kubernetes-backend/src/service/KubernetesBuilder.test.ts b/plugins/kubernetes-backend/src/service/KubernetesBuilder.test.ts index 5e0caaf6a5..cc2dea26c0 100644 --- a/plugins/kubernetes-backend/src/service/KubernetesBuilder.test.ts +++ b/plugins/kubernetes-backend/src/service/KubernetesBuilder.test.ts @@ -276,6 +276,21 @@ describe('KubernetesBuilder', () => { const worker = setupServer(); setupRequestMockHandlers(worker); + beforeEach(() => { + worker.use( + rest.post('https://localhost:1234/api/v1/namespaces', (req, res, ctx) => + req + .arrayBuffer() + .then(body => + res( + ctx.set('content-type', `${req.headers.get('content-type')}`), + ctx.body(body), + ), + ), + ), + ); + }); + it('returns the given request body', async () => { const requestBody = { kind: 'Namespace', @@ -290,21 +305,7 @@ describe('KubernetesBuilder', () => { .set(HEADER_KUBERNETES_CLUSTER, 'some-cluster') .send(requestBody); - worker.use( - rest.post('https://localhost:1234/api/v1/namespaces', (req, res, ctx) => - req - .arrayBuffer() - .then(body => - res( - ctx.set('content-type', `${req.headers.get('content-type')}`), - ctx.body(body), - ), - ), - ), - rest.all(proxyEndpointRequest.url, (req, _res, _ctx) => - req.passthrough(), - ), - ); + worker.use(rest.all(proxyEndpointRequest.url, req => req.passthrough())); const response = await proxyEndpointRequest; @@ -325,21 +326,7 @@ metadata: .set('content-type', 'application/yaml') .send(requestBody); - worker.use( - rest.post('https://localhost:1234/api/v1/namespaces', (req, res, ctx) => - req - .arrayBuffer() - .then(body => - res( - ctx.set('content-type', `${req.headers.get('content-type')}`), - ctx.body(body), - ), - ), - ), - rest.all(proxyEndpointRequest.url, (req, _res, _ctx) => - req.passthrough(), - ), - ); + worker.use(rest.all(proxyEndpointRequest.url, req => req.passthrough())); const response = await proxyEndpointRequest; expect(response.text).toEqual(requestBody); diff --git a/plugins/kubernetes-backend/src/service/KubernetesBuilder.ts b/plugins/kubernetes-backend/src/service/KubernetesBuilder.ts index 1e7b704ec5..92773ece6d 100644 --- a/plugins/kubernetes-backend/src/service/KubernetesBuilder.ts +++ b/plugins/kubernetes-backend/src/service/KubernetesBuilder.ts @@ -265,6 +265,7 @@ export class KubernetesBuilder { ): express.Router { const logger = this.env.logger; const router = Router(); + router.use('/proxy', proxy.createRequestHandler()); router.use(express.json()); // @deprecated @@ -297,8 +298,6 @@ export class KubernetesBuilder { }); }); - router.use('/proxy', proxy.createRequestHandler()); - addResourceRoutesToRouter(router, catalogApi, objectsProvider); return router; From 7ff81f76926b89522109094a6063d1fd480a076d Mon Sep 17 00:00:00 2001 From: Ruben Vallejo Date: Wed, 1 Feb 2023 10:31:26 -0500 Subject: [PATCH 3/3] generate changeset Signed-off-by: Ruben Vallejo --- .changeset/neat-schools-raise.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/neat-schools-raise.md diff --git a/.changeset/neat-schools-raise.md b/.changeset/neat-schools-raise.md new file mode 100644 index 0000000000..b778ba8795 --- /dev/null +++ b/.changeset/neat-schools-raise.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-kubernetes-backend': patch +--- + +Kubernetes proxy endpoint now accepts content types that are not json