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

Use positionnal argument for directory/file input/output path #232

Merged
merged 6 commits into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
7 changes: 5 additions & 2 deletions cmd/kubehound/dumper.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ var (
},
RunE: func(cobraCmd *cobra.Command, args []string) error {
// using compress feature
viper.Set(config.CollectorFileArchiveFormat, true)
viper.Set(config.CollectorFileArchiveNoCompress, false)

// Create a temporary directory
tmpDir, err := os.MkdirTemp("", "kubehound")
Expand Down Expand Up @@ -64,10 +64,13 @@ var (
},
}
dumpLocalCmd = &cobra.Command{
Use: "local",
Use: "local [directory to dump the data]",
Short: "Dump locally the k8s resources of a targeted cluster",
Args: cobra.ExactArgs(1),
Long: `Collect all Kubernetes resources needed to build the attack path in an offline format locally (compressed or flat)`,
PreRunE: func(cobraCmd *cobra.Command, args []string) error {
viper.Set(config.CollectorFileDirectory, args[0])

return cmd.InitializeKubehoundConfig(cobraCmd.Context(), "", true, true)
},
RunE: func(cobraCmd *cobra.Command, args []string) error {
Expand Down
10 changes: 3 additions & 7 deletions cmd/kubehound/ingest.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@ import (
"github.com/spf13/viper"
)

var (
inputFilePath string
)

var (
ingestCmd = &cobra.Command{
Use: "ingest",
Expand All @@ -22,9 +18,10 @@ var (
}

localIngestCmd = &cobra.Command{
Use: "local",
Use: "local [directory or tar.gz path]",
Short: "Ingest data locally from a KubeHound dump",
Long: `Run an ingestion locally using a previous dump (directory or tar.gz)`,
Args: cobra.ExactArgs(1),
PreRunE: func(cobraCmd *cobra.Command, args []string) error {
return cmd.InitializeKubehoundConfig(cobraCmd.Context(), "", true, true)
},
Expand All @@ -35,7 +32,7 @@ var (
return fmt.Errorf("get config: %w", err)
}

return core.CoreLocalIngest(cobraCmd.Context(), khCfg, inputFilePath)
return core.CoreLocalIngest(cobraCmd.Context(), khCfg, args[0])
},
}

Expand Down Expand Up @@ -82,7 +79,6 @@ func init() {

ingestCmd.AddCommand(localIngestCmd)
cmd.InitLocalIngestCmd(localIngestCmd)
localIngestCmd.Flags().StringVar(&inputFilePath, "data", "", "Filepath for the data to process (directory or tar.gz path)")

ingestCmd.AddCommand(remoteIngestCmd)
cmd.InitRemoteIngestCmd(remoteIngestCmd, true)
Expand Down
8 changes: 2 additions & 6 deletions pkg/cmd/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,8 @@ func InitDumpCmd(cmd *cobra.Command) {
}

func InitLocalDumpCmd(cmd *cobra.Command) {
cmd.Flags().Bool("compress", false, "Enable compression for the dumped data (generates a tar.gz file)")
viper.BindPFlag(config.CollectorFileArchiveFormat, cmd.Flags().Lookup("compress")) //nolint: errcheck

cmd.Flags().String("output-dir", "", "Directory to dump the data")
viper.BindPFlag(config.CollectorFileDirectory, cmd.Flags().Lookup("output-dir")) //nolint: errcheck
cmd.MarkFlagRequired("output-dir") //nolint: errcheck
cmd.Flags().Bool("no-compress", false, "Enable compression for the dumped data (generates a tar.gz file)")
Copy link
Contributor

Choose a reason for hiding this comment

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

I feel the name no-compress tells the opposite of Enable compression

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 fixed

viper.BindPFlag(config.CollectorFileArchiveNoCompress, cmd.Flags().Lookup("no-compress")) //nolint: errcheck
}

func InitRemoteDumpCmd(cmd *cobra.Command) {
Expand Down
23 changes: 12 additions & 11 deletions pkg/config/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,17 @@ const (
DefaultK8sAPIPageBufferSize int32 = 10
DefaultK8sAPIRateLimitPerSecond int = 100
DefaultK8sAPINonInteractive bool = false

CollectorLiveRate = "collector.live.rate_limit_per_second"
CollectorLivePageSize = "collector.live.page_size"
CollectorLivePageBufferSize = "collector.live.page_buffer_size"
CollectorNonInteractive = "collector.non_interactive"
CollectorFileArchiveFormat = "collector.file.archive.format"
CollectorFileDirectory = "collector.file.directory"
CollectorFileClusterName = "collector.file.cluster_name"
CollectorFileBlobRegion = "collector.file.blob.region"
CollectorFileBlobBucket = "collector.file.blob.bucket"
DefaultArchiveNoCompress bool = false

CollectorLiveRate = "collector.live.rate_limit_per_second"
CollectorLivePageSize = "collector.live.page_size"
CollectorLivePageBufferSize = "collector.live.page_buffer_size"
CollectorNonInteractive = "collector.non_interactive"
CollectorFileArchiveNoCompress = "collector.file.archive.no_compress"
CollectorFileDirectory = "collector.file.directory"
CollectorFileClusterName = "collector.file.cluster_name"
CollectorFileBlobRegion = "collector.file.blob.region"
CollectorFileBlobBucket = "collector.file.blob.bucket"
)

// CollectorConfig configures collector specific parameters.
Expand Down Expand Up @@ -47,7 +48,7 @@ type FileCollectorConfig struct {

type FileArchiveConfig struct {
ArchiveName string `mapstructure:"archive_name"` // Name of the output archive
Format bool `mapstructure:"format"` // Enable compression for the dumped data (generates a tar.gz file)
NoCompress bool `mapstructure:"no_compress"` // Disable compression for the dumped data (generates a tar.gz file)
}

type BlobConfig struct {
Expand Down
3 changes: 3 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ func SetDefaultValues(c *viper.Viper) {
c.SetDefault(CollectorLiveRate, DefaultK8sAPIRateLimitPerSecond)
c.SetDefault(CollectorNonInteractive, DefaultK8sAPINonInteractive)

// File collector module
c.SetDefault(CollectorFileArchiveNoCompress, DefaultArchiveNoCompress)

// Default values for storage provider
c.SetDefault("storage.wipe", true)
c.SetDefault("storage.retry", DefaultRetry)
Expand Down
8 changes: 8 additions & 0 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ func TestMustLoadConfig(t *testing.T) {
File: &FileCollectorConfig{
Directory: "cluster-data/",
ClusterName: "test-cluster",
Archive: &FileArchiveConfig{
NoCompress: DefaultArchiveNoCompress,
},
},
// This is always set as the default value
Live: &K8SAPICollectorConfig{
Expand Down Expand Up @@ -102,6 +105,11 @@ func TestMustLoadConfig(t *testing.T) {
},
Collector: CollectorConfig{
Type: CollectorTypeK8sAPI,
File: &FileCollectorConfig{
Archive: &FileArchiveConfig{
NoCompress: DefaultArchiveNoCompress,
},
},
Live: &K8SAPICollectorConfig{
PageSize: 500,
PageBufferSize: 10,
Expand Down
2 changes: 1 addition & 1 deletion pkg/kubehound/core/core_dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func runLocalDump(ctx context.Context, khCfg *config.KubehoundConfig) (string, e

// Create the dumper instance
collectorLocalOutputDir := khCfg.Collector.File.Directory
collectorLocalCompress := khCfg.Collector.File.Archive.Format
collectorLocalCompress := !khCfg.Collector.File.Archive.NoCompress
log.I.Infof("Dumping %q to %q", khCfg.Dynamic.ClusterName, collectorLocalOutputDir)
dumpIngestor, err := dump.NewDumpIngestor(ctx, collect, collectorLocalCompress, collectorLocalOutputDir, khCfg.Dynamic.RunID)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion test/system/setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func Dump(ctx context.Context, compress bool) (*config.KubehoundConfig, string)
}
cmd.InitDumpCmd(dumpCmd)

viper.Set(config.CollectorFileArchiveFormat, compress)
viper.Set(config.CollectorFileArchiveNoCompress, !compress)

tmpDir, err := os.MkdirTemp("/tmp/", "kh-system-tests-*")
if err != nil {
Expand Down
Loading