Skip to content

Commit 091bd10

Browse files
committed
Update: Add proxy support to the cli
1 parent 8e66f16 commit 091bd10

File tree

6 files changed

+28
-3
lines changed

6 files changed

+28
-3
lines changed

src/cli.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,8 @@ program
193193
'--space <space>',
194194
'the target Kibana spaces for the pushed monitors — spaces help you organise pushed monitors.'
195195
)
196+
.option('--proxy_uri <uri>', 'proxy uri to use when pushing to kibana')
197+
.option('--proxy_token <token>', 'auth token to use the proxy')
196198
.option('-y, --yes', 'skip all questions and run non-interactively')
197199
.addOption(pattern)
198200
.addOption(tags)

src/common_types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,8 @@ export type PushOptions = Partial<ProjectSettings> &
263263
retestOnFailure?: MonitorConfig['retestOnFailure'];
264264
enabled?: boolean;
265265
grepOpts?: GrepOptions;
266+
proxy_uri?: string;
267+
proxy_token?: string;
266268
};
267269

268270
export type ProjectSettings = {

src/locations/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ 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.
5253
});
5354
return resp.locations;
5455
}

src/push/kibana_api.ts

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

3939
// Default batch size for bulk put / delete
4040
export const BATCH_SIZE = parseInt(process.env.CHUNK_SIZE) || 250;
@@ -57,6 +57,7 @@ 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),
6061
});
6162

6263
const { failedMonitors } = resp;
@@ -103,6 +104,7 @@ const fetchMonitors = async (options: PushOptions, afterKey?: string) => {
103104
url,
104105
method: 'GET',
105106
auth: options.auth,
107+
proxyAgent: build_proxy_settings(options.proxy_uri, options.proxy_token),
106108
});
107109
return {
108110
afterKey: resp.after_key,
@@ -124,6 +126,7 @@ export async function bulkDeleteMonitors(
124126
method: 'DELETE',
125127
auth: options.auth,
126128
body: JSON.stringify({ monitors: monitorIDs }),
129+
proxyAgent: build_proxy_settings(options.proxy_uri, options.proxy_token),
127130
});
128131
}
129132

@@ -138,6 +141,7 @@ export async function getVersion(options: PushOptions) {
138141
url: generateURL(options, 'status'),
139142
method: 'GET',
140143
auth: options.auth,
144+
proxyAgent: build_proxy_settings(options?.proxy_uri, options?.proxy_token),
141145
});
142146

143147
return data.kibana.version;
@@ -169,6 +173,7 @@ export async function createMonitorsLegacy({
169173
method: 'PUT',
170174
auth: options.auth,
171175
body: JSON.stringify(schema),
176+
proxyAgent: build_proxy_settings(options.proxy_uri, options.proxy_token),
172177
});
173178

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

src/push/request.ts

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

3434
export type APIRequestOptions = {
35+
proxyAgent: Dispatcher;
3536
url: string;
3637
method: Dispatcher.HttpMethod;
3738
auth: string;
3839
body?: string;
3940
};
4041

4142
export async function sendRequest(options: APIRequestOptions) {
42-
return await request(options.url, {
43+
const request_options = {
4344
method: options.method,
4445
body: options.body,
4546
headers: {
@@ -51,7 +52,9 @@ export async function sendRequest(options: APIRequestOptions) {
5152
},
5253
// align with the default timeout of the kibana route
5354
headersTimeout: 2 * 60 * 1000,
54-
});
55+
};
56+
if (options.proxyAgent) request_options['dispatcher'] = options.proxyAgent;
57+
return await request(options.url, request_options);
5558
}
5659

5760
export async function sendReqAndHandleError<T>(

src/push/utils.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ 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';
3132

3233
export function logDiff<T extends Set<string>>(
3334
newIDs: T,
@@ -199,3 +200,14 @@ export function normalizeMonitorName(p: string, replacement = '_') {
199200
p = p.replace(/[:]+/g, replacement);
200201
return p;
201202
}
203+
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;
213+
}

0 commit comments

Comments
 (0)