aquasecurity / kube-hunter

Hunt for security weaknesses in Kubernetes clusters
Apache License 2.0
4.75k stars 585 forks source link

Optimize NewHostEvent get_cloud() #324

Closed mormamn closed 4 years ago

mormamn commented 4 years ago

What would you like to be added

Right now in HostDiscovery.execute() we try to map out a cidr and assign a cloud to it, in the code only the first IP of a cidr is sent to the get_cloud() func(for example out of 192.168.0.1/24, only 192.168.0.1 is sent) while we ignore a few facts:

  1. The first IP might not exist in the cloud, but another might exist.
  2. The first IP might exist in Azure, but another may belongs to other cloud vendor (if subnet is big enough).

So if we scan all IP's that should solve the problem, but then we have a performance issue, because we will scan each and every IP without even knowing if theres a VM behind it.

Why is this needed

It's a feature in kube-hunter that currently isn't working right, and it's causing some hunters to not deliver what they should do (aks.py AzureSpnHunter for example)

What can it look like

We can use cached-property pip package, so that when we acually access the event.cloud property it will call up the get_cloud function. So the event.cloud property will act as a lazy load - only when we access it, it will try to obtain it.