mirror of
https://github.com/balena-os/balena-supervisor.git
synced 2024-12-26 00:41:07 +00:00
7b1f03ced5
This allows a response to an input with dport=`supevisor api port` and is required when the host OS is doing stateful firewalling. This should not affect things when stateful firewalling is not in effect, as the standard OUTPUT chain policy is ACCEPT, so we're just being explicit about it. Change-type: patch Backport-to: next, current, sunset Signed-off-by: Cameron Diver <cameron@balena.io>
139 lines
5.0 KiB
TypeScript
139 lines
5.0 KiB
TypeScript
import * as Bluebird from 'bluebird';
|
|
import { stub } from 'sinon';
|
|
import { expect } from './lib/chai-config';
|
|
|
|
import * as iptables from '../src/lib/iptables';
|
|
|
|
describe('iptables', async () => {
|
|
it('calls iptables to delete and recreate rules to block a port', async () => {
|
|
stub(iptables, 'execAsync').returns(Bluebird.resolve(''));
|
|
|
|
await iptables.rejectOnAllInterfacesExcept(['foo', 'bar'], 42);
|
|
expect((iptables.execAsync as sinon.SinonStub).callCount).to.equal(16);
|
|
expect(iptables.execAsync).to.be.calledWith(
|
|
'iptables -D INPUT -p tcp --dport 42 -i foo -j ACCEPT',
|
|
);
|
|
expect(iptables.execAsync).to.be.calledWith(
|
|
'iptables -I INPUT -p tcp --dport 42 -i foo -j ACCEPT',
|
|
);
|
|
expect(iptables.execAsync).to.be.calledWith(
|
|
'iptables -D INPUT -p tcp --dport 42 -i bar -j ACCEPT',
|
|
);
|
|
expect(iptables.execAsync).to.be.calledWith(
|
|
'iptables -I INPUT -p tcp --dport 42 -i bar -j ACCEPT',
|
|
);
|
|
expect(iptables.execAsync).to.be.calledWith(
|
|
'iptables -D OUTPUT -p tcp --sport 42 -m state --state ESTABLISHED -j ACCEPT',
|
|
);
|
|
expect(iptables.execAsync).to.be.calledWith(
|
|
'iptables -A OUTPUT -p tcp --sport 42 -m state --state ESTABLISHED -j ACCEPT',
|
|
);
|
|
expect(iptables.execAsync).to.be.calledWith(
|
|
'iptables -D INPUT -p tcp --dport 42 -j REJECT',
|
|
);
|
|
expect(iptables.execAsync).to.be.calledWith(
|
|
'iptables -A INPUT -p tcp --dport 42 -j REJECT',
|
|
);
|
|
expect(iptables.execAsync).to.be.calledWith(
|
|
'ip6tables -D INPUT -p tcp --dport 42 -i foo -j ACCEPT',
|
|
);
|
|
expect(iptables.execAsync).to.be.calledWith(
|
|
'ip6tables -I INPUT -p tcp --dport 42 -i foo -j ACCEPT',
|
|
);
|
|
expect(iptables.execAsync).to.be.calledWith(
|
|
'ip6tables -D INPUT -p tcp --dport 42 -i bar -j ACCEPT',
|
|
);
|
|
expect(iptables.execAsync).to.be.calledWith(
|
|
'ip6tables -I INPUT -p tcp --dport 42 -i bar -j ACCEPT',
|
|
);
|
|
expect(iptables.execAsync).to.be.calledWith(
|
|
'ip6tables -D OUTPUT -p tcp --sport 42 -m state --state ESTABLISHED -j ACCEPT',
|
|
);
|
|
expect(iptables.execAsync).to.be.calledWith(
|
|
'ip6tables -A OUTPUT -p tcp --sport 42 -m state --state ESTABLISHED -j ACCEPT',
|
|
);
|
|
expect(iptables.execAsync).to.be.calledWith(
|
|
'ip6tables -D INPUT -p tcp --dport 42 -j REJECT',
|
|
);
|
|
expect(iptables.execAsync).to.be.calledWith(
|
|
'ip6tables -A INPUT -p tcp --dport 42 -j REJECT',
|
|
);
|
|
(iptables.execAsync as sinon.SinonStub).restore();
|
|
});
|
|
|
|
it("falls back to blocking the port with DROP if there's no REJECT support", async () => {
|
|
stub(iptables, 'execAsync').callsFake(cmd => {
|
|
if (/REJECT$/.test(cmd)) {
|
|
return Bluebird.reject(new Error());
|
|
} else {
|
|
return Bluebird.resolve('');
|
|
}
|
|
});
|
|
|
|
await iptables.rejectOnAllInterfacesExcept(['foo', 'bar'], 42);
|
|
expect((iptables.execAsync as sinon.SinonStub).callCount).to.equal(20);
|
|
expect(iptables.execAsync).to.be.calledWith(
|
|
'iptables -D INPUT -p tcp --dport 42 -i foo -j ACCEPT',
|
|
);
|
|
expect(iptables.execAsync).to.be.calledWith(
|
|
'iptables -I INPUT -p tcp --dport 42 -i foo -j ACCEPT',
|
|
);
|
|
expect(iptables.execAsync).to.be.calledWith(
|
|
'iptables -D INPUT -p tcp --dport 42 -i bar -j ACCEPT',
|
|
);
|
|
expect(iptables.execAsync).to.be.calledWith(
|
|
'iptables -I INPUT -p tcp --dport 42 -i bar -j ACCEPT',
|
|
);
|
|
expect(iptables.execAsync).to.be.calledWith(
|
|
'iptables -D OUTPUT -p tcp --sport 42 -m state --state ESTABLISHED -j ACCEPT',
|
|
);
|
|
expect(iptables.execAsync).to.be.calledWith(
|
|
'iptables -A OUTPUT -p tcp --sport 42 -m state --state ESTABLISHED -j ACCEPT',
|
|
);
|
|
expect(iptables.execAsync).to.be.calledWith(
|
|
'iptables -D INPUT -p tcp --dport 42 -j REJECT',
|
|
);
|
|
expect(iptables.execAsync).to.be.calledWith(
|
|
'iptables -A INPUT -p tcp --dport 42 -j REJECT',
|
|
);
|
|
expect(iptables.execAsync).to.be.calledWith(
|
|
'iptables -D INPUT -p tcp --dport 42 -j DROP',
|
|
);
|
|
expect(iptables.execAsync).to.be.calledWith(
|
|
'iptables -A INPUT -p tcp --dport 42 -j DROP',
|
|
);
|
|
expect(iptables.execAsync).to.be.calledWith(
|
|
'ip6tables -D INPUT -p tcp --dport 42 -i foo -j ACCEPT',
|
|
);
|
|
expect(iptables.execAsync).to.be.calledWith(
|
|
'ip6tables -I INPUT -p tcp --dport 42 -i foo -j ACCEPT',
|
|
);
|
|
expect(iptables.execAsync).to.be.calledWith(
|
|
'ip6tables -D INPUT -p tcp --dport 42 -i bar -j ACCEPT',
|
|
);
|
|
expect(iptables.execAsync).to.be.calledWith(
|
|
'ip6tables -I INPUT -p tcp --dport 42 -i bar -j ACCEPT',
|
|
);
|
|
expect(iptables.execAsync).to.be.calledWith(
|
|
'ip6tables -D OUTPUT -p tcp --sport 42 -m state --state ESTABLISHED -j ACCEPT',
|
|
);
|
|
expect(iptables.execAsync).to.be.calledWith(
|
|
'ip6tables -A OUTPUT -p tcp --sport 42 -m state --state ESTABLISHED -j ACCEPT',
|
|
);
|
|
expect(iptables.execAsync).to.be.calledWith(
|
|
'ip6tables -D INPUT -p tcp --dport 42 -j REJECT',
|
|
);
|
|
expect(iptables.execAsync).to.be.calledWith(
|
|
'ip6tables -A INPUT -p tcp --dport 42 -j REJECT',
|
|
);
|
|
expect(iptables.execAsync).to.be.calledWith(
|
|
'ip6tables -D INPUT -p tcp --dport 42 -j DROP',
|
|
);
|
|
expect(iptables.execAsync).to.be.calledWith(
|
|
'ip6tables -A INPUT -p tcp --dport 42 -j DROP',
|
|
);
|
|
|
|
(iptables.execAsync as sinon.SinonStub).restore();
|
|
});
|
|
});
|