Skip to content

POC for bruk av Gotenberg til å generere PDF av søknad #1380

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

Closed
wants to merge 10 commits into from
Closed
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/fyllut/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ spec:
value: {{skjemabygging-proxy-url}}
- name: SKJEMABYGGING_PROXY_CLIENT_ID
value: {{skjemabygging-proxy-client-id}}
- name: GOTENBERG_URL
value: {{gotenberg-url}}
- name: SEND_INN_TOKEN_X_CLIENT_ID
value: {{send-inn-token-x-client-id}}
- name: SEND_INN_HOST
Expand Down
4 changes: 4 additions & 0 deletions .nais/fyllut/dev-delingslenke.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ foersteside-url: https://www.nav.no/soknader/api/forsteside
dekorator-url: https://www.nav.no/dekoratoren?simple=true
skjemabygging-proxy-client-id: 95170319-b4d7-4190-8271-118ed19bafbf
skjemabygging-proxy-url: https://skjemabygging-proxy.dev-fss-pub.nais.io
gotenberg-url: http://upload-convert-to-pdf.fyllut-sendinn
send-inn-token-x-client-id: dev-gcp:team-soknad:innsending-api
send-inn-host: http://innsending-api.team-soknad
forms-api-url: https://forms-api.nav.no
Expand Down Expand Up @@ -46,6 +47,9 @@ accessPolicy:
- application: nav-dekoratoren
namespace: personbruker
cluster: dev-gcp
- application: upload-convert-to-pdf
namespace: fyllut-sendinn
cluster: dev-gcp
external:
- host: skjemabygging-proxy.dev-fss-pub.nais.io
- host: formio-api.intern.dev.nav.no
Expand Down
4 changes: 4 additions & 0 deletions .nais/fyllut/dev.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ foersteside-url: https://www.nav.no/soknader/api/forsteside
dekorator-url: https://www.nav.no/dekoratoren?simple=true
skjemabygging-proxy-client-id: 95170319-b4d7-4190-8271-118ed19bafbf
skjemabygging-proxy-url: https://skjemabygging-proxy.dev-fss-pub.nais.io
gotenberg-url: http://upload-convert-to-pdf.fyllut-sendinn
send-inn-token-x-client-id: dev-gcp:team-soknad:innsending-api
send-inn-host: http://innsending-api.team-soknad
kodeverk:
Expand Down Expand Up @@ -40,6 +41,9 @@ accessPolicy:
- application: forms-api
namespace: fyllut-sendinn
cluster: dev-gcp
- application: upload-convert-to-pdf
namespace: fyllut-sendinn
cluster: dev-gcp
external:
- host: skjemabygging-proxy.dev-fss-pub.nais.io
- host: formio-api.intern.dev.nav.no
Expand Down
4 changes: 4 additions & 0 deletions .nais/fyllut/preprod-alt.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ foersteside-url: https://www.nav.no/soknader/api/forsteside
dekorator-url: https://www.nav.no/dekoratoren?simple=true
skjemabygging-proxy-client-id: 95170319-b4d7-4190-8271-118ed19bafbf
skjemabygging-proxy-url: https://skjemabygging-proxy.dev-fss-pub.nais.io
gotenberg-url: http://upload-convert-to-pdf.fyllut-sendinn
send-inn-token-x-client-id: dev-gcp:team-soknad:innsending-api
send-inn-host: http://innsending-api.team-soknad
kodeverk:
Expand Down Expand Up @@ -47,6 +48,9 @@ accessPolicy:
- application: forms-api
namespace: fyllut-sendinn
cluster: dev-gcp
- application: upload-convert-to-pdf
namespace: fyllut-sendinn
cluster: dev-gcp
external:
- host: skjemabygging-proxy.dev-fss-pub.nais.io
- host: formio-api.intern.dev.nav.no
Expand Down
4 changes: 4 additions & 0 deletions .nais/fyllut/preprod.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ foersteside-url: https://www.nav.no/soknader/api/forsteside
dekorator-url: https://www.nav.no/dekoratoren?simple=true
skjemabygging-proxy-client-id: 95170319-b4d7-4190-8271-118ed19bafbf
skjemabygging-proxy-url: https://skjemabygging-proxy.dev-fss-pub.nais.io
gotenberg-url: http://upload-convert-to-pdf.fyllut-sendinn
send-inn-token-x-client-id: dev-gcp:team-soknad:innsending-api
send-inn-host: http://innsending-api.team-soknad
kodeverk:
Expand Down Expand Up @@ -48,6 +49,9 @@ accessPolicy:
- application: forms-api
namespace: fyllut-sendinn
cluster: dev-gcp
- application: upload-convert-to-pdf
namespace: fyllut-sendinn
cluster: dev-gcp
external:
- host: skjemabygging-proxy.dev-fss-pub.nais.io
- host: formio-api.intern.dev.nav.no
Expand Down
4 changes: 4 additions & 0 deletions .nais/fyllut/prod.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ foersteside-url: https://www.nav.no/soknader/api/forsteside
dekorator-url: https://www.nav.no/dekoratoren?simple=true
skjemabygging-proxy-client-id: 031863f2-9881-44af-ace5-6b5ab52afeb1
skjemabygging-proxy-url: https://skjemabygging-proxy.prod-fss-pub.nais.io
gotenberg-url: http://upload-convert-to-pdf.fyllut-sendinn
send-inn-token-x-client-id: prod-gcp:team-soknad:innsending-api
send-inn-host: http://innsending-api.team-soknad
kodeverk:
Expand Down Expand Up @@ -45,6 +46,9 @@ accessPolicy:
- application: forms-api
namespace: fyllut-sendinn
cluster: prod-gcp
- application: upload-convert-to-pdf
namespace: fyllut-sendinn
cluster: prod-gcp
external:
- host: skjemabygging-proxy.prod-fss-pub.nais.io
- host: formio-api.intern.nav.no
2 changes: 1 addition & 1 deletion mocks/mocks/collections.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"get-translations:success",
"post-azure-openid-token:success",
"get-kodeverk-currencies:success",
"post-exstream-pdf:success",
"post-gotenberg-pdf:success",
"get-tokenx-endpoint:success",
"post-tokenx:success",
"post-soknad:success",
Expand Down
4 changes: 2 additions & 2 deletions mocks/mocks/routes/skjemabygging-proxy.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module.exports = [
{
id: 'post-exstream-pdf',
url: '/skjemabygging-proxy/exstream',
id: 'post-gotenberg-pdf',
url: '/skjemabygging-proxy/extream',
method: 'POST',
variants: [
{
Expand Down
3 changes: 2 additions & 1 deletion packages/bygger/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
},
"devDependencies": {
"@testing-library/react": "^16.0.1",
"lodash.template": "^4.5.0"
"lodash.template": "^4.5.0",
"vite-plugin-static-copy": "^2.2.0"
}
}
1 change: 1 addition & 0 deletions packages/fyllut-backend/.env.test
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ TOKEN_X_WELL_KNOWN_URL=http://127.0.0.1:3300/tokenx/.well-known
AMPLITUDE_API_ENDPOINT=http://127.0.0.1:3300/amplitude/collect-auto
AMPLITUDE_DISABLE_BATCH=true
# FYLLUT_BACKEND_LOGLEVEL=debug
GOTENBERG_URL=https://convert-to-pdf.intern.dev.nav.no
3 changes: 3 additions & 0 deletions packages/fyllut-backend/icons/nav-logo.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 6 additions & 10 deletions packages/fyllut-backend/src/app.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,15 +224,14 @@ describe('app', () => {
skjemabyggingproxyScope.done();
});

it('Performs TokenX exchange and retrieves pdf from exstream before calling SendInn', async () => {
it('Performs TokenX exchange and retrieves pdf from Gotenberg before calling SendInn', async () => {
const key = await generateJwk();
nock('https://testoidc.unittest.no')
.get('/idporten-oidc-provider/jwk')
.reply(200, { keys: [key.toJSON(false)] });

const innsendingsId = '65ed0008-ec72-4c90-8b44-165d3c265da0';
const sendInnLocation = 'http://www.unittest.nav.no/sendInn/123';
const azureTokenEndpoint = process.env.AZURE_OPENID_CONFIG_TOKEN_ENDPOINT!;
const tokenxEndpoint = 'http://tokenx-unittest.nav.no/token';
const applicationData = {
form: {
Expand All @@ -248,13 +247,11 @@ describe('app', () => {
submissionMethod: 'digital',
innsendingsId,
};

const azureOpenidScope = nock(extractHost(azureTokenEndpoint))
.post(extractPath(azureTokenEndpoint))
.reply(200, { access_token: 'azure-access-token' });
const skjemabyggingproxyScope = nock(process.env.SKJEMABYGGING_PROXY_URL as string)
.post('/exstream')
.reply(200, { data: { result: [{ content: { data: '' } }] } });
const mockSuccessResponse = new Uint8Array([37, 80, 68, 70, 45]);
console.log(process.env.GOTENBERG_URL as string);
const skjemabyggingproxyScope = nock(process.env.GOTENBERG_URL as string)
.post('/forms/chromium/convert/html')
.reply(200, mockSuccessResponse);
const tokenxWellKnownScope = nock(extractHost(tokenxConfig?.wellKnownUrl))
.get(extractPath(tokenxConfig?.wellKnownUrl))
.reply(200, { token_endpoint: tokenxEndpoint });
Expand All @@ -273,7 +270,6 @@ describe('app', () => {
expect(res.status).toBe(201);
expect(res.headers['location']).toMatch(sendInnLocation);

azureOpenidScope.done();
skjemabyggingproxyScope.done();
tokenxWellKnownScope.done();
tokenEndpointNockScope.done();
Expand Down
4 changes: 3 additions & 1 deletion packages/fyllut-backend/src/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ const localDevelopmentConfig: DefaultConfig = {
decoratorUrl: 'https://www.nav.no/dekoratoren?simple=true',
skjemabyggingProxyUrl: process.env.SKJEMABYGGING_PROXY_URL || 'https://skjemabygging-proxy.dev-fss-pub.nais.io',
skjemabyggingProxyClientId: '95170319-b4d7-4190-8271-118ed19bafbf',
gotenbergUrl: process.env.GOTENBERG_URL || 'https://convert-to-pdf.intern.dev.nav.no',
azureOpenidTokenEndpoint:
process.env.AZURE_OPENID_CONFIG_TOKEN_ENDPOINT ||
'https://login.microsoftonline.com/966ac572-f5b7-4bbe-aa88-c76419c0f851/oauth2/v2.0/token',
Expand Down Expand Up @@ -125,6 +126,7 @@ const defaultConfig: DefaultConfig = {
decoratorUrl: process.env.DECORATOR_URL!,
skjemabyggingProxyUrl: process.env.SKJEMABYGGING_PROXY_URL!,
skjemabyggingProxyClientId: process.env.SKJEMABYGGING_PROXY_CLIENT_ID!,
gotenbergUrl: process.env.GOTENBERG_URL!,
azureOpenidTokenEndpoint: process.env.AZURE_OPENID_CONFIG_TOKEN_ENDPOINT!,
clientId: process.env.AZURE_APP_CLIENT_ID!,
skjemaDir: process.env.SKJEMA_DIR!,
Expand Down Expand Up @@ -171,4 +173,4 @@ const checkConfigConsistency = (config: ConfigType, logError = logger.error, exi
}
};

export { checkConfigConsistency, config };
export { checkConfigConsistency, config, defaultConfig };
1 change: 1 addition & 0 deletions packages/fyllut-backend/src/config/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export type DefaultConfig = {
decoratorUrl: string;
skjemabyggingProxyUrl: string;
skjemabyggingProxyClientId: string;
gotenbergUrl: string;
azureOpenidTokenEndpoint: string;
clientId: string;
skjemaDir?: string;
Expand Down
45 changes: 0 additions & 45 deletions packages/fyllut-backend/src/routers/api/exstream.test.ts

This file was deleted.

83 changes: 83 additions & 0 deletions packages/fyllut-backend/src/routers/api/gotenberg.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import nock from 'nock';
import { config } from '../../config/config';
import { mockRequest, mockResponse } from '../../test/testHelpers';
import gotenberg from './gotenberg';

const formTitle = 'testskjema';
const defaultBody = {
form: JSON.stringify({ title: formTitle, components: [], properties: { skjemanummer: 'NAV 12.34-56' } }),
submission: JSON.stringify({ data: {} }),
submissionMethod: 'paper',
translations: JSON.stringify({}),
language: 'nb-NO',
};
const mockSuccessResponse = new Uint8Array([37, 80, 68, 70, 45]); // A minimal valid PDF file header

/*
const hexToString = hex => {
let str = '';
for (let i = 0; i < hex.length; i += 2)
str += String.fromCharCode(parseInt(hex.substr(i, 2), 16));
return str;
}
*/

describe('gotenberg', () => {
afterEach(() => {
expect(nock.isDone()).toBe(true);
});

it('decodes and sends the pdf on success', async () => {
//, 'Content-Disposition': 'attachment; filename=\"index.html\"'
const url = config.gotenbergUrl;
//const url = config.gotenbergUrl;
const uri = '/forms/chromium/convert/html';

console.log(`${url}${uri}`);
const gotenbergScope = nock(`${url}` as string)
.post(`${uri}`)
.matchHeader('Content-Type', 'multipart/form-data')
.reply(200, mockSuccessResponse);

nock.recorder.rec();
console.log(gotenbergScope.activeMocks());

// Test
try {
const response = await fetch(`${url}${uri}`, {
method: 'POST',
//headers: { 'Accept': 'application/pdf, text/plain', 'Content-Type': 'multipart/form-data' },
body: JSON.stringify(defaultBody),
});
console.log(response.status);
} catch (e) {
console.error(e);
}
// end test

const req = mockRequest({ headers: {}, body: defaultBody });
const res = mockResponse();
const next = vi.fn();
await gotenberg.post(req, res, next);
expect(next).not.toHaveBeenCalled();
expect(res.send).toHaveBeenCalledTimes(1);
expect(res.send).toHaveReturned();
gotenbergScope.done();
});

it('calls next if skjemabygging-proxy returns error', async () => {
const url = config.gotenbergUrl;
const gotenbergScope = nock(url).post('/forms/chromium/convert/html').reply(500, 'error body');
const req = mockRequest({ headers: {}, body: defaultBody });
const res = mockResponse();
const next = vi.fn();
await gotenberg.post(req, res, next);

//expect(next).toHaveBeenCalledTimes(1);
const error = next.mock.calls[0][0];
expect(error?.message).toBe('Generering av PDF feilet');
expect(error?.render_html).toBe(true);
expect(res.send).not.toHaveBeenCalled();
gotenbergScope.done();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
Submission,
} from '@navikt/skjemadigitalisering-shared-domain';
import { NextFunction, Request, Response } from 'express';
import { base64Decode } from '../../utils/base64';
import { htmlResponseError } from '../../utils/errorHandling';
import { logErrorWithStacktrace } from '../../utils/errors';
import { createPdf } from './helpers/pdfService';
Expand All @@ -28,7 +27,7 @@ const parseBody = (
return { form, submission, submissionMethod, translations, language };
};

const exstream = {
const gotenberg = {
post: async (req: Request, res: Response, next: NextFunction) => {
try {
const { form, submission, submissionMethod, translations, language } = parseBody(req);
Expand All @@ -42,8 +41,9 @@ const exstream = {
translations,
languageCode,
);
res.contentType(pdf.contentType);
res.send(base64Decode(pdf.data));

res.setHeader('Content-Type', 'application/pdf');
res.send(Buffer.from(new Uint8Array(pdf)));
} catch (e) {
logErrorWithStacktrace(e as Error);
const createPdfError = htmlResponseError('Generering av PDF feilet');
Expand All @@ -52,4 +52,4 @@ const exstream = {
},
};

export default exstream;
export default gotenberg;
Loading
Loading