From 8610dd7f2c7438c6b91459b2603f9c3dc902c7e5 Mon Sep 17 00:00:00 2001 From: Celia Amador Date: Thu, 19 Nov 2026 10:35:11 +0100 Subject: [PATCH 1/2] Make UI ports configurable Assisted-by: OpenAI Codex --- CONFIGURATION.md | 8 +++++++- apps/standalone/webpack.config.ts | 4 ++-- libs/cypress/cypress.config.ts | 2 +- proxy/app.go | 2 +- proxy/config/config.go | 2 +- 5 files changed, 12 insertions(+), 6 deletions(-) diff --git a/CONFIGURATION.md b/CONFIGURATION.md index 9e4511d448..c7990a48b0 100644 --- a/CONFIGURATION.md +++ b/CONFIGURATION.md @@ -13,7 +13,7 @@ This document describes all environment variables and configuration options avai | Variable | Description | Default | Values | | --------------------------------------- | --------------------------------------------------------------------------------------------------- | ------------------------ | -------------------------------------------- | -| `BASE_UI_URL` | Base URL for UI application | `http://localhost:9000` | `https://ui.flightctl.example.com` | +| `BASE_UI_URL` | Base URL for UI application (defaults to `http://localhost:$UI_PORT`) | `http://localhost:9000` | `https://ui.flightctl.example.com` | | `FLIGHTCTL_SERVER` | Flight Control API server URL | `https://localhost:3443` | `https://api.flightctl.example.com` | | `FLIGHTCTL_REMOTE_ACCESS_SERVER` | Remote access service URL for application console WebSocket bridging (serial console is VM-only today) | `https://localhost:3444` | `https://flightctl-remote-access:3444` | | `FLIGHTCTL_SERVER_INSECURE_SKIP_VERIFY` | Skip backend server TLS verification | `false` | `true`, `false` | @@ -26,6 +26,7 @@ This document describes all environment variables and configuration options avai | `TLS_CERT` | Path to TLS certificate | _(empty)_ | `/path/to/server.crt` | | `TLS_KEY` | Path to TLS private key | _(empty)_ | `/path/to/server.key` | | `API_PORT` | UI proxy server port | `3001` | `8080`, `3000`, etc. | +| `UI_PORT` | UI dev server port (standalone dev mode) | `9000` | `8000`, `8080`, etc. | | `IS_OCP_PLUGIN` | Run as OpenShift Console plugin | `false` | `true`, `false` | | `IS_RHEM` | Red Hat Enterprise Mode | _(empty)_ | `true`, `false` | @@ -48,3 +49,8 @@ FLIGHTCTL_REMOTE_ACCESS_SERVER=https://remote-access.flightctl.prod.example.com ENABLE_CLI_ARTIFACTS=false \ npm run dev ``` + +```shell +# Run the dev servers on custom ports +UI_PORT=8001 API_PORT=4000 npm run dev +``` diff --git a/apps/standalone/webpack.config.ts b/apps/standalone/webpack.config.ts index d13a06a1e6..43579c3e74 100644 --- a/apps/standalone/webpack.config.ts +++ b/apps/standalone/webpack.config.ts @@ -23,7 +23,7 @@ const config: Configuration & { devtool: 'eval-source-map', devServer: { host: 'localhost', - port: 9000, + port: Number(process.env.UI_PORT) || 9000, historyApiFallback: true, open: true, static: { @@ -196,7 +196,7 @@ if (NODE_ENV === 'production') { } else { config.plugins?.push( new DefinePlugin({ - 'window.API_PORT': JSON.stringify(process.env.API_PORT) || '3001', + 'window.API_PORT': JSON.stringify(process.env.API_PORT || '3001'), }), ); } diff --git a/libs/cypress/cypress.config.ts b/libs/cypress/cypress.config.ts index 12e0505c0b..afcc69e155 100644 --- a/libs/cypress/cypress.config.ts +++ b/libs/cypress/cypress.config.ts @@ -6,7 +6,7 @@ export default defineConfig({ setupNodeEvents() {}, specPattern: './**/*.cy.ts', supportFile: 'libs/cypress/support/index.ts', - baseUrl: 'http://localhost:9000', + baseUrl: `http://localhost:${process.env.UI_PORT || 9000}`, experimentalRunAllSpecs: true, experimentalMemoryManagement: true, numTestsKeptInMemory: 5, diff --git a/proxy/app.go b/proxy/app.go index be624a1f65..02d9961096 100644 --- a/proxy/app.go +++ b/proxy/app.go @@ -19,7 +19,7 @@ import ( func corsHandler(router *mux.Router) http.Handler { return gorillaHandlers.CORS( - gorillaHandlers.AllowedOrigins([]string{"http://localhost:9000"}), + gorillaHandlers.AllowedOrigins([]string{config.BaseUiUrl}), gorillaHandlers.AllowedMethods([]string{"GET", "POST", "PUT", "DELETE", "PATCH"}), gorillaHandlers.AllowedHeaders([]string{"Content-Type", "Authorization", "X-FlightCtl-Organization-ID", "Flightctl-API-Version"}), gorillaHandlers.AllowCredentials(), diff --git a/proxy/config/config.go b/proxy/config/config.go index e26a489840..a116503acd 100644 --- a/proxy/config/config.go +++ b/proxy/config/config.go @@ -19,7 +19,7 @@ var ( AlertManagerApiUrl = getEnvUrlVar("FLIGHTCTL_ALERTMANAGER_PROXY", "https://localhost:8443") TlsKeyPath = getEnvVar("TLS_KEY", "") TlsCertPath = getEnvVar("TLS_CERT", "") - BaseUiUrl = getEnvUrlVar("BASE_UI_URL", "http://localhost:9000") + BaseUiUrl = getEnvUrlVar("BASE_UI_URL", "http://localhost:"+getEnvVar("UI_PORT", "9000")) AuthInsecure = getEnvVar("AUTH_INSECURE_SKIP_VERIFY", "") OcpPlugin = getEnvVar("IS_OCP_PLUGIN", "false") ) From a3b6a513ba4f2d7a7d957126ccd3064f8b166b13 Mon Sep 17 00:00:00 2001 From: Celia Amador Date: Thu, 19 Nov 2026 12:02:53 +0100 Subject: [PATCH 2/2] Address code review issues Assisted-by: OpenAI Codex --- CONFIGURATION.md | 2 +- libs/cypress/scripts/serve-dist.js | 2 +- proxy/app.go | 2 +- proxy/config/config.go | 38 +++++++++++++++++++++++++++++- proxy/config/config_test.go | 19 +++++++++++++++ 5 files changed, 59 insertions(+), 4 deletions(-) diff --git a/CONFIGURATION.md b/CONFIGURATION.md index c7990a48b0..c153d726f7 100644 --- a/CONFIGURATION.md +++ b/CONFIGURATION.md @@ -26,7 +26,7 @@ This document describes all environment variables and configuration options avai | `TLS_CERT` | Path to TLS certificate | _(empty)_ | `/path/to/server.crt` | | `TLS_KEY` | Path to TLS private key | _(empty)_ | `/path/to/server.key` | | `API_PORT` | UI proxy server port | `3001` | `8080`, `3000`, etc. | -| `UI_PORT` | UI dev server port (standalone dev mode) | `9000` | `8000`, `8080`, etc. | +| `UI_PORT` | UI web server port (dev server and static server used by integration tests) | `9000` | `8000`, `8080`, etc. | | `IS_OCP_PLUGIN` | Run as OpenShift Console plugin | `false` | `true`, `false` | | `IS_RHEM` | Red Hat Enterprise Mode | _(empty)_ | `true`, `false` | diff --git a/libs/cypress/scripts/serve-dist.js b/libs/cypress/scripts/serve-dist.js index 780613c439..698028866f 100644 --- a/libs/cypress/scripts/serve-dist.js +++ b/libs/cypress/scripts/serve-dist.js @@ -13,7 +13,7 @@ const http = require('http'); const fs = require('fs'); const path = require('path'); -const PORT = Number(process.env.PORT) || 9000; +const PORT = Number(process.env.UI_PORT) || 9000; const DIST_DIR = path.resolve(__dirname, '../../../apps/standalone/dist'); const INDEX_FILE = path.join(DIST_DIR, 'index.html'); diff --git a/proxy/app.go b/proxy/app.go index 02d9961096..88f7424b22 100644 --- a/proxy/app.go +++ b/proxy/app.go @@ -19,7 +19,7 @@ import ( func corsHandler(router *mux.Router) http.Handler { return gorillaHandlers.CORS( - gorillaHandlers.AllowedOrigins([]string{config.BaseUiUrl}), + gorillaHandlers.AllowedOrigins([]string{config.UiOrigin()}), gorillaHandlers.AllowedMethods([]string{"GET", "POST", "PUT", "DELETE", "PATCH"}), gorillaHandlers.AllowedHeaders([]string{"Content-Type", "Authorization", "X-FlightCtl-Organization-ID", "Flightctl-API-Version"}), gorillaHandlers.AllowCredentials(), diff --git a/proxy/config/config.go b/proxy/config/config.go index a116503acd..2e06eb3c78 100644 --- a/proxy/config/config.go +++ b/proxy/config/config.go @@ -4,6 +4,7 @@ import ( "log" "net" "net/http" + "net/url" "os" "strings" ) @@ -19,7 +20,7 @@ var ( AlertManagerApiUrl = getEnvUrlVar("FLIGHTCTL_ALERTMANAGER_PROXY", "https://localhost:8443") TlsKeyPath = getEnvVar("TLS_KEY", "") TlsCertPath = getEnvVar("TLS_CERT", "") - BaseUiUrl = getEnvUrlVar("BASE_UI_URL", "http://localhost:"+getEnvVar("UI_PORT", "9000")) + BaseUiUrl = getEnvUrlVar("BASE_UI_URL", "http://localhost:"+envOr("UI_PORT", "9000")) AuthInsecure = getEnvVar("AUTH_INSECURE_SKIP_VERIFY", "") OcpPlugin = getEnvVar("IS_OCP_PLUGIN", "false") ) @@ -142,6 +143,41 @@ func getEnvUrlVar(key string, defaultValue string) string { return strings.TrimSuffix(urlValue, "/") } +func envOr(key string, defaultValue string) string { + if val := getEnvVar(key, ""); val != "" { + return val + } + return defaultValue +} + +// UiOrigin returns the CORS origin for the configured UI URL, normalized to the +// browser's representation. +func UiOrigin() string { + return originOf(BaseUiUrl) +} + +// originOf strips path/query so the value matches the browser's Origin header. +// An unparsable URL is returned unchanged so it can never match a real origin. +func originOf(rawUrl string) string { + u, err := url.Parse(rawUrl) + if err != nil || u.Scheme == "" || u.Hostname() == "" { + return rawUrl + } + scheme := strings.ToLower(u.Scheme) + port := u.Port() + if (scheme == "http" && port == "80") || (scheme == "https" && port == "443") { + port = "" + } + host := strings.ToLower(u.Hostname()) + switch { + case port != "": + host = net.JoinHostPort(host, port) + case strings.Contains(host, ":"): + host = "[" + host + "]" // IPv6 literal + } + return scheme + "://" + host +} + func getEnvVar(key string, defaultValue string) string { val, ok := os.LookupEnv(key) if !ok { diff --git a/proxy/config/config_test.go b/proxy/config/config_test.go index f247979e32..2bc31251ff 100644 --- a/proxy/config/config_test.go +++ b/proxy/config/config_test.go @@ -62,3 +62,22 @@ func TestShouldTrustForwardedHeaders(t *testing.T) { //nolint:paralleltest // mu t.Fatal("expected false when RemoteAddr is outside CIDR") } } + +func TestOriginOf(t *testing.T) { + t.Parallel() + cases := map[string]string{ + "https://ui.example.com/ui": "https://ui.example.com", + "http://localhost:9000/": "http://localhost:9000", + "http://LOCALHOST:9000": "http://localhost:9000", + "HTTPS://HOST.Example.com:443": "https://host.example.com", + "http://host:80": "http://host", + "http://host:8080": "http://host:8080", + "not a url": "not a url", + "": "", + } + for raw, want := range cases { + if got := originOf(raw); got != want { + t.Fatalf("originOf(%q) = %q, want %q", raw, got, want) + } + } +}