Add Kibana version header when requesting Synthetics Service's /allowed endpoint (#137076)

* add Kibana version header when requesting synthetics service's /allowed endpoint

* [CI] Auto-commit changed files from 'node scripts/precommit_hook.js --ref HEAD~1..HEAD --fix'

* Update x-pack/plugins/synthetics/server/synthetics_service/service_api_client.ts

Co-authored-by: Dominique Clarke <doclarke71@gmail.com>

* fix test header casing for kibana version header on synth

* fix type

Co-authored-by: kibanamachine <42973632+kibanamachine@users.noreply.github.com>
Co-authored-by: Dominique Clarke <doclarke71@gmail.com>
Co-authored-by: Dominique Clarke <dominique.clarke@elastic.co>
This commit is contained in:
Lucas F. da Costa 2022-07-26 17:50:11 +01:00 committed by GitHub
parent 8c005cf15e
commit 153b6b7a19
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 76 additions and 29 deletions

View file

@ -5,10 +5,13 @@
* 2.0.
*/
jest.mock('axios', () => jest.fn());
import { Logger } from '@kbn/core/server';
import { ServiceAPIClient } from './service_api_client';
import { UptimeServerSetup } from '../legacy_uptime/lib/adapters';
import { ServiceConfig } from '../../common/config';
import axios from 'axios';
jest.mock('@kbn/server-http-tools', () => ({
SslConfig: jest.fn().mockImplementation(({ certificate, key }) => ({ certificate, key })),
@ -59,3 +62,44 @@ describe('getHttpsAgent', () => {
expect(result).toEqual(expect.objectContaining({ cert: 'crt', key: 'k' }));
});
});
describe('checkAccountAccessStatus', () => {
beforeEach(() => {
(axios as jest.MockedFunction<typeof axios>).mockReset();
});
afterEach(() => jest.restoreAllMocks());
it('includes a header with the kibana version', async () => {
const apiClient = new ServiceAPIClient(
jest.fn() as unknown as Logger,
{ tls: { certificate: 'crt', key: 'k' } } as ServiceConfig,
{ isDev: false, kibanaVersion: '8.4' } as UptimeServerSetup
);
apiClient.locations = [
{
id: 'test-location',
url: 'http://localhost',
label: 'Test location',
isServiceManaged: true,
},
];
(axios as jest.MockedFunction<typeof axios>).mockResolvedValue({
status: 200,
statusText: 'ok',
headers: {},
config: {},
data: { allowed: true, signupUrl: 'http://localhost:666/example' },
});
const result = await apiClient.checkAccountAccessStatus();
expect(axios).toHaveBeenCalledWith(
expect.objectContaining({ headers: { 'x-kibana-version': '8.4' } })
);
expect(result).toEqual({ allowed: true, signupUrl: 'http://localhost:666/example' });
});
});

View file

@ -5,7 +5,7 @@
* 2.0.
*/
import axios, { AxiosError } from 'axios';
import axios, { AxiosError, AxiosRequestConfig } from 'axios';
import { forkJoin, from as rxjsFrom, Observable, of } from 'rxjs';
import { catchError, tap } from 'rxjs/operators';
import * as https from 'https';
@ -95,6 +95,11 @@ export class ServiceAPIClient {
return this.callAPI('POST', { ...data, runOnce: true });
}
addVersionHeader(req: AxiosRequestConfig) {
req.headers = { ...req.headers, 'x-kibana-version': this.kibanaVersion };
return req;
}
async checkAccountAccessStatus() {
if (this.authorization) {
// in case username/password is provided, we assume it's always allowed
@ -111,17 +116,13 @@ export class ServiceAPIClient {
if (httpsAgent) {
try {
const { data } = await axios({
method: 'GET',
url: url + '/allowed',
headers:
process.env.NODE_ENV !== 'production' && this.authorization
? {
Authorization: this.authorization,
}
: undefined,
httpsAgent,
});
const { data } = await axios(
this.addVersionHeader({
method: 'GET',
url: url + '/allowed',
httpsAgent,
})
);
const { allowed, signupUrl } = data;
return { allowed, signupUrl };
@ -149,23 +150,25 @@ export class ServiceAPIClient {
convertToDataStreamFormat(rest)
);
return axios({
method,
url: url + (runOnce ? '/run' : '/monitors'),
data: {
monitors: monitorsStreams,
output,
stack_version: this.kibanaVersion,
is_edit: isEdit,
},
headers:
process.env.NODE_ENV !== 'production' && this.authorization
? {
Authorization: this.authorization,
}
: undefined,
httpsAgent: this.getHttpsAgent(url),
});
return axios(
this.addVersionHeader({
method,
url: url + (runOnce ? '/run' : '/monitors'),
data: {
monitors: monitorsStreams,
output,
stack_version: this.kibanaVersion,
is_edit: isEdit,
},
headers:
process.env.NODE_ENV !== 'production' && this.authorization
? {
Authorization: this.authorization,
}
: undefined,
httpsAgent: this.getHttpsAgent(url),
})
);
};
const pushErrors: ServiceLocationErrors = [];