Skip to content

Commit 1dc08a6

Browse files
committed
feat: validate benchmark type
1 parent 43bbe1d commit 1dc08a6

File tree

11 files changed

+105
-23
lines changed

11 files changed

+105
-23
lines changed

beater/cloudbeat.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,10 @@ package beater
2020
import (
2121
"context"
2222
"fmt"
23-
"github.com/elastic/cloudbeat/resources/providers"
2423
"time"
2524

25+
"github.com/elastic/cloudbeat/resources/providers"
26+
2627
"github.com/elastic/cloudbeat/config"
2728
"github.com/elastic/cloudbeat/dataprovider"
2829
"github.com/elastic/cloudbeat/evaluator"

beater/validator.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ type validator struct{}
3232
func (v *validator) Validate(cfg *agentconfig.C) error {
3333
c, err := config.New(cfg)
3434
if err != nil {
35-
return fmt.Errorf("could not parse reconfiguration %v, skipping with error: %v", cfg.FlattenedKeys(), err)
35+
return fmt.Errorf("could not parse reconfiguration %v, skipping with error: %w", cfg.FlattenedKeys(), err)
3636
}
3737

3838
if c.RuntimeCfg == nil {

config/benchmark.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package config
2+
3+
type Benchmark int
4+
5+
// https://github.com/elastic/integrations/tree/main/packages/cloud_security_posture/data_stream/findings/agent/stream
6+
const (
7+
CIS_K8S = "cis_k8s"
8+
CIS_EKS = "cis_eks"
9+
CIS_AWS = "cis_aws"
10+
)
11+
12+
var SupportedCIS = []string{CIS_AWS, CIS_K8S, CIS_EKS}

config/config.go

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,14 @@ package config
2222

2323
import (
2424
"context"
25-
"github.com/elastic/elastic-agent-libs/logp"
25+
"errors"
26+
"fmt"
2627
"os"
2728
"path/filepath"
2829
"time"
2930

31+
"github.com/elastic/elastic-agent-libs/logp"
32+
3033
"github.com/elastic/beats/v7/libbeat/processors"
3134
"github.com/elastic/beats/v7/x-pack/libbeat/common/aws"
3235
"github.com/elastic/elastic-agent-libs/config"
@@ -38,11 +41,7 @@ const DefaultNamespace = "default"
3841

3942
const ResultsDatastreamIndexPrefix = "logs-cloud_security_posture.findings"
4043

41-
const (
42-
InputTypeVanillaK8s = "cloudbeat/cis_k8s"
43-
InputTypeEks = "cloudbeat/cis_eks"
44-
InputTypeAws = "cloudbeat/cis_aws"
45-
)
44+
var ErrBenchmarkNotSupported = errors.New("benchmark is not supported")
4645

4746
type Fetcher struct {
4847
Name string `config:"name"` // Name of the fetcher
@@ -57,6 +56,7 @@ type Config struct {
5756
Period time.Duration `config:"period"`
5857
Processors processors.PluginConfig `config:"processors"`
5958
BundlePath string `config:"bundle_path"`
59+
Benchmark *string `config:"config.v1.benchmark"`
6060
}
6161

6262
type RuntimeConfig struct {
@@ -79,16 +79,23 @@ func New(cfg *config.C) (*Config, error) {
7979
return nil, err
8080
}
8181

82-
if c.RuntimeCfg != nil && c.RuntimeCfg.ActivatedRules != nil && len(c.RuntimeCfg.ActivatedRules.CisEks) > 0 {
83-
c.Type = InputTypeEks
82+
if c.Benchmark != nil {
83+
if !isSupportedBenchmark(*c.Benchmark) {
84+
return c, ErrBenchmarkNotSupported
85+
}
86+
c.Type = buildConfigType(*c.Benchmark)
87+
} else {
88+
if c.RuntimeCfg != nil && c.RuntimeCfg.ActivatedRules != nil && len(c.RuntimeCfg.ActivatedRules.CisEks) > 0 {
89+
c.Type = buildConfigType(CIS_EKS)
90+
}
8491
}
8592
return c, nil
8693
}
8794

8895
func defaultConfig() (*Config, error) {
8996
ret := &Config{
9097
Period: 4 * time.Hour,
91-
Type: InputTypeVanillaK8s,
98+
Type: buildConfigType(CIS_K8S),
9299
}
93100

94101
bundle, err := getBundlePath()
@@ -120,3 +127,16 @@ func Datastream(namespace string, indexPrefix string) string {
120127
type AwsConfigProvider interface {
121128
InitializeAWSConfig(ctx context.Context, cfg aws.ConfigAWS, log *logp.Logger) (awssdk.Config, error)
122129
}
130+
131+
func isSupportedBenchmark(benchmark string) bool {
132+
for _, s := range SupportedCIS {
133+
if benchmark == s {
134+
return true
135+
}
136+
}
137+
return false
138+
}
139+
140+
func buildConfigType(benchmark string) string {
141+
return fmt.Sprintf("cloudbeat/%s", benchmark)
142+
}

config/config_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,48 @@ not_runtime_cfg:
170170
}
171171
}
172172

173+
func (s *ConfigTestSuite) TestBenchmarkType() {
174+
tests := []struct {
175+
config string
176+
expected string
177+
wantError bool
178+
}{
179+
{
180+
`
181+
config:
182+
v1:
183+
benchmark: cis_eks
184+
`,
185+
"cis_eks",
186+
false,
187+
},
188+
{
189+
`
190+
config:
191+
v1:
192+
benchmark: cis_gcp
193+
`,
194+
"",
195+
true,
196+
},
197+
}
198+
199+
for i, test := range tests {
200+
s.Run(fmt.Sprint(i), func() {
201+
cfg, err := config.NewConfigFrom(test.config)
202+
s.NoError(err)
203+
204+
c, err := New(cfg)
205+
if test.wantError {
206+
s.Error(err)
207+
return
208+
}
209+
s.NoError(err)
210+
s.Equal(test.expected, *c.Benchmark)
211+
})
212+
}
213+
}
214+
173215
func (s *ConfigTestSuite) TestRuntimeConfig() {
174216
tests := []struct {
175217
config string

launcher/launcher.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,14 @@
2121
package launcher
2222

2323
import (
24+
"errors"
2425
"fmt"
2526
"sync"
2627
"time"
2728

2829
"github.com/elastic/beats/v7/libbeat/beat"
30+
"github.com/elastic/beats/v7/libbeat/management"
31+
cloudbeat_config "github.com/elastic/cloudbeat/config"
2932
"github.com/elastic/elastic-agent-libs/config"
3033
"github.com/elastic/elastic-agent-libs/logp"
3134
"github.com/elastic/go-ucfg"
@@ -239,6 +242,9 @@ func (l *launcher) reconfigureWait(timeout time.Duration) (*config.C, error) {
239242
err := l.validator.Validate(update)
240243
if err != nil {
241244
l.log.Errorf("Config update validation failed: %v", err)
245+
if errors.Is(err, cloudbeat_config.ErrBenchmarkNotSupported) {
246+
l.beat.Manager.UpdateStatus(management.Degraded, cloudbeat_config.ErrBenchmarkNotSupported.Error())
247+
}
242248
continue
243249
}
244250
}

resources/fetchersManager/factory.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ func (fa *factories) parseConfigFetcher(log *logp.Logger, fcfg *agentconfig.C, c
9494
// This function takes the configuration file provided by the integration the `cfg` file
9595
// and depending on the input type, extract the relevant credentials and add them to the fetcher config
9696
func addCredentialsToFetcherConfiguration(log *logp.Logger, cfg *config.Config, fcfg *agentconfig.C) {
97-
if cfg.Type == config.InputTypeEks || cfg.Type == config.InputTypeAws {
97+
if cfg.Type == config.CIS_EKS || cfg.Type == config.CIS_AWS {
9898
err := fcfg.Merge(cfg.AWSConfig)
9999
if err != nil {
100100
log.Errorf("Failed to merge aws configuration to fetcher configuration: %v", err)

resources/fetchersManager/factory_aws_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ func (s *FactoriesTestSuite) TestRegisterFetchersWithAwsCredentials() {
169169

170170
func createEksAgentConfig(s *FactoriesTestSuite, awsConfig aws.ConfigAWS, fetcherName string) *config.Config {
171171
conf := &config.Config{
172-
Type: config.InputTypeEks,
172+
Type: config.CIS_EKS,
173173
AWSConfig: awsConfig,
174174
RuntimeCfg: nil,
175175
Fetchers: []*agentconfig.C{agentconfig.MustNewConfigFrom(fmt.Sprint("name: ", fetcherName))},

resources/providers/cluster_provider.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ package providers
2020
import (
2121
"context"
2222
"fmt"
23+
2324
"github.com/elastic/cloudbeat/config"
2425
"github.com/elastic/cloudbeat/resources/providers/awslib"
2526
"github.com/elastic/elastic-agent-libs/logp"
@@ -40,9 +41,9 @@ type ClusterNameProvider struct {
4041

4142
func (provider ClusterNameProvider) GetClusterName(ctx context.Context, cfg *config.Config, log *logp.Logger) (string, error) {
4243
switch cfg.Type {
43-
case config.InputTypeVanillaK8s:
44+
case config.CIS_K8S:
4445
return provider.KubernetesClusterNameProvider.GetClusterName(cfg, provider.KubeClient)
45-
case config.InputTypeEks:
46+
case config.CIS_EKS:
4647
awsConfig, err := provider.AwsConfigProvider.InitializeAWSConfig(ctx, cfg.AWSConfig, log)
4748
if err != nil {
4849
return "", fmt.Errorf("failed to initialize aws configuration for identifying the cluster name: %v", err)

resources/providers/cluster_provider_test.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,14 @@ package providers
1919

2020
import (
2121
"context"
22+
"testing"
23+
2224
awssdk "github.com/aws/aws-sdk-go-v2/aws"
2325
"github.com/elastic/beats/v7/x-pack/libbeat/common/aws"
2426
"github.com/elastic/cloudbeat/config"
2527
"github.com/elastic/cloudbeat/resources/providers/awslib"
2628
"github.com/stretchr/testify/mock"
2729
k8sfake "k8s.io/client-go/kubernetes/fake"
28-
"testing"
2930

3031
"github.com/elastic/elastic-agent-libs/logp"
3132
"github.com/stretchr/testify/suite"
@@ -49,15 +50,15 @@ func TestClusterProviderTestSuite(t *testing.T) {
4950
}
5051

5152
func (s *ClusterProviderTestSuite) TestGetClusterName() {
52-
var tests = []struct {
53+
tests := []struct {
5354
config config.Config
5455
vanillaClusterName string
5556
eksClusterName string
5657
expectedClusterName string
5758
}{
5859
{
5960
config.Config{
60-
Type: config.InputTypeVanillaK8s,
61+
Type: config.CIS_K8S,
6162
KubeConfig: "",
6263
},
6364
"vanilla-cluster",
@@ -66,7 +67,7 @@ func (s *ClusterProviderTestSuite) TestGetClusterName() {
6667
},
6768
{
6869
config.Config{
69-
Type: config.InputTypeEks,
70+
Type: config.CIS_EKS,
7071
AWSConfig: aws.ConfigAWS{},
7172
},
7273
"vanilla-cluster",

0 commit comments

Comments
 (0)