getConnectorCredential, setConnectorCredential and setConnectorCredentialBundle are now plain functions over a SecretStore in packages/agent/src/connectors/connector-credentials.ts. The secret names, metadata layout and the rule that tokens never go into plaintext metadata are unchanged, so existing vault files keep working. A bundle still lands as one update: SecretStore gains writeBatch, which VaultStore applies with a single vault-file replacement. Connector tests seed a Map-backed SecretStore instead of mocking VaultStore methods; the helper tests from core and server moved to the agent package.
187 lines
6.6 KiB
TypeScript
187 lines
6.6 KiB
TypeScript
import { describe, it, expect, afterEach, vi } from 'vitest';
|
|
import fs from 'node:fs';
|
|
import os from 'node:os';
|
|
import path from 'node:path';
|
|
import { VaultStore } from '@waggle/core';
|
|
import {
|
|
getConnectorCredential,
|
|
setConnectorCredential,
|
|
setConnectorCredentialBundle,
|
|
} from '../../src/connectors/connector-credentials.js';
|
|
|
|
describe('connector credentials in the secret store', () => {
|
|
const tempDirs: string[] = [];
|
|
|
|
function openVault(): { vault: VaultStore; dir: string } {
|
|
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'waggle-connector-cred-'));
|
|
tempDirs.push(dir);
|
|
return { vault: new VaultStore(dir), dir };
|
|
}
|
|
|
|
function readVaultFile(dir: string): Record<string, { encrypted: string; metadata?: Record<string, unknown>; updatedAt: string }> {
|
|
return JSON.parse(fs.readFileSync(path.join(dir, 'vault.json'), 'utf-8'));
|
|
}
|
|
|
|
afterEach(() => {
|
|
for (const dir of tempDirs) fs.rmSync(dir, { recursive: true, force: true });
|
|
tempDirs.length = 0;
|
|
});
|
|
|
|
it('stores a credential and reads it back with its metadata', () => {
|
|
const { vault } = openVault();
|
|
|
|
setConnectorCredential(vault, 'github', {
|
|
type: 'bearer',
|
|
value: 'ghp_test_token_12345',
|
|
scopes: ['repo', 'user'],
|
|
});
|
|
|
|
const cred = getConnectorCredential(vault, 'github');
|
|
expect(cred).not.toBeNull();
|
|
expect(cred!.value).toBe('ghp_test_token_12345');
|
|
expect(cred!.type).toBe('bearer');
|
|
expect(cred!.scopes).toEqual(['repo', 'user']);
|
|
expect(cred!.isExpired).toBe(false);
|
|
});
|
|
|
|
it('reports a credential past its expiry as expired', () => {
|
|
const { vault } = openVault();
|
|
|
|
setConnectorCredential(vault, 'slack', {
|
|
type: 'oauth2',
|
|
value: 'xoxb-expired-token',
|
|
expiresAt: '2020-01-01T00:00:00.000Z',
|
|
});
|
|
|
|
expect(getConnectorCredential(vault, 'slack')!.isExpired).toBe(true);
|
|
});
|
|
|
|
it('returns null for a connector without a credential, and after it is deleted', () => {
|
|
const { vault } = openVault();
|
|
expect(getConnectorCredential(vault, 'nonexistent')).toBeNull();
|
|
|
|
setConnectorCredential(vault, 'github', { type: 'bearer', value: 'ghp_test_token' });
|
|
expect(getConnectorCredential(vault, 'github')).not.toBeNull();
|
|
|
|
vault.delete('connector:github');
|
|
expect(getConnectorCredential(vault, 'github')).toBeNull();
|
|
});
|
|
|
|
it('defaults the type to api_key when the stored metadata has none', () => {
|
|
const { vault } = openVault();
|
|
vault.set('connector:legacy', 'legacy-token');
|
|
|
|
expect(getConnectorCredential(vault, 'legacy')).toMatchObject({
|
|
value: 'legacy-token',
|
|
type: 'api_key',
|
|
isExpired: false,
|
|
});
|
|
});
|
|
|
|
it('keeps the refresh token out of plaintext, in its own encrypted entry', () => {
|
|
const { vault, dir } = openVault();
|
|
const refreshToken = 'rt-super-secret-refresh-token-xyz';
|
|
|
|
setConnectorCredential(vault, 'github', {
|
|
type: 'oauth2',
|
|
value: 'gho_access_token_123',
|
|
refreshToken,
|
|
expiresAt: '2099-01-01T00:00:00Z',
|
|
scopes: ['repo', 'user'],
|
|
});
|
|
|
|
expect(fs.readFileSync(path.join(dir, 'vault.json'), 'utf-8')).not.toContain(refreshToken);
|
|
const parsed = readVaultFile(dir);
|
|
expect(parsed['connector:github'].metadata).toEqual({
|
|
credentialType: 'oauth2',
|
|
expiresAt: '2099-01-01T00:00:00Z',
|
|
scopes: ['repo', 'user'],
|
|
});
|
|
expect(parsed['connector:github:refresh'].encrypted).toMatch(/^[0-9a-f]+:[0-9a-f]+:[0-9a-f]+$/);
|
|
expect(parsed['connector:github:refresh']).not.toHaveProperty('metadata');
|
|
});
|
|
|
|
it('returns the decrypted refresh token', () => {
|
|
const { vault } = openVault();
|
|
|
|
setConnectorCredential(vault, 'github', {
|
|
type: 'oauth2',
|
|
value: 'gho_access_token_456',
|
|
refreshToken: 'rt-secret-refresh-token-abc',
|
|
expiresAt: '2099-01-01T00:00:00Z',
|
|
scopes: ['repo'],
|
|
});
|
|
|
|
expect(getConnectorCredential(vault, 'github')).toEqual({
|
|
value: 'gho_access_token_456',
|
|
type: 'oauth2',
|
|
refreshToken: 'rt-secret-refresh-token-abc',
|
|
expiresAt: '2099-01-01T00:00:00Z',
|
|
scopes: ['repo'],
|
|
isExpired: false,
|
|
});
|
|
});
|
|
|
|
it('does not create a refresh entry for a credential without one', () => {
|
|
const { vault, dir } = openVault();
|
|
|
|
setConnectorCredential(vault, 'slack', { type: 'bearer', value: 'xoxb-token-123' });
|
|
|
|
const parsed = readVaultFile(dir);
|
|
expect(parsed['connector:slack']).toBeDefined();
|
|
expect(parsed['connector:slack:refresh']).toBeUndefined();
|
|
expect(getConnectorCredential(vault, 'slack')!.refreshToken).toBeUndefined();
|
|
});
|
|
|
|
it('removes the old refresh token when a credential is stored again without one', () => {
|
|
const { vault } = openVault();
|
|
|
|
setConnectorCredential(vault, 'github', {
|
|
type: 'oauth2',
|
|
value: 'gho_token_1',
|
|
refreshToken: 'rt-old-refresh',
|
|
});
|
|
expect(getConnectorCredential(vault, 'github')!.refreshToken).toBe('rt-old-refresh');
|
|
|
|
setConnectorCredential(vault, 'github', { type: 'oauth2', value: 'gho_token_2' });
|
|
|
|
const cred = getConnectorCredential(vault, 'github');
|
|
expect(cred!.value).toBe('gho_token_2');
|
|
expect(cred!.refreshToken).toBeUndefined();
|
|
expect(vault.has('connector:github:refresh')).toBe(false);
|
|
});
|
|
|
|
it('writes a credential bundle as one secret-store update', () => {
|
|
const { vault, dir } = openVault();
|
|
const writeBatch = vi.spyOn(vault, 'writeBatch');
|
|
const set = vi.spyOn(vault, 'set');
|
|
|
|
setConnectorCredentialBundle(vault, 'jira', { type: 'basic', value: 'jira-token' }, {
|
|
email: 'user@example.com',
|
|
base_url: 'https://team.atlassian.net',
|
|
});
|
|
|
|
expect(writeBatch).toHaveBeenCalledTimes(1);
|
|
expect(set).not.toHaveBeenCalled();
|
|
expect(getConnectorCredential(vault, 'jira')!.value).toBe('jira-token');
|
|
expect(vault.get('connector:jira:email')!.value).toBe('user@example.com');
|
|
expect(vault.get('connector:jira:base_url')!.value).toBe('https://team.atlassian.net');
|
|
|
|
const parsed = readVaultFile(dir);
|
|
expect(parsed['connector:jira:email']).not.toHaveProperty('metadata');
|
|
expect(new Set(Object.values(parsed).map(entry => entry.updatedAt)).size).toBe(1);
|
|
});
|
|
|
|
it('rejects an invalid companion suffix before writing anything', () => {
|
|
const { vault } = openVault();
|
|
setConnectorCredential(vault, 'jira', { type: 'basic', value: 'old-token' });
|
|
|
|
expect(() => setConnectorCredentialBundle(vault, 'jira', { type: 'basic', value: 'new-token' }, {
|
|
'Base-URL': 'https://team.atlassian.net',
|
|
})).toThrow(TypeError);
|
|
|
|
expect(getConnectorCredential(vault, 'jira')!.value).toBe('old-token');
|
|
expect(vault.list().map(entry => entry.name)).toEqual(['connector:jira']);
|
|
});
|
|
});
|