Merge pull request #27257 from mbenson/proxy-extension

Add proxyEndpointsExtensionPoint
This commit is contained in:
Ben Lambert
2025-01-14 08:30:46 +01:00
committed by GitHub
14 changed files with 265 additions and 11 deletions
+28
View File
@@ -15,7 +15,35 @@
*/
import { createBackend } from '@backstage/backend-defaults';
import { createBackendModule } from '@backstage/backend-plugin-api';
import { proxyEndpointsExtensionPoint } from '@backstage/plugin-proxy-node/alpha';
const backend = createBackend();
backend.add(import('../src/alpha'));
backend.add(
createBackendModule({
pluginId: 'proxy',
moduleId: 'demo-additional-endpoints',
register: reg => {
reg.registerInit({
deps: {
proxyEndpoints: proxyEndpointsExtensionPoint,
},
init: async ({ proxyEndpoints }) => {
proxyEndpoints.addProxyEndpoints({
...Object.fromEntries(
['foo', 'bar', 'baz'].map(msv => [
`/${msv}`,
{ target: `http://${msv}.org` },
]),
),
'/gocd': 'http://cannot-override-config.no',
});
},
});
},
}),
);
backend.start();
+1
View File
@@ -56,6 +56,7 @@
"@backstage/backend-common": "^0.25.0",
"@backstage/backend-plugin-api": "workspace:^",
"@backstage/config": "workspace:^",
"@backstage/plugin-proxy-node": "workspace:^",
"@backstage/types": "workspace:^",
"@types/express": "^4.17.6",
"express": "^4.17.1",
+3
View File
@@ -7,6 +7,7 @@ import { BackendFeature } from '@backstage/backend-plugin-api';
import { DiscoveryService } from '@backstage/backend-plugin-api';
import express from 'express';
import { Logger } from 'winston';
import { ProxyConfig } from '@backstage/plugin-proxy-node/alpha';
import { RootConfigService } from '@backstage/backend-plugin-api';
// @public @deprecated
@@ -18,6 +19,8 @@ export default proxyPlugin;
// @public @deprecated (undocumented)
export interface RouterOptions {
// (undocumented)
additionalEndpoints?: ProxyConfig;
// (undocumented)
config: RootConfigService;
// (undocumented)
+9
View File
@@ -20,6 +20,7 @@ import {
coreServices,
} from '@backstage/backend-plugin-api';
import { createRouterInternal } from './service/router';
import { proxyEndpointsExtensionPoint } from '@backstage/plugin-proxy-node/alpha';
/**
* The proxy backend plugin.
@@ -29,6 +30,13 @@ import { createRouterInternal } from './service/router';
export const proxyPlugin = createBackendPlugin({
pluginId: 'proxy',
register(env) {
const additionalEndpoints = {};
env.registerExtensionPoint(proxyEndpointsExtensionPoint, {
addProxyEndpoints(endpoints) {
Object.assign(additionalEndpoints, endpoints);
},
});
env.registerInit({
deps: {
config: coreServices.rootConfig,
@@ -42,6 +50,7 @@ export const proxyPlugin = createBackendPlugin({
discovery,
logger: loggerToWinstonLogger(logger),
httpRouterService: httpRouter,
additionalEndpoints,
});
},
});
+10 -11
View File
@@ -20,7 +20,6 @@ import Router from 'express-promise-router';
import {
createProxyMiddleware,
fixRequestBody,
Options,
RequestHandler,
} from 'http-proxy-middleware';
import { Logger } from 'winston';
@@ -31,6 +30,7 @@ import {
HttpRouterService,
RootConfigService,
} from '@backstage/backend-plugin-api';
import { ProxyConfig } from '@backstage/plugin-proxy-node/alpha';
// A list of headers that are always forwarded to the proxy targets.
const safeForwardHeaders = [
@@ -63,12 +63,7 @@ export interface RouterOptions {
discovery: DiscoveryService;
skipInvalidProxies?: boolean;
reviveConsumedRequestBodies?: boolean;
}
export interface ProxyConfig extends Options {
allowedMethods?: string[];
allowedHeaders?: string[];
reviveRequestBody?: boolean;
additionalEndpoints?: ProxyConfig;
}
// Creates a proxy middleware, possibly with defaults added on top of the
@@ -87,7 +82,7 @@ export function buildMiddleware(
fullConfig = { target: config };
credentialsPolicy = 'require';
} else {
const { credentials, ...rest } = config as any;
const { credentials, ...rest } = config;
fullConfig = rest;
credentialsPolicy = credentials ?? 'require';
}
@@ -315,7 +310,11 @@ export async function createRouterInternal(
const externalUrl = await options.discovery.getExternalBaseUrl('proxy');
const { pathname: pathPrefix } = new URL(externalUrl);
const proxyConfig = readProxyConfig(options.config, options.logger);
const proxyConfig: ProxyConfig = {
...(options.additionalEndpoints ?? {}),
...readProxyConfig(options.config, options.logger),
};
configureMiddlewares(
proxyOptions,
currentRouter,
@@ -358,10 +357,10 @@ function configureMiddlewares(
},
router: express.Router,
pathPrefix: string,
proxyConfig: any,
proxyConfig: ProxyConfig,
httpRouterService?: HttpRouterService,
) {
Object.entries<any>(proxyConfig).forEach(([route, proxyRouteConfig]) => {
Object.entries(proxyConfig).forEach(([route, proxyRouteConfig]) => {
try {
router.use(
route,