Skip to content

Commit dc0e75f

Browse files
authored
fix(kbn-evals): default VAULT_ADDR and auto-login on init (#270194)
Engineers running `node scripts/evals init` previously had to manually export `VAULT_ADDR=https://secrets.elastic.co:8200` and pre-run `vault login --method oidc` before the script would work. Apply the Elastic Vault address as the default for all vault CLI invocations from kbn-evals, and auto-trigger an OIDC login when no valid token is present (matching the behavior already in kbn-es/eis_setup). ## Summary Summarize your PR. If it involves visual changes include a screenshot or gif. ### Checklist Check the PR satisfies following conditions. Reviewers should verify this PR satisfies this list as well. - [ ] Any text added follows [EUI's writing guidelines](https://elastic.github.io/eui/#/guidelines/writing), uses sentence case text and includes [i18n support](https://github.com/elastic/kibana/blob/main/src/platform/packages/shared/kbn-i18n/README.md) - [ ] [Documentation](https://www.elastic.co/guide/en/kibana/master/development-documentation.html) was added for features that require explanation or tutorials - [ ] [Unit or functional tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html) were updated or added to match the most common scenarios - [ ] If a plugin configuration key changed, check if it needs to be allowlisted in the cloud and added to the [docker list](https://github.com/elastic/kibana/blob/main/src/dev/build/tasks/os_packages/docker_generator/resources/base/bin/kibana-docker) - [ ] This was checked for breaking HTTP API changes, and any breaking changes have been approved by the breaking-change committee. The `release_note:breaking` label should be applied in these situations. - [ ] [Flaky Test Runner](https://ci-stats.kibana.dev/trigger_flaky_test_runner/1) was used on any tests changed - [ ] The PR description includes the appropriate Release Notes section, and the correct `release_note:*` label is applied per the [guidelines](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process) - [ ] Review the [backport guidelines](https://docs.google.com/document/d/1VyN5k91e5OVumlc0Gb9RPa3h1ewuPE705nRtioPiTvY/edit?usp=sharing) and apply applicable `backport:*` labels. ### Identify risks Does this PR introduce any risks? For example, consider risks like hard to test bugs, performance regression, potential of data loss. Describe the risk, its severity, and mitigation for each identified risk. Invite stakeholders and evaluate how to proceed before merging. - [ ] [See some risk examples](https://github.com/elastic/kibana/blob/main/RISK_MATRIX.mdx) - [ ] ...
1 parent c01d094 commit dc0e75f

3 files changed

Lines changed: 68 additions & 11 deletions

File tree

x-pack/platform/packages/shared/kbn-evals/scripts/vault/manage_secrets.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ import { writeFile, readFile } from 'fs/promises';
1212
import { REPO_ROOT } from '@kbn/repo-info';
1313
import { schema } from '@kbn/config-schema';
1414

15+
const DEFAULT_VAULT_ADDR = 'https://secrets.elastic.co:8200';
16+
const getVaultAddr = (): string => process.env.VAULT_ADDR || DEFAULT_VAULT_ADDR;
17+
1518
/**
1619
* Vault-backed config used by @kbn/evals CI.
1720
*
@@ -137,6 +140,10 @@ export const retrieveFromVault = async (vaultPath: string, filePath: string, fie
137140
const { stdout } = await execa('vault', ['read', `-field=${field}`, vaultPath], {
138141
cwd: REPO_ROOT,
139142
buffer: true,
143+
env: {
144+
...process.env,
145+
VAULT_ADDR: getVaultAddr(),
146+
},
140147
});
141148

142149
const value = Buffer.from(stdout, 'base64').toString('utf-8').trim();
@@ -160,6 +167,10 @@ export const uploadToVault = async (vaultPath: string, filePath: string, field:
160167
await execa('vault', ['write', vaultPath, `${field}=${asB64}`], {
161168
cwd: REPO_ROOT,
162169
buffer: true,
170+
env: {
171+
...process.env,
172+
VAULT_ADDR: getVaultAddr(),
173+
},
163174
});
164175
};
165176

x-pack/platform/packages/shared/kbn-evals/src/cli/commands/init.ts

Lines changed: 50 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@
55
* 2.0.
66
*/
77

8-
import { execSync, spawnSync } from 'child_process';
8+
import { execSync, spawnSync, spawn } from 'child_process';
99
import Fs from 'fs';
1010
import Os from 'os';
1111
import Path from 'path';
1212
import inquirer from 'inquirer';
1313
import type { Command } from '@kbn/dev-cli-runner';
1414
import { set } from '@kbn/safer-lodash-set';
1515
import { parseConnectorsFromEnv, parseConnectorsFromKibanaDevYml } from '../prompts';
16-
import { safeExec, VAULT_SECRET_PATH } from '../utils';
16+
import { safeExec, getVaultAddr, VAULT_SECRET_PATH } from '../utils';
1717
import { VAULT_CONFIG_DIR } from '../profiles';
1818
import { buildApiKeyPayload } from '../../api_key/build_api_key_payload';
1919

@@ -380,6 +380,49 @@ const checkVaultAuth = (): boolean => {
380380
return safeExec('vault', ['token', 'lookup', '-format=json']) !== null;
381381
};
382382

383+
const vaultLogin = async (log: {
384+
info: (msg: string) => void;
385+
error: (msg: string) => void;
386+
}): Promise<void> => {
387+
const vaultAddr = getVaultAddr();
388+
log.info(`Launching Vault OIDC login against ${vaultAddr}...`);
389+
log.info('A browser window should open. If it does not, follow the URL printed below.');
390+
391+
const child = spawn('vault', ['login', '--method', 'oidc'], {
392+
env: { ...process.env, VAULT_ADDR: vaultAddr },
393+
stdio: 'inherit',
394+
});
395+
396+
const exitCode = await new Promise<number | null>((resolve, reject) => {
397+
child.on('error', (err) => reject(err));
398+
child.on('exit', (code) => resolve(code));
399+
});
400+
401+
if (exitCode !== 0) {
402+
throw new Error(
403+
[
404+
`Vault login failed (exit code ${exitCode}).`,
405+
'',
406+
'See https://docs.elastic.dev/vault for setup instructions.',
407+
].join('\n')
408+
);
409+
}
410+
};
411+
412+
const ensureVaultAuth = async (log: {
413+
info: (msg: string) => void;
414+
error: (msg: string) => void;
415+
}): Promise<void> => {
416+
if (checkVaultAuth()) {
417+
return;
418+
}
419+
log.info('Vault session expired or not found — attempting login...');
420+
await vaultLogin(log);
421+
if (!checkVaultAuth()) {
422+
throw new Error('Vault authentication still failing after login attempt.');
423+
}
424+
};
425+
383426
const fetchCcmApiKey = (log: { info: (msg: string) => void }): string => {
384427
log.info('Fetching EIS CCM API key from Vault...');
385428
const result = safeExec('vault', ['read', '-field=key', VAULT_SECRET_PATH]);
@@ -560,15 +603,11 @@ export const initCmd: Command<void> = {
560603
// --- EIS path ---
561604
log.info('');
562605

563-
// 1. Check Vault auth
564-
log.info('Checking Vault auth...');
565-
if (!checkVaultAuth()) {
566-
log.error('Vault authentication failed. Please log in first:');
567-
log.error(' vault login --method oidc');
568-
log.error('');
569-
log.error('Then re-run: node scripts/evals init');
570-
throw new Error('Vault authentication required');
571-
}
606+
const vaultAddr = getVaultAddr();
607+
608+
// 1. Check Vault auth (auto-login if not authenticated)
609+
log.info(`Checking Vault auth (VAULT_ADDR=${vaultAddr})...`);
610+
await ensureVaultAuth(log);
572611
log.info('Vault auth OK');
573612

574613
// 2. Fetch CCM API key

x-pack/platform/packages/shared/kbn-evals/src/cli/utils.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,19 @@
88
import { execFileSync } from 'child_process';
99

1010
export const VAULT_SECRET_PATH = 'secret/kibana-issues/dev/inference/kibana-eis-ccm';
11+
export const DEFAULT_VAULT_ADDR = 'https://secrets.elastic.co:8200';
12+
13+
export const getVaultAddr = (): string => process.env.VAULT_ADDR || DEFAULT_VAULT_ADDR;
1114

1215
export const safeExec = (command: string, args: string[]): string | null => {
1316
try {
1417
return execFileSync(command, args, {
1518
encoding: 'utf8',
1619
stdio: ['ignore', 'pipe', 'ignore'],
20+
env: {
21+
...process.env,
22+
VAULT_ADDR: getVaultAddr(),
23+
},
1724
}).trim();
1825
} catch {
1926
return null;

0 commit comments

Comments
 (0)