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

Exposure analysis #236

Closed
adisos opened this issue Sep 11, 2023 · 5 comments
Closed

Exposure analysis #236

adisos opened this issue Sep 11, 2023 · 5 comments
Assignees
Labels
enhancement New feature or request

Comments

@adisos
Copy link
Collaborator

adisos commented Sep 11, 2023

Consider enhancement of the connectivity analysis:
A "what-if" analysis, to report if an analyzed workload is exposed (ingress of egress) to workloads which are not included in the input manifests.

@adisos adisos added the enhancement New feature or request label Oct 1, 2023
@adisos
Copy link
Collaborator Author

adisos commented Nov 12, 2023

More details:

Possible situations, which may not be reflected on the current connectivity report (since such connections may only be possible with peer workloads that do not exists on the input dir of the analyzed workload manifests):

  • Pod is exposed to entire cluster (both ingress and egress) – not protected by netpols
  • Pod is exposed to cluster only on ingress / egress (protected only on one direction by netpols)
  • exposure to entire namespace (one or more, also potentials)
  • Pod is exposed to potential specific namespaces / specific pods (which may exist or not on the cluster, but do not exist on the analyzed workload manifests)

How to provide/add this information in a connectivity report / graph?
(Currently, the concept of a “connection” between workloads in the report is defined only for workloads that exist on the input dir, and for which both source is allowed by netpols to egress to destination, and destination is allowed by netpols to ingress from source).

@zivnevo
Copy link
Member

zivnevo commented Nov 14, 2023

The analysis provided by ACS only includes the first two bullets + indicating pods that are externally exposed.
image

@shireenf-ibm shireenf-ibm self-assigned this Jan 3, 2024
@shireenf-ibm
Copy link
Contributor

shireenf-ibm commented Jan 4, 2024

work plan:

This was referenced Jan 21, 2024
@adisos adisos self-assigned this Jun 23, 2024
@shireenf-ibm
Copy link
Contributor

Summary of the implementation flow: (highlights)

  • exposure-analysis results are produced by running list command with --exposure flag
    (./bin/k8snetpolicy list --dirpath <testing-dir> --exposure)

  • the string output when running with exposure-analysis will include both

    • connlist results, i.e. connections between peers from the manifests / connections between real peers and ip-blocks.
    • exposure analysis results, i.e. connections between real peers from the manifests and potential pods. (connections to any namespace in the cluster will be represented by entire-cluster, connections to external resources will be represented by ip-blocks).
  • getting a structured exposure-results from connlist-analyzer is also possible by calling connlistAnalyzer's ExposedPeers() which returns a list of objects of ExposedPeer interface type.

    • each ExposedPeer object captures potential ingress and egress connections data for an exposed Peer; i.e. an ExposedPeer is a real peer (from the manifests) which is:
      • potentially exposed to peers that are not in the manifests, those peers are described by their potential NamespaceLabels and PodLabels. OR
      • exposed widely , i.e the peer is exposed to all namespaces in the cluster (entire-cluster)/ exposed to the whole world (entire-cluster and any external resource (ip-blocks))

- flow of computing the exposure results:
1. building a policy-engine for exposure goals:

  • first-step: inserting the policies from input-resource to the policy-engine:
    for each policy: looping policy's rules and update as following:
    • if the policy allows all (empty rules list) then the PolicyExposureWithoutSelectors field of the affected policy-direction is updated to indicate that the policy exposes its selected pods to all external and cluster resources with all connections
    • if the policy contains any rule which exposes its selected pods to any-namespace in the cluster, the ClusterWideExposure sub-field of the PolicyExposureWithoutSelectors field of the affected policy-direction is updated with the rule's port.
      • a rule that exposes the selected pods to any-namespace in the cluster is a rule that:
        • contains an empty namespaceSelector and nil podSelector
        • contains empty podSelector and empty namespaceSelector
    • otherwise, the rule contains:
      a nil namespaceSelector and non-empty podSelector / only non-empty namespaceSelector / both non-empty namespaceSelector and podSelector;
      for a such rule, a representative peer with those namespaceSelector and podSelector are generated.
    • note:
      • if the namespaceSelector is nil, the representative peer is generated in the policy's namespace and its namespaceSelector will conatin the matchLabels of the namespace name.
      • otherwise the representative-peer will be generated without a namespace as itself include all info about its potential namespace.
  • generating representative-peers occurs only for different policy rules (representative-peers are unique).
  • second-step: while inserting the pods/workloads from the input-resources, we remove all redundant representative-peers.
    i.e. a representative-peer that has a matching pod with same pod matchLabels and pod's namespace matchLabels is removed
    exceptional cases:
    * representative peers matching any-namespace or any-pod in a namespace will not be removed.
    * representative peers inferred from rules containing matchExpressions will not be removed either

2. computing allowed connections:

  • after building the policy-engine with all input-resources and all generated representative-peers, next step is computing allowed connections between all pairs of peers (including pairs of a real peer and a representative-peer) to build the regular connlist and an exposure-map.

    • connections between real-peers or real-peers and ip-blocks are stored in the connlist
    • connections between real workloads and representative-peers are stored in an exposure-map
    • moreover, a peer which is exposed widely by the policies that selected it will be inserted to the map with the wide-connection info
    • also, a peer which is not selected by any policy (not protected) is inserted to the map since such peer is exposed to whole world (in and out the cluster) with all conns on ingress and egress
  • the exposure map is a map from real-peer to its exposure-info which will be converted finally to the []ExposedPeer

  • more notes:

    • it was also determined that when computing allowed connections, a rule matches a representative peer if :
      • the rule is empty (matches all)
      • the rule points to same reference of the selectors of the representative peer
      • the rule and the selectors of the representative peer have same requirements (1 way containment is not a match)

@shireenf-ibm
Copy link
Contributor

moved the not yet handled tasks to #392

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

No branches or pull requests

3 participants