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

[Dumper] Rework of the object ingestion #174

Merged
merged 10 commits into from
Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
159 changes: 8 additions & 151 deletions pkg/dump/ingestor.go
Original file line number Diff line number Diff line change
@@ -1,27 +1,19 @@
package dump

Check failure on line 1 in pkg/dump/ingestor.go

View workflow job for this annotation

GitHub Actions / linter

: # github.com/DataDog/KubeHound/pkg/dump

import (
"context"
"encoding/json"
"fmt"
"path"

"time"

"github.com/DataDog/KubeHound/pkg/collector"
"github.com/DataDog/KubeHound/pkg/dump/writer"
"github.com/DataDog/KubeHound/pkg/globals/types"
"github.com/DataDog/KubeHound/pkg/kubehound/ingestor/preflight"
"github.com/DataDog/KubeHound/pkg/telemetry/log"
"github.com/DataDog/KubeHound/pkg/telemetry/metric"
"github.com/DataDog/KubeHound/pkg/telemetry/span"
"github.com/DataDog/KubeHound/pkg/telemetry/statsd"
"github.com/DataDog/KubeHound/pkg/telemetry/tag"

Check failure on line 13 in pkg/dump/ingestor.go

View workflow job for this annotation

GitHub Actions / linter

"github.com/DataDog/KubeHound/pkg/telemetry/tag" imported and not used

Check failure on line 13 in pkg/dump/ingestor.go

View workflow job for this annotation

GitHub Actions / linter

"github.com/DataDog/KubeHound/pkg/telemetry/tag" imported and not used
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer"
)

var _ collector.GenericIngestor = (*DumpIngestor)(nil)

type DumpIngestor struct {
directoryOutput string
ResultName string
Expand Down Expand Up @@ -81,158 +73,23 @@
return d.writer.OutputPath()
}

func (d *DumpIngestor) IngestNode(ctx context.Context, node types.NodeType) error {
if ok, err := preflight.CheckNode(node); !ok {

return err
}

return d.processObject(ctx, node, collector.NodePath)
}

func ingestPodPath(pod types.PodType) string {
return path.Join(pod.Namespace, collector.PodPath)
}

func (d *DumpIngestor) IngestPod(ctx context.Context, pod types.PodType) error {
if ok, err := preflight.CheckPod(pod); !ok {
entity := tag.Entity(tag.EntityPods)
_ = statsd.Incr(metric.CollectorSkip, append(d.collector.Tags(ctx), entity), 1)

return err
}

filePath := ingestPodPath(pod)

return d.processObject(ctx, pod, filePath)
}

func ingestRolePath(roleBinding types.RoleType) string {
return path.Join(roleBinding.Namespace, collector.RolesPath)
}

func (d *DumpIngestor) IngestRole(ctx context.Context, role types.RoleType) error {
if ok, err := preflight.CheckRole(role); !ok {
entity := tag.Entity(tag.EntityRoles)
_ = statsd.Incr(metric.CollectorSkip, append(d.collector.Tags(ctx), entity), 1)

return err
}

filePath := ingestRolePath(role)

return d.processObject(ctx, role, filePath)
}

func (d *DumpIngestor) IngestClusterRole(ctx context.Context, clusterRole types.ClusterRoleType) error {
if ok, err := preflight.CheckClusterRole(clusterRole); !ok {
entity := tag.Entity(tag.EntityClusterRoles)
_ = statsd.Incr(metric.CollectorSkip, append(d.collector.Tags(ctx), entity), 1)

return err
}

return d.processObject(ctx, clusterRole, collector.ClusterRolesPath)
}

func ingestRoleBindingPath(roleBinding types.RoleBindingType) string {
return path.Join(roleBinding.Namespace, collector.RoleBindingsPath)
}

func (d *DumpIngestor) IngestRoleBinding(ctx context.Context, roleBinding types.RoleBindingType) error {
if ok, err := preflight.CheckRoleBinding(roleBinding); !ok {
entity := tag.Entity(tag.EntityRolebindings)
_ = statsd.Incr(metric.CollectorSkip, append(d.collector.Tags(ctx), entity), 1)

return err
}

filePath := ingestRoleBindingPath(roleBinding)

return d.processObject(ctx, roleBinding, filePath)
}

func (d *DumpIngestor) IngestClusterRoleBinding(ctx context.Context, clusterRoleBinding types.ClusterRoleBindingType) error {
if ok, err := preflight.CheckClusterRoleBinding(clusterRoleBinding); !ok {
entity := tag.Entity(tag.EntityClusterRolebindings)
_ = statsd.Incr(metric.CollectorSkip, append(d.collector.Tags(ctx), entity), 1)

return err
}

return d.processObject(ctx, clusterRoleBinding, collector.ClusterRoleBindingsPath)
}

func ingestEndpointPath(endpoint types.EndpointType) string {
return path.Join(endpoint.Namespace, collector.EndpointPath)
}

func (d *DumpIngestor) IngestEndpoint(ctx context.Context, endpoint types.EndpointType) error {
if ok, err := preflight.CheckEndpoint(endpoint); !ok {
entity := tag.Entity(tag.EntityEndpoints)
_ = statsd.Incr(metric.CollectorSkip, append(d.collector.Tags(ctx), entity), 1)

return err
}

filePath := ingestEndpointPath(endpoint)

return d.processObject(ctx, endpoint, filePath)
}

// Static wrapper to dump k8s object dynamically (streams Kubernetes objects to the collector writer).
func dumpK8sObjs(ctx context.Context, operationName string, entity string, streamFunc StreamFunc) (context.Context, error) {
log.I.Infof("Dumping %s", entity)
span, ctx := tracer.StartSpanFromContext(ctx, operationName, tracer.Measured())
span.SetTag(tag.EntityTag, entity)
defer span.Finish()
err := streamFunc(ctx)

return ctx, err
}

func (d *DumpIngestor) DumpK8sObjects(ctx context.Context) error {
spanDump, ctx := tracer.StartSpanFromContext(ctx, span.CollectorDump, tracer.Measured())
defer spanDump.Finish()

ctx, pipeline, err := newPipelineDumpIngestor(ctx, d)
// ctx, pipeline, err := pipeline.NewPipelineDumpIngestor(ctx, d.collector, d.writer)
if err != nil {

Check failure on line 81 in pkg/dump/ingestor.go

View workflow job for this annotation

GitHub Actions / linter

undefined: err

Check failure on line 81 in pkg/dump/ingestor.go

View workflow job for this annotation

GitHub Actions / linter

undefined: err
return fmt.Errorf("create pipeline ingestor: %w", err)

Check failure on line 82 in pkg/dump/ingestor.go

View workflow job for this annotation

GitHub Actions / linter

undefined: err (typecheck)

Check failure on line 82 in pkg/dump/ingestor.go

View workflow job for this annotation

GitHub Actions / linter

undefined: err) (typecheck)
}
spanDump.SetTag(tag.DumperWorkerNumberTag, pipeline.WorkerNumber)

err = pipeline.Run(ctx)
if err != nil {
return fmt.Errorf("run pipeline ingestor: %w", err)
}

return pipeline.Wait(ctx)
}

func marshalK8sObj(obj interface{}) ([]byte, error) {
jsonData, err := json.Marshal(obj)
if err != nil {
return nil, fmt.Errorf("failed to marshal Kubernetes object: %w", err)
}

return jsonData, nil
}

func (d *DumpIngestor) processObject(ctx context.Context, obj interface{}, filePath string) error {
jsonData, err := marshalK8sObj(obj)
if err != nil {
return err
}

return d.writer.Write(ctx, jsonData, filePath)
}
return nil
// spanDump.SetTag(tag.DumperWorkerNumberTag, pipeline.WorkerNumber)

// Complete() is invoked by the collector when all k8s assets have been streamed.
// The function flushes all writers and waits for completion.
func (d *DumpIngestor) Complete(ctx context.Context) error {
d.writer.Flush(ctx)
// err = pipeline.Run(ctx)
// if err != nil {
// return fmt.Errorf("run pipeline ingestor: %w", err)
// }

return nil
// return pipeline.Wait(ctx)
Comment on lines +89 to +94
Copy link
Contributor

Choose a reason for hiding this comment

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

This is commented for the next PR?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes it is. to have a "pain less" split.

}

// Close() is invoked by the collector to close all handlers used to dump k8s objects.
Expand Down
138 changes: 0 additions & 138 deletions pkg/dump/pipeline.go

This file was deleted.

37 changes: 37 additions & 0 deletions pkg/dump/pipeline/cluster_role_binding_ingest.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package pipeline

import (
"context"

"github.com/DataDog/KubeHound/pkg/collector"
"github.com/DataDog/KubeHound/pkg/dump/writer"
"github.com/DataDog/KubeHound/pkg/globals/types"
"github.com/DataDog/KubeHound/pkg/kubehound/ingestor/preflight"
rbacv1 "k8s.io/api/rbac/v1"
)

type ClusterRoleBindingIngestor struct {
buffer map[string]*rbacv1.ClusterRoleBindingList
writer writer.DumperWriter
}

func NewClusterRoleBindingIngestor(ctx context.Context, dumpWriter writer.DumperWriter) *ClusterRoleBindingIngestor {
return &ClusterRoleBindingIngestor{
buffer: make(map[string]*rbacv1.ClusterRoleBindingList),
writer: dumpWriter,
}
}

func (d *ClusterRoleBindingIngestor) IngestClusterRoleBinding(ctx context.Context, clusterRoleBinding types.ClusterRoleBindingType) error {
if ok, err := preflight.CheckClusterRoleBinding(clusterRoleBinding); !ok {
return err
}

return bufferObject[rbacv1.ClusterRoleBindingList, types.ClusterRoleBindingType](ctx, collector.ClusterRoleBindingsPath, d.buffer, clusterRoleBinding)
}

// Complete() is invoked by the collector when all k8s assets have been streamed.
// The function flushes all writers and waits for completion.
func (d *ClusterRoleBindingIngestor) Complete(ctx context.Context) error {
return dumpObj[*rbacv1.ClusterRoleBindingList](ctx, d.buffer, d.writer)
}
Loading
Loading