From df167ce1f9a12941dd41fef4dccd66138c2266ad Mon Sep 17 00:00:00 2001 From: Matt Simerson Date: Tue, 11 Feb 2025 17:14:28 -0800 Subject: [PATCH] apply index template --- README.md | 6 ++++- index.js | 3 ++- templates/index/composable.json | 4 +-- test/template.js | 47 ++++++++++++++++++++++++++------- 4 files changed, 46 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index c69656c..028b6e3 100644 --- a/README.md +++ b/README.md @@ -78,7 +78,11 @@ transactions. The connections index tends to be mostly noise (monitoring, blocked connections, bruteforce auth attempts, etc.). To collapse them into the same index, set the value for both identically. -# Index template +# Composable Index Templates + +Composable templates are the modern (circa 2025) method of creating index templates. In the file ./test/template.js, there are unit tests with working JS code that install the component templates as well as the index template that uses them. If you're standing up a new ES cluster, copy/pasting that into a .js file will get you bootstrapped fairly quickly. + +# Index template (legacy) Creating an index template will apply the template(s) to any future indexes that match the pattern/name in the template setting. This is how to manually apply diff --git a/index.js b/index.js index e7071e5..84e6c73 100644 --- a/index.js +++ b/index.js @@ -57,7 +57,8 @@ exports.load_es_ini = function () { this.logdebug('Using nodes') this.clientArgs = { nodes: this.cfg.es_hosts } } - if (Object.keys(this.cfg.auth).length > 0) this.clientArgs.auth = this.cfg.auth + if (Object.keys(this.cfg.auth).length > 0) + this.clientArgs.auth = this.cfg.auth if (Object.keys(this.cfg.tls).length > 0) this.clientArgs.tls = this.cfg.tls } diff --git a/templates/index/composable.json b/templates/index/composable.json index b007c68..be2b856 100644 --- a/templates/index/composable.json +++ b/templates/index/composable.json @@ -21,9 +21,7 @@ "haraka-plugin-spf", "haraka-results" ], - "index_patterns": [ - "smtp-*" - ], + "index_patterns": ["smtp-*"], "template": { "mappings": { "dynamic_templates": [ diff --git a/test/template.js b/test/template.js index 9601156..30f6a73 100644 --- a/test/template.js +++ b/test/template.js @@ -27,23 +27,54 @@ describe('templates', function () { it('saves component templates to ES', function (done) { this.timeout(8000) - const plugin = this.plugin - plugin.load_es_ini() + this.plugin.load_es_ini() const filePath = path.resolve('templates', 'component') const files = fs.readdirSync(filePath) - plugin.es_connect((err) => { + this.plugin.es_connect((err) => { assert.ifError(err) for (const f of files) { if (path.extname(f) !== '.json') continue const data = fs.readFileSync(path.join(filePath, f)) - const template = JSON.parse(data).component_templates[0].component_template.template + const template = + JSON.parse(data).component_templates[0].component_template.template + + this.plugin.es.cluster + .putComponentTemplate({ + name: `haraka-${f}`, + template, + }) + .then((result) => { + console.log(`${f}: ${result}`) + }) + .catch((err2) => { + console.error(err2) + }) + } - plugin.es.cluster.putComponentTemplate({ - name: `haraka-${f}`, + done() + }) + }) + + it('saves a composable index template to ES', function (done) { + this.timeout(4000) + + this.plugin.load_es_ini() + + const filePath = path.resolve('templates', 'index', 'composable.json') + + this.plugin.es_connect((err) => { + assert.ifError(err) + + const data = fs.readFileSync(filePath) + const template = JSON.parse(data).index_templates[0].index_template + + this.plugin.es.indices + .putIndexTemplate({ + name: `haraka-results`, template, }) .then((result) => { @@ -52,9 +83,7 @@ describe('templates', function () { .catch((err2) => { console.error(err2) }) - } - - done() + .finally(done) }) })