[Fleet] added kibana version to user-agent (#121297) (#121315)

* added kibana version to user-agent

* Update x-pack/plugins/fleet/server/services/epm/registry/requests.ts

Co-authored-by: Nicolas Chaulet <n.chaulet@gmail.com>

Co-authored-by: Nicolas Chaulet <n.chaulet@gmail.com>

Co-authored-by: Julia Bardi <90178898+juliaElastic@users.noreply.github.com>
Co-authored-by: Nicolas Chaulet <n.chaulet@gmail.com>
This commit is contained in:
Kibana Machine 2021-12-15 11:50:24 -05:00 committed by GitHub
parent 6ec6fd2edf
commit 0cd4bcab3b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 52 additions and 5 deletions

View file

@ -6,10 +6,24 @@
*/
import { RegistryError, RegistryConnectionError, RegistryResponseError } from '../../../errors';
import { appContextService } from '../../app_context';
import { fetchUrl } from './requests';
import { fetchUrl, getResponse } from './requests';
jest.mock('node-fetch');
let mockRegistryProxyUrl: string | undefined;
jest.mock('./proxy', () => ({
getProxyAgent: jest.fn().mockReturnValue('proxy agent'),
getRegistryProxyUrl: () => mockRegistryProxyUrl,
}));
jest.mock('../../app_context', () => ({
appContextService: {
getKibanaVersion: jest.fn(),
getLogger: jest.fn().mockReturnValue({ debug: jest.fn() }),
},
}));
const { Response, FetchError } = jest.requireActual('node-fetch');
// eslint-disable-next-line @typescript-eslint/no-var-requires
const fetchMock = require('node-fetch') as jest.Mock;
@ -22,6 +36,35 @@ describe('Registry request', () => {
jest.clearAllMocks();
});
describe('fetch options', () => {
beforeEach(() => {
fetchMock.mockImplementationOnce(() => Promise.resolve(new Response('')));
(appContextService.getKibanaVersion as jest.Mock).mockReturnValue('8.0.0');
});
it('should set User-Agent header including kibana version', async () => {
getResponse('');
expect(fetchMock).toHaveBeenCalledWith('', {
headers: {
'User-Agent': 'Kibana/8.0.0 node-fetch',
},
});
});
it('should set User-Agent header including kibana version with agent', async () => {
mockRegistryProxyUrl = 'url';
getResponse('');
expect(fetchMock).toHaveBeenCalledWith('', {
agent: 'proxy agent',
headers: {
'User-Agent': 'Kibana/8.0.0 node-fetch',
},
});
});
});
describe('fetchUrl / getResponse errors', () => {
it('regular Errors do not retry. Becomes RegistryError', async () => {
fetchMock.mockImplementationOnce(() => {

View file

@ -88,15 +88,19 @@ function isSystemError(error: FailedAttemptErrors): boolean {
}
export function getFetchOptions(targetUrl: string): RequestInit | undefined {
const options: RequestInit = {
headers: {
'User-Agent': `Kibana/${appContextService.getKibanaVersion()} node-fetch`,
},
};
const proxyUrl = getRegistryProxyUrl();
if (!proxyUrl) {
return undefined;
return options;
}
const logger = appContextService.getLogger();
logger.debug(`Using ${proxyUrl} as proxy for ${targetUrl}`);
return {
agent: getProxyAgent({ proxyUrl, targetUrl }),
};
options.agent = getProxyAgent({ proxyUrl, targetUrl });
return options;
}