[Synthetics] Add space id to documents (#170871)

Co-authored-by: kibanamachine <42973632+kibanamachine@users.noreply.github.com>
This commit is contained in:
Shahzad 2023-11-09 15:11:31 +01:00 committed by GitHub
parent eeaf815d23
commit 369a022ceb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
18 changed files with 307 additions and 46 deletions

View file

@ -107,7 +107,7 @@ describe('checking migration metadata changes on all registered SO types', () =>
"ingest-agent-policies": "7633e578f60c074f8267bc50ec4763845e431437",
"ingest-download-sources": "279a68147e62e4d8858c09ad1cf03bd5551ce58d",
"ingest-outputs": "8546f1123ec30dcbd6f238f72729c5f1656a4d9b",
"ingest-package-policies": "a0c9fb48e04dcd638e593db55f1c6451523f90ea",
"ingest-package-policies": "f4c2767e852b700a8b82678925b86bac08958b43",
"ingest_manager_settings": "64955ef1b7a9ffa894d4bb9cf863b5602bfa6885",
"inventory-view": "b8683c8e352a286b4aca1ab21003115a4800af83",
"kql-telemetry": "93c1d16c1a0dfca9c8842062cf5ef8f62ae401ad",

View file

@ -25,6 +25,8 @@ import {
UNINSTALL_TOKENS_SAVED_OBJECT_TYPE,
} from '../constants';
import { migrateSyntheticsPackagePolicyToV8120 } from './migrations/synthetics/to_v8_12_0';
import {
migratePackagePolicyEvictionsFromV8110,
migratePackagePolicyToV8110,
@ -394,6 +396,14 @@ const getSavedObjectTypes = (): { [key: string]: SavedObjectsType } => ({
},
],
},
'5': {
changes: [
{
type: 'data_backfill',
backfillFn: migrateSyntheticsPackagePolicyToV8120,
},
],
},
},
migrations: {
'7.10.0': migratePackagePolicyToV7100,

View file

@ -0,0 +1,146 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import type { SavedObjectModelTransformationContext } from '@kbn/core-saved-objects-server';
import type { PackagePolicy } from '../../../../common';
import { getBrowserPolicy } from './fixtures/8.7.0';
import { migrateSyntheticsPackagePolicyToV8120 as migration } from './to_v8_12_0';
import { migrateSyntheticsPackagePolicyToV8100 as migration10 } from './to_v8_10_0';
describe('8.12.0 Synthetics Package Policy migration', () => {
describe('processors migration', () => {
it('handles processors field for empty values', () => {
const doc = getBrowserPolicy('false');
const actual10 = migration10(doc, {} as SavedObjectModelTransformationContext);
doc.attributes = actual10.attributes as PackagePolicy;
const actual = migration(
{ ...doc, namespace: 'default' },
{} as SavedObjectModelTransformationContext
);
expect(actual.attributes?.inputs?.[3]?.streams[0]?.vars?.processors?.value).toEqual(
JSON.stringify([
{
add_fields: {
fields: {
'monitor.fleet_managed': true,
config_id: '420754e9-40f2-486c-bc2e-265bafd735c5',
meta: { space_id: 'default' },
},
target: '',
},
},
])
);
expect(actual.attributes?.inputs?.[3]?.streams[0]?.compiled_stream?.processors).toEqual(
JSON.stringify([
{
add_fields: {
fields: {
'monitor.fleet_managed': true,
config_id: '420754e9-40f2-486c-bc2e-265bafd735c5',
meta: { space_id: 'default' },
},
target: '',
},
},
])
);
});
it('handles processors field for project monitor', () => {
const doc = getBrowserPolicy('', 'test-project');
const actual10 = migration10(doc, {} as SavedObjectModelTransformationContext);
doc.attributes = actual10.attributes as PackagePolicy;
const actual = migration(
{ ...doc, namespace: 'default' },
{} as SavedObjectModelTransformationContext
);
expect(actual.attributes?.inputs?.[3]?.streams[0]?.vars?.processors?.value).toEqual(
JSON.stringify([
{
add_fields: {
fields: {
'monitor.fleet_managed': true,
config_id: '420754e9-40f2-486c-bc2e-265bafd735c5',
'monitor.project.name': 'test-project',
'monitor.project.id': 'test-project',
meta: { space_id: 'default' },
},
target: '',
},
},
])
);
expect(actual.attributes?.inputs?.[3]?.streams[0]?.compiled_stream.processors).toEqual(
JSON.stringify([
{
add_fields: {
fields: {
'monitor.fleet_managed': true,
config_id: '420754e9-40f2-486c-bc2e-265bafd735c5',
'monitor.project.name': 'test-project',
'monitor.project.id': 'test-project',
meta: { space_id: 'default' },
},
target: '',
},
},
])
);
});
it('handles processors field for test now fields', () => {
const doc = getBrowserPolicy('', 'test-project', 'test-run-id', true);
const actual10 = migration10(doc, {} as SavedObjectModelTransformationContext);
doc.attributes = actual10.attributes as PackagePolicy;
const actual = migration(
{ ...doc, namespace: 'test' },
{} as SavedObjectModelTransformationContext
);
expect(actual.attributes?.inputs?.[3]?.streams[0]?.vars?.processors?.value).toEqual(
JSON.stringify([
{
add_fields: {
fields: {
'monitor.fleet_managed': true,
test_run_id: 'test-run-id',
run_once: true,
config_id: '420754e9-40f2-486c-bc2e-265bafd735c5',
'monitor.project.name': 'test-project',
'monitor.project.id': 'test-project',
meta: { space_id: 'test' },
},
target: '',
},
},
])
);
expect(actual.attributes?.inputs?.[3]?.streams[0]?.compiled_stream.processors).toEqual(
JSON.stringify([
{
add_fields: {
fields: {
'monitor.fleet_managed': true,
test_run_id: 'test-run-id',
run_once: true,
config_id: '420754e9-40f2-486c-bc2e-265bafd735c5',
'monitor.project.name': 'test-project',
'monitor.project.id': 'test-project',
meta: { space_id: 'test' },
},
target: '',
},
},
])
);
});
});
});

View file

@ -0,0 +1,54 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import type { SavedObjectModelDataBackfillFn } from '@kbn/core-saved-objects-server';
import type { PackagePolicy } from '../../../../common';
export const migrateSyntheticsPackagePolicyToV8120: SavedObjectModelDataBackfillFn<
PackagePolicy,
PackagePolicy
> = (packagePolicyDoc) => {
if (
packagePolicyDoc.attributes.package?.name !== 'synthetics' ||
!packagePolicyDoc.attributes.is_managed
) {
return packagePolicyDoc;
}
const updatedAttributes = packagePolicyDoc.attributes;
const namespace = packagePolicyDoc.namespace;
const enabledInput = updatedAttributes.inputs.find((input) => input.enabled === true);
const enabledStream = enabledInput?.streams.find((stream) => {
return ['browser', 'http', 'icmp', 'tcp'].includes(stream.data_stream.dataset);
});
if (!enabledStream) {
return {
attributes: updatedAttributes,
};
}
if (enabledStream.vars) {
const processors = processorsFormatter(enabledStream.vars.processors.value, namespace);
enabledStream.vars.processors = { value: processors, type: 'yaml' };
enabledStream.compiled_stream.processors = processors;
}
return {
attributes: updatedAttributes,
};
};
export const processorsFormatter = (processorsStr: string, namespace?: string) => {
try {
const processors = JSON.parse(processorsStr);
processors[0].add_fields.fields.meta = { space_id: namespace };
return JSON.stringify(processors);
} catch (e) {
return processorsStr;
}
};

View file

@ -321,21 +321,26 @@ export const SyntheticsMonitorWithIdCodec = t.intersection([
t.interface({ id: t.string }),
]);
const HeartbeatFieldsCodec = t.intersection([
t.interface({
config_id: t.string,
}),
t.partial({
run_once: t.boolean,
test_run_id: t.string,
'monitor.project.name': t.string,
'monitor.id': t.string,
'monitor.project.id': t.string,
'monitor.fleet_managed': t.boolean,
meta: t.record(t.string, t.string),
}),
]);
export const HeartbeatConfigCodec = t.intersection([
SyntheticsMonitorWithIdCodec,
t.partial({
fields_under_root: t.boolean,
fields: t.intersection([
t.interface({
config_id: t.string,
}),
t.partial({
run_once: t.boolean,
test_run_id: t.string,
'monitor.project.name': t.string,
'monitor.project.id': t.string,
}),
]),
fields: HeartbeatFieldsCodec,
}),
]);
@ -400,6 +405,7 @@ export type BrowserFields = t.TypeOf<typeof BrowserFieldsCodec>;
export type BrowserSimpleFields = t.TypeOf<typeof BrowserSimpleFieldsCodec>;
export type BrowserAdvancedFields = t.TypeOf<typeof BrowserAdvancedFieldsCodec>;
export type MonitorFields = t.TypeOf<typeof MonitorFieldsCodec>;
export type HeartbeatFields = t.TypeOf<typeof HeartbeatFieldsCodec>;
export type SyntheticsMonitor = t.TypeOf<typeof SyntheticsMonitorCodec>;
export type SyntheticsMonitorWithId = t.TypeOf<typeof SyntheticsMonitorWithIdCodec>;
export type EncryptedSyntheticsSavedMonitor = t.TypeOf<typeof EncryptedSyntheticsSavedMonitorCodec>;

View file

@ -23,6 +23,7 @@ export interface ProcessorFields {
'monitor.id': string;
test_run_id: string;
run_once: boolean;
space_id: string;
}
export const formatSyntheticsPolicy = (
@ -70,7 +71,7 @@ export const formatSyntheticsPolicy = (
const processorItem = dataStream?.vars?.processors;
if (processorItem) {
processorItem.value = processorsFormatter(config);
processorItem.value = processorsFormatter(config as MonitorFields & ProcessorFields);
}
// TODO: remove this once we remove legacy support

View file

@ -6,43 +6,31 @@
*/
import { ProcessorFields } from './format_synthetics_policy';
import { MonitorFields } from '../../../../common/runtime_types';
type Fields = Record<string, string | boolean>;
import { HeartbeatFields, MonitorFields } from '../../../../common/runtime_types';
interface FieldProcessor {
add_fields: {
target: string;
fields: Fields;
fields: HeartbeatFields;
};
}
export const processorsFormatter = (config: Partial<MonitorFields & ProcessorFields>) => {
const fields: Fields = {
'monitor.fleet_managed': true,
};
if (config.test_run_id) {
fields.test_run_id = config.test_run_id;
}
if (config.run_once) {
fields.run_once = config.run_once;
}
if (config.config_id) {
fields.config_id = config.config_id;
}
if (config['monitor.project.name']) {
fields['monitor.project.name'] = config['monitor.project.name'];
}
if (config['monitor.project.id']) {
fields['monitor.project.id'] = config['monitor.project.id'];
}
if (config['monitor.id']) {
fields['monitor.id'] = config['monitor.id'];
}
export const processorsFormatter = (config: MonitorFields & ProcessorFields) => {
const processors: FieldProcessor[] = [
{
add_fields: {
fields,
fields: {
'monitor.fleet_managed': true,
config_id: config.config_id,
test_run_id: config.test_run_id,
run_once: config.run_once,
'monitor.id': config['monitor.id'],
'monitor.project.name': config['monitor.project.name'],
'monitor.project.id': config['monitor.project.id'],
meta: {
space_id: config.space_id,
},
},
target: '',
},
},

View file

@ -305,6 +305,7 @@ describe('formatHeartbeatRequest', () => {
monitor: testBrowserConfig as SyntheticsMonitor,
configId: monitorId,
heartbeatId,
spaceId: 'test-space-id',
},
'{"a":"param"}'
);
@ -317,6 +318,9 @@ describe('formatHeartbeatRequest', () => {
'monitor.project.id': testBrowserConfig.project_id,
run_once: undefined,
test_run_id: undefined,
meta: {
space_id: 'test-space-id',
},
},
fields_under_root: true,
});
@ -329,6 +333,7 @@ describe('formatHeartbeatRequest', () => {
monitor: testBrowserConfig as SyntheticsMonitor,
configId: monitorId,
heartbeatId: monitorId,
spaceId: 'test-space-id',
},
JSON.stringify({ key: 'value' })
);
@ -341,6 +346,9 @@ describe('formatHeartbeatRequest', () => {
'monitor.project.id': testBrowserConfig.project_id,
run_once: undefined,
test_run_id: undefined,
meta: {
space_id: 'test-space-id',
},
},
fields_under_root: true,
params: '{"key":"value"}',
@ -354,6 +362,7 @@ describe('formatHeartbeatRequest', () => {
monitor,
configId: monitorId,
heartbeatId: monitorId,
spaceId: 'test-space-id',
});
expect(actual).toEqual({
@ -365,6 +374,9 @@ describe('formatHeartbeatRequest', () => {
'monitor.project.id': undefined,
run_once: undefined,
test_run_id: undefined,
meta: {
space_id: 'test-space-id',
},
},
fields_under_root: true,
});
@ -377,6 +389,7 @@ describe('formatHeartbeatRequest', () => {
monitor,
configId: monitorId,
heartbeatId: monitorId,
spaceId: 'test-space-id',
});
expect(actual).toEqual({
@ -388,6 +401,9 @@ describe('formatHeartbeatRequest', () => {
'monitor.project.id': undefined,
run_once: undefined,
test_run_id: undefined,
meta: {
space_id: 'test-space-id',
},
},
fields_under_root: true,
});
@ -400,6 +416,7 @@ describe('formatHeartbeatRequest', () => {
configId: monitorId,
runOnce: true,
heartbeatId: monitorId,
spaceId: 'test-space-id',
});
expect(actual).toEqual({
@ -411,6 +428,9 @@ describe('formatHeartbeatRequest', () => {
'monitor.project.id': testBrowserConfig.project_id,
run_once: true,
test_run_id: undefined,
meta: {
space_id: 'test-space-id',
},
},
fields_under_root: true,
});
@ -424,6 +444,7 @@ describe('formatHeartbeatRequest', () => {
configId: monitorId,
testRunId,
heartbeatId: monitorId,
spaceId: 'test-space-id',
});
expect(actual).toEqual({
@ -435,6 +456,9 @@ describe('formatHeartbeatRequest', () => {
'monitor.project.id': testBrowserConfig.project_id,
run_once: undefined,
test_run_id: testRunId,
meta: {
space_id: 'test-space-id',
},
},
fields_under_root: true,
});
@ -448,6 +472,7 @@ describe('formatHeartbeatRequest', () => {
configId: monitorId,
testRunId,
heartbeatId: monitorId,
spaceId: 'test-space-id',
});
expect(actual).toEqual({
@ -460,6 +485,9 @@ describe('formatHeartbeatRequest', () => {
'monitor.project.id': testBrowserConfig.project_id,
run_once: undefined,
test_run_id: testRunId,
meta: {
space_id: 'test-space-id',
},
},
fields_under_root: true,
});

View file

@ -85,10 +85,11 @@ export interface ConfigData {
runOnce?: boolean;
testRunId?: string;
params: Record<string, string>;
spaceId: string;
}
export const formatHeartbeatRequest = (
{ monitor, configId, heartbeatId, runOnce, testRunId }: Omit<ConfigData, 'params'>,
{ monitor, configId, heartbeatId, runOnce, testRunId, spaceId }: Omit<ConfigData, 'params'>,
params?: string
): HeartbeatConfig => {
const projectId = (monitor as BrowserFields)[ConfigKey.PROJECT_ID];
@ -106,6 +107,9 @@ export const formatHeartbeatRequest = (
'monitor.project.id': projectId || undefined,
run_once: runOnce,
test_run_id: testRunId,
meta: {
space_id: spaceId,
},
},
fields_under_root: true,
params: monitor.type === 'browser' ? paramsString : '',

View file

@ -105,6 +105,7 @@ export class SyntheticsPrivateLocation {
config.type,
{
...(config as Partial<MonitorFields>),
space_id: spaceId,
config_id: config.fields?.config_id,
location_name: stringifyString(privateLocation.label),
location_id: privateLocation.id,

View file

@ -206,6 +206,7 @@ describe('SyntheticsMonitorClient', () => {
params: {
username: 'elastic',
},
spaceId: 'test-space',
},
]);
expect(syntheticsService.deleteConfigs).toHaveBeenCalledTimes(1);

View file

@ -111,6 +111,7 @@ export class SyntheticsMonitorClient {
);
const configData = {
spaceId,
params: paramsBySpace[spaceId],
monitor: editedMonitor.monitor,
configId: editedMonitor.id,
@ -128,7 +129,11 @@ export class SyntheticsMonitorClient {
);
if (deletedPublicConfig) {
deletedPublicConfigs.push({ ...deletedPublicConfig, params: paramsBySpace[spaceId] });
deletedPublicConfigs.push({
...deletedPublicConfig,
params: paramsBySpace[spaceId],
spaceId,
});
}
if (privateLocations.length > 0 || this.hasPrivateLocations(editedMonitor.previousMonitor)) {
@ -165,7 +170,7 @@ export class SyntheticsMonitorClient {
const privateDeletePromise = this.privateLocationAPI.deleteMonitors(monitors, spaceId);
const publicDeletePromise = this.syntheticsService.deleteConfigs(
monitors.map((monitor) => ({ monitor, configId: monitor.config_id, params: {} }))
monitors.map((monitor) => ({ spaceId, monitor, configId: monitor.config_id, params: {} }))
);
const [pubicResponse] = await Promise.all([publicDeletePromise, privateDeletePromise]);
@ -286,7 +291,7 @@ export class SyntheticsMonitorClient {
for (const monitor of monitors) {
const { publicLocations, privateLocations } = this.parseLocations(monitor);
if (publicLocations.length > 0) {
publicConfigs.push({ monitor, configId: monitor.config_id, params: {} });
publicConfigs.push({ spaceId, monitor, configId: monitor.config_id, params: {} });
}
if (privateLocations.length > 0) {
@ -370,6 +375,7 @@ export class SyntheticsMonitorClient {
heartbeatConfigs.push(
formatHeartbeatRequest(
{
spaceId,
monitor: normalizedMonitor,
configId: monitor.id,
},
@ -388,6 +394,7 @@ export class SyntheticsMonitorClient {
) {
const { monitor, id } = monitorObj;
const config = {
spaceId,
monitor,
configId: id,
params: paramsBySpace[spaceId],

View file

@ -640,6 +640,7 @@ export class SyntheticsService {
monitor: normalizeSecrets(monitor).attributes,
configId: monitor.id,
heartbeatId: attributes[ConfigKey.MONITOR_QUERY_ID],
spaceId: monitorSpace,
};
});

View file

@ -398,6 +398,7 @@ export default function ({ getService }: FtrProviderContext) {
id: monitorId,
location: { id: testFleetPolicyID },
namespace: formatKibanaNamespace(SPACE_ID),
spaceId: SPACE_ID,
})
);
await supertestWithoutAuth

View file

@ -84,7 +84,9 @@ export default function ({ getService }: FtrProviderContext) {
'response.include_body_max_bytes': '1024',
ipv4: true,
ipv6: true,
fields: {},
fields: {
meta: { space_id: 'default' },
},
fields_under_root: true,
},
],
@ -147,6 +149,7 @@ export default function ({ getService }: FtrProviderContext) {
throttling: { download: 5, upload: 3, latency: 20 },
original_space: 'default',
fields: {
meta: { space_id: 'default' },
'monitor.project.name': 'test-project-cb47c83a-45e7-416a-9301-cb476b5bff01',
'monitor.project.id': 'test-project-cb47c83a-45e7-416a-9301-cb476b5bff01',
},
@ -223,6 +226,7 @@ export default function ({ getService }: FtrProviderContext) {
add_fields: {
target: '',
fields: {
meta: { space_id: 'default' },
'monitor.fleet_managed': true,
},
},

View file

@ -22,6 +22,7 @@ interface PolicyProps {
proxyUrl?: string;
params?: Record<string, any>;
isBrowser?: boolean;
spaceId?: string;
}
export const getTestSyntheticsPolicy = (props: PolicyProps): PackagePolicy => {
@ -130,6 +131,8 @@ export const getHttpInput = ({
proxyUrl,
isTLSEnabled,
isBrowser,
spaceId,
namespace,
name = 'check if title is present-Test private location 0',
}: PolicyProps) => {
const enabled = !isBrowser;
@ -197,6 +200,7 @@ export const getHttpInput = ({
fields: {
'monitor.fleet_managed': true,
config_id: id,
meta: { space_id: spaceId ?? 'default' },
'monitor.project.name': projectId,
'monitor.project.id': projectId,
},
@ -297,6 +301,9 @@ export const getHttpInput = ({
add_fields: {
fields: {
config_id: id,
meta: {
space_id: spaceId ?? 'default',
},
'monitor.fleet_managed': true,
...(projectId
? { 'monitor.project.id': projectId, 'monitor.project.name': projectId }

View file

@ -158,6 +158,7 @@ export const getTestProjectSyntheticsPolicyLightweight = (
config_id: configId,
'monitor.project.name': projectId,
'monitor.project.id': projectId,
meta: { space_id: 'default' },
},
target: '',
},
@ -282,6 +283,7 @@ export const getTestProjectSyntheticsPolicyLightweight = (
'monitor.fleet_managed': true,
'monitor.project.id': projectId,
'monitor.project.name': projectId,
meta: { space_id: 'default' },
},
target: '',
},

View file

@ -11,7 +11,7 @@ import { SyntheticsPrivateLocations } from '@kbn/synthetics-plugin/common/runtim
import { FtrProviderContext } from '../../../ftr_provider_context';
import { KibanaSupertestProvider } from '../../../../../../test/api_integration/services/supertest';
export const INSTALLED_VERSION = '1.1.0';
export const INSTALLED_VERSION = '1.1.1';
export class PrivateLocationTestService {
private supertest: ReturnType<typeof KibanaSupertestProvider>;