Skip to content

Conversation

@QxBytes
Copy link
Contributor

@QxBytes QxBytes commented Nov 16, 2023

Reason for Change:

Pods should not be able to communicate with the wireserver on port 80.

Issue Fixed:

See above.

Requirements:

Notes:

  • wanted to create a unit test for the iptables command failing but there is no mock interface or way of injecting a mock "iptables" dependency.
  • considered placing the code for the vm iptable rule in transparent vlan endpoint client, but this iptable rule only needs to be run once.
  • wanted to match the capitalization convention of enabling ipv4 forwarding, but the linter (only now) is giving me warnings about capitalization (not sure why it didn't warn me about the capitalization before).

@QxBytes QxBytes added cni Related to CNI. fix Fixes something. multitenancy labels Nov 16, 2023
@QxBytes QxBytes force-pushed the alew/block-wireserver branch from 2a4d40d to c6acc01 Compare November 16, 2023 22:13
@QxBytes QxBytes marked this pull request as ready for review November 16, 2023 22:34
@QxBytes QxBytes requested a review from a team as a code owner November 16, 2023 22:34
@QxBytes QxBytes force-pushed the alew/block-wireserver branch from 82e82a1 to 658d3c9 Compare November 16, 2023 23:02

func BlockWireserverTraffic() error {
// iptables -t filter -I FORWARD -j DROP -d <wireserver ip>/32 -p tcp -m tcp --dport 80
dropWireserver := fmt.Sprintf("-d %s/32 -p tcp -m tcp --dport 80", AzureDNS)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should drop for udp as well

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replicating capz rule to block for tcp protocol only as we discussed.

return nil
}

func BlockWireserverTraffic() error {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can it be BlockEgressTrafficFromContainer(ipAddress, port)?

@QxBytes QxBytes force-pushed the alew/block-wireserver branch from 987e54b to 744d816 Compare November 18, 2023 00:16
}
logger.Info("Ipv4 forwarding enabled")
if err := networkutils.BlockEgressTrafficFromContainer(networkutils.AzureDNS, iptables.HTTPPort); err != nil {
return nil, fmt.Errorf("unable to insert vm iptables rule drop all wireserver port 80 packets: %w", err)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Port 80 in the error message is from iptables.HTTPPort ? if so can we use the variable instead of hard coding.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Should we be using errors.wrapf (not sure but want to keep the cni files consistent going forward)?
@tamilmani1989

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Port 80 in the error message is from iptables.HTTPPort ? if so can we use the variable instead of hard coding.

The insert ip table function calls the ExecuteCommand function (which logs the command itself) so I'll probably remove the details (like port or ip) from the error message altogether.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes we should errors.Wrapf .. @QxBytes can you make that change?

func BlockEgressTrafficFromContainer(ipAddress string, port int) error {
// iptables -t filter -I FORWARD -j DROP -d <ip>/32 -p tcp -m tcp --dport <port>
dropTraffic := fmt.Sprintf("-d %s/32 -p tcp -m tcp --dport %d", ipAddress, port)
return errors.Wrap(iptables.InsertIptableRule(iptables.V4, iptables.Filter, iptables.Forward, dropTraffic, iptables.Drop), "iptables block traffic failed")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are just blocking ipv4 ? Should we make the function intake the type of address as well ? (ipv4 or ipv6)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there is no ipv6 wireserver endpoint. but i asked @QxBytes to create separate PR for blocking all ipv6 addrs from pod.

@QxBytes QxBytes force-pushed the alew/block-wireserver branch 2 times, most recently from aa25ae8 to 87aee51 Compare November 21, 2023 18:00
return nil
}

func BlockEgressTrafficFromContainer(ipAddress string, port int) error {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we take protocol also as part of arg?

func BlockEgressTrafficFromContainer(ipAddress string, port int) error {
// iptables -t filter -I FORWARD -j DROP -d <ip>/32 -p tcp -m tcp --dport <port>
dropTraffic := fmt.Sprintf("-d %s/32 -p tcp -m tcp --dport %d", ipAddress, port)
return errors.Wrap(iptables.InsertIptableRule(iptables.V4, iptables.Filter, iptables.Forward, dropTraffic, iptables.Drop), "iptables block traffic failed")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there is no ipv6 wireserver endpoint. but i asked @QxBytes to create separate PR for blocking all ipv6 addrs from pod.

}
logger.Info("Ipv4 forwarding enabled")
if err := networkutils.BlockEgressTrafficFromContainer(networkutils.AzureDNS, iptables.HTTPPort); err != nil {
return nil, fmt.Errorf("unable to insert vm iptables rule drop all wireserver port 80 packets: %w", err)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes we should errors.Wrapf .. @QxBytes can you make that change?

}
logger.Info("Ipv4 forwarding enabled")
if err := networkutils.BlockEgressTrafficFromContainer(iptables.V4, networkutils.AzureDNS, iptables.HTTPPort); err != nil {
return nil, fmt.Errorf("unable to insert vm iptables rule drop wireserver packets: %w", err)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should use errors.wrapf

vipul-21
vipul-21 previously approved these changes Nov 21, 2023
@QxBytes QxBytes force-pushed the alew/block-wireserver branch from 2d7b3b2 to 90e6e83 Compare November 22, 2023 00:04
if err := iptables.InsertIptableRule(iptables.V4, "mangle", "PREROUTING", match, "ACCEPT"); err != nil {
return errors.Wrap(err, "unable to insert iptables rule accept all incoming from vlan interface")
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add a comment this is for blocking wireserver traffic from customer vnet nic

return nil, errors.Wrap(err, "ipv4 forwarding failed")
}
logger.Info("Ipv4 forwarding enabled")
if err := networkutils.BlockEgressTrafficFromContainer(iptables.V4, networkutils.AzureDNS, iptables.TCP, iptables.HTTPPort); err != nil {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add a comment this is for blocking wireserver traffic from apipa nic

tamilmani1989
tamilmani1989 previously approved these changes Nov 28, 2023
@QxBytes QxBytes force-pushed the alew/block-wireserver branch from 317948f to 79a2698 Compare November 29, 2023 18:08
@QxBytes QxBytes merged commit 2382637 into master Nov 29, 2023
@QxBytes QxBytes deleted the alew/block-wireserver branch November 29, 2023 21:37
matmerr pushed a commit that referenced this pull request Jan 17, 2024
* Add vm and vnet ns block wireserver port 80 rule

* Use existing variable for known ip

* Move code to networkutils

* Address feedback

* Address iptables version feedback

* Address protocol and format feedback

* Add comments

* Remove cidr in case ipv6 is used
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cni Related to CNI. fix Fixes something. multitenancy

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants