Skip to content

Commit

Permalink
Kubehound with no Docker lib dependency (#315)
Browse files Browse the repository at this point in the history
* adding go tag for docker dependencies

* adding tag to makefile
  • Loading branch information
jt-dd authored Jan 16, 2025
1 parent dd25721 commit 56408b5
Show file tree
Hide file tree
Showing 10 changed files with 99 additions and 38 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
22 changes: 12 additions & 10 deletions cmd/kubehound/backend.go
Original file line number Diff line number Diff line change
@@ -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
)

Expand All @@ -24,7 +26,7 @@ var (
uiProfile = append(uiProfile, "invana")
}

return docker.NewBackend(cobraCmd.Context(), composePath, uiProfile)
return backend.NewBackend(cobraCmd.Context(), composePath, uiProfile)
},
}

Expand All @@ -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())
},
}

Expand All @@ -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())
},
}

Expand All @@ -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())
},
}

Expand All @@ -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())
},
}
)
Expand Down
9 changes: 5 additions & 4 deletions cmd/kubehound/dev.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
//go:build no_backend

package main

import (
"context"
"os"

"github.com/DataDog/KubeHound/pkg/backend"
docker "github.com/DataDog/KubeHound/pkg/backend"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -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() {
Expand Down
9 changes: 2 additions & 7 deletions cmd/kubehound/dumper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
}

Expand Down
16 changes: 1 addition & 15 deletions cmd/kubehound/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down
23 changes: 23 additions & 0 deletions cmd/kubehound/util_default.go
Original file line number Diff line number Diff line change
@@ -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
}
48 changes: 48 additions & 0 deletions cmd/kubehound/util_no_backend.go
Original file line number Diff line number Diff line change
@@ -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
}
2 changes: 2 additions & 0 deletions pkg/backend/containers.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build no_backend

package backend

import (
Expand Down
2 changes: 2 additions & 0 deletions pkg/backend/project.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build no_backend

package backend

import (
Expand Down
2 changes: 2 additions & 0 deletions pkg/backend/util.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build no_backend

package backend

func mergeMaps(currentMap map[interface{}]interface{}, newMap map[interface{}]interface{}) map[interface{}]interface{} {
Expand Down

0 comments on commit 56408b5

Please sign in to comment.