Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Osquery] Fix ECS editor field array value #132786

Merged
merged 24 commits into from
May 25, 2022
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
c8ba56e
[Osquery] Fix ECS editor field array value
patrykkopycinski May 24, 2022
693b89d
types
patrykkopycinski May 24, 2022
f987f63
Merge branch 'main' into fix/osquery-ecs-field
kibanamachine May 24, 2022
3db819a
fix
patrykkopycinski May 24, 2022
eda6c59
fix defaultValues.map (useArray) issue
tomsonpl May 24, 2022
305ac43
fix
patrykkopycinski May 24, 2022
8a779bc
fix
patrykkopycinski May 24, 2022
d74b006
Merge branch 'main' into fix/osquery-ecs-field
kibanamachine May 24, 2022
cc71282
Merge branch 'main' into fix/osquery-ecs-field
kibanamachine May 24, 2022
cf6e709
snapshots
patrykkopycinski May 24, 2022
90fefdb
Merge branch 'main' into fix/osquery-ecs-field
kibanamachine May 24, 2022
51d4b49
Merge branch 'main' of github.com:elastic/kibana into fix/osquery-ecs…
patrykkopycinski May 24, 2022
16ef804
unskip
patrykkopycinski May 24, 2022
b7addd1
Merge branch 'main' into fix/osquery-ecs-field
kibanamachine May 24, 2022
1e588a9
Merge branch 'main' into fix/osquery-ecs-field
kibanamachine May 25, 2022
af141c8
snapshot
patrykkopycinski May 25, 2022
98fee43
Merge branch 'main' into fix/osquery-ecs-field
kibanamachine May 25, 2022
38e3307
skip
patrykkopycinski May 25, 2022
e654f72
cypress
patrykkopycinski May 25, 2022
cbfc961
fix
patrykkopycinski May 25, 2022
06ca802
types
patrykkopycinski May 25, 2022
170e7a2
Merge branch 'main' of github.com:elastic/kibana into fix/osquery-ecs…
patrykkopycinski May 25, 2022
c3390ff
fix
patrykkopycinski May 25, 2022
dbb8266
fix
patrykkopycinski May 25, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ describe('ALL - Add Integration', () => {
it('should have integration and packs copied when upgrading integration', () => {
const packageName = 'osquery_manager';
const oldVersion = '1.2.0';
const newVersion = '1.3.0';
const newVersion = '1.3.1';

cy.visit(`app/integrations/detail/${packageName}-${oldVersion}/overview`);
cy.contains('Add Osquery Manager').click();
Expand Down
35 changes: 19 additions & 16 deletions x-pack/plugins/osquery/public/live_queries/form/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import {
import { FormattedMessage } from '@kbn/i18n-react';
import React, { useCallback, useEffect, useMemo, useState } from 'react';
import { useMutation } from 'react-query';
import deepMerge from 'deepmerge';
import styled from 'styled-components';

import { pickBy, isEmpty, map } from 'lodash';
Expand Down Expand Up @@ -110,8 +109,13 @@ const LiveQueryFormComponent: React.FC<LiveQueryFormProps> = ({
options: {
stripEmptyFields: false,
},
// eslint-disable-next-line @typescript-eslint/naming-convention
serializer: ({ savedQueryId, ecs_mapping, ...formData }) =>
// @ts-expect-error update types
patrykkopycinski marked this conversation as resolved.
Show resolved Hide resolved
serializer: ({
savedQueryId,
// eslint-disable-next-line @typescript-eslint/naming-convention
ecs_mapping,
...formData
}) =>
pickBy(
{
...formData,
Expand All @@ -120,20 +124,17 @@ const LiveQueryFormComponent: React.FC<LiveQueryFormProps> = ({
},
(value) => !isEmpty(value)
),
defaultValue: deepMerge(
{
agentSelection: {
agents: [],
allAgentsSelected: false,
platformsSelected: [],
policiesSelected: [],
},
query: '',
savedQueryId: null,
ecs_mapping: [],
defaultValue: {
agentSelection: {
agents: [],
allAgentsSelected: false,
platformsSelected: [],
policiesSelected: [],
},
defaultValue ?? {}
),
query: '',
savedQueryId: null,
ecs_mapping: [],
},
});

const { updateFieldValues, setFieldValue, submit, isSubmitting } = form;
Expand Down Expand Up @@ -182,6 +183,7 @@ const LiveQueryFormComponent: React.FC<LiveQueryFormProps> = ({
updateFieldValues({
query: savedQuery.query,
savedQueryId: savedQuery.savedQueryId,
// @ts-expect-error update types
patrykkopycinski marked this conversation as resolved.
Show resolved Hide resolved
ecs_mapping: savedQuery.ecs_mapping
? map(savedQuery.ecs_mapping, (value, key) => ({
key,
Expand Down Expand Up @@ -359,6 +361,7 @@ const LiveQueryFormComponent: React.FC<LiveQueryFormProps> = ({
agentSelection: defaultValue.agentSelection,
query: defaultValue.query,
savedQueryId: defaultValue.savedQueryId,
// @ts-expect-error update types
ecs_mapping: defaultValue.ecs_mapping
? map(defaultValue.ecs_mapping, (value, key) => ({
key,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ const QueriesFieldComponent: React.FC<QueriesFieldProps> = ({
pickBy(
{
id: newQueryId,
interval: newQuery.interval ?? parsedContent.interval,
interval: newQuery.interval ?? parsedContent.interval ?? '3600',
query: newQuery.query,
version: newQuery.version ?? parsedContent.version,
platform: getSupportedPlatforms(newQuery.platform ?? parsedContent.platform),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ const OsqueryColumnFieldComponent: React.FC<OsqueryColumnFieldProps> = ({
return ecsKeySchemaOption?.value?.normalization !== 'array';
}

return true;
return !!ecsKey?.length;
}, [typeValue, formData, item.path]);

const onTypeChange = useCallback(
Expand Down Expand Up @@ -637,6 +637,7 @@ export const ECSMappingEditorForm: React.FC<ECSMappingEditorFormProps> = ({
osquerySchemaOptions,
editForm: !isLastItem,
},
readDefaultValueOnForm: !item.isNew,
config: {
valueChangeDebounceTime: 300,
type: FIELD_TYPES.COMBO_BOX,
Expand Down Expand Up @@ -702,6 +703,7 @@ export const ECSMappingEditorForm: React.FC<ECSMappingEditorFormProps> = ({
component={ECSComboboxField}
euiFieldProps={ecsComboBoxEuiFieldProps}
validationData={validationData}
readDefaultValueOnForm={!item.isNew}
// @ts-expect-error update types
config={config}
/>
Expand Down Expand Up @@ -1017,7 +1019,9 @@ export const ECSMappingEditorField = React.memo(
if (itemKey) {
const serializedFormData = formDataSerializer();
const itemValue =
serializedFormData.ecs_mapping && serializedFormData.ecs_mapping[`${itemKey}`]?.field;
serializedFormData.ecs_mapping &&
(serializedFormData.ecs_mapping[`${itemKey}`]?.field ||
serializedFormData.ecs_mapping[`${itemKey}`]?.value);

if (itemValue && onAdd.current) {
onAdd.current();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import { ALL_OSQUERY_VERSIONS_OPTIONS } from './constants';
import { UsePackQueryFormProps, PackFormData, usePackQueryForm } from './use_pack_query_form';
import { SavedQueriesDropdown } from '../../saved_queries/saved_queries_dropdown';
import { ECSMappingEditorField } from './lazy_ecs_mapping_editor_field';
import { useKibana } from '../../common/lib/kibana';

const CommonUseField = getUseField({ component: Field });

Expand All @@ -46,6 +47,7 @@ const QueryFlyoutComponent: React.FC<QueryFlyoutProps> = ({
onSave,
onClose,
}) => {
const permissions = useKibana().services.application.capabilities.osquery;
const [isEditMode] = useState(!!defaultValue);
const { form } = usePackQueryForm({
uniqueQueryIds,
Expand Down Expand Up @@ -117,7 +119,7 @@ const QueryFlyoutComponent: React.FC<QueryFlyoutProps> = ({
</EuiFlyoutHeader>
<EuiFlyoutBody>
<Form form={form}>
{!isEditMode ? (
{!isEditMode && permissions.readSavedQueries ? (
<>
<SavedQueriesDropdown onChange={handleSetQueryValue} />
<EuiSpacer />
Expand Down
4 changes: 2 additions & 2 deletions x-pack/plugins/osquery/public/packs/queries/schema.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { FIELD_TYPES } from '../../shared_imports';

import {
createIdFieldValidations,
intervalFieldValidation,
intervalFieldValidations,
queryFieldValidation,
} from './validations';

Expand Down Expand Up @@ -46,7 +46,7 @@ export const createFormSchema = (ids: Set<string>) => ({
label: i18n.translate('xpack.osquery.pack.queryFlyoutForm.intervalFieldLabel', {
defaultMessage: 'Interval (s)',
}),
validations: [{ validator: intervalFieldValidation }],
validations: intervalFieldValidations,
},
platform: {
type: FIELD_TYPES.TEXT,
Expand Down
40 changes: 28 additions & 12 deletions x-pack/plugins/osquery/public/packs/queries/validations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import { i18n } from '@kbn/i18n';

import { ValidationFunc, fieldValidators } from '../../shared_imports';
import { ValidationConfig, ValidationFunc, fieldValidators } from '../../shared_imports';
export { queryFieldValidation } from '../../common/validations';

const idPattern = /^[a-zA-Z0-9-_]+$/;
Expand Down Expand Up @@ -48,14 +48,30 @@ export const createIdFieldValidations = (ids: Set<string>) => [
createUniqueIdValidation(ids),
];

export const intervalFieldValidation: ValidationFunc<
// eslint-disable-next-line @typescript-eslint/no-explicit-any
any,
string,
number
> = fieldValidators.numberGreaterThanField({
than: 0,
message: i18n.translate('xpack.osquery.pack.queryFlyoutForm.invalidIntervalField', {
defaultMessage: 'A positive interval value is required',
}),
});
export const intervalFieldValidations: Array<
ValidationConfig<
// eslint-disable-next-line @typescript-eslint/no-explicit-any
any,
string,
number
>
> = [
{
validator: fieldValidators.numberGreaterThanField({
than: 0,
message: i18n.translate('xpack.osquery.pack.queryFlyoutForm.intervalFieldMinNumberError', {
defaultMessage: 'A positive interval value is required',
}),
}),
},
{
validator: fieldValidators.numberSmallerThanField({
than: 604800,
message: ({ than }) =>
i18n.translate('xpack.osquery.pack.queryFlyoutForm.intervalFieldMaxNumberError', {
defaultMessage: 'An interval value must be lower than {than}',
values: { than },
}),
}),
},
];
1 change: 1 addition & 0 deletions x-pack/plugins/osquery/public/shared_imports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export type {
FormData,
FormHook,
FormSchema,
ValidationConfig,
ValidationError,
ValidationFunc,
ValidationFuncArg,
Expand Down
8 changes: 5 additions & 3 deletions x-pack/plugins/osquery/server/routes/saved_query/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,16 @@ const getInstallation = async (osqueryContext: OsqueryAppContext) =>

export const getInstalledSavedQueriesMap = async (osqueryContext: OsqueryAppContext) => {
const installation = await getInstallation(osqueryContext);

if (installation) {
return reduce(
return reduce<KibanaAssetReference, Record<string, KibanaAssetReference>>(
patrykkopycinski marked this conversation as resolved.
Show resolved Hide resolved
installation.installed_kibana,
// @ts-expect-error not sure why it shouts, but still it's properly typed
(acc: Record<string, KibanaAssetReference>, item: KibanaAssetReference) => {
(acc, item) => {
if (item.type === savedQuerySavedObjectType) {
return { ...acc, [item.id]: item };
}

return acc;
patrykkopycinski marked this conversation as resolved.
Show resolved Hide resolved
},
{}
);
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion x-pack/plugins/translations/translations/fr-FR.json
Original file line number Diff line number Diff line change
Expand Up @@ -21926,7 +21926,7 @@
"xpack.osquery.pack.queryFlyoutForm.idFieldLabel": "ID",
"xpack.osquery.pack.queryFlyoutForm.intervalFieldLabel": "Intervalle (s)",
"xpack.osquery.pack.queryFlyoutForm.invalidIdError": "Les caractères doivent être alphanumériques, _ ou -",
"xpack.osquery.pack.queryFlyoutForm.invalidIntervalField": "Une valeur d'intervalle positive est requise",
"xpack.osquery.pack.queryFlyoutForm.intervalFieldMinNumberError": "Une valeur d'intervalle positive est requise",
"xpack.osquery.pack.queryFlyoutForm.mappingEcsFieldLabel": "Champ ECS",
"xpack.osquery.pack.queryFlyoutForm.mappingValueFieldLabel": "Valeur",
"xpack.osquery.pack.queryFlyoutForm.osqueryResultFieldRequiredErrorMessage": "Valeur obligatoire.",
Expand Down
Loading