Skip to content
Open
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
8 changes: 7 additions & 1 deletion CONFIGURATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -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` |
Expand All @@ -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 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` |

Expand All @@ -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
```
4 changes: 2 additions & 2 deletions apps/standalone/webpack.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const config: Configuration & {
devtool: 'eval-source-map',
devServer: {
host: 'localhost',
port: 9000,
port: Number(process.env.UI_PORT) || 9000,
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
historyApiFallback: true,
open: true,
static: {
Expand Down Expand Up @@ -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'),
}),
);
}
Expand Down
2 changes: 1 addition & 1 deletion libs/cypress/cypress.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`,
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
experimentalRunAllSpecs: true,
experimentalMemoryManagement: true,
numTestsKeptInMemory: 5,
Expand Down
2 changes: 1 addition & 1 deletion libs/cypress/scripts/serve-dist.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down
2 changes: 1 addition & 1 deletion proxy/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.UiOrigin()}),
gorillaHandlers.AllowedMethods([]string{"GET", "POST", "PUT", "DELETE", "PATCH"}),
gorillaHandlers.AllowedHeaders([]string{"Content-Type", "Authorization", "X-FlightCtl-Organization-ID", "Flightctl-API-Version"}),
gorillaHandlers.AllowCredentials(),
Expand Down
38 changes: 37 additions & 1 deletion proxy/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"log"
"net"
"net/http"
"net/url"
"os"
"strings"
)
Expand All @@ -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:9000")
BaseUiUrl = getEnvUrlVar("BASE_UI_URL", "http://localhost:"+envOr("UI_PORT", "9000"))
AuthInsecure = getEnvVar("AUTH_INSECURE_SKIP_VERIFY", "")
OcpPlugin = getEnvVar("IS_OCP_PLUGIN", "false")
)
Expand Down Expand Up @@ -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 {
Expand Down
19 changes: 19 additions & 0 deletions proxy/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
Loading