Use the getBasicAuthHeader util function for Cases Webhook

This commit is contained in:
Christos Nasikas 2024-05-10 22:42:33 +03:00
parent 1a62c77d46
commit 104f881251
2 changed files with 47 additions and 5 deletions

View file

@ -13,6 +13,7 @@ import { CasesWebhookMethods, CasesWebhookPublicConfigurationType, ExternalServi
import { Logger } from '@kbn/core/server';
import { loggingSystemMock } from '@kbn/core/server/mocks';
import { actionsConfigMock } from '@kbn/actions-plugin/server/actions_config.mock';
import { getBasicAuthHeader } from '@kbn/actions-plugin/server/lib';
const logger = loggingSystemMock.create().get() as jest.Mocked<Logger>;
jest.mock('@kbn/actions-plugin/server/lib/axios_utils', () => {
@ -124,6 +125,43 @@ describe('Cases webhook service', () => {
)
).not.toThrow();
});
test('uses the basic auth header for authentication', () => {
createExternalService(
actionId,
{
config,
secrets: { user: 'username', password: 'password' },
},
logger,
configurationUtilities
);
expect(axios.create).toHaveBeenCalledWith({
headers: {
...getBasicAuthHeader({ username: 'username', password: 'password' }),
'content-type': 'application/json',
},
});
});
test('does not add the basic auth header for authentication if hasAuth=false', () => {
createExternalService(
actionId,
{
config: { ...config, hasAuth: false },
secrets: { user: 'username', password: 'password' },
},
logger,
configurationUtilities
);
expect(axios.create).toHaveBeenCalledWith({
headers: {
'content-type': 'application/json',
},
});
});
});
describe('getIncident', () => {

View file

@ -8,10 +8,10 @@
import axios, { AxiosResponse } from 'axios';
import { Logger } from '@kbn/core/server';
import { isString } from 'lodash';
import { renderMustacheStringNoEscape } from '@kbn/actions-plugin/server/lib/mustache_renderer';
import { request } from '@kbn/actions-plugin/server/lib/axios_utils';
import { ActionsConfigurationUtilities } from '@kbn/actions-plugin/server/actions_config';
import { combineHeadersWithBasicAuthHeader } from '@kbn/actions-plugin/server/lib';
import { validateAndNormalizeUrl, validateJson } from './validators';
import {
createServiceError,
@ -69,14 +69,18 @@ export const createExternalService = (
}
const createIncidentUrl = removeSlash(createIncidentUrlConfig);
const headersWithBasicAuth = hasAuth
? combineHeadersWithBasicAuthHeader({
username: user ?? undefined,
password: password ?? undefined,
headers,
})
: {};
const axiosInstance = axios.create({
...(hasAuth && isString(secrets.user) && isString(secrets.password)
? { auth: { username: secrets.user, password: secrets.password } }
: {}),
headers: {
['content-type']: 'application/json',
...(headers != null ? headers : {}),
...headersWithBasicAuth,
},
});