Skip to content

Commit

Permalink
Fix issue with management.ConfigTransform.SetTransform.
Browse files Browse the repository at this point in the history
  • Loading branch information
blakerouse committed Apr 4, 2024
1 parent a108161 commit 14bcf16
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 26 deletions.
20 changes: 16 additions & 4 deletions x-pack/agentbeat/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,28 @@ into a single agentbeat binary.`,
}

func prepareCommand(rootCmd *cmd.BeatsRootCmd) *cobra.Command {
if rootCmd.PersistentPreRun != nil || rootCmd.PersistentPreRunE != nil {
panic("command cannot already have PersistentPreRun or PersistentPreRunE set")
}
rootCmd.PersistentPreRun = func(cmd *cobra.Command, args []string) {
var origPersistentPreRun func(cmd *cobra.Command, args []string)
var origPersistentPreRunE func(cmd *cobra.Command, args []string) error
origPersistentPreRun = rootCmd.PersistentPreRun
origPersistentPreRunE = rootCmd.PersistentPreRunE
rootCmd.PersistentPreRun = nil
rootCmd.PersistentPreRunE = func(cmd *cobra.Command, args []string) error {
// same logic is used inside of *cobra.Command; if both are set the E version is used instead
if origPersistentPreRunE != nil {
if err := origPersistentPreRunE(cmd, args); err != nil {
// no context is added by cobra, same approach here
return err
}
} else if origPersistentPreRun != nil {
origPersistentPreRun(cmd, args)
}
// must be set to the correct file before the actual Run is performed otherwise it will not be the correct
// filename, as all the beats set this in the initialization.
err := cfgfile.ChangeDefaultCfgfileFlag(rootCmd.Use)
if err != nil {
panic(fmt.Errorf("failed to set default config file path: %v", err))
}
return nil
}
return &rootCmd.Command
}
22 changes: 15 additions & 7 deletions x-pack/auditbeat/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@ import (
"fmt"
"strings"

"github.com/spf13/cobra"

auditbeatcmd "github.com/elastic/beats/v7/auditbeat/cmd"
"github.com/elastic/beats/v7/libbeat/cmd"
"github.com/elastic/beats/v7/libbeat/common/reload"
"github.com/elastic/beats/v7/libbeat/processors"
"github.com/elastic/beats/v7/x-pack/auditbeat/module/system"
"github.com/elastic/beats/v7/x-pack/libbeat/management"
"github.com/elastic/elastic-agent-client/v7/pkg/client"
"github.com/elastic/elastic-agent-client/v7/pkg/proto"
Expand Down Expand Up @@ -43,12 +46,15 @@ func auditbeatCfg(rawIn *proto.UnitExpectedConfig, agentInfo *client.AgentInfo)
return nil, fmt.Errorf("error creating input list from raw expected config: %w", err)
}

// Extract the type field that has "audit/auditd", treat this
// as the module config key
module := strings.Split(rawIn.Type, "/")[1]

for iter := range modules {
modules[iter]["module"] = module
if system.ModuleName == "audit/system" {
// running in agentbeat; no need to transform the type field
} else {
// not running in agentbeat; extract the type field that has
// "audit/auditd", treat this as the module config key
module := strings.Split(rawIn.Type, "/")[1]
for iter := range modules {
modules[iter]["module"] = module
}
}

// Format for the reloadable list needed bythe cm.Reload() method.
Expand All @@ -61,14 +67,16 @@ func auditbeatCfg(rawIn *proto.UnitExpectedConfig, agentInfo *client.AgentInfo)
}

func init() {
management.ConfigTransform.SetTransform(auditbeatCfg)
globalProcs, err := processors.NewPluginConfigFromList(defaultProcessors())
if err != nil { // these are hard-coded, shouldn't fail
panic(fmt.Errorf("error creating global processors: %w", err))
}
settings := auditbeatcmd.AuditbeatSettings(globalProcs)
settings.ElasticLicensed = true
RootCmd = auditbeatcmd.Initialize(settings)
RootCmd.PersistentPreRun = func(cmd *cobra.Command, args []string) {
management.ConfigTransform.SetTransform(auditbeatCfg)
}
}

func defaultProcessors() []mapstr.M {
Expand Down
6 changes: 5 additions & 1 deletion x-pack/filebeat/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"fmt"
"os"

"github.com/spf13/cobra"

fbcmd "github.com/elastic/beats/v7/filebeat/cmd"
cmd "github.com/elastic/beats/v7/libbeat/cmd"
"github.com/elastic/beats/v7/libbeat/processors"
Expand All @@ -26,7 +28,6 @@ const Name = fbcmd.Name

// Filebeat build the beat root command for executing filebeat and it's subcommands.
func Filebeat() *cmd.BeatsRootCmd {
management.ConfigTransform.SetTransform(filebeatCfg)
settings := fbcmd.FilebeatSettings()
globalProcs, err := processors.NewPluginConfigFromList(defaultProcessors())
if err != nil { // these are hard-coded, shouldn't fail
Expand All @@ -35,6 +36,9 @@ func Filebeat() *cmd.BeatsRootCmd {
settings.Processing = processing.MakeDefaultSupport(true, globalProcs, processing.WithECS, processing.WithHost, processing.WithAgentMeta())
settings.ElasticLicensed = true
command := fbcmd.Filebeat(inputs.Init, settings)
command.PersistentPreRun = func(cmd *cobra.Command, args []string) {
management.ConfigTransform.SetTransform(filebeatCfg)
}
return command
}

Expand Down
6 changes: 5 additions & 1 deletion x-pack/heartbeat/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ package cmd
import (
"fmt"

"github.com/spf13/cobra"

heartbeatCmd "github.com/elastic/beats/v7/heartbeat/cmd"
"github.com/elastic/beats/v7/libbeat/cmd"
"github.com/elastic/beats/v7/libbeat/common/reload"
Expand Down Expand Up @@ -43,8 +45,10 @@ func TransformRawIn(rawIn *proto.UnitExpectedConfig) []map[string]interface{} {
}

func init() {
management.ConfigTransform.SetTransform(heartbeatCfg)
settings := heartbeatCmd.HeartbeatSettings()
settings.ElasticLicensed = true
RootCmd = heartbeatCmd.Initialize(settings)
RootCmd.PersistentPreRun = func(cmd *cobra.Command, args []string) {
management.ConfigTransform.SetTransform(heartbeatCfg)
}
}
5 changes: 4 additions & 1 deletion x-pack/metricbeat/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"flag"
"fmt"

"github.com/spf13/cobra"
"github.com/spf13/pflag"

"github.com/elastic/beats/v7/libbeat/cmd"
Expand Down Expand Up @@ -46,7 +47,6 @@ var withECSVersion = processing.WithFields(mapstr.M{
})

func init() {
management.ConfigTransform.SetTransform(metricbeatCfg)
var runFlags = pflag.NewFlagSet(Name, pflag.ExitOnError)
runFlags.AddGoFlag(flag.CommandLine.Lookup("system.hostfs"))
globalProcs, err := processors.NewPluginConfigFromList(defaultProcessors())
Expand All @@ -63,6 +63,9 @@ func init() {
RootCmd = cmd.GenRootCmdWithSettings(beater.DefaultCreator(), settings)
RootCmd.AddCommand(cmd.GenModulesCmd(Name, "", mbcmd.BuildModulesManager))
RootCmd.TestCmd.AddCommand(test.GenTestModulesCmd(Name, "", beater.DefaultTestModulesCreator()))
RootCmd.PersistentPreRun = func(cmd *cobra.Command, args []string) {
management.ConfigTransform.SetTransform(metricbeatCfg)
}
}

func defaultProcessors() []mapstr.M {
Expand Down
20 changes: 11 additions & 9 deletions x-pack/osquerybeat/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,21 @@ package cmd
import (
"fmt"

"github.com/spf13/cobra"

"github.com/elastic/beats/v7/x-pack/libbeat/management"
"github.com/elastic/elastic-agent-client/v7/pkg/client"
"github.com/elastic/elastic-agent-client/v7/pkg/proto"
"github.com/elastic/elastic-agent-libs/logp"
"github.com/elastic/elastic-agent-libs/mapstr"

cmd "github.com/elastic/beats/v7/libbeat/cmd"
"github.com/elastic/beats/v7/libbeat/cmd/instance"
"github.com/elastic/beats/v7/libbeat/common/cli"
"github.com/elastic/beats/v7/libbeat/common/reload"
"github.com/elastic/beats/v7/libbeat/ecs"
"github.com/elastic/beats/v7/libbeat/processors"
"github.com/elastic/beats/v7/libbeat/publisher/processing"
"github.com/elastic/beats/v7/x-pack/libbeat/management"
"github.com/elastic/elastic-agent-client/v7/pkg/client"
"github.com/elastic/elastic-agent-client/v7/pkg/proto"
"github.com/elastic/elastic-agent-libs/logp"
"github.com/elastic/elastic-agent-libs/mapstr"

"github.com/spf13/cobra"

_ "github.com/elastic/beats/v7/x-pack/libbeat/include"
"github.com/elastic/beats/v7/x-pack/osquerybeat/beater"
_ "github.com/elastic/beats/v7/x-pack/osquerybeat/include"
Expand All @@ -44,7 +44,6 @@ var withECSVersion = processing.WithFields(mapstr.M{
var RootCmd = Osquerybeat()

func Osquerybeat() *cmd.BeatsRootCmd {
management.ConfigTransform.SetTransform(osquerybeatCfg)
globalProcs, err := processors.NewPluginConfigFromList(defaultProcessors())
if err != nil { // these are hard-coded, shouldn't fail
panic(fmt.Errorf("error creating global processors: %w", err))
Expand All @@ -55,6 +54,9 @@ func Osquerybeat() *cmd.BeatsRootCmd {
ElasticLicensed: true,
}
command := cmd.GenRootCmdWithSettings(beater.New, settings)
command.PersistentPreRun = func(cmd *cobra.Command, args []string) {
management.ConfigTransform.SetTransform(osquerybeatCfg)
}

// Add verify command
command.AddCommand(genVerifyCmd(settings))
Expand Down
10 changes: 7 additions & 3 deletions x-pack/packetbeat/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ package cmd
import (
"fmt"

"github.com/spf13/cobra"

"github.com/elastic/beats/v7/libbeat/cmd"
"github.com/elastic/beats/v7/libbeat/common/reload"
"github.com/elastic/beats/v7/libbeat/processors"
Expand Down Expand Up @@ -55,16 +57,18 @@ func packetbeatCfg(rawIn *proto.UnitExpectedConfig, agentInfo *client.AgentInfo)
}

func init() {
// Register packetbeat with central management to perform any needed config
// transformations before agent configs are sent to the beat during reload.
management.ConfigTransform.SetTransform(packetbeatCfg)
globalProcs, err := processors.NewPluginConfigFromList(defaultProcessors())
if err != nil { // these are hard-coded, shouldn't fail
panic(fmt.Errorf("error creating global processors: %w", err))
}
settings := packetbeatCmd.PacketbeatSettings(globalProcs)
settings.ElasticLicensed = true
RootCmd = packetbeatCmd.Initialize(settings)
RootCmd.PersistentPreRun = func(cmd *cobra.Command, args []string) {
// Register packetbeat with central management to perform any needed config
// transformations before agent configs are sent to the beat during reload.
management.ConfigTransform.SetTransform(packetbeatCfg)
}
}

func defaultProcessors() []mapstr.M {
Expand Down

0 comments on commit 14bcf16

Please sign in to comment.