Skip to content
Open
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
55 changes: 40 additions & 15 deletions pkg/acquisition/modules/kubernetesaudit/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,39 +20,64 @@ type Configuration struct {
configuration.DataSourceCommonCfg `yaml:",inline"`
}

func (s *Source) UnmarshalConfig(yamlConfig []byte) error {
k8sConfig := Configuration{}
func ConfigurationFromYAML(y []byte) (Configuration, error) {
var cfg Configuration

if err := yaml.UnmarshalWithOptions(y, &cfg, yaml.Strict()); err != nil {
return cfg, fmt.Errorf("cannot parse: %s", yaml.FormatError(err, false, false))
}

err := yaml.UnmarshalWithOptions(yamlConfig, &k8sConfig, yaml.Strict())
cfg.SetDefaults()
cfg.Normalize()

err := cfg.Validate()
if err != nil {
return fmt.Errorf("cannot parse k8s-audit configuration: %s", yaml.FormatError(err, false, false))
return cfg, err
}

s.config = k8sConfig
return cfg, nil
}

if s.config.ListenAddr == "" {
return errors.New("listen_addr cannot be empty")
func (c *Configuration) SetDefaults() {
if c.Mode == "" {
c.Mode = configuration.TAIL_MODE
}
}

if s.config.ListenPort == 0 {
return errors.New("listen_port cannot be empty")
func (s *Source) UnmarshalConfig(yamlConfig []byte) error {
cfg, err := ConfigurationFromYAML(yamlConfig)
if err != nil {
return err
}

if s.config.WebhookPath == "" {
return errors.New("webhook_path cannot be empty")
s.config = cfg

return nil
}

func (c *Configuration) Validate() error {
if c.ListenAddr == "" {
return errors.New("listen_addr cannot be empty")
}

if s.config.WebhookPath[0] != '/' {
s.config.WebhookPath = "/" + s.config.WebhookPath
if c.ListenPort == 0 {
return errors.New("listen_port cannot be empty")
}

if s.config.Mode == "" {
s.config.Mode = configuration.TAIL_MODE
if c.WebhookPath == "" {
return errors.New("webhook_path cannot be empty")
}

return nil
}


func (c *Configuration) Normalize() {
if c.WebhookPath != "" && c.WebhookPath[0] != '/' {
c.WebhookPath = "/" + c.WebhookPath
}
}

func (s *Source) Configure(_ context.Context, config []byte, logger *log.Entry, metricsLevel metrics.AcquisitionMetricsLevel) error {
s.logger = logger
s.metricsLevel = metricsLevel
Expand Down