From 1010f4d6b120a5ea7267dffc3f28b01abaf90756 Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Mon, 1 Jun 2020 17:10:50 +0200 Subject: [PATCH] plugins: added initial mock-idp-backend --- plugins/mock-idp-backend/.eslintrc.js | 3 + plugins/mock-idp-backend/README.md | 7 ++ plugins/mock-idp-backend/package.json | 41 ++++++++++ plugins/mock-idp-backend/src/index.ts | 17 ++++ plugins/mock-idp-backend/src/run.ts | 80 +++++++++++++++++++ plugins/mock-idp-backend/src/service/index.ts | 17 ++++ .../src/service/router.test.ts | 36 +++++++++ .../mock-idp-backend/src/service/router.ts | 34 ++++++++ plugins/mock-idp-backend/src/setupTests.ts | 17 ++++ plugins/mock-idp-backend/tsconfig.json | 15 ++++ 10 files changed, 267 insertions(+) create mode 100644 plugins/mock-idp-backend/.eslintrc.js create mode 100644 plugins/mock-idp-backend/README.md create mode 100644 plugins/mock-idp-backend/package.json create mode 100644 plugins/mock-idp-backend/src/index.ts create mode 100644 plugins/mock-idp-backend/src/run.ts create mode 100644 plugins/mock-idp-backend/src/service/index.ts create mode 100644 plugins/mock-idp-backend/src/service/router.test.ts create mode 100644 plugins/mock-idp-backend/src/service/router.ts create mode 100644 plugins/mock-idp-backend/src/setupTests.ts create mode 100644 plugins/mock-idp-backend/tsconfig.json diff --git a/plugins/mock-idp-backend/.eslintrc.js b/plugins/mock-idp-backend/.eslintrc.js new file mode 100644 index 0000000000..16a033dbc6 --- /dev/null +++ b/plugins/mock-idp-backend/.eslintrc.js @@ -0,0 +1,3 @@ +module.exports = { + extends: [require.resolve('@backstage/cli/config/eslint.backend')], +}; diff --git a/plugins/mock-idp-backend/README.md b/plugins/mock-idp-backend/README.md new file mode 100644 index 0000000000..c1925e326f --- /dev/null +++ b/plugins/mock-idp-backend/README.md @@ -0,0 +1,7 @@ +# Mock IdP Backend + +Mock backend for demonstrating 3rd party identity provider flow with SAML 2.0. + +## Links + +- (The Backstage homepage)[https://backstage.io] diff --git a/plugins/mock-idp-backend/package.json b/plugins/mock-idp-backend/package.json new file mode 100644 index 0000000000..e4ff95e46a --- /dev/null +++ b/plugins/mock-idp-backend/package.json @@ -0,0 +1,41 @@ +{ + "name": "@backstage/plugin-mock-idp-backend", + "version": "0.1.1-alpha.6", + "main": "dist", + "types": "src/index.ts", + "license": "Apache-2.0", + "private": true, + "scripts": { + "start": "tsc-watch --onFirstSuccess \"cross-env NODE_ENV=development nodemon dist/run.js\"", + "build": "tsc", + "lint": "backstage-cli lint", + "test": "backstage-cli test", + "prepack": "backstage-cli prepack", + "postpack": "backstage-cli postpack", + "clean": "backstage-cli clean" + }, + "dependencies": { + "@backstage/backend-common": "^0.1.1-alpha.6", + "compression": "^1.7.4", + "cors": "^2.8.5", + "express": "^4.17.1", + "express-promise-router": "^3.0.3", + "fs-extra": "^9.0.0", + "helmet": "^3.22.0", + "morgan": "^1.10.0", + "winston": "^3.2.1", + "yn": "^4.0.0" + }, + "devDependencies": { + "@backstage/cli": "^0.1.1-alpha.6", + "@types/supertest": "^2.0.8", + "supertest": "^4.0.2", + "tsc-watch": "^4.2.3" + }, + "files": [ + "dist" + ], + "nodemonConfig": { + "watch": "./dist" + } +} diff --git a/plugins/mock-idp-backend/src/index.ts b/plugins/mock-idp-backend/src/index.ts new file mode 100644 index 0000000000..7612c392a2 --- /dev/null +++ b/plugins/mock-idp-backend/src/index.ts @@ -0,0 +1,17 @@ +/* + * Copyright 2020 Spotify AB + * + * 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. + */ + +export * from './service/router'; diff --git a/plugins/mock-idp-backend/src/run.ts b/plugins/mock-idp-backend/src/run.ts new file mode 100644 index 0000000000..ea67d335fd --- /dev/null +++ b/plugins/mock-idp-backend/src/run.ts @@ -0,0 +1,80 @@ +/* + * Copyright 2020 Spotify AB + * + * 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 { + errorHandler, + getRootLogger, + notFoundHandler, + requestLoggingHandler, +} from '@backstage/backend-common'; +import compression from 'compression'; +import cors from 'cors'; +import express from 'express'; +import helmet from 'helmet'; +import { Server } from 'http'; +import { Logger } from 'winston'; +import { createRouter } from './service'; + +export type ServerConfig = { + port: number; + logger: Logger; +}; + +function readConfig() { + const port = Number(process.env.PLUGIN_PORT) || 3003; + const logger = getRootLogger().child({ service: 'mock-idp-backend' }); + return { port, logger }; +} + +async function startStandaloneServer(config: ServerConfig): Promise { + const { port, logger } = config; + logger.debug('Creating application...'); + + const app = express(); + + app.use(helmet()); + app.use(cors()); + app.use(compression()); + app.use(express.json()); + app.use(requestLoggingHandler()); + app.use(await createRouter({ logger })); + app.use(notFoundHandler()); + app.use(errorHandler()); + + logger.debug('Starting application server...'); + + process.on('SIGINT', () => { + logger.info('CTRL+C pressed; exiting.'); + process.exit(0); + }); + + return await new Promise((resolve, reject) => { + const server = app.listen(port, (err?: Error) => { + if (err) { + reject(err); + return; + } + + logger.info(`Listening on port ${port}`); + resolve(server); + }); + }); +} + +startStandaloneServer(readConfig()).catch(err => { + console.error(err.stack || err); + process.exit(1); +}); diff --git a/plugins/mock-idp-backend/src/service/index.ts b/plugins/mock-idp-backend/src/service/index.ts new file mode 100644 index 0000000000..38fbb697c4 --- /dev/null +++ b/plugins/mock-idp-backend/src/service/index.ts @@ -0,0 +1,17 @@ +/* + * Copyright 2020 Spotify AB + * + * 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. + */ + +export { createRouter } from './router'; diff --git a/plugins/mock-idp-backend/src/service/router.test.ts b/plugins/mock-idp-backend/src/service/router.test.ts new file mode 100644 index 0000000000..192baeebb9 --- /dev/null +++ b/plugins/mock-idp-backend/src/service/router.test.ts @@ -0,0 +1,36 @@ +/* + * Copyright 2020 Spotify AB + * + * 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 { getVoidLogger } from '@backstage/backend-common'; +import { createRouter } from './router'; +import express from 'express'; +import request from 'supertest'; + +async function makeApp() { + const router = await createRouter({ logger: getVoidLogger() }); + const app = express(); + app.use(router); + return app; +} + +describe('router', () => { + it('should echo', async () => { + const app = await makeApp(); + const response = await request(app).get('/echo'); + expect(response.status).toEqual(200); + expect(response.text).toEqual('echo'); + }); +}); diff --git a/plugins/mock-idp-backend/src/service/router.ts b/plugins/mock-idp-backend/src/service/router.ts new file mode 100644 index 0000000000..62bffb050d --- /dev/null +++ b/plugins/mock-idp-backend/src/service/router.ts @@ -0,0 +1,34 @@ +/* + * Copyright 2020 Spotify AB + * + * 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 Router from 'express-promise-router'; +import { Logger } from 'winston'; + +export interface RouterOptions { + logger: Logger; +} + +export async function createRouter(options: RouterOptions) { + const { logger } = options; + const router = Router(); + + router.get('/echo', (_req, res) => { + logger.info('sending echo'); + res.send('echo'); + }); + + return router; +} diff --git a/plugins/mock-idp-backend/src/setupTests.ts b/plugins/mock-idp-backend/src/setupTests.ts new file mode 100644 index 0000000000..ba33cf996b --- /dev/null +++ b/plugins/mock-idp-backend/src/setupTests.ts @@ -0,0 +1,17 @@ +/* + * Copyright 2020 Spotify AB + * + * 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. + */ + +export {}; diff --git a/plugins/mock-idp-backend/tsconfig.json b/plugins/mock-idp-backend/tsconfig.json new file mode 100644 index 0000000000..015a967f76 --- /dev/null +++ b/plugins/mock-idp-backend/tsconfig.json @@ -0,0 +1,15 @@ +{ + "include": ["src"], + "compilerOptions": { + "outDir": "dist", + "incremental": true, + "sourceMap": true, + "declaration": true, + "strict": true, + "target": "es2019", + "module": "commonjs", + "esModuleInterop": true, + "lib": ["es2019"], + "types": ["node", "jest"] + } +}