-
Notifications
You must be signed in to change notification settings - Fork 33
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
Expose ip tables package #361
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
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
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
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
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
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 |
---|---|---|
|
@@ -10,7 +10,7 @@ import ( | |
|
||
log "github.com/sirupsen/logrus" | ||
|
||
util "github.com/linkerd/linkerd2-proxy-init/internal/util" | ||
util "github.com/linkerd/linkerd2-proxy-init/pkg/util" | ||
) | ||
|
||
const ( | ||
|
@@ -35,15 +35,15 @@ const ( | |
|
||
var ( | ||
// ExecutionTraceID provides a unique identifier for this script's execution. | ||
ExecutionTraceID = strconv.Itoa(int(time.Now().Unix())) | ||
executionTraceID = strconv.Itoa(int(time.Now().Unix())) | ||
|
||
preroutingRuleRegex = regexp.MustCompile(`(?m)^-A PREROUTING (.+ )?-j PROXY_INIT_REDIRECT`) | ||
outputRuleRegex = regexp.MustCompile(`(?m)^-A OUTPUT (.+ )?-j PROXY_INIT_OUTPUT`) | ||
redirectChainRegex = regexp.MustCompile(`(?m)^:PROXY_INIT_REDIRECT `) | ||
outputChainRegex = regexp.MustCompile(`(?m)^:PROXY_INIT_OUTPUT `) | ||
) | ||
|
||
// FirewallConfiguration specifies how to configure a pod's iptables. | ||
// FirewallConfiguration specifies how to configure iptables. | ||
type FirewallConfiguration struct { | ||
Mode string | ||
PortsToRedirectInbound []int | ||
|
@@ -58,13 +58,14 @@ type FirewallConfiguration struct { | |
UseWaitFlag bool | ||
BinPath string | ||
SaveBinPath string | ||
ContinueOnError bool | ||
} | ||
|
||
// ConfigureFirewall configures a pod's internal iptables to redirect all desired traffic through the proxy, allowing for | ||
// the pod to join the service mesh. A lot of this logic was based on | ||
// ConfigureFirewall configures iptables to redirect all desired traffic through the proxy, allowing for | ||
// the workload to join the service mesh. A lot of this logic was based on | ||
// https://github.com/istio/istio/blob/e83411e/pilot/docker/prepare_proxy.sh | ||
func ConfigureFirewall(firewallConfiguration FirewallConfiguration) error { | ||
log.Debugf("tracing script execution as [%s]", ExecutionTraceID) | ||
log.Debugf("tracing script execution as [%s]", executionTraceID) | ||
log.Debugf("using '%s' to set-up firewall rules", firewallConfiguration.BinPath) | ||
log.Debugf("using '%s' to list all available rules", firewallConfiguration.SaveBinPath) | ||
|
||
|
@@ -90,7 +91,44 @@ func ConfigureFirewall(firewallConfiguration FirewallConfiguration) error { | |
} | ||
|
||
if _, err := executeCommand(firewallConfiguration, cmd); err != nil { | ||
return err | ||
if !firewallConfiguration.ContinueOnError { | ||
return err | ||
} | ||
|
||
log.Debugf("continuing despite error: %s", err) | ||
} | ||
} | ||
|
||
_, _ = executeCommand(firewallConfiguration, firewallConfiguration.makeShowAllRules()) | ||
|
||
return nil | ||
} | ||
|
||
// CleanupFirewallConfig removed the iptables rules that have been added as a result of | ||
// calling ConfigureFirewall. | ||
func CleanupFirewallConfig(firewallConfiguration FirewallConfiguration) error { | ||
log.Debugf("tracing script execution as [%s]", executionTraceID) | ||
log.Debugf("using '%s' to clean-up firewall rules", firewallConfiguration.BinPath) | ||
log.Debugf("using '%s' to list all available rules", firewallConfiguration.SaveBinPath) | ||
|
||
commands := make([]*exec.Cmd, 0) | ||
commands = firewallConfiguration.cleanupRules(commands) | ||
|
||
if firewallConfiguration.UseWaitFlag { | ||
log.Debug("'useWaitFlag' set: iptables will wait for xtables to become available") | ||
} | ||
|
||
for _, cmd := range commands { | ||
if firewallConfiguration.UseWaitFlag { | ||
cmd.Args = append(cmd.Args, "-w") | ||
} | ||
|
||
if _, err := executeCommand(firewallConfiguration, cmd); err != nil { | ||
if !firewallConfiguration.ContinueOnError { | ||
return err | ||
} | ||
|
||
log.Debugf("continuing despite error: %s", err) | ||
} | ||
} | ||
|
||
|
@@ -99,10 +137,40 @@ func ConfigureFirewall(firewallConfiguration FirewallConfiguration) error { | |
return nil | ||
} | ||
|
||
func (fc FirewallConfiguration) cleanupRules(commands []*exec.Cmd) []*exec.Cmd { | ||
// delete ref from prerouting | ||
commands = append( | ||
commands, | ||
fc.makeJumpFromChainToAnotherForAllProtocols( | ||
IptablesPreroutingChainName, | ||
redirectChainName, | ||
"install-proxy-init-prerouting", | ||
true)) | ||
|
||
// delete ref from output | ||
commands = append( | ||
commands, | ||
fc.makeJumpFromChainToAnotherForAllProtocols( | ||
IptablesOutputChainName, | ||
outputChainName, | ||
"install-proxy-init-output", | ||
true)) | ||
|
||
// flush chains | ||
commands = append(commands, fc.makeFlushChain(outputChainName)) | ||
commands = append(commands, fc.makeFlushChain(redirectChainName)) | ||
|
||
// delete chains | ||
commands = append(commands, fc.makeDeleteChain(outputChainName)) | ||
commands = append(commands, fc.makeDeleteChain(redirectChainName)) | ||
|
||
return commands | ||
} | ||
|
||
// formatComment is used to format iptables comments in such way that it is possible to identify when the rules were added. | ||
// This helps debug when iptables has some stale rules from previous runs, something that can happen frequently on minikube. | ||
func formatComment(text string) string { | ||
return fmt.Sprintf("proxy-init/%s/%s", text, ExecutionTraceID) | ||
return fmt.Sprintf("proxy-init/%s", text) | ||
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. Now that the timestamp is not included, this function comment should be amended to not make mention of "when the rules were added". |
||
} | ||
|
||
func (fc FirewallConfiguration) addOutgoingTrafficRules(existingRules []byte, commands []*exec.Cmd) []*exec.Cmd { | ||
|
@@ -270,6 +338,12 @@ func (fc FirewallConfiguration) makeFlushChain(name string) *exec.Cmd { | |
"-F", name) | ||
} | ||
|
||
func (fc FirewallConfiguration) makeDeleteChain(name string) *exec.Cmd { | ||
return exec.Command(fc.BinPath, | ||
"-t", "nat", | ||
"-X", name) | ||
} | ||
|
||
func (fc FirewallConfiguration) makeCreateNewChain(name string) *exec.Cmd { | ||
return exec.Command(fc.BinPath, | ||
"-t", "nat", | ||
|
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
File renamed without changes.
File renamed without changes.
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
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
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.