From 56408b531bfd9bed690c8f6144c83c53f4f94ddb Mon Sep 17 00:00:00 2001 From: jt-dd <112463504+jt-dd@users.noreply.github.com> Date: Thu, 16 Jan 2025 20:32:54 +0100 Subject: [PATCH] Kubehound with no Docker lib dependency (#315) * adding go tag for docker dependencies * adding tag to makefile --- Makefile | 4 +-- cmd/kubehound/backend.go | 22 ++++++++------- cmd/kubehound/dev.go | 9 +++--- cmd/kubehound/dumper.go | 9 ++---- cmd/kubehound/root.go | 16 +---------- cmd/kubehound/util_default.go | 23 +++++++++++++++ cmd/kubehound/util_no_backend.go | 48 ++++++++++++++++++++++++++++++++ pkg/backend/containers.go | 2 ++ pkg/backend/project.go | 2 ++ pkg/backend/util.go | 2 ++ 10 files changed, 99 insertions(+), 38 deletions(-) create mode 100644 cmd/kubehound/util_default.go create mode 100644 cmd/kubehound/util_no_backend.go diff --git a/Makefile b/Makefile index ab83714a3..eae703791 100644 --- a/Makefile +++ b/Makefile @@ -66,11 +66,11 @@ generate: ## Generate code for the application .PHONY: build build: ## Build the application - go build $(BUILD_FLAGS) -o "$(or $(DESTDIR),./bin/build)/kubehound$(BINARY_EXT)" ./cmd/kubehound/ + go build $(BUILD_FLAGS) -o "$(or $(DESTDIR),./bin/build)/kubehound$(BINARY_EXT)" -tags no_backend ./cmd/kubehound/ .PHONY: binary binary: - $(BUILDX_CMD) bake binary-with-coverage + $(BUILDX_CMD) bake binary .PHONY: lint lint: diff --git a/cmd/kubehound/backend.go b/cmd/kubehound/backend.go index e44f5a554..b4412af8e 100644 --- a/cmd/kubehound/backend.go +++ b/cmd/kubehound/backend.go @@ -1,16 +1,18 @@ +//go:build no_backend + package main import ( - docker "github.com/DataDog/KubeHound/pkg/backend" + "github.com/DataDog/KubeHound/pkg/backend" "github.com/spf13/cobra" ) var ( - Backend *docker.Backend + Backend *backend.Backend hard bool composePath []string - uiProfile = docker.DefaultUIProfile + uiProfile = backend.DefaultUIProfile uiInvana bool ) @@ -24,7 +26,7 @@ var ( uiProfile = append(uiProfile, "invana") } - return docker.NewBackend(cobraCmd.Context(), composePath, uiProfile) + return backend.NewBackend(cobraCmd.Context(), composePath, uiProfile) }, } @@ -33,7 +35,7 @@ var ( Short: "Spawn the kubehound stack", Long: `Spawn the kubehound stack`, RunE: func(cobraCmd *cobra.Command, args []string) error { - return docker.Up(cobraCmd.Context()) + return backend.Up(cobraCmd.Context()) }, } @@ -42,7 +44,7 @@ var ( Short: "Wipe the persisted backend data", Long: `Wipe the persisted backend data`, RunE: func(cobraCmd *cobra.Command, args []string) error { - return docker.Wipe(cobraCmd.Context()) + return backend.Wipe(cobraCmd.Context()) }, } @@ -51,7 +53,7 @@ var ( Short: "Tear down the kubehound stack", Long: `Tear down the kubehound stack`, RunE: func(cobraCmd *cobra.Command, args []string) error { - return docker.Down(cobraCmd.Context()) + return backend.Down(cobraCmd.Context()) }, } @@ -60,19 +62,19 @@ var ( Short: "Restart the kubehound stack", Long: `Restart the kubehound stack`, RunE: func(cobraCmd *cobra.Command, args []string) error { - err := docker.Down(cobraCmd.Context()) + err := backend.Down(cobraCmd.Context()) if err != nil { return err } if hard { - err = docker.Wipe(cobraCmd.Context()) + err = backend.Wipe(cobraCmd.Context()) if err != nil { return err } } - return docker.Reset(cobraCmd.Context()) + return backend.Reset(cobraCmd.Context()) }, } ) diff --git a/cmd/kubehound/dev.go b/cmd/kubehound/dev.go index bd40f3074..7c5a367b1 100644 --- a/cmd/kubehound/dev.go +++ b/cmd/kubehound/dev.go @@ -1,3 +1,5 @@ +//go:build no_backend + package main import ( @@ -5,7 +7,6 @@ import ( "os" "github.com/DataDog/KubeHound/pkg/backend" - docker "github.com/DataDog/KubeHound/pkg/backend" "github.com/spf13/cobra" ) @@ -64,15 +65,15 @@ func runEnv(ctx context.Context, composePaths []string) error { profiles = append(profiles, backend.DevUIProfile) } - err := docker.NewBackend(ctx, composePaths, profiles) + err := backend.NewBackend(ctx, composePaths, profiles) if err != nil { return err } if downTesting { - return docker.Down(ctx) + return backend.Down(ctx) } - return docker.BuildUp(ctx, noCache) + return backend.BuildUp(ctx, noCache) } func init() { diff --git a/cmd/kubehound/dumper.go b/cmd/kubehound/dumper.go index e0d7de076..cd3632f65 100644 --- a/cmd/kubehound/dumper.go +++ b/cmd/kubehound/dumper.go @@ -4,7 +4,6 @@ import ( "fmt" "os" - docker "github.com/DataDog/KubeHound/pkg/backend" "github.com/DataDog/KubeHound/pkg/cmd" "github.com/DataDog/KubeHound/pkg/config" "github.com/DataDog/KubeHound/pkg/kubehound/core" @@ -91,13 +90,9 @@ var ( } if startBackend { - err = docker.NewBackend(cobraCmd.Context(), composePath, docker.DefaultUIProfile) + err = runBackendCompose(cobraCmd.Context()) if err != nil { - return fmt.Errorf("new backend: %w", err) - } - err = docker.Up(cobraCmd.Context()) - if err != nil { - return fmt.Errorf("docker up: %w", err) + return err } } diff --git a/cmd/kubehound/root.go b/cmd/kubehound/root.go index fa82e1eaf..f1d2ee958 100644 --- a/cmd/kubehound/root.go +++ b/cmd/kubehound/root.go @@ -3,7 +3,6 @@ package main import ( "fmt" - "github.com/DataDog/KubeHound/pkg/backend" "github.com/DataDog/KubeHound/pkg/cmd" "github.com/DataDog/KubeHound/pkg/kubehound/core" "github.com/DataDog/KubeHound/pkg/telemetry/log" @@ -27,23 +26,10 @@ var ( l := log.Logger(cobraCmd.Context()) // auto spawning the backend stack if !skipBackend { - // Forcing the embed docker config to be loaded - err := backend.NewBackend(cobraCmd.Context(), []string{""}, backend.DefaultUIProfile) + err := runBackend(cobraCmd.Context()) if err != nil { return err } - res, err := backend.IsStackRunning(cobraCmd.Context()) - if err != nil { - return err - } - if !res { - err = backend.Up(cobraCmd.Context()) - if err != nil { - return err - } - } else { - l.Info("Backend stack is already running") - } } // Passing the Kubehound config from viper diff --git a/cmd/kubehound/util_default.go b/cmd/kubehound/util_default.go new file mode 100644 index 000000000..e2dabd4c5 --- /dev/null +++ b/cmd/kubehound/util_default.go @@ -0,0 +1,23 @@ +//go:build !no_backend + +package main + +import ( + "context" + + "github.com/DataDog/KubeHound/pkg/telemetry/log" +) + +func runBackend(ctx context.Context) error { + l := log.Logger(ctx) + l.Warn("Backend is not supported in this build") + + return nil +} + +func runBackendCompose(ctx context.Context) error { + l := log.Logger(ctx) + l.Warn("Backend is not supported in this build") + + return nil +} diff --git a/cmd/kubehound/util_no_backend.go b/cmd/kubehound/util_no_backend.go new file mode 100644 index 000000000..382ece41f --- /dev/null +++ b/cmd/kubehound/util_no_backend.go @@ -0,0 +1,48 @@ +//go:build no_backend + +package main + +import ( + "context" + "fmt" + + "github.com/DataDog/KubeHound/pkg/backend" + "github.com/DataDog/KubeHound/pkg/telemetry/log" +) + +func runBackend(ctx context.Context) error { + l := log.Logger(ctx) + + // Forcing the embed docker config to be loaded + err := backend.NewBackend(ctx, []string{""}, backend.DefaultUIProfile) + if err != nil { + return err + } + res, err := backend.IsStackRunning(ctx) + if err != nil { + return err + } + if !res { + err = backend.Up(ctx) + if err != nil { + return err + } + } else { + l.Info("Backend stack is already running") + } + + return nil +} + +func runBackendCompose(ctx context.Context) error { + err := backend.NewBackend(ctx, composePath, backend.DefaultUIProfile) + if err != nil { + return fmt.Errorf("new backend: %w", err) + } + err = backend.Up(ctx) + if err != nil { + return fmt.Errorf("docker up: %w", err) + } + + return nil +} diff --git a/pkg/backend/containers.go b/pkg/backend/containers.go index 4fab653e8..c318a9b07 100644 --- a/pkg/backend/containers.go +++ b/pkg/backend/containers.go @@ -1,3 +1,5 @@ +//go:build no_backend + package backend import ( diff --git a/pkg/backend/project.go b/pkg/backend/project.go index e168bc702..2d67fce82 100644 --- a/pkg/backend/project.go +++ b/pkg/backend/project.go @@ -1,3 +1,5 @@ +//go:build no_backend + package backend import ( diff --git a/pkg/backend/util.go b/pkg/backend/util.go index a56f16c44..6009d4f89 100644 --- a/pkg/backend/util.go +++ b/pkg/backend/util.go @@ -1,3 +1,5 @@ +//go:build no_backend + package backend func mergeMaps(currentMap map[interface{}]interface{}, newMap map[interface{}]interface{}) map[interface{}]interface{} {