Skip to content

Commit

Permalink
[7.17] [Ops] Upgrade to axios 1.4 (#163732) (#164418)
Browse files Browse the repository at this point in the history
# Backport

This will backport the following commits from `main` to `7.17`:
- [[Ops] Upgrade to axios 1.4
(#163732)](#163732)

<!--- Backport version: 8.9.8 --->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT [{"author":{"name":"Alex
Szabo","email":"alex.szabo@elastic.co"},"sourceCommit":{"committedDate":"2023-08-22T09:47:48Z","message":"[Ops]
Upgrade to axios 1.4 (#163732)\n\n## Summary\r\nupgrade `axios` to
1.4\r\n\r\n- adjust to header usage, and config optionality\r\n- Axios'
adapters are now resolved from a string key by axios, no need\r\nto
import/instantiate adapters\r\n- most of the changed code stems from
changes in Axios' types\r\n - `response.config` is now optional\r\n-
there was a change in the type of AxiosHeaders
<->\r\nInternalAxiosHeaders\r\n\r\nCloses: #162661 \r\nCloses:
#162414\r\n\r\n---------\r\n\r\nCo-authored-by: Kibana Machine
<42973632+kibanamachine@users.noreply.github.com>","sha":"9d2696b5c5159870cecf02c9f23090b26c924c87","branchLabelMapping":{"^v8.11.0$":"main","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["Team:Operations","Team:APM","release_note:skip","backport:prev-MAJOR","v8.10.0","v7.17.13","v8.11.0"],"number":163732,"url":"#163732
Upgrade to axios 1.4 (#163732)\n\n## Summary\r\nupgrade `axios` to
1.4\r\n\r\n- adjust to header usage, and config optionality\r\n- Axios'
adapters are now resolved from a string key by axios, no need\r\nto
import/instantiate adapters\r\n- most of the changed code stems from
changes in Axios' types\r\n - `response.config` is now optional\r\n-
there was a change in the type of AxiosHeaders
<->\r\nInternalAxiosHeaders\r\n\r\nCloses: #162661 \r\nCloses:
#162414\r\n\r\n---------\r\n\r\nCo-authored-by: Kibana Machine
<42973632+kibanamachine@users.noreply.github.com>","sha":"9d2696b5c5159870cecf02c9f23090b26c924c87"}},"sourceBranch":"main","suggestedTargetBranches":["7.17"],"targetPullRequestStates":[{"branch":"8.10","label":"v8.10.0","labelRegex":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"url":"#164409
[Ops] Upgrade to axios 1.4 (#163732) (#164409)\n\n# Backport\n\nThis
will backport the following commits from `main` to `8.10`:\n- [[Ops]
Upgrade to axios
1.4\n(#163732)](#163732
Questions ?\nPlease refer to the [Backport
tool\ndocumentation](https://github.com/sqren/backport)\n\n<!--BACKPORT
[{\"author\":{\"name\":\"Alex\nSzabo\",\"email\":\"alex.szabo@elastic.co\"},\"sourceCommit\":{\"committedDate\":\"2023-08-22T09:47:48Z\",\"message\":\"[Ops]\nUpgrade
to axios 1.4 (#163732)\\n\\n## Summary\\r\\nupgrade `axios`
to\n1.4\\r\\n\\r\\n- adjust to header usage, and config
optionality\\r\\n- Axios'\nadapters are now resolved from a string key
by axios, no need\\r\\nto\nimport/instantiate adapters\\r\\n- most of
the changed code stems from\nchanges in Axios' types\\r\\n -
`response.config` is now optional\\r\\n-\nthere was a change in the type
of AxiosHeaders\n<->\\r\\nInternalAxiosHeaders\\r\\n\\r\\nCloses:
#162661
\\r\\nCloses:\n#162414\\r\\n\\r\\n---------\\r\\n\\r\\nCo-authored-by:
Kibana
Machine\n<42973632+kibanamachine@users.noreply.github.com>\",\"sha\":\"9d2696b5c5159870cecf02c9f23090b26c924c87\",\"branchLabelMapping\":{\"^v8.11.0$\":\"main\",\"^v(\\\\d+).(\\\\d+).\\\\d+$\":\"$1.$2\"}},\"sourcePullRequest\":{\"labels\":[\"Team:Operations\",\"Team:APM\",\"release_note:skip\",\"backport:prev-MAJOR\",\"v7.17.13\",\"v8.11.0\"],\"number\":163732,\"url\":\"#163732
to axios 1.4 (#163732)\\n\\n## Summary\\r\\nupgrade `axios`
to\n1.4\\r\\n\\r\\n- adjust to header usage, and config
optionality\\r\\n- Axios'\nadapters are now resolved from a string key
by axios, no need\\r\\nto\nimport/instantiate adapters\\r\\n- most of
the changed code stems from\nchanges in Axios' types\\r\\n -
`response.config` is now optional\\r\\n-\nthere was a change in the type
of AxiosHeaders\n<->\\r\\nInternalAxiosHeaders\\r\\n\\r\\nCloses:
#162661
\\r\\nCloses:\n#162414\\r\\n\\r\\n---------\\r\\n\\r\\nCo-authored-by:
Kibana
Machine\n<42973632+kibanamachine@users.noreply.github.com>\",\"sha\":\"9d2696b5c5159870cecf02c9f23090b26c924c87\"}},\"sourceBranch\":\"main\",\"suggestedTargetBranches\":[\"7.17\"],\"targetPullRequestStates\":[{\"branch\":\"7.17\",\"label\":\"v7.17.13\",\"labelRegex\":\"^v(\\\\d+).(\\\\d+).\\\\d+$\",\"isSourceBranch\":false,\"state\":\"NOT_CREATED\"},{\"branch\":\"main\",\"label\":\"v8.11.0\",\"labelRegex\":\"^v8.11.0$\",\"isSourceBranch\":true,\"state\":\"MERGED\",\"url\":\"#163732
to axios 1.4 (#163732)\\n\\n## Summary\\r\\nupgrade `axios`
to\n1.4\\r\\n\\r\\n- adjust to header usage, and config
optionality\\r\\n- Axios'\nadapters are now resolved from a string key
by axios, no need\\r\\nto\nimport/instantiate adapters\\r\\n- most of
the changed code stems from\nchanges in Axios' types\\r\\n -
`response.config` is now optional\\r\\n-\nthere was a change in the type
of AxiosHeaders\n<->\\r\\nInternalAxiosHeaders\\r\\n\\r\\nCloses:
#162661
\\r\\nCloses:\n#162414\\r\\n\\r\\n---------\\r\\n\\r\\nCo-authored-by:
Kibana
Machine\n<42973632+kibanamachine@users.noreply.github.com>\",\"sha\":\"9d2696b5c5159870cecf02c9f23090b26c924c87\"}}]}]\n\nCo-authored-by:
Alex Szabo
<alex.szabo@elastic.co>"}},{"branch":"7.17","label":"v7.17.13","labelRegex":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"},{"branch":"main","label":"v8.11.0","labelRegex":"^v8.11.0$","isSourceBranch":true,"state":"MERGED","url":"#163732
Upgrade to axios 1.4 (#163732)\n\n## Summary\r\nupgrade `axios` to
1.4\r\n\r\n- adjust to header usage, and config optionality\r\n- Axios'
adapters are now resolved from a string key by axios, no need\r\nto
import/instantiate adapters\r\n- most of the changed code stems from
changes in Axios' types\r\n - `response.config` is now optional\r\n-
there was a change in the type of AxiosHeaders
<->\r\nInternalAxiosHeaders\r\n\r\nCloses: #162661 \r\nCloses:
#162414\r\n\r\n---------\r\n\r\nCo-authored-by: Kibana Machine
<42973632+kibanamachine@users.noreply.github.com>","sha":"9d2696b5c5159870cecf02c9f23090b26c924c87"}}]}]
BACKPORT-->

---------

Co-authored-by: kibanamachine <42973632+kibanamachine@users.noreply.github.com>
  • Loading branch information
delanni and kibanamachine committed Aug 23, 2023
1 parent d48e3db commit dfef87f
Show file tree
Hide file tree
Showing 21 changed files with 7,152 additions and 4,763 deletions.
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -191,7 +191,7 @@
"JSONStream": "1.3.5",
"antlr4ts": "^0.5.0-alpha.3",
"archiver": "^5.2.0",
"axios": "^0.27.2",
"axios": "^1.4.0",
"base64-js": "^1.3.1",
"bluebird": "3.5.5",
"brace": "0.11.1",
Expand Down
Expand Up @@ -14,8 +14,6 @@ import crypto from 'crypto';

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

import { ToolingLog } from '../tooling_log';
import { parseConfig, Config } from './ci_stats_config';
Expand Down Expand Up @@ -346,7 +344,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
11,818 changes: 7,110 additions & 4,708 deletions packages/kbn-pm/dist/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion packages/kbn-test/jest-preset.js
Expand Up @@ -130,5 +130,5 @@ module.exports = {
],

// A custom resolver to preserve symlinks by default
resolver: '<rootDir>/node_modules/@kbn/test/target_node/jest/setup/preserve_symlinks_resolver.js',
resolver: '<rootDir>/node_modules/@kbn/test/target_node/jest/setup/resolver.js',
};
8 changes: 4 additions & 4 deletions packages/kbn-test/src/failed_tests_reporter/github_api.ts
Expand Up @@ -8,7 +8,7 @@

import Url from 'url';

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

const BASE_URL = 'https://api.github.com/repos/elastic/kibana/';
Expand Down Expand Up @@ -129,7 +129,7 @@ export class GithubApi {
): Promise<{
status: number;
statusText: string;
headers: Record<string, string | string[] | undefined>;
headers: Record<string, AxiosHeaderValue | undefined>;
data: T;
}> {
const executeRequest = !this.dryRun || options.safeForDryRun;
Expand All @@ -144,7 +144,7 @@ export class GithubApi {
return {
status: 200,
statusText: 'OK',
headers: {},
headers: new AxiosHeaders(),
data: dryRunResponse,
};
}
Expand All @@ -157,7 +157,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
Expand Up @@ -14,7 +14,15 @@
const resolve = require('resolve');

module.exports = (request, options) => {
if (request === 'axios') {
return resolve.sync('axios/dist/node/axios.cjs', {
basedir: options.basedir,
extensions: options.extensions,
});
}

try {
// Preserve symlinks
return resolve.sync(request, {
basedir: options.basedir,
extensions: options.extensions,
Expand Down
2 changes: 1 addition & 1 deletion packages/kbn-test/src/kbn_client/kbn_client_requester.ts
Expand Up @@ -15,7 +15,7 @@ import { ToolingLog, isAxiosRequestError, isAxiosResponseError } from '@kbn/dev-

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
Expand Up @@ -15,10 +15,6 @@ import { createHash } from 'crypto';
import Axios from 'axios';
import { ToolingLog, isAxiosResponseError } from '@kbn/dev-utils';

// 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 @@ -75,7 +71,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 @@ -151,7 +147,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
6 changes: 1 addition & 5 deletions src/dev/typescript/ref_output_cache/archives.ts
Expand Up @@ -16,10 +16,6 @@ import { ToolingLog } from '@kbn/dev-utils';
import Axios from 'axios';
import del from 'del';

// 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';

interface Archive {
sha: string;
path: string;
Expand Down Expand Up @@ -116,7 +112,7 @@ export class Archives {
const resp = await Axios.request({
url,
responseType: 'stream',
adapter: AxiosHttpAdapter,
adapter: 'http',
});

await Fs.mkdir(Path.dirname(target), { recursive: true });
Expand Down
Expand Up @@ -6,7 +6,7 @@
*/

import { isObjectLike, isEmpty } from 'lodash';
import { AxiosInstance, Method, AxiosResponse, AxiosBasicCredentials } from 'axios';
import { AxiosInstance, Method, AxiosResponse, AxiosHeaders, AxiosHeaderValue } from 'axios';
import { Logger } from '../../../../../../src/core/server';
import { getCustomAgents } from './get_custom_agents';
import { ActionsConfigurationUtilities } from '../../actions_config';
Expand All @@ -28,18 +28,16 @@ export const request = async <T = unknown>({
data?: T;
params?: unknown;
configurationUtilities: ActionsConfigurationUtilities;
headers?: Record<string, string> | null;
headers?: Record<string, AxiosHeaderValue>;
validateStatus?: (status: number) => boolean;
auth?: AxiosBasicCredentials;
}): Promise<AxiosResponse> => {
const { httpAgent, httpsAgent } = getCustomAgents(configurationUtilities, logger, url);
const { maxContentLength, timeout } = configurationUtilities.getResponseSettings();

return await axios(url, {
...rest,
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 @@ -144,6 +142,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,
});
Expand Up @@ -45,7 +45,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 = require('axios/lib/adapters/http');
axios.defaults.adapter = 'http';
});

afterAll(() => {
Expand Down
Expand Up @@ -8,10 +8,10 @@
import { fromNullable, Option, map, filter } from 'fp-ts/lib/Option';
import { pipe } from 'fp-ts/lib/pipeable';

export function getRetryAfterIntervalFromHeaders(headers: Record<string, string>): Option<number> {
export function getRetryAfterIntervalFromHeaders(headers: Record<string, unknown>): Option<number> {
return pipe(
fromNullable(headers['retry-after']),
map((retryAfter) => parseInt(retryAfter, 10)),
map((retryAfter) => parseInt(retryAfter as string, 10)),
filter((retryAfter) => !isNaN(retryAfter))
);
}
Expand Up @@ -6,6 +6,7 @@
*/
jest.mock('axios', () => ({
create: jest.fn(),
AxiosHeaders: jest.requireActual('axios').AxiosHeaders,
}));

import axios from 'axios';
Expand Down
Expand Up @@ -165,7 +165,7 @@ export async function executor(
url,
logger,
...basicAuth,
headers,
headers: headers ? headers : {},
data,
configurationUtilities,
})
Expand Down
Expand Up @@ -40,9 +40,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');

describe('axios connections', () => {
let testServer: http.Server | https.Server | null;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand All @@ -52,7 +49,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
Expand Up @@ -40,9 +40,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 @@ -54,7 +51,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
Expand Up @@ -72,7 +72,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
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
Expand Up @@ -30,7 +30,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
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/

import axios, { AxiosInstance, AxiosRequestHeaders } from 'axios';
import axios, { AxiosInstance } from 'axios';
import type { Capabilities as UICapabilities } from 'src/core/types';
import { format as formatUrl } from 'url';
import util from 'util';
Expand Down Expand Up @@ -61,7 +61,7 @@ export class UICapabilitiesService {
this.log.debug(
`requesting ${spaceUrlPrefix}/api/core/capabilities to parse the uiCapabilities`
);
const requestHeaders: AxiosRequestHeaders = credentials
const requestHeaders: Record<string, string> = credentials
? {
Authorization: `Basic ${Buffer.from(
`${credentials.username}:${credentials.password}`
Expand Down
10 changes: 1 addition & 9 deletions yarn.lock
Expand Up @@ -8513,14 +8513,6 @@ axios@^0.21.1:
dependencies:
follow-redirects "^1.14.0"

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.4.0:
version "1.4.0"
resolved "https://registry.yarnpkg.com/axios/-/axios-1.4.0.tgz#38a7bf1224cd308de271146038b551d725f0be1f"
Expand Down Expand Up @@ -14542,7 +14534,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.9:
follow-redirects@^1.0.0, follow-redirects@^1.14.0:
version "1.15.0"
resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.15.0.tgz#06441868281c86d0dda4ad8bdaead2d02dca89d4"
integrity sha512-aExlJShTV4qOUOL7yF1U5tvLCB0xQuudbf6toyYA0E/acBNw71mvjFTnLaRp50aQaYocMR0a/RMMBIHeZnGyjQ==
Expand Down

0 comments on commit dfef87f

Please sign in to comment.