Improve connector credential setup UX

This commit is contained in:
Luca Martial 2026-05-10 16:12:51 -07:00
parent d89be2390f
commit 1b5a9fe120
6 changed files with 882 additions and 115 deletions

View file

@ -1,8 +1,21 @@
import { describe, expect, it } from 'vitest';
import { mkdtemp, rm, writeFile } from 'node:fs/promises';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
import type { KtxProjectConnectionConfig } from '../../../project/index.js';
import { metabaseRuntimeConfigFromLocalConnection } from './local-metabase.adapter.js';
describe('metabaseRuntimeConfigFromLocalConnection', () => {
let tempDir: string;
beforeEach(async () => {
tempDir = await mkdtemp(join(tmpdir(), 'ktx-metabase-runtime-'));
});
afterEach(async () => {
await rm(tempDir, { recursive: true, force: true });
});
it('resolves api_url and env-backed api_key_ref from a flat ktx.yaml connection', () => {
const connection: KtxProjectConnectionConfig = {
driver: 'metabase',
@ -20,6 +33,21 @@ describe('metabaseRuntimeConfigFromLocalConnection', () => {
});
});
it('resolves file-backed api_key_ref from pasted setup secrets', async () => {
const keyPath = join(tempDir, 'metabase-main-api-key');
await writeFile(keyPath, 'mb_file_key\n', 'utf-8'); // pragma: allowlist secret
const connection: KtxProjectConnectionConfig = {
driver: 'metabase',
api_url: 'https://metabase.example.com',
api_key_ref: `file:${keyPath}`,
};
expect(metabaseRuntimeConfigFromLocalConnection('prod-metabase', connection)).toEqual({
apiUrl: 'https://metabase.example.com',
apiKey: 'mb_file_key', // pragma: allowlist secret
});
});
it('accepts url as the local api URL alias', () => {
const connection: KtxProjectConnectionConfig = {
driver: 'metabase',

View file

@ -1,5 +1,6 @@
import type { KtxLocalProject, KtxProjectConnectionConfig } from '../../../project/index.js';
import { ktxLocalStateDbPath } from '../../../project/index.js';
import { resolveKtxConfigReference } from '../../../core/config-reference.js';
import { DEFAULT_METABASE_CLIENT_CONFIG, DefaultMetabaseConnectionClientFactory } from './client.js';
import {
IngestMetabaseClientFactory,
@ -13,14 +14,6 @@ function stringField(value: unknown): string | null {
return typeof value === 'string' && value.trim().length > 0 ? value.trim() : null;
}
function resolveEnvReference(ref: string, env: NodeJS.ProcessEnv): string | null {
if (!ref.startsWith('env:')) {
return null;
}
const name = ref.slice('env:'.length);
return stringField(env[name]);
}
function hasNetworkProxy(connection: KtxProjectConnectionConfig): boolean {
return connection.networkProxy != null || connection.network_proxy != null;
}
@ -42,7 +35,7 @@ export function metabaseRuntimeConfigFromLocalConnection(
const apiUrl = stringField(connection.api_url) ?? stringField(connection.apiUrl) ?? stringField(connection.url);
const literalApiKey = stringField(connection.api_key) ?? stringField(connection.apiKey);
const apiKeyRef = stringField(connection.api_key_ref) ?? stringField(connection.apiKeyRef);
const apiKey = literalApiKey ?? (apiKeyRef ? resolveEnvReference(apiKeyRef, env) : null);
const apiKey = literalApiKey ?? (apiKeyRef ? resolveKtxConfigReference(apiKeyRef, env) : null);
if (!apiUrl) {
throw new Error(`Connection "${connectionId}" is missing metabase api_url`);