Connect Postgres, Teradata and S3
One surfaces.config.ts that reads a Postgres database, a Delta table in S3 and a Teradata system, all read-only, with every credential named by a secret and read from the server's environment. A source that cannot be reached does not stop the server; testSource says what is wrong.
The config names three sources. None of them holds a password, key or token: each secret is a name, and the dev server looks the values up in its own environment when it opens the source.
surfaces.config.ts:
import { defineConfig } from "@surfaces-ui/dev-server";
/**
* Three places data lives, as the dev server reads them. No credential is
* written here: each `secret` is a name, and the server reads the values
* from its own environment (`PG_PASSWORD`, `LAKE_KEY_ID`, `LAKE_SECRET`,
* `TD_PASSWORD`). Every source is read-only.
*/
export default defineConfig({
sources: [
{
// A Postgres database (also Supabase, Neon or RDS), attached read-only through DuckDB.
// Its tables are named shop.<schema>.<table>, e.g. shop.public.orders.
id: "shop",
displayName: "Shop database",
engine: "duckdb",
tables: [],
catalogs: [
{
name: "shop",
kind: "postgres",
// Where to connect and as whom. Never the password: that is PG_PASSWORD.
uri: "host=db.example.com port=5432 dbname=shop user=surfaces_reader sslmode=require",
secret: "PG",
},
],
},
{
// A Delta table in S3. Only paths under the connection's scope can be read.
id: "lake",
displayName: "Sales lake",
engine: "duckdb",
connections: [{ name: "sales", kind: "s3", scope: "s3://acme-lake/sales/", secret: "LAKE", region: "eu-west-1" }],
tables: [
{
name: "orders",
format: "delta",
path: "s3://acme-lake/sales/orders",
comment: "One row per order, written by the checkout service.",
// Parquet and Delta hold no keys; say what they are here.
model: { primaryKey: ["order_id"], columns: { amount: "Order total in euros, after discounts." } },
},
],
},
{
// Teradata through Teradata Query Service (its REST API).
id: "warehouse",
displayName: "Teradata warehouse",
engine: "teradata",
baseUrl: "https://vantage.example.com:1443",
system: "prod",
// A user that holds SELECT rights only: Teradata has no read-only session.
user: "surfaces_reader",
secret: "TD",
database: "sales",
},
],
});What each source needs:
- Postgres is attached to DuckDB read-only. Its tables are named
shop.<schema>.<table>. Theurisays where to connect and as whom, never the password; auriwithpassword=in it is refused. Give it a role that can only read. - S3 is a connection with a
scope. Only paths under the scope can be read, and the Delta table's path must sit inside it. For MinIO or R2, addendpoint. Keys that Delta files do not hold go inmodel. - Teradata goes through Teradata Query Service over HTTPS. Teradata has no read-only session, so the user must hold SELECT rights only. The session is set to UTC, and pages are cut with
TOPandQUALIFY ROW_NUMBER(), neverLIMITorOFFSET.
Set these on the machine that runs the server, never in the repo. Names only; this is what goes in .env.example:
# Postgres (secret "PG")
PG_PASSWORD=
# S3 (secret "LAKE")
LAKE_KEY_ID=
LAKE_SECRET=
# LAKE_SESSION_TOKEN= # only for temporary credentials
# Teradata (secret "TD")
TD_PASSWORD=
# TD_TOKEN= # a JWT instead of a passwordTo read secrets from a vault instead, open the sources yourself and pass a resolver: openSources(config, dir, (name) => vault.read(name)). On a cloud machine with a role, ambientCredentials: true on the S3 connection uses that role and needs no secret.
Then run pnpm serve (or npx surfaces-dev) next to the config. To see which sources answer, ask the server to test each one:
check-sources.ts:
import { type DataSource, queryErrorInfo } from "@surfaces-ui/data";
export interface SourceReport {
id: string;
ok: boolean;
/** One line per table, catalog or check, in words a person can act on. */
lines: string[];
}
/**
* Asks the server to reach every source it has and says what worked. A
* source that cannot be reached is reported, not thrown, so one broken
* connection does not hide the state of the others.
*/
export async function checkSources(data: DataSource): Promise<SourceReport[]> {
const { sources } = await data.listSources({});
return Promise.all(
sources.map(async (source): Promise<SourceReport> => {
if (!data.testSource) return { id: source.id, ok: false, lines: ["This backend cannot test sources."] };
try {
const result = await data.testSource({ target: { case: "id", value: source.id } });
return {
id: source.id,
ok: result.ok,
lines: [result.engineVersion ? `${result.message} (${result.engineVersion})` : result.message, ...result.checks.map((c) => `${c.ok ? "ok" : "failed"}: ${c.name}: ${c.message}`)],
};
} catch (error) {
// A source that cannot be reached comes back as a failed result; this catches the rest (the data server itself being down).
return { id: source.id, ok: false, lines: [queryErrorInfo(error).message] };
}
}),
);
}A database that is down is reported in that list and the other sources keep working. A client is told the secret's name, never its value: listSources shows credentialsSecret: "TD". When something between the server and Teradata echoes the sign-in in an error, the Teradata adapter replaces it with *** before the message leaves the server.
The test checks the config, then runs the Teradata source through the real dev server and a Connect client, with Teradata Query Service played by a stand-in fetch:
sources.test.ts:
import { createConnectTransport } from "@connectrpc/connect-node";
import { connectDataSource } from "@surfaces-ui/client-connect";
import { type DataSource, collect, toRows } from "@surfaces-ui/data";
import { type SurfacesServer, type TeradataSourceConfig, createSurfacesServer, envSecrets, openSources } from "@surfaces-ui/dev-server";
import { ConnectionKind, SourceKind } from "@surfaces-ui/proto";
import { afterEach, describe, expect, it, vi } from "vitest";
import { checkSources } from "./check-sources.js";
import config from "./surfaces.config.js";
const PASSWORD = "not-a-real-password-7f3a";
const BASIC = `Basic ${Buffer.from(`surfaces_reader:${PASSWORD}`).toString("base64")}`;
/** Every `secret` named anywhere in the config. */
function secretNames(value: unknown): string[] {
if (Array.isArray(value)) return value.flatMap(secretNames);
if (!value || typeof value !== "object") return [];
return Object.entries(value).flatMap(([key, v]) => (key === "secret" && typeof v === "string" ? [v] : secretNames(v)));
}
describe("the config", () => {
it("names each credential and holds none", () => {
expect(secretNames(config).sort()).toEqual(["LAKE", "PG", "TD"]);
const text = JSON.stringify(config);
expect(text).not.toMatch(/password\s*=|"(password|token|keyId|secretKey)"/i);
const shop = config.sources!.find((s) => s.id === "shop");
expect(shop?.engine === "duckdb" && shop.catalogs?.[0]?.uri).toMatch(/^host=\S+ port=5432 dbname=shop user=surfaces_reader sslmode=require$/);
});
it("reads only paths under the S3 connection's scope", () => {
const lake = config.sources!.find((s) => s.id === "lake");
if (lake?.engine !== "duckdb") throw new Error("lake is a DuckDB source");
for (const table of lake.tables) expect(lake.connections!.some((c) => table.path.startsWith(c.scope))).toBe(true);
});
it("finds the values under the variable names in .env.example", () => {
const env = { PG_PASSWORD: "a", LAKE_KEY_ID: "b", LAKE_SECRET: "c", TD_PASSWORD: "d" };
const resolve = envSecrets(env);
expect(resolve("PG")).toMatchObject({ password: "a" });
expect(resolve("LAKE")).toMatchObject({ keyId: "b", secret: "c" });
expect(resolve("TD")).toMatchObject({ password: "d" });
expect(resolve("MISSING")).toBeUndefined();
});
});
/**
* The Teradata source as the dev server opens it, with Query Service played
* by a stand-in `fetch`. No Teradata system is contacted. The Postgres and
* S3 sources are not opened here: DuckDB would install its postgres, httpfs
* and delta extensions, which downloads them.
*/
describe("the Teradata source behind the server", () => {
const warehouse = config.sources!.find((s): s is TeradataSourceConfig => s.id === "warehouse")!;
const realFetch = globalThis.fetch;
let server: SurfacesServer | undefined;
afterEach(async () => {
await server?.close();
server = undefined;
vi.unstubAllGlobals();
vi.unstubAllEnvs();
});
/** Starts the server on a free port with only the Teradata source, and reaches it over Connect like a browser would. */
async function start(queryService: (url: string, init: RequestInit) => Promise<Response>): Promise<DataSource> {
vi.stubEnv("TD_PASSWORD", PASSWORD);
vi.stubGlobal("fetch", (async (url: RequestInfo | URL, init: RequestInit = {}) =>
String(url).startsWith(warehouse.baseUrl) ? queryService(String(url), init) : realFetch(url, init)) as typeof fetch);
// The default resolver reads TD_PASSWORD from process.env when the source is first used.
const sources = await openSources({ sources: [warehouse] }, process.cwd());
server = await createSurfacesServer({ sources, discovery: false });
const { url } = await server.listen(0);
return connectDataSource({ transport: createConnectTransport({ baseUrl: url, httpVersion: "1.1" }) });
}
it("starts when Teradata cannot be reached, and says so without the password", async () => {
const data = await start(async (_url, init) => {
// A proxy that echoes the request in its error is the worst case: the message still must not carry the credential.
throw new Error(`connect ECONNREFUSED (sent ${(init.headers as Record<string, string>).Authorization})`);
});
const { sources } = await data.listSources({});
expect(sources.map((s) => s.id)).toEqual(["warehouse"]);
const [report] = await checkSources(data);
expect(report).toMatchObject({ id: "warehouse", ok: false });
expect(report!.lines.join("\n")).toMatch(/could not be reached/);
expect(report!.lines.join("\n")).not.toContain(PASSWORD);
expect(report!.lines.join("\n")).not.toContain(BASIC.slice(6));
});
it("signs in with the password from TD_PASSWORD, and never hands it to a client", async () => {
const seen: { url: string; authorization: string; body: Record<string, unknown> }[] = [];
const data = await start(async (url, init) => {
const body = init.body ? (JSON.parse(String(init.body)) as Record<string, unknown>) : {};
seen.push({ url, authorization: (init.headers as Record<string, string>).Authorization!, body });
if (url.endsWith("/sessions")) return Response.json({ sessionId: 42 });
const sql = String(body.query ?? "");
if (/DBCInfoV/.test(sql)) return Response.json({ results: [{ resultSet: true, columns: [{ name: "InfoData", type: "VARCHAR" }], data: [["17.20"]] }] });
if (/DatabasesV/.test(sql)) return Response.json({ results: [{ resultSet: true, columns: [{ name: "DatabaseName", type: "VARCHAR" }], data: [["sales"]] }] });
if (/FROM orders/.test(sql)) return Response.json({ results: [{ resultSet: true, columns: [{ name: "region", type: "VARCHAR(20)" }], data: [["Europe"], ["Asia"]] }] });
return Response.json({ results: [{ resultSet: true, columns: [{ name: "x", type: "INTEGER" }], data: [[1]] }] });
});
const [report] = await checkSources(data);
expect(report).toMatchObject({ id: "warehouse", ok: true });
expect(report!.lines[0]).toBe("Connected (Teradata 17.20)");
// Every request carried the credential read by secret name, and went to the configured system.
expect(seen.length).toBeGreaterThan(0);
for (const request of seen) {
expect(request.authorization).toBe(BASIC);
expect(request.url.startsWith("https://vantage.example.com:1443/systems/prod/")).toBe(true);
}
// The session is UTC and the default database is the configured one.
expect(seen.map((s) => s.body.query)).toEqual(expect.arrayContaining(["SET TIME ZONE INTERVAL '00:00' HOUR TO MINUTE", 'DATABASE "sales"']));
// A page of rows is cut by Teradata (no LIMIT/OFFSET there).
const result = await collect(data.execute({ sourceId: "warehouse", query: { baseSql: "SELECT region FROM orders", page: { limit: 2, offset: 0n } } }));
expect(toRows(result.table)).toEqual([{ region: "Europe" }, { region: "Asia" }]);
const sent = String(seen.at(-1)!.body.query);
expect(sent).toContain("SELECT region FROM orders");
expect(sent).toMatch(/^SELECT TOP 2 /);
expect(sent).not.toMatch(/\bLIMIT\b|\bOFFSET\b/i);
// What a client sees names the secret, never its value.
const { sources } = await data.listSources({});
expect(sources[0]).toMatchObject({ id: "warehouse", kind: SourceKind.TERADATA, readOnly: true });
expect(sources[0]!.connections).toEqual([expect.objectContaining({ name: "prod", kind: ConnectionKind.HTTP, scope: warehouse.baseUrl, credentialsSecret: "TD" })]);
const shown = JSON.stringify(sources, (_k, v) => (typeof v === "bigint" ? String(v) : v));
expect(shown).not.toContain(PASSWORD);
expect(shown).not.toContain(BASIC.slice(6));
});
it("names the secret, not its value, when Teradata refuses the sign-in", async () => {
const data = await start(async () => new Response(`bad credentials for ${PASSWORD}`, { status: 401 }));
const [report] = await checkSources(data);
expect(report!.ok).toBe(false);
expect(report!.lines.join("\n")).toMatch(/refused the sign-in.*"TD"/);
expect(report!.lines.join("\n")).not.toContain(PASSWORD);
});
});What has not been run:
- Real S3. Opening the
lakesource makes DuckDB install itshttpfsanddeltaextensions, which downloads them, so the test does not open it.packages/engine-duckdbtests only the secret DuckDB is given for a connection; no object store has been read, real or stand-in. - A hosted Postgres over TLS. Postgres has been run only against Postgres 17 in a local container (
packages/dev-server/src/postgres.test.ts, whenSURFACES_TEST_POSTGRESis set). - A real Teradata system. The Query Service requests follow Teradata's published specification; nothing here has run against one.
packages/dev-server/src/teradata.test.tsruns live whenSURFACES_TEST_TERADATAis set. - When a system cannot be reached at all,
testSourcereturnsok: falsewith the reason, instead of failing the call.checkSourcesstill catches errors, for when the data server itself is down.
Source and test: apps/recipes/src/connect-sources