Overriding Core Backend Service Configuration
February 26, 2025 ยท View on GitHub
Overview
The Backstage backend platform consists of a number of core services that are well encapsulated. The configuration of these core services is normally done by directly customizing the backend source code and rebuilding. However, the dynamic plugin functionality adds the ability for core service customization via installing it as a BackendFeature. The Developer Hub backend normally installs all of these default core services statically during initialization. Environment variables can configure the backend to avoid statically installing a given default core service, allowing for dynamic plugin installation.
An Example
Some use cases may be easier solved at a lower level service than what's available in the Backstage backend plugin API. Adding a middleware function to handle all incoming requests can be done by installing a custom configure function for the root HTTP router backend service, which allows access to the underlying Express app.
// Create the BackendFeature
export const customRootHttpServerFactory: BackendFeature =
rootHttpRouterServiceFactory({
configure: ({ app, routes, middleware, logger }) => {
logger.info(
'Using custom root HttpRouterServiceFactory configure function',
);
app.use(middleware.helmet());
app.use(middleware.cors());
app.use(middleware.compression());
app.use(middleware.logging());
// Add a the custom middleware function before all
// of the route handlers
app.use(addTestHeaderMiddleware({ logger }));
app.use(routes);
app.use(middleware.notFound());
app.use(middleware.error());
},
});
// Export the BackendFeature as the default entrypoint
export default customRootHttpServerFactory;
This BackendFeature overrides the default HTTP router service factory. Because this is overriding the default implementation of a core service, the above example would need the ENABLE_CORE_ROOTHTTPROUTER_OVERRIDE environment variable set to true so that the Developer Hub does not install the default implementation automatically.
Override Environment Variables
To allow a dynamic plugin to load a core service override, start the Developer Hub backend with the environment variable set that corresponds with the core service ID to be overridden. Here is a list of the available environment variables and core service IDs:
ENABLE_CORE_AUTH_OVERRIDE- allow overriding thecore.authserviceENABLE_CORE_CACHE_OVERRIDE- allow overriding thecore.cacheserviceENABLE_CORE_ROOTCONFIG_OVERRIDE- allow overriding thecore.rootConfigserviceENABLE_CORE_DATABASE_OVERRIDE- allow overriding thecore.databaseserviceENABLE_CORE_DISCOVERY_OVERRIDE- allow overriding thecore.discoveryserviceENABLE_CORE_HTTPAUTH_OVERRIDE- allow overriding thecore.httpAuthserviceENABLE_CORE_HTTPROUTER_OVERRIDE- allow overriding thecore.httpRouterserviceENABLE_CORE_LIFECYCLE_OVERRIDE- allow overriding thecore.lifecycleserviceENABLE_CORE_LOGGER_OVERRIDE- allow overriding thecore.loggerserviceENABLE_CORE_PERMISSIONS_OVERRIDE- allow overriding thecore.permissionsserviceENABLE_CORE_ROOTHEALTH_OVERRIDE- allow overriding thecore.rootHealthserviceENABLE_CORE_ROOTHTTPROUTER_OVERRIDE- allow overriding thecore.rootHttpRouterserviceENABLE_CORE_ROOTLIFECYCLE_OVERRIDE- allow overriding thecore.rootLifecycleserviceENABLE_CORE_SCHEDULER_OVERRIDE- allow overriding thecore.schedulerserviceENABLE_CORE_USERINFO_OVERRIDE- allow overriding thecore.userInfoserviceENABLE_CORE_URLREADER_OVERRIDE- allow overriding thecore.urlReaderserviceENABLE_EVENTS_SERVICE_OVERRIDE- allow overriding theevents.serviceservice
Overriding the provided authentication module
Developer Hub ships with an opinionated authentication module setup that supports many use-cases. However it is also possible to disable this authentication module entirely and compose an authentication solution from a set of dynamic frontend and backend plugins. This requires disabling the provided authentication module so it doesn't conflict with any custom authentication configuration.
ENABLE_AUTH_PROVIDER_MODULE_OVERRIDE- set to "true" to disable the Developer Hub provided backend authentication module