refactor(agent): move the connector credential helpers out of VaultStore (P1.2)

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.
This commit is contained in:
shumeiko.it
2026-10-24 16:34:20 +02:00
parent 23431550ae
commit 3528a242f6
60 changed files with 602 additions and 606 deletions

View File

@@ -6,6 +6,7 @@
*/
import type { WaggleConnector, ConnectorResult } from './connector-sdk.js';
import { getConnectorCredential } from './connectors/connector-credentials.js';
import type { ToolDefinition } from './tools.js';
import type { VaultStore } from '@waggle/core';
import type { ConnectorDefinition, ConnectorHealth } from '@waggle/shared';
@@ -109,7 +110,7 @@ export class ConnectorRegistry {
if (this.connectors.get(connector.id) !== connector) return false;
if (this.hydration.get(connector)?.status !== 'ready') return false;
if (ALWAYS_CONNECTED_CONNECTOR_IDS.has(connector.id)) return true;
const cred = this.vault.getConnectorCredential(connector.id);
const cred = getConnectorCredential(this.vault, connector.id);
return Boolean(
cred
&& !cred.isExpired
@@ -128,7 +129,7 @@ export class ConnectorRegistry {
/** Get ConnectorDefinition[] with live status from vault (for REST API responses) */
getDefinitions(): ConnectorDefinition[] {
return [...this.connectors.values()].map(c => {
const cred = this.vault.getConnectorCredential(c.id);
const cred = getConnectorCredential(this.vault, c.id);
let status: ConnectorDefinition['status'] = 'disconnected';
if (cred) {
status = cred.isExpired ? 'expired' : (this.isConnected(c) ? 'connected' : 'disconnected');

View File

@@ -4,6 +4,7 @@
*/
import { BaseConnector, type ConnectorAction, type ConnectorResult } from '../connector-sdk.js';
import { getConnectorCredential } from './connector-credentials.js';
import type { VaultStore } from '@waggle/core';
import type { ConnectorHealth } from '@waggle/shared';
@@ -107,7 +108,7 @@ export class AirtableConnector extends BaseConnector {
private token: string | null = null;
async connect(vault: VaultStore): Promise<void> {
const cred = vault.getConnectorCredential(this.id);
const cred = getConnectorCredential(vault, this.id);
this.token = cred?.value ?? null;
}

View File

@@ -4,6 +4,7 @@
*/
import { BaseConnector, type ConnectorAction, type ConnectorResult } from '../connector-sdk.js';
import { getConnectorCredential } from './connector-credentials.js';
import type { VaultStore } from '@waggle/core';
import type { ConnectorHealth } from '@waggle/shared';
@@ -101,7 +102,7 @@ export class AsanaConnector extends BaseConnector {
private token: string | null = null;
async connect(vault: VaultStore): Promise<void> {
const cred = vault.getConnectorCredential(this.id);
const cred = getConnectorCredential(vault, this.id);
this.token = cred?.value ?? null;
}

View File

@@ -4,6 +4,7 @@
*/
import { BaseConnector, type ConnectorAction, type ConnectorResult } from '../connector-sdk.js';
import { getConnectorCredential } from './connector-credentials.js';
import type { VaultStore } from '@waggle/core';
import type { ConnectorHealth } from '@waggle/shared';
@@ -96,7 +97,7 @@ export class BitbucketConnector extends BaseConnector {
private token: string | null = null;
async connect(vault: VaultStore): Promise<void> {
const cred = vault.getConnectorCredential(this.id);
const cred = getConnectorCredential(vault, this.id);
this.token = cred?.value ?? null;
}

View File

@@ -8,6 +8,7 @@
*/
import { BaseConnector, type ConnectorAction, type ConnectorResult } from '../connector-sdk.js';
import { getConnectorCredential } from './connector-credentials.js';
import type { VaultStore } from '@waggle/core';
import type { ConnectorHealth } from '@waggle/shared';
@@ -91,7 +92,7 @@ export class ComposioConnector extends BaseConnector {
private apiKey: string | null = null;
async connect(vault: VaultStore): Promise<void> {
const cred = vault.getConnectorCredential(this.id);
const cred = getConnectorCredential(vault, this.id);
this.apiKey = cred?.value ?? null;
}

View File

@@ -4,6 +4,7 @@
*/
import { BaseConnector, type ConnectorAction, type ConnectorResult } from '../connector-sdk.js';
import { getConnectorCredential } from './connector-credentials.js';
import type { VaultStore } from '@waggle/core';
import type { ConnectorHealth } from '@waggle/shared';
@@ -90,7 +91,7 @@ export class ConfluenceConnector extends BaseConnector {
private baseUrl: string | null = null;
async connect(vault: VaultStore): Promise<void> {
const cred = vault.getConnectorCredential(this.id);
const cred = getConnectorCredential(vault, this.id);
if (!cred) {
this.authHeader = null;
this.baseUrl = null;

View File

@@ -0,0 +1,88 @@
/**
* Connector credentials in the secret store.
*
* Secret names (the layout existing vault files already use):
* connector:<id> token; metadata { credentialType, expiresAt, scopes }
* connector:<id>:refresh OAuth refresh token
* connector:<id>:<suffix> companion values such as email or base_url
* Metadata is stored unencrypted, so tokens and refresh tokens never go there.
*/
import type { SecretStore, SecretWrite } from '@waggle/core';
export interface ConnectorCredentialInput {
type: 'api_key' | 'oauth2' | 'bearer' | 'basic';
value: string;
refreshToken?: string;
expiresAt?: string;
scopes?: string[];
}
export interface StoredConnectorCredential {
value: string;
type: string;
refreshToken?: string;
expiresAt?: string;
scopes?: string[];
isExpired: boolean;
}
/** The connector's credential with its refresh token, or null when none is stored. */
export function getConnectorCredential(secrets: SecretStore, connectorId: string): StoredConnectorCredential | null {
const entry = secrets.get(`connector:${connectorId}`);
if (!entry) return null;
const expiresAt = entry.metadata?.expiresAt as string | undefined;
const refreshEntry = secrets.get(`connector:${connectorId}:refresh`);
return {
value: entry.value,
type: (entry.metadata?.credentialType as string) ?? 'api_key',
refreshToken: refreshEntry?.value,
expiresAt,
scopes: entry.metadata?.scopes as string[] | undefined,
isExpired: expiresAt ? new Date(expiresAt) < new Date() : false,
};
}
/** Store a connector credential. Storing one without a refresh token removes the old refresh token. */
export function setConnectorCredential(
secrets: SecretStore,
connectorId: string,
credential: ConnectorCredentialInput,
): void {
setConnectorCredentialBundle(secrets, connectorId, credential);
}
/**
* Store a connector credential and its companion values in one secret-store
* update. This prevents a multi-field credential (for example Jira token +
* email + site origin) from being partially updated.
*/
export function setConnectorCredentialBundle(
secrets: SecretStore,
connectorId: string,
credential: ConnectorCredentialInput,
relatedSecrets: Readonly<Record<string, string>> = {},
): void {
const related = Object.entries(relatedSecrets);
if (related.some(([suffix]) => !/^[a-z][a-z0-9_]*$/.test(suffix))) {
throw new TypeError('Invalid connector credential suffix');
}
const refreshName = `connector:${connectorId}:refresh`;
const writes: SecretWrite[] = [{
name: `connector:${connectorId}`,
value: credential.value,
metadata: {
credentialType: credential.type,
expiresAt: credential.expiresAt,
scopes: credential.scopes,
},
}];
if (credential.refreshToken) {
writes.push({ name: refreshName, value: credential.refreshToken });
}
for (const [suffix, value] of related) {
writes.push({ name: `connector:${connectorId}:${suffix}`, value });
}
secrets.writeBatch({ set: writes, delete: credential.refreshToken ? [] : [refreshName] });
}

View File

@@ -4,6 +4,7 @@
*/
import { BaseConnector, type ConnectorAction, type ConnectorResult } from '../connector-sdk.js';
import { getConnectorCredential } from './connector-credentials.js';
import type { VaultStore } from '@waggle/core';
import type { ConnectorHealth } from '@waggle/shared';
@@ -94,7 +95,7 @@ export class DiscordConnector extends BaseConnector {
private token: string | null = null;
async connect(vault: VaultStore): Promise<void> {
const cred = vault.getConnectorCredential(this.id);
const cred = getConnectorCredential(vault, this.id);
this.token = cred?.value ?? null;
}

View File

@@ -5,6 +5,7 @@
*/
import { BaseConnector, type ConnectorAction, type ConnectorResult } from '../connector-sdk.js';
import { getConnectorCredential } from './connector-credentials.js';
import type { VaultStore } from '@waggle/core';
import type { ConnectorHealth } from '@waggle/shared';
@@ -89,7 +90,7 @@ export class DropboxConnector extends BaseConnector {
private token: string | null = null;
async connect(vault: VaultStore): Promise<void> {
const cred = vault.getConnectorCredential(this.id);
const cred = getConnectorCredential(vault, this.id);
this.token = cred?.value ?? null;
}

View File

@@ -5,6 +5,7 @@
*/
import { BaseConnector, type ConnectorAction, type ConnectorResult } from '../connector-sdk.js';
import { getConnectorCredential } from './connector-credentials.js';
import type { VaultStore } from '@waggle/core';
import type { ConnectorHealth } from '@waggle/shared';
@@ -72,7 +73,7 @@ export class EmailConnector extends BaseConnector {
private maxDailyEmails = 100;
async connect(vault: VaultStore): Promise<void> {
const cred = vault.getConnectorCredential(this.id);
const cred = getConnectorCredential(vault, this.id);
this.apiKey = cred?.value ?? null;
// Optional from_email/from_name config

View File

@@ -4,6 +4,7 @@
*/
import { BaseConnector, type ConnectorAction, type ConnectorResult } from '../connector-sdk.js';
import { getConnectorCredential, setConnectorCredential } from './connector-credentials.js';
import type { VaultStore } from '@waggle/core';
import type { ConnectorHealth } from '@waggle/shared';
@@ -97,7 +98,7 @@ export class GoogleCalendarConnector extends BaseConnector {
async connect(vault: VaultStore): Promise<void> {
this.credentialGeneration += 1;
this.vault = vault;
const cred = vault.getConnectorCredential(this.id);
const cred = getConnectorCredential(vault, this.id);
this.accessToken = cred?.value ?? null;
this.refreshToken = cred?.refreshToken ?? null;
this.expiresAt = cred?.expiresAt ?? null;
@@ -190,7 +191,7 @@ export class GoogleCalendarConnector extends BaseConnector {
if (!res.ok) throw new Error(`Token refresh failed: ${res.status}`);
const data = await res.json() as { access_token: string; expires_in: number; refresh_token?: string };
const currentCredential = vault?.getConnectorCredential(this.id);
const currentCredential = vault ? getConnectorCredential(vault, this.id) : null;
if (
this.credentialGeneration !== credentialGeneration
|| !currentCredential
@@ -206,7 +207,7 @@ export class GoogleCalendarConnector extends BaseConnector {
// Persist updated tokens back to vault
if (vault) {
vault.setConnectorCredential(this.id, {
setConnectorCredential(vault, this.id, {
type: 'oauth2',
value: this.accessToken,
refreshToken: this.refreshToken ?? undefined,

View File

@@ -4,6 +4,7 @@
*/
import { BaseConnector, type ConnectorAction, type ConnectorResult } from '../connector-sdk.js';
import { getConnectorCredential } from './connector-credentials.js';
import type { VaultStore } from '@waggle/core';
import type { ConnectorHealth } from '@waggle/shared';
@@ -74,7 +75,7 @@ export class GoogleDocsConnector extends BaseConnector {
private token: string | null = null;
async connect(vault: VaultStore): Promise<void> {
const cred = vault.getConnectorCredential(this.id);
const cred = getConnectorCredential(vault, this.id);
this.token = cred?.value ?? null;
}

View File

@@ -4,6 +4,7 @@
*/
import { BaseConnector, type ConnectorAction, type ConnectorResult } from '../connector-sdk.js';
import { getConnectorCredential } from './connector-credentials.js';
import type { VaultStore } from '@waggle/core';
import type { ConnectorHealth } from '@waggle/shared';
@@ -102,7 +103,7 @@ export class GoogleDriveConnector extends BaseConnector {
private token: string | null = null;
async connect(vault: VaultStore): Promise<void> {
const cred = vault.getConnectorCredential(this.id);
const cred = getConnectorCredential(vault, this.id);
this.token = cred?.value ?? null;
}

View File

@@ -4,6 +4,7 @@
*/
import { BaseConnector, type ConnectorAction, type ConnectorResult } from '../connector-sdk.js';
import { getConnectorCredential } from './connector-credentials.js';
import type { VaultStore } from '@waggle/core';
import type { ConnectorHealth } from '@waggle/shared';
@@ -126,7 +127,7 @@ export class GitHubConnector extends BaseConnector {
private baseUrl = API_BASE;
async connect(vault: VaultStore): Promise<void> {
const cred = vault.getConnectorCredential(this.id);
const cred = getConnectorCredential(vault, this.id);
this.token = cred?.value ?? null;
// Support GitHub Enterprise via connector config
const configEntry = vault.get(`connector:${this.id}:base_url`);

View File

@@ -5,6 +5,7 @@
*/
import { BaseConnector, type ConnectorAction, type ConnectorResult } from '../connector-sdk.js';
import { getConnectorCredential } from './connector-credentials.js';
import type { VaultStore } from '@waggle/core';
import type { ConnectorHealth } from '@waggle/shared';
@@ -108,7 +109,7 @@ export class GitLabConnector extends BaseConnector {
private baseUrl = DEFAULT_API_BASE;
async connect(vault: VaultStore): Promise<void> {
const cred = vault.getConnectorCredential(this.id);
const cred = getConnectorCredential(vault, this.id);
this.token = cred?.value ?? null;
// Support self-hosted GitLab via connector config

View File

@@ -4,6 +4,7 @@
*/
import { BaseConnector, type ConnectorAction, type ConnectorResult } from '../connector-sdk.js';
import { getConnectorCredential } from './connector-credentials.js';
import type { VaultStore } from '@waggle/core';
import type { ConnectorHealth } from '@waggle/shared';
@@ -85,7 +86,7 @@ export class GmailConnector extends BaseConnector {
private token: string | null = null;
async connect(vault: VaultStore): Promise<void> {
const cred = vault.getConnectorCredential(this.id);
const cred = getConnectorCredential(vault, this.id);
this.token = cred?.value ?? null;
}

View File

@@ -4,6 +4,7 @@
*/
import { BaseConnector, type ConnectorAction, type ConnectorResult } from '../connector-sdk.js';
import { getConnectorCredential } from './connector-credentials.js';
import type { VaultStore } from '@waggle/core';
import type { ConnectorHealth } from '@waggle/shared';
@@ -90,7 +91,7 @@ export class GoogleSheetsConnector extends BaseConnector {
private token: string | null = null;
async connect(vault: VaultStore): Promise<void> {
const cred = vault.getConnectorCredential(this.id);
const cred = getConnectorCredential(vault, this.id);
this.token = cred?.value ?? null;
}

View File

@@ -4,6 +4,7 @@
*/
import { BaseConnector, type ConnectorAction, type ConnectorResult } from '../connector-sdk.js';
import { getConnectorCredential } from './connector-credentials.js';
import type { VaultStore } from '@waggle/core';
import type { ConnectorHealth } from '@waggle/shared';
@@ -116,7 +117,7 @@ export class HubSpotConnector extends BaseConnector {
private token: string | null = null;
async connect(vault: VaultStore): Promise<void> {
const cred = vault.getConnectorCredential(this.id);
const cred = getConnectorCredential(vault, this.id);
this.token = cred?.value ?? null;
}

View File

@@ -4,6 +4,7 @@
*/
import { BaseConnector, type ConnectorAction, type ConnectorResult } from '../connector-sdk.js';
import { getConnectorCredential } from './connector-credentials.js';
import type { VaultStore } from '@waggle/core';
import type { ConnectorDefinition, ConnectorHealth, ConnectorStatus } from '@waggle/shared';
@@ -131,7 +132,7 @@ export class JiraConnector extends BaseConnector {
}
async connect(vault: VaultStore): Promise<void> {
const cred = vault.getConnectorCredential(this.id);
const cred = getConnectorCredential(vault, this.id);
if (!cred) {
this.authHeader = null;
this.baseUrl = null;

View File

@@ -4,6 +4,7 @@
*/
import { BaseConnector, type ConnectorAction, type ConnectorResult } from '../connector-sdk.js';
import { getConnectorCredential } from './connector-credentials.js';
import type { VaultStore } from '@waggle/core';
import type { ConnectorHealth } from '@waggle/shared';
@@ -102,7 +103,7 @@ export class LinearConnector extends BaseConnector {
private token: string | null = null;
async connect(vault: VaultStore): Promise<void> {
const cred = vault.getConnectorCredential(this.id);
const cred = getConnectorCredential(vault, this.id);
this.token = cred?.value ?? null;
}

View File

@@ -4,6 +4,7 @@
*/
import { BaseConnector, type ConnectorAction, type ConnectorResult } from '../connector-sdk.js';
import { getConnectorCredential } from './connector-credentials.js';
import type { VaultStore } from '@waggle/core';
import type { ConnectorHealth } from '@waggle/shared';
@@ -91,7 +92,7 @@ export class MondayConnector extends BaseConnector {
private token: string | null = null;
async connect(vault: VaultStore): Promise<void> {
const cred = vault.getConnectorCredential(this.id);
const cred = getConnectorCredential(vault, this.id);
this.token = cred?.value ?? null;
}

View File

@@ -4,6 +4,7 @@
*/
import { BaseConnector, type ConnectorAction, type ConnectorResult } from '../connector-sdk.js';
import { getConnectorCredential } from './connector-credentials.js';
import type { VaultStore } from '@waggle/core';
import type { ConnectorHealth } from '@waggle/shared';
@@ -95,7 +96,7 @@ export class MSTeamsConnector extends BaseConnector {
private token: string | null = null;
async connect(vault: VaultStore): Promise<void> {
const cred = vault.getConnectorCredential(this.id);
const cred = getConnectorCredential(vault, this.id);
this.token = cred?.value ?? null;
}

View File

@@ -4,6 +4,7 @@
*/
import { BaseConnector, type ConnectorAction, type ConnectorResult } from '../connector-sdk.js';
import { getConnectorCredential } from './connector-credentials.js';
import type { VaultStore } from '@waggle/core';
import type { ConnectorHealth } from '@waggle/shared';
@@ -114,7 +115,7 @@ export class NotionConnector extends BaseConnector {
private token: string | null = null;
async connect(vault: VaultStore): Promise<void> {
const cred = vault.getConnectorCredential(this.id);
const cred = getConnectorCredential(vault, this.id);
this.token = cred?.value ?? null;
}

View File

@@ -9,6 +9,7 @@
import fs from 'node:fs';
import path from 'node:path';
import { BaseConnector, type ConnectorAction, type ConnectorResult } from '../connector-sdk.js';
import { getConnectorCredential } from './connector-credentials.js';
import type { VaultStore } from '@waggle/core';
import type { ConnectorHealth } from '@waggle/shared';
@@ -105,7 +106,7 @@ export class ObsidianConnector extends BaseConnector {
private vaultPath: string | null = null;
async connect(vault: VaultStore): Promise<void> {
const cred = vault.getConnectorCredential(this.id);
const cred = getConnectorCredential(vault, this.id);
this.vaultPath = cred?.value ?? null;
}

View File

@@ -4,6 +4,7 @@
*/
import { BaseConnector, type ConnectorAction, type ConnectorResult } from '../connector-sdk.js';
import { getConnectorCredential } from './connector-credentials.js';
import type { VaultStore } from '@waggle/core';
import type { ConnectorHealth } from '@waggle/shared';
@@ -83,7 +84,7 @@ export class OneDriveConnector extends BaseConnector {
private token: string | null = null;
async connect(vault: VaultStore): Promise<void> {
const cred = vault.getConnectorCredential(this.id);
const cred = getConnectorCredential(vault, this.id);
this.token = cred?.value ?? null;
}

View File

@@ -12,6 +12,7 @@
*/
import { BaseConnector, type ConnectorAction, type ConnectorResult } from '../connector-sdk.js';
import { getConnectorCredential } from './connector-credentials.js';
import type { VaultStore } from '@waggle/core';
import type { ConnectorHealth } from '@waggle/shared';
@@ -125,7 +126,7 @@ export class OneNoteConnector extends BaseConnector {
private token: string | null = null;
async connect(vault: VaultStore): Promise<void> {
const cred = vault.getConnectorCredential(this.id);
const cred = getConnectorCredential(vault, this.id);
this.token = cred?.value ?? null;
}

View File

@@ -4,6 +4,7 @@
*/
import { BaseConnector, type ConnectorAction, type ConnectorResult } from '../connector-sdk.js';
import { getConnectorCredential } from './connector-credentials.js';
import type { VaultStore } from '@waggle/core';
import type { ConnectorHealth } from '@waggle/shared';
@@ -117,7 +118,7 @@ export class OutlookConnector extends BaseConnector {
private token: string | null = null;
async connect(vault: VaultStore): Promise<void> {
const cred = vault.getConnectorCredential(this.id);
const cred = getConnectorCredential(vault, this.id);
this.token = cred?.value ?? null;
}

View File

@@ -4,6 +4,7 @@
*/
import { BaseConnector, type ConnectorAction, type ConnectorResult } from '../connector-sdk.js';
import { getConnectorCredential } from './connector-credentials.js';
import type { VaultStore } from '@waggle/core';
import type { ConnectorHealth } from '@waggle/shared';
@@ -118,7 +119,7 @@ export class PipedriveConnector extends BaseConnector {
private apiToken: string | null = null;
async connect(vault: VaultStore): Promise<void> {
const cred = vault.getConnectorCredential(this.id);
const cred = getConnectorCredential(vault, this.id);
this.apiToken = cred?.value ?? null;
}

View File

@@ -5,6 +5,7 @@
*/
import { BaseConnector, type ConnectorAction, type ConnectorResult } from '../connector-sdk.js';
import { getConnectorCredential } from './connector-credentials.js';
import type { VaultStore } from '@waggle/core';
import type { ConnectorHealth } from '@waggle/shared';
@@ -90,7 +91,7 @@ export class PostgresConnector extends BaseConnector {
private client: PgClient | null = null;
async connect(vault: VaultStore): Promise<void> {
const cred = vault.getConnectorCredential(this.id);
const cred = getConnectorCredential(vault, this.id);
this.connectionString = cred?.value ?? null;
// Try to dynamically import pg

View File

@@ -5,6 +5,7 @@
*/
import { BaseConnector, type ConnectorAction, type ConnectorResult } from '../connector-sdk.js';
import { getConnectorCredential } from './connector-credentials.js';
import { safeFetch } from '../url-egress-guard.js';
import type { VaultStore } from '@waggle/core';
import type { ConnectorDefinition, ConnectorHealth, ConnectorStatus } from '@waggle/shared';
@@ -214,7 +215,7 @@ export class SalesforceConnector extends BaseConnector {
}
async connect(vault: VaultStore): Promise<void> {
const cred = vault.getConnectorCredential(this.id);
const cred = getConnectorCredential(vault, this.id);
this.token = cred?.value ?? null;
// Instance URL from vault metadata (e.g., "https://mycompany.salesforce.com")

View File

@@ -4,6 +4,7 @@
*/
import { BaseConnector, type ConnectorAction, type ConnectorResult } from '../connector-sdk.js';
import { getConnectorCredential } from './connector-credentials.js';
import type { VaultStore } from '@waggle/core';
import type { ConnectorHealth } from '@waggle/shared';
@@ -73,7 +74,7 @@ export class SlackConnector extends BaseConnector {
private token: string | null = null;
async connect(vault: VaultStore): Promise<void> {
const cred = vault.getConnectorCredential(this.id);
const cred = getConnectorCredential(vault, this.id);
this.token = cred?.value ?? null;
}

View File

@@ -4,6 +4,7 @@
*/
import { BaseConnector, type ConnectorAction, type ConnectorResult } from '../connector-sdk.js';
import { getConnectorCredential } from './connector-credentials.js';
import type { VaultStore } from '@waggle/core';
import type { ConnectorHealth } from '@waggle/shared';
@@ -109,7 +110,7 @@ export class TrelloConnector extends BaseConnector {
private apiToken: string | null = null;
async connect(vault: VaultStore): Promise<void> {
const cred = vault.getConnectorCredential(this.id);
const cred = getConnectorCredential(vault, this.id);
this.apiToken = cred?.value ?? null;
// API key stored as a separate vault entry
const keyEntry = vault.get(`connector:${this.id}:api_key`);

View File

@@ -404,6 +404,10 @@ export {
} from './skill-watcher.js';
export { BaseConnector, type WaggleConnector, type ConnectorAction, type ConnectorResult } from './connector-sdk.js';
export { ConnectorRegistry, type AuditLogger } from './connector-registry.js';
export {
getConnectorCredential, setConnectorCredential, setConnectorCredentialBundle,
type ConnectorCredentialInput, type StoredConnectorCredential,
} from './connectors/connector-credentials.js';
export {
GitHubConnector, SlackConnector, JiraConnector, EmailConnector, GoogleCalendarConnector,
DiscordConnector, LinearConnector, AsanaConnector, TrelloConnector, MondayConnector,

View File

@@ -2,8 +2,10 @@ import { describe, it, expect, vi, beforeEach } from 'vitest';
import { BaseConnector, type ConnectorAction, type ConnectorResult, type WaggleConnector } from '../src/connector-sdk.js';
import { ConnectorRegistry, type AuditLogger } from '../src/connector-registry.js';
import { needsConfirmationWithAutonomy } from '../src/confirmation.js';
import type { VaultStore } from '@waggle/core';
import { getConnectorCredential } from '../src/connectors/connector-credentials.js';
import type { SecretStore } from '@waggle/core';
import type { ConnectorHealth, ConnectorStatus } from '@waggle/shared';
import { createMemorySecretStore, seedCredential } from './helpers/memory-secret-store.js';
// ─── Mock Connector ──────────────────────────────────────────────────────
@@ -40,9 +42,9 @@ class MockConnector extends BaseConnector {
healthCheckCalled = false;
executeCalls: Array<{ action: string; params: Record<string, unknown> }> = [];
async connect(vault: VaultStore): Promise<void> {
async connect(vault: SecretStore): Promise<void> {
this.connectCalled = true;
const cred = vault.getConnectorCredential(this.id);
const cred = getConnectorCredential(vault, this.id);
this.token = cred?.value ?? null;
}
@@ -66,21 +68,12 @@ class MockConnector extends BaseConnector {
// ─── Mock Vault ──────────────────────────────────────────────────────────
function createMockVault(credentials: Record<string, { value: string; isExpired: boolean }> = {}): VaultStore {
return {
getConnectorCredential: vi.fn((id: string) => {
const cred = credentials[id];
if (!cred) return null;
return { value: cred.value, type: 'bearer', isExpired: cred.isExpired };
}),
setConnectorCredential: vi.fn(),
set: vi.fn(),
get: vi.fn(),
delete: vi.fn(),
list: vi.fn(() => []),
has: vi.fn(() => false),
migrateFromConfig: vi.fn(() => 0),
} as unknown as VaultStore;
function createMockVault(credentials: Record<string, { value: string; isExpired: boolean }> = {}): SecretStore {
return createMemorySecretStore({
connectors: Object.fromEntries(
Object.entries(credentials).map(([id, cred]) => [id, seedCredential(cred)]),
),
});
}
async function registerAndHydrate(
@@ -140,7 +133,7 @@ describe('WaggleConnector interface', () => {
// ─── ConnectorRegistry ───────────────────────────────────────────────────
describe('ConnectorRegistry', () => {
let vault: VaultStore;
let vault: SecretStore;
let registry: ConnectorRegistry;
beforeEach(() => {

View File

@@ -1,22 +1,5 @@
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
import { DiscordConnector } from '../src/connectors/discord-connector.js';
import type { VaultStore } from '@waggle/core';
function createMockVault(connectorId: string, cred?: { value: string; isExpired: boolean }): VaultStore {
return {
getConnectorCredential: vi.fn((id: string) => {
if (id === connectorId && cred) return { ...cred, type: 'bearer' };
return null;
}),
get: vi.fn(() => null),
set: vi.fn(),
delete: vi.fn(),
list: vi.fn(() => []),
has: vi.fn(() => false),
setConnectorCredential: vi.fn(),
migrateFromConfig: vi.fn(() => 0),
} as unknown as VaultStore;
}
describe('Discord connector (communication)', () => {
let connector: DiscordConnector;

View File

@@ -0,0 +1,186 @@
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']);
});
});

View File

@@ -1,21 +1,9 @@
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
import { ComposioConnector } from '../../src/connectors/composio-connector.js';
import type { VaultStore } from '@waggle/core';
import { createMemorySecretStore, seedCredential, type MockSecretStore } from '../helpers/memory-secret-store.js';
function createMockVault(cred?: { value: string; isExpired: boolean }): VaultStore {
return {
getConnectorCredential: vi.fn((id: string) => {
if (id === 'composio' && cred) return { ...cred, type: 'api_key' };
return null;
}),
get: vi.fn(() => null),
set: vi.fn(),
delete: vi.fn(),
list: vi.fn(() => []),
has: vi.fn(() => false),
setConnectorCredential: vi.fn(),
migrateFromConfig: vi.fn(() => 0),
} as unknown as VaultStore;
function createMockVault(cred?: { value: string; isExpired: boolean }): MockSecretStore {
return createMemorySecretStore({ connectors: cred ? { composio: seedCredential(cred, 'api_key') } : {} });
}
describe('ComposioConnector', () => {
@@ -66,7 +54,7 @@ describe('ComposioConnector', () => {
it('connect() retrieves API key from vault', async () => {
const vault = createMockVault({ value: 'cmp_test_key', isExpired: false });
await connector.connect(vault);
expect(vault.getConnectorCredential).toHaveBeenCalledWith('composio');
expect(vault.get).toHaveBeenCalledWith('connector:composio');
});
it('execute returns error when not connected', async () => {

View File

@@ -8,7 +8,7 @@ import { BitbucketConnector } from '../../src/connectors/bitbucket-connector.js'
import { DropboxConnector } from '../../src/connectors/dropbox-connector.js';
import { PostgresConnector } from '../../src/connectors/postgres-connector.js';
import { safeFetch } from '../../src/url-egress-guard.js';
import type { VaultStore } from '@waggle/core';
import { createMemorySecretStore, seedCredential, type MockSecretStore } from '../helpers/memory-secret-store.js';
vi.mock('../../src/url-egress-guard.js', () => ({
safeFetch: vi.fn((url: string, init?: RequestInit) => globalThis.fetch(url, {
@@ -17,23 +17,11 @@ vi.mock('../../src/url-egress-guard.js', () => ({
})),
}));
function createMockVault(connectorId: string, cred?: { value: string; isExpired: boolean }, extras?: Record<string, string>): VaultStore {
return {
getConnectorCredential: vi.fn((id: string) => {
if (id === connectorId && cred) return { ...cred, type: 'bearer' };
return null;
}),
get: vi.fn((key: string) => {
if (extras && extras[key]) return { value: extras[key] };
return null;
}),
set: vi.fn(),
delete: vi.fn(),
list: vi.fn(() => []),
has: vi.fn(() => false),
setConnectorCredential: vi.fn(),
migrateFromConfig: vi.fn(() => 0),
} as unknown as VaultStore;
function createMockVault(connectorId: string, cred?: { value: string; isExpired: boolean }, extras?: Record<string, string>): MockSecretStore {
return createMemorySecretStore({
connectors: cred ? { [connectorId]: seedCredential(cred) } : {},
secrets: extras,
});
}
// ── CRM Connectors ──────────────────────────────────────────

View File

@@ -3,22 +3,10 @@ import { GmailConnector } from '../../src/connectors/gmail-connector.js';
import { GoogleDocsConnector } from '../../src/connectors/gdocs-connector.js';
import { GoogleDriveConnector } from '../../src/connectors/gdrive-connector.js';
import { GoogleSheetsConnector } from '../../src/connectors/gsheets-connector.js';
import type { VaultStore } from '@waggle/core';
import { createMemorySecretStore, seedCredential, type MockSecretStore } from '../helpers/memory-secret-store.js';
function createMockVault(connectorId: string, cred?: { value: string; isExpired: boolean }): VaultStore {
return {
getConnectorCredential: vi.fn((id: string) => {
if (id === connectorId && cred) return { ...cred, type: 'bearer' };
return null;
}),
get: vi.fn(() => null),
set: vi.fn(),
delete: vi.fn(),
list: vi.fn(() => []),
has: vi.fn(() => false),
setConnectorCredential: vi.fn(),
migrateFromConfig: vi.fn(() => 0),
} as unknown as VaultStore;
function createMockVault(connectorId: string, cred?: { value: string; isExpired: boolean }): MockSecretStore {
return createMemorySecretStore({ connectors: cred ? { [connectorId]: seedCredential(cred) } : {} });
}
// ─── Gmail Connector ───
@@ -91,7 +79,7 @@ describe('GmailConnector', () => {
it('connect retrieves token from vault', async () => {
const vault = createMockVault('gmail', { value: 'ya29.test_token', isExpired: false });
await connector.connect(vault);
expect(vault.getConnectorCredential).toHaveBeenCalledWith('gmail');
expect(vault.get).toHaveBeenCalledWith('connector:gmail');
});
it('healthCheck returns connected when API responds OK', async () => {
@@ -201,7 +189,7 @@ describe('GoogleDocsConnector', () => {
it('connect retrieves token from vault', async () => {
const vault = createMockVault('gdocs', { value: 'ya29.test_token', isExpired: false });
await connector.connect(vault);
expect(vault.getConnectorCredential).toHaveBeenCalledWith('gdocs');
expect(vault.get).toHaveBeenCalledWith('connector:gdocs');
});
it('healthCheck returns connected when API responds OK', async () => {
@@ -317,7 +305,7 @@ describe('GoogleDriveConnector', () => {
it('connect retrieves token from vault', async () => {
const vault = createMockVault('gdrive', { value: 'ya29.test_token', isExpired: false });
await connector.connect(vault);
expect(vault.getConnectorCredential).toHaveBeenCalledWith('gdrive');
expect(vault.get).toHaveBeenCalledWith('connector:gdrive');
});
it('healthCheck returns connected when API responds OK', async () => {
@@ -431,7 +419,7 @@ describe('GoogleSheetsConnector', () => {
it('connect retrieves token from vault', async () => {
const vault = createMockVault('gsheets', { value: 'ya29.test_token', isExpired: false });
await connector.connect(vault);
expect(vault.getConnectorCredential).toHaveBeenCalledWith('gsheets');
expect(vault.get).toHaveBeenCalledWith('connector:gsheets');
});
it('healthCheck returns connected when API responds OK', async () => {

View File

@@ -5,7 +5,7 @@ import path from 'node:path';
import { NotionConnector } from '../../src/connectors/notion-connector.js';
import { ConfluenceConnector } from '../../src/connectors/confluence-connector.js';
import { ObsidianConnector } from '../../src/connectors/obsidian-connector.js';
import type { VaultStore } from '@waggle/core';
import { createMemorySecretStore, seedCredential, type MockSecretStore } from '../helpers/memory-secret-store.js';
/** Shape of ConnectorResult.data fields asserted by these knowledge-connector tests. */
type KnowledgeData = {
@@ -21,20 +21,11 @@ function createMockVault(
connectorId: string,
cred?: { value: string; isExpired: boolean },
extras?: Record<string, { value: string }>,
): VaultStore {
return {
getConnectorCredential: vi.fn((id: string) => {
if (id === connectorId && cred) return { ...cred, type: 'api_key' };
return null;
}),
get: vi.fn((key: string) => extras?.[key] ?? null),
set: vi.fn(),
delete: vi.fn(),
list: vi.fn(() => []),
has: vi.fn(() => false),
setConnectorCredential: vi.fn(),
migrateFromConfig: vi.fn(() => 0),
} as unknown as VaultStore;
): MockSecretStore {
return createMemorySecretStore({
connectors: cred ? { [connectorId]: seedCredential(cred, 'api_key') } : {},
secrets: Object.fromEntries(Object.entries(extras ?? {}).map(([name, entry]) => [name, entry.value])),
});
}
// ── Notion Connector ──────────────────────────────────────────────────
@@ -85,7 +76,7 @@ describe('NotionConnector', () => {
it('connect() retrieves token from vault', async () => {
const vault = createMockVault('notion', { value: 'ntn_test123', isExpired: false });
await connector.connect(vault);
expect(vault.getConnectorCredential).toHaveBeenCalledWith('notion');
expect(vault.get).toHaveBeenCalledWith('connector:notion');
});
it('healthCheck() returns connected when API responds OK', async () => {
@@ -193,7 +184,7 @@ describe('ConfluenceConnector', () => {
'connector:confluence:domain': { value: 'mycompany' },
});
await connector.connect(vault);
expect(vault.getConnectorCredential).toHaveBeenCalledWith('confluence');
expect(vault.get).toHaveBeenCalledWith('connector:confluence');
expect(vault.get).toHaveBeenCalledWith('connector:confluence:email');
expect(vault.get).toHaveBeenCalledWith('connector:confluence:domain');
});
@@ -314,7 +305,7 @@ describe('ObsidianConnector', () => {
it('connect() retrieves vault path from vault', async () => {
const vault = createMockVault('obsidian', { value: tmpDir, isExpired: false });
await connector.connect(vault);
expect(vault.getConnectorCredential).toHaveBeenCalledWith('obsidian');
expect(vault.get).toHaveBeenCalledWith('connector:obsidian');
});
it('healthCheck() returns connected when directory exists', async () => {

View File

@@ -3,25 +3,13 @@ import { MSTeamsConnector } from '../../src/connectors/ms-teams-connector.js';
import { OutlookConnector } from '../../src/connectors/outlook-connector.js';
import { OneDriveConnector } from '../../src/connectors/onedrive-connector.js';
import { OneNoteConnector } from '../../src/connectors/onenote-connector.js';
import type { VaultStore } from '@waggle/core';
import { createMemorySecretStore, seedCredential, type MockSecretStore } from '../helpers/memory-secret-store.js';
function createMockVault(
connectorId: string,
cred?: { value: string; isExpired: boolean },
): VaultStore {
return {
getConnectorCredential: vi.fn((id: string) => {
if (id === connectorId && cred) return { ...cred, type: 'bearer' };
return null;
}),
get: vi.fn(() => null),
set: vi.fn(),
delete: vi.fn(),
list: vi.fn(() => []),
has: vi.fn(() => false),
setConnectorCredential: vi.fn(),
migrateFromConfig: vi.fn(() => 0),
} as unknown as VaultStore;
): MockSecretStore {
return createMemorySecretStore({ connectors: cred ? { [connectorId]: seedCredential(cred) } : {} });
}
// ── Microsoft Teams Connector ─────────────────────────────────────────
@@ -79,7 +67,7 @@ describe('MSTeamsConnector', () => {
it('connect() retrieves token from vault', async () => {
const vault = createMockVault('ms-teams', { value: 'graph-token-123', isExpired: false });
await connector.connect(vault);
expect(vault.getConnectorCredential).toHaveBeenCalledWith('ms-teams');
expect(vault.get).toHaveBeenCalledWith('connector:ms-teams');
});
it('healthCheck() returns connected when Graph API responds OK', async () => {
@@ -209,7 +197,7 @@ describe('OutlookConnector', () => {
it('connect() retrieves token from vault', async () => {
const vault = createMockVault('outlook', { value: 'graph-token-456', isExpired: false });
await connector.connect(vault);
expect(vault.getConnectorCredential).toHaveBeenCalledWith('outlook');
expect(vault.get).toHaveBeenCalledWith('connector:outlook');
});
it('healthCheck() returns connected when Graph API responds OK', async () => {
@@ -368,7 +356,7 @@ describe('OneDriveConnector', () => {
it('connect() retrieves token from vault', async () => {
const vault = createMockVault('onedrive', { value: 'graph-token-789', isExpired: false });
await connector.connect(vault);
expect(vault.getConnectorCredential).toHaveBeenCalledWith('onedrive');
expect(vault.get).toHaveBeenCalledWith('connector:onedrive');
});
it('healthCheck() returns connected when Graph API responds OK', async () => {
@@ -562,7 +550,7 @@ describe('OneNoteConnector', () => {
it('connect() retrieves token from vault', async () => {
const vault = createMockVault('onenote', { value: 'graph-token-onenote', isExpired: false });
await connector.connect(vault);
expect(vault.getConnectorCredential).toHaveBeenCalledWith('onenote');
expect(vault.get).toHaveBeenCalledWith('connector:onenote');
});
it('healthCheck() probes /me/onenote/notebooks to exercise the Notes.Read scope', async () => {

View File

@@ -3,25 +3,13 @@ import { LinearConnector } from '../../src/connectors/linear-connector.js';
import { AsanaConnector } from '../../src/connectors/asana-connector.js';
import { TrelloConnector } from '../../src/connectors/trello-connector.js';
import { MondayConnector } from '../../src/connectors/monday-connector.js';
import type { VaultStore } from '@waggle/core';
import { createMemorySecretStore, seedCredential, type MockSecretStore } from '../helpers/memory-secret-store.js';
function createMockVault(connectorId: string, cred?: { value: string; isExpired: boolean }, extras?: Record<string, string>): VaultStore {
return {
getConnectorCredential: vi.fn((id: string) => {
if (id === connectorId && cred) return { ...cred, type: 'bearer' };
return null;
}),
get: vi.fn((key: string) => {
if (extras && extras[key]) return { value: extras[key] };
return null;
}),
set: vi.fn(),
delete: vi.fn(),
list: vi.fn(() => []),
has: vi.fn(() => false),
setConnectorCredential: vi.fn(),
migrateFromConfig: vi.fn(() => 0),
} as unknown as VaultStore;
function createMockVault(connectorId: string, cred?: { value: string; isExpired: boolean }, extras?: Record<string, string>): MockSecretStore {
return createMemorySecretStore({
connectors: cred ? { [connectorId]: seedCredential(cred) } : {},
secrets: extras,
});
}
// ─── Linear Connector ───
@@ -81,7 +69,7 @@ describe('LinearConnector', () => {
it('connect retrieves token from vault', async () => {
const vault = createMockVault('linear', { value: 'lin_api_test123', isExpired: false });
await connector.connect(vault);
expect(vault.getConnectorCredential).toHaveBeenCalledWith('linear');
expect(vault.get).toHaveBeenCalledWith('connector:linear');
});
it('healthCheck returns connected when API responds OK', async () => {
@@ -239,7 +227,7 @@ describe('AsanaConnector', () => {
it('connect retrieves token from vault', async () => {
const vault = createMockVault('asana', { value: 'asana_pat_test123', isExpired: false });
await connector.connect(vault);
expect(vault.getConnectorCredential).toHaveBeenCalledWith('asana');
expect(vault.get).toHaveBeenCalledWith('connector:asana');
});
it('healthCheck returns connected when API responds OK', async () => {
@@ -341,7 +329,7 @@ describe('TrelloConnector', () => {
'connector:trello:api_key': 'trello_key_test',
});
await connector.connect(vault);
expect(vault.getConnectorCredential).toHaveBeenCalledWith('trello');
expect(vault.get).toHaveBeenCalledWith('connector:trello');
expect(vault.get).toHaveBeenCalledWith('connector:trello:api_key');
});
@@ -453,7 +441,7 @@ describe('MondayConnector', () => {
it('connect retrieves token from vault', async () => {
const vault = createMockVault('monday', { value: 'monday_api_test123', isExpired: false });
await connector.connect(vault);
expect(vault.getConnectorCredential).toHaveBeenCalledWith('monday');
expect(vault.get).toHaveBeenCalledWith('connector:monday');
});
it('healthCheck returns connected when API responds OK', async () => {

View File

@@ -1,21 +1,9 @@
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
import { DiscordConnector } from '../../src/connectors/discord-connector.js';
import type { VaultStore } from '@waggle/core';
import { createMemorySecretStore, seedCredential, type MockSecretStore } from '../helpers/memory-secret-store.js';
function createMockVault(cred?: { value: string; isExpired: boolean }): VaultStore {
return {
getConnectorCredential: vi.fn((id: string) => {
if (id === 'discord' && cred) return { ...cred, type: 'bearer' };
return null;
}),
get: vi.fn(() => null),
set: vi.fn(),
delete: vi.fn(),
list: vi.fn(() => []),
has: vi.fn(() => false),
setConnectorCredential: vi.fn(),
migrateFromConfig: vi.fn(() => 0),
} as unknown as VaultStore;
function createMockVault(cred?: { value: string; isExpired: boolean }): MockSecretStore {
return createMemorySecretStore({ connectors: cred ? { discord: seedCredential(cred) } : {} });
}
describe('DiscordConnector', () => {
@@ -50,7 +38,7 @@ describe('DiscordConnector', () => {
it('connect() retrieves token from vault', async () => {
const vault = createMockVault({ value: 'bot-test-token', isExpired: false });
await connector.connect(vault);
expect(vault.getConnectorCredential).toHaveBeenCalledWith('discord');
expect(vault.get).toHaveBeenCalledWith('connector:discord');
});
it('execute() returns error when not connected', async () => {

View File

@@ -1,21 +1,9 @@
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
import { EmailConnector } from '../../src/connectors/email-connector.js';
import type { VaultStore } from '@waggle/core';
import { createMemorySecretStore, type MockSecretStore } from '../helpers/memory-secret-store.js';
function createMockVault(cred?: { value: string }): VaultStore {
return {
getConnectorCredential: vi.fn((id: string) => {
if (id === 'email' && cred) return { ...cred, type: 'api_key', isExpired: false };
return null;
}),
get: vi.fn(() => null),
set: vi.fn(),
delete: vi.fn(),
list: vi.fn(() => []),
has: vi.fn(() => false),
setConnectorCredential: vi.fn(),
migrateFromConfig: vi.fn(() => 0),
} as unknown as VaultStore;
function createMockVault(cred?: { value: string }): MockSecretStore {
return createMemorySecretStore({ connectors: cred ? { email: { type: 'api_key', value: cred.value } } : {} });
}
describe('EmailConnector', () => {
@@ -41,7 +29,7 @@ describe('EmailConnector', () => {
it('connect() retrieves API key from vault', async () => {
const vault = createMockVault({ value: 'SG.test_key' });
await connector.connect(vault);
expect(vault.getConnectorCredential).toHaveBeenCalledWith('email');
expect(vault.get).toHaveBeenCalledWith('connector:email');
});
it('healthCheck() validates API key', async () => {

View File

@@ -1,6 +1,7 @@
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
import { GoogleCalendarConnector } from '../../src/connectors/gcal-connector.js';
import type { VaultStore } from '@waggle/core';
import { getConnectorCredential } from '../../src/connectors/connector-credentials.js';
import { createMemorySecretStore, type MockSecretStore } from '../helpers/memory-secret-store.js';
function createMockVault(opts?: {
accessToken?: string;
@@ -8,32 +9,23 @@ function createMockVault(opts?: {
expiresAt?: string;
clientId?: string;
clientSecret?: string;
}): VaultStore {
return {
getConnectorCredential: vi.fn((id: string) => {
if (id === 'gcal' && opts?.accessToken) {
return {
value: opts.accessToken,
}): MockSecretStore {
const secrets: Record<string, string> = {};
if (opts?.clientId) secrets['connector:gcal:client_id'] = opts.clientId;
if (opts?.clientSecret) secrets['connector:gcal:client_secret'] = opts.clientSecret;
return createMemorySecretStore({
connectors: opts?.accessToken
? {
gcal: {
type: 'oauth2',
isExpired: false,
value: opts.accessToken,
refreshToken: opts.refreshToken,
expiresAt: opts.expiresAt,
};
},
}
return null;
}),
get: vi.fn((name: string) => {
if (name === 'connector:gcal:client_id' && opts?.clientId) return { value: opts.clientId };
if (name === 'connector:gcal:client_secret' && opts?.clientSecret) return { value: opts.clientSecret };
return null;
}),
set: vi.fn(),
delete: vi.fn(),
list: vi.fn(() => []),
has: vi.fn(() => false),
setConnectorCredential: vi.fn(),
migrateFromConfig: vi.fn(() => 0),
} as unknown as VaultStore;
: {},
secrets,
});
}
describe('GoogleCalendarConnector', () => {
@@ -63,7 +55,7 @@ describe('GoogleCalendarConnector', () => {
expiresAt: new Date(Date.now() + 3600000).toISOString(),
});
await connector.connect(vault);
expect(vault.getConnectorCredential).toHaveBeenCalledWith('gcal');
expect(vault.get).toHaveBeenCalledWith('connector:gcal');
});
it('healthCheck() validates access token', async () => {
@@ -104,10 +96,10 @@ describe('GoogleCalendarConnector', () => {
const health = await connector.healthCheck();
expect(health.status).toBe('connected');
// Verify tokens were stored back in vault
expect(vault.setConnectorCredential).toHaveBeenCalledWith('gcal', expect.objectContaining({
expect(getConnectorCredential(vault, 'gcal')).toMatchObject({
type: 'oauth2',
value: 'ya29.refreshed',
}));
});
});
it('execute(list_events) returns events', async () => {
@@ -205,10 +197,10 @@ describe('OAuth2 token refresh', () => {
await connector.execute('list_events', {});
expect(vault.setConnectorCredential).toHaveBeenCalledWith('gcal', expect.objectContaining({
expect(getConnectorCredential(vault, 'gcal')).toMatchObject({
value: 'ya29.new',
refreshToken: 'rt_new',
}));
});
});
it('returns error when refresh token is invalid', async () => {

View File

@@ -1,21 +1,9 @@
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
import { GitHubConnector } from '../../src/connectors/github-connector.js';
import type { VaultStore } from '@waggle/core';
import { createMemorySecretStore, seedCredential, type MockSecretStore } from '../helpers/memory-secret-store.js';
function createMockVault(cred?: { value: string; isExpired: boolean }): VaultStore {
return {
getConnectorCredential: vi.fn((id: string) => {
if (id === 'github' && cred) return { ...cred, type: 'bearer' };
return null;
}),
get: vi.fn(() => null),
set: vi.fn(),
delete: vi.fn(),
list: vi.fn(() => []),
has: vi.fn(() => false),
setConnectorCredential: vi.fn(),
migrateFromConfig: vi.fn(() => 0),
} as unknown as VaultStore;
function createMockVault(cred?: { value: string; isExpired: boolean }): MockSecretStore {
return createMemorySecretStore({ connectors: cred ? { github: seedCredential(cred) } : {} });
}
describe('GitHubConnector', () => {
@@ -41,7 +29,7 @@ describe('GitHubConnector', () => {
it('connect() retrieves token from vault', async () => {
const vault = createMockVault({ value: 'ghp_test123', isExpired: false });
await connector.connect(vault);
expect(vault.getConnectorCredential).toHaveBeenCalledWith('github');
expect(vault.get).toHaveBeenCalledWith('connector:github');
});
it('healthCheck() returns connected when API responds OK', async () => {

View File

@@ -1,25 +1,15 @@
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
import { JiraConnector } from '../../src/connectors/jira-connector.js';
import type { VaultStore } from '@waggle/core';
import { createMemorySecretStore, type MockSecretStore } from '../helpers/memory-secret-store.js';
function createMockVault(opts?: { token?: string; email?: string; baseUrl?: string }): VaultStore {
return {
getConnectorCredential: vi.fn((id: string) => {
if (id === 'jira' && opts?.token) return { value: opts.token, type: 'bearer', isExpired: false };
return null;
}),
get: vi.fn((name: string) => {
if (name === 'connector:jira:email' && opts?.email) return { value: opts.email };
if (name === 'connector:jira:base_url' && opts?.baseUrl) return { value: opts.baseUrl };
return null;
}),
set: vi.fn(),
delete: vi.fn(),
list: vi.fn(() => []),
has: vi.fn(() => false),
setConnectorCredential: vi.fn(),
migrateFromConfig: vi.fn(() => 0),
} as unknown as VaultStore;
function createMockVault(opts?: { token?: string; email?: string; baseUrl?: string }): MockSecretStore {
const secrets: Record<string, string> = {};
if (opts?.email) secrets['connector:jira:email'] = opts.email;
if (opts?.baseUrl) secrets['connector:jira:base_url'] = opts.baseUrl;
return createMemorySecretStore({
connectors: opts?.token ? { jira: { type: 'bearer', value: opts.token } } : {},
secrets,
});
}
describe('JiraConnector', () => {
@@ -48,7 +38,7 @@ describe('JiraConnector', () => {
baseUrl: 'https://mycompany.atlassian.net',
});
await connector.connect(vault);
expect(vault.getConnectorCredential).toHaveBeenCalledWith('jira');
expect(vault.get).toHaveBeenCalledWith('connector:jira');
expect(vault.get).toHaveBeenCalledWith('connector:jira:email');
});

View File

@@ -1,21 +1,9 @@
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
import { SlackConnector } from '../../src/connectors/slack-connector.js';
import type { VaultStore } from '@waggle/core';
import { createMemorySecretStore, seedCredential, type MockSecretStore } from '../helpers/memory-secret-store.js';
function createMockVault(cred?: { value: string; isExpired: boolean }): VaultStore {
return {
getConnectorCredential: vi.fn((id: string) => {
if (id === 'slack' && cred) return { ...cred, type: 'bearer' };
return null;
}),
get: vi.fn(() => null),
set: vi.fn(),
delete: vi.fn(),
list: vi.fn(() => []),
has: vi.fn(() => false),
setConnectorCredential: vi.fn(),
migrateFromConfig: vi.fn(() => 0),
} as unknown as VaultStore;
function createMockVault(cred?: { value: string; isExpired: boolean }): MockSecretStore {
return createMemorySecretStore({ connectors: cred ? { slack: seedCredential(cred) } : {} });
}
describe('SlackConnector', () => {
@@ -41,7 +29,7 @@ describe('SlackConnector', () => {
it('connect() retrieves token from vault', async () => {
const vault = createMockVault({ value: 'xoxb-test-token', isExpired: false });
await connector.connect(vault);
expect(vault.getConnectorCredential).toHaveBeenCalledWith('slack');
expect(vault.get).toHaveBeenCalledWith('connector:slack');
});
it('healthCheck() returns connected when auth.test succeeds', async () => {

View File

@@ -12,7 +12,7 @@ import { executeCoordinator } from '../../../worker/src/execution/coordinator.js
import { AgentMessageBus } from '../../src/agent-message-bus.js';
import { createAgentCommsTools } from '../../src/agent-comms-tools.js';
import { WorkspaceSessionManager } from '../../../server/src/local/workspace-sessions.js';
import type { VaultStore } from '@waggle/core';
import { createMemorySecretStore, type MockSecretStore } from '../helpers/memory-secret-store.js';
// The session manager is typed against the published @waggle/agent surface.
import type { Orchestrator } from '@waggle/agent';
import type { ConnectorHealth } from '@waggle/shared';
@@ -46,17 +46,10 @@ class MockConnector extends BaseConnector {
}
}
function createMockVault(connected: string[]): VaultStore {
return {
getConnectorCredential: vi.fn((id: string) => {
if (connected.includes(id)) return { value: 'tok', type: 'bearer', isExpired: false };
return null;
}),
setConnectorCredential: vi.fn(),
set: vi.fn(), get: vi.fn(), delete: vi.fn(),
list: vi.fn(() => []), has: vi.fn(() => false),
migrateFromConfig: vi.fn(() => 0),
} as unknown as VaultStore;
function createMockVault(connected: string[]): MockSecretStore {
return createMemorySecretStore({
connectors: Object.fromEntries(connected.map(id => [id, { type: 'bearer' as const, value: 'tok' }])),
});
}
function createMockDeps(): ExecutionDeps {

View File

@@ -0,0 +1,67 @@
import { vi, type Mock } from 'vitest';
import type { SecretEntry, SecretStore } from '@waggle/core';
import {
setConnectorCredential,
type ConnectorCredentialInput,
} from '../../src/connectors/connector-credentials.js';
export type MockSecretStore = { [K in keyof SecretStore]: Mock<SecretStore[K]> };
/** An expiry in the past, for credentials that should read as expired. */
export const EXPIRED_AT = '2020-01-01T00:00:00.000Z';
/** A seed credential from the `{ value, isExpired }` shape the connector tests use. */
export function seedCredential(
cred: { value: string; isExpired: boolean },
type: ConnectorCredentialInput['type'] = 'bearer',
): ConnectorCredentialInput {
return { type, value: cred.value, expiresAt: cred.isExpired ? EXPIRED_AT : undefined };
}
export interface SecretStoreSeed {
/** Connector credentials by connector id, stored with setConnectorCredential. */
connectors?: Record<string, ConnectorCredentialInput>;
/** Plain secrets by name, such as `connector:jira:email`. */
secrets?: Record<string, string>;
}
/**
* A Map-backed SecretStore for connector tests. Every method is a vitest spy.
* The seed is written before the spies exist, so it does not show up in their calls.
*/
export function createMemorySecretStore(seed: SecretStoreSeed = {}): MockSecretStore {
const entries = new Map<string, SecretEntry>();
const store: SecretStore = {
get: name => entries.get(name) ?? null,
set: (name, value, metadata) => {
entries.set(name, { name, value, metadata, updatedAt: new Date().toISOString() });
},
setAsync: async (name, value, metadata) => store.set(name, value, metadata),
writeBatch: (batch) => {
for (const name of batch.delete ?? []) entries.delete(name);
for (const { name, value, metadata } of batch.set ?? []) store.set(name, value, metadata);
},
delete: name => entries.delete(name),
deleteAsync: async name => store.delete(name),
list: () => [...entries.values()].map(({ name, metadata, updatedAt }) => ({ name, metadata, updatedAt })),
has: name => entries.has(name),
};
for (const [connectorId, credential] of Object.entries(seed.connectors ?? {})) {
setConnectorCredential(store, connectorId, credential);
}
for (const [name, value] of Object.entries(seed.secrets ?? {})) {
store.set(name, value);
}
return {
get: vi.fn(store.get),
set: vi.fn(store.set),
setAsync: vi.fn(store.setAsync),
writeBatch: vi.fn(store.writeBatch),
delete: vi.fn(store.delete),
deleteAsync: vi.fn(store.deleteAsync),
list: vi.fn(store.list),
has: vi.fn(store.has),
};
}

View File

@@ -11,7 +11,7 @@ import { composePersonaPrompt, PERSONAS, getPersona } from '../../src/personas.j
import { AgentMessageBus } from '../../src/agent-message-bus.js';
import { needsConfirmation, getApprovalClass } from '../../src/confirmation.js';
import { WorkspaceSessionManager } from '../../../server/src/local/workspace-sessions.js';
import type { VaultStore } from '@waggle/core';
import { createMemorySecretStore, type MockSecretStore } from '../helpers/memory-secret-store.js';
// The session manager is typed against the published @waggle/agent surface.
import type { Orchestrator } from '@waggle/agent';
import type { ConnectorHealth } from '@waggle/shared';
@@ -50,17 +50,10 @@ class PerfConnector extends BaseConnector {
}
}
function createMockVault(connectedIds: string[]): VaultStore {
return {
getConnectorCredential: vi.fn((id: string) => {
if (connectedIds.includes(id)) return { value: 'tok', type: 'bearer', isExpired: false };
return null;
}),
setConnectorCredential: vi.fn(),
set: vi.fn(), get: vi.fn(), delete: vi.fn(),
list: vi.fn(() => []), has: vi.fn(() => false),
migrateFromConfig: vi.fn(() => 0),
} as unknown as VaultStore;
function createMockVault(connectedIds: string[]): MockSecretStore {
return createMemorySecretStore({
connectors: Object.fromEntries(connectedIds.map(id => [id, { type: 'bearer' as const, value: 'tok' }])),
});
}
function timeMs(fn: () => void): number {

View File

@@ -70,7 +70,7 @@ export {
type PendingActionRow, type PendingActionStatus, type SavePendingActionInput,
} from './cron-store.js';
export { VaultStore, type VaultEntry } from './vault.js';
export type { SecretStore, SecretEntry, SecretSummary } from './secret-store.js';
export type { SecretStore, SecretEntry, SecretSummary, SecretBatch, SecretWrite } from './secret-store.js';
export { TelemetryStore, TelemetryCollector, TELEMETRY_EVENTS, type TelemetryEvent, type TelemetrySummary } from './telemetry.js';
export {
SkillHashStore, computeSkillHash, SKILL_HASHES_TABLE_SQL,

View File

@@ -18,11 +18,29 @@ export interface SecretSummary {
updatedAt: string;
}
export interface SecretWrite {
name: string;
value: string;
metadata?: Record<string, unknown>;
}
/** Writes and deletions that a store applies as one update. */
export interface SecretBatch {
set?: readonly SecretWrite[];
delete?: readonly string[];
}
export interface SecretStore {
/** The decrypted secret, or null when it is missing or cannot be read. */
get(name: string): SecretEntry | null;
set(name: string, value: string, metadata?: Record<string, unknown>): void;
setAsync(name: string, value: string, metadata?: Record<string, unknown>): Promise<void>;
/**
* Applies several writes and deletions as one update, so secrets that belong
* together (a connector token and its companion values) are never left
* half-written. Deletions apply before writes.
*/
writeBatch(batch: SecretBatch): void;
/** Returns true when the secret existed. */
delete(name: string): boolean;
deleteAsync(name: string): Promise<boolean>;

View File

@@ -12,7 +12,7 @@ import * as fs from 'node:fs';
import * as path from 'node:path';
import { execFileSync } from 'node:child_process';
import { createCoreLogger } from './logger.js';
import type { SecretEntry, SecretStore } from './secret-store.js';
import type { SecretBatch, SecretEntry, SecretStore } from './secret-store.js';
const log = createCoreLogger('vault');
const ALGORITHM = 'aes-256-gcm';
@@ -34,14 +34,6 @@ interface VaultRecord {
updatedAt: string;
}
interface ConnectorCredentialInput {
type: 'api_key' | 'oauth2' | 'bearer' | 'basic';
value: string;
refreshToken?: string;
expiresAt?: string;
scopes?: string[];
}
/** The file-backed SecretStore. */
export class VaultStore implements SecretStore {
private dataDir: string;
@@ -270,6 +262,19 @@ export class VaultStore implements SecretStore {
this.set(name, value, metadata);
}
/** Apply several writes and deletions with one vault-file replacement. */
writeBatch(batch: SecretBatch): void {
const vault = this.readVault();
for (const name of batch.delete ?? []) {
delete vault[name];
}
const updatedAt = new Date().toISOString();
for (const { name, value, metadata } of batch.set ?? []) {
vault[name] = { encrypted: this.encrypt(value), metadata, updatedAt };
}
this.writeVault(vault);
}
/** Get a decrypted secret by name. Returns null if not found or decryption fails. */
get(name: string): VaultEntry | null {
const vault = this.readVault();
@@ -323,83 +328,6 @@ export class VaultStore implements SecretStore {
return name in vault;
}
/** Set a connector credential with typed metadata. */
setConnectorCredential(connectorId: string, credential: ConnectorCredentialInput): void {
this.setConnectorCredentialBundle(connectorId, credential);
}
/**
* Persist a connector credential and its encrypted companion values with one
* vault-file replacement. This prevents a multi-field credential (for
* example Jira token + email + site origin) from being partially updated.
*/
setConnectorCredentialBundle(
connectorId: string,
credential: ConnectorCredentialInput,
relatedSecrets: Readonly<Record<string, string>> = {},
): void {
const secretEntries = Object.entries(relatedSecrets);
if (secretEntries.some(([suffix]) => !/^[a-z][a-z0-9_]*$/.test(suffix))) {
throw new TypeError('Invalid connector credential suffix');
}
const vault = this.readVault();
const updatedAt = new Date().toISOString();
vault[`connector:${connectorId}`] = {
encrypted: this.encrypt(credential.value),
metadata: {
credentialType: credential.type,
expiresAt: credential.expiresAt,
scopes: credential.scopes,
},
updatedAt,
};
const refreshKey = `connector:${connectorId}:refresh`;
if (credential.refreshToken) {
vault[refreshKey] = {
encrypted: this.encrypt(credential.refreshToken),
updatedAt,
};
} else {
delete vault[refreshKey];
}
for (const [suffix, value] of secretEntries) {
vault[`connector:${connectorId}:${suffix}`] = {
encrypted: this.encrypt(value),
updatedAt,
};
}
this.writeVault(vault);
}
/** Get a connector credential with typed metadata */
getConnectorCredential(connectorId: string): {
value: string;
type: string;
refreshToken?: string;
expiresAt?: string;
scopes?: string[];
isExpired: boolean;
} | null {
const entry = this.get(`connector:${connectorId}`);
if (!entry) return null;
const expiresAt = entry.metadata?.expiresAt as string | undefined;
// Retrieve refresh token from its own encrypted entry
const refreshEntry = this.get(`connector:${connectorId}:refresh`);
return {
value: entry.value,
type: (entry.metadata?.credentialType as string) ?? 'api_key',
// Review Major #7: removed dead plaintext metadata fallback that contradicted
// the "never in plaintext metadata" security contract from setConnectorCredential.
refreshToken: refreshEntry?.value,
expiresAt,
scopes: entry.metadata?.scopes as string[] | undefined,
isExpired: expiresAt ? new Date(expiresAt) < new Date() : false,
};
}
/** Migrate plaintext providers from config.json to vault. Returns count migrated. */
migrateFromConfig(config: { providers?: Record<string, { apiKey: string; models?: string[]; baseUrl?: string }> }): number {
if (!config.providers) return 0;

View File

@@ -229,102 +229,6 @@ describe('VaultStore', () => {
expect(keyHex2).toBe(keyHex);
});
it('setConnectorCredential — refresh token is NOT stored as plaintext in vault.json', () => {
const dir = makeTempDir();
const vault = new VaultStore(dir);
const refreshToken = 'rt-super-secret-refresh-token-xyz';
vault.setConnectorCredential('github', {
type: 'oauth2',
value: 'gho_access_token_123',
refreshToken,
expiresAt: '2099-01-01T00:00:00Z',
scopes: ['repo', 'user'],
});
// Read raw vault.json and verify the refresh token is NOT in plaintext
const rawContent = fs.readFileSync(path.join(dir, 'vault.json'), 'utf-8');
expect(rawContent).not.toContain(refreshToken);
// The metadata for the main connector entry must NOT contain refreshToken
const parsed = JSON.parse(rawContent);
const mainEntry = parsed['connector:github'];
expect(mainEntry).toBeDefined();
expect(mainEntry.metadata).not.toHaveProperty('refreshToken');
// The refresh token should be stored as a separate encrypted entry
const refreshEntry = parsed['connector:github:refresh'];
expect(refreshEntry).toBeDefined();
expect(refreshEntry.encrypted).toMatch(/^[0-9a-f]+:[0-9a-f]+:[0-9a-f]+$/);
});
it('getConnectorCredential — returns decrypted refresh token correctly', () => {
const dir = makeTempDir();
const vault = new VaultStore(dir);
const accessToken = 'gho_access_token_456';
const refreshToken = 'rt-secret-refresh-token-abc';
vault.setConnectorCredential('github', {
type: 'oauth2',
value: accessToken,
refreshToken,
expiresAt: '2099-01-01T00:00:00Z',
scopes: ['repo'],
});
const cred = vault.getConnectorCredential('github');
expect(cred).not.toBeNull();
expect(cred!.value).toBe(accessToken);
expect(cred!.refreshToken).toBe(refreshToken);
expect(cred!.type).toBe('oauth2');
expect(cred!.expiresAt).toBe('2099-01-01T00:00:00Z');
expect(cred!.scopes).toEqual(['repo']);
expect(cred!.isExpired).toBe(false);
});
it('setConnectorCredential — without refresh token does not create refresh entry', () => {
const dir = makeTempDir();
const vault = new VaultStore(dir);
vault.setConnectorCredential('slack', {
type: 'bearer',
value: 'xoxb-token-123',
});
const rawContent = fs.readFileSync(path.join(dir, 'vault.json'), 'utf-8');
const parsed = JSON.parse(rawContent);
expect(parsed['connector:slack']).toBeDefined();
expect(parsed['connector:slack:refresh']).toBeUndefined();
const cred = vault.getConnectorCredential('slack');
expect(cred).not.toBeNull();
expect(cred!.value).toBe('xoxb-token-123');
expect(cred!.refreshToken).toBeUndefined();
});
it('setConnectorCredential — clears refresh token when re-set without one', () => {
const dir = makeTempDir();
const vault = new VaultStore(dir);
// First set with refresh token
vault.setConnectorCredential('github', {
type: 'oauth2',
value: 'gho_token_1',
refreshToken: 'rt-old-refresh',
});
expect(vault.getConnectorCredential('github')!.refreshToken).toBe('rt-old-refresh');
// Re-set without refresh token
vault.setConnectorCredential('github', {
type: 'oauth2',
value: 'gho_token_2',
});
const cred = vault.getConnectorCredential('github');
expect(cred!.value).toBe('gho_token_2');
expect(cred!.refreshToken).toBeUndefined();
expect(vault.has('connector:github:refresh')).toBe(false);
});
it('corrupted key file — truncated hex throws clear error', () => {
const dir = makeTempDir();
const keyPath = path.join(dir, '.vault-key');

View File

@@ -1,7 +1,10 @@
import type { FastifyInstance } from 'fastify';
import type { ConnectorHealth } from '@waggle/shared';
import type { RecordAuditInput } from '@waggle/core';
import { JiraConnector, SalesforceConnector } from '@waggle/agent';
import {
JiraConnector, SalesforceConnector,
getConnectorCredential, setConnectorCredential, setConnectorCredentialBundle,
} from '@waggle/agent';
/** Vault sub-key holding the C16 manual-sync stamp. Matches the
* `connector:{id}:*` prefix so disconnect/revoke auto-clean it. */
@@ -80,7 +83,7 @@ export async function connectorRoutes(fastify: FastifyInstance) {
}
// Fallback: basic health without registry
const cred = fastify.vault?.getConnectorCredential(id);
const cred = fastify.vault ? getConnectorCredential(fastify.vault, id) : null;
const lastSyncAt = fastify.vault?.get(lastSyncKey(id))?.value;
const health: ConnectorHealth = {
id,
@@ -161,12 +164,12 @@ export async function connectorRoutes(fastify: FastifyInstance) {
};
if (jiraEmail && jiraBaseUrl) {
fastify.vault.setConnectorCredentialBundle(id, credential, {
setConnectorCredentialBundle(fastify.vault, id, credential, {
email: jiraEmail,
base_url: jiraBaseUrl,
});
} else {
fastify.vault.setConnectorCredential(id, credential);
setConnectorCredential(fastify.vault, id, credential);
}
if (salesforceInstanceUrl) {

View File

@@ -19,6 +19,7 @@ import fs from 'node:fs';
import path from 'node:path';
import os from 'node:os';
import { MindDB, FrameStore, SessionStore, VaultStore } from '@waggle/core';
import { getConnectorCredential, setConnectorCredential } from '@waggle/agent';
import { buildLocalServer } from '../src/local/index.js';
import type { FastifyInstance } from 'fastify';
import { injectWithAuth } from './test-utils.js';
@@ -439,13 +440,13 @@ describe('Vault encryption cross-platform', () => {
it('connector credential helpers work', () => {
const vault = new VaultStore(tmpDir);
vault.setConnectorCredential('github', {
setConnectorCredential(vault, 'github', {
type: 'bearer',
value: 'ghp_test123',
scopes: ['repo', 'read:org'],
});
const cred = vault.getConnectorCredential('github');
const cred = getConnectorCredential(vault, 'github');
expect(cred).not.toBeNull();
expect(cred!.value).toBe('ghp_test123');
expect(cred!.type).toBe('bearer');

View File

@@ -1,7 +1,7 @@
import { describe, it, expect, vi } from 'vitest';
import { ConnectorRegistry } from '@waggle/agent';
import { BaseConnector, type ConnectorAction, type ConnectorResult } from '@waggle/agent';
import type { VaultStore } from '@waggle/core';
import type { SecretStore } from '@waggle/core';
import type { ConnectorHealth } from '@waggle/shared';
import { needsConfirmation, getApprovalClass } from '@waggle/agent';
@@ -38,21 +38,30 @@ class TestConnector extends BaseConnector {
}
}
function createMockVault(credentials: Record<string, { value: string; isExpired: boolean }> = {}): VaultStore {
/** Serves `connector:<id>` entries in the layout setConnectorCredential writes. */
function createMockVault(credentials: Record<string, { value: string; isExpired: boolean }> = {}): SecretStore {
return {
getConnectorCredential: vi.fn((id: string) => {
const cred = credentials[id];
get: vi.fn((name: string) => {
const cred = credentials[name.replace(/^connector:/, '')];
if (!cred) return null;
return { value: cred.value, type: 'bearer', isExpired: cred.isExpired };
return {
name,
value: cred.value,
metadata: {
credentialType: 'bearer',
expiresAt: cred.isExpired ? '2020-01-01T00:00:00.000Z' : undefined,
},
updatedAt: new Date().toISOString(),
};
}),
setConnectorCredential: vi.fn(),
set: vi.fn(),
get: vi.fn(),
setAsync: vi.fn(),
writeBatch: vi.fn(),
delete: vi.fn(),
deleteAsync: vi.fn(),
list: vi.fn(() => []),
has: vi.fn(() => false),
migrateFromConfig: vi.fn(() => 0),
} as unknown as VaultStore;
};
}
// ─── Integration Tests ───────────────────────────────────────────────

View File

@@ -19,6 +19,8 @@ import {
GoogleCalendarConnector,
JiraConnector,
SalesforceConnector,
getConnectorCredential,
setConnectorCredential,
type ConnectorAction,
type ConnectorResult,
} from '@waggle/agent';
@@ -48,7 +50,7 @@ class TestConnector extends BaseConnector {
this.connectCalls += 1;
if (this.connectGate) await this.connectGate;
if (this.connectError) throw this.connectError;
this.credentialValue = vault.getConnectorCredential(this.id)?.value ?? null;
this.credentialValue = getConnectorCredential(vault, this.id)?.value ?? null;
}
async healthCheck(): Promise<ConnectorHealth> {
this.healthCheckCalls += 1;
@@ -147,7 +149,7 @@ describe('Connector routes — Phase 4 extensions', () => {
expect(res.statusCode).toBe(400);
expect(res.json()).toEqual({ error: 'Valid Salesforce instanceUrl required' });
expect(vault.getConnectorCredential('salesforce')).toBeNull();
expect(getConnectorCredential(vault, 'salesforce')).toBeNull();
expect(vault.get('connector:salesforce:instance_url')).toBeNull();
});
@@ -173,7 +175,7 @@ describe('Connector routes — Phase 4 extensions', () => {
});
it('rehydrates persisted Salesforce state on restart and hides incomplete legacy state', async () => {
vault.setConnectorCredential('salesforce', { type: 'bearer', value: 'persisted-token' });
setConnectorCredential(vault, 'salesforce', { type: 'bearer', value: 'persisted-token' });
vault.set('connector:salesforce:instance_url', 'https://acme.my.salesforce.com');
const restarted = registerConnectors(vault);
@@ -193,7 +195,7 @@ describe('Connector routes — Phase 4 extensions', () => {
it('rejects unsafe Jira site origins without partial writes, then accepts a corrected retry', async () => {
const jira = new JiraConnector();
registry.register(jira);
vault.setConnectorCredential('jira', { type: 'bearer', value: 'old-token' });
setConnectorCredential(vault, 'jira', { type: 'bearer', value: 'old-token' });
vault.set('connector:jira:email', 'old@example.com');
vault.set('connector:jira:base_url', 'https://old-team.atlassian.net');
await registry.hydrate('jira');
@@ -210,7 +212,7 @@ describe('Connector routes — Phase 4 extensions', () => {
expect(blankToken.statusCode).toBe(400);
expect(blankToken.json()).toEqual({ error: 'token or apiKey required' });
expect(vault.getConnectorCredential('jira')?.value).toBe('old-token');
expect(getConnectorCredential(vault, 'jira')?.value).toBe('old-token');
expect(vault.get('connector:jira:email')?.value).toBe('old@example.com');
expect(vault.get('connector:jira:base_url')?.value).toBe('https://old-team.atlassian.net');
@@ -226,7 +228,7 @@ describe('Connector routes — Phase 4 extensions', () => {
expect(rejected.statusCode).toBe(400);
expect(rejected.json()).toEqual({ error: 'Valid Jira baseUrl required' });
expect(vault.getConnectorCredential('jira')?.value).toBe('old-token');
expect(getConnectorCredential(vault, 'jira')?.value).toBe('old-token');
expect(vault.get('connector:jira:email')?.value).toBe('old@example.com');
expect(vault.get('connector:jira:base_url')?.value).toBe('https://old-team.atlassian.net');
@@ -242,7 +244,7 @@ describe('Connector routes — Phase 4 extensions', () => {
expect(accepted.statusCode).toBe(200);
expect(accepted.json()).toEqual({ connected: true, connectorId: 'jira' });
expect(vault.getConnectorCredential('jira')?.value).toBe('new-token');
expect(getConnectorCredential(vault, 'jira')?.value).toBe('new-token');
expect(vault.get('connector:jira:email')?.value).toBe('new@example.com');
expect(vault.get('connector:jira:base_url')?.value).toBe('https://new-team.atlassian.net');
expect(registry.getDefinitions().find(def => def.id === 'jira')?.status).toBe('connected');
@@ -269,7 +271,7 @@ describe('Connector routes — Phase 4 extensions', () => {
it('does not advertise Jira as usable when email or a safe site origin is missing', async () => {
const jira = new JiraConnector();
registry.register(jira);
vault.setConnectorCredential('jira', { type: 'bearer', value: 'jira-token' });
setConnectorCredential(vault, 'jira', { type: 'bearer', value: 'jira-token' });
vault.set('connector:jira:base_url', 'https://team.atlassian.net');
await registry.hydrate('jira');
@@ -287,7 +289,7 @@ describe('Connector routes — Phase 4 extensions', () => {
error: expect.stringContaining('Not connected'),
});
vault.setConnectorCredential('jira', { type: 'bearer', value: ' ' });
setConnectorCredential(vault, 'jira', { type: 'bearer', value: ' ' });
vault.set('connector:jira:base_url', 'https://team.atlassian.net');
await registry.hydrate('jira');
@@ -297,7 +299,7 @@ describe('Connector routes — Phase 4 extensions', () => {
});
it('waits for startup hydration before health checks or issued-tool execution', async () => {
vault.setConnectorCredential('test-conn', { type: 'bearer', value: 'persisted-token' });
setConnectorCredential(vault, 'test-conn', { type: 'bearer', value: 'persisted-token' });
let releaseHydration!: () => void;
const hydrationGate = new Promise<void>((resolve) => {
releaseHydration = resolve;
@@ -363,7 +365,7 @@ describe('Connector routes — Phase 4 extensions', () => {
payload: { token: 'secret-token' },
});
const issuedTool = registry.generateTools().find(tool => tool.name === 'connector_test-conn_read_data')!;
const credentialRead = vi.spyOn(vault, 'getConnectorCredential').mockImplementation(() => {
const credentialRead = vi.spyOn(vault, 'get').mockImplementation(() => {
throw new Error('vault offline');
});
@@ -475,7 +477,7 @@ describe('Connector routes — Phase 4 extensions', () => {
expect(healthResponse.statusCode).toBe(200);
expect(healthResponse.json().status).toBe('error');
expect(vault.getConnectorCredential('gcal')).toBeNull();
expect(getConnectorCredential(vault, 'gcal')).toBeNull();
expect(fetchMock).toHaveBeenCalledTimes(1);
});
@@ -550,7 +552,7 @@ describe('Connector routes — Phase 4 extensions', () => {
it('revoke purges credentials, sub-keys AND OAuth tokens, then audits', async () => {
// Seed: connector credential + sub-keys + the oauth.ts-style token keys
vault.setConnectorCredential('test-conn', { type: 'oauth2', value: 'access-tok', refreshToken: 'refresh-tok' });
setConnectorCredential(vault, 'test-conn', { type: 'oauth2', value: 'access-tok', refreshToken: 'refresh-tok' });
vault.set('connector:test-conn:email', 'a@b.c');
vault.set('connector:test-conn:lastSync', new Date().toISOString());
vault.set('test-conn_oauth_token', 'oauth-access', { credentialType: 'oauth2' });
@@ -561,7 +563,7 @@ describe('Connector routes — Phase 4 extensions', () => {
expect(res.json()).toMatchObject({ ok: true, connectorId: 'test-conn', revoked: true, oauthPurged: 2 });
// Everything gone: credential, every sub-key, both OAuth tokens
expect(vault.getConnectorCredential('test-conn')).toBeNull();
expect(getConnectorCredential(vault, 'test-conn')).toBeNull();
expect(vault.get('connector:test-conn:email')).toBeNull();
expect(vault.get('connector:test-conn:lastSync')).toBeNull();
expect(vault.get('connector:test-conn:refresh')).toBeNull();
@@ -579,7 +581,7 @@ describe('Connector routes — Phase 4 extensions', () => {
// gcal/gdrive/gdocs/gmail/gsheets all authenticate via the 'google' OAuth
// provider — oauth.ts stores google_oauth_token, never gcal_oauth_token.
// Without the id→provider map the live google token pair survived forever.
vault.setConnectorCredential('gcal', { type: 'oauth2', value: 'access-tok' });
setConnectorCredential(vault, 'gcal', { type: 'oauth2', value: 'access-tok' });
vault.set('google_oauth_token', 'g-access', { credentialType: 'oauth2' });
vault.set('google_oauth_refresh_token', 'g-refresh', { credentialType: 'oauth2' });
@@ -590,7 +592,7 @@ describe('Connector routes — Phase 4 extensions', () => {
// The PROVIDER-keyed pair (the actual live tokens) is purged
expect(vault.get('google_oauth_token')).toBeNull();
expect(vault.get('google_oauth_refresh_token')).toBeNull();
expect(vault.getConnectorCredential('gcal')).toBeNull();
expect(getConnectorCredential(vault, 'gcal')).toBeNull();
const audit = auditStore.getByCapability('gcal');
expect(audit).toHaveLength(1);
@@ -604,7 +606,7 @@ describe('Connector routes — Phase 4 extensions', () => {
});
it('disconnect stays the lighter alias: cleans vault keys but writes NO audit entry', async () => {
vault.setConnectorCredential('test-conn', { type: 'bearer', value: 'tok' });
setConnectorCredential(vault, 'test-conn', { type: 'bearer', value: 'tok' });
vault.set('test-conn_oauth_token', 'oauth-access');
const res = await server.inject({ method: 'POST', url: '/api/connectors/test-conn/disconnect' });
@@ -612,7 +614,7 @@ describe('Connector routes — Phase 4 extensions', () => {
expect(res.json().disconnected).toBe(true);
// Connector keyspace cleaned, but OAuth tokens are NOT purged (revoke-only)
expect(vault.getConnectorCredential('test-conn')).toBeNull();
expect(getConnectorCredential(vault, 'test-conn')).toBeNull();
expect(vault.get('test-conn_oauth_token')?.value).toBe('oauth-access');
expect(auditStore.getByCapability('test-conn')).toHaveLength(0);
});

View File

@@ -1,8 +1,4 @@
import { describe, it, expect } from 'vitest';
import path from 'node:path';
import { VaultStore } from '@waggle/core';
import fs from 'node:fs';
import os from 'node:os';
describe('Connector Foundation', () => {
it('exports connectorRoutes function', async () => {
@@ -12,74 +8,6 @@ describe('Connector Foundation', () => {
});
});
describe('Vault Connector Credential Methods', () => {
const tmpDir = path.join(os.tmpdir(), `waggle-vault-test-${Date.now()}`);
it('setConnectorCredential stores and getConnectorCredential retrieves', () => {
fs.mkdirSync(tmpDir, { recursive: true });
const vault = new VaultStore(tmpDir);
vault.setConnectorCredential('github', {
type: 'bearer',
value: 'ghp_test_token_12345',
scopes: ['repo', 'user'],
});
const cred = vault.getConnectorCredential('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);
// Cleanup
fs.rmSync(tmpDir, { recursive: true, force: true });
});
it('detects expired tokens', () => {
fs.mkdirSync(tmpDir, { recursive: true });
const vault = new VaultStore(tmpDir);
vault.setConnectorCredential('slack', {
type: 'oauth2',
value: 'xoxb-expired-token',
expiresAt: '2020-01-01T00:00:00.000Z', // Way in the past
});
const cred = vault.getConnectorCredential('slack');
expect(cred).not.toBeNull();
expect(cred!.isExpired).toBe(true);
fs.rmSync(tmpDir, { recursive: true, force: true });
});
it('returns null for non-existent connector', () => {
fs.mkdirSync(tmpDir, { recursive: true });
const vault = new VaultStore(tmpDir);
const cred = vault.getConnectorCredential('nonexistent');
expect(cred).toBeNull();
fs.rmSync(tmpDir, { recursive: true, force: true });
});
it('disconnect removes credential from vault', () => {
fs.mkdirSync(tmpDir, { recursive: true });
const vault = new VaultStore(tmpDir);
vault.setConnectorCredential('github', {
type: 'bearer',
value: 'ghp_test_token',
});
expect(vault.getConnectorCredential('github')).not.toBeNull();
vault.delete('connector:github');
expect(vault.getConnectorCredential('github')).toBeNull();
fs.rmSync(tmpDir, { recursive: true, force: true });
});
});
describe('ConnectorDefinition Types', () => {
it('shared types include connector types', async () => {
// Verify the types compile correctly by importing them