Skip to content

Commit

Permalink
feat: validate benchmark type
Browse files Browse the repository at this point in the history
  • Loading branch information
olegsu committed Dec 19, 2022
1 parent db2733e commit 36d98fb
Show file tree
Hide file tree
Showing 11 changed files with 122 additions and 23 deletions.
3 changes: 2 additions & 1 deletion beater/cloudbeat.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ package beater
import (
"context"
"fmt"
"github.com/elastic/cloudbeat/resources/providers"
"time"

"github.com/elastic/cloudbeat/resources/providers"

"github.com/elastic/cloudbeat/config"
"github.com/elastic/cloudbeat/dataprovider"
"github.com/elastic/cloudbeat/evaluator"
Expand Down
2 changes: 1 addition & 1 deletion beater/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ type validator struct{}
func (v *validator) Validate(cfg *agentconfig.C) error {
c, err := config.New(cfg)
if err != nil {
return fmt.Errorf("could not parse reconfiguration %v, skipping with error: %v", cfg.FlattenedKeys(), err)
return fmt.Errorf("could not parse reconfiguration %v, skipping with error: %w", cfg.FlattenedKeys(), err)
}

if c.RuntimeCfg == nil {
Expand Down
29 changes: 29 additions & 0 deletions config/benchmark.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 29 additions & 9 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,14 @@ package config

import (
"context"
"github.com/elastic/elastic-agent-libs/logp"
"errors"
"fmt"
"os"
"path/filepath"
"time"

"github.com/elastic/elastic-agent-libs/logp"

"github.com/elastic/beats/v7/libbeat/processors"
"github.com/elastic/beats/v7/x-pack/libbeat/common/aws"
"github.com/elastic/elastic-agent-libs/config"
Expand All @@ -38,11 +41,7 @@ const DefaultNamespace = "default"

const ResultsDatastreamIndexPrefix = "logs-cloud_security_posture.findings"

const (
InputTypeVanillaK8s = "cloudbeat/cis_k8s"
InputTypeEks = "cloudbeat/cis_eks"
InputTypeAws = "cloudbeat/cis_aws"
)
var ErrBenchmarkNotSupported = errors.New("benchmark is not supported")

type Fetcher struct {
Name string `config:"name"` // Name of the fetcher
Expand All @@ -57,6 +56,7 @@ type Config struct {
Period time.Duration `config:"period"`
Processors processors.PluginConfig `config:"processors"`
BundlePath string `config:"bundle_path"`
Benchmark *string `config:"config.v1.benchmark"`
}

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

if c.RuntimeCfg != nil && c.RuntimeCfg.ActivatedRules != nil && len(c.RuntimeCfg.ActivatedRules.CisEks) > 0 {
c.Type = InputTypeEks
if c.Benchmark != nil {
if !isSupportedBenchmark(*c.Benchmark) {
return c, ErrBenchmarkNotSupported
}
c.Type = buildConfigType(*c.Benchmark)
} else {
if c.RuntimeCfg != nil && c.RuntimeCfg.ActivatedRules != nil && len(c.RuntimeCfg.ActivatedRules.CisEks) > 0 {
c.Type = buildConfigType(CIS_EKS)
}
}
return c, nil
}

func defaultConfig() (*Config, error) {
ret := &Config{
Period: 4 * time.Hour,
Type: InputTypeVanillaK8s,
Type: buildConfigType(CIS_K8S),
}

bundle, err := getBundlePath()
Expand Down Expand Up @@ -120,3 +127,16 @@ func Datastream(namespace string, indexPrefix string) string {
type AwsConfigProvider interface {
InitializeAWSConfig(ctx context.Context, cfg aws.ConfigAWS, log *logp.Logger) (awssdk.Config, error)
}

func isSupportedBenchmark(benchmark string) bool {
for _, s := range SupportedCIS {
if benchmark == s {
return true
}
}
return false
}

func buildConfigType(benchmark string) string {
return fmt.Sprintf("cloudbeat/%s", benchmark)
}
42 changes: 42 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,48 @@ not_runtime_cfg:
}
}

func (s *ConfigTestSuite) TestBenchmarkType() {
tests := []struct {
config string
expected string
wantError bool
}{
{
`
config:
v1:
benchmark: cis_eks
`,
"cis_eks",
false,
},
{
`
config:
v1:
benchmark: cis_gcp
`,
"",
true,
},
}

for i, test := range tests {
s.Run(fmt.Sprint(i), func() {
cfg, err := config.NewConfigFrom(test.config)
s.NoError(err)

c, err := New(cfg)
if test.wantError {
s.Error(err)
return
}
s.NoError(err)
s.Equal(test.expected, *c.Benchmark)
})
}
}

func (s *ConfigTestSuite) TestRuntimeConfig() {
tests := []struct {
config string
Expand Down
6 changes: 6 additions & 0 deletions launcher/launcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,14 @@
package launcher

import (
"errors"
"fmt"
"sync"
"time"

"github.com/elastic/beats/v7/libbeat/beat"
"github.com/elastic/beats/v7/libbeat/management"
cloudbeat_config "github.com/elastic/cloudbeat/config"
"github.com/elastic/elastic-agent-libs/config"
"github.com/elastic/elastic-agent-libs/logp"
"github.com/elastic/go-ucfg"
Expand Down Expand Up @@ -239,6 +242,9 @@ func (l *launcher) reconfigureWait(timeout time.Duration) (*config.C, error) {
err := l.validator.Validate(update)
if err != nil {
l.log.Errorf("Config update validation failed: %v", err)
if errors.Is(err, cloudbeat_config.ErrBenchmarkNotSupported) {
l.beat.Manager.UpdateStatus(management.Degraded, cloudbeat_config.ErrBenchmarkNotSupported.Error())
}
continue
}
}
Expand Down
2 changes: 1 addition & 1 deletion resources/fetchersManager/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func (fa *factories) parseConfigFetcher(log *logp.Logger, fcfg *agentconfig.C, c
// This function takes the configuration file provided by the integration the `cfg` file
// and depending on the input type, extract the relevant credentials and add them to the fetcher config
func addCredentialsToFetcherConfiguration(log *logp.Logger, cfg *config.Config, fcfg *agentconfig.C) {
if cfg.Type == config.InputTypeEks || cfg.Type == config.InputTypeAws {
if cfg.Type == config.CIS_EKS || cfg.Type == config.CIS_AWS {
err := fcfg.Merge(cfg.AWSConfig)
if err != nil {
log.Errorf("Failed to merge aws configuration to fetcher configuration: %v", err)
Expand Down
2 changes: 1 addition & 1 deletion resources/fetchersManager/factory_aws_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ func (s *FactoriesTestSuite) TestRegisterFetchersWithAwsCredentials() {

func createEksAgentConfig(s *FactoriesTestSuite, awsConfig aws.ConfigAWS, fetcherName string) *config.Config {
conf := &config.Config{
Type: config.InputTypeEks,
Type: config.CIS_EKS,
AWSConfig: awsConfig,
RuntimeCfg: nil,
Fetchers: []*agentconfig.C{agentconfig.MustNewConfigFrom(fmt.Sprint("name: ", fetcherName))},
Expand Down
5 changes: 3 additions & 2 deletions resources/providers/cluster_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package providers
import (
"context"
"fmt"

"github.com/elastic/cloudbeat/config"
"github.com/elastic/cloudbeat/resources/providers/awslib"
"github.com/elastic/elastic-agent-libs/logp"
Expand All @@ -40,10 +41,10 @@ type ClusterNameProvider struct {

func (provider ClusterNameProvider) GetClusterName(ctx context.Context, cfg *config.Config, log *logp.Logger) (string, error) {
switch cfg.Type {
case config.InputTypeVanillaK8s:
case config.CIS_K8S:
log.Debugf("Trying to identify Kubernetes Vanilla cluster name")
return provider.KubernetesClusterNameProvider.GetClusterName(cfg, provider.KubeClient)
case config.InputTypeEks:
case config.CIS_EKS:
log.Debugf("Trying to identify EKS cluster name")
awsConfig, err := provider.AwsConfigProvider.InitializeAWSConfig(ctx, cfg.AWSConfig, log)
if err != nil {
Expand Down
9 changes: 5 additions & 4 deletions resources/providers/cluster_provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@ package providers

import (
"context"
"testing"

awssdk "github.com/aws/aws-sdk-go-v2/aws"
"github.com/elastic/beats/v7/x-pack/libbeat/common/aws"
"github.com/elastic/cloudbeat/config"
"github.com/elastic/cloudbeat/resources/providers/awslib"
"github.com/stretchr/testify/mock"
k8sfake "k8s.io/client-go/kubernetes/fake"
"testing"

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

func (s *ClusterProviderTestSuite) TestGetClusterName() {
var tests = []struct {
tests := []struct {
config config.Config
vanillaClusterName string
eksClusterName string
expectedClusterName string
}{
{
config.Config{
Type: config.InputTypeVanillaK8s,
Type: config.CIS_K8S,
KubeConfig: "",
},
"vanilla-cluster",
Expand All @@ -66,7 +67,7 @@ func (s *ClusterProviderTestSuite) TestGetClusterName() {
},
{
config.Config{
Type: config.InputTypeEks,
Type: config.CIS_EKS,
AWSConfig: aws.ConfigAWS{},
},
"vanilla-cluster",
Expand Down
7 changes: 3 additions & 4 deletions scripts/common.sh
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ copy_to_agents() {
}

restart_agents() {
echo "Agent restart is not supported yet"
# for P in $(get_agents); do
# exec_pod $POD "elastic-agent restart" # https://github.com/elastic/cloudbeat/pull/458#issuecomment-1308837098
# done
for P in $(get_agents); do
exec_pod $POD "elastic-agent restart" # https://github.com/elastic/cloudbeat/pull/458#issuecomment-1308837098
done
}

0 comments on commit 36d98fb

Please sign in to comment.