Add proxyEndpointsExtensionPoint

Signed-off-by: Matt Benson <gudnabrsam@gmail.com>
This commit is contained in:
Matt Benson
2024-10-18 14:09:51 -05:00
parent 41ab5cf4a0
commit 11b001cf4d
14 changed files with 235 additions and 1 deletions
+6
View File
@@ -0,0 +1,6 @@
---
'@backstage/plugin-proxy-backend': minor
'@backstage/plugin-proxy-node': minor
---
Add proxyEndpointsExtensionPoint
+39
View File
@@ -120,3 +120,42 @@ third-parties.
The same logic applies to headers that are sent from the target back to the
frontend.
### New backend extension
The proxy plugin additionally supports a `proxyExtensionEndpoint` which a proxy
plugin module can utilize in order to programmatically register additional
endpoints, whose payloads are specified exactly as in app-config (described
above). Note that endpoints configured in app-config will always override those
registered in this manner.
Example:
```ts
backend.add(
createBackendModule({
pluginId: 'proxy',
moduleId: 'demo-additional-endpoints',
register: reg => {
reg.registerInit({
deps: {
proxyEndpoints: proxyEndpointsExtensionPoint,
},
init: async ({ proxyEndpoints }) => {
let largerExampleAuth: string = /* exercise for the reader */;
proxyEndpoints.addProxyEndpoints({
"/simple-example": "http://simple.example.com:8080",
"/larger-example/v1": {
target: "http://larger.example.com:8080/svc.v1",
credentials: "require",
headers: {
Authorization: largerExampleAuth
},
},
});
},
});
},
}),
);
```
+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
@@ -6,6 +6,7 @@
import { BackendFeature } from '@backstage/backend-plugin-api';
import { DiscoveryService } from '@backstage/backend-plugin-api';
import express from 'express';
import { JsonObject } from '@backstage/types';
import { Logger } from 'winston';
import { RootConfigService } from '@backstage/backend-plugin-api';
@@ -18,6 +19,8 @@ export default proxyPlugin;
// @public @deprecated (undocumented)
export interface RouterOptions {
// (undocumented)
additionalEndpoints?: JsonObject;
// (undocumented)
config: RootConfigService;
// (undocumented)
+10
View File
@@ -20,6 +20,8 @@ import {
coreServices,
} from '@backstage/backend-plugin-api';
import { createRouterInternal } from './service/router';
import { proxyEndpointsExtensionPoint } from '@backstage/plugin-proxy-node/alpha';
import { JsonObject } from '@backstage/types';
/**
* The proxy backend plugin.
@@ -29,6 +31,13 @@ import { createRouterInternal } from './service/router';
export const proxyPlugin = createBackendPlugin({
pluginId: 'proxy',
register(env) {
const additionalEndpoints = {};
env.registerExtensionPoint(proxyEndpointsExtensionPoint, {
addProxyEndpoints(endpoints: JsonObject) {
Object.assign(additionalEndpoints, endpoints);
},
});
env.registerInit({
deps: {
config: coreServices.rootConfig,
@@ -42,6 +51,7 @@ export const proxyPlugin = createBackendPlugin({
discovery,
logger: loggerToWinstonLogger(logger),
httpRouterService: httpRouter,
additionalEndpoints,
});
},
});
+5 -1
View File
@@ -63,6 +63,7 @@ export interface RouterOptions {
discovery: DiscoveryService;
skipInvalidProxies?: boolean;
reviveConsumedRequestBodies?: boolean;
additionalEndpoints?: JsonObject;
}
export interface ProxyConfig extends Options {
@@ -315,7 +316,10 @@ 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 = {
...(options.additionalEndpoints ?? {}),
...readProxyConfig(options.config, options.logger),
};
configureMiddlewares(
proxyOptions,
currentRouter,
+1
View File
@@ -0,0 +1 @@
module.exports = require('@backstage/cli/config/eslint-factory')(__dirname);
+5
View File
@@ -0,0 +1,5 @@
# backstage-plugin-proxy-node-node
Welcome to the Node.js library package for the proxy-node plugin!
_This plugin was created through the Backstage CLI_
+10
View File
@@ -0,0 +1,10 @@
apiVersion: backstage.io/v1alpha1
kind: Component
metadata:
name: backstage-plugin-proxy-node
title: '@backstage/plugin-proxy-node'
description: The plugin-proxy-node module for @backstage/plugin-proxy-backend
spec:
lifecycle: experimental
type: backstage-node-library
owner: maintainers
+59
View File
@@ -0,0 +1,59 @@
{
"name": "@backstage/plugin-proxy-node",
"version": "0.0.0",
"description": "The plugin-proxy-node module for @backstage/plugin-proxy-backend",
"backstage": {
"role": "node-library",
"pluginId": "proxy-backend",
"pluginPackages": [
"@backstage/plugin-proxy-node"
]
},
"publishConfig": {
"access": "public"
},
"homepage": "https://backstage.io",
"repository": {
"type": "git",
"url": "https://github.com/backstage/backstage",
"directory": "plugins/proxy-node"
},
"license": "Apache-2.0",
"exports": {
"./alpha": "./src/alpha/index.ts",
"./package.json": "./package.json"
},
"main": "src/alpha/index.ts",
"types": "src/alpha/index.ts",
"typesVersions": {
"*": {
"alpha": [
"src/alpha/index.ts"
],
"package.json": [
"package.json"
]
}
},
"files": [
"dist"
],
"scripts": {
"build": "backstage-cli package build",
"clean": "backstage-cli package clean",
"lint": "backstage-cli package lint",
"prepack": "backstage-cli package prepack",
"postpack": "backstage-cli package postpack",
"start": "backstage-cli package start",
"test": "backstage-cli package test"
},
"dependencies": {
"@backstage/backend-plugin-api": "workspace:^",
"@backstage/types": "workspace:^"
},
"devDependencies": {
"@backstage/backend-test-utils": "workspace:^",
"@backstage/cli": "workspace:^",
"@backstage/config": "workspace:^"
}
}
+19
View File
@@ -0,0 +1,19 @@
## API Report File for "@backstage/plugin-proxy-node"
> Do not edit this file. It is a report generated by [API Extractor](https://api-extractor.com/).
```ts
import { ExtensionPoint } from '@backstage/backend-plugin-api';
import { JsonObject } from '@backstage/types';
// @alpha
export interface ProxyEndpointsExtensionPoint {
// (undocumented)
addProxyEndpoints(endpoints: JsonObject): void;
}
// @alpha
export const proxyEndpointsExtensionPoint: ExtensionPoint<ProxyEndpointsExtensionPoint>;
// (No @packageDocumentation comment for this package)
```
+36
View File
@@ -0,0 +1,36 @@
/*
* Copyright 2024 The Backstage Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { createExtensionPoint } from '@backstage/backend-plugin-api';
import { JsonObject } from '@backstage/types';
/**
* Extension point interface for managing proxy endpoints.
*
* @alpha
*/
export interface ProxyEndpointsExtensionPoint {
addProxyEndpoints(endpoints: JsonObject): void;
}
/**
* Extension point for managing proxy endpoints.
*
* @alpha
*/
export const proxyEndpointsExtensionPoint =
createExtensionPoint<ProxyEndpointsExtensionPoint>({
id: 'proxy.endpoints',
});
+13
View File
@@ -7120,6 +7120,7 @@ __metadata:
"@backstage/config": "workspace:^"
"@backstage/config-loader": "workspace:^"
"@backstage/errors": "workspace:^"
"@backstage/plugin-proxy-node": "workspace:^"
"@backstage/types": "workspace:^"
"@types/express": ^4.17.6
"@types/http-proxy-middleware": ^1.0.0
@@ -7137,6 +7138,18 @@ __metadata:
languageName: unknown
linkType: soft
"@backstage/plugin-proxy-node@workspace:^, @backstage/plugin-proxy-node@workspace:plugins/proxy-node":
version: 0.0.0-use.local
resolution: "@backstage/plugin-proxy-node@workspace:plugins/proxy-node"
dependencies:
"@backstage/backend-plugin-api": "workspace:^"
"@backstage/backend-test-utils": "workspace:^"
"@backstage/cli": "workspace:^"
"@backstage/config": "workspace:^"
"@backstage/types": "workspace:^"
languageName: unknown
linkType: soft
"@backstage/plugin-scaffolder-backend-module-azure@workspace:^, @backstage/plugin-scaffolder-backend-module-azure@workspace:plugins/scaffolder-backend-module-azure":
version: 0.0.0-use.local
resolution: "@backstage/plugin-scaffolder-backend-module-azure@workspace:plugins/scaffolder-backend-module-azure"