devtools-backend: migate to support new auth services
Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
@@ -35,13 +35,24 @@ export const devtoolsPlugin = createBackendPlugin({
|
||||
logger: coreServices.logger,
|
||||
permissions: coreServices.permissions,
|
||||
httpRouter: coreServices.httpRouter,
|
||||
discovery: coreServices.discovery,
|
||||
httpAuth: coreServices.httpAuth,
|
||||
},
|
||||
async init({ config, logger, permissions, httpRouter }) {
|
||||
async init({
|
||||
config,
|
||||
logger,
|
||||
permissions,
|
||||
httpRouter,
|
||||
discovery,
|
||||
httpAuth,
|
||||
}) {
|
||||
httpRouter.use(
|
||||
await createRouter({
|
||||
config,
|
||||
logger: loggerToWinstonLogger(logger),
|
||||
permissions,
|
||||
discovery,
|
||||
httpAuth,
|
||||
}),
|
||||
);
|
||||
},
|
||||
|
||||
@@ -20,6 +20,7 @@ import express from 'express';
|
||||
import request from 'supertest';
|
||||
import { PermissionEvaluator } from '@backstage/plugin-permission-common';
|
||||
import { createRouter } from './router';
|
||||
import { mockServices } from '@backstage/backend-test-utils';
|
||||
|
||||
const mockedAuthorize: jest.MockedFunction<PermissionEvaluator['authorize']> =
|
||||
jest.fn();
|
||||
@@ -49,6 +50,7 @@ describe('createRouter', () => {
|
||||
],
|
||||
},
|
||||
}),
|
||||
discovery: mockServices.discovery(),
|
||||
permissions: permissionEvaluator,
|
||||
});
|
||||
app = express().use(router);
|
||||
|
||||
@@ -13,10 +13,7 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
import {
|
||||
AuthorizeResult,
|
||||
PermissionEvaluator,
|
||||
} from '@backstage/plugin-permission-common';
|
||||
import { AuthorizeResult } from '@backstage/plugin-permission-common';
|
||||
import {
|
||||
devToolsConfigReadPermission,
|
||||
devToolsExternalDependenciesReadPermission,
|
||||
@@ -29,17 +26,26 @@ import { DevToolsBackendApi } from '../api';
|
||||
import { Logger } from 'winston';
|
||||
import { NotAllowedError } from '@backstage/errors';
|
||||
import Router from 'express-promise-router';
|
||||
import { errorHandler } from '@backstage/backend-common';
|
||||
import {
|
||||
createLegacyAuthAdapters,
|
||||
errorHandler,
|
||||
} from '@backstage/backend-common';
|
||||
import express from 'express';
|
||||
import { getBearerTokenFromAuthorizationHeader } from '@backstage/plugin-auth-node';
|
||||
import { createPermissionIntegrationRouter } from '@backstage/plugin-permission-node';
|
||||
import {
|
||||
DiscoveryService,
|
||||
HttpAuthService,
|
||||
PermissionsService,
|
||||
} from '@backstage/backend-plugin-api';
|
||||
|
||||
/** @public */
|
||||
export interface RouterOptions {
|
||||
devToolsBackendApi?: DevToolsBackendApi;
|
||||
logger: Logger;
|
||||
config: Config;
|
||||
permissions: PermissionEvaluator;
|
||||
permissions: PermissionsService;
|
||||
discovery: DiscoveryService;
|
||||
httpAuth?: HttpAuthService;
|
||||
}
|
||||
|
||||
/** @public */
|
||||
@@ -48,6 +54,8 @@ export async function createRouter(
|
||||
): Promise<express.Router> {
|
||||
const { logger, config, permissions } = options;
|
||||
|
||||
const { httpAuth } = createLegacyAuthAdapters(options);
|
||||
|
||||
const devToolsBackendApi =
|
||||
options.devToolsBackendApi || new DevToolsBackendApi(logger, config);
|
||||
|
||||
@@ -64,16 +72,10 @@ export async function createRouter(
|
||||
});
|
||||
|
||||
router.get('/info', async (req, response) => {
|
||||
const token = getBearerTokenFromAuthorizationHeader(
|
||||
req.header('authorization'),
|
||||
);
|
||||
|
||||
const decision = (
|
||||
await permissions.authorize(
|
||||
[{ permission: devToolsInfoReadPermission }],
|
||||
{
|
||||
token,
|
||||
},
|
||||
{ credentials: await httpAuth.credentials(req) },
|
||||
)
|
||||
)[0];
|
||||
|
||||
@@ -87,16 +89,10 @@ export async function createRouter(
|
||||
});
|
||||
|
||||
router.get('/config', async (req, response) => {
|
||||
const token = getBearerTokenFromAuthorizationHeader(
|
||||
req.header('authorization'),
|
||||
);
|
||||
|
||||
const decision = (
|
||||
await permissions.authorize(
|
||||
[{ permission: devToolsConfigReadPermission }],
|
||||
{
|
||||
token,
|
||||
},
|
||||
{ credentials: await httpAuth.credentials(req) },
|
||||
)
|
||||
)[0];
|
||||
|
||||
@@ -110,16 +106,10 @@ export async function createRouter(
|
||||
});
|
||||
|
||||
router.get('/external-dependencies', async (req, response) => {
|
||||
const token = getBearerTokenFromAuthorizationHeader(
|
||||
req.header('authorization'),
|
||||
);
|
||||
|
||||
const decision = (
|
||||
await permissions.authorize(
|
||||
[{ permission: devToolsExternalDependenciesReadPermission }],
|
||||
{
|
||||
token,
|
||||
},
|
||||
{ credentials: await httpAuth.credentials(req) },
|
||||
)
|
||||
)[0];
|
||||
|
||||
|
||||
@@ -50,6 +50,7 @@ export async function startStandaloneServer(
|
||||
logger,
|
||||
config,
|
||||
permissions,
|
||||
discovery,
|
||||
});
|
||||
|
||||
let service = createServiceBuilder(module)
|
||||
|
||||
Reference in New Issue
Block a user