From 2cb3a59c739524f8ebe8984bb770a8ea6de60cda Mon Sep 17 00:00:00 2001 From: Denys Bohdan Date: Tue, 18 Jun 2024 12:22:03 +0200 Subject: [PATCH 1/2] STCLI-246 Add a proxy server to overcome issues with cookies SameSite policy. --- CHANGELOG.md | 2 ++ doc/commands.md | 6 ++++++ lib/commands/common-options.js | 11 +++++++++++ lib/commands/serve.js | 18 ++++++++++++++++++ lib/platform/tenant-config.js | 1 + lib/run-proxy.js | 17 +++++++++++++++++ package.json | 1 + 7 files changed, 56 insertions(+) create mode 100644 lib/run-proxy.js diff --git a/CHANGELOG.md b/CHANGELOG.md index f8152f3c..6c961038 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ## 3.2.0 IN PROGRESS +* Add a proxy server to overcome issues with cookies SameSite policy. Refs STCLI-246. + ## [3.1.0](https://github.com/folio-org/stripes-cli/tree/v3.1.0) (2024-03-12) [Full Changelog](https://github.com/folio-org/stripes-cli/compare/v3.0.0...v3.1.0) diff --git a/doc/commands.md b/doc/commands.md index fd41059a..3a4a1706 100644 --- a/doc/commands.md +++ b/doc/commands.md @@ -1131,6 +1131,8 @@ Option | Description | Type | Notes `--port` | Development server port | number | default: 3000 `--stripesConfig` | Stripes config JSON | string | supports stdin `--tenant` | Specify a tenant ID | string | +`--startProxy` | Start a local proxy server between the platform and okapi | boolean | default: false +`--proxyPort` | Port number for the proxy server | number | default: 3010 Examples: @@ -1146,6 +1148,10 @@ Serve a build previously created with "stripes build": ``` $ stripes serve --existing-build output ``` +Serve a platform with a local proxy server that points to remove okapi server: +``` +$ stripes serve --startProxy --proxyPort 3010 --okapi http://some-okapi-server.folio.org +``` Serve an app (in app context) with a mock backend server": ``` $ stripes serve --mirage diff --git a/lib/commands/common-options.js b/lib/commands/common-options.js index 6d9177f3..f3504609 100644 --- a/lib/commands/common-options.js +++ b/lib/commands/common-options.js @@ -5,6 +5,17 @@ module.exports.serverOptions = { default: 3000, group: 'Server Options:', }, + startProxy: { + type: 'boolean', + describe: 'Start a proxy server', + group: 'Server Options:', + }, + proxyPort: { + type: 'number', + describe: 'Proxy server port', + default: 3010, + group: 'Server Options:', + }, host: { type: 'string', describe: 'Development server host', diff --git a/lib/commands/serve.js b/lib/commands/serve.js index 4d9e6bd0..00300e28 100644 --- a/lib/commands/serve.js +++ b/lib/commands/serve.js @@ -1,4 +1,6 @@ const importLazy = require('import-lazy')(require); +const childProcess = require('child_process'); +const path = require('path'); const { contextMiddleware } = importLazy('../cli/context-middleware'); const { stripesConfigMiddleware } = importLazy('../cli/stripes-config-middleware'); @@ -12,6 +14,15 @@ const server = importLazy('../server'); const serveBuildOptions = Object.assign({}, buildOptions); delete serveBuildOptions.publicPath; +function replaceArgvOkapiWithProxyURL(argv) { + const proxyURL = `http://localhost:${argv.proxyPort}`; + argv.okapi = proxyURL; + + if (argv.stripesConfig?.okapi) { + argv.stripesConfig.okapi.url = proxyURL; + } +} + function serveCommand(argv) { const context = argv.context; // Default serve command to development env @@ -39,6 +50,13 @@ function serveCommand(argv) { return; } + if (argv.startProxy) { + console.info('starting proxy'); + childProcess.fork(path.resolve(context.cliRoot, './lib/run-proxy.js'), [argv.okapi, argv.proxyPort]); + // if we're using a proxy server - we need to pass the proxy host as okapi to Stripes platform + replaceArgvOkapiWithProxyURL(argv); + } + const platform = new StripesPlatform(argv.stripesConfig, context, argv); const webpackOverrides = platform.getWebpackOverrides(context); diff --git a/lib/platform/tenant-config.js b/lib/platform/tenant-config.js index 370c969e..5b0e0864 100644 --- a/lib/platform/tenant-config.js +++ b/lib/platform/tenant-config.js @@ -11,6 +11,7 @@ const defaultConfig = { showPerms: false, hasAllPerms: false, languages: ['en'], + useSecureTokens: false, }, modules: { }, diff --git a/lib/run-proxy.js b/lib/run-proxy.js new file mode 100644 index 00000000..31e62a17 --- /dev/null +++ b/lib/run-proxy.js @@ -0,0 +1,17 @@ +const express = require('express'); +const { createProxyMiddleware } = require('http-proxy-middleware'); + +const app = express(); + +const OKAPI = process.argv[2]; +const PORT = process.argv[3]; + +app.use( + '/', + createProxyMiddleware({ + target: OKAPI, + changeOrigin: true, + }), +); + +app.listen(PORT); diff --git a/package.json b/package.json index 2a8ac27a..57aed750 100644 --- a/package.json +++ b/package.json @@ -37,6 +37,7 @@ "fs-extra": "^11.1.1", "get-stdin": "^6.0.0", "global-dirs": "^0.1.1", + "http-proxy-middleware": "^3.0.0", "import-lazy": "^3.1.0", "inquirer": "^8.2.0", "is-installed-globally": "^0.4.0", From a79d7978882d31b83a1be28d606af6ac29dd1563 Mon Sep 17 00:00:00 2001 From: Denys Bohdan Date: Tue, 18 Jun 2024 15:02:14 +0200 Subject: [PATCH 2/2] STCLI-246 Update some token and proxy-related defaults --- doc/commands.md | 2 +- lib/commands/common-options.js | 1 + lib/platform/tenant-config.js | 2 +- 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/doc/commands.md b/doc/commands.md index 3a4a1706..94f1e6e7 100644 --- a/doc/commands.md +++ b/doc/commands.md @@ -1148,7 +1148,7 @@ Serve a build previously created with "stripes build": ``` $ stripes serve --existing-build output ``` -Serve a platform with a local proxy server that points to remove okapi server: +Serve a platform with a local proxy server that points to a remote okapi server: ``` $ stripes serve --startProxy --proxyPort 3010 --okapi http://some-okapi-server.folio.org ``` diff --git a/lib/commands/common-options.js b/lib/commands/common-options.js index f3504609..00b8bbb6 100644 --- a/lib/commands/common-options.js +++ b/lib/commands/common-options.js @@ -8,6 +8,7 @@ module.exports.serverOptions = { startProxy: { type: 'boolean', describe: 'Start a proxy server', + default: false, group: 'Server Options:', }, proxyPort: { diff --git a/lib/platform/tenant-config.js b/lib/platform/tenant-config.js index 5b0e0864..ee6f4f65 100644 --- a/lib/platform/tenant-config.js +++ b/lib/platform/tenant-config.js @@ -11,7 +11,7 @@ const defaultConfig = { showPerms: false, hasAllPerms: false, languages: ['en'], - useSecureTokens: false, + useSecureTokens: true, }, modules: { },