-
Notifications
You must be signed in to change notification settings - Fork 8.2k
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
[EPM][Security Solution] Implementing dataset component templates #70517
Changes from all commits
efaea2b
bda26bb
cdbb7e3
79785e0
e5be224
1dee045
df2346e
31e15b1
715e218
74c1446
710dc53
7189679
d6d9ed2
fd973f1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,13 @@ | |
*/ | ||
|
||
import Boom from 'boom'; | ||
import { Dataset, RegistryPackage, ElasticsearchAssetType, TemplateRef } from '../../../../types'; | ||
import { | ||
Dataset, | ||
RegistryPackage, | ||
ElasticsearchAssetType, | ||
TemplateRef, | ||
RegistryElasticsearch, | ||
} from '../../../../types'; | ||
import { CallESAsCurrentUser } from '../../../../types'; | ||
import { Field, loadFieldsFromYaml, processFields } from '../../fields/field'; | ||
import { getPipelineNameForInstallation } from '../ingest_pipeline/install'; | ||
|
@@ -157,6 +163,98 @@ export async function installTemplateForDataset({ | |
}); | ||
} | ||
|
||
function putComponentTemplate( | ||
body: object | undefined, | ||
name: string, | ||
callCluster: CallESAsCurrentUser | ||
): { clusterPromise: Promise<any>; name: string } | undefined { | ||
if (body) { | ||
const callClusterParams: { | ||
method: string; | ||
path: string; | ||
ignore: number[]; | ||
body: any; | ||
} = { | ||
method: 'PUT', | ||
path: `/_component_template/${name}`, | ||
ignore: [404], | ||
body, | ||
}; | ||
|
||
return { clusterPromise: callCluster('transport.request', callClusterParams), name }; | ||
} | ||
} | ||
|
||
function buildComponentTemplates(registryElasticsearch: RegistryElasticsearch | undefined) { | ||
let mappingsTemplate; | ||
let settingsTemplate; | ||
|
||
if (registryElasticsearch && registryElasticsearch['index_template.mappings']) { | ||
mappingsTemplate = { | ||
template: { | ||
mappings: { | ||
...registryElasticsearch['index_template.mappings'], | ||
// temporary change until https://github.com/elastic/elasticsearch/issues/58956 is resolved | ||
// hopefully we'll be able to remove the entire properties section once that issue is resolved | ||
properties: { | ||
// if the timestamp_field changes here: https://github.com/elastic/kibana/blob/master/x-pack/plugins/ingest_manager/server/services/epm/elasticsearch/template/template.ts#L309 | ||
// we'll need to update this as well | ||
'@timestamp': { | ||
type: 'date', | ||
}, | ||
}, | ||
}, | ||
}, | ||
}; | ||
} | ||
|
||
if (registryElasticsearch && registryElasticsearch['index_template.settings']) { | ||
settingsTemplate = { | ||
template: { | ||
settings: registryElasticsearch['index_template.settings'], | ||
}, | ||
}; | ||
} | ||
return { settingsTemplate, mappingsTemplate }; | ||
} | ||
|
||
async function installDatasetComponentTemplates( | ||
templateName: string, | ||
registryElasticsearch: RegistryElasticsearch | undefined, | ||
callCluster: CallESAsCurrentUser | ||
) { | ||
const templates: string[] = []; | ||
const componentPromises: Array<Promise<any>> = []; | ||
|
||
const compTemplates = buildComponentTemplates(registryElasticsearch); | ||
|
||
const mappings = putComponentTemplate( | ||
compTemplates.mappingsTemplate, | ||
`${templateName}-mappings`, | ||
callCluster | ||
); | ||
|
||
const settings = putComponentTemplate( | ||
compTemplates.settingsTemplate, | ||
`${templateName}-settings`, | ||
callCluster | ||
); | ||
|
||
if (mappings) { | ||
templates.push(mappings.name); | ||
componentPromises.push(mappings.clusterPromise); | ||
} | ||
|
||
if (settings) { | ||
templates.push(settings.name); | ||
componentPromises.push(settings.clusterPromise); | ||
} | ||
|
||
// TODO: Check return values for errors | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add/link a ticket? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good call. I created a new ticket here: #70586 I'll update the second link once this PR is merged. |
||
await Promise.all(componentPromises); | ||
return templates; | ||
} | ||
|
||
export async function installTemplate({ | ||
callCluster, | ||
fields, | ||
|
@@ -180,13 +278,22 @@ export async function installTemplate({ | |
packageVersion, | ||
}); | ||
} | ||
|
||
const composedOfTemplates = await installDatasetComponentTemplates( | ||
templateName, | ||
dataset.elasticsearch, | ||
callCluster | ||
); | ||
|
||
const template = getTemplate({ | ||
type: dataset.type, | ||
templateName, | ||
mappings, | ||
pipelineName, | ||
packageName, | ||
composedOfTemplates, | ||
}); | ||
|
||
// TODO: Check return values for errors | ||
const callClusterParams: { | ||
method: string; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,14 +43,16 @@ export function getTemplate({ | |
mappings, | ||
pipelineName, | ||
packageName, | ||
composedOfTemplates, | ||
}: { | ||
type: string; | ||
templateName: string; | ||
mappings: IndexTemplateMappings; | ||
pipelineName?: string | undefined; | ||
packageName: string; | ||
composedOfTemplates: string[]; | ||
}): IndexTemplate { | ||
const template = getBaseTemplate(type, templateName, mappings, packageName); | ||
const template = getBaseTemplate(type, templateName, mappings, packageName, composedOfTemplates); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 5 params is hefty, but seemingly not introduced here and can be reduced later |
||
if (pipelineName) { | ||
template.template.settings.index.default_pipeline = pipelineName; | ||
} | ||
|
@@ -244,7 +246,8 @@ function getBaseTemplate( | |
type: string, | ||
templateName: string, | ||
mappings: IndexTemplateMappings, | ||
packageName: string | ||
packageName: string, | ||
composedOfTemplates: string[] | ||
): IndexTemplate { | ||
return { | ||
// This takes precedence over all index templates installed by ES by default (logs-*-* and metrics-*-*) | ||
|
@@ -308,6 +311,7 @@ function getBaseTemplate( | |
data_stream: { | ||
timestamp_field: '@timestamp', | ||
}, | ||
composed_of: composedOfTemplates, | ||
_meta: { | ||
package: { | ||
name: packageName, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
- name: dataset.type | ||
type: constant_keyword | ||
description: > | ||
Dataset type. | ||
- name: dataset.name | ||
type: constant_keyword | ||
description: > | ||
Dataset name. | ||
- name: dataset.namespace | ||
type: constant_keyword | ||
description: > | ||
Dataset namespace. | ||
- name: '@timestamp' | ||
type: date | ||
description: > | ||
Event timestamp. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
title: Test Dataset | ||
|
||
type: logs | ||
|
||
elasticsearch: | ||
index_template.mappings: | ||
dynamic: false | ||
index_template.settings: | ||
index.lifecycle.name: reference |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Test package | ||
|
||
For testing the that the settings and mappings section get used |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
format_version: 1.0.0 | ||
name: overrides | ||
title: Mappings Settings Test | ||
description: This is a test package for testing that the mappings and settings sections in the dataset manifest are applied. | ||
version: 0.1.0 | ||
categories: ['security'] | ||
release: beta | ||
type: integration | ||
license: basic | ||
|
||
requirement: | ||
elasticsearch: | ||
versions: '>7.7.0' | ||
kibana: | ||
versions: '>7.7.0' | ||
|
||
icons: | ||
- src: '/img/logo_overrides_64_color.svg' | ||
size: '16x16' | ||
type: 'image/svg+xml' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I'll be able to remove this once this: elastic/elasticsearch#58642 is merged.