Skip to content

Commit 1a3a08b

Browse files
committed
Add initial proxy support for push command
1 parent 091bd10 commit 1a3a08b

File tree

9 files changed

+94
-41
lines changed

9 files changed

+94
-41
lines changed

package-lock.json

Lines changed: 5 additions & 17 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@
7474
"sonic-boom": "^3.3.0",
7575
"source-map-support": "^0.5.21",
7676
"stack-utils": "^2.0.6",
77-
"undici": "^5.29.0",
77+
"undici": "^7.11.0",
7878
"unzip-stream": "^0.3.4",
7979
"yaml": "^2.2.2"
8080
},

src/cli.ts

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ import { createLightweightMonitors } from './push/monitor';
5454
import { getVersion } from './push/kibana_api';
5555
import { installTransform } from './core/transform';
5656
import { totp, TOTPCmdOptions } from './core/mfa';
57+
import { setGlobalProxy } from './push/utils';
5758

5859
/* eslint-disable-next-line @typescript-eslint/no-var-requires */
5960
const { name, version } = require('../package.json');
@@ -68,6 +69,11 @@ const {
6869
tags,
6970
match,
7071
fields,
72+
proxyToken,
73+
proxyUri,
74+
proxyNoVerify,
75+
proxyCa,
76+
proxyCert,
7177
} = getCommonCommandOpts();
7278

7379
program
@@ -193,9 +199,12 @@ program
193199
'--space <space>',
194200
'the target Kibana spaces for the pushed monitors — spaces help you organise pushed monitors.'
195201
)
196-
.option('--proxy_uri <uri>', 'proxy uri to use when pushing to kibana')
197-
.option('--proxy_token <token>', 'auth token to use the proxy')
198202
.option('-y, --yes', 'skip all questions and run non-interactively')
203+
.addOption(proxyUri)
204+
.addOption(proxyToken)
205+
.addOption(proxyNoVerify)
206+
.addOption(proxyCa)
207+
.addOption(proxyCert)
199208
.addOption(pattern)
200209
.addOption(tags)
201210
.addOption(fields)
@@ -218,6 +227,16 @@ program
218227
},
219228
'push'
220229
)) as PushOptions;
230+
231+
//Set up global proxy agent if any of the related options are set
232+
setGlobalProxy(
233+
options.proxyUri,
234+
options.proxyToken,
235+
options.proxyNoVerify ? false : true,
236+
options.proxyCa,
237+
options.proxyCert
238+
);
239+
221240
await validatePush(options, settings);
222241
const monitors = runner._buildMonitors(options);
223242
if ((options as CliArgs).throttling == null) {

src/common_types.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -263,8 +263,11 @@ export type PushOptions = Partial<ProjectSettings> &
263263
retestOnFailure?: MonitorConfig['retestOnFailure'];
264264
enabled?: boolean;
265265
grepOpts?: GrepOptions;
266-
proxy_uri?: string;
267-
proxy_token?: string;
266+
proxyUri?: string;
267+
proxyToken?: string;
268+
proxyCa?: string;
269+
proxyCert?: string;
270+
proxyNoVerify?: string;
268271
};
269272

270273
export type ProjectSettings = {

src/locations/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ export async function getLocations(options: LocationCmdOptions) {
4949
url: generateURL(options, 'location'),
5050
method: 'GET',
5151
auth: options.auth,
52-
proxyAgent: null, // for now we don't support proxy with this.
5352
});
5453
return resp.locations;
5554
}

src/options.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ import { createOption } from 'commander';
2828
import { readConfig } from './config';
2929
import type { CliArgs, RunOptions } from './common_types';
3030
import { THROTTLING_WARNING_MSG, warn } from './helpers';
31+
import path from 'path';
32+
import { existsSync, readFileSync } from 'fs';
3133

3234
type Mode = 'run' | 'push';
3335

@@ -255,6 +257,27 @@ export function getCommonCommandOpts() {
255257
}, {});
256258
});
257259

260+
const proxyUri = createOption(
261+
'--proxy-uri <uri>',
262+
'proxy uri to use when pushing to kibana'
263+
);
264+
const proxyToken = createOption(
265+
'--proxy-token <token>',
266+
'auth token to use the proxy'
267+
);
268+
const proxyCa = createOption(
269+
'--proxy-ca <path>',
270+
'provide a CA override for proxy endpoint, as a path to be loaded or as string '
271+
).argParser(parseFileOption);
272+
const proxyCert = createOption(
273+
'--proxy-cert <path>',
274+
'provide a cert override for proxy endpoint, as a path to be loaded or as string '
275+
).argParser(parseFileOption);
276+
const proxyNoVerify = createOption(
277+
'--proxy-no-verify',
278+
'disable TLS verification for the proxy connection'
279+
);
280+
258281
return {
259282
auth,
260283
authMandatory,
@@ -265,6 +288,11 @@ export function getCommonCommandOpts() {
265288
tags,
266289
match,
267290
fields,
291+
proxyUri,
292+
proxyToken,
293+
proxyCa,
294+
proxyCert,
295+
proxyNoVerify,
268296
};
269297
}
270298

@@ -299,3 +327,17 @@ function parseAsBuffer(value: any): Buffer {
299327
return value;
300328
}
301329
}
330+
331+
function parseFileOption(value: string) {
332+
const filePath = path.resolve(value);
333+
334+
if (!existsSync(filePath)) {
335+
return value;
336+
}
337+
338+
try {
339+
return readFileSync(filePath);
340+
} catch (e) {
341+
throw new Error(`could not be read provided path ${filePath}: ${e}`);
342+
}
343+
}

src/push/kibana_api.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ import {
3434
sendRequest,
3535
APIMonitorError,
3636
} from './request';
37-
import { build_proxy_settings, generateURL } from './utils';
37+
import { generateURL } from './utils';
3838

3939
// Default batch size for bulk put / delete
4040
export const BATCH_SIZE = parseInt(process.env.CHUNK_SIZE) || 250;
@@ -57,7 +57,6 @@ export async function bulkPutMonitors(
5757
method: 'PUT',
5858
auth: options.auth,
5959
body: JSON.stringify({ monitors: schemas }),
60-
proxyAgent: build_proxy_settings(options.proxy_uri, options.proxy_token),
6160
});
6261

6362
const { failedMonitors } = resp;
@@ -104,7 +103,6 @@ const fetchMonitors = async (options: PushOptions, afterKey?: string) => {
104103
url,
105104
method: 'GET',
106105
auth: options.auth,
107-
proxyAgent: build_proxy_settings(options.proxy_uri, options.proxy_token),
108106
});
109107
return {
110108
afterKey: resp.after_key,
@@ -126,7 +124,6 @@ export async function bulkDeleteMonitors(
126124
method: 'DELETE',
127125
auth: options.auth,
128126
body: JSON.stringify({ monitors: monitorIDs }),
129-
proxyAgent: build_proxy_settings(options.proxy_uri, options.proxy_token),
130127
});
131128
}
132129

@@ -141,7 +138,6 @@ export async function getVersion(options: PushOptions) {
141138
url: generateURL(options, 'status'),
142139
method: 'GET',
143140
auth: options.auth,
144-
proxyAgent: build_proxy_settings(options?.proxy_uri, options?.proxy_token),
145141
});
146142

147143
return data.kibana.version;
@@ -173,7 +169,6 @@ export async function createMonitorsLegacy({
173169
method: 'PUT',
174170
auth: options.auth,
175171
body: JSON.stringify(schema),
176-
proxyAgent: build_proxy_settings(options.proxy_uri, options.proxy_token),
177172
});
178173

179174
const resBody = await handleError(statusCode, url, body);

src/push/request.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ import { indent, symbols } from '../helpers';
3232
const { version } = require('../../package.json');
3333

3434
export type APIRequestOptions = {
35-
proxyAgent: Dispatcher;
3635
url: string;
3736
method: Dispatcher.HttpMethod;
3837
auth: string;
@@ -53,7 +52,6 @@ export async function sendRequest(options: APIRequestOptions) {
5352
// align with the default timeout of the kibana route
5453
headersTimeout: 2 * 60 * 1000,
5554
};
56-
if (options.proxyAgent) request_options['dispatcher'] = options.proxyAgent;
5755
return await request(options.url, request_options);
5856
}
5957

src/push/utils.ts

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ import { progress, removeTrailingSlash } from '../helpers';
2828
import { green, red, grey, yellow, Colorize, bold } from 'kleur/colors';
2929
import { PushOptions } from '../common_types';
3030
import { Monitor } from '../dsl/monitor';
31-
import { ProxyAgent } from 'undici';
31+
import { ProxyAgent, setGlobalDispatcher } from 'undici';
32+
import { EnvHttpProxyAgent } from 'undici';
3233

3334
export function logDiff<T extends Set<string>>(
3435
newIDs: T,
@@ -201,13 +202,21 @@ export function normalizeMonitorName(p: string, replacement = '_') {
201202
return p;
202203
}
203204

204-
export function build_proxy_settings(proxy_uri: string, proxy_auth: string) {
205-
let proxyAgent = null;
206-
if (proxy_uri) {
207-
proxyAgent = new ProxyAgent({
208-
uri: proxy_uri,
209-
token: proxy_auth,
210-
});
211-
}
212-
return proxyAgent;
205+
export function setGlobalProxy(
206+
uri: string,
207+
token: string,
208+
rejectUnauthorized: boolean,
209+
ca: string,
210+
cert: string
211+
) {
212+
// Create proxy agent if options exist, if not attempt to load env proxy
213+
const proxyAgent = uri
214+
? new ProxyAgent({
215+
uri,
216+
token,
217+
proxyTls: { ca: ca ?? null, cert: cert ?? null, rejectUnauthorized },
218+
})
219+
: new EnvHttpProxyAgent();
220+
221+
setGlobalDispatcher(proxyAgent);
213222
}

0 commit comments

Comments
 (0)