-
Notifications
You must be signed in to change notification settings - Fork 257
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adds ability to use iptables-restore #124
Open
trozet
wants to merge
1
commit into
coreos:main
Choose a base branch
from
trozet:iptables-restore
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+136
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -72,6 +72,7 @@ const ( | |
|
||
type IPTables struct { | ||
path string | ||
rpath string | ||
proto Protocol | ||
hasCheck bool | ||
hasWait bool | ||
|
@@ -155,6 +156,12 @@ func New(opts ...option) (*IPTables, error) { | |
} | ||
ipt.path = path | ||
|
||
rpath, err := exec.LookPath(getIptablesRestoreCommand(ipt.proto)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
ipt.rpath = rpath | ||
|
||
vstring, err := getIptablesVersionString(path) | ||
if err != nil { | ||
return nil, fmt.Errorf("could not get iptables version: %v", err) | ||
|
@@ -233,6 +240,23 @@ func (ipt *IPTables) InsertUnique(table, chain string, pos int, rulespec ...stri | |
return nil | ||
} | ||
|
||
// Restore replaces specified chains and rules in a specific table | ||
// rulesMap is keyed by chain name, and holds slices of rulespecs | ||
// Only chains specified in the map will be flushed and replaced. Other chains will not be affected. | ||
func (ipt *IPTables) Restore(table string, rulesMap map[string][][]string) error { | ||
restoreRules := "*" + table | ||
for chain, rules := range rulesMap { | ||
restoreRules += "\n" + fmt.Sprintf(":%s - [0:0]", strings.ToUpper(chain)) | ||
for _, rule := range rules { | ||
restoreRules += "\n" + fmt.Sprintf("-I %s %s", chain, strings.Join(rule, " ")) | ||
} | ||
} | ||
restoreRules += "\nCOMMIT\n" | ||
cmd := []string{"-n"} | ||
|
||
return ipt.runRestore(cmd, restoreRules) | ||
} | ||
|
||
// Append appends rulespec to specified table/chain | ||
func (ipt *IPTables) Append(table, chain string, rulespec ...string) error { | ||
cmd := append([]string{"-t", table, "-A", chain}, rulespec...) | ||
|
@@ -554,6 +578,57 @@ func (ipt *IPTables) run(args ...string) error { | |
return ipt.runWithOutput(args, nil) | ||
} | ||
|
||
// runWithOutput runs an iptables command with the given arguments, | ||
// writing any stdout output to the given writer | ||
func (ipt *IPTables) runRestore(args []string, input string) error { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't know if the maintainers are planning to merge this PR but it'd be nice to make this function public or maybe create a wrapper |
||
args = append([]string{ipt.rpath}, args...) | ||
if ipt.hasWait { | ||
args = append(args, "--wait") | ||
if ipt.timeout != 0 && ipt.waitSupportSecond { | ||
args = append(args, strconv.Itoa(ipt.timeout)) | ||
} | ||
} else { | ||
fmu, err := newXtablesFileLock() | ||
if err != nil { | ||
return err | ||
} | ||
ul, err := fmu.tryLock() | ||
if err != nil { | ||
syscall.Close(fmu.fd) | ||
return err | ||
} | ||
defer ul.Unlock() | ||
} | ||
|
||
var stderr bytes.Buffer | ||
cmd := exec.Cmd{ | ||
Path: ipt.rpath, | ||
Args: args, | ||
Stderr: &stderr, | ||
} | ||
|
||
stdin, err := cmd.StdinPipe() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
go func() { | ||
defer stdin.Close() | ||
io.WriteString(stdin, input) | ||
}() | ||
|
||
if err := cmd.Run(); err != nil { | ||
switch e := err.(type) { | ||
case *exec.ExitError: | ||
return &Error{*e, cmd, stderr.String(), nil} | ||
default: | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// runWithOutput runs an iptables command with the given arguments, | ||
// writing any stdout output to the given writer | ||
func (ipt *IPTables) runWithOutput(args []string, stdout io.Writer) error { | ||
|
@@ -607,6 +682,15 @@ func getIptablesCommand(proto Protocol) string { | |
} | ||
} | ||
|
||
// getIptablesRestoreCommand returns the correct command for the given protocol, either "iptables" or "ip6tables". | ||
func getIptablesRestoreCommand(proto Protocol) string { | ||
if proto == ProtocolIPv6 { | ||
return "ip6tables-restore" | ||
} else { | ||
return "iptables-restore" | ||
} | ||
} | ||
|
||
// Checks if iptables has the "-C" and "--wait" flag | ||
func getIptablesCommandSupport(v1 int, v2 int, v3 int) (bool, bool, bool, bool) { | ||
return iptablesHasCheckCommand(v1, v2, v3), iptablesHasWaitCommand(v1, v2, v3), iptablesWaitSupportSecond(v1, v2, v3), iptablesHasRandomFully(v1, v2, v3) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here I assumed Insert for the rules...but maybe it would be more appropriate to use Append. Another option is make it part of the rule, or make it a parameter to the function. I dont have any preference.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would personally expect
Append
but perhaps it could be documented that this is the behavior