salt icon indicating copy to clipboard operation
salt copied to clipboard

Implement iptables state

Open techhat opened this issue 13 years ago • 19 comments

Managing iptables is going to be difficult at best. This issue is here to converse about the proper way to handle it.

In iptables, there are always at least two elements that are being handled: a table (such as filter) and a chain (such as INPUT). Every chain will have a default policy (such as ACCEPT), and an ordered list of rules. Because the default policy is not always enough, it is frequently set to ACCEPT, and the final rule of the chain applies the actual policy.

Chains are evaluated by iptables in order. In a stateless chain, order is generally functionally unimportant: you block what you block, accept what you accept, and then apply a default policy to anything else. In a stateful chain, order is crucial: you usually have a header (a set of rules to set up the state), the set of rules to be evaluated (order is frequently unimportant here) and a footer (effectively, a default chain policy, evaluated before the actual default chain policy).

iptables itself knows nothing about rules on disk; iptables-apply and iptables-save are tools used to persist rules from disk to memory. The execution module addresses this to some extent. I think the correct action here is to persist the rules to disk, and not apply them to memory until all of the rules have been persisted. I also think that the user should have the option to not do this (persist=True vs persist=False).

The biggest problem we have is that as it stands, the current iptables functionality is non-deterministic: no matter what we do, every single highstate run will cause the iptables rules to be refreshed. I would never want to do this in production.

We also have a problem with abstraction. What if a rule is specified that is functionally identical to an existing rule, but is generated with matches in a different order? There would be a conflict. It seems to me that we'll need to abstract each rule.

Let me lay out a simple data structure to start the conversation:

http:
  iptables:
    - managed
    - persist: True
    - table: filter
    - chain: INPUT
    - target: ACCEPT
    - matches:
        - state
        - dport: 80
        - proto: tcp
        - sports: 1025-65535

I fully expect this structure to be beaten up and torn apart during this conversation.

techhat avatar Feb 05 '13 19:02 techhat

Okay, here's the plan.

  1. When the state module runs, it checks the __context__ dict to see if any rules have been stored. If not, it initializes two rule sets (orig and new), based on the current, in-memory rules.
__context__['iptables.orig']['table']['CHAIN']
__context__['iptables.new']['table']['CHAIN']
  1. If the rule is not in __context__:
- add to context (using the configured index, or append or prepend, default to prepend)
- apply to iptables (in memory)
- persist to disk if persist is set to True (default to True)
  1. Check to see if salt can still communicate with the master (configurable in config.option). If it cannot communicate with the master, then revert to the rules in __context__['iptables.orig']['table']['CHAIN'] and apply them.

  2. The very last rule in the state should persist. If it does not, then only the in-memory rules will be applied (changes will not persist to disk).

techhat avatar Feb 05 '13 21:02 techhat

Tagging @toddejohnson.

techhat avatar Feb 05 '13 21:02 techhat

@techhat Feel free to close https://github.com/saltstack/salt/pull/3594 add my changes to your commit. I don't care about credit if it means progress.

toddejohnson avatar Feb 05 '13 23:02 toddejohnson

You're a good man, @toddejohnson.

techhat avatar Feb 06 '13 00:02 techhat

Any progress on this? Would really like to implement iptables management from a state level and unsure of the intricacies of salt/iptables to be able to write this myself.

masonhugus avatar Feb 26 '13 15:02 masonhugus

This is currently under heavy development for 0.14.0, it might take a little longer, but that is the goal

thatch45 avatar Feb 26 '13 17:02 thatch45

How is the progress? We are now on 0.15.1, and I would love to use this. :)

JasonSwindle avatar May 10 '13 15:05 JasonSwindle

Yes, this keeps getting bumped, we have designed it and added backend features to the state system to support the iptables state, it is just a little more work than originally anticipated.

thatch45 avatar May 10 '13 16:05 thatch45

Do not forget about ip6tables.

faust avatar May 23 '13 07:05 faust

Is there a branch where this is being worked on?

hdonnay avatar May 31 '13 20:05 hdonnay

Bump :)

tomasfejfar avatar Jun 20 '13 15:06 tomasfejfar

@techhat, can we bump this up once the next salt-cloud is released?

thatch45 avatar Jun 20 '13 15:06 thatch45

I put it on the next release milestone so we see it more often.

basepi avatar Jun 20 '13 16:06 basepi

This issue isn't resolved yet.

techhat avatar Jul 10 '13 21:07 techhat

This doesn't seem to work in 0.16. It looks like this was closed and then re-opened. Does that mean I'm seeing it correctly that it doesn't work?

jslusher avatar Aug 23 '13 21:08 jslusher

JFYI - we "replaced" iptables with ferm and it works great - using ferm.d/ dir and salt's file templating ability it works great and offer better control of the rules than iptables... you can also test-run the setting with auto-revert in 30s if anything goes terribly wrong - like you blocking 22 on remote server :))

tomasfejfar avatar Aug 25 '13 11:08 tomasfejfar

@jslusher Correct, it is not yet done. @UtahDave just thought the above pull req fixed the issue, though it actually needs more work, thus the close-reopen action.

basepi avatar Aug 26 '13 20:08 basepi

I know I've used iptables states in 2014.7 and 2015.5 with reasonable success (I also submitted some bugfixes). How many of these issues are still present?

anlutro avatar May 22 '15 06:05 anlutro

The iptables state has drifted from the original spec, but it's been used for some time by a number of people, so I think we can close this. Thanks @anlutro!

techhat avatar May 22 '15 12:05 techhat