Skip to content
Merged
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
5 changes: 5 additions & 0 deletions .github/workflows/verify.yml
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,11 @@ jobs:
- name: Verify user-directory storage through CLI commands
run: bun test tests/user-directories.test.ts

- name: Verify Windows native credential storage
if: runner.os == 'Windows'
working-directory: cli
run: bun test src/lib/windows-credentials.test.ts

- name: Build npm bundle
working-directory: cli
run: bun run build
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed

- Document the Windows PowerShell installation path, including Bun's native installer and the `bun.exe` requirement for npm launchers.
- Store Windows credentials in the current user's Credential Manager by default, migrate existing file credentials after successful native storage, and fail closed on vault errors with the cause and how to opt out. `ALTERTABLE_SECRET_BACKEND=file` keeps the file backend, for example in CI.
- Stop queries that run longer than 6 minutes from failing with `Request failed (network error): … (The operation timed out.)`. `--read-timeout` is now the only limit on query streams, and `0` means no limit.

- Keep the TypeScript manifest compiler out of CLI downloads by installing it with the cached single-file preview runtime.
Expand Down
6 changes: 4 additions & 2 deletions DEVELOPMENT.md
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,10 @@ Keep updater tests hermetic. Use fake `fetch` implementations and temp executabl
## Credential storage

- **Non-secret config**: `~/.config/altertable/config`
- **Secrets**: macOS Keychain when available, otherwise `~/.config/altertable/credentials` (`chmod 600`)
- Override backend: `ALTERTABLE_SECRET_BACKEND=keychain|file`
- **Secrets**: macOS Keychain when available, Windows Credential Manager on Windows, otherwise `~/.config/altertable/credentials` (`chmod 600`)
- Override backend: `ALTERTABLE_SECRET_BACKEND=keychain|windows|file`
- On Windows, each process lists the vault once and reuses that snapshot until another CLI process writes credentials; writes replace the random token in `credential-manager.revision` in the config directory
- The real Credential Manager test (`cli/src/lib/windows-credentials.test.ts`) only runs on Windows; CI runs it in the user-directories Windows job

## Local deployment endpoints

Expand Down
14 changes: 12 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -263,8 +263,18 @@ Configuration is stored in `ALTERTABLE_CONFIG_HOME` when set, otherwise
`$XDG_CONFIG_HOME/altertable`, otherwise `~/.config/altertable`. Directory settings must
be absolute. The default home directory follows OS conventions (`HOME` on Unix, `USERPROFILE` on Windows).

Credentials use macOS Keychain when available; the file fallback stores plaintext credentials.
Keep the configuration directory private.
Credentials use macOS Keychain when available and the current user's Windows
Credential Manager on Windows. Windows access goes through Windows PowerShell, with
secret values passed over stdin rather than process arguments. Existing Windows file
credentials move to Credential Manager when first read and leave the file only after
native storage succeeds. Credential Manager errors fail the command instead of
saving plaintext; each Windows secret can be at most 2,560 bytes.

Other systems use a plaintext file with Unix mode `600`. Set
`ALTERTABLE_SECRET_BACKEND=file` to use the file everywhere (for example, in CI or
where PowerShell is restricted), or `ALTERTABLE_SECRET_BACKEND=windows` to require
Credential Manager. Keep the configuration directory private; Windows ACLs are not
checked when the file backend is selected.

Named profiles store credentials and endpoint overrides per environment. Global display defaults (`query_layout`, `query_max_width`, `query_pager`) stay in the root config and apply to all profiles.

Expand Down
1 change: 1 addition & 0 deletions cli/src/lib/config-files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,5 +148,6 @@ export function kvUnset(filePath: string, key: string): void {
.split('\n')
.filter(line => configKey(line) !== key)
.join('\n');
if (output === content) return;
writeConfigFileAtomically(filePath, output, 'update');
}
4 changes: 3 additions & 1 deletion cli/src/lib/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,9 @@ const ENV_SCHEMA = {
ALTERTABLE_OAUTH_REDIRECT_PORT: { parse: port },
ALTERTABLE_OAUTH_SCOPE: { parse: optional },
ALTERTABLE_PROFILE: { parse: optional },
ALTERTABLE_SECRET_BACKEND: { parse: oneOf(['file', 'keychain'] as const) },
ALTERTABLE_SECRET_BACKEND: {
parse: oneOf(['file', 'keychain', 'windows'] as const),
},
ALTERTABLE_UPDATE_CHECK: {
parse: oneOf(['0', '1', 'false', 'true', 'never'] as const),
},
Expand Down
275 changes: 275 additions & 0 deletions cli/src/lib/secrets-windows.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,275 @@
import { afterEach, beforeEach, expect, test } from 'bun:test';
import { existsSync, mkdtempSync, rmSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';

import { credentialsFile, kvGet, kvSet } from '@/lib/config.ts';
import { ensureProfileId, getProfileId } from '@/lib/profile-store.ts';
import { createSecretStore } from '@/lib/secrets.ts';

const savedEnv = {
ALTERTABLE_CONFIG_HOME: process.env.ALTERTABLE_CONFIG_HOME,
ALTERTABLE_SECRET_BACKEND: process.env.ALTERTABLE_SECRET_BACKEND,
};
let directory = '';
beforeEach(() => {
directory = mkdtempSync(join(tmpdir(), 'altertable-wincred-test-'));
process.env.ALTERTABLE_CONFIG_HOME = directory;
delete process.env.ALTERTABLE_SECRET_BACKEND;
});
afterEach(() => {
rmSync(directory, { recursive: true, force: true });
for (const [name, value] of Object.entries(savedEnv)) {
if (value === undefined) delete process.env[name];
else process.env[name] = value;
}
});

type ProcessResult = {
status: number | null;
stdout: string;
stderr?: string;
error?: Error;
};

// Stands in for powershell.exe: it speaks the wrapper's stdin/stdout protocol
// over an in-memory vault. Pass another fake's values to model a second CLI
// process sharing the same vault.
function fakeWindowsVault(values = new Map<string, string>()) {
const calls: { command: string; args: string[]; input: string }[] = [];
const failures = new Map<string, ProcessResult>();
const store = createSecretStore({
platform: 'win32',
spawnSync(command, args, options) {
if (typeof options.input !== 'string')
throw new Error('Expected JSON stdin');
calls.push({ command, args, input: options.input });
const request = JSON.parse(options.input) as {
action: string;
account?: string;
value?: string;
};
const failure = failures.get(request.action);
if (failure) return failure;
if (request.action === 'list') {
return {
status: 0,
stdout: JSON.stringify({ credentials: Object.fromEntries(values) }),
};
}
if (request.action === 'set')
values.set(request.account!, request.value!);
if (request.action === 'delete') values.delete(request.account!);
return { status: 0, stdout: '{}' };
},
});
return { store, values, calls, failures };
}

function actions(calls: { input: string }[]): string[] {
return calls.map(
call => (JSON.parse(call.input) as { action: string }).action
);
}

function stableAccount(key: string, profileName: string): string {
return `profile/${getProfileId(profileName)}/${key}`;
}

test('Windows defaults to Credential Manager without writing plaintext', () => {
const { store, values, calls } = fakeWindowsVault();
store.secretSet('api-key', 'atm_native_test', 'default', { fromArgv: true });

expect(values.get(stableAccount('api-key', 'default'))).toBe(
'atm_native_test'
);
expect(store.secretGet('api-key', 'default')).toBe('atm_native_test');
expect(store.secretExists('api-key', 'default')).toBe(true);
expect(store.secretStoreDisplay()).toBe('Windows Credential Manager');
expect(existsSync(credentialsFile())).toBe(false);
expect(calls.every(call => call.command === 'powershell.exe')).toBe(true);
expect(calls.some(call => call.input.includes('atm_native_test'))).toBe(true);
expect(
calls.every(call => !call.args.join(' ').includes('atm_native_test'))
).toBe(true);

store.secretDelete('api-key', 'default');
expect(store.secretExists('api-key', 'default')).toBe(false);
});

test('Windows serves a command’s secret lookups from one vault read', () => {
const vault = fakeWindowsVault();
vault.values.set(
`profile/${ensureProfileId('default')}/api-key`,
'atm_native_test'
);

// A management command looks up the OAuth token (current and pre-ID
// account names) before falling back to the API key.
expect(vault.store.secretGet('oauth/access-token', 'default')).toBe('');
expect(vault.store.secretGet('api-key', 'default')).toBe('atm_native_test');
expect(vault.store.secretExists('lakehouse/password', 'default')).toBe(false);

expect(vault.calls.map(call => JSON.parse(call.input).action)).toEqual([
'list',
]);
});

test('Windows reuses one vault read until another CLI process writes', () => {
const first = fakeWindowsVault();
const second = fakeWindowsVault(first.values);

first.store.secretSet('api-key', 'atm_one', 'default');
expect(first.store.secretGet('api-key', 'default')).toBe('atm_one');
first.store.secretSet('api-key', 'atm_two', 'default');
expect(first.store.secretGet('api-key', 'default')).toBe('atm_two');
expect(actions(first.calls)).toEqual(['set', 'list', 'set']);

second.store.secretSet('api-key', 'atm_three', 'default');
expect(first.store.secretGet('api-key', 'default')).toBe('atm_three');
expect(first.store.secretGet('api-key', 'default')).toBe('atm_three');
expect(actions(first.calls)).toEqual(['set', 'list', 'set', 'list']);
});

test('Windows deletes only secrets the vault holds', () => {
const { store, calls } = fakeWindowsVault();
store.secretSet('api-key', 'atm_native_test', 'default');

store.secretDelete('api-key', 'default');
store.secretDelete('lakehouse/password', 'default');

// Each key is looked up under its stable and pre-ID account names.
expect(actions(calls)).toEqual(['set', 'list', 'delete']);
expect(store.secretExists('api-key', 'default')).toBe(false);
});

test('Windows migrates existing file secrets only after native storage succeeds', () => {
const { store, values, failures } = fakeWindowsVault();
kvSet(credentialsFile(), 'profile/default/api-key', 'atm_legacy_test');
failures.set('set', { status: 1, stdout: '' });
expect(() => store.secretGet('api-key', 'default')).toThrow(
'Windows Credential Manager could not store credentials.'
);
expect(kvGet(credentialsFile(), 'profile/default/api-key')).toBe(
'atm_legacy_test'
);

failures.clear();
store.migrateProfileSecrets('default', ['api-key']);
expect(values.get(stableAccount('api-key', 'default'))).toBe(
'atm_legacy_test'
);
expect(values.has('profile/default/api-key')).toBe(false);
expect(kvGet(credentialsFile(), 'profile/default/api-key')).toBe('');
});

test('Windows native failures never fall back to a plaintext write or stale file value', () => {
const { store, values, failures } = fakeWindowsVault();
failures.set('set', { status: 1, stdout: '' });
expect(() =>
store.secretSet('api-key', 'atm_do_not_write', 'default')
).toThrow('Windows Credential Manager');
expect(existsSync(credentialsFile())).toBe(false);

kvSet(credentialsFile(), 'profile/default/api-key', 'atm_stale');
failures.set('list', { status: 1, stdout: '' });
failures.set('delete', { status: 1, stdout: '' });
expect(() => store.secretGet('api-key', 'default')).toThrow(
'Windows Credential Manager could not read credentials.'
);
expect(() => store.secretExists('api-key', 'default')).toThrow(
'Windows Credential Manager could not read credentials.'
);
expect(() => store.secretDelete('api-key', 'default')).toThrow(
'Windows Credential Manager could not read credentials.'
);

failures.delete('list');
values.set('profile/default/api-key', 'atm_native');
expect(() => store.secretDelete('api-key', 'default')).toThrow(
'Windows Credential Manager could not delete credentials.'
);
expect(values.get('profile/default/api-key')).toBe('atm_native');
expect(kvGet(credentialsFile(), 'profile/default/api-key')).toBe('atm_stale');
});

test('Windows rejects secrets larger than Credential Manager stores', () => {
const { store, calls } = fakeWindowsVault();
const oversized = 'x'.repeat(2561);

expect(() =>
store.secretSet('oauth/access-token', oversized, 'default')
).toThrow(
'This secret is 2561 bytes; Windows Credential Manager stores at most 2560.'
);
expect(calls).toHaveLength(0);
expect(existsSync(credentialsFile())).toBe(false);

store.secretSet('oauth/access-token', 'x'.repeat(2560), 'default');
expect(store.secretGet('oauth/access-token', 'default')).toHaveLength(2560);
});

test.each([
[
{
status: null,
stdout: '',
error: Object.assign(new Error('spawn'), { code: 'ENOENT' }),
},
'requires Windows PowerShell (powershell.exe), which was not found.',
],
[
{
status: null,
stdout: '',
error: Object.assign(new Error('spawn'), { code: 'ETIMEDOUT' }),
},
'did not read credentials within 15 seconds.',
],
[
{ status: 2, stdout: '' },
'PowerShell is restricted by a language mode policy',
],
[
{ status: 3, stdout: '', stderr: '1312' },
'this logon session has no credential vault',
],
[{ status: 3, stdout: '', stderr: '5' }, '(Windows error 5).'],
] satisfies [ProcessResult, string][])(
'Windows explains native failures and how to opt out: %#',
(failure, reason) => {
const { store, failures } = fakeWindowsVault();
failures.set('list', failure);

let message = '';
try {
store.secretGet('api-key', 'default');
} catch (error) {
message = (error as Error).message;
}
expect(message).toContain(reason);
expect(message).toContain('Set ALTERTABLE_SECRET_BACKEND=file');
}
);

test('Windows backend can be required explicitly, and opted out of', () => {
process.env.ALTERTABLE_SECRET_BACKEND = 'windows';
expect(fakeWindowsVault().store.secretStoreDisplay()).toBe(
'Windows Credential Manager'
);
const linux = createSecretStore({
platform: 'linux',
spawnSync: () => ({ status: 1, stdout: '' }),
});
expect(() => linux.secretStoreDisplay()).toThrow(
'ALTERTABLE_SECRET_BACKEND=windows requires Windows Credential Manager.'
);

process.env.ALTERTABLE_SECRET_BACKEND = 'file';
const { store, calls } = fakeWindowsVault();
store.secretSet('api-key', 'atm_file_test', 'default');
expect(kvGet(credentialsFile(), stableAccount('api-key', 'default'))).toBe(
'atm_file_test'
);
expect(calls).toHaveLength(0);
});
Loading
Loading