Skip to content

feat: proxy support to the cli #1034

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,8 @@ program
'--space <space>',
'the target Kibana spaces for the pushed monitors — spaces help you organise pushed monitors.'
)
.option('--proxy_uri <uri>', 'proxy uri to use when pushing to kibana')
.option('--proxy_token <token>', 'auth token to use the proxy')
.option('-y, --yes', 'skip all questions and run non-interactively')
.addOption(pattern)
.addOption(tags)
Expand Down
2 changes: 2 additions & 0 deletions src/common_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,8 @@ export type PushOptions = Partial<ProjectSettings> &
retestOnFailure?: MonitorConfig['retestOnFailure'];
enabled?: boolean;
grepOpts?: GrepOptions;
proxy_uri?: string;
proxy_token?: string;
};

export type ProjectSettings = {
Expand Down
1 change: 1 addition & 0 deletions src/locations/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export async function getLocations(options: LocationCmdOptions) {
url: generateURL(options, 'location'),
method: 'GET',
auth: options.auth,
proxyAgent: null, // for now we don't support proxy with this.
});
return resp.locations;
}
Expand Down
7 changes: 6 additions & 1 deletion src/push/kibana_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import {
sendRequest,
APIMonitorError,
} from './request';
import { generateURL } from './utils';
import { build_proxy_settings, generateURL } from './utils';

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

const { failedMonitors } = resp;
Expand Down Expand Up @@ -103,6 +104,7 @@ const fetchMonitors = async (options: PushOptions, afterKey?: string) => {
url,
method: 'GET',
auth: options.auth,
proxyAgent: build_proxy_settings(options.proxy_uri, options.proxy_token),
});
return {
afterKey: resp.after_key,
Expand All @@ -124,6 +126,7 @@ export async function bulkDeleteMonitors(
method: 'DELETE',
auth: options.auth,
body: JSON.stringify({ monitors: monitorIDs }),
proxyAgent: build_proxy_settings(options.proxy_uri, options.proxy_token),
});
}

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

return data.kibana.version;
Expand Down Expand Up @@ -169,6 +173,7 @@ export async function createMonitorsLegacy({
method: 'PUT',
auth: options.auth,
body: JSON.stringify(schema),
proxyAgent: build_proxy_settings(options.proxy_uri, options.proxy_token),
});

const resBody = await handleError(statusCode, url, body);
Expand Down
7 changes: 5 additions & 2 deletions src/push/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,15 @@ import { indent, symbols } from '../helpers';
const { version } = require('../../package.json');

export type APIRequestOptions = {
proxyAgent: Dispatcher;
url: string;
method: Dispatcher.HttpMethod;
auth: string;
body?: string;
};

export async function sendRequest(options: APIRequestOptions) {
return await request(options.url, {
const request_options = {
method: options.method,
body: options.body,
headers: {
Expand All @@ -51,7 +52,9 @@ export async function sendRequest(options: APIRequestOptions) {
},
// align with the default timeout of the kibana route
headersTimeout: 2 * 60 * 1000,
});
};
if (options.proxyAgent) request_options['dispatcher'] = options.proxyAgent;
return await request(options.url, request_options);
}

export async function sendReqAndHandleError<T>(
Expand Down
12 changes: 12 additions & 0 deletions src/push/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import { progress, removeTrailingSlash } from '../helpers';
import { green, red, grey, yellow, Colorize, bold } from 'kleur/colors';
import { PushOptions } from '../common_types';
import { Monitor } from '../dsl/monitor';
import { ProxyAgent } from 'undici';

export function logDiff<T extends Set<string>>(
newIDs: T,
Expand Down Expand Up @@ -199,3 +200,14 @@ export function normalizeMonitorName(p: string, replacement = '_') {
p = p.replace(/[:]+/g, replacement);
return p;
}

export function build_proxy_settings(proxy_uri: string, proxy_auth: string) {
let proxyAgent = null;
if (proxy_uri) {
proxyAgent = new ProxyAgent({
uri: proxy_uri,
token: proxy_auth,
});
}
return proxyAgent;
}
Loading