From 8aa40795c9789257109b99eeaa936548ab47676b Mon Sep 17 00:00:00 2001 From: Kris McGinnes Date: Fri, 13 Nov 2026 09:30:28 -0600 Subject: [PATCH] Read IAM as true or false in any letter case for the Default Connection The Default Connection is now built as an object and sent as JSON, so its values are JSON-escaped. Layer: 6-iam-setting --- .../docs/guides/deploy-with-docker.mdx | 1 + .../content/docs/references/configuration.mdx | 2 + .../src/app.test.ts | 20 +-- .../graph-explorer-proxy-server/src/app.ts | 12 +- .../src/config-pipeline.test.ts | 7 +- .../src/default-connection.test.ts | 145 +++++++++++------- .../src/default-connection.ts | 60 +++++--- .../src/run-server.ts | 2 +- .../src/startup-config.test.ts | 35 +++-- .../src/startup-config.ts | 14 +- .../src/testing/deployment-scenarios.ts | 56 ++++--- 11 files changed, 218 insertions(+), 136 deletions(-) diff --git a/packages/docs/src/content/docs/guides/deploy-with-docker.mdx b/packages/docs/src/content/docs/guides/deploy-with-docker.mdx index e57946910..f60d7308e 100644 --- a/packages/docs/src/content/docs/guides/deploy-with-docker.mdx +++ b/packages/docs/src/content/docs/guides/deploy-with-docker.mdx @@ -94,3 +94,4 @@ The server now reads its startup configuration itself, where earlier versions ra - With `HOST` set, the container reuses the certificate files in `cert-info` when all of them exist, `server.crt` names `HOST`, and it hasn't expired, and generates a certificate otherwise. A restart, or a new container that mounts the same `cert-info` volume, keeps its certificate, so you no longer have to drop `HOST` after the first run. A new container without the volume generates its own, as [HTTPS Connections](../../references/security/#https-connections) describes. Earlier versions generated a certificate for every new container when the configuration folder held no lines from an earlier start. - A run from source (`pnpm dev`, `pnpm start`) now reads a `config.json` at the repository root, the same way the image does, so it applies the Default Connection settings and the Neptune Notebook preset from that file, and it ignores the lines earlier versions appended to `.env`. When that `config.json` sets `PROXY_SERVER_HTTPS_CONNECTION` to `true`, the server stops with `PROXY_SERVER_HTTPS_CONNECTION is true but certificate files are missing`, because only the image generates certificates. Set it to `false`, or remove it, for runs from source. - A key set more than once in `config.json`, `.env`, or `.env.local` now stops startup with an error that names the file, the key, and its lines, so keep one line for each key and remove the others. In `.env` and `.env.local`, earlier versions used the last line, so keep that one to start as before. The lines earlier versions appended to `.env` don't count. +- An `IAM` value other than `true` or `false` in any letter case now uses `false` with a warning, and `True` or `TRUE` now reads as `true`. Earlier versions wrote the value as it was, which could make a default connection the browser couldn't read. diff --git a/packages/docs/src/content/docs/references/configuration.mdx b/packages/docs/src/content/docs/references/configuration.mdx index 05d69d513..6b16f22fa 100644 --- a/packages/docs/src/content/docs/references/configuration.mdx +++ b/packages/docs/src/content/docs/references/configuration.mdx @@ -208,6 +208,8 @@ These are the valid environment variables used for the default connection, their - `AWS_REGION` - `None` - `SERVICE_TYPE` - `neptune-db`, Set this as `neptune-db` for Neptune database or `neptune-graph` for Neptune Analytics. +`IAM` is `true` or `false` in any letter case. For any other value, the server logs a warning and the default connection uses `false`. + ### JSON Configuration Approach First, create a `config.json` file containing values for the connection attributes: diff --git a/packages/graph-explorer-proxy-server/src/app.test.ts b/packages/graph-explorer-proxy-server/src/app.test.ts index 900c6b4cb..9b31f01e9 100644 --- a/packages/graph-explorer-proxy-server/src/app.test.ts +++ b/packages/graph-explorer-proxy-server/src/app.test.ts @@ -32,7 +32,7 @@ function createTestApp( ) { const app = createApp({ configPath, - defaultConnectionJson: undefined, + defaultConnection: undefined, staticFilesVirtualPath: "/explorer", staticFilesPath: ".", version: testVersion, @@ -273,13 +273,15 @@ describe("createApp", () => { }); describe("GET /defaultConnection", () => { - const defaultConnectionJson = - '\n{\n"GRAPH_EXP_CONNECTION_URL":"https://db:8182",\n"GRAPH_EXP_USING_PROXY_SERVER":false\n}\n'; + const defaultConnection = { + GRAPH_EXP_CONNECTION_URL: "https://db:8182", + GRAPH_EXP_USING_PROXY_SERVER: true, + }; it("serves the in-memory Default Connection as JSON", async () => { const app = createApp({ configPath: ".", - defaultConnectionJson, + defaultConnection, staticFilesVirtualPath: "/explorer", staticFilesPath: ".", }); @@ -289,7 +291,7 @@ describe("createApp", () => { expect(response.status).toBe(200); expect(response.type).toBe("application/json"); - expect(response.text).toBe(defaultConnectionJson); + expect(response.body).toStrictEqual(defaultConnection); }); it("prefers the in-memory Default Connection over defaultConnection.json", async () => { @@ -301,7 +303,7 @@ describe("createApp", () => { ); const app = createApp({ configPath: tmpDir, - defaultConnectionJson, + defaultConnection, staticFilesVirtualPath: "/explorer", staticFilesPath: ".", }); @@ -309,7 +311,7 @@ describe("createApp", () => { const response = await request(app).get("/defaultConnection"); - expect(response.text).toBe(defaultConnectionJson); + expect(response.body).toStrictEqual(defaultConnection); } finally { fs.rmSync(tmpDir, { recursive: true, force: true }); } @@ -371,7 +373,7 @@ describe("createApp", () => { function createStaticTestApp(staticFilesVirtualPath = "/explorer") { const app = createApp({ configPath: ".", - defaultConnectionJson: undefined, + defaultConnection: undefined, staticFilesVirtualPath, staticFilesPath: tmpDir, version: testVersion, @@ -1497,7 +1499,7 @@ describe("createApp", () => { const app = createApp({ configPath: ".", - defaultConnectionJson: undefined, + defaultConnection: undefined, staticFilesVirtualPath: "/explorer", staticFilesPath: ".", }); diff --git a/packages/graph-explorer-proxy-server/src/app.ts b/packages/graph-explorer-proxy-server/src/app.ts index 2c70c247e..9b8683f74 100644 --- a/packages/graph-explorer-proxy-server/src/app.ts +++ b/packages/graph-explorer-proxy-server/src/app.ts @@ -14,6 +14,8 @@ import path from "path"; import { pipeline } from "stream"; import { z } from "zod"; +import type { DefaultConnection } from "./default-connection.ts"; + import { blockedResolutionError, dbAgentFor, @@ -165,8 +167,8 @@ function onClientDisconnect(res: Response, onDisconnect: () => void) { interface CreateAppOptions { configPath: string; - /** Served at `/defaultConnection`. Without it, the configuration folder's `defaultConnection.json` is. */ - defaultConnectionJson: string | undefined; + /** Served at `/defaultConnection`. Without one, the configuration folder's `defaultConnection.json` is. */ + defaultConnection: DefaultConnection | undefined; staticFilesVirtualPath: string; staticFilesPath: string; version?: string; @@ -176,7 +178,7 @@ interface CreateAppOptions { export function createApp({ configPath, - defaultConnectionJson, + defaultConnection, staticFilesVirtualPath, staticFilesPath, version, @@ -203,11 +205,11 @@ export function createApp({ ), ); app.get("/defaultConnection", (_req, res, next) => { - if (defaultConnectionJson === undefined) { + if (defaultConnection === undefined) { next(); return; } - res.type("json").send(defaultConnectionJson); + res.json(defaultConnection); }); app.use( "/defaultConnection", diff --git a/packages/graph-explorer-proxy-server/src/config-pipeline.test.ts b/packages/graph-explorer-proxy-server/src/config-pipeline.test.ts index 5e80d9cec..e47e7e24a 100644 --- a/packages/graph-explorer-proxy-server/src/config-pipeline.test.ts +++ b/packages/graph-explorer-proxy-server/src/config-pipeline.test.ts @@ -4,6 +4,7 @@ import path from "path"; import request from "supertest"; import { afterEach, beforeEach, describe, it, vi } from "vite-plus/test"; +import type { DefaultConnection } from "./default-connection.ts"; import type { EnvironmentValues } from "./env.ts"; import { createApp } from "./app.ts"; @@ -56,11 +57,11 @@ function readFolder(folder: string) { async function requestDefaultConnection( configDir: string, - defaultConnectionJson: string | undefined, + defaultConnection: DefaultConnection | undefined, ): Promise { const app = createApp({ configPath: configDir, - defaultConnectionJson, + defaultConnection, staticFilesVirtualPath: "/explorer", staticFilesPath: ".", version: "test", @@ -278,7 +279,7 @@ describe("deployment scenarios: TypeScript startup", () => { certificatesGenerated: certificates === "generated", defaultConnection: await requestDefaultConnection( configDir, - startup.defaultConnectionJson, + startup.defaultConnection, ), }, }; diff --git a/packages/graph-explorer-proxy-server/src/default-connection.test.ts b/packages/graph-explorer-proxy-server/src/default-connection.test.ts index 4e4c118e1..3faae53ea 100644 --- a/packages/graph-explorer-proxy-server/src/default-connection.test.ts +++ b/packages/graph-explorer-proxy-server/src/default-connection.test.ts @@ -2,13 +2,18 @@ import { describe, expect, it } from "vite-plus/test"; import type { ConfigJson } from "./config-json.ts"; -import { renderDefaultConnection } from "./default-connection.ts"; +import { + type DefaultConnection, + resolveDefaultConnection, +} from "./default-connection.ts"; type Row = { name: string; settings: ConfigJson["values"]; - /** The Default Connection the text parses to, or `undefined` when no connection is configured. */ - expected: Record | undefined; + /** The Default Connection, or `undefined` when no connection is configured. */ + expected: DefaultConnection | undefined; + /** The warnings the resolver gives. */ + warnings?: string[]; }; const db = "https://db:8182"; @@ -318,82 +323,108 @@ const rowGroups: Record = { ], // #2291: the shell never wrote these two settings. + // Earlier versions wrote the IAM value as it was given, so anything but a + // lowercase true or false made the file invalid JSON. + "IAM values": [ + { + name: "reads IAM=True as true", + settings: { GRAPH_CONNECTION_URL: db, IAM: "True" }, + expected: { + GRAPH_EXP_CONNECTION_URL: db, + GRAPH_EXP_SERVICE_TYPE: "neptune-db", + GRAPH_EXP_IAM: true, + GRAPH_EXP_AWS_REGION: "", + GRAPH_EXP_USING_PROXY_SERVER: true, + }, + }, + { + name: "uses false for IAM=yes with a warning", + settings: { GRAPH_CONNECTION_URL: db, IAM: "yes" }, + expected: { + GRAPH_EXP_CONNECTION_URL: db, + GRAPH_EXP_SERVICE_TYPE: "neptune-db", + GRAPH_EXP_IAM: false, + GRAPH_EXP_AWS_REGION: "", + GRAPH_EXP_USING_PROXY_SERVER: true, + }, + warnings: [ + `IAM is "yes", which isn't true or false, so the Default Connection uses false.`, + ], + }, + { + // A config.json null reaches here as the text "null". + name: "uses false for IAM=null with a warning", + settings: { GRAPH_CONNECTION_URL: db, IAM: "null" }, + expected: { + GRAPH_EXP_CONNECTION_URL: db, + GRAPH_EXP_SERVICE_TYPE: "neptune-db", + GRAPH_EXP_IAM: false, + GRAPH_EXP_AWS_REGION: "", + GRAPH_EXP_USING_PROXY_SERVER: true, + }, + warnings: [ + `IAM is "null", which isn't true or false, so the Default Connection uses false.`, + ], + }, + { + name: "ignores IAM on a direct connection without a warning", + settings: { + USING_PROXY_SERVER: "false", + PUBLIC_OR_PROXY_ENDPOINT: "https://public:9250", + IAM: "yes", + }, + expected: { + GRAPH_EXP_CONNECTION_URL: "https://public:9250", + GRAPH_EXP_USING_PROXY_SERVER: false, + }, + }, + ], }; -/** The rendered Default Connection, parsed. */ -function renderParsed(settings: ConfigJson["values"]): unknown { - const text = renderDefaultConnection(settings); - return text === undefined ? undefined : JSON.parse(text); -} - -describe("renderDefaultConnection", () => { +describe("resolveDefaultConnection", () => { describe.for(Object.entries(rowGroups))("%s", ([_group, groupRows]) => { it.for(groupRows.map(row => [row.name, row] as const))( "%s", - ([_name, { settings, expected }]) => { - expect(renderParsed(settings)).toStrictEqual(expected); + ([_name, { settings, expected, warnings = [] }]) => { + expect(resolveDefaultConnection(settings)).toStrictEqual({ + defaultConnection: expected, + warnings, + }); }, ); }); - it("renders the text process-environment.sh wrote, line by line", () => { - expect( - renderDefaultConnection({ - GRAPH_CONNECTION_URL: db, - GRAPH_TYPE: "gremlin", - IAM: "true", - AWS_REGION: "us-east-1", - }), - ).toBe( - [ - "", - "{", - '"GRAPH_EXP_CONNECTION_URL":"https://db:8182",', - '"GRAPH_EXP_GRAPH_TYPE":"gremlin",', - '"GRAPH_EXP_SERVICE_TYPE":"neptune-db",', - '"GRAPH_EXP_IAM":true,', - '"GRAPH_EXP_AWS_REGION":"us-east-1",', - '"GRAPH_EXP_USING_PROXY_SERVER":true', - "}", - "", - ].join("\n"), - ); - }); - - // Earlier versions wrote the IAM value as it was given, so a value that - // isn't a JSON literal made a file the browser couldn't read. - it("writes an IAM of True as it is", () => { - expect( - renderDefaultConnection({ GRAPH_CONNECTION_URL: db, IAM: "True" }), - ).toContain('"GRAPH_EXP_IAM":True,\n'); - }); - // Earlier versions wrote the legacy names to defaultConnection.json for no // reason. The browser reads only the GRAPH_EXP_ names. it("never includes the legacy variable names", () => { - expect( - renderParsed({ - USING_PROXY_SERVER: "true", - GRAPH_CONNECTION_URL: "https://proxied:8182", - PUBLIC_OR_PROXY_ENDPOINT: "https://public:9250", - }), - ).not.toHaveProperty("GRAPH_EXP_PUBLIC_OR_PROXY_ENDPOINT"); + const { defaultConnection } = resolveDefaultConnection({ + USING_PROXY_SERVER: "true", + GRAPH_CONNECTION_URL: "https://proxied:8182", + PUBLIC_OR_PROXY_ENDPOINT: "https://public:9250", + }); + + expect(defaultConnection).not.toHaveProperty( + "GRAPH_EXP_PUBLIC_OR_PROXY_ENDPOINT", + ); }); it("treats an empty setting as unset", () => { expect( - renderParsed({ + resolveDefaultConnection({ GRAPH_CONNECTION_URL: db, GRAPH_TYPE: "", SERVICE_TYPE: "", IAM: "", }), ).toStrictEqual({ - GRAPH_EXP_CONNECTION_URL: db, - GRAPH_EXP_SERVICE_TYPE: "neptune-db", - GRAPH_EXP_IAM: false, - GRAPH_EXP_AWS_REGION: "", - GRAPH_EXP_USING_PROXY_SERVER: true, + defaultConnection: { + GRAPH_EXP_CONNECTION_URL: db, + GRAPH_EXP_SERVICE_TYPE: "neptune-db", + GRAPH_EXP_IAM: false, + GRAPH_EXP_AWS_REGION: "", + GRAPH_EXP_USING_PROXY_SERVER: true, + }, + warnings: [], }); }); }); diff --git a/packages/graph-explorer-proxy-server/src/default-connection.ts b/packages/graph-explorer-proxy-server/src/default-connection.ts index 7d9a6cd0b..096de9310 100644 --- a/packages/graph-explorer-proxy-server/src/default-connection.ts +++ b/packages/graph-explorer-proxy-server/src/default-connection.ts @@ -1,11 +1,18 @@ import type { ConfigJson } from "./config-json.ts"; +/** The connection the proxy server serves at `/defaultConnection`. */ +export type DefaultConnection = { + GRAPH_EXP_CONNECTION_URL: string; + GRAPH_EXP_GRAPH_TYPE?: string; + GRAPH_EXP_SERVICE_TYPE?: string; + GRAPH_EXP_IAM?: boolean; + GRAPH_EXP_AWS_REGION?: string; + GRAPH_EXP_USING_PROXY_SERVER: boolean; +}; + /** - * Renders the Default Connection from the connection settings, as the text - * process-environment.sh wrote to `defaultConnection.json`, or returns - * `undefined` when they name no URL. Each value is written as it is, so an - * IAM value that isn't a JSON literal, such as `True`, makes text that isn't - * valid JSON, as before. + * Resolves the Default Connection from the connection settings, or + * `undefined` when they name no URL. * * A connection is proxied when USING_PROXY_SERVER is true, or when it is unset * and only GRAPH_CONNECTION_URL is set. Earlier versions treated an unset @@ -14,9 +21,11 @@ import type { ConfigJson } from "./config-json.ts"; * connection drops IAM, region, and service type because the browser sends its * requests and nothing would sign them. */ -export function renderDefaultConnection( - settings: ConfigJson["values"], -): string | undefined { +export function resolveDefaultConnection(settings: ConfigJson["values"]): { + defaultConnection: DefaultConnection | undefined; + warnings: string[]; +} { + const warnings: string[] = []; const endpoint = settings.PUBLIC_OR_PROXY_ENDPOINT ?? ""; const connectionUrl = settings.GRAPH_CONNECTION_URL ?? ""; const usingProxyServer = settings.USING_PROXY_SERVER ?? ""; @@ -26,24 +35,35 @@ export function renderDefaultConnection( (usingProxyServer === "" && connectionUrl !== "" && endpoint === ""); const url = isProxied ? connectionUrl : endpoint || connectionUrl; if (url === "") { - return undefined; + return { defaultConnection: undefined, warnings }; } const graphType = settings.GRAPH_TYPE || (settings.SERVICE_TYPE === "neptune-graph" ? "openCypher" : undefined); - const lines = ["", "{", `"GRAPH_EXP_CONNECTION_URL":"${url}",`]; - if (graphType) { - lines.push(`"GRAPH_EXP_GRAPH_TYPE":"${graphType}",`); + const defaultConnection: DefaultConnection = { + GRAPH_EXP_CONNECTION_URL: url, + ...(graphType && { GRAPH_EXP_GRAPH_TYPE: graphType }), + ...(isProxied && { + GRAPH_EXP_SERVICE_TYPE: settings.SERVICE_TYPE || "neptune-db", + GRAPH_EXP_IAM: readIam(settings.IAM, warnings), + GRAPH_EXP_AWS_REGION: settings.AWS_REGION ?? "", + }), + GRAPH_EXP_USING_PROXY_SERVER: isProxied, + }; + return { defaultConnection, warnings }; +} + +function readIam(value: string | undefined, warnings: string[]) { + if (!value || /^false$/i.test(value)) { + return false; } - if (isProxied) { - lines.push( - `"GRAPH_EXP_SERVICE_TYPE":"${settings.SERVICE_TYPE || "neptune-db"}",`, - `"GRAPH_EXP_IAM":${settings.IAM || "false"},`, - `"GRAPH_EXP_AWS_REGION":"${settings.AWS_REGION ?? ""}",`, - ); + if (/^true$/i.test(value)) { + return true; } - lines.push(`"GRAPH_EXP_USING_PROXY_SERVER":${isProxied}`, "}"); - return lines.map(line => `${line}\n`).join(""); + warnings.push( + `IAM is "${value}", which isn't true or false, so the Default Connection uses false.`, + ); + return false; } diff --git a/packages/graph-explorer-proxy-server/src/run-server.ts b/packages/graph-explorer-proxy-server/src/run-server.ts index ab9bc0ad9..8c0ecdaf7 100644 --- a/packages/graph-explorer-proxy-server/src/run-server.ts +++ b/packages/graph-explorer-proxy-server/src/run-server.ts @@ -114,7 +114,7 @@ export function runServer({ const app = createApp({ configPath, - defaultConnectionJson: startup.defaultConnectionJson, + defaultConnection: startup.defaultConnection, staticFilesVirtualPath, staticFilesPath, version: packageJson.version, diff --git a/packages/graph-explorer-proxy-server/src/startup-config.test.ts b/packages/graph-explorer-proxy-server/src/startup-config.test.ts index e12b81bb8..741f5d76f 100644 --- a/packages/graph-explorer-proxy-server/src/startup-config.test.ts +++ b/packages/graph-explorer-proxy-server/src/startup-config.test.ts @@ -280,12 +280,6 @@ describe("resolveStartupConfig", () => { }); describe("config.json after the environment", () => { - /** The Default Connection the startup serves, parsed. */ - function defaultConnectionOf(inputs: Inputs) { - const text = resolve(inputs).defaultConnectionJson; - return text === undefined ? undefined : JSON.parse(text); - } - it("reads the values from config.json", () => { const startup = resolve({ configJson: { @@ -298,7 +292,7 @@ describe("resolveStartupConfig", () => { }); expect(startup.serverEnv.PROXY_SERVER_HTTPS_CONNECTION).toBe("false"); - expect(JSON.parse(startup.defaultConnectionJson ?? "")).toMatchObject({ + expect(startup.defaultConnection).toMatchObject({ GRAPH_EXP_GRAPH_TYPE: "sparql", GRAPH_EXP_IAM: true, GRAPH_EXP_CONNECTION_URL: db, @@ -308,7 +302,7 @@ describe("resolveStartupConfig", () => { it("yields to the environment for a key both set", () => { expect( - defaultConnectionOf({ + resolve({ configJson: { GRAPH_TYPE: "sparql", GRAPH_CONNECTION_URL: "https://from-config:8182", @@ -319,7 +313,7 @@ describe("resolveStartupConfig", () => { GRAPH_CONNECTION_URL: "https://from-env:8182", AWS_REGION: "eu-west-1", }, - }), + }).defaultConnection, ).toMatchObject({ GRAPH_EXP_GRAPH_TYPE: "gremlin", GRAPH_EXP_CONNECTION_URL: "https://from-env:8182", @@ -329,10 +323,10 @@ describe("resolveStartupConfig", () => { it("fills the keys the environment leaves unset", () => { expect( - defaultConnectionOf({ + resolve({ configJson: { GRAPH_TYPE: "sparql", AWS_REGION: "us-west-2" }, processEnv: { GRAPH_CONNECTION_URL: db, AWS_REGION: "eu-west-1" }, - }), + }).defaultConnection, ).toMatchObject({ GRAPH_EXP_CONNECTION_URL: db, GRAPH_EXP_GRAPH_TYPE: "sparql", @@ -353,7 +347,7 @@ describe("resolveStartupConfig", () => { }); expect(startup.serverEnv.PROXY_SERVER_HTTPS_CONNECTION).toBe("false"); - expect(startup.defaultConnectionJson).toContain(db); + expect(startup.defaultConnection?.GRAPH_EXP_CONNECTION_URL).toBe(db); }); it("leaves an empty environment value of a key config.json can't hold for the server to reject", () => { @@ -410,8 +404,8 @@ describe("resolveStartupConfig", () => { it("come from the environment without a config.json", () => { expect( resolve({ processEnv: { GRAPH_CONNECTION_URL: db, IAM: "true" } }) - .defaultConnectionJson, - ).toContain('"GRAPH_EXP_IAM":true,'); + .defaultConnection, + ).toMatchObject({ GRAPH_EXP_CONNECTION_URL: db, GRAPH_EXP_IAM: true }); }); // The shipped packages/graph-explorer/.env sets SERVICE_TYPE, and the @@ -420,9 +414,18 @@ describe("resolveStartupConfig", () => { expect( resolve({ dotEnv: `GRAPH_CONNECTION_URL=${db}\nSERVICE_TYPE=neptune-graph\n`, - }).defaultConnectionJson, + }).defaultConnection, ).toBeUndefined(); }); + + it("collect the Default Connection's warnings", () => { + expect( + resolve({ processEnv: { GRAPH_CONNECTION_URL: db, IAM: "yes" } }) + .warnings, + ).toStrictEqual([ + `IAM is "yes", which isn't true or false, so the Default Connection uses false.`, + ]); + }); }); describe(".env files", () => { @@ -794,7 +797,7 @@ describe("loadStartup", () => { const { env, startup } = load(); expect(env.PROXY_SERVER_HTTPS_CONNECTION).toBe(false); - expect(startup.defaultConnectionJson).toContain(db); + expect(startup.defaultConnection?.GRAPH_EXP_CONNECTION_URL).toBe(db); }); it("reads config.json from source", () => { diff --git a/packages/graph-explorer-proxy-server/src/startup-config.ts b/packages/graph-explorer-proxy-server/src/startup-config.ts index 1c84ca018..38e6933af 100644 --- a/packages/graph-explorer-proxy-server/src/startup-config.ts +++ b/packages/graph-explorer-proxy-server/src/startup-config.ts @@ -7,7 +7,10 @@ import { configJsonKeys, readConfigJson, } from "./config-json.ts"; -import { renderDefaultConnection } from "./default-connection.ts"; +import { + type DefaultConnection, + resolveDefaultConnection, +} from "./default-connection.ts"; import { EnvFileError, parseEnvFile, @@ -38,8 +41,7 @@ export type StartupConfig = { serverEnv: Record; /** The settings `.env` files hold, for libraries that read `process.env`. */ envFileValues: Record; - /** The text served at `/defaultConnection`, when a connection is configured. */ - defaultConnectionJson: string | undefined; + defaultConnection: DefaultConnection | undefined; /** The host to generate a certificate for, when one is set. */ certificateHost: string | undefined; /** @@ -161,16 +163,18 @@ export function resolveStartupConfig({ // A .env line never reaches the server, as before. serverEnv.NEPTUNE_NOTEBOOK = settings.NEPTUNE_NOTEBOOK; + const connection = resolveDefaultConnection(settings); + return { serverEnv, envFileValues: { ...dotEnvFileValues, ...dotEnvLocalFileValues }, - defaultConnectionJson: renderDefaultConnection(settings), + defaultConnection: connection.defaultConnection, // The process environment only, so a HOST in .env never replaces // certificates kept in a volume. certificateHost: environment.HOST || undefined, httpsNotice: image ? httpsNoticeFor(notebook, https) : undefined, info, - warnings: configJson?.warnings ?? [], + warnings: [...(configJson?.warnings ?? []), ...connection.warnings], }; } diff --git a/packages/graph-explorer-proxy-server/src/testing/deployment-scenarios.ts b/packages/graph-explorer-proxy-server/src/testing/deployment-scenarios.ts index 90ed71382..47362d7ba 100644 --- a/packages/graph-explorer-proxy-server/src/testing/deployment-scenarios.ts +++ b/packages/graph-explorer-proxy-server/src/testing/deployment-scenarios.ts @@ -270,11 +270,9 @@ function servingConnection(body: unknown, base: ServerStart = https) { return served(base, { defaultConnection: { status: 200, body } }); } -/** A start that serves `text`, which isn't JSON, as the Default Connection. */ -function servingInvalidConnection(text: string, base: ServerStart = https) { - return served(base, { - defaultConnection: { status: 200, invalidJson: text }, - }); +/** The warning for an IAM value that isn't true or false. */ +function iamWarning(value: string) { + return `IAM is "${value}", which isn't true or false, so the Default Connection uses false.`; } const noCertificateFiles: StartupOutcome = { @@ -898,11 +896,9 @@ export const deployments: Deployment[] = [ { name: "-e IAM=True with GRAPH_CONNECTION_URL", dockerEnv: { GRAPH_CONNECTION_URL: clusterUrl, IAM: "True" }, - // Earlier versions wrote the IAM value as it was, so anything but a JSON - // boolean makes the file invalid. - expected: servingInvalidConnection( - expect.stringContaining('"GRAPH_EXP_IAM":True,'), - ), + // IAM is read without regard to case, so True is the boolean true. Before, + // the value was written as it was, which made the file invalid JSON. + expected: servingConnection(proxiedConnection({ GRAPH_EXP_IAM: true })), }, { name: "a mounted defaultConnection.json", @@ -978,8 +974,12 @@ export const deployments: Deployment[] = [ { name: "config.json IAM null with GRAPH_CONNECTION_URL", configJson: { GRAPH_CONNECTION_URL: clusterUrl, IAM: null }, - // Earlier versions wrote the IAM value as it was. - expected: servingConnection(proxiedConnection({ GRAPH_EXP_IAM: null })), + // An IAM that isn't true or false uses false with a warning. Before, the + // value was written as it was. + expected: servingConnection(proxiedConnection({ GRAPH_EXP_IAM: false }), { + ...https, + warnings: [iamWarning("null")], + }), }, { name: "config.json with PROXY_SERVER_HTTPS_CONNECTION false commented out", @@ -1358,20 +1358,36 @@ export const deployments: Deployment[] = [ }), ), }, - ...["TRUE", "FALSE", "yes"].map((value): Deployment => ({ + ...( + [ + // IAM is read without regard to case. Before, the value was written as + // it was, which made the file invalid JSON. + ["TRUE", servingConnection(proxiedConnection({ GRAPH_EXP_IAM: true }))], + ["FALSE", servingConnection(proxiedConnection({ GRAPH_EXP_IAM: false }))], + // An IAM that isn't true or false uses false with a warning. Before, the + // value was written as it was, which made the file invalid JSON. + [ + "yes", + servingConnection(proxiedConnection({ GRAPH_EXP_IAM: false }), { + ...https, + warnings: [iamWarning("yes")], + }), + ], + ] as const + ).map(([value, expected]): Deployment => ({ name: `-e IAM=${value} with GRAPH_CONNECTION_URL`, dockerEnv: { GRAPH_CONNECTION_URL: clusterUrl, IAM: value }, - // Earlier versions wrote the IAM value as it was, so anything but a JSON - // boolean makes the file invalid. - expected: servingInvalidConnection( - expect.stringContaining(`"GRAPH_EXP_IAM":${value},`), - ), + expected, })), { name: "config.json IAM 1 with GRAPH_CONNECTION_URL", configJson: { GRAPH_CONNECTION_URL: clusterUrl, IAM: 1 }, - // Earlier versions wrote the IAM value as it was. - expected: servingConnection(proxiedConnection({ GRAPH_EXP_IAM: 1 })), + // An IAM that isn't true or false uses false with a warning. Before, the + // value was written as it was. + expected: servingConnection(proxiedConnection({ GRAPH_EXP_IAM: false }), { + ...https, + warnings: [iamWarning("1")], + }), }, { name: 'config.json IAM "true" with GRAPH_CONNECTION_URL',