Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bygger backend as Formio API proxy #1197

Merged
merged 4 commits into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .nais/bygger/nais.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ spec:
value: {{skjemabygging-proxy-url}}
- name: SKJEMABYGGING_PROXY_CLIENT_ID
value: {{skjemabygging-proxy-client-id}}
- name: FORMIO_PROJECT_NAME
value: jvcemxwcpghcqjn
envFrom:
{{#each secrets as |secret|}}
- secret: {{secret}}
Expand Down
4 changes: 3 additions & 1 deletion packages/bygger-backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,17 @@
"test:coverage": "vitest --run --coverage"
},
"dependencies": {
"@navikt/skjemadigitalisering-shared-domain": "1.0.0",
"@octokit/auth-app": "^7.1.0",
"@octokit/rest": "^20.1.1",
"@octokit/webhooks-types": "^7.5.1",
"@navikt/skjemadigitalisering-shared-domain": "1.0.0",
"bcryptjs": "^2.4.3",
"csv-stringify": "^6.5.0",
"dotenv": "^16.4.5",
"express": "^4.19.2",
"express-correlation-id": "^2.0.1",
"express-rate-limit": "^7.3.0",
"http-proxy": "^1.18.1",
"jose": "^5.3.0",
"jsonwebtoken": "^9.0.2",
"luxon": "^3.4.4",
Expand All @@ -38,6 +39,7 @@
"@types/bcryptjs": "^2.4.6",
"@types/express": "^4.17.21",
"@types/express-correlation-id": "^1.2.6",
"@types/http-proxy": "^1.17.14",
"@types/jsonwebtoken": "^9.0.6",
"@types/luxon": "^3.4.2",
"@types/memorystream": "^0.3.4",
Expand Down
1 change: 1 addition & 0 deletions packages/bygger-backend/src/config/development.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export const devSkjemabyggingProxy: Partial<SkjemabyggingProxyConfig> = {

export const devFormio: Partial<FormioConfig> = {
projectUrl: 'https://formio-api.intern.dev.nav.no/jvcemxwcpghcqjn',
projectName: 'jvcemxwcpghcqjn',
roleIds: {
administrator: '628ca77305690db58c974d04',
authenticated: '628ca77305690db58c974d09',
Expand Down
1 change: 1 addition & 0 deletions packages/bygger-backend/src/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ const config: ConfigType = {
},
formio: {
projectUrl: env('FORMIO_PROJECT_URL', devFormio.projectUrl),
projectName: env('FORMIO_PROJECT_NAME', devFormio.projectName),
projectId: env('FORMIO_PROJECT_ID'),
roleIds: {
administrator: env('FORMIO_ROLE_ID_ADMINISTRATOR', devFormio.roleIds?.administrator),
Expand Down
1 change: 1 addition & 0 deletions packages/bygger-backend/src/config/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export type PublishRepoConfig = {

export type FormioConfig = {
projectUrl: string;
projectName: string;
projectId: string;
roleIds: Record<string, string>;
formIds: Record<string, string>;
Expand Down
3 changes: 2 additions & 1 deletion packages/bygger-backend/src/routers/api/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ import { Response } from 'express';
import appConfig from '../../config';
import { createFormioJwt } from '../../middleware/authHandler';
import { ByggerRequest } from '../../types';
import { getByggerUrl } from '../../util/url';

const config = (req: ByggerRequest, res: Response) => {
const user = appConfig.isDevelopment ? undefined : req.getUser?.();
if (user) {
res.header('Bygger-Formio-Token', createFormioJwt(user));
}
res.json({
formioProjectUrl: appConfig.formio.projectUrl,
formioProjectUrl: `${getByggerUrl(req)}/${appConfig.formio.projectName}`,
fyllutBaseUrl: appConfig.fyllut.baseUrl,
pusherCluster: appConfig.pusher.cluster,
pusherKey: appConfig.pusher.key,
Expand Down
49 changes: 49 additions & 0 deletions packages/bygger-backend/src/routers/formio-proxy/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import express from 'express';
import httpProxy from 'http-proxy';
import config from '../../config';
import { logger } from '../../logging/logger';

const formioProjectUrl = config.formio.projectUrl;
const proxy = httpProxy.createProxyServer({});

const formioProxyRouter = express.Router();

formioProxyRouter.all('*', async (req, res, next) => {
const { method, url } = req;
try {
logger.info(`Formio API proxy ${method} ${url}`, { formioProjectUrl });
proxy.web(
req,
res,
{
target: formioProjectUrl,
changeOrigin: true,
proxyTimeout: 60000,
timeout: 60000,
},
(err) => {
const { message, stack, ...errDetails } = err;
logger.error(`Formio API proxy - error callback: ${message}`, {
method,
url,
formioProjectUrl,
stack,
errDetails,
});
next(err);
},
);
} catch (err: any) {
const { message, stack, ...errDetails } = err;
logger.error(`Formio API proxy - error was thrown: ${message}`, {
method,
url,
formioProjectUrl,
stack,
errDetails,
});
next(err);
}
});

export default formioProxyRouter;
2 changes: 2 additions & 0 deletions packages/bygger-backend/src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ import { buildDirectory, buildDirectoryIndexHtml } from './context.js';
import authHandler from './middleware/authHandler';
import { fsAccessRateLimiter } from './middleware/ratelimit';
import apiRouter from './routers/api';
import formioProxyRouter from './routers/formio-proxy';
import internalRouter from './routers/internal';
import notificationsRouter from './routers/notifications';
import './util/errorToJson';

const app = express();

app.use(`/${config.formio.projectName}`, formioProxyRouter);
app.use(express.json({ limit: '50mb' }));
app.use(express.urlencoded({ extended: true, limit: '50mb' }));
app.use(correlator());
Expand Down
6 changes: 6 additions & 0 deletions packages/bygger-backend/src/util/url.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { Request } from 'express';
import config from '../config';

export const getByggerUrl = (req: Request) => {
return config.isDevelopment ? `http://localhost:${config.port}` : `https://${req.get('host')}`;
};
21 changes: 21 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1439,6 +1439,13 @@
resolved "https://registry.npmjs.org/@types/http-errors/-/http-errors-2.0.1.tgz"
integrity sha512-/K3ds8TRAfBvi5vfjuz8y6+GiAYBZ0x4tXv1Av6CWBWn0IlADc+ZX9pMq7oU0fNQPnBwIZl3rmeLp6SBApbxSQ==

"@types/http-proxy@^1.17.14":
version "1.17.14"
resolved "https://registry.yarnpkg.com/@types/http-proxy/-/http-proxy-1.17.14.tgz#57f8ccaa1c1c3780644f8a94f9c6b5000b5e2eec"
integrity sha512-SSrD0c1OQzlFX7pGu1eXxSEjemej64aaNPRhhVYUGqXh0BtldAAx37MG8btcumvpgKyZp1F5Gn3JkktdxiFv6w==
dependencies:
"@types/node" "*"

"@types/json-schema@^7.0.9":
version "7.0.12"
resolved "https://registry.npmjs.org/@types/json-schema/-/json-schema-7.0.12.tgz"
Expand Down Expand Up @@ -4392,6 +4399,11 @@ [email protected]:
resolved "https://registry.npmjs.org/fn.name/-/fn.name-1.1.0.tgz"
integrity sha512-GRnmB5gPyJpAhTQdSZTSp9uaPSvl09KoYcMQtsB9rQoOmzs9dH6ffeccH+Z+cv6P68Hu5bC6JjRh4Ah/mHSNRw==

follow-redirects@^1.0.0:
version "1.15.6"
resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.15.6.tgz#7f815c0cda4249c74ff09e95ef97c23b5fd0399b"
integrity sha512-wWN62YITEaOpSK584EZXJafH1AGpO8RVgElfkuXbTOrPX4fIfOyEpW/CsiNd8JdYrAoOvafRTOEnvsO++qCqFA==

for-each@^0.3.3:
version "0.3.3"
resolved "https://registry.npmjs.org/for-each/-/for-each-0.3.3.tgz"
Expand Down Expand Up @@ -4907,6 +4919,15 @@ http-proxy-agent@^7.0.2:
agent-base "^7.1.0"
debug "^4.3.4"

http-proxy@^1.18.1:
version "1.18.1"
resolved "https://registry.yarnpkg.com/http-proxy/-/http-proxy-1.18.1.tgz#401541f0534884bbf95260334e72f88ee3976549"
integrity sha512-7mz/721AbnJwIVbnaSv1Cz3Am0ZLT/UBwkC92VlxhXv/k/BBQfM2fXElQNC27BVGr0uwUpplYPQM9LnaBMR5NQ==
dependencies:
eventemitter3 "^4.0.0"
follow-redirects "^1.0.0"
requires-port "^1.0.0"

http-signature@~1.3.6:
version "1.3.6"
resolved "https://registry.npmjs.org/http-signature/-/http-signature-1.3.6.tgz"
Expand Down