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

Fuzz config.go #1901

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
4 changes: 1 addition & 3 deletions KubeArmor/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ const (
ConfigAnnotateResources string = "annotateResources"
)

func readCmdLineParams() {
func ReadCmdLineParams() {
hostname, _ := os.Hostname()
clusterStr := flag.String(ConfigCluster, "default", "cluster name")
hostStr := flag.String(ConfigHost, hostname, "host name")
Expand Down Expand Up @@ -226,8 +226,6 @@ func readCmdLineParams() {

// LoadConfig Load configuration
func LoadConfig() error {
// Read configuration from command line
readCmdLineParams()

// Read configuration from env var
// Note that the env var has to be set in uppercase for e.g, CLUSTER=xyz ./kubearmor
Expand Down
59 changes: 59 additions & 0 deletions KubeArmor/config/config_fuzz_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// SPDX-License-Identifier: Apache-2.0
// Copyright 2024 Authors of KubeArmor

package config

import (
"os"
"testing"
"fmt"
"strings"
)

type ConfigParseError struct {
err error
}

func (pe ConfigParseError) Error() string {
return fmt.Sprintf("While parsing config: %s", pe.err.Error())
}

func LoadConfig_from_byteslice(data []byte) error {
tempFile, err := os.CreateTemp(".", "kubearmor-*.yaml")
if err != nil {
return err
}
defer os.Remove(tempFile.Name())
if _, err := tempFile.Write(data); err != nil {
return err
}
os.Setenv("KUBEARMOR_CFG", tempFile.Name())
return LoadConfig()
}

func FuzzConfig(f *testing.F){
data1 := []byte(`
cluster: "default"
gRPC: "32767"
hostVisibility: "process,file,network,capabilities"
visibility: "process,file,network,capabilities"
enableKubeArmorHostPolicy: true
enableKubeArmorVm: false
k8s: false
alertThrottling: true
maxAlertPerSec: 10
throttleSec: 30
`)
f.Add(data1)
f.Fuzz(func(t *testing.T, data []byte) {
err := LoadConfig_from_byteslice(data)
if err != nil{
if strings.Contains(err.Error(), "While parsing config:") {
// Skip as these cases are handled by viper internally.
t.Skipf("Skipping config parsing errors: %v", err)
return
}
t.Errorf("Unexpected error: %v", err)
}
})
}
3 changes: 2 additions & 1 deletion KubeArmor/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ func main() {
kg.Err(err.Error())
return
}

// Read configuration from command line
cfg.ReadCmdLineParams()
if err := cfg.LoadConfig(); err != nil {
kg.Err(err.Error())
return
Expand Down
Loading