Skip to content
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

W.I.P Extend anp egress peer #447

Open
wants to merge 122 commits into
base: main
Choose a base branch
from
Open

Conversation

shireenf-ibm
Copy link
Contributor

@shireenf-ibm shireenf-ibm commented Nov 18, 2024

base PR: #380
issue: #442

  • support Networks field in AdminNetworkPolicyEgressPeer
  • add logger to k8s objects and raise some warnings (on IPv6 cidr / nodes field / named ports without match ..)
  • more tests (good paths w/wo warnings)
  • unit tests on logger warnings
  • some "tbd" in the committed code

…ll but: udp 5353 instead of SCTP 1-65535,TCP 1-65535,UDP 1-5352,5354-65535)
Base automatically changed from support_admin_netpolicy to main November 28, 2024 11:42
@shireenf-ibm shireenf-ibm marked this pull request as ready for review November 28, 2024 15:16
Copy link
Collaborator

@adisos adisos left a comment

Choose a reason for hiding this comment

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

initial comments

@@ -59,8 +61,9 @@ type (
)

// NewPolicyEngine returns a new PolicyEngine with an empty initial state
func NewPolicyEngine() *PolicyEngine {
func NewPolicyEngine(l logger.Logger) *PolicyEngine {
Copy link
Collaborator

Choose a reason for hiding this comment

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

can avoid breaking current exported functions API ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

reverted this change

@@ -71,16 +74,16 @@ func NewPolicyEngine() *PolicyEngine {
}
}

func NewPolicyEngineWithObjects(objects []parser.K8sObject) (*PolicyEngine, error) {
pe := NewPolicyEngine()
func NewPolicyEngineWithObjects(objects []parser.K8sObject, l logger.Logger) (*PolicyEngine, error) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

here 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.

reverted this change

err := pe.addObjectsByKind(objects)
return pe, err
}

// NewPolicyEngineWithOptions returns a new policy engine with an empty state but updating the exposure analysis flag
// TBD: currently exposure-analysis is the only option supported by policy-engine, so no need for options list param
func NewPolicyEngineWithOptions(exposureFlag bool) *PolicyEngine {
pe := NewPolicyEngine()
func NewPolicyEngineWithOptions(exposureFlag bool, l logger.Logger) *PolicyEngine {
Copy link
Collaborator

Choose a reason for hiding this comment

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

and here

Copy link
Collaborator

Choose a reason for hiding this comment

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

there is an option WithLogger in connlist, can it be used here as well, as an option for NewPolicyEngineWithOptions ?
Instead of breaking the exported functions API?
better using same pattern as in connlist, have dedicated function "With.." for each option, and a dedicated type such as type ConnlistAnalyzerOption func(*ConnlistAnalyzer).

Copy link
Contributor Author

@shireenf-ibm shireenf-ibm Dec 1, 2024

Choose a reason for hiding this comment

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

is it ok to change the input parameter of NewPolicyEngineWithOptions to be a list of options ...PolicyEngineOption?
(the options will include both the exposure flag and logger)
or this will be considered as API breaking?

Copy link
Collaborator

Choose a reason for hiding this comment

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

this is also API breaking..
can add new func NewPolicyEngineWithOptionsList to be used with the new type of options, and add a comment for the NewPolicyEngineWithOptions that it is deprecated.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

////////////////////////////////////////////////////////////////////////////////////////////

// AdminNetworkPolicy is an alias for k8s adminNetworkPolicy object
type AdminNetworkPolicy apisv1a.AdminNetworkPolicy
type AdminNetworkPolicy struct {
Copy link
Collaborator

Choose a reason for hiding this comment

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

please move this type to be at the top of this file (also for banp)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done; (the banp struct is on top of its file)

Copy link
Collaborator

@adisos adisos Dec 1, 2024

Choose a reason for hiding this comment

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

why is the ingress connections cidrs split, if not impacted by netpols?
if this is due to computation of disjoint cidrs, should consider separate the computation to ingress and egress, so that refinement is not "global", but per each direction separately.

Copy link
Collaborator

@adisos adisos left a comment

Choose a reason for hiding this comment

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

warning formats: be consistent with current format, for example:

$ ./bin/k8snetpolicy list --dirpath tests/bad_netpols/subdir5/
2024/12/01 15:12:11 Network policy "default/shippingservice-netpol": port name:  "abc" has no match in the configuration of the destination peer "default/shippingservice-1"; it will be ignored, and will not appear in the connectivity results.
0.0.0.0-255.255.255.255 => default/adservice[Deployment] : All Connections
0.0.0.0-255.255.255.255 => default/cartservice[Deployment] : All Connections
0.0.0.0-255.255.255.255 => default/checkoutservice[Deployment] : All Connections

and example from current implementation:

r$ ./bin/k8snetpolicy list --dirpath tests/anp_and_banp_using_networks_with_ipv6_test/
2024/12/01 15:14:13 admin network policy "network-as-egress-peer": Warning  in rule "allow-all-egress-to-intranet": IPv6 addresses are not supported; it will be ignored, and will not appear in the connectivity results.

can be changed to:

admin network policy "network-as-egress-peer": in rule "allow-all-egress-to-intranet": IPv6 addresses are not supported

also, better explain the impact of the "IPV6 not supported" warning in the README documentation.

@adisos
Copy link
Collaborator

adisos commented Dec 1, 2024

warning formats: be consistent with current format, for example:

$ ./bin/k8snetpolicy list --dirpath tests/bad_netpols/subdir5/
2024/12/01 15:12:11 Network policy "default/shippingservice-netpol": port name:  "abc" has no match in the configuration of the destination peer "default/shippingservice-1"; it will be ignored, and will not appear in the connectivity results.
0.0.0.0-255.255.255.255 => default/adservice[Deployment] : All Connections
0.0.0.0-255.255.255.255 => default/cartservice[Deployment] : All Connections
0.0.0.0-255.255.255.255 => default/checkoutservice[Deployment] : All Connections

and example from current implementation:

r$ ./bin/k8snetpolicy list --dirpath tests/anp_and_banp_using_networks_with_ipv6_test/
2024/12/01 15:14:13 admin network policy "network-as-egress-peer": Warning  in rule "allow-all-egress-to-intranet": IPv6 addresses are not supported; it will be ignored, and will not appear in the connectivity results.

can be changed to:

admin network policy "network-as-egress-peer": in rule "allow-all-egress-to-intranet": IPv6 addresses are not supported

also, better explain the impact of the "IPV6 not supported" warning in the README documentation.

Also, in this example (from anp_and_banp_using_networks_with_ipv6_test) there are 2 actual warnings, repeated multiple times in the log.. can this be fixed?

Copy link
Collaborator

Choose a reason for hiding this comment

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

can enhance this example with some cidr range contained in "pass" to be allowed by regular network policy, and so that there is another cidr range in "pass" reaching the "base" ANP as in the current example. (so that it demonstrates effect of both levels of NP and BANP for pass of IP-block).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

Comment on lines 119 to 120
minimumPort = 1
maximumPort = 65535
Copy link
Collaborator

Choose a reason for hiding this comment

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

ports are already defined in pkg/netpol/internal/common/portset.go (can be exported and used from there)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

// raising a warning that the "named port" of the rule is ignored, since it has no match in the pod config.
np.Logger.Warnf(np.netpolWarning(alerts.WarnUnmatchedNamedPort(portName, dst.String())))
}
} // @tbd should raise a warning/error if the portRange is empty (when also namedPort is empty)
Copy link
Collaborator

@adisos adisos Dec 1, 2024

Choose a reason for hiding this comment

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

as for "tbd should raise a warning/error if the portRange is empty" :
please add an example test covering this case.. what is the output currently? can a warning by sufficient? (rule becomes ineffective?)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

added a test.
currently an empty port range is ignored (skipped);
so on one hand I can add a warning (since no problems are caused); but on the other hand according to this
end port:

This field cannot be defined if the port field is not defined or if the port field is defined as a named (string) port. The endPort must be equal or greater than port

.
so it is illegal; does this mean we should raise an error?

Copy link
Collaborator

@adisos adisos Dec 2, 2024

Choose a reason for hiding this comment

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

ok, so let's change to an error (to be consistent, if for other invalid values in policy resource an error is issued).

@@ -185,9 +412,15 @@ func ruleConnections(ports *[]apisv1a.AdminNetworkPolicyPort, dst Peer) (*common
}
portSet.AddPort(intstr.FromInt32(anpPort.PortNumber.Port))
case anpPort.NamedPort != nil:
if dst.PeerType() == IPBlockType {
// IPblock does not have named-ports defined
// @tbd should return error? (a rule that combines networks and pods may have such port?)
Copy link
Collaborator

Choose a reason for hiding this comment

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

as for "tbd" comment: can add an example test for this case as well?
if a consistent connectivity output is possible, a warning is preferred.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done; added test anp_test_named_ports_multiple_peers with a warning that the named-port is ignored for an IP-Block

@shireenf-ibm
Copy link
Contributor Author

warning formats: be consistent with current format, for example:

$ ./bin/k8snetpolicy list --dirpath tests/bad_netpols/subdir5/
2024/12/01 15:12:11 Network policy "default/shippingservice-netpol": port name:  "abc" has no match in the configuration of the destination peer "default/shippingservice-1"; it will be ignored, and will not appear in the connectivity results.
0.0.0.0-255.255.255.255 => default/adservice[Deployment] : All Connections
0.0.0.0-255.255.255.255 => default/cartservice[Deployment] : All Connections
0.0.0.0-255.255.255.255 => default/checkoutservice[Deployment] : All Connections

and example from current implementation:

r$ ./bin/k8snetpolicy list --dirpath tests/anp_and_banp_using_networks_with_ipv6_test/
2024/12/01 15:14:13 admin network policy "network-as-egress-peer": Warning  in rule "allow-all-egress-to-intranet": IPv6 addresses are not supported; it will be ignored, and will not appear in the connectivity results.

can be changed to:

admin network policy "network-as-egress-peer": in rule "allow-all-egress-to-intranet": IPv6 addresses are not supported

also, better explain the impact of the "IPV6 not supported" warning in the README documentation.

made tiny fixes so warnings from netpols and ANPs be consistent (actually, both warnings from netpols and anps were added in this PR).
added a note under ### Possible warnings in the docs\connlist_output.md regarding unsupported IPv6

@shireenf-ibm
Copy link
Contributor Author

shireenf-ibm commented Dec 2, 2024

warning formats: be consistent with current format, for example:

$ ./bin/k8snetpolicy list --dirpath tests/bad_netpols/subdir5/
2024/12/01 15:12:11 Network policy "default/shippingservice-netpol": port name:  "abc" has no match in the configuration of the destination peer "default/shippingservice-1"; it will be ignored, and will not appear in the connectivity results.
0.0.0.0-255.255.255.255 => default/adservice[Deployment] : All Connections
0.0.0.0-255.255.255.255 => default/cartservice[Deployment] : All Connections
0.0.0.0-255.255.255.255 => default/checkoutservice[Deployment] : All Connections

and example from current implementation:

r$ ./bin/k8snetpolicy list --dirpath tests/anp_and_banp_using_networks_with_ipv6_test/
2024/12/01 15:14:13 admin network policy "network-as-egress-peer": Warning  in rule "allow-all-egress-to-intranet": IPv6 addresses are not supported; it will be ignored, and will not appear in the connectivity results.

can be changed to:

admin network policy "network-as-egress-peer": in rule "allow-all-egress-to-intranet": IPv6 addresses are not supported

also, better explain the impact of the "IPV6 not supported" warning in the README documentation.

Also, in this example (from anp_and_banp_using_networks_with_ipv6_test) there are 2 actual warnings, repeated multiple times in the log.. can this be fixed?

the warning is logged each time the rule is scanned - for finding allowed conns between two peers (different peers each time);
we can "mark the policy as visited" (adding an attribute to its struct) to avoid repeating the warnings log;
should add it now or in another issue?

@adisos
Copy link
Collaborator

adisos commented Dec 2, 2024

warning formats: be consistent with current format, for example:

$ ./bin/k8snetpolicy list --dirpath tests/bad_netpols/subdir5/
2024/12/01 15:12:11 Network policy "default/shippingservice-netpol": port name:  "abc" has no match in the configuration of the destination peer "default/shippingservice-1"; it will be ignored, and will not appear in the connectivity results.
0.0.0.0-255.255.255.255 => default/adservice[Deployment] : All Connections
0.0.0.0-255.255.255.255 => default/cartservice[Deployment] : All Connections
0.0.0.0-255.255.255.255 => default/checkoutservice[Deployment] : All Connections

and example from current implementation:

r$ ./bin/k8snetpolicy list --dirpath tests/anp_and_banp_using_networks_with_ipv6_test/
2024/12/01 15:14:13 admin network policy "network-as-egress-peer": Warning  in rule "allow-all-egress-to-intranet": IPv6 addresses are not supported; it will be ignored, and will not appear in the connectivity results.

can be changed to:

admin network policy "network-as-egress-peer": in rule "allow-all-egress-to-intranet": IPv6 addresses are not supported

also, better explain the impact of the "IPV6 not supported" warning in the README documentation.

Also, in this example (from anp_and_banp_using_networks_with_ipv6_test) there are 2 actual warnings, repeated multiple times in the log.. can this be fixed?

the warning is logged each time the rule is scanned - for finding allowed conns between two peers (different peers each time); we can "mark the policy as visited" (adding an attribute to its struct) to avoid repeating the warnings log; should add it now or in another issue?

ok, let's fix this in this branch

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

AdminNetworkPolicy : extend AdminNetworkPolicyEgressPeer fields support
3 participants