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

pre proccessing policy for general conns #306

Merged
merged 7 commits into from
Feb 12, 2024

Conversation

shireenf-ibm
Copy link
Contributor

issue : #236
sub-task:

    • optimize policy pre-processing : store data on policy's general conns (to whole world/ entire cluster)more info

@shireenf-ibm
Copy link
Contributor Author

Some notes:

1. main changes:

- step1 : while upserting a new network-policy, scan its ingress and egress rules and store :

- all destinations (whole world) connections
- entire cluster connections

notes on step1:

  • can not do the pre-process only for exposure analysis; since policy-engine's exposure analysis flag is updated only after upserting the objects (the pe.exposureAnalysisFlag is updated after pe.NewPolicyEngineWithObjects which calls UpsertObject funcs)

  • currently, I stored both entire world and entire cluster conns (entire cluster contains also entire world) although I use only entire cluster;

    in the mean while kept the entire-world connection cause we may use it in next optimizations; if we don't will eliminate it.

  • a rule may contain namedPort; in pre-process we can not convert the namedPort into portNum (we don't have all peers yet, neither know what peers are selected); so :

    1. on pre-process; I store the new connection with the namedPort (however; here I found a bug in PortSet(detailed below))
    2. while computing allowed connections between peers (scanning the policy rules for finding selected peers;), each time a namedPort was converted, saved it in the policy struct; (a policy object has a map from protocol to map from namedPorts to port numbers)

- step2: while scaning the policies selecting a pod for allowed connections computations; updated the pod's general exposure data from the policy's stored data.

(when updating the pod's data, checked and replaced namedPort in the poliy's entire cluster conns using the map; (worked with local test with namedPort))

2. notes on PortSet :

  • although PortSet has a
NamedPorts         map[string]bool
ExcludedNamedPorts map[string]bool

they were never used before and never initialized;
so while running a test with a namedPort I got : panic: assignment to entry in nil map error;
I updated the MakePortSet with initialized empty maps; but still got the error;
since almost always a new PortSet is initialized by PortSet{};
so currently in the PortSet funcs that are used for storing the namedPort; I check and initialize the maps if are nil

@shireenf-ibm
Copy link
Contributor Author

** TODO:** (for next commit?)
if new data of the Policy is accepted :

  • we can benefit from it for computing allowed conns;
    I suggest that the netpol func ruleSelectsPeer checks only if there is a specified rule selecting the specific peer; (specified podSelector/ NamespaceSelector ...)
    and then for-example :
    the allowed conns between peers is the entireCluster.Union(this-rules-conn)
    for IPblock the policy's entireWorldConn.Union(specific IPBlock rule conn)

@shireenf-ibm shireenf-ibm marked this pull request as draft February 5, 2024 15:53
@shireenf-ibm shireenf-ibm mentioned this pull request Feb 5, 2024
@adisos
Copy link
Collaborator

adisos commented Feb 6, 2024

some questions/comments:

can not do the pre-process only for exposure analysis; since policy-engine's exposure analysis flag is updated only after upserting the objects

I think we can find solutions for this, such as adding more api funcs to generate policy engine as desired.

while computing allowed connections between peers (scanning the policy rules for finding selected peers;), each time a namedPort was converted, saved it in the policy struct

why does it need to be stored in the policy?

what if the connection from actual pod to a "fake" (representative) pod depends on a named port? how will this be handled by exposure analysis?

I suggest that the netpol func ruleSelectsPeer checks only ...

not sure I understand the entire sentence, though I think ruleSelectsPeer could avoid iterating all rules in case we already know from pre-processing (when available, from exposure analysis activation) that at least one of the rules is matching all namespaces or any pod/ip-block .. (for this case we do need the "all destinations (whole world) connections").

// - the maximal connection-set which the policy's rules allow to all namespaces in the cluster on egress direction
EgressGeneralConns PolicyGeneralRulesConns

namedPortsToPortNum map[v1.Protocol]map[string]int32 // internal map; map from protocol to map from namedPort which
Copy link
Collaborator

Choose a reason for hiding this comment

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

the matching of named port to its number is per pod, so why is it stored here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed the handling of named-ports

adding some examples - locally tested :

  1. an ingress policy; exposes the workload on ingress to entire-cluster on named ports; since the dst is the workload itself - converts the ports to numbers

ing_spec_result

  1. an egress policy - which exposes the workload to entire cluster ; on named ports : we will see these potential named ports on the result.

egress_res

  1. an example combining named ports and port numbers:

example_spec_res

Copy link
Collaborator

Choose a reason for hiding this comment

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

thanks, the example seem to make sense

@shireenf-ibm
Copy link
Contributor Author

shireenf-ibm commented Feb 6, 2024

some questions/comments:

can not do the pre-process only for exposure analysis; since policy-engine's exposure analysis flag is updated only after upserting the objects

I think we can find solutions for this, such as adding more api funcs to generate policy engine as desired.

True, so do you suggest performing this pre-process only for exposure analysis ?

while computing allowed connections between peers (scanning the policy rules for finding selected peers;), each time a namedPort was converted, saved it in the policy struct

why does it need to be stored in the policy?

True, actually I was very unsure when and how to store these;
additionally, if we want to benefit from the general conns in ruleSelectsPeer ; we might not get the converted named port for the general rules.

I think also that I don't fully understand when a named port should be converted..

but if it is a "src" (egress conn); should we convert? or keep it as it? (a named port)
(I would like to discuss this with you)

what if the connection from actual pod to a "fake" (representative) pod depends on a named port? how will this be handled by exposure analysis?

in this PR; focused on "general" (entire cluster) connections;
my plan for optimizing PR: is that if the "dst" is representative and there is a conn with named port is to store the named port in the connection data (portSet.NamedPorts)
I can also save potential namedPorts in the RepresentativePeer struct so we can use it in the output if we want

I suggest that the netpol func ruleSelectsPeer checks only ...

not sure I understand the entire sentence, though I think ruleSelectsPeer could avoid iterating all rules in case we already know from pre-processing (when available, from exposure analysis activation) that at least one of the rules is matching all namespaces or any pod/ip-block .. (for this case we do need the "all destinations (whole world) connections").

yes that is what I meant

@shireenf-ibm
Copy link
Contributor Author

shireenf-ibm commented Feb 8, 2024

next tasks to be done are:

  • adding new api func to the policy-engine; so all pre-process (and storing general conns for pods) will be computed only for exposure-analysis.
  • in case of exposure-analysis; updating some computation that can benefit from the stored data of the policy

since this PR is already labeled L ; I think should commit these to a new PR ?
(sub-task : * - [ ] adding new api func for policy-engine for running exposure-analysis individually)

@adisos
Copy link
Collaborator

adisos commented Feb 8, 2024

next tasks to be done are:

* adding new api func to the policy-engine; so all pre-process (and storing general conns for pods) will be computed only for exposure-analysis.

* in case of exposure-analysis; updating some computation that can benefit from the stored data of the policy

since this PR is already labeled L ; I think should commit these to a new PR ? (sub-task : * - [ ] adding new api func for policy-engine for running exposure-analysis individually)

ok, let's have these in a new PR

pkg/netpol/internal/common/portset.go Outdated Show resolved Hide resolved
pkg/netpol/internal/common/portset.go Outdated Show resolved Hide resolved
pkg/netpol/eval/resources.go Outdated Show resolved Hide resolved
pkg/netpol/eval/internal/k8s/pod.go Outdated Show resolved Hide resolved
pkg/netpol/eval/resources.go Outdated Show resolved Hide resolved
pkg/netpol/eval/internal/k8s/netpol.go Outdated Show resolved Hide resolved
pkg/netpol/eval/internal/k8s/netpol.go Show resolved Hide resolved
@shireenf-ibm shireenf-ibm marked this pull request as ready for review February 12, 2024 09:32
@shireenf-ibm shireenf-ibm merged commit db2c484 into new_exposure_analysis_first_branch Feb 12, 2024
2 checks passed
@shireenf-ibm shireenf-ibm deleted the policy_pre_process branch February 12, 2024 09:32
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.

None yet

2 participants