Skip to content

Commit

Permalink
remove circular dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
parkiino committed Sep 30, 2020
1 parent 575282e commit 369318c
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import {
} from '../../../ingest_manager/common';
import { factory as policyConfigFactory } from './models/policy_config';
import { HostMetadata } from './types';
import { KbnClientWithApiKeySupport } from '../../scripts/endpoint/resolver_generator_script';
import { KbnClientWithApiKeySupport } from '../../scripts/endpoint/kbn_client_with_api_key_support';

export async function indexHostsAndAlerts(
client: Client,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import { KbnClient, ToolingLog } from '@kbn/dev-utils';
import { KibanaConfig } from '@kbn/dev-utils/target/kbn_client/kbn_client_requester';
import fetch, { RequestInit as FetchRequestInit } from 'node-fetch';

export class KbnClientWithApiKeySupport extends KbnClient {
private kibanaUrlNoAuth: string;
constructor(log: ToolingLog, kibanaConfig: KibanaConfig) {
super(log, kibanaConfig);
const kibanaUrl = this.resolveUrl(kibanaConfig.url);
const matches = kibanaUrl.match(/(https?:\/\/)(.*\:.*\@)(.*)/);
// strip auth from url
this.kibanaUrlNoAuth =
matches && matches.length >= 3
? matches[1] + matches[3].replace('/', '')
: kibanaUrl.replace('/', '');
}
/**
* The fleet api to enroll and agent requires an api key when you mke the request, however KbnClient currently does not support sending an api key with the request. This function allows you to send an api key with a request.
*/
requestWithApiKey(path: string, init?: RequestInit | undefined): Promise<Response> {
return (fetch(
`${this.kibanaUrlNoAuth}${path}`,
init as FetchRequestInit
) as unknown) as Promise<Response>;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,38 +9,14 @@ import { Client, ClientOptions } from '@elastic/elasticsearch';
import { ResponseError } from '@elastic/elasticsearch/lib/errors';
import { KbnClient, ToolingLog } from '@kbn/dev-utils';
import { AxiosResponse } from 'axios';
import { KibanaConfig } from '@kbn/dev-utils/target/kbn_client/kbn_client_requester';
import fetch, { RequestInit as FetchRequestInit } from 'node-fetch';
import { indexHostsAndAlerts } from '../../common/endpoint/index_data';
import { ANCESTRY_LIMIT } from '../../common/endpoint/generate_data';
import { FLEET_SETUP_API_ROUTES, SETUP_API_ROUTE } from '../../../ingest_manager/common/constants';
import {
CreateFleetSetupResponse,
PostIngestSetupResponse,
} from '../../../ingest_manager/common/types/rest_spec';

export class KbnClientWithApiKeySupport extends KbnClient {
private kibanaUrlNoAuth: string;
constructor(log: ToolingLog, kibanaConfig: KibanaConfig) {
super(log, kibanaConfig);
const kibanaUrl = this.resolveUrl(kibanaConfig.url);
const matches = kibanaUrl.match(/(https?:\/\/)(.*\:.*\@)(.*)/);
// strip auth from url
this.kibanaUrlNoAuth =
matches && matches.length >= 3
? matches[1] + matches[3].replace('/', '')
: kibanaUrl.replace('/', '');
}
/**
* The fleet api to enroll and agent requires an api key when you mke the request, however KbnClient currently does not support sending an api key with the request. This function allows you to send an api key with a request.
*/
requestWithApiKey(path: string, init?: RequestInit | undefined): Promise<Response> {
return (fetch(
`${this.kibanaUrlNoAuth}${path}`,
init as FetchRequestInit
) as unknown) as Promise<Response>;
}
}
import { KbnClientWithApiKeySupport } from './kbn_client_with_api_key_support';

main();

Expand Down

0 comments on commit 369318c

Please sign in to comment.