All files / core/blong-gogo/src/adapter/server vault.ts

63.01% Statements 247/392
35.93% Branches 23/64
55.55% Functions 5/9
63.01% Lines 247/392

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 3931x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 12x 12x 12x 1x 1x 6x 6x 6x     6x 6x 1x                                                                                         1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x     1x 1x 1x 1x 1x 1x 1x 1x 1x     1x   1x 1x 1x 1x 1x 1x 1x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 14x 14x 14x 14x 17x 17x 17x 17x 3x 3x     3x     3x 3x 3x 3x 3x 3x       3x 3x 3x 3x 3x 3x 3x 3x 3x 3x     3x 3x               3x 17x 17x 4x 4x     4x     4x 4x 4x     4x 4x 4x 4x 4x     4x 17x 17x 4x 4x     4x     4x 4x 4x 4x 4x         4x 4x 4x 4x 4x     4x 17x 4x 4x     4x 4x 1x 1x 1x 1x 1x 4x 4x 4x 4x 4x     4x 4x 4x 4x 4x 4x 4x 1x 1x 1x     1x 4x     3x 3x 3x 3x 3x 3x 4x             3x 3x 3x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 1x 1x 1x 1x 1x 1x     4x 17x                                               17x                               17x 1x 1x 1x 1x     1x 17x 1x 1x 1x 1x     1x 17x     1x 1x  
import {adapter, type Errors, type IErrorMap, type IMeta} from '@feasibleone/blong/types';
import vault from 'node-vault';
 
export interface IConfig {
    vault: {
        endpoint?: string;
        token?: string;
        apiVersion?: string;
        namespace?: string;
        requestOptions?: object;
        roleId?: string;
        secretId?: string;
        authMethod?: 'token' | 'approle' | 'userpass' | 'ldap';
        username?: string;
        password?: string;
    };
    context: {
        vault?: vault.client;
    };
}
 
const errorMap: IErrorMap = {
    'vault.generic': 'Vault Error',
    'vault.invalid': 'Invalid Vault Operation',
    'vault.notFound': 'Vault Secret Not Found',
    'vault.unauthorized': 'Vault Unauthorized',
    'vault.forbidden': 'Vault Access Forbidden',
    'vault.missingKey': 'Missing key value for {key}',
    'vault.missingPath': 'Missing secret path',
    'vault.authFailed': 'Vault authentication failed',
};
 
let _errors: Errors<typeof errorMap>;
 
// KV v2 secret-engine mount paths (trailing slash, e.g. `secret/`), discovered
// from `vault.mount.list`. KV v2 stores the actual secrets under `metadata/`
// (listing) / `data/` (reading) instead of the mount root.
const kv2Mounts = new Set<string>();
 
/** Collapse doubled separators from `{parent.path}/{key}` joins. */
function normalizePath(path: string): string {
    return path.replace(/\/{2,}/g, '/');
}
 
/** The KV v2 mount a path belongs to (e.g. `secret/`), or null. */
function kv2MountFor(path: string): string | null {
    const normalized = normalizePath(path);
    for (const mount of kv2Mounts) {
        if (normalized === mount || normalized.startsWith(mount)) return mount;
    }
    return null;
}
 
async function authenticateVault(this: {config: IConfig}): Promise<void> {
    const {authMethod, roleId, secretId, username, password} = this.config.vault;

    try {
        switch (authMethod) {
            case 'approle': {
                if (!roleId || !secretId)
                    throw _errors['vault.missingKey']({key: 'roleId or secretId'});
                const result = await this.config.context.vault!.approleLogin({
                    role_id: roleId,
                    secret_id: secretId,
                });
                this.config.context.vault!.token = result.auth.client_token;
                break;
            }
            case 'userpass': {
                if (!username || !password)
                    throw _errors['vault.missingKey']({key: 'username or password'});
                const result = await this.config.context.vault!.userpassLogin({
                    username,
                    password,
                });
                this.config.context.vault!.token = result.auth.client_token;
                break;
            }
            case 'ldap': {
                if (!username || !password)
                    throw _errors['vault.missingKey']({key: 'username or password'});
                const result = await this.config.context.vault!.ldapLogin({
                    username,
                    password,
                });
                this.config.context.vault!.token = result.auth.client_token;
                break;
            }
            default:
                throw _errors['vault.authFailed']();
        }
    } catch (error) {
        throw _errors['vault.authFailed'](error);
    }
}

export default adapter<IConfig>(({utError}) => {
    _errors ||= utError.register(errorMap);
 
    return {
        activation: {
            default: {
                type: 'vault',
            },
        },
        async start() {
            const vaultOptions = {
                endpoint: this.config.vault.endpoint || 'http://127.0.0.1:8200',
                token: this.config.vault.token,
                apiVersion: this.config.vault.apiVersion || 'v1',
                namespace: this.config.vault.namespace,
                requestOptions: this.config.vault.requestOptions || {},
            };
 
            this.config.context = {vault: vault(vaultOptions)};
 
            // Handle authentication if not using token directly
            if (!this.config.vault.token && this.config.vault.authMethod) {
                await authenticateVault.call(this);
            }
 
            super.connect();
            return super.start();
        },
        async stop(...params: unknown[]) {
            let result;
            try {
                // Revoke token if we authenticated
                if (this.config.context?.vault?.token && this.config.vault.authMethod) {
                    await this.config.context.vault!.tokenRevokeSelf();
                }
            } catch {
                // Ignore revocation errors during shutdown
            } finally {
                this.config.context = {};
                result = await super.stop(...params);
            }
            return result;
        },
        async exec(
            params:
                | ({
                      path?: string;
                      data?: Record<string, unknown>;
                      mount_point?: string;
                      type?: string;
                      description?: string;
                      config?: Record<string, unknown>;
                      version?: number;
                      metadata?: Record<string, unknown>;
                  } & Record<string, unknown>)
                | unknown[],
            $meta: IMeta,
        ) {
            const {method} = $meta;
            const [, resource, operation] = method!.split('.');
            let secretPath = resource;
            let actualParams = params;
 
            if (!Array.isArray(params) && params.path) {
                secretPath = params.path;
                const {path: _pathParam, ...rest} = params;
                actualParams = rest;
            }
 
            switch (operation) {
                case 'read':
                case 'get': {
                    // Read secret from Vault
                    if (Array.isArray(actualParams)) {
                        throw this.error(_errors['vault.invalid'](), $meta);
                    }
                    if (!secretPath) {
                        throw this.error(_errors['vault.missingPath'](), $meta);
                    }
 
                    // KV v2: secrets are read from `<mount>data/<name>`, and the
                    // secret fields are wrapped under `data`.
                    let readPath = normalizePath(secretPath);
                    const mount = kv2MountFor(readPath);
                    if (mount && !readPath.startsWith(`${mount}data/`)) {
                        const name = readPath.slice(mount.length);
                        readPath = `${mount}data/${name}`;
                    }
 
                    try {
                        const result = await this.config.context.vault!.read(readPath);
                        const payload = result.data;
                        if (
                            payload &&
                            typeof payload === 'object' &&
                            !Array.isArray(payload) &&
                            'data' in payload
                        ) {
                            return (payload as {data: unknown}).data;
                        }
                        return payload;
                    } catch (error: unknown) {
                        throw this.error(
                            (error as {response?: {statusCode?: number}})?.response?.statusCode === 404
                                ? _errors['vault.notFound'](error)
                                : _errors['vault.generic'](error),
                            $meta,
                        );
                    }
                }
                case 'write':
                case 'put': {
                    // Write secret to Vault
                    if (Array.isArray(actualParams)) {
                        throw this.error(_errors['vault.invalid'](), $meta);
                    }
                    if (!secretPath) {
                        throw this.error(_errors['vault.missingPath'](), $meta);
                    }
 
                    const {data, metadata} = actualParams;
                    if (!data) {
                        throw this.error(_errors['vault.missingKey']({key: 'data'}), $meta);
                    }
 
                    try {
                        const writeParams = metadata ? {data, metadata} : data;
                        return await this.config.context.vault!.write(secretPath, writeParams);
                    } catch (error: unknown) {
                        throw this.error(_errors['vault.generic'](error), $meta);
                    }
                }
                case 'delete':
                case 'remove': {
                    // Delete secret from Vault
                    if (Array.isArray(actualParams)) {
                        throw this.error(_errors['vault.invalid'](), $meta);
                    }
                    if (!secretPath) {
                        throw this.error(_errors['vault.missingPath'](), $meta);
                    }
 
                    const {version} = actualParams;
 
                    try {
                        if (version !== undefined) {
                            // Delete specific version for KV v2
                            return await this.config.context.vault!.delete(`${secretPath}`, {
                                versions: [version],
                            });
                        } else {
                            // Delete latest version or entire secret
                            return await this.config.context.vault!.delete(secretPath);
                        }
                    } catch (error: unknown) {
                        throw this.error(_errors['vault.generic'](error), $meta);
                    }
                }
                case 'list': {
                    // List secrets at path
                    if (Array.isArray(actualParams)) {
                        throw this.error(_errors['vault.invalid'](), $meta);
                    }
                    // `vault.mount.list` — enumerate mounted secret engines
                    if (resource === 'mount') {
                        try {
                            const result = await this.config.context.vault!.mounts();
                            kv2Mounts.clear();
                            const items = Object.entries(result?.data ?? {}).map(
                                ([path, cfg]: [string, unknown]) => {
                                    const c = cfg as {
                                        type?: string;
                                        options?: {version?: number};
                                    };
                                    if (c?.type === 'kv' && (c.options?.version ?? 0) === 2) {
                                        kv2Mounts.add(path);
                                    }
                                    return {
                                        path,
                                        ...(typeof cfg === 'object' && cfg !== null
                                            ? (cfg as Record<string, unknown>)
                                            : {}),
                                    };
                                },
                            );
                            return {items};
                        } catch (error: unknown) {
                            throw this.error(_errors['vault.generic'](error), $meta);
                        }
                    }
                    if (!secretPath) {
                        throw this.error(_errors['vault.missingPath'](), $meta);
                    }
 
                    // KV v2: a mount root (or its `data/` marker) lists the actual
                    // secrets through `metadata/` — never the raw mount root.
                    const originalPath = normalizePath(secretPath);
                    let listPath = originalPath;
                    const mount = kv2MountFor(listPath);
                    if (mount) {
                        const trimmedMount = mount.replace(/\/+$/, '');
                        const trimmedList = listPath.replace(/\/+$/, '');
                        if (trimmedList === trimmedMount || trimmedList === `${trimmedMount}/data`) {
                            listPath = `${trimmedMount}/metadata/`;
                        }
                    }
 
                    try {
                        const result = await this.config.context.vault!.list(listPath);
                        const data = (result.data ?? {}) as {keys?: string[]};
                        // Keep the native Vault `keys` shape (relied on by the
                        // integration tests) and additionally expose commander-style
                        // `items` rows (consistent with `vault.mount.list`) so the
                        // generic explorer can render the secrets as a table. Keys
                        // ending in `/` are sub-path (directory) markers, not leaf
                        // secrets — clicking one would 404 ("Vault Secret Not
                        // Found"), so they are filtered out. Rows carry the MOUNT
                        // path so the deeper level's `{parent.path}/{key}` open
                        // resolves to `<mount>/<name>`.
                        const keys = (data.keys ?? []).filter(key => !key.endsWith('/'));
                        return {
                            ...data,
                            keys,
                            items: keys.map(key => ({key, path: originalPath})),
                        };
                    } catch (error: unknown) {
                        if (
                            (error as {response?: {statusCode?: number}})?.response?.statusCode ===
                            404
                        ) {
                            return {keys: [], items: []};
                        }
                        throw this.error(_errors['vault.generic'](error), $meta);
                    }
                }
                case 'mount': {
                    // Mount secret engine
                    if (Array.isArray(actualParams)) {
                        throw this.error(_errors['vault.invalid'](), $meta);
                    }
                    const {mount_point, type, description, config} = actualParams;
                    if (!mount_point) {
                        throw this.error(_errors['vault.missingKey']({key: 'mount_point'}), $meta);
                    }
                    if (!type) {
                        throw this.error(_errors['vault.missingKey']({key: 'type'}), $meta);
                    }

                    try {
                        return await this.config.context.vault!.mount({
                            mount_point,
                            type,
                            description,
                            config,
                        });
                    } catch (error: unknown) {
                        throw this.error(_errors['vault.generic'](error), $meta);
                    }
                }
                case 'unmount': {
                    // Unmount secret engine
                    if (Array.isArray(actualParams)) {
                        throw this.error(_errors['vault.invalid'](), $meta);
                    }
                    const {mount_point} = actualParams;
                    if (!mount_point) {
                        throw this.error(_errors['vault.missingKey']({key: 'mount_point'}), $meta);
                    }

                    try {
                        return await this.config.context.vault!.unmount({mount_point});
                    } catch (error: unknown) {
                        throw this.error(_errors['vault.generic'](error), $meta);
                    }
                }
                case 'health': {
                    // Check Vault health
                    try {
                        return await this.config.context.vault!.health();
                    } catch (error: unknown) {
                        throw this.error(_errors['vault.generic'](error), $meta);
                    }
                }
                case 'status': {
                    // Get Vault status
                    try {
                        return await this.config.context.vault!.status();
                    } catch (error: unknown) {
                        throw this.error(_errors['vault.generic'](error), $meta);
                    }
                }
            }
            throw this.error(_errors['vault.generic']({}), $meta);
        },
    };
});