-
-
Notifications
You must be signed in to change notification settings - Fork 375
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
16 changed files
with
341 additions
and
16 deletions.
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
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,56 @@ | ||
package firewall | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net" | ||
) | ||
|
||
func (c *configurator) SetOutboundSubnets(ctx context.Context, subnets []net.IPNet) (err error) { | ||
c.stateMutex.Lock() | ||
defer c.stateMutex.Unlock() | ||
|
||
if !c.enabled { | ||
c.logger.Info("firewall disabled, only updating allowed subnets internal list") | ||
c.outboundSubnets = make([]net.IPNet, len(subnets)) | ||
copy(c.outboundSubnets, subnets) | ||
return nil | ||
} | ||
|
||
c.logger.Info("setting allowed subnets through firewall...") | ||
|
||
subnetsToAdd := findSubnetsToAdd(c.outboundSubnets, subnets) | ||
subnetsToRemove := findSubnetsToRemove(c.outboundSubnets, subnets) | ||
if len(subnetsToAdd) == 0 && len(subnetsToRemove) == 0 { | ||
return nil | ||
} | ||
|
||
c.removeOutboundSubnets(ctx, subnetsToRemove) | ||
if err := c.addOutboundSubnets(ctx, subnetsToAdd); err != nil { | ||
return fmt.Errorf("cannot set allowed subnets through firewall: %w", err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (c *configurator) removeOutboundSubnets(ctx context.Context, subnets []net.IPNet) { | ||
const remove = true | ||
for _, subnet := range subnets { | ||
if err := c.acceptOutputFromIPToSubnet(ctx, c.defaultInterface, c.localIP, subnet, remove); err != nil { | ||
c.logger.Error("cannot remove outdated outbound subnet through firewall: %s", err) | ||
continue | ||
} | ||
c.outboundSubnets = removeSubnetFromSubnets(c.outboundSubnets, subnet) | ||
} | ||
} | ||
|
||
func (c *configurator) addOutboundSubnets(ctx context.Context, subnets []net.IPNet) error { | ||
const remove = false | ||
for _, subnet := range subnets { | ||
if err := c.acceptOutputFromIPToSubnet(ctx, c.defaultInterface, c.localIP, subnet, remove); err != nil { | ||
return fmt.Errorf("cannot add allowed subnet through firewall: %w", err) | ||
} | ||
c.outboundSubnets = append(c.outboundSubnets, subnet) | ||
} | ||
return nil | ||
} |
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,53 @@ | ||
package firewall | ||
|
||
import ( | ||
"net" | ||
) | ||
|
||
func findSubnetsToAdd(oldSubnets, newSubnets []net.IPNet) (subnetsToAdd []net.IPNet) { | ||
for _, newSubnet := range newSubnets { | ||
found := false | ||
for _, oldSubnet := range oldSubnets { | ||
if subnetsAreEqual(oldSubnet, newSubnet) { | ||
found = true | ||
break | ||
} | ||
} | ||
if !found { | ||
subnetsToAdd = append(subnetsToAdd, newSubnet) | ||
} | ||
} | ||
return subnetsToAdd | ||
} | ||
|
||
func findSubnetsToRemove(oldSubnets, newSubnets []net.IPNet) (subnetsToRemove []net.IPNet) { | ||
for _, oldSubnet := range oldSubnets { | ||
found := false | ||
for _, newSubnet := range newSubnets { | ||
if subnetsAreEqual(oldSubnet, newSubnet) { | ||
found = true | ||
break | ||
} | ||
} | ||
if !found { | ||
subnetsToRemove = append(subnetsToRemove, oldSubnet) | ||
} | ||
} | ||
return subnetsToRemove | ||
} | ||
|
||
func subnetsAreEqual(a, b net.IPNet) bool { | ||
return a.IP.Equal(b.IP) && a.Mask.String() == b.Mask.String() | ||
} | ||
|
||
func removeSubnetFromSubnets(subnets []net.IPNet, subnet net.IPNet) []net.IPNet { | ||
L := len(subnets) | ||
for i := range subnets { | ||
if subnetsAreEqual(subnet, subnets[i]) { | ||
subnets[i] = subnets[L-1] | ||
subnets = subnets[:L-1] | ||
break | ||
} | ||
} | ||
return subnets | ||
} |
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 |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package params | ||
|
||
import ( | ||
"fmt" | ||
"net" | ||
"strings" | ||
) | ||
|
||
// GetOutboundSubnets obtains the CIDR subnets from the comma separated list of the | ||
// environment variable FIREWALL_OUTBOUND_SUBNETS. | ||
func (r *reader) GetOutboundSubnets() (outboundSubnets []net.IPNet, err error) { | ||
const key = "FIREWALL_OUTBOUND_SUBNETS" | ||
s, err := r.envParams.GetEnv(key) | ||
if err != nil { | ||
return nil, err | ||
} else if s == "" { | ||
return nil, nil | ||
} | ||
subnets := strings.Split(s, ",") | ||
for _, subnet := range subnets { | ||
_, cidr, err := net.ParseCIDR(subnet) | ||
if err != nil { | ||
return nil, fmt.Errorf("cannot parse outbound subnet %q from environment variable with key %s: %w", subnet, key, err) | ||
} else if cidr == nil { | ||
return nil, fmt.Errorf("cannot parse outbound subnet %q from environment variable with key %s: subnet is nil", | ||
subnet, key) | ||
} | ||
outboundSubnets = append(outboundSubnets, *cidr) | ||
} | ||
return outboundSubnets, nil | ||
} |
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 |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package routing | ||
|
||
import ( | ||
"fmt" | ||
"net" | ||
) | ||
|
||
func (r *routing) SetOutboundRoutes(outboundSubnets []net.IPNet) error { | ||
defaultInterface, defaultGateway, err := r.DefaultRoute() | ||
if err != nil { | ||
return fmt.Errorf("cannot set oubtound subnets in routing: %w", err) | ||
} | ||
return r.setOutboundRoutes(outboundSubnets, defaultInterface, defaultGateway) | ||
} | ||
|
||
func (r *routing) setOutboundRoutes(outboundSubnets []net.IPNet, | ||
defaultInterfaceName string, defaultGateway net.IP) error { | ||
r.stateMutex.Lock() | ||
defer r.stateMutex.Unlock() | ||
|
||
subnetsToRemove := findSubnetsToRemove(r.outboundSubnets, outboundSubnets) | ||
subnetsToAdd := findSubnetsToAdd(r.outboundSubnets, outboundSubnets) | ||
|
||
if len(subnetsToAdd) == 0 && len(subnetsToRemove) == 0 { | ||
return nil | ||
} | ||
|
||
r.removeOutboundSubnets(subnetsToRemove, defaultInterfaceName, defaultGateway) | ||
if err := r.addOutboundSubnets(subnetsToAdd, defaultInterfaceName, defaultGateway); err != nil { | ||
return fmt.Errorf("cannot set outbound subnets in routing: %w", err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (r *routing) removeOutboundSubnets(subnets []net.IPNet, | ||
defaultInterfaceName string, defaultGateway net.IP) { | ||
for _, subnet := range subnets { | ||
const table = 0 | ||
if err := r.deleteRouteVia(subnet, defaultGateway, defaultInterfaceName, table); err != nil { | ||
r.logger.Error("cannot remove outdated outbound subnet from routing: %s", err) | ||
continue | ||
} | ||
r.outboundSubnets = removeSubnetFromSubnets(r.outboundSubnets, subnet) | ||
} | ||
} | ||
|
||
func (r *routing) addOutboundSubnets(subnets []net.IPNet, | ||
defaultInterfaceName string, defaultGateway net.IP) error { | ||
for _, subnet := range subnets { | ||
const table = 0 | ||
if err := r.addRouteVia(subnet, defaultGateway, defaultInterfaceName, table); err != nil { | ||
return fmt.Errorf("cannot add outbound subnet %s to routing: %w", subnet, err) | ||
} | ||
r.outboundSubnets = append(r.outboundSubnets, subnet) | ||
} | ||
return nil | ||
} |
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.