Skip to content

Commit

Permalink
localhost/ssl support for scripts/synthtrace
Browse files Browse the repository at this point in the history
  • Loading branch information
walterra committed Jun 18, 2024
1 parent 20a066f commit c7a009f
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { Client } from '@elastic/elasticsearch';
import { ApmSynthtraceEsClient } from '../../..';
import { Logger } from '../../lib/utils/create_logger';
import { RunOptions } from './parse_run_cli_flags';
import { getEsClientTlsSettings } from './ssl';

export function getApmEsClient({
target,
Expand All @@ -23,6 +24,7 @@ export function getApmEsClient({
}) {
const client = new Client({
node: target,
tls: getEsClientTlsSettings(target),
});

const apmEsClient = new ApmSynthtraceEsClient({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { Client } from '@elastic/elasticsearch';
import { AssetsSynthtraceEsClient } from '../../lib/assets/assets_synthtrace_es_client';
import { Logger } from '../../lib/utils/create_logger';
import { RunOptions } from './parse_run_cli_flags';
import { getEsClientTlsSettings } from './ssl';

export function getAssetsEsClient({
target,
Expand All @@ -21,6 +22,7 @@ export function getAssetsEsClient({
}) {
const client = new Client({
node: target,
tls: getEsClientTlsSettings(target),
});

return new AssetsSynthtraceEsClient({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { Client } from '@elastic/elasticsearch';
import { InfraSynthtraceEsClient } from '../../lib/infra/infra_synthtrace_es_client';
import { Logger } from '../../lib/utils/create_logger';
import { RunOptions } from './parse_run_cli_flags';
import { getEsClientTlsSettings } from './ssl';

export function getInfraEsClient({
target,
Expand All @@ -21,6 +22,7 @@ export function getInfraEsClient({
}) {
const client = new Client({
node: target,
tls: getEsClientTlsSettings(target),
});

return new InfraSynthtraceEsClient({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { Client } from '@elastic/elasticsearch';
import { LogsSynthtraceEsClient } from '../../lib/logs/logs_synthtrace_es_client';
import { Logger } from '../../lib/utils/create_logger';
import { RunOptions } from './parse_run_cli_flags';
import { getEsClientTlsSettings } from './ssl';

export function getLogsEsClient({
target,
Expand All @@ -21,6 +22,7 @@ export function getLogsEsClient({
}) {
const client = new Client({
node: target,
tls: getEsClientTlsSettings(target),
});

return new LogsSynthtraceEsClient({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import fetch from 'node-fetch';
import { format, parse, Url } from 'url';
import { Logger } from '../../lib/utils/create_logger';
import { RunOptions } from './parse_run_cli_flags';
import { getFetchAgent } from './ssl';

async function discoverAuth(parsedTarget: Url) {
const possibleCredentials = [`admin:changeme`, `elastic:changeme`, `elastic_serverless:changeme`];
Expand All @@ -20,7 +21,9 @@ async function discoverAuth(parsedTarget: Url) {
});
let status: number;
try {
const response = await fetch(url);
const response = await fetch(url, {
agent: getFetchAgent(url),
});
status = response.status;
} catch (err) {
status = 0;
Expand All @@ -43,6 +46,7 @@ async function getKibanaUrl({ target, logger }: { target: string; logger: Logger
method: 'HEAD',
follow: 1,
redirect: 'manual',
agent: getFetchAgent(target),
});

const discoveredKibanaUrl =
Expand All @@ -62,6 +66,7 @@ async function getKibanaUrl({ target, logger }: { target: string; logger: Logger

const redirectedResponse = await fetch(discoveredKibanaUrlWithAuth, {
method: 'HEAD',
agent: getFetchAgent(discoveredKibanaUrlWithAuth),
});

if (redirectedResponse.status !== 200) {
Expand Down
31 changes: 31 additions & 0 deletions packages/kbn-apm-synthtrace/src/cli/utils/ssl.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import * as Fs from 'fs';
import { CA_CERT_PATH } from '@kbn/dev-utils';
import https from 'https';

export function getFetchAgent(url: string) {
const isHTTPS = new URL(url).protocol === 'https:';
const isLocalhost = new URL(url).hostname === 'localhost';
return isHTTPS && isLocalhost ? new https.Agent({ rejectUnauthorized: false }) : undefined;
}

export function getEsClientTlsSettings(url: string) {
const isHTTPS = new URL(url).protocol === 'https:';
// load the CA cert from disk if necessary
const caCert = isHTTPS ? Fs.readFileSync(CA_CERT_PATH) : null;
const isLocalhost = new URL(url).hostname === 'localhost';

return caCert && isLocalhost
? {
ca: caCert,
rejectUnauthorized: true,
}
: undefined;
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import fetch from 'node-fetch';
import pRetry from 'p-retry';
import { Logger } from '../../utils/create_logger';
import { kibanaHeaders } from '../../shared/client_headers';
import { getFetchAgent } from '../../../cli/utils/ssl';

export class ApmSynthtraceKibanaClient {
private readonly logger: Logger;
Expand All @@ -34,6 +35,7 @@ export class ApmSynthtraceKibanaClient {
const response = await fetch(url, {
method: 'GET',
headers: kibanaHeaders(),
agent: getFetchAgent(url),
});

const responseJson = await response.json();
Expand Down Expand Up @@ -62,6 +64,7 @@ export class ApmSynthtraceKibanaClient {
method: 'POST',
headers: kibanaHeaders(),
body: '{"force":true}',
agent: getFetchAgent(url),
});

if (!res.ok) {
Expand Down Expand Up @@ -109,6 +112,7 @@ export class ApmSynthtraceKibanaClient {
method: 'DELETE',
headers: kibanaHeaders(),
body: '{"force":true}',
agent: getFetchAgent(url),
});

if (!res.ok) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import fetch from 'node-fetch';
import pRetry from 'p-retry';
import { Logger } from '../utils/create_logger';
import { kibanaHeaders } from '../shared/client_headers';
import { getFetchAgent } from '../../cli/utils/ssl';

export class InfraSynthtraceKibanaClient {
private readonly logger: Logger;
Expand All @@ -30,6 +31,7 @@ export class InfraSynthtraceKibanaClient {
const response = await fetch(fleetPackageApiUrl, {
method: 'GET',
headers: kibanaHeaders(),
agent: getFetchAgent(fleetPackageApiUrl),
});

const responseJson = await response.json();
Expand All @@ -54,6 +56,7 @@ export class InfraSynthtraceKibanaClient {
method: 'POST',
headers: kibanaHeaders(),
body: '{"force":true}',
agent: getFetchAgent(url),
});
});

Expand Down

0 comments on commit c7a009f

Please sign in to comment.