Skip to content

Commit

Permalink
test: try installing component templates
Browse files Browse the repository at this point in the history
  • Loading branch information
msimerson committed Feb 11, 2025
1 parent e87e224 commit 65a6e29
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 79 deletions.
4 changes: 2 additions & 2 deletions config/elasticsearch.ini
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@
[cloud]
; id=PROJECT-NAME:Long-base64-string

; [auth]
[auth]
; username=haraka
; password=nice-long-pass-phrase
; apiKey=base64-string
; bearer=token

; [tls]
[tls]
; rejectUnauthorized=false


Expand Down
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ exports.load_es_ini = function () {
this.logdebug('Using nodes')
this.clientArgs = { nodes: this.cfg.es_hosts }
}
if (this.cfg.auth) this.clientArgs.auth = this.cfg.auth
if (this.cfg.tls) this.clientArgs.tls = this.cfg.tls
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
}

exports.get_es_hosts = function () {
Expand Down
8 changes: 8 additions & 0 deletions templates/component/get-componenents.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/sh

ES_URL=https://user:[email protected]:9200

for _p in connection email-message results plugin-access plugin-asn plugin-bounce plugin-dkim plugin-fcrdns plugin-geoip plugin-helo_checks plugin-karma plugin-known-senders plugin-limit plugin-p0f plugin-rspamd plugin-spamassassin plugin-spf; do
curl -k -X GET "$ES_URL/_component_template/haraka-$_p" | jq -S > $_p.json
done

75 changes: 0 additions & 75 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,78 +207,3 @@ describe('trim_plugin_name', function () {
done()
})
})

describe('storesIndexMapTemplate', function () {
beforeEach(setup)

it('saves an index map template to Elasticsearch', function (done) {
this.timeout(4000)

const plugin = this.plugin
const filePath = path.resolve('templates', 'index', 'v8.json')

plugin.load_es_ini()

plugin.es_connect((err) => {
assert.ifError(err)

if (err) {
done()
return
}

fs.readFile(filePath, 'utf8', (err2, data) => {
if (err2) {
console.error(err2)
return done()
}

plugin.es.indices
.putTemplate({
name: 'haraka-results',
index_patterns: 'smtp-*',
mappings: JSON.parse(data),
})
.then((result) => {
console.log(result)
})
.catch((err3) => {
if (err3.status !== 404) {
console.error(err3)
}
// other tests are running, so currently
// stored mapping may conflict
})
.finally(done)
})
})
})
})

describe('log_connection', function () {
beforeEach(setup)

it('saves results to Elasticsearch', function (done) {
const plugin = this.plugin

plugin.load_es_ini()
plugin.es_connect(function (err) {
assert.ifError(err)

console.log('giving ES a few secs to start up')

const connection = fixtures.connection.createConnection()
connection.local.ip = '127.0.0.1'
connection.remote.ip = '172.1.1.1'
connection.uuid = utils.uuid()
connection.count = { msg: { accepted: 1 } }
connection.results.add({ name: 'rspamd' }, { msg: 'test' })

// console.log(util.inspect(connection, { depth: null }));
plugin.log_connection(function () {
// assert.ok(1);
done()
}, connection)
})
})
})
129 changes: 129 additions & 0 deletions test/template.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
'use strict'

const assert = require('node:assert')
const fs = require('node:fs')
const path = require('node:path')

const fixtures = require('haraka-test-fixtures')
const utils = require('haraka-utils')

function setup(done) {
try {
this.plugin = new fixtures.plugin('../index')
} catch (e) {
console.error(`unable to load elasticsearch plugin: ${e}`)
return done('failed to load elasticsearch')
}

this.connection = fixtures.connection.createConnection()
this.plugin.config.root_path = path.resolve(__dirname, '..', '..', 'config')

done()
}

describe('templates', function () {
beforeEach(setup)

it('saves component templates to ES', function (done) {
this.timeout(8000)

const plugin = this.plugin
plugin.load_es_ini()

const filePath = path.resolve('templates', 'component')
const files = fs.readdirSync(filePath)

plugin.es_connect((err) => {
assert.ifError(err)

for (const f of files) {
if (path.extname(f) !== '.json') continue
console.log(`\t${f}`)

const data = fs.readFileSync(path.join(filePath, f))
const template = JSON.parse(data).component_templates[0].component_template.template

plugin.es.cluster.putComponentTemplate({
name: `haraka-${f}`,
template,
})
.then((result) => {
console.log(result)
})
.catch((err2) => {
console.error(err2)
})
}

done()
})
})

it('saves a legacy index template to Elasticsearch', function (done) {
this.timeout(4000)

const plugin = this.plugin
const filePath = path.resolve('templates', 'index', 'v8.json')

plugin.load_es_ini()

plugin.es_connect((err) => {
assert.ifError(err)

if (err) return done()

fs.readFile(filePath, 'utf8', (err2, data) => {
if (err2) {
console.error(err2)
return done()
}

plugin.es.indices
.putTemplate({
name: 'haraka-results',
index_patterns: 'smtp-*',
mappings: JSON.parse(data),
})
.then((result) => {
console.log(result)
})
.catch((err3) => {
if (err3.status !== 404) {
console.error(err3)
}
// other tests are running, so currently
// stored mapping may conflict
})
.finally(done)
})
})
})
})

describe('log_connection', function () {
beforeEach(setup)

it('saves results to Elasticsearch', function (done) {
const plugin = this.plugin

plugin.load_es_ini()
plugin.es_connect(function (err) {
assert.ifError(err)

console.log('giving ES a few secs to start up')

const connection = fixtures.connection.createConnection()
connection.local.ip = '127.0.0.1'
connection.remote.ip = '172.1.1.1'
connection.uuid = utils.uuid()
connection.count = { msg: { accepted: 1 } }
connection.results.add({ name: 'rspamd' }, { msg: 'test' })

// console.log(util.inspect(connection, { depth: null }));
plugin.log_connection(function () {
// assert.ok(1);
done()
}, connection)
})
})
})

0 comments on commit 65a6e29

Please sign in to comment.