Skip to content

Commit

Permalink
Add explicit YAML, JSON tags to config struct (#147)
Browse files Browse the repository at this point in the history
  • Loading branch information
yashtewari committed May 15, 2022
1 parent 5bf1f12 commit 0d2061b
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 12 deletions.
12 changes: 3 additions & 9 deletions beater/cloudbeat.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ import (
csppolicies "github.com/elastic/csp-security-policies/bundle"

"github.com/gofrs/uuid"
"gopkg.in/yaml.v3"
)

// cloudbeat configuration.
Expand Down Expand Up @@ -170,23 +169,18 @@ func (bt *cloudbeat) Run(b *beat.Beat) error {
break
}

// TODO(yashtewari): Figure out the scenarios in which the integration sends
// multiple input streams. Since only one instance of our integration is allowed per
// agent policy, is it even possible that multiple input streams are received?
y, err := yaml.Marshal(bt.config.Streams[0].DataYaml)
y, err := bt.config.DataYaml()
if err != nil {
logp.L().Errorf("Could not marshal to YAML: %v", err)
break
}

s := string(y)

if err := csppolicies.HostBundleWithDataYaml("bundle.tar.gz", policies, s); err != nil {
if err := csppolicies.HostBundleWithDataYaml("bundle.tar.gz", policies, y); err != nil {
logp.L().Errorf("Could not update bundle with dataYaml: %v", err)
break
}

logp.L().Infof("Bundle updated with dataYaml: %s", s)
logp.L().Infof("Bundle updated with dataYaml: %s", y)

case fetchedResources := <-output:
cycleId, _ := uuid.NewV4()
Expand Down
19 changes: 16 additions & 3 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (

"github.com/elastic/beats/v7/libbeat/common"
"github.com/elastic/beats/v7/libbeat/processors"
"gopkg.in/yaml.v3"
)

const DefaultNamespace = "default"
Expand All @@ -43,9 +44,9 @@ type Config struct {
type Stream struct {
DataYaml struct {
ActivatedRules struct {
CISK8S []string `config:"cis_k8s"`
} `config:"activated_rules"`
} `config:"data_yaml"`
CISK8S []string `config:"cis_k8s" yaml:"cis_k8s" json:"cis_k8s"`
} `config:"activated_rules" yaml:"activated_rules" json:"activated_rules"`
} `config:"data_yaml" yaml:"data_yaml" json:"data_yaml"`
}

var DefaultConfig = Config{
Expand All @@ -70,6 +71,18 @@ func (c *Config) Update(cfg *common.Config) error {
return nil
}

func (c *Config) DataYaml() (string, error) {
// TODO(yashtewari): Figure out the scenarios in which the integration sends
// multiple input streams. Since only one instance of our integration is allowed per
// agent policy, is it even possible that multiple input streams are received?
y, err := yaml.Marshal(c.Streams[0].DataYaml)
if err != nil {
return "", err
}

return string(y), nil
}

// Datastream function to generate the datastream value
func Datastream(namespace string, indexPrefix string) string {
if namespace == "" {
Expand Down
42 changes: 42 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
package config

import (
"strings"
"testing"

"github.com/elastic/beats/v7/libbeat/common"
Expand Down Expand Up @@ -68,3 +69,44 @@ func (s *ConfigTestSuite) TestNew() {
s.Equal(test.expectedPatterns, c.Streams[0].DataYaml.ActivatedRules.CISK8S)
}
}

func (s *ConfigTestSuite) TestDataYaml() {
var tests = []struct {
config string
expectedYaml string
}{
{
`
streams:
- data_yaml:
activated_rules:
cis_k8s:
- a
- b
- c
- d
`,
`
activated_rules:
cis_k8s:
- a
- b
- c
- d
`,
},
}

for _, test := range tests {
cfg, err := common.NewConfigFrom(test.config)
s.NoError(err)

c, err := New(cfg)
s.NoError(err)

dy, err := c.DataYaml()
s.NoError(err)

s.Equal(strings.TrimSpace(test.expectedYaml), strings.TrimSpace(dy))
}
}

0 comments on commit 0d2061b

Please sign in to comment.