Skip to content

Commit

Permalink
[Synthetics] Add lightweight params support (#148634)
Browse files Browse the repository at this point in the history
## Summary

Fixes #147467


Users will be able to specify params in following format in project
monitors or UI


```
- type: http
  name: Admin Check
  url: ...
  username: ${admin_user:admin}
  password: ${admin_password}
```

---------

Co-authored-by: kibanamachine <[email protected]>
Co-authored-by: Dominique Clarke <[email protected]>
  • Loading branch information
3 people authored Mar 28, 2023
1 parent 55afbba commit 378c5c1
Show file tree
Hide file tree
Showing 40 changed files with 2,011 additions and 615 deletions.
1 change: 1 addition & 0 deletions .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
Expand Up @@ -813,6 +813,7 @@ packages/kbn-yarn-lock-validator @elastic/kibana-operations
/x-pack/test/functional/es_archives/uptime @elastic/uptime
/x-pack/test/functional/services/uptime @elastic/uptime
/x-pack/test/api_integration/apis/uptime @elastic/uptime
/x-pack/test/api_integration/apis/synthetics @elastic/uptime
/x-pack/plugins/observability/public/components/shared/exploratory_view @elastic/uptime
/x-pack/plugins/observability/public/components/shared/field_value_suggestions @elastic/uptime
/x-pack/plugins/observability/public/components/shared/core_web_vitals @elastic/uptime
Expand Down
56 changes: 25 additions & 31 deletions x-pack/plugins/synthetics/common/formatters/browser/formatters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,16 @@
*/
import { BrowserFields, ConfigKey } from '../../runtime_types/monitor_management';

import { Formatter, commonFormatters } from '../common/formatters';
import {
Formatter,
commonFormatters,
objectToJsonFormatter,
arrayToJsonFormatter,
objectToJsonFormatter,
stringToJsonFormatter,
} from '../common/formatters';
import {
tlsValueToYamlFormatter,
tlsValueToStringFormatter,
tlsArrayToYamlFormatter,
} from '../tls/formatters';
tlsValueToStringFormatter,
tlsValueToYamlFormatter,
} from '../formatting_utils';

import { tlsFormatters } from '../tls/formatters';

export type BrowserFormatMap = Record<keyof BrowserFields, Formatter>;
Expand All @@ -37,44 +35,40 @@ const throttlingFormatter: Formatter = (fields) => {
.join('/');
};

export const browserFormatters: BrowserFormatMap = {
[ConfigKey.METADATA]: (fields) => objectToJsonFormatter(fields[ConfigKey.METADATA]),
[ConfigKey.URLS]: null,
[ConfigKey.PORT]: null,
export const deprecatedZipUrlFormatters = {
[ConfigKey.SOURCE_ZIP_URL]: null,
[ConfigKey.SOURCE_ZIP_USERNAME]: null,
[ConfigKey.SOURCE_ZIP_PASSWORD]: null,
[ConfigKey.SOURCE_ZIP_FOLDER]: null,
[ConfigKey.SOURCE_ZIP_PROXY_URL]: null,
[ConfigKey.ZIP_URL_TLS_CERTIFICATE_AUTHORITIES]: tlsValueToYamlFormatter,
[ConfigKey.ZIP_URL_TLS_CERTIFICATE]: tlsValueToYamlFormatter,
[ConfigKey.ZIP_URL_TLS_KEY]: tlsValueToYamlFormatter,
[ConfigKey.ZIP_URL_TLS_KEY_PASSPHRASE]: tlsValueToStringFormatter,
[ConfigKey.ZIP_URL_TLS_VERIFICATION_MODE]: tlsValueToStringFormatter,
[ConfigKey.ZIP_URL_TLS_VERSION]: tlsArrayToYamlFormatter,
};

export const browserFormatters: BrowserFormatMap = {
[ConfigKey.SOURCE_PROJECT_CONTENT]: null,
[ConfigKey.SOURCE_INLINE]: (fields) => stringToJsonFormatter(fields[ConfigKey.SOURCE_INLINE]),
[ConfigKey.PARAMS]: null,
[ConfigKey.SCREENSHOTS]: null,
[ConfigKey.IS_THROTTLING_ENABLED]: null,
[ConfigKey.DOWNLOAD_SPEED]: null,
[ConfigKey.UPLOAD_SPEED]: null,
[ConfigKey.LATENCY]: null,
[ConfigKey.SYNTHETICS_ARGS]: (fields) => arrayToJsonFormatter(fields[ConfigKey.SYNTHETICS_ARGS]),
[ConfigKey.ZIP_URL_TLS_CERTIFICATE_AUTHORITIES]: (fields) =>
tlsValueToYamlFormatter(fields[ConfigKey.ZIP_URL_TLS_CERTIFICATE_AUTHORITIES]),
[ConfigKey.ZIP_URL_TLS_CERTIFICATE]: (fields) =>
tlsValueToYamlFormatter(fields[ConfigKey.ZIP_URL_TLS_CERTIFICATE]),
[ConfigKey.ZIP_URL_TLS_KEY]: (fields) =>
tlsValueToYamlFormatter(fields[ConfigKey.ZIP_URL_TLS_KEY]),
[ConfigKey.ZIP_URL_TLS_KEY_PASSPHRASE]: (fields) =>
tlsValueToStringFormatter(fields[ConfigKey.ZIP_URL_TLS_KEY_PASSPHRASE]),
[ConfigKey.ZIP_URL_TLS_VERIFICATION_MODE]: (fields) =>
tlsValueToStringFormatter(fields[ConfigKey.ZIP_URL_TLS_VERIFICATION_MODE]),
[ConfigKey.ZIP_URL_TLS_VERSION]: (fields) =>
tlsArrayToYamlFormatter(fields[ConfigKey.ZIP_URL_TLS_VERSION]),
[ConfigKey.JOURNEY_FILTERS_MATCH]: (fields) =>
stringToJsonFormatter(fields[ConfigKey.JOURNEY_FILTERS_MATCH]),
[ConfigKey.JOURNEY_FILTERS_TAGS]: (fields) =>
arrayToJsonFormatter(fields[ConfigKey.JOURNEY_FILTERS_TAGS]),
[ConfigKey.THROTTLING_CONFIG]: throttlingFormatter,
[ConfigKey.IGNORE_HTTPS_ERRORS]: null,
[ConfigKey.PLAYWRIGHT_OPTIONS]: null,
[ConfigKey.TEXT_ASSERTION]: null,
[ConfigKey.PORT]: null,
[ConfigKey.URLS]: null,
[ConfigKey.METADATA]: objectToJsonFormatter,
[ConfigKey.SOURCE_INLINE]: stringToJsonFormatter,
[ConfigKey.SYNTHETICS_ARGS]: arrayToJsonFormatter,
[ConfigKey.JOURNEY_FILTERS_MATCH]: stringToJsonFormatter,
[ConfigKey.JOURNEY_FILTERS_TAGS]: arrayToJsonFormatter,
[ConfigKey.THROTTLING_CONFIG]: throttlingFormatter,
...deprecatedZipUrlFormatters,
...commonFormatters,
...tlsFormatters,
};
Original file line number Diff line number Diff line change
Expand Up @@ -5,49 +5,62 @@
* 2.0.
*/

import { ConfigKey } from '../../runtime_types';
import {
secondsToCronFormatter,
arrayToJsonFormatter,
objectToJsonFormatter,
stringToJsonFormatter,
secondsToCronFormatter,
} from './formatters';
} from '../formatting_utils';

describe('formatters', () => {
describe('cronToSecondsNormalizer', () => {
it('takes a number of seconds and converts it to cron format', () => {
expect(secondsToCronFormatter('3')).toEqual('3s');
expect(secondsToCronFormatter({ [ConfigKey.WAIT]: '3' }, ConfigKey.WAIT)).toEqual('3s');
});
});

describe('arrayToJsonFormatter', () => {
it('takes an array and converts it to json', () => {
expect(arrayToJsonFormatter(['tag1', 'tag2'])).toEqual('["tag1","tag2"]');
expect(arrayToJsonFormatter({ [ConfigKey.TAGS]: ['tag1', 'tag2'] }, ConfigKey.TAGS)).toEqual(
'["tag1","tag2"]'
);
});

it('returns null if the array has length of 0', () => {
expect(arrayToJsonFormatter([])).toEqual(null);
expect(arrayToJsonFormatter({ [ConfigKey.TAGS]: [] }, ConfigKey.TAGS)).toEqual(null);
});
});

describe('objectToJsonFormatter', () => {
it('takes a json object string and returns an object', () => {
expect(objectToJsonFormatter({ key: 'value' })).toEqual('{"key":"value"}');
expect(
objectToJsonFormatter(
{ [ConfigKey.RESPONSE_HEADERS_CHECK]: { key: 'value' } },
ConfigKey.RESPONSE_HEADERS_CHECK
)
).toEqual('{"key":"value"}');
});

it('returns null if the object has no keys', () => {
expect(objectToJsonFormatter({})).toEqual(null);
expect(objectToJsonFormatter({ [ConfigKey.METADATA]: {} }, ConfigKey.METADATA)).toEqual(null);
});
});

describe('stringToJsonFormatter', () => {
it('takes a string and returns an json string', () => {
expect(stringToJsonFormatter('step("test step", () => {})')).toEqual(
'"step(\\"test step\\", () => {})"'
);
expect(
stringToJsonFormatter(
{ [ConfigKey.SOURCE_INLINE]: 'step("test step", () => {})' },
ConfigKey.SOURCE_INLINE
)
).toEqual('"step(\\"test step\\", () => {})"');
});

it('returns null if the string is falsy', () => {
expect(stringToJsonFormatter('')).toEqual(null);
expect(
stringToJsonFormatter({ [ConfigKey.SOURCE_INLINE]: '' }, ConfigKey.SOURCE_INLINE)
).toEqual(null);
});
});
});
29 changes: 12 additions & 17 deletions x-pack/plugins/synthetics/common/formatters/common/formatters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,21 @@
* 2.0.
*/

import { CommonFields, ConfigKey, MonitorFields } from '../../runtime_types/monitor_management';
import { CommonFields, ConfigKey, SourceType } from '../../runtime_types/monitor_management';
import { arrayToJsonFormatter, FormatterFn } from '../formatting_utils';

export type Formatter = null | ((fields: Partial<MonitorFields>) => string | null);
export type Formatter = null | FormatterFn;

export type CommonFormatMap = Record<keyof CommonFields | ConfigKey.NAME, Formatter>;

export const commonFormatters: CommonFormatMap = {
[ConfigKey.APM_SERVICE_NAME]: null,
[ConfigKey.NAME]: null,
[ConfigKey.LOCATIONS]: null,
[ConfigKey.MONITOR_TYPE]: null,
[ConfigKey.ENABLED]: null,
[ConfigKey.ALERT_CONFIG]: null,
[ConfigKey.CONFIG_ID]: null,
[ConfigKey.SCHEDULE]: (fields) =>
JSON.stringify(
`@every ${fields[ConfigKey.SCHEDULE]?.number}${fields[ConfigKey.SCHEDULE]?.unit}`
),
[ConfigKey.APM_SERVICE_NAME]: null,
[ConfigKey.TAGS]: (fields) => arrayToJsonFormatter(fields[ConfigKey.TAGS]),
[ConfigKey.TIMEOUT]: (fields) => secondsToCronFormatter(fields[ConfigKey.TIMEOUT] || undefined),
[ConfigKey.NAMESPACE]: null,
[ConfigKey.REVISION]: null,
[ConfigKey.MONITOR_SOURCE_TYPE]: null,
Expand All @@ -35,14 +30,14 @@ export const commonFormatters: CommonFormatMap = {
[ConfigKey.ORIGINAL_SPACE]: null,
[ConfigKey.CONFIG_HASH]: null,
[ConfigKey.MONITOR_QUERY_ID]: null,
[ConfigKey.SCHEDULE]: (fields) =>
JSON.stringify(
`@every ${fields[ConfigKey.SCHEDULE]?.number}${fields[ConfigKey.SCHEDULE]?.unit}`
),
[ConfigKey.TAGS]: arrayToJsonFormatter,
[ConfigKey.TIMEOUT]: (fields) => secondsToCronFormatter(fields[ConfigKey.TIMEOUT] || undefined),
[ConfigKey.MONITOR_SOURCE_TYPE]: (fields) =>
fields[ConfigKey.MONITOR_SOURCE_TYPE] || SourceType.UI,
};

export const arrayToJsonFormatter = (value: string[] = []) =>
value.length ? JSON.stringify(value) : null;

export const secondsToCronFormatter = (value: string = '') => (value ? `${value}s` : null);

export const objectToJsonFormatter = (value: Record<string, any> = {}) =>
Object.keys(value).length ? JSON.stringify(value) : null;

export const stringToJsonFormatter = (value: string = '') => (value ? JSON.stringify(value) : null);
Loading

0 comments on commit 378c5c1

Please sign in to comment.