Skip to content

Commit

Permalink
Merge branch '8.0' into backport/8.0/pr-120379
Browse files Browse the repository at this point in the history
  • Loading branch information
kibanamachine authored Dec 15, 2021
2 parents 0c91204 + 1177261 commit 30c7a26
Show file tree
Hide file tree
Showing 24 changed files with 199 additions and 465 deletions.
2 changes: 1 addition & 1 deletion .buildkite/scripts/post_build_kibana.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ echo "--- Upload Build Artifacts"
# Moving to `target/` first will keep `buildkite-agent` from including directories in the artifact name
cd "$KIBANA_DIR/target"
cp kibana-*-linux-x86_64.tar.gz kibana-default.tar.gz
buildkite-agent artifact upload "./*.tar.gz;./*.zip"
buildkite-agent artifact upload "./*.tar.gz;./*.zip;./*.deb;./*.rpm"
cd -
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ kibana_vars=(
xpack.fleet.agents.kibana.host
xpack.fleet.agents.tlsCheckDisabled
xpack.fleet.packages
xpack.fleet.registryProxyUrl
xpack.fleet.registryUrl
xpack.graph.canEditDrillDownUrls
xpack.graph.savePolicy
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ import { readFileSync } from 'fs';
import path from 'path';

import { safeLoad } from 'js-yaml';
import { loggerMock } from '@kbn/logging/mocks';
import { elasticsearchServiceMock } from 'src/core/server/mocks';

import { createAppContextStartContractMock } from '../../../../mocks';
import { appContextService } from '../../../../services';

import type { RegistryDataStream } from '../../../../types';
import { processFields } from '../../fields/field';
import type { Field } from '../../fields/field';
Expand All @@ -22,6 +23,7 @@ import {
getTemplate,
getTemplatePriority,
generateTemplateIndexPattern,
updateCurrentWriteIndices,
} from './template';

const FLEET_COMPONENT_TEMPLATE = '.fleet_component_template-1';
Expand Down Expand Up @@ -801,4 +803,34 @@ describe('EPM template', () => {
expect(templateIndexPattern).toEqual(templateIndexPatternDatasetIsPrefixTrue);
expect(templatePriority).toEqual(templatePriorityDatasetIsPrefixTrue);
});

describe('updateCurrentWriteIndices', () => {
it('update non replicated datastream', async () => {
const esClient = elasticsearchServiceMock.createElasticsearchClient();
esClient.indices.getDataStream.mockResolvedValue({
body: {
data_streams: [
{ name: 'test-non-replicated' },
{ name: 'test-replicated', replicated: true },
],
},
} as any);
const logger = loggerMock.create();
await updateCurrentWriteIndices(esClient, logger, [
{
templateName: 'test',
indexTemplate: {
template: {
settings: { index: {} },
mappings: { properties: {} },
},
} as any,
},
]);

const putMappingsCall = esClient.indices.putMapping.mock.calls.map(([{ index }]) => index);
expect(putMappingsCall).toHaveLength(1);
expect(putMappingsCall[0]).toBe('test-non-replicated');
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export interface IndexTemplateMapping {
}
export interface CurrentDataStream {
dataStreamName: string;
replicated: boolean;
indexTemplate: IndexTemplate;
}
const DEFAULT_SCALING_FACTOR = 1000;
Expand Down Expand Up @@ -415,8 +416,17 @@ export const updateCurrentWriteIndices = async (
if (!templates.length) return;

const allIndices = await queryDataStreamsFromTemplates(esClient, templates);
if (!allIndices.length) return;
return updateAllDataStreams(allIndices, esClient, logger);
const allUpdatablesIndices = allIndices.filter((indice) => {
if (indice.replicated) {
logger.warn(
`Datastream ${indice.dataStreamName} cannot be updated because this is a replicated datastream.`
);
return false;
}
return true;
});
if (!allUpdatablesIndices.length) return;
return updateAllDataStreams(allUpdatablesIndices, esClient, logger);
};

function isCurrentDataStream(item: CurrentDataStream[] | undefined): item is CurrentDataStream[] {
Expand All @@ -440,10 +450,12 @@ const getDataStreams = async (
): Promise<CurrentDataStream[] | undefined> => {
const { templateName, indexTemplate } = template;
const { body } = await esClient.indices.getDataStream({ name: `${templateName}-*` });

const dataStreams = body.data_streams;
if (!dataStreams.length) return;
return dataStreams.map((dataStream: any) => ({
dataStreamName: dataStream.name,
replicated: dataStream.replicated,
indexTemplate,
}));
};
Expand Down Expand Up @@ -478,7 +490,6 @@ const updateExistingDataStream = async ({
// to skip updating and assume the value in the index mapping is correct
delete mappings.properties.stream;
delete mappings.properties.data_stream;

// try to update the mappings first
try {
await retryTransientEsErrors(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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(() => {
Expand Down
12 changes: 8 additions & 4 deletions x-pack/plugins/fleet/server/services/epm/registry/requests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ function AlertsPage() {
const { prepend } = core.http.basePath;
const refetch = useRef<() => void>();
const timefilterService = useTimefilterService();
const { rangeFrom, setRangeFrom, rangeTo, setRangeTo, kuery, setKuery, workflowStatus } =
const { rangeFrom, setRangeFrom, rangeTo, setRangeTo, kuery, setKuery } =
useAlertsPageStateContainer();

useEffect(() => {
Expand Down Expand Up @@ -112,15 +112,6 @@ function AlertsPage() {
];
}, [indexNames]);

// Keep the Workflow status code commented (no delete) as requested: https://github.com/elastic/kibana/issues/117686

// const setWorkflowStatusFilter = useCallback(
// (value: AlertWorkflowStatus) => {
// setWorkflowStatus(value);
// },
// [setWorkflowStatus]
// );

const onQueryChange = useCallback(
({ dateRange, query }) => {
if (rangeFrom === dateRange.from && rangeTo === dateRange.to && kuery === (query ?? '')) {
Expand Down Expand Up @@ -223,8 +214,6 @@ function AlertsPage() {
<EuiFlexItem>
<EuiFlexGroup justifyContent="spaceBetween" alignItems="center">
<EuiFlexItem grow={false}>
{/* Keep the Workflow status code commented (no delete) as requested: https://github.com/elastic/kibana/issues/117686*/}
{/* <WorkflowStatusFilter status={workflowStatus} onChange={setWorkflowStatusFilter} /> */}
<AlertsStatusFilter status={alertFilterStatus} onChange={setAlertStatusFilter} />
</EuiFlexItem>
</EuiFlexGroup>
Expand All @@ -236,7 +225,6 @@ function AlertsPage() {
rangeFrom={rangeFrom}
rangeTo={rangeTo}
kuery={kuery}
workflowStatus={workflowStatus}
setRefetch={setRefetch}
/>
</EuiFlexItem>
Expand Down
Loading

0 comments on commit 30c7a26

Please sign in to comment.