[8.16] [APM][Profiling] Add x-elastic-internal-origin: Kibana header to agentless axios (#203590) (#203646)

# Backport

This will backport the following commits from `main` to `8.16`:
- [[APM][Profiling] Add x-elastic-internal-origin: Kibana header to
agentless axios
(#203590)](https://github.com/elastic/kibana/pull/203590)

<!--- Backport version: 9.4.3 -->

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

<!--BACKPORT [{"author":{"name":"Carlos
Crespo","email":"crespocarlos@users.noreply.github.com"},"sourceCommit":{"committedDate":"2024-12-10T16:52:33Z","message":"[APM][Profiling]
Add x-elastic-internal-origin: Kibana header to agentless axios
(#203590)\n\ncloses
[203571](https://github.com/elastic/kibana/issues/203571)\r\n\r\n##
Summary\r\n\r\nKibana 9.0 requires all request made with `axios` have
the request\r\nheader `x-elastic-internal-origin` set to `Kibana` to
avoid appearing as\r\nan “external” integration. Any [requests without
it will be blocked
in\r\nKibana\r\n9.0](https://docs.google.com/document/d/1W7csjn6QYjrBjmbXMzSz_JUD4KcmWz8jTTtAWFwgUJM/edit?tab=t.0#heading=h.brxkig5phxcz)","sha":"cc6dc2ac2be199aea47477d17648a7336c33ff7d","branchLabelMapping":{"^v9.0.0$":"main","^v8.18.0$":"8.x","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:skip","v9.0.0","backport:prev-minor","v8.9.0","ci:project-deploy-observability","Team:obs-ux-infra_services"],"title":"[APM][Profiling]
Add x-elastic-internal-origin: Kibana header to agentless
axios","number":203590,"url":"https://github.com/elastic/kibana/pull/203590","mergeCommit":{"message":"[APM][Profiling]
Add x-elastic-internal-origin: Kibana header to agentless axios
(#203590)\n\ncloses
[203571](https://github.com/elastic/kibana/issues/203571)\r\n\r\n##
Summary\r\n\r\nKibana 9.0 requires all request made with `axios` have
the request\r\nheader `x-elastic-internal-origin` set to `Kibana` to
avoid appearing as\r\nan “external” integration. Any [requests without
it will be blocked
in\r\nKibana\r\n9.0](https://docs.google.com/document/d/1W7csjn6QYjrBjmbXMzSz_JUD4KcmWz8jTTtAWFwgUJM/edit?tab=t.0#heading=h.brxkig5phxcz)","sha":"cc6dc2ac2be199aea47477d17648a7336c33ff7d"}},"sourceBranch":"main","suggestedTargetBranches":["8.9"],"targetPullRequestStates":[{"branch":"main","label":"v9.0.0","branchLabelMappingKey":"^v9.0.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/203590","number":203590,"mergeCommit":{"message":"[APM][Profiling]
Add x-elastic-internal-origin: Kibana header to agentless axios
(#203590)\n\ncloses
[203571](https://github.com/elastic/kibana/issues/203571)\r\n\r\n##
Summary\r\n\r\nKibana 9.0 requires all request made with `axios` have
the request\r\nheader `x-elastic-internal-origin` set to `Kibana` to
avoid appearing as\r\nan “external” integration. Any [requests without
it will be blocked
in\r\nKibana\r\n9.0](https://docs.google.com/document/d/1W7csjn6QYjrBjmbXMzSz_JUD4KcmWz8jTTtAWFwgUJM/edit?tab=t.0#heading=h.brxkig5phxcz)","sha":"cc6dc2ac2be199aea47477d17648a7336c33ff7d"}},{"branch":"8.9","label":"v8.9.0","branchLabelMappingKey":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"}]}]
BACKPORT-->

Co-authored-by: Carlos Crespo <crespocarlos@users.noreply.github.com>
This commit is contained in:
Kibana Machine 2024-12-11 05:44:07 +11:00 committed by GitHub
parent 7bcb2627ea
commit 466cdf5347
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 26 additions and 8 deletions

View file

@ -164,7 +164,12 @@ async function getHostnameWithBasePath(kibanaHostname?: string) {
const parsedHostName = parseHostName(kibanaHostname);
try {
await axios.get(parsedHostName, { maxRedirects: 0 });
await axios.get(parsedHostName, {
maxRedirects: 0,
headers: {
'x-elastic-internal-origin': 'Kibana',
},
});
} catch (e) {
if (isAxiosError(e) && e.response?.status === 302) {
const location = e.response?.headers?.location ?? '';

View file

@ -54,6 +54,7 @@ export async function initDiagnosticsBundle({
headers: {
'kbn-xsrf': 'true',
'elastic-api-version': '2023-10-31',
'x-elastic-internal-origin': 'Kibana',
...apiKeyHeader,
},
};
@ -104,8 +105,8 @@ async function getApmIndices(kbnClientOpts: AxiosRequestConfig) {
async function getFleetPackageInfo(kbnClientOpts: AxiosRequestConfig) {
const res = await axios.get('/api/fleet/epm/packages/apm', kbnClientOpts);
return {
version: res.data.response.version,
isInstalled: res.data.response.status,
version: res.data.item.version,
isInstalled: res.data.item.status,
};
}

View file

@ -8,6 +8,10 @@ import axios, { AxiosRequestConfig, AxiosError } from 'axios';
import { once } from 'lodash';
import { Elasticsearch, Kibana } from '../create_apm_users';
const DEFAULT_HEADERS = {
'kbn-xsrf': 'true',
'x-elastic-internal-origin': 'Kibana',
};
export async function callKibana<T>({
elasticsearch,
kibana,
@ -24,14 +28,18 @@ export async function callKibana<T>({
...options,
baseURL: baseUrl,
auth: { username, password },
headers: { 'kbn-xsrf': 'true', ...options.headers },
headers: { ...DEFAULT_HEADERS, ...options.headers },
});
return data;
}
const getBaseUrl = once(async (kibanaHostname: string) => {
try {
await axios.request({ url: kibanaHostname, maxRedirects: 0 });
await axios.request({
url: kibanaHostname,
maxRedirects: 0,
headers: DEFAULT_HEADERS,
});
} catch (e) {
if (isAxiosError(e)) {
const location = e.response?.headers?.location ?? '';

View file

@ -13,6 +13,10 @@ import { FtrProviderContext } from './ftr_provider_context';
import { loadProfilingData } from './load_profiling_data';
import { setupProfilingResources } from './setup_profiling_resources';
const DEFAULT_HEADERS = {
'kbn-xsrf': true,
'x-elastic-internal-origin': 'Kibana',
};
export async function cypressTestRunner({
ftrProviderContext: { getService },
cypressExecution,
@ -42,11 +46,11 @@ export async function cypressTestRunner({
});
// Ensure Fleet setup is complete
await axios.post(`${kibanaUrlWithAuth}/api/fleet/setup`, {}, { headers: { 'kbn-xsrf': true } });
await axios.post(`${kibanaUrlWithAuth}/api/fleet/setup`, {}, { headers: DEFAULT_HEADERS });
const profilingResources = await axios.get<{ has_setup: boolean; has_data: boolean }>(
`${kibanaUrlWithAuth}/internal/profiling/setup/es_resources`,
{ headers: { 'kbn-xsrf': true } }
{ headers: DEFAULT_HEADERS }
);
// Only runs the setup once. This is useful when runing the tests with --times args

View file

@ -17,7 +17,7 @@ export async function setupProfilingResources({
await axios.post(
`${kibanaUrlWithAuth}/internal/profiling/setup/es_resources`,
{},
{ headers: { 'kbn-xsrf': true } }
{ headers: { 'kbn-xsrf': true, 'x-elastic-internal-origin': 'Kibana' } }
);
// eslint-disable-next-line no-console
console.log('[Done] Setting up Universal profiling resources.');