Merge pull request #16117 from RubenV-dev/proxy-write

Bug fix: Allow Kubernetes Proxy endpoint to support write operations
This commit is contained in:
Fredrik Adelöw
2023-02-02 19:57:18 +01:00
committed by GitHub
3 changed files with 71 additions and 2 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/plugin-kubernetes-backend': patch
---
Kubernetes proxy endpoint now accepts content types that are not json
@@ -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,65 @@ describe('KubernetesBuilder', () => {
expect(response.status).toEqual(200);
});
});
describe('post /proxy', () => {
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',
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.all(proxyEndpointRequest.url, req => 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.all(proxyEndpointRequest.url, req => req.passthrough()));
const response = await proxyEndpointRequest;
expect(response.text).toEqual(requestBody);
});
});
});
@@ -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;