Skip to content

Commit

Permalink
Mage: Validate modules.d dir in Filebeat + Disabled filesets
Browse files Browse the repository at this point in the history
  • Loading branch information
adriansr committed Sep 6, 2021
1 parent 20be506 commit 4baf4dd
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 0 deletions.
70 changes: 70 additions & 0 deletions dev-tools/mage/modules.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,15 @@
package mage

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"

"github.com/joeshaw/multierror"
"github.com/pkg/errors"
"gopkg.in/yaml.v2"
)

var modulesDConfigTemplate = `
Expand Down Expand Up @@ -71,3 +76,68 @@ func GenerateDirModulesD() error {
}
return nil
}

type datasetDefinition struct {
Enabled *bool
}

type moduleDefinition struct {
Name string `yaml:"module"`
Filesets map[string]datasetDefinition `yaml:",inline"`
}

// ValidateDirModulesD validates a modules.d directory containing the
// <module>.yml.disabled files. It checks that the files are valid
// yaml and conform to module definitions.
func ValidateDirModulesD() error {
_, err := loadModulesD()
return err
}

// ValidateDirModulesDDatasetsDisabled ensures that all the datasets
// are disabled by default.
func ValidateDirModulesDDatasetsDisabled() error {
cfgs, err := loadModulesD()
if err != nil {
return err
}
var errs multierror.Errors
for path, cfg := range cfgs {
// A config.yml is a list of module configurations.
for modIdx, mod := range cfg {
// A module config is a map of datasets.
for dsName, ds := range mod.Filesets {
if ds.Enabled == nil || *ds.Enabled {
var entry string
if len(cfg) > 1 {
entry = fmt.Sprintf(" (entry #%d)", modIdx+1)
}
err = fmt.Errorf("in file '%s': %s module%s dataset %s must be explicitly disabled (needs `enabled: false`)",
path, mod.Name, entry, dsName)
errs = append(errs, err)
}
}
}
}
return errs.Err()
}

func loadModulesD() (modules map[string][]moduleDefinition, err error) {
files, err := filepath.Glob("modules.d/*.disabled")
if err != nil {
return nil, err
}
modules = make(map[string][]moduleDefinition, len(files))
for _, file := range files {
contents, err := ioutil.ReadFile(file)
if err != nil {
return nil, errors.Wrapf(err, "reading %s", file)
}
var cfg []moduleDefinition
if err = yaml.Unmarshal(contents, &cfg); err != nil {
return nil, errors.Wrapf(err, "parsing %s as YAML", file)
}
modules[file] = cfg
}
return modules, nil
}
1 change: 1 addition & 0 deletions filebeat/magefile.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ func Update() {
// modules.d directory.
func Config() {
mg.Deps(devtools.GenerateDirModulesD, configYML)
mg.SerialDeps(devtools.ValidateDirModulesD, devtools.ValidateDirModulesDDatasetsDisabled)
}

func configYML() error {
Expand Down
1 change: 1 addition & 0 deletions x-pack/filebeat/magefile.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ func ExportDashboard() error {
// Config generates both the short and reference configs.
func Config() {
mg.Deps(configYML, devtools.GenerateDirModulesD)
mg.SerialDeps(devtools.ValidateDirModulesD, devtools.ValidateDirModulesDDatasetsDisabled)
}

func configYML() error {
Expand Down

0 comments on commit 4baf4dd

Please sign in to comment.