google / cloud-forensics-utils

Python library to carry out DFIR analysis on the Cloud
Apache License 2.0
453 stars 89 forks source link

Core Kubernetes class structure #375

Closed zkck closed 2 years ago

zkck commented 2 years ago

This PR includes the core class hierarchy for the Kubernetes/GKE functionality that we'd like to add to CFU.

Overview of the class hierarchy:

K8sClient (abstract)  // All Kubernetes objects
├── K8sCluster
└── K8sResource (abstract)  // Named Kubernetes objects
    ├── K8sNamespacedResource (abstract)  // Named Kubernetes objects within a namespace
    │   ├── K8sPod
    │   └── K8sWorkload
    │       └── K8sDeployment
    └── K8sNode

Example usage:

from libcloudforensics.providers.gcp.internal.gke import GkeCluster
from libcloudforensics.providers.kubernetes.base import K8sCluster
from libcloudforensics.providers.kubernetes.workloads import K8sDeployment

gke_cluster = GkeCluster('project-id', 'zone-id', 'cluster-id')
api_client = gke_cluster.GetCredentials()
k8s_cluster = K8sCluster(api_client)

print()
print('Pods in namespace "default":')
for pod in k8s_cluster.ListPods('default'):
  print(pod.name, pod.namespace, pod.GetNode().name)

print()
print('Pods in namespace "kube-system":')
for pod in k8s_cluster.ListPods('kube-system'):
  print(pod.name, pod.namespace, pod.GetNode().name)

print()
print('Pods in all namespaces:')
for pod in k8s_cluster.ListPods():
  print(pod.name, pod.namespace, pod.GetNode().name)

print()
print('Pods in nginx deployment')
for pod in K8sDeployment(api_client, 'nginx', 'default').GetCoveredPods():
  print(pod.name, pod.namespace, pod.GetNode().name)

Still in draft phase since mock tests must be added.

codecov-commenter commented 2 years ago

Codecov Report

:exclamation: No coverage uploaded for pull request base (main@1787510). Click here to learn what that means. The diff coverage is n/a.

Impacted file tree graph

@@           Coverage Diff           @@
##             main     #375   +/-   ##
=======================================
  Coverage        ?   61.25%           
=======================================
  Files           ?       40           
  Lines           ?     3198           
  Branches        ?        0           
=======================================
  Hits            ?     1959           
  Misses          ?     1239           
  Partials        ?        0           
Flag Coverage Δ
nosetests 61.25% <0.00%> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.


Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update 1787510...a1328f0. Read the comment docs.

zkck commented 2 years ago

Quick note, I wasn't sure of the best placement for the kubernetes package. It's under providers right now, I'm open to suggestions.