Skip to content

Commit

Permalink
Merge branch 'main' into 160371-infra-ui-add-alerts-to-asset-details-…
Browse files Browse the repository at this point in the history
…flyout
  • Loading branch information
jennypavlova authored Jul 18, 2023
2 parents 81c0fa6 + e66ebdb commit 37e8fca
Show file tree
Hide file tree
Showing 8 changed files with 175 additions and 82 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export enum ConfigKey {
PARAMS = 'params',
PASSWORD = 'password',
PLAYWRIGHT_OPTIONS = 'playwright_options',
ORIGINAL_SPACE = 'original_space', // the original space the montior was saved in. Used by push monitors to ensure uniqueness of monitor id sent to heartbeat and prevent data collisions
ORIGINAL_SPACE = 'original_space', // the original space the monitor was saved in. Used by push monitors to ensure uniqueness of monitor id sent to heartbeat and prevent data collisions
PORT = 'url.port',
PROXY_URL = 'proxy_url',
PROXY_HEADERS = 'proxy_headers',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,15 @@ import { ConfigKey } from '../../../common/constants/monitor_management';
export const PARAMS_KEYS_TO_SKIP = [
'secrets',
'fields',
ConfigKey.PARAMS,
ConfigKey.PROJECT_ID,
ConfigKey.JOURNEY_ID,
ConfigKey.CONFIG_HASH,
ConfigKey.MONITOR_QUERY_ID,
ConfigKey.LOCATIONS,
ConfigKey.TLS_VERSION,
ConfigKey.SOURCE_PROJECT_CONTENT,
ConfigKey.SOURCE_INLINE,
ConfigKey.RESPONSE_JSON_CHECK,
ConfigKey.CUSTOM_HEARTBEAT_ID,
];
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,81 @@ describe('replaceStringWithParams', () => {

expect(result).toEqual('Basic https://elastic.co https://elastic.co/product');
});

it('works with $ as part of value', () => {
const result = replaceStringWithParams(
'Basic $auth',
{ homePageUrl1: 'https://elastic.co/product' },
logger
);

expect(result).toEqual('Basic $auth');
});

it('works with ${ as part of value', () => {
const result = replaceStringWithParams(
'Basic ${auth',
{ homePageUrl1: 'https://elastic.co/product' },
logger
);

expect(result).toEqual('Basic ${auth');
});
it('works with { as part of value', () => {
const result = replaceStringWithParams(
'Basic {auth',
{ homePageUrl1: 'https://elastic.co/product' },
logger
);

expect(result).toEqual('Basic {auth');
});
it('works with {} as part of value', () => {
const result = replaceStringWithParams(
'Basic {}',
{ homePageUrl1: 'https://elastic.co/product' },
logger
);

expect(result).toEqual('Basic {}');
});
it('works with {$ as part of value', () => {
const result = replaceStringWithParams(
'Basic {$',
{ homePageUrl1: 'https://elastic.co/product' },
logger
);

expect(result).toEqual('Basic {$');
});

it('works with ${} as part of value', () => {
const result = replaceStringWithParams(
'Basic ${} value',
{ homePageUrl1: 'https://elastic.co/product' },
logger
);

expect(result).toEqual('Basic ${} value');
});

it('works with } ${abc} as part of value', () => {
const result = replaceStringWithParams(
'Basic } ${homePageUrl1} value',
{ homePageUrl1: 'https://elastic.co/product' },
logger
);

expect(result).toEqual('Basic } https://elastic.co/product value');
});

it('works with ${abc} { as part of value', () => {
const result = replaceStringWithParams(
'Basic ${homePageUrl1} { value',
{ homePageUrl1: 'https://elastic.co/product' },
logger
);

expect(result).toEqual('Basic https://elastic.co/product { value');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,18 @@ export const replaceStringWithParams = (
try {
if (typeof value !== 'string') {
const strValue = JSON.stringify(value);
if (hasNoParams(strValue)) {
return value as string | null;
}

const parsedVars: ParsedVars = variableParser.parse(strValue);

const parseValue = replaceVarsWithParams(parsedVars, params);
return JSON.parse(parseValue);
}
if (hasNoParams(value)) {
return value as string | null;
}

const parsedVars: ParsedVars = variableParser.parse(value);

Expand All @@ -40,6 +47,11 @@ export const replaceStringWithParams = (
return value as string | null;
};

export const hasNoParams = (strVal: string) => {
const shellParamsRegex = /\$\{[a-zA-Z_][a-zA-Z0-9_]*\}/g;
return strVal.match(shellParamsRegex) === null;
};

export const secondsToCronFormatter: FormatterFn = (fields, key) => {
const value = (fields[key] as string) ?? '';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import { omit } from 'lodash';
import { secretKeys } from '@kbn/synthetics-plugin/common/constants/monitor_management';
import { PackagePolicy } from '@kbn/fleet-plugin/common';
import expect from '@kbn/expect';
import { syntheticsMonitorType } from '@kbn/synthetics-plugin/common/types/saved_objects';
import { FtrProviderContext } from '../../ftr_provider_context';
import { getFixtureJson } from './helper/get_fixture_json';
import { comparePolicies, getTestSyntheticsPolicy } from './sample_data/test_policy';
Expand All @@ -35,7 +34,7 @@ export default function ({ getService }: FtrProviderContext) {
const security = getService('security');

before(async () => {
await kibanaServer.savedObjects.clean({ types: [syntheticsMonitorType] });
await kibanaServer.savedObjects.cleanStandardList();
await testPrivateLocations.installSyntheticsPackage();

_httpMonitorJson = getFixtureJson('http_monitor');
Expand Down Expand Up @@ -85,26 +84,26 @@ export default function ({ getService }: FtrProviderContext) {
it('does not add a monitor if there is an error in creating integration', async () => {
const newMonitor = { ...httpMonitorJson };

const invalidName = '!@#$%^&*()_++[\\-\\]- wow';
const invalidLocation = testFleetPolicyID + '1';
const invalidName = 'invalid name';

newMonitor.locations.push({
id: testFleetPolicyID,
const location = {
id: invalidLocation,
label: 'Test private location 0',
isServiceManaged: false,
});
};

newMonitor.name = invalidName;

const apiResponse = await supertestAPI
.post(SYNTHETICS_API_URLS.SYNTHETICS_MONITORS)
.set('kbn-xsrf', 'true')
.send(newMonitor)
.send({ ...newMonitor, locations: [location, ...newMonitor.locations] })
.expect(500);

expect(apiResponse.body).eql({
statusCode: 500,
message:
'YAMLException: unknown escape sequence at line 3, column 34:\n name: "!@#$,%,^,&,*,(,),_,+,+,[,\\,\\,-,\\,\\,],-, ,w,o,w,"\n ^',
message: `Unable to find Synthetics private location for agentId ${invalidLocation}`,
error: 'Internal Server Error',
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export default function ({ getService }: FtrProviderContext) {
this.tags('skipCloud');

const supertest = getService('supertest');
const kibanaServer = getService('kibanaServer');

let projectMonitors: ProjectMonitorsRequest;

Expand Down Expand Up @@ -46,6 +47,10 @@ export default function ({ getService }: FtrProviderContext) {
await testPrivateLocations.setTestLocations([testPolicyId]);
});

after(async () => {
await kibanaServer.savedObjects.cleanStandardList();
});

beforeEach(() => {
projectMonitors = setUniqueIds({
monitors: getFixtureJson('project_browser_monitor').monitors,
Expand All @@ -58,17 +63,20 @@ export default function ({ getService }: FtrProviderContext) {
const secondMonitor = {
...projectMonitors.monitors[0],
id: 'test-id-2',
privateLocations: ['Test private location 0'],
privateLocations: ['Test private location 7'],
};
const testMonitors = [
projectMonitors.monitors[0],
{ ...secondMonitor, name: '!@#$%^&*()_++[\\-\\]- wow name' },
{
...secondMonitor,
name: '!@#$%^&*()_++[\\-\\]- wow name',
},
];
try {
const body = await monitorTestService.addProjectMonitors(project, testMonitors);
expect(body.createdMonitors.length).eql(1);
expect(body.failedMonitors[0].reason).eql(
'unknown escape sequence at line 3, column 34:\n name: "!@#$,%,^,&,*,(,),_,+,+,[,\\,\\,-,\\,\\,],-, ,w,o,w, ,n,a,m,e,"\n ^'
"Couldn't save or update monitor because of an invalid configuration."
);
} finally {
await Promise.all([
Expand All @@ -88,32 +96,25 @@ export default function ({ getService }: FtrProviderContext) {
privateLocations: ['Test private location 0'],
};
const testMonitors = [projectMonitors.monitors[0], secondMonitor];
try {
const body = await monitorTestService.addProjectMonitors(project, testMonitors);
expect(body.createdMonitors.length).eql(2);
const editedBody = await monitorTestService.addProjectMonitors(project, testMonitors);
expect(editedBody.createdMonitors.length).eql(0);
expect(editedBody.updatedMonitors.length).eql(2);

testMonitors[1].name = '!@#$%^&*()_++[\\-\\]- wow name';

const editedBodyError = await monitorTestService.addProjectMonitors(project, testMonitors);
expect(editedBodyError.createdMonitors.length).eql(0);
expect(editedBodyError.updatedMonitors.length).eql(1);
expect(editedBodyError.failedMonitors.length).eql(1);
expect(editedBodyError.failedMonitors[0].details).eql(
'Failed to update journey: test-id-2'
);
expect(editedBodyError.failedMonitors[0].reason).eql(
'unknown escape sequence at line 3, column 34:\n name: "!@#$,%,^,&,*,(,),_,+,+,[,\\,\\,-,\\,\\,],-, ,w,o,w, ,n,a,m,e,"\n ^'
);
} finally {
await Promise.all([
testMonitors.map((monitor) => {
return monitorTestService.deleteMonitorByJourney(projectMonitors, monitor.id, project);
}),
]);
}
const body = await monitorTestService.addProjectMonitors(project, testMonitors);
expect(body.createdMonitors.length).eql(2);
const editedBody = await monitorTestService.addProjectMonitors(project, testMonitors);
expect(editedBody.createdMonitors.length).eql(0);
expect(editedBody.updatedMonitors.length).eql(2);

testMonitors[1].name = '!@#$%^&*()_++[\\-\\]- wow name';
testMonitors[1].privateLocations = ['Test private location 8'];

const editedBodyError = await monitorTestService.addProjectMonitors(project, testMonitors);
expect(editedBodyError.createdMonitors.length).eql(0);
expect(editedBodyError.updatedMonitors.length).eql(1);
expect(editedBodyError.failedMonitors.length).eql(1);
expect(editedBodyError.failedMonitors[0].details).eql(
'Invalid private location: "Test private location 8". Remove it or replace it with a valid private location.'
);
expect(editedBodyError.failedMonitors[0].reason).eql(
"Couldn't save or update monitor because of an invalid configuration."
);
});
});
}
80 changes: 39 additions & 41 deletions x-pack/test/api_integration/apis/synthetics/edit_monitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export default function ({ getService }: FtrProviderContext) {
};

before(async () => {
await kibanaServer.savedObjects.cleanStandardList();
_httpMonitorJson = getFixtureJson('http_monitor');
await supertest.post('/api/fleet/setup').set('kbn-xsrf', 'true').send().expect(200);
await supertest
Expand All @@ -56,6 +57,10 @@ export default function ({ getService }: FtrProviderContext) {
await testPrivateLocations.setTestLocations([testPolicyId]);
});

after(async () => {
await kibanaServer.savedObjects.cleanStandardList();
});

beforeEach(() => {
httpMonitorJson = { ..._httpMonitorJson };
});
Expand Down Expand Up @@ -299,7 +304,7 @@ export default function ({ getService }: FtrProviderContext) {
expect(editResponse.body).not.to.have.keys('unknownkey');
});

it('handles private location errors and does not update the monitor if integration policy is unable to be updated', async () => {
it.skip('handles private location errors and does not update the monitor if integration policy is unable to be updated', async () => {
const name = 'Monitor with private location';
const newMonitor = {
name,
Expand Down Expand Up @@ -390,54 +395,47 @@ export default function ({ getService }: FtrProviderContext) {
const SPACE_NAME = `test-space-name ${uuidv4()}`;
let monitorId = '';

try {
await kibanaServer.spaces.create({ id: SPACE_ID, name: SPACE_NAME });
await kibanaServer.spaces.create({ id: SPACE_ID, name: SPACE_NAME });

const response = await supertest
.post(`/s/${SPACE_ID}${SYNTHETICS_API_URLS.SYNTHETICS_MONITORS}`)
.set('kbn-xsrf', 'true')
.send(newMonitor)
.expect(200);
const response = await supertest
.post(`/s/${SPACE_ID}${SYNTHETICS_API_URLS.SYNTHETICS_MONITORS}`)
.set('kbn-xsrf', 'true')
.send(newMonitor)
.expect(200);

const { id, attributes: savedMonitor } = response.body;
monitorId = id;
const toUpdate = {
...savedMonitor,
urls: 'https://google.com',
};
await supertest
.put(`/s/${SPACE_ID}${SYNTHETICS_API_URLS.SYNTHETICS_MONITORS}/${monitorId}`)
.set('kbn-xsrf', 'true')
.send(toUpdate)
.expect(200);
const { id, attributes: savedMonitor } = response.body;
monitorId = id;
const toUpdate = {
...savedMonitor,
urls: 'https://google.com',
};
await supertest
.put(`/s/${SPACE_ID}${SYNTHETICS_API_URLS.SYNTHETICS_MONITORS}/${monitorId}`)
.set('kbn-xsrf', 'true')
.send(toUpdate)
.expect(200);

const updatedResponse = await monitorTestService.getMonitor(monitorId, true, SPACE_ID);
const updatedResponse = await monitorTestService.getMonitor(monitorId, true, SPACE_ID);

// ensure monitor was updated
expect(updatedResponse.body.urls).eql(toUpdate.urls);
// ensure monitor was updated
expect(updatedResponse.body.urls).eql(toUpdate.urls);

// update a second time, ensures AAD was not corrupted
const toUpdate2 = {
...savedMonitor,
urls: 'https://google.com',
};
// update a second time, ensures AAD was not corrupted
const toUpdate2 = {
...savedMonitor,
urls: 'https://google.com',
};

await supertest
.put(`/s/${SPACE_ID}${SYNTHETICS_API_URLS.SYNTHETICS_MONITORS}/${monitorId}`)
.set('kbn-xsrf', 'true')
.send(toUpdate2)
.expect(200);
await supertest
.put(`/s/${SPACE_ID}${SYNTHETICS_API_URLS.SYNTHETICS_MONITORS}/${monitorId}`)
.set('kbn-xsrf', 'true')
.send(toUpdate2)
.expect(200);

const updatedResponse2 = await monitorTestService.getMonitor(monitorId, true, SPACE_ID);
const updatedResponse2 = await monitorTestService.getMonitor(monitorId, true, SPACE_ID);

// ensure monitor was updated
expect(updatedResponse2.body.urls).eql(toUpdate2.urls);
} finally {
await supertest
.delete(`/s/${SPACE_ID}${SYNTHETICS_API_URLS.SYNTHETICS_MONITORS}/${monitorId}`)
.set('kbn-xsrf', 'true')
.expect(200);
}
// ensure monitor was updated
expect(updatedResponse2.body.urls).eql(toUpdate2.urls);
});
});
}
Loading

0 comments on commit 37e8fca

Please sign in to comment.