-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Logs+] Enforce dataset names (#166654)
## Summary Closes #163830 This adds server side validation to enforce dataset name format rules for custom integrations. It then enhances the custom integrations Kibana package to handle this seamlessly in the create form. There is no client side validation for the rules per se because as long as the dataset name passes other validations (length etc) then it is always valid - it just comes down to whether it's prefixed or not. ## Other notes - Added a "fields pipeline" to improve the readability of the context update. ## UI / UX changes - Users are informed when a prefix will be added. <img width="886" alt="Screenshot 2023-09-20 at 13 19 49" src="https://github.com/elastic/kibana/assets/471693/764d2bd0-03ef-40ce-8dae-107079c15feb"> - If the integration name has been touched, and the dataset name is untouched, the dataset name will automatically match the integration name. ![matching](https://github.com/elastic/kibana/assets/471693/b72604c0-23f9-4ff1-8db7-9b6c523b36c6) --------- Co-authored-by: Kibana Machine <[email protected]>
- Loading branch information
1 parent
fabaa2f
commit 795ec3e
Showing
18 changed files
with
358 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
134 changes: 134 additions & 0 deletions
134
packages/kbn-custom-integrations/src/state_machines/create/pipelines/fields.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
/* | ||
* 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 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import { pipe } from 'fp-ts/lib/pipeable'; | ||
import { replaceSpecialChars } from '../../../components/create/utils'; | ||
import { CreateCustomIntegrationContext, UpdateFieldsEvent, WithTouchedFields } from '../types'; | ||
|
||
type ValuesTuple = [CreateCustomIntegrationContext, UpdateFieldsEvent]; | ||
|
||
// Pipeline for updating the fields and touchedFields properties within context | ||
export const executeFieldsPipeline = ( | ||
context: CreateCustomIntegrationContext, | ||
event: UpdateFieldsEvent | ||
) => { | ||
return pipe( | ||
[context, event] as ValuesTuple, | ||
updateFields(context), | ||
updateTouchedFields(context), | ||
maybeMatchDatasetNameToIntegrationName(context), | ||
replaceSpecialCharacters(context) | ||
); | ||
}; | ||
|
||
const updateFields = | ||
(originalContext: CreateCustomIntegrationContext) => | ||
(values: ValuesTuple): ValuesTuple => { | ||
const [context, event] = values; | ||
|
||
const mergedContext = { | ||
...context, | ||
fields: { | ||
...context.fields, | ||
...event.fields, | ||
}, | ||
}; | ||
return [mergedContext, event]; | ||
}; | ||
|
||
const updateTouchedFields = | ||
(originalContext: CreateCustomIntegrationContext) => | ||
(values: ValuesTuple): ValuesTuple => { | ||
const [context, event] = values; | ||
|
||
const mergedContext = { | ||
...context, | ||
touchedFields: { | ||
...context.touchedFields, | ||
...Object.keys(event.fields).reduce<WithTouchedFields['touchedFields']>( | ||
(acc, field) => ({ ...acc, [field]: true }), | ||
{} as WithTouchedFields['touchedFields'] | ||
), | ||
}, | ||
}; | ||
return [mergedContext, event]; | ||
}; | ||
|
||
const maybeMatchDatasetNameToIntegrationName = | ||
(originalContext: CreateCustomIntegrationContext) => | ||
(values: ValuesTuple): ValuesTuple => { | ||
const [context, event] = values; | ||
if (context.touchedFields.integrationName && !context.touchedFields.datasets) { | ||
return [ | ||
{ | ||
...context, | ||
fields: { | ||
...context.fields, | ||
datasets: context.fields.datasets.map((dataset, index) => ({ | ||
...dataset, | ||
name: index === 0 ? context.fields.integrationName : dataset.name, | ||
})), | ||
}, | ||
}, | ||
event, | ||
]; | ||
} else { | ||
return [context, event]; | ||
} | ||
}; | ||
|
||
const replaceSpecialCharacters = | ||
(originalContext: CreateCustomIntegrationContext) => | ||
(values: ValuesTuple): ValuesTuple => { | ||
const [context, event] = values; | ||
|
||
const mergedContext = { | ||
...context, | ||
fields: { | ||
...context.fields, | ||
integrationName: replaceSpecialChars(context.fields.integrationName), | ||
datasets: context.fields.datasets.map((dataset) => ({ | ||
...dataset, | ||
name: replaceSpecialChars(dataset.name), | ||
})), | ||
}, | ||
}; | ||
|
||
return [mergedContext, event]; | ||
}; | ||
|
||
export const getDatasetNamePrefix = (integrationName: string) => `${integrationName}.`; | ||
export const datasetNameIsPrefixed = (datasetName: string, integrationName: string) => | ||
datasetName.startsWith(getDatasetNamePrefix(integrationName)); | ||
export const datasetNameWillBePrefixed = (datasetName: string, integrationName: string) => | ||
datasetName !== integrationName; | ||
export const prefixDatasetName = (datasetName: string, integrationName: string) => | ||
`${getDatasetNamePrefix(integrationName)}${datasetName}`; | ||
|
||
// The content after the integration name prefix. | ||
export const getDatasetNameWithoutPrefix = (datasetName: string, integrationName: string) => | ||
datasetNameIsPrefixed(datasetName, integrationName) | ||
? datasetName.split(getDatasetNamePrefix(integrationName))[1] | ||
: datasetName; | ||
|
||
// The machine holds unprefixed names internally to dramatically reduce complexity and improve performance for input changes in the UI. | ||
// Prefixed names are used at the outermost edges e.g. when providing initial state and submitting to the API. | ||
export const normalizeDatasetNames = (fields: UpdateFieldsEvent['fields']) => { | ||
const value = { | ||
...fields, | ||
...(fields.datasets !== undefined && fields.integrationName !== undefined | ||
? { | ||
datasets: fields.datasets.map((dataset) => ({ | ||
...dataset, | ||
name: getDatasetNameWithoutPrefix(dataset.name, fields.integrationName!), | ||
})), | ||
} | ||
: {}), | ||
}; | ||
return value; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.