aboutsummaryrefslogtreecommitdiff
path: root/docker/tag-config
diff options
context:
space:
mode:
authorQuentin <quentin@deuxfleurs.fr>2019-06-01 16:02:49 +0200
committerQuentin Dufour <quentin@deuxfleurs.fr>2019-07-11 09:33:07 +0200
commit61d009f18d5886db8b22ae41e04bb41a4ba2fddb (patch)
treee44bb326caf3107653c7a48749527cfd77f02cf2 /docker/tag-config
downloadinfrastructure-61d009f18d5886db8b22ae41e04bb41a4ba2fddb.tar.gz
infrastructure-61d009f18d5886db8b22ae41e04bb41a4ba2fddb.zip
Initial commit
Diffstat (limited to 'docker/tag-config')
-rw-r--r--docker/tag-config/.gitignore1
-rw-r--r--docker/tag-config/README.md22
-rw-r--r--docker/tag-config/index.mjs56
-rw-r--r--docker/tag-config/package-lock.json74
-rw-r--r--docker/tag-config/package.json18
-rw-r--r--docker/tag-config/src/catalog/consul.mjs30
-rw-r--r--docker/tag-config/src/injector/iptables.mjs53
-rw-r--r--docker/tag-config/src/injector/upnp.mjs0
-rw-r--r--docker/tag-config/src/io/files.mjs8
-rw-r--r--docker/tag-config/src/io/run.mjs9
-rw-r--r--docker/tag-config/static.iptables10
-rw-r--r--docker/tag-config/test/io.mjs10
-rw-r--r--docker/tag-config/test/iptables.mjs28
-rw-r--r--docker/tag-config/test/runner.mjs28
14 files changed, 347 insertions, 0 deletions
diff --git a/docker/tag-config/.gitignore b/docker/tag-config/.gitignore
new file mode 100644
index 0000000..3c3629e
--- /dev/null
+++ b/docker/tag-config/.gitignore
@@ -0,0 +1 @@
+node_modules
diff --git a/docker/tag-config/README.md b/docker/tag-config/README.md
new file mode 100644
index 0000000..0746388
--- /dev/null
+++ b/docker/tag-config/README.md
@@ -0,0 +1,22 @@
+```
+npm install
+npm test
+```
+
+You will probably need to run consul in parallel:
+
+```
+consul agent -dev
+```
+
+You can register services like that:
+
+```
+consul services register -name=toto -tag="public_port=4848"
+```
+
+You will need some arguments to run the software:
+
+```
+sudo npm start node=rincevent ipt_base=./static.iptables
+```
diff --git a/docker/tag-config/index.mjs b/docker/tag-config/index.mjs
new file mode 100644
index 0000000..a5b51fc
--- /dev/null
+++ b/docker/tag-config/index.mjs
@@ -0,0 +1,56 @@
+'use strict'
+import consul from 'consul'
+import { exec } from './src/io/run.mjs'
+import { readFile } from './src/io/files.mjs'
+
+import ctlg_consul from './src/catalog/consul.mjs'
+import inj_iptables from './src/injector/iptables.mjs'
+
+const get_args = () => process
+ .argv
+ .slice(2)
+ .map(a => a.split('='))
+ .reduce((dict, tuple) => {
+ dict[tuple[0]] = tuple.length > 1 ? tuple[1] : null
+ return dict
+ }, {})
+
+/**
+ * If we have multiple catalogs
+ * we cache the results of the other ones
+ */
+function* notifications_aggregator(injectors) {
+ const states = []
+ for(let idx = 0; true; idx++) {
+ yield async (tag_list) => {
+ states[idx] = tag_list
+ await Promise.all(
+ injectors.map(notify =>
+ notify(states.reduce((acc, tag) => [...acc, ...tag], []))))
+ }
+ }
+}
+
+const main = async () => {
+ try {
+ const args = get_args()
+
+ // Initialize all injectors
+ const injectors = [
+ await inj_iptables(args.ipt_base, readFile, exec, console.log)
+ ]
+
+ // Initialize all catalogs and map them to the injectors
+ const aggr = notifications_aggregator(injectors)
+ const catalogs = [
+ await ctlg_consul(args.node, consul(), console.log, aggr.next().value)
+ ]
+
+ console.log("[main] initialized")
+ } catch(e) {
+ console.error("initialization failed", e)
+ process.exit(1)
+ }
+}
+
+main()
diff --git a/docker/tag-config/package-lock.json b/docker/tag-config/package-lock.json
new file mode 100644
index 0000000..a4d30b9
--- /dev/null
+++ b/docker/tag-config/package-lock.json
@@ -0,0 +1,74 @@
+{
+ "name": "consul-to-igd",
+ "version": "1.0.0",
+ "lockfileVersion": 1,
+ "requires": true,
+ "dependencies": {
+ "assertion-error": {
+ "version": "1.1.0",
+ "resolved": "https://registry.npmjs.org/assertion-error/-/assertion-error-1.1.0.tgz",
+ "integrity": "sha512-jgsaNduz+ndvGyFt3uSuWqvy4lCnIJiovtouQN5JZHOKCS2QuhEdbcQHFhVksz2N2U9hXJo8odG7ETyWlEeuDw==",
+ "dev": true
+ },
+ "chai": {
+ "version": "4.2.0",
+ "resolved": "https://registry.npmjs.org/chai/-/chai-4.2.0.tgz",
+ "integrity": "sha512-XQU3bhBukrOsQCuwZndwGcCVQHyZi53fQ6Ys1Fym7E4olpIqqZZhhoFJoaKVvV17lWQoXYwgWN2nF5crA8J2jw==",
+ "dev": true,
+ "requires": {
+ "assertion-error": "^1.1.0",
+ "check-error": "^1.0.2",
+ "deep-eql": "^3.0.1",
+ "get-func-name": "^2.0.0",
+ "pathval": "^1.1.0",
+ "type-detect": "^4.0.5"
+ }
+ },
+ "check-error": {
+ "version": "1.0.2",
+ "resolved": "https://registry.npmjs.org/check-error/-/check-error-1.0.2.tgz",
+ "integrity": "sha1-V00xLt2Iu13YkS6Sht1sCu1KrII=",
+ "dev": true
+ },
+ "consul": {
+ "version": "0.34.1",
+ "resolved": "https://registry.npmjs.org/consul/-/consul-0.34.1.tgz",
+ "integrity": "sha512-xCLBzPQBgnDgC2LdYnrT/Fc6PglRU6u7EBkpW0ExAx3Am/CdtKcP5o/3jfwOy7PBAwBqnJk3AYdwwGg+arriiQ==",
+ "requires": {
+ "papi": "^0.29.0"
+ }
+ },
+ "deep-eql": {
+ "version": "3.0.1",
+ "resolved": "https://registry.npmjs.org/deep-eql/-/deep-eql-3.0.1.tgz",
+ "integrity": "sha512-+QeIQyN5ZuO+3Uk5DYh6/1eKO0m0YmJFGNmFHGACpf1ClL1nmlV/p4gNgbl2pJGxgXb4faqo6UE+M5ACEMyVcw==",
+ "dev": true,
+ "requires": {
+ "type-detect": "^4.0.0"
+ }
+ },
+ "get-func-name": {
+ "version": "2.0.0",
+ "resolved": "https://registry.npmjs.org/get-func-name/-/get-func-name-2.0.0.tgz",
+ "integrity": "sha1-6td0q+5y4gQJQzoGY2YCPdaIekE=",
+ "dev": true
+ },
+ "papi": {
+ "version": "0.29.1",
+ "resolved": "https://registry.npmjs.org/papi/-/papi-0.29.1.tgz",
+ "integrity": "sha512-Y9ipSMfWuuVFO3zY9PlxOmEg+bQ7CeJ28sa9/a0veYNynLf9fwjR3+3fld5otEy7okUaEOUuCHVH62MyTmACXQ=="
+ },
+ "pathval": {
+ "version": "1.1.0",
+ "resolved": "https://registry.npmjs.org/pathval/-/pathval-1.1.0.tgz",
+ "integrity": "sha1-uULm1L3mUwBe9rcTYd74cn0GReA=",
+ "dev": true
+ },
+ "type-detect": {
+ "version": "4.0.8",
+ "resolved": "https://registry.npmjs.org/type-detect/-/type-detect-4.0.8.tgz",
+ "integrity": "sha512-0fr/mIH1dlO+x7TlcMy+bIDqKPsw/70tVyeHW787goQjhmqaZe10uwLujubK9q9Lg6Fiho1KUKDYz0Z7k7g5/g==",
+ "dev": true
+ }
+ }
+}
diff --git a/docker/tag-config/package.json b/docker/tag-config/package.json
new file mode 100644
index 0000000..892fb87
--- /dev/null
+++ b/docker/tag-config/package.json
@@ -0,0 +1,18 @@
+{
+ "name": "consul-to-igd",
+ "version": "1.0.0",
+ "description": "",
+ "main": "index.mjs",
+ "dependencies": {
+ "consul": "^0.34.1"
+ },
+ "devDependencies": {
+ "chai": "^4.2.0"
+ },
+ "scripts": {
+ "start": "node --experimental-modules ./index.mjs",
+ "test": "node --experimental-modules ./test/runner.mjs"
+ },
+ "author": "Quentin",
+ "license": "AGPL-3.0-or-later"
+}
diff --git a/docker/tag-config/src/catalog/consul.mjs b/docker/tag-config/src/catalog/consul.mjs
new file mode 100644
index 0000000..655c61f
--- /dev/null
+++ b/docker/tag-config/src/catalog/consul.mjs
@@ -0,0 +1,30 @@
+'use strict'
+
+let l
+export default l = async (node, consul, log, notify) => {
+ const watch = consul.watch({ method: consul.catalog.node.services, options: {node: node}})
+
+ const extract_tags = data =>
+ data ?
+ Object
+ .keys(data.Services)
+ .map(k => data.Services[k].Tags)
+ .reduce((acc, v) => [...acc, ...v], []) :
+ []
+
+ watch.on('error', err => {
+ console.error('error', err)
+ })
+
+ watch.on('change', async (data, res) => {
+ try {
+ const tags = extract_tags(data)
+ log(`[consul] new update, detected ${tags.length} tags`)
+ await notify(tags)
+ } catch(e) {
+ console.error('failed to notify target', e)
+ }
+ })
+
+ log('[consul] initialized')
+}
diff --git a/docker/tag-config/src/injector/iptables.mjs b/docker/tag-config/src/injector/iptables.mjs
new file mode 100644
index 0000000..584b560
--- /dev/null
+++ b/docker/tag-config/src/injector/iptables.mjs
@@ -0,0 +1,53 @@
+'use strict'
+
+let l;
+export default l = async (path, readFile, exec, log) => {
+
+ const load_static_rules = async path =>
+ (await readFile(path, 'utf-8'))
+ .split('\n')
+ .filter(e => e)
+
+ const get_current_rules = async () =>
+ (await exec('iptables -S INPUT'))
+ .stdout
+ .split('\n')
+ .filter(e => e.match(/^-A INPUT/g))
+
+ const compute_rules_to_add = (current, target) =>
+ target.filter(r => !current.includes(r))
+
+ const compute_rules_to_del = (current, target) =>
+ current
+ .filter(r => !target.includes(r))
+ .map(r => r.replace(/^-A INPUT/g, '-D INPUT'))
+
+ const update_rules = async (current, target) =>
+ await Promise.all([
+ ...compute_rules_to_del(current, target),
+ ...compute_rules_to_add(current, target)
+ ].map(r => exec(`iptables ${r}`)))
+
+ const build_target_rules = (tag_list) =>
+ tag_list
+ .map(t => /^public_port=(\d+)(-(\d+))?\/(udp|tcp)/g.exec(t))
+ .filter(t => t)
+ .map(t => new Object({ start: t[1], stop: t[3], protocol: t[4] }))
+ .map(t => t.stop
+ ? `-A INPUT -p ${t.protocol} --match multiport --dports ${t.start}:${t.stop} -j ACCEPT`
+ : `-A INPUT -p ${t.protocol} --dport ${t.start} -j ACCEPT`)
+
+ const do_log = (tag_list, r) => {
+ //log('[iptables]', tag_list)
+ log(`[iptables] ran ${r.length} commands`)
+ }
+
+ const static_rules = path ? await load_static_rules(path) : []
+ log(`[iptables] initialized with ${static_rules.length} static rules`)
+ return async tag_list =>
+ do_log(
+ tag_list,
+ await update_rules(
+ await get_current_rules(),
+ [...static_rules, ...build_target_rules(tag_list)]))
+}
diff --git a/docker/tag-config/src/injector/upnp.mjs b/docker/tag-config/src/injector/upnp.mjs
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/docker/tag-config/src/injector/upnp.mjs
diff --git a/docker/tag-config/src/io/files.mjs b/docker/tag-config/src/io/files.mjs
new file mode 100644
index 0000000..c3eca1b
--- /dev/null
+++ b/docker/tag-config/src/io/files.mjs
@@ -0,0 +1,8 @@
+'use strict'
+
+import fs from 'fs'
+
+export const readFile = (file, opts) =>
+ new Promise((resolve, reject) =>
+ fs.readFile(file, opts, (err, data) =>
+ err ? reject(err) : resolve(data)))
diff --git a/docker/tag-config/src/io/run.mjs b/docker/tag-config/src/io/run.mjs
new file mode 100644
index 0000000..8774043
--- /dev/null
+++ b/docker/tag-config/src/io/run.mjs
@@ -0,0 +1,9 @@
+'use strict'
+
+import child_process from 'child_process'
+
+export const exec = (cmd, opts) =>
+ new Promise((resolve, reject) =>
+ child_process.exec(cmd, opts, (error, stdout, stderr) =>
+ error ? reject({err: error, stdout: stdout, stderr: stderr}) : resolve({stdout: stdout, stderr: stderr})))
+
diff --git a/docker/tag-config/static.iptables b/docker/tag-config/static.iptables
new file mode 100644
index 0000000..d9e7d38
--- /dev/null
+++ b/docker/tag-config/static.iptables
@@ -0,0 +1,10 @@
+-A INPUT -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
+-A INPUT -i docker0 -j ACCEPT
+-A INPUT -s 127.0.0.0/8 -j ACCEPT
+-A INPUT -s 192.168.1.2/32 -j ACCEPT
+-A INPUT -s 192.168.1.3/32 -j ACCEPT
+-A INPUT -s 192.168.1.4/32 -j ACCEPT
+-A INPUT -p udp -m udp --dport 53 -j ACCEPT
+-A INPUT -p tcp -m tcp --dport 53 -j ACCEPT
+-A INPUT -p tcp -m tcp --dport 22 -j ACCEPT
+-A INPUT -p tcp -m tcp --dport 110 -j ACCEPT
diff --git a/docker/tag-config/test/io.mjs b/docker/tag-config/test/io.mjs
new file mode 100644
index 0000000..d88ad15
--- /dev/null
+++ b/docker/tag-config/test/io.mjs
@@ -0,0 +1,10 @@
+import chai from 'chai'
+import { readFile } from '../src/io/files.mjs'
+const expect = chai.expect
+
+export default [
+ (async () => {
+ const dirname = import.meta.url.replace(/^file:\/\//g, '').replace(/io.mjs$/g, '')
+ expect(await readFile(`${dirname}/../package.json`, 'utf-8')).to.include('Quentin')
+ })
+]
diff --git a/docker/tag-config/test/iptables.mjs b/docker/tag-config/test/iptables.mjs
new file mode 100644
index 0000000..1ae1cb0
--- /dev/null
+++ b/docker/tag-config/test/iptables.mjs
@@ -0,0 +1,28 @@
+'use strict'
+
+import chai from 'chai'
+import iptables from '../src/injector/iptables.mjs'
+const expect = chai.expect
+
+export default [
+ (async () => {
+ const effective_actions = []
+ const expected_actions = [
+ 'iptables -A INPUT -p tcp --dport 56 -j ACCEPT',
+ 'iptables -A INPUT -p tcp --dport 53 -j ACCEPT',
+ 'iptables -A INPUT -p udp --match multiport --dports 25630:25999 -j ACCEPT',
+ 'iptables -D INPUT -p tcp --dport 54 -j ACCEPT'
+ ]
+
+ const mockLog = () => {}
+ const mockReadFile = (file, opt) => '-A INPUT -p tcp --dport 53 -j ACCEPT'
+ const mockExecCommand = (cmd, opts) => {
+ if (cmd.match(/^iptables -S/g)) return { stdout: '-A INPUT -p tcp --dport 54 -j ACCEPT' }
+ else effective_actions.push(cmd)
+ return { stdout: '' } }
+
+ const fw = await iptables('static', mockReadFile, mockExecCommand, mockLog)
+ await fw(['public_port=56/tcp', 'public_port=25630-25999/udp', 'public_port=13', 'traefik.entrypoints=Host:im.deuxfleurs.fr;PathPrefix:/_matrix'])
+ expect(effective_actions).to.have.members(expected_actions)
+ })
+]
diff --git a/docker/tag-config/test/runner.mjs b/docker/tag-config/test/runner.mjs
new file mode 100644
index 0000000..b4da1de
--- /dev/null
+++ b/docker/tag-config/test/runner.mjs
@@ -0,0 +1,28 @@
+'use strict'
+
+import io from './io.mjs'
+import iptables from './iptables.mjs'
+
+(async () => {
+ const res = await [
+ ...io,
+ ...iptables
+ ].map(async f => {
+ try {
+ await f()
+ return 'passed'
+ }
+ catch(e) {
+ console.error(e)
+ return 'failed'
+ }
+ }).reduce(async (acc, r) => {
+ const accumulator = await acc
+ const result = await r
+ accumulator.total++
+ accumulator[result]++
+ return accumulator
+ }, {total: 0, passed: 0, failed: 0})
+
+ console.log(`Done. passed: ${res.passed}, failed: ${res.failed}, total: ${res.total}`)
+})()