Skip to content

Commit

Permalink
apply index template
Browse files Browse the repository at this point in the history
  • Loading branch information
msimerson committed Feb 12, 2025
1 parent 5ee328c commit df167ce
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 14 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
4 changes: 1 addition & 3 deletions templates/index/composable.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@
"haraka-plugin-spf",
"haraka-results"
],
"index_patterns": [
"smtp-*"
],
"index_patterns": ["smtp-*"],
"template": {
"mappings": {
"dynamic_templates": [
Expand Down
47 changes: 38 additions & 9 deletions test/template.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand All @@ -52,9 +83,7 @@ describe('templates', function () {
.catch((err2) => {
console.error(err2)
})
}

done()
.finally(done)
})
})

Expand Down

0 comments on commit df167ce

Please sign in to comment.