Merge pull request #913 from balena-io/synchronous-iptables

Run iptables rules synchronous to avoid locking errors
This commit is contained in:
CameronDiver 2019-02-19 18:08:45 +00:00 committed by GitHub
commit 6e603928d1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 5 deletions

View File

@ -7,10 +7,17 @@ export const execAsync: (args: string) => Bluebird<string> = Bluebird.promisify(
);
function applyIptablesArgs(args: string): Bluebird<void> {
return Bluebird.all([
execAsync(`iptables ${args}`),
execAsync(`ip6tables ${args}`),
]).return();
let err: Error | null = null;
// We want to run both commands regardless, but also rethrow an error
// if one of them fails
return execAsync(`iptables ${args}`)
.catch(e => (err = e))
.then(() => execAsync(`ip6tables ${args}`).catch(e => (err = e)))
.then(() => {
if (err != null) {
throw err;
}
});
}
function clearIptablesRule(rule: string): Bluebird<void> {

View File

@ -29,7 +29,7 @@ describe 'iptables', ->
it "falls back to blocking the port with DROP if there's no REJECT support", ->
stub(iptables, 'execAsync').callsFake (cmd) ->
if /REJECT$/.test(cmd)
Promise.reject()
Promise.reject(new Error())
else
Promise.resolve()
iptables.rejectOnAllInterfacesExcept(['foo', 'bar'], 42)