-
Notifications
You must be signed in to change notification settings - Fork 38.7k
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
Add a pkg for iptables support #1597
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
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 |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/* | ||
Copyright 2014 Google Inc. All rights reserved. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
// Package iptables provides an interface and implementations for running iptables commands. | ||
package iptables |
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 |
---|---|---|
@@ -0,0 +1,176 @@ | ||
/* | ||
Copyright 2014 Google Inc. All rights reserved. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package iptables | ||
|
||
import ( | ||
"fmt" | ||
"sync" | ||
|
||
utilexec "github.com/GoogleCloudPlatform/kubernetes/pkg/util/exec" | ||
"github.com/golang/glog" | ||
) | ||
|
||
// An injectable interface for running iptables commands. Implementations must be goroutine-safe. | ||
type Interface interface { | ||
// EnsureChain checks if the specified chain exists and, if not, creates it. If the chain existed, return true. | ||
EnsureChain(table Table, chain Chain) (bool, error) | ||
// FlushChain clears the specified chain. | ||
FlushChain(table Table, chain Chain) error | ||
// EnsureRule checks if the specified rule is present and, if not, creates it. If the rule existed, return true. | ||
EnsureRule(table Table, chain Chain, args ...string) (bool, error) | ||
// DeleteRule checks if the specified rule is present and, if so, deletes it. | ||
DeleteRule(table Table, chain Chain, args ...string) error | ||
} | ||
|
||
type Table string | ||
|
||
const ( | ||
TableNAT Table = "nat" | ||
) | ||
|
||
type Chain string | ||
|
||
const ( | ||
ChainPrerouting Chain = "PREROUTING" | ||
ChainOutput Chain = "OUTPUT" | ||
) | ||
|
||
// runner implements Interface in terms of exec("iptables"). | ||
type runner struct { | ||
mu sync.Mutex | ||
exec utilexec.Interface | ||
} | ||
|
||
// New returns a new Interface which will exec iptables. | ||
func New(exec utilexec.Interface) Interface { | ||
return &runner{exec: exec} | ||
} | ||
|
||
// EnsureChain is part of Interface. | ||
func (runner *runner) EnsureChain(table Table, chain Chain) (bool, error) { | ||
fullArgs := makeFullArgs(table, chain) | ||
|
||
runner.mu.Lock() | ||
defer runner.mu.Unlock() | ||
|
||
out, err := runner.run(opCreateChain, fullArgs) | ||
if err != nil { | ||
if ee, ok := err.(utilexec.ExitError); ok { | ||
if ee.Exited() && ee.ExitStatus() == 1 { | ||
return true, nil | ||
} | ||
} | ||
return false, fmt.Errorf("error creating chain %q: %s: %s", chain, err, out) | ||
} | ||
return false, nil | ||
} | ||
|
||
// FlushChain is part of Interface. | ||
func (runner *runner) FlushChain(table Table, chain Chain) error { | ||
fullArgs := makeFullArgs(table, chain) | ||
|
||
runner.mu.Lock() | ||
defer runner.mu.Unlock() | ||
|
||
out, err := runner.run(opFlushChain, fullArgs) | ||
if err != nil { | ||
return fmt.Errorf("error flushing chain %q: %s: %s", chain, err, out) | ||
} | ||
return nil | ||
} | ||
|
||
// EnsureRule is part of Interface. | ||
func (runner *runner) EnsureRule(table Table, chain Chain, args ...string) (bool, error) { | ||
fullArgs := makeFullArgs(table, chain, args...) | ||
|
||
runner.mu.Lock() | ||
defer runner.mu.Unlock() | ||
|
||
exists, err := runner.checkRule(fullArgs) | ||
if err != nil { | ||
return false, err | ||
} | ||
if exists { | ||
return true, nil | ||
} | ||
out, err := runner.run(opAppendRule, fullArgs) | ||
if err != nil { | ||
return false, fmt.Errorf("error appending rule: %s: %s", err, out) | ||
} | ||
return false, nil | ||
} | ||
|
||
// DeleteRule is part of Interface. | ||
func (runner *runner) DeleteRule(table Table, chain Chain, args ...string) error { | ||
fullArgs := makeFullArgs(table, chain, args...) | ||
|
||
runner.mu.Lock() | ||
defer runner.mu.Unlock() | ||
|
||
exists, err := runner.checkRule(fullArgs) | ||
if err != nil { | ||
return err | ||
} | ||
if !exists { | ||
return nil | ||
} | ||
out, err := runner.run(opDeleteRule, fullArgs) | ||
if err != nil { | ||
return fmt.Errorf("error deleting rule: %s: %s", err, out) | ||
} | ||
return nil | ||
} | ||
|
||
func (runner *runner) run(op operation, args []string) ([]byte, error) { | ||
const iptablesCmd = "iptables" | ||
|
||
fullArgs := append([]string{string(op)}, args...) | ||
glog.V(1).Infof("running iptables %s %v", string(op), args) | ||
return runner.exec.Command(iptablesCmd, fullArgs...).CombinedOutput() | ||
// Don't log err here - callers might not think it is an error. | ||
} | ||
|
||
// Returns (bool, nil) if it was able to check the existence of the rule, or | ||
// (<undefined>, error) if the process of checking failed. | ||
func (runner *runner) checkRule(args []string) (bool, error) { | ||
out, err := runner.run(opCheckRule, args) | ||
if err == nil { | ||
return true, nil | ||
} | ||
if ee, ok := err.(utilexec.ExitError); ok { | ||
// iptables uses exit(1) to indicate a failure of the operation, | ||
// as compared to a malformed commandline, for example. | ||
if ee.Exited() && ee.ExitStatus() == 1 { | ||
return false, nil | ||
} | ||
} | ||
return false, fmt.Errorf("error checking rule: %s: %s", err, out) | ||
} | ||
|
||
type operation string | ||
|
||
const ( | ||
opCreateChain operation = "-N" | ||
opFlushChain operation = "-F" | ||
opAppendRule operation = "-A" | ||
opCheckRule operation = "-C" | ||
opDeleteRule operation = "-D" | ||
) | ||
|
||
func makeFullArgs(table Table, chain Chain, args ...string) []string { | ||
return append([]string{string(chain), "-t", string(table)}, args...) | ||
} |
Oops, something went wrong.
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.
Is the caller responsible for handling the inherent raciness of iptables, or should this interface provide that?
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.
Specifically that multiple programs can be iptabling at the same time on the machine? I don't see how this can solve that problem.
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.
Multiple goroutines is at least under our control. If it's not worth handling in proc races I'd add a comment to the package doc to that effect.
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.
Ahh, easy fix. Done.