From 9d2696b5c5159870cecf02c9f23090b26c924c87 Mon Sep 17 00:00:00 2001 From: Alex Szabo Date: Tue, 22 Aug 2023 11:47:48 +0200 Subject: [PATCH] [Ops] Upgrade to axios 1.4 (#163732) ## Summary upgrade `axios` to 1.4 - adjust to header usage, and config optionality - Axios' adapters are now resolved from a string key by axios, no need to import/instantiate adapters - most of the changed code stems from changes in Axios' types - `response.config` is now optional - there was a change in the type of AxiosHeaders <-> InternalAxiosHeaders Closes: #162661 Closes: #162414 --------- Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com> --- package.json | 2 +- .../src/ci_stats_reporter.ts | 5 +---- .../failed_tests_reporter/github_api.ts | 8 ++++---- packages/kbn-test/src/jest/resolver.js | 7 +++++++ .../src/kbn_client/kbn_client_requester.ts | 2 +- src/dev/build/lib/download.ts | 8 ++------ .../axios_utils_connection.test.ts | 5 +---- .../axios_utils_proxy.test.ts | 5 +---- .../plugins/actions/server/lib/axios_utils.ts | 20 ++++++++++++++----- .../sub_action_connector.test.ts | 4 ++-- .../sub_action_connector.ts | 15 ++++++++++---- .../create_apm_users/create_apm_users_cli.ts | 2 +- .../create_apm_users/helpers/get_version.ts | 6 +++--- .../integration_tests/downloads.test.ts | 2 +- .../endpoint/common/format_axios_error.ts | 6 +++--- .../server/lib/telemetry/preview_sender.ts | 6 ++++-- .../cases_webhook/utils.test.ts | 2 ++ .../email/send_email_graph_api.test.ts | 1 + .../connector_types/gen_ai/lib/utils.ts | 4 ++-- .../lib/http_response_retry_header.ts | 4 ++-- .../lib/servicenow/utils.test.ts | 3 ++- .../connector_types/lib/servicenow/utils.ts | 9 ++++++--- .../connector_types/slack_api/service.ts | 6 ++++-- .../server/connector_types/torq/index.ts | 1 - .../common/services/ui_capabilities.ts | 4 ++-- yarn.lock | 10 +--------- 26 files changed, 80 insertions(+), 67 deletions(-) diff --git a/package.json b/package.json index e4bb9e23dbd490..19247a3a1a2fc4 100644 --- a/package.json +++ b/package.json @@ -808,7 +808,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", diff --git a/packages/kbn-ci-stats-reporter/src/ci_stats_reporter.ts b/packages/kbn-ci-stats-reporter/src/ci_stats_reporter.ts index 0f469de2a4ebe3..62da562ad14768 100644 --- a/packages/kbn-ci-stats-reporter/src/ci_stats_reporter.ts +++ b/packages/kbn-ci-stats-reporter/src/ci_stats_reporter.ts @@ -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'; @@ -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, diff --git a/packages/kbn-failed-test-reporter-cli/failed_tests_reporter/github_api.ts b/packages/kbn-failed-test-reporter-cli/failed_tests_reporter/github_api.ts index 5ad0f8417d651e..5475dfb9f2da8a 100644 --- a/packages/kbn-failed-test-reporter-cli/failed_tests_reporter/github_api.ts +++ b/packages/kbn-failed-test-reporter-cli/failed_tests_reporter/github_api.ts @@ -8,7 +8,7 @@ import Url from 'url'; -import Axios, { AxiosRequestConfig, AxiosInstance } from 'axios'; +import Axios, { AxiosRequestConfig, AxiosInstance, AxiosHeaders, AxiosHeaderValue } from 'axios'; import { isAxiosResponseError, isAxiosRequestError } from '@kbn/dev-utils'; import { ToolingLog } from '@kbn/tooling-log'; @@ -130,7 +130,7 @@ export class GithubApi { ): Promise<{ status: number; statusText: string; - headers: Record; + headers: Record; data: T; }> { const executeRequest = !this.dryRun || options.safeForDryRun; @@ -145,7 +145,7 @@ export class GithubApi { return { status: 200, statusText: 'OK', - headers: {}, + headers: new AxiosHeaders(), data: dryRunResponse, }; } @@ -158,7 +158,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; diff --git a/packages/kbn-test/src/jest/resolver.js b/packages/kbn-test/src/jest/resolver.js index 6f964b1478fb79..2723851340ae48 100644 --- a/packages/kbn-test/src/jest/resolver.js +++ b/packages/kbn-test/src/jest/resolver.js @@ -43,6 +43,13 @@ module.exports = (request, options) => { return module.exports(request.replace('@elastic/eui/lib/', '@elastic/eui/test-env/'), options); } + if (request === 'axios') { + return resolve.sync('axios/dist/node/axios.cjs', { + basedir: options.basedir, + extensions: options.extensions, + }); + } + if (request === `elastic-apm-node`) { return APM_AGENT_MOCK; } diff --git a/packages/kbn-test/src/kbn_client/kbn_client_requester.ts b/packages/kbn-test/src/kbn_client/kbn_client_requester.ts index be9ea42d94d66b..9481e6481b936b 100644 --- a/packages/kbn-test/src/kbn_client/kbn_client_requester.ts +++ b/packages/kbn-test/src/kbn_client/kbn_client_requester.ts @@ -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 ); }; diff --git a/src/dev/build/lib/download.ts b/src/dev/build/lib/download.ts index ea9f56cb4fe35d..e7ee49e72f79f0 100644 --- a/src/dev/build/lib/download.ts +++ b/src/dev/build/lib/download.ts @@ -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) { @@ -78,7 +74,7 @@ export async function downloadToDisk({ const response = await Axios.request({ url, responseType: 'stream', - adapter: AxiosHttpAdapter, + adapter: 'http', }); if (response.status !== 200) { @@ -171,7 +167,7 @@ export async function downloadToString({ const resp = await Axios.request({ url, method: 'GET', - adapter: AxiosHttpAdapter, + adapter: 'http', responseType: 'text', validateStatus: !expectStatus ? undefined : (status) => status === expectStatus, }); diff --git a/x-pack/plugins/actions/server/integration_tests/axios_utils_connection.test.ts b/x-pack/plugins/actions/server/integration_tests/axios_utils_connection.test.ts index 7a5ed95170cc6f..21d2f095377272 100644 --- a/x-pack/plugins/actions/server/integration_tests/axios_utils_connection.test.ts +++ b/x-pack/plugins/actions/server/integration_tests/axios_utils_connection.test.ts @@ -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 @@ -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(() => { diff --git a/x-pack/plugins/actions/server/integration_tests/axios_utils_proxy.test.ts b/x-pack/plugins/actions/server/integration_tests/axios_utils_proxy.test.ts index fb979968a4636d..d5dd8ae91632e5 100644 --- a/x-pack/plugins/actions/server/integration_tests/axios_utils_proxy.test.ts +++ b/x-pack/plugins/actions/server/integration_tests/axios_utils_proxy.test.ts @@ -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', () => { @@ -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(() => { diff --git a/x-pack/plugins/actions/server/lib/axios_utils.ts b/x-pack/plugins/actions/server/lib/axios_utils.ts index 30232991f39646..181893db15f0c4 100644 --- a/x-pack/plugins/actions/server/lib/axios_utils.ts +++ b/x-pack/plugins/actions/server/lib/axios_utils.ts @@ -6,7 +6,14 @@ */ import { isObjectLike, isEmpty } from 'lodash'; -import { AxiosInstance, Method, AxiosResponse, AxiosRequestConfig } from 'axios'; +import { + AxiosInstance, + Method, + AxiosResponse, + AxiosRequestConfig, + AxiosHeaders, + AxiosHeaderValue, +} from 'axios'; import { Logger } from '@kbn/core/server'; import { getCustomAgents } from './get_custom_agents'; import { ActionsConfigurationUtilities } from '../actions_config'; @@ -29,7 +36,7 @@ export const request = async ({ method?: Method; data?: T; configurationUtilities: ActionsConfigurationUtilities; - headers?: Record | null; + headers?: Record; sslOverrides?: SSLSettings; } & AxiosRequestConfig): Promise => { const { httpAgent, httpsAgent } = getCustomAgents( @@ -43,8 +50,7 @@ export const request = async ({ return await axios(url, { ...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, @@ -149,6 +155,10 @@ export const createAxiosResponse = (res: Partial): 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, }); diff --git a/x-pack/plugins/actions/server/sub_action_framework/sub_action_connector.test.ts b/x-pack/plugins/actions/server/sub_action_framework/sub_action_connector.test.ts index 006286cc99a356..5980072217dd51 100644 --- a/x-pack/plugins/actions/server/sub_action_framework/sub_action_connector.test.ts +++ b/x-pack/plugins/actions/server/sub_action_framework/sub_action_connector.test.ts @@ -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'; @@ -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; diff --git a/x-pack/plugins/actions/server/sub_action_framework/sub_action_connector.ts b/x-pack/plugins/actions/server/sub_action_framework/sub_action_connector.ts index 66f29afcd5ef1f..f8b395d31ce4a9 100644 --- a/x-pack/plugins/actions/server/sub_action_framework/sub_action_connector.ts +++ b/x-pack/plugins/actions/server/sub_action_framework/sub_action_connector.ts @@ -8,7 +8,14 @@ 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, + AxiosHeaderValue, +} from 'axios'; import { SavedObjectsClientContract } from '@kbn/core-saved-objects-api-server'; import { ElasticsearchClient } from '@kbn/core-elasticsearch-server'; import { finished } from 'stream/promises'; @@ -75,7 +82,7 @@ export abstract class SubActionConnector { } } - private getHeaders(headers?: AxiosRequestHeaders) { + private getHeaders(headers?: AxiosRequestHeaders): Record { return { ...headers, 'Content-Type': 'application/json' }; } @@ -130,7 +137,7 @@ export abstract class SubActionConnector { method, data: this.normalizeData(data), configurationUtilities: this.configurationUtilities, - headers: this.getHeaders(headers), + headers: this.getHeaders(headers as AxiosHeaders), }); this.validateResponse(responseSchema, res.data); @@ -139,7 +146,7 @@ export abstract class SubActionConnector { } 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}` ); let responseBody = ''; diff --git a/x-pack/plugins/apm/scripts/create_apm_users/create_apm_users_cli.ts b/x-pack/plugins/apm/scripts/create_apm_users/create_apm_users_cli.ts index 24cda99fd669b2..752c1dba04e93b 100644 --- a/x-pack/plugins/apm/scripts/create_apm_users/create_apm_users_cli.ts +++ b/x-pack/plugins/apm/scripts/create_apm_users/create_apm_users_cli.ts @@ -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 })` ); diff --git a/x-pack/plugins/apm/server/test_helpers/create_apm_users/helpers/get_version.ts b/x-pack/plugins/apm/server/test_helpers/create_apm_users/helpers/get_version.ts index a00747ae785826..070c86dd0be9f5 100644 --- a/x-pack/plugins/apm/server/test_helpers/create_apm_users/helpers/get_version.ts +++ b/x-pack/plugins/apm/server/test_helpers/create_apm_users/helpers/get_version.ts @@ -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 "` + `Cannot access Kibana on ${e.config?.baseURL}. Please specify Kibana with: "--kibana-url "` ); } } diff --git a/x-pack/plugins/screenshotting/server/browsers/chromium/integration_tests/downloads.test.ts b/x-pack/plugins/screenshotting/server/browsers/chromium/integration_tests/downloads.test.ts index 17031c9dc8c700..a2c331e039389d 100644 --- a/x-pack/plugins/screenshotting/server/browsers/chromium/integration_tests/downloads.test.ts +++ b/x-pack/plugins/screenshotting/server/browsers/chromium/integration_tests/downloads.test.ts @@ -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(() => { diff --git a/x-pack/plugins/security_solution/scripts/endpoint/common/format_axios_error.ts b/x-pack/plugins/security_solution/scripts/endpoint/common/format_axios_error.ts index ccb3dc125f5616..f1b69c8665fc6d 100644 --- a/x-pack/plugins/security_solution/scripts/endpoint/common/format_axios_error.ts +++ b/x-pack/plugins/security_solution/scripts/endpoint/common/format_axios_error.ts @@ -25,9 +25,9 @@ export class FormattedAxiosError extends Error { super(axiosError.message); this.request = { - method: axiosError.config.method ?? '?', - url: axiosError.config.url ?? '?', - data: axiosError.config.data ?? '', + method: axiosError.config?.method ?? '?', + url: axiosError.config?.url ?? '?', + data: axiosError.config?.data ?? '', }; this.response = { diff --git a/x-pack/plugins/security_solution/server/lib/telemetry/preview_sender.ts b/x-pack/plugins/security_solution/server/lib/telemetry/preview_sender.ts index 2c6a55900a14d1..a26bbef5ee9d76 100644 --- a/x-pack/plugins/security_solution/server/lib/telemetry/preview_sender.ts +++ b/x-pack/plugins/security_solution/server/lib/telemetry/preview_sender.ts @@ -6,7 +6,7 @@ */ import type { AxiosInstance, AxiosResponse } from 'axios'; -import axios from 'axios'; +import axios, { AxiosHeaders } from 'axios'; import type { Logger } from '@kbn/core/server'; import type { TelemetryPluginStart, TelemetryPluginSetup } from '@kbn/telemetry-plugin/server'; import type { UsageCounter } from '@kbn/usage-collection-plugin/server'; @@ -71,7 +71,9 @@ export class PreviewTelemetryEventsSender implements ITelemetryEventsSender { status: 200, statusText: 'ok', headers: {}, - config: {}, + config: { + headers: new AxiosHeaders(), + }, }; return Promise.resolve(okResponse); } diff --git a/x-pack/plugins/stack_connectors/server/connector_types/cases_webhook/utils.test.ts b/x-pack/plugins/stack_connectors/server/connector_types/cases_webhook/utils.test.ts index 29c732c1b7d400..8e73e1d6682fdd 100644 --- a/x-pack/plugins/stack_connectors/server/connector_types/cases_webhook/utils.test.ts +++ b/x-pack/plugins/stack_connectors/server/connector_types/cases_webhook/utils.test.ts @@ -5,6 +5,7 @@ * 2.0. */ +import { AxiosHeaders } from 'axios'; import { getObjectValueByKeyAsString, stringifyObjValues, @@ -60,6 +61,7 @@ describe('cases_webhook/utils', () => { config: { method: 'post', url: 'https://poster.com', + headers: new AxiosHeaders({}), }, }; it('Throws error when missing content-type', () => { diff --git a/x-pack/plugins/stack_connectors/server/connector_types/email/send_email_graph_api.test.ts b/x-pack/plugins/stack_connectors/server/connector_types/email/send_email_graph_api.test.ts index 199681a13e9683..bbb5fa2ae5f4e1 100644 --- a/x-pack/plugins/stack_connectors/server/connector_types/email/send_email_graph_api.test.ts +++ b/x-pack/plugins/stack_connectors/server/connector_types/email/send_email_graph_api.test.ts @@ -6,6 +6,7 @@ */ jest.mock('axios', () => ({ create: jest.fn(), + AxiosHeaders: jest.requireActual('axios').AxiosHeaders, })); import axios from 'axios'; diff --git a/x-pack/plugins/stack_connectors/server/connector_types/gen_ai/lib/utils.ts b/x-pack/plugins/stack_connectors/server/connector_types/gen_ai/lib/utils.ts index b0c953eaa3ae11..d51f85c7aa5141 100644 --- a/x-pack/plugins/stack_connectors/server/connector_types/gen_ai/lib/utils.ts +++ b/x-pack/plugins/stack_connectors/server/connector_types/gen_ai/lib/utils.ts @@ -5,7 +5,7 @@ * 2.0. */ -import { AxiosRequestHeaders, AxiosResponse, ResponseType } from 'axios'; +import { AxiosResponse, ResponseType } from 'axios'; import { IncomingMessage } from 'http'; import { OpenAiProviderType } from '../../../../common/gen_ai/constants'; import { @@ -77,7 +77,7 @@ export const getAxiosOptions = ( provider: string, apiKey: string, stream: boolean -): { headers: AxiosRequestHeaders; responseType?: ResponseType } => { +): { headers: Record; responseType?: ResponseType } => { const responseType = stream ? { responseType: 'stream' as ResponseType } : {}; switch (provider) { case OpenAiProviderType.OpenAi: diff --git a/x-pack/plugins/stack_connectors/server/connector_types/lib/http_response_retry_header.ts b/x-pack/plugins/stack_connectors/server/connector_types/lib/http_response_retry_header.ts index 7f13c3bcf022c2..d3c36219da6411 100644 --- a/x-pack/plugins/stack_connectors/server/connector_types/lib/http_response_retry_header.ts +++ b/x-pack/plugins/stack_connectors/server/connector_types/lib/http_response_retry_header.ts @@ -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): Option { +export function getRetryAfterIntervalFromHeaders(headers: Record): Option { return pipe( fromNullable(headers['retry-after']), - map((retryAfter) => parseInt(retryAfter, 10)), + map((retryAfter) => parseInt(retryAfter as string, 10)), filter((retryAfter) => !isNaN(retryAfter)) ); } diff --git a/x-pack/plugins/stack_connectors/server/connector_types/lib/servicenow/utils.test.ts b/x-pack/plugins/stack_connectors/server/connector_types/lib/servicenow/utils.test.ts index d31dbca880f361..79ca4a662608c8 100644 --- a/x-pack/plugins/stack_connectors/server/connector_types/lib/servicenow/utils.test.ts +++ b/x-pack/plugins/stack_connectors/server/connector_types/lib/servicenow/utils.test.ts @@ -27,6 +27,7 @@ jest.mock('@kbn/actions-plugin/server/lib/get_oauth_jwt_access_token', () => ({ jest.mock('axios', () => ({ create: jest.fn(), + AxiosHeaders: jest.requireActual('axios').AxiosHeaders, })); const createAxiosInstanceMock = axios.create as jest.Mock; const axiosInstanceMock = { @@ -226,7 +227,7 @@ describe('utils', () => { const mockRequestCallback = (axiosInstanceMock.interceptors.request.use as jest.Mock).mock .calls[0][0]; expect(await mockRequestCallback({ headers: {} })).toEqual({ - headers: { Authorization: 'Bearer tokentokentoken' }, + headers: new axios.AxiosHeaders({ Authorization: 'Bearer tokentokentoken' }), }); expect(getOAuthJwtAccessToken as jest.Mock).toHaveBeenCalledWith({ diff --git a/x-pack/plugins/stack_connectors/server/connector_types/lib/servicenow/utils.ts b/x-pack/plugins/stack_connectors/server/connector_types/lib/servicenow/utils.ts index f5ae7baa3e2561..2e6ba5327ed2b1 100644 --- a/x-pack/plugins/stack_connectors/server/connector_types/lib/servicenow/utils.ts +++ b/x-pack/plugins/stack_connectors/server/connector_types/lib/servicenow/utils.ts @@ -5,7 +5,7 @@ * 2.0. */ -import axios, { AxiosInstance, AxiosRequestConfig, AxiosResponse } from 'axios'; +import axios, { AxiosHeaders, AxiosInstance, AxiosResponse } from 'axios'; import { Logger } from '@kbn/core/server'; import { addTimeZoneToDate, getErrorMessage } from '@kbn/actions-plugin/server/lib/axios_utils'; import { ActionsConfigurationUtilities } from '@kbn/actions-plugin/server/actions_config'; @@ -112,7 +112,7 @@ export const getAxiosInstance = ({ } else { axiosInstance = axios.create(); axiosInstance.interceptors.request.use( - async (axiosConfig: AxiosRequestConfig) => { + async (axiosConfig) => { const accessToken = await getOAuthJwtAccessToken({ connectorId, logger, @@ -137,7 +137,10 @@ export const getAxiosInstance = ({ if (!accessToken) { throw new Error(`Unable to retrieve access token for connectorId: ${connectorId}`); } - axiosConfig.headers = { ...axiosConfig.headers, Authorization: accessToken }; + axiosConfig.headers = new AxiosHeaders({ + ...axiosConfig.headers, + Authorization: accessToken, + }); return axiosConfig; }, (error) => { diff --git a/x-pack/plugins/stack_connectors/server/connector_types/slack_api/service.ts b/x-pack/plugins/stack_connectors/server/connector_types/slack_api/service.ts index 2db25dc52f2238..ae85127eb457aa 100644 --- a/x-pack/plugins/stack_connectors/server/connector_types/slack_api/service.ts +++ b/x-pack/plugins/stack_connectors/server/connector_types/slack_api/service.ts @@ -5,7 +5,7 @@ * 2.0. */ -import axios, { AxiosResponse } from 'axios'; +import axios, { AxiosHeaders, AxiosResponse } from 'axios'; import { Logger } from '@kbn/core/server'; import { i18n } from '@kbn/i18n'; import { ActionsConfigurationUtilities } from '@kbn/actions-plugin/server/actions_config'; @@ -147,7 +147,9 @@ export const createExternalService = ( status: 0, statusText: '', headers: {}, - config: {}, + config: { + headers: new AxiosHeaders({}), + }, }; while (numberOfFetch < RE_TRY) { diff --git a/x-pack/plugins/stack_connectors/server/connector_types/torq/index.ts b/x-pack/plugins/stack_connectors/server/connector_types/torq/index.ts index 860e87397eb447..9d805291cf92c7 100644 --- a/x-pack/plugins/stack_connectors/server/connector_types/torq/index.ts +++ b/x-pack/plugins/stack_connectors/server/connector_types/torq/index.ts @@ -366,7 +366,6 @@ function retryResult(actionId: string, serviceMessage: string): ActionTypeExecut function retryResultSeconds( actionId: string, serviceMessage: string, - retryAfter: number ): ActionTypeExecutorResult { const retryEpoch = Date.now() + retryAfter * 1000; diff --git a/x-pack/test/ui_capabilities/common/services/ui_capabilities.ts b/x-pack/test/ui_capabilities/common/services/ui_capabilities.ts index 9cb3da7e8fd5c7..4367b53d1f0f25 100644 --- a/x-pack/test/ui_capabilities/common/services/ui_capabilities.ts +++ b/x-pack/test/ui_capabilities/common/services/ui_capabilities.ts @@ -5,7 +5,7 @@ * 2.0. */ -import axios, { AxiosInstance, AxiosRequestHeaders } from 'axios'; +import axios, { AxiosInstance } from 'axios'; import type { Capabilities as UICapabilities } from '@kbn/core/types'; import { format as formatUrl } from 'url'; import util from 'util'; @@ -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 = credentials ? { Authorization: `Basic ${Buffer.from( `${credentials.username}:${credentials.password}` diff --git a/yarn.lock b/yarn.lock index a97726e56888d7..61fe8a795521c1 100644 --- a/yarn.lock +++ b/yarn.lock @@ -11291,14 +11291,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" @@ -16705,7 +16697,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==