Skip to content

Commit

Permalink
chore: upgrade axios to 1.4, adjust to header usage, and config optio…
Browse files Browse the repository at this point in the history
…nality
  • Loading branch information
delanni committed Aug 14, 2023
1 parent f09a5c9 commit 794be75
Show file tree
Hide file tree
Showing 16 changed files with 54 additions and 55 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -801,7 +801,7 @@
"antlr4ts": "^0.5.0-alpha.3",
"archiver": "^5.3.1",
"async": "^3.2.3",
"axios": "^0.27.2",
"axios": "^1.4.0",
"base64-js": "^1.3.1",
"bitmap-sdf": "^1.0.3",
"blurhash": "^2.0.1",
Expand Down
5 changes: 1 addition & 4 deletions packages/kbn-ci-stats-reporter/src/ci_stats_reporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@ import { REPO_ROOT, kibanaPackageJson } from '@kbn/repo-info';
import { parseConfig, Config, CiStatsMetadata } from '@kbn/ci-stats-core';
import type { SomeDevLog } from '@kbn/some-dev-log';

// @ts-expect-error not "public", but necessary to prevent Jest shimming from breaking things
import httpAdapter from 'axios/lib/adapters/http';

import type { CiStatsTestGroupInfo, CiStatsTestRun } from './ci_stats_test_group_types';

const BASE_URL = 'https://ci-stats.kibana.dev';
Expand Down Expand Up @@ -375,7 +372,7 @@ export class CiStatsReporter {
headers,
data: body,
params: query,
adapter: httpAdapter,
adapter: 'http',

// if it can be serialized into a string, send it
maxBodyLength: Infinity,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,13 @@

import Url from 'url';

import Axios, { AxiosRequestConfig, AxiosInstance } from 'axios';
import Axios, {
AxiosRequestConfig,
AxiosInstance,
AxiosHeaders,
RawAxiosResponseHeaders,
AxiosResponseHeaders,
} from 'axios';
import { isAxiosResponseError, isAxiosRequestError } from '@kbn/dev-utils';
import { ToolingLog } from '@kbn/tooling-log';

Expand Down Expand Up @@ -130,7 +136,7 @@ export class GithubApi {
): Promise<{
status: number;
statusText: string;
headers: Record<string, string | string[] | undefined>;
headers: RawAxiosResponseHeaders | AxiosResponseHeaders;
data: T;
}> {
const executeRequest = !this.dryRun || options.safeForDryRun;
Expand All @@ -145,7 +151,7 @@ export class GithubApi {
return {
status: 200,
statusText: 'OK',
headers: {},
headers: new AxiosHeaders(),
data: dryRunResponse,
};
}
Expand All @@ -158,7 +164,7 @@ export class GithubApi {
const githubApiFailed = isAxiosResponseError(error) && error.response.status >= 500;
const errorResponseLog =
isAxiosResponseError(error) &&
`[${error.config.method} ${error.config.url}] ${error.response.status} ${error.response.statusText} Error`;
`[${error.config?.method} ${error.config?.url}] ${error.response.status} ${error.response.statusText} Error`;

if ((unableToReachGithub || githubApiFailed) && attempt < maxAttempts) {
const waitMs = 1000 * attempt;
Expand Down
2 changes: 1 addition & 1 deletion packages/kbn-test/src/kbn_client/kbn_client_requester.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { KbnClientRequesterError } from './kbn_client_requester_error';

const isConcliftOnGetError = (error: any) => {
return (
isAxiosResponseError(error) && error.config.method === 'GET' && error.response.status === 409
isAxiosResponseError(error) && error.config?.method === 'GET' && error.response.status === 409
);
};

Expand Down
8 changes: 2 additions & 6 deletions src/dev/build/lib/download.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@ import Axios from 'axios';
import { isAxiosResponseError } from '@kbn/dev-utils';
import { ToolingLog } from '@kbn/tooling-log';

// https://github.com/axios/axios/tree/ffea03453f77a8176c51554d5f6c3c6829294649/lib/adapters
// @ts-expect-error untyped internal module used to prevent axios from using xhr adapter in tests
import AxiosHttpAdapter from 'axios/lib/adapters/http';

import { mkdirp } from './fs';

function tryUnlink(path: string) {
Expand Down Expand Up @@ -78,7 +74,7 @@ export async function downloadToDisk({
const response = await Axios.request({
url,
responseType: 'stream',
adapter: AxiosHttpAdapter,
adapter: 'http',
});

if (response.status !== 200) {
Expand Down Expand Up @@ -171,7 +167,7 @@ export async function downloadToString({
const resp = await Axios.request<string>({
url,
method: 'GET',
adapter: AxiosHttpAdapter,
adapter: 'http',
responseType: 'text',
validateStatus: !expectStatus ? undefined : (status) => status === expectStatus,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,6 @@ const UNAUTHORIZED_CA = fsReadFileSync(UNAUTHORIZED_CA_FILE);
const Auth = 'elastic:changeme';
const AuthB64 = Buffer.from(Auth).toString('base64');

// eslint-disable-next-line @typescript-eslint/no-var-requires
const AxiosDefaultsAadapter = require('axios/lib/adapters/http');

describe('axios connections', () => {
let testServer: http.Server | https.Server | null;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand All @@ -65,7 +62,7 @@ describe('axios connections', () => {
// needed to prevent the dreaded Error: Cross origin http://localhost forbidden
// see: https://github.com/axios/axios/issues/1754#issuecomment-572778305
savedAxiosDefaultsAdapter = axios.defaults.adapter;
axios.defaults.adapter = AxiosDefaultsAadapter;
axios.defaults.adapter = 'http';
});

afterEach(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,6 @@ const CA = fsReadFileSync(CA_FILE, 'utf8');
const Auth = 'elastic:changeme';
const AuthB64 = Buffer.from(Auth).toString('base64');

// eslint-disable-next-line @typescript-eslint/no-var-requires
const AxiosDefaultsAadapter = require('axios/lib/adapters/http');

const ServerResponse = 'A unique response returned by the server!';

describe('axios connections', () => {
Expand All @@ -53,7 +50,7 @@ describe('axios connections', () => {
// needed to prevent the dreaded Error: Cross origin http://localhost forbidden
// see: https://github.com/axios/axios/issues/1754#issuecomment-572778305
savedAxiosDefaultsAdapter = axios.defaults.adapter;
axios.defaults.adapter = AxiosDefaultsAadapter;
axios.defaults.adapter = 'http';
});

afterEach(() => {
Expand Down
12 changes: 8 additions & 4 deletions x-pack/plugins/actions/server/lib/axios_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/

import { isObjectLike, isEmpty } from 'lodash';
import { AxiosInstance, Method, AxiosResponse, AxiosRequestConfig } from 'axios';
import { AxiosInstance, Method, AxiosResponse, AxiosRequestConfig, AxiosHeaders } from 'axios';
import { Logger } from '@kbn/core/server';
import { getCustomAgents } from './get_custom_agents';
import { ActionsConfigurationUtilities } from '../actions_config';
Expand All @@ -29,7 +29,7 @@ export const request = async <T = unknown>({
method?: Method;
data?: T;
configurationUtilities: ActionsConfigurationUtilities;
headers?: Record<string, string> | null;
headers?: AxiosHeaders;
sslOverrides?: SSLSettings;
} & AxiosRequestConfig): Promise<AxiosResponse> => {
const { httpAgent, httpsAgent } = getCustomAgents(
Expand All @@ -44,7 +44,7 @@ export const request = async <T = unknown>({
...config,
method,
// Axios doesn't support `null` value for `headers` property.
headers: headers ?? undefined,
headers,
data: data ?? {},
// use httpAgent and httpsAgent and set axios proxy: false, to be able to handle fail on invalid certs
httpAgent,
Expand Down Expand Up @@ -149,6 +149,10 @@ export const createAxiosResponse = (res: Partial<AxiosResponse>): AxiosResponse
status: 200,
statusText: 'OK',
headers: { ['content-type']: 'application/json' },
config: { method: 'GET', url: 'https://example.com' },
config: {
method: 'GET',
url: 'https://example.com',
headers: new AxiosHeaders(),
},
...res,
});
6 changes: 3 additions & 3 deletions x-pack/plugins/actions/server/lib/request_oauth_token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/

import qs from 'query-string';
import axios from 'axios';
import axios, { AxiosHeaders } from 'axios';
import stringify from 'json-stable-stringify';
import { Logger } from '@kbn/core/server';
import { request } from './axios_utils';
Expand Down Expand Up @@ -37,9 +37,9 @@ export async function requestOAuthToken<T>(
...bodyRequest,
grant_type: grantType,
}),
headers: {
headers: new AxiosHeaders({
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
},
}),
configurationUtilities,
validateStatus: () => true,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/

import axios, { AxiosError, AxiosInstance, AxiosResponse } from 'axios';
import axios, { AxiosError, AxiosHeaders, AxiosInstance, AxiosResponse } from 'axios';
import { loggingSystemMock } from '@kbn/core/server/mocks';
import { MockedLogger } from '@kbn/logging-mocks';
import { actionsConfigMock } from '../actions_config.mock';
Expand All @@ -29,7 +29,7 @@ const requestMock = utils.request as jest.Mock;
const createAxiosError = (): AxiosError => {
const error = new Error() as AxiosError;
error.isAxiosError = true;
error.config = { method: 'get', url: 'https://example.com' };
error.config = { method: 'get', url: 'https://example.com', headers: new AxiosHeaders() };
error.response = {
data: { errorMessage: 'An error occurred', errorCode: 500 },
} as AxiosResponse;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,13 @@
import { isPlainObject, isEmpty } from 'lodash';
import { Type } from '@kbn/config-schema';
import { Logger } from '@kbn/logging';
import axios, { AxiosInstance, AxiosResponse, AxiosError, AxiosRequestHeaders } from 'axios';
import axios, {
AxiosInstance,
AxiosResponse,
AxiosError,
AxiosRequestHeaders,
AxiosHeaders,
} from 'axios';
import { SavedObjectsClientContract } from '@kbn/core-saved-objects-api-server';
import { ElasticsearchClient } from '@kbn/core-elasticsearch-server';
import { assertURL } from './helpers/validators';
Expand All @@ -26,6 +32,7 @@ const isAxiosError = (error: unknown): error is AxiosError => (error as AxiosErr

export abstract class SubActionConnector<Config, Secrets> {
[k: string]: ((params: unknown) => unknown) | unknown;

private axiosInstance: AxiosInstance;
private subActions: Map<string, SubAction> = new Map();
private configurationUtilities: ActionsConfigurationUtilities;
Expand Down Expand Up @@ -74,7 +81,7 @@ export abstract class SubActionConnector<Config, Secrets> {
}

private getHeaders(headers?: AxiosRequestHeaders) {
return { ...headers, 'Content-Type': 'application/json' };
return headers?.set('Content-Type', 'application/json');
}

private validateResponse(responseSchema: Type<unknown>, data: unknown) {
Expand Down Expand Up @@ -128,7 +135,7 @@ export abstract class SubActionConnector<Config, Secrets> {
method,
data: this.normalizeData(data),
configurationUtilities: this.configurationUtilities,
headers: this.getHeaders(headers),
headers: this.getHeaders(headers as AxiosHeaders),
});

this.validateResponse(responseSchema, res.data);
Expand All @@ -137,7 +144,7 @@ export abstract class SubActionConnector<Config, Secrets> {
} catch (error) {
if (isAxiosError(error)) {
this.logger.debug(
`Request to external service failed. Connector Id: ${this.connector.id}. Connector type: ${this.connector.type}. Method: ${error.config.method}. URL: ${error.config.url}`
`Request to external service failed. Connector Id: ${this.connector.id}. Connector type: ${this.connector.type}. Method: ${error.config?.method}. URL: ${error.config?.url}`
);

const errorMessage = `Status code: ${
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ init().catch((e) => {
console.error(e.message);
} else if (isAxiosError(e)) {
console.error(
`${e.config.method?.toUpperCase() || 'GET'} ${e.config.url} (Code: ${
`${e.config?.method?.toUpperCase() || 'GET'} ${e.config?.url} (Code: ${
e.response?.status
})`
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,17 @@ export async function getKibanaVersion({
switch (e.response?.status) {
case 401:
throw new AbortError(
`Could not access Kibana with the provided credentials. Username: "${e.config.auth?.username}". Password: "${e.config.auth?.password}"`
`Could not access Kibana with the provided credentials. Username: "${e.config?.auth?.username}". Password: "${e.config.auth?.password}"`
);

case 404:
throw new AbortError(
`Could not get version on ${e.config.url} (Code: 404)`
`Could not get version on ${e.config?.url} (Code: 404)`
);

default:
throw new AbortError(
`Cannot access Kibana on ${e.config.baseURL}. Please specify Kibana with: "--kibana-url <url>"`
`Cannot access Kibana on ${e.config?.baseURL}. Please specify Kibana with: "--kibana-url <url>"`
);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ describe.each(packageInfos)('Chromium archive: %s/%s', (architecture, platform)

const originalAxios = axios.defaults.adapter;
beforeAll(async () => {
axios.defaults.adapter = require('axios/lib/adapters/http'); // allow Axios to send actual requests
axios.defaults.adapter = 'http';
});

afterAll(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/

import { AxiosRequestHeaders, AxiosResponse, ResponseType } from 'axios';
import { AxiosHeaders, AxiosRequestHeaders, AxiosResponse, ResponseType } from 'axios';
import { IncomingMessage } from 'http';
import { OpenAiProviderType } from '../../../../common/gen_ai/constants';
import {
Expand Down Expand Up @@ -82,16 +82,19 @@ export const getAxiosOptions = (
switch (provider) {
case OpenAiProviderType.OpenAi:
return {
headers: { Authorization: `Bearer ${apiKey}`, ['content-type']: 'application/json' },
headers: new AxiosHeaders({
Authorization: `Bearer ${apiKey}`,
['content-type']: 'application/json',
}),
...responseType,
};
case OpenAiProviderType.AzureAi:
return {
headers: { ['api-key']: apiKey, ['content-type']: 'application/json' },
headers: new AxiosHeaders({ ['api-key']: apiKey, ['content-type']: 'application/json' }),
...responseType,
};
default:
return { headers: {} };
return { headers: new AxiosHeaders() };
}
};

Expand Down
10 changes: 1 addition & 9 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -11293,14 +11293,6 @@ axios@^0.26.0:
dependencies:
follow-redirects "^1.14.8"

axios@^0.27.2:
version "0.27.2"
resolved "https://registry.yarnpkg.com/axios/-/axios-0.27.2.tgz#207658cc8621606e586c85db4b41a750e756d972"
integrity sha512-t+yRIyySRTp/wua5xEr+z1q60QmLq8ABsS5O9Me1AsE5dfKqgnCFzwiCZZ/cGNd1lq4/7akDWMxdhVlucjmnOQ==
dependencies:
follow-redirects "^1.14.9"
form-data "^4.0.0"

axios@^1.3.4, axios@^1.4.0:
version "1.4.0"
resolved "https://registry.yarnpkg.com/axios/-/axios-1.4.0.tgz#38a7bf1224cd308de271146038b551d725f0be1f"
Expand Down Expand Up @@ -16679,7 +16671,7 @@ folktale@2.3.2:
resolved "https://registry.yarnpkg.com/folktale/-/folktale-2.3.2.tgz#38231b039e5ef36989920cbf805bf6b227bf4fd4"
integrity sha512-+8GbtQBwEqutP0v3uajDDoN64K2ehmHd0cjlghhxh0WpcfPzAIjPA03e1VvHlxL02FVGR0A6lwXsNQKn3H1RNQ==

follow-redirects@^1.0.0, follow-redirects@^1.14.0, follow-redirects@^1.14.8, follow-redirects@^1.14.9, follow-redirects@^1.15.0:
follow-redirects@^1.0.0, follow-redirects@^1.14.0, follow-redirects@^1.14.8, follow-redirects@^1.15.0:
version "1.15.2"
resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.15.2.tgz#b460864144ba63f2681096f274c4e57026da2c13"
integrity sha512-VQLG33o04KaQ8uYi2tVNbdrWp1QWxNNea+nmIB4EVM28v0hmP17z7aG1+wAkNzVq4KeXTq3221ye5qTJP91JwA==
Expand Down

0 comments on commit 794be75

Please sign in to comment.