Skip to content

Commit

Permalink
allowlist for backwards compatible args
Browse files Browse the repository at this point in the history
  • Loading branch information
leehinman committed Oct 17, 2024
1 parent 6d0ff13 commit 1fb1489
Show file tree
Hide file tree
Showing 35 changed files with 141 additions and 9 deletions.
2 changes: 2 additions & 0 deletions auditbeat/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ func init() {
systemTest = flag.Bool("systemTest", false, "Set to true when running system tests")

cmd.RootCmd.PersistentFlags().AddGoFlag(flag.CommandLine.Lookup("systemTest"))
cfgfile.AddAllowedBackwardsCompatibleFlag("systemTest")
cmd.RootCmd.PersistentFlags().AddGoFlag(flag.CommandLine.Lookup("test.coverprofile"))
cfgfile.AddAllowedBackwardsCompatibleFlag("test.coverprofile")
}

// Test started when the test binary is started. Only calls main.
Expand Down
7 changes: 7 additions & 0 deletions filebeat/cmd/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/elastic/beats/v7/filebeat/generator/fields"
"github.com/elastic/beats/v7/filebeat/generator/fileset"
"github.com/elastic/beats/v7/filebeat/generator/module"
"github.com/elastic/beats/v7/libbeat/cfgfile"
"github.com/elastic/beats/v7/libbeat/common/cli"
"github.com/elastic/elastic-agent-libs/paths"
)
Expand Down Expand Up @@ -63,7 +64,9 @@ func genGenerateModuleCmd() *cobra.Command {
}

genModuleCmd.Flags().String("modules-path", defaultHomePath, "Path to modules directory")
cfgfile.AddAllowedBackwardsCompatibleFlag("modules-path")
genModuleCmd.Flags().String("es-beats", defaultHomePath, "Path to Elastic Beats")
cfgfile.AddAllowedBackwardsCompatibleFlag("es-beats")

return genModuleCmd
}
Expand All @@ -88,7 +91,9 @@ func genGenerateFilesetCmd() *cobra.Command {
}

genFilesetCmd.Flags().String("modules-path", defaultHomePath, "Path to modules directory")
cfgfile.AddAllowedBackwardsCompatibleFlag("modules-path")
genFilesetCmd.Flags().String("es-beats", defaultHomePath, "Path to Elastic Beats")
cfgfile.AddAllowedBackwardsCompatibleFlag("es-beats")

return genFilesetCmd
}
Expand All @@ -113,7 +118,9 @@ func genGenerateFieldsCmd() *cobra.Command {
}

genFieldsCmd.Flags().String("es-beats", defaultHomePath, "Path to Elastic Beats")
cfgfile.AddAllowedBackwardsCompatibleFlag("es-beats")
genFieldsCmd.Flags().Bool("without-documentation", false, "Do not add description fields")
cfgfile.AddAllowedBackwardsCompatibleFlag("without-documentation")

return genFieldsCmd
}
5 changes: 5 additions & 0 deletions filebeat/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/elastic/beats/v7/filebeat/fileset"
"github.com/elastic/beats/v7/filebeat/include"
"github.com/elastic/beats/v7/filebeat/input"
"github.com/elastic/beats/v7/libbeat/cfgfile"
"github.com/elastic/beats/v7/libbeat/cmd"
"github.com/elastic/beats/v7/libbeat/cmd/instance"

Expand All @@ -49,7 +50,9 @@ func FilebeatSettings(moduleNameSpace string) instance.Settings {
}
runFlags := pflag.NewFlagSet(Name, pflag.ExitOnError)
runFlags.AddGoFlag(flag.CommandLine.Lookup("once"))
cfgfile.AddAllowedBackwardsCompatibleFlag("once")
runFlags.AddGoFlag(flag.CommandLine.Lookup("modules"))
cfgfile.AddAllowedBackwardsCompatibleFlag("modules")
return instance.Settings{
RunFlags: runFlags,
Name: Name,
Expand All @@ -66,8 +69,10 @@ func FilebeatSettings(moduleNameSpace string) instance.Settings {
func Filebeat(inputs beater.PluginFactory, settings instance.Settings) *cmd.BeatsRootCmd {
command := cmd.GenRootCmdWithSettings(beater.New(inputs), settings)
command.PersistentFlags().AddGoFlag(flag.CommandLine.Lookup("M"))
cfgfile.AddAllowedBackwardsCompatibleFlag("M")
command.TestCmd.Flags().AddGoFlag(flag.CommandLine.Lookup("modules"))
command.SetupCmd.Flags().AddGoFlag(flag.CommandLine.Lookup("modules"))
cfgfile.AddAllowedBackwardsCompatibleFlag("modules")
command.AddCommand(cmd.GenModulesCmd(Name, "", buildModulesManager))
command.AddCommand(genGenerateCmd())
return command
Expand Down
2 changes: 2 additions & 0 deletions filebeat/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ func init() {
systemTest = flag.Bool("systemTest", false, "Set to true when running system tests")
fbCommand = fbcmd.Filebeat(inputs.Init, fbcmd.FilebeatSettings(""))
fbCommand.PersistentFlags().AddGoFlag(flag.CommandLine.Lookup("systemTest"))
cfgfile.AddAllowedBackwardsCompatibleFlag("systemTest")
fbCommand.PersistentFlags().AddGoFlag(flag.CommandLine.Lookup("test.coverprofile"))
cfgfile.AddAllowedBackwardsCompatibleFlag("test.coverprofile")
}

// Test started when the test binary is started. Only calls main.
Expand Down
2 changes: 2 additions & 0 deletions heartbeat/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

"github.com/elastic/beats/v7/heartbeat/beater"
"github.com/elastic/beats/v7/heartbeat/include"
"github.com/elastic/beats/v7/libbeat/cfgfile"
cmd "github.com/elastic/beats/v7/libbeat/cmd"
"github.com/elastic/beats/v7/libbeat/cmd/instance"
"github.com/elastic/beats/v7/libbeat/ecs"
Expand Down Expand Up @@ -81,6 +82,7 @@ func Initialize(settings instance.Settings) *cmd.BeatsRootCmd {
`
setup.ResetFlags()
setup.Flags().Bool(cmd.IndexManagementKey, false, "Setup all components related to Elasticsearch index management, including template, ilm policy and rollover alias")
cfgfile.AddAllowedBackwardsCompatibleFlag(cmd.IndexManagementKey)

return rootCmd
}
Expand Down
2 changes: 2 additions & 0 deletions heartbeat/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ func init() {
testing.Init()
systemTest = flag.Bool("systemTest", false, "Set to true when running system tests")
cmd.RootCmd.PersistentFlags().AddGoFlag(flag.CommandLine.Lookup("systemTest"))
cfgfile.AddAllowedBackwardsCompatibleFlag("systemTest")
cmd.RootCmd.PersistentFlags().AddGoFlag(flag.CommandLine.Lookup("test.coverprofile"))
cfgfile.AddAllowedBackwardsCompatibleFlag("test.coverprofile")
}

// Test started when the test binary is started. Only calls main.
Expand Down
41 changes: 32 additions & 9 deletions libbeat/cfgfile/cfgfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,15 @@ import (

// Evil package level globals
var (
once sync.Once
configfiles *config.StringsFlag
overwrites *config.C
defaults *config.C
homePath *string
configPath *string
dataPath *string
logsPath *string
once sync.Once
configfiles *config.StringsFlag
overwrites *config.C
defaults *config.C
homePath *string
configPath *string
dataPath *string

Check failure on line 42 in libbeat/cfgfile/cfgfile.go

View workflow job for this annotation

GitHub Actions / lint (linux)

var `dataPath` is unused (unused)
logsPath *string

Check failure on line 43 in libbeat/cfgfile/cfgfile.go

View workflow job for this annotation

GitHub Actions / lint (linux)

var `logsPath` is unused (unused)
allowedBackwardsCompatibleFlags []string
)

func Initialize() {
Expand All @@ -50,7 +51,9 @@ func Initialize() {
// created. See ChangeDefaultCfgfileFlag which should
// be called prior to flags.Parse().
configfiles = config.StringArrFlag(nil, "c", "beat.yml", "Configuration file, relative to path.config")
AddAllowedBackwardsCompatibleFlag("c")
overwrites = config.SettingFlag(nil, "E", "Configuration overwrite")
AddAllowedBackwardsCompatibleFlag("E")
defaults = config.MustNewConfigFrom(map[string]interface{}{
"path": map[string]interface{}{
"home": ".", // to be initialized by beat
Expand All @@ -60,16 +63,36 @@ func Initialize() {
},
})
homePath = config.ConfigOverwriteFlag(nil, overwrites, "path.home", "path.home", "", "Home path")
AddAllowedBackwardsCompatibleFlag("path.home")
configPath = config.ConfigOverwriteFlag(nil, overwrites, "path.config", "path.config", "", "Configuration path")
AddAllowedBackwardsCompatibleFlag("path.config")
dataPath = config.ConfigOverwriteFlag(nil, overwrites, "path.data", "path.data", "", "Data path")
AddAllowedBackwardsCompatibleFlag("path.data")
logsPath = config.ConfigOverwriteFlag(nil, overwrites, "path.logs", "path.logs", "", "Logs path")
AddAllowedBackwardsCompatibleFlag("path.logs")
})
}

func isAllowedBackwardsCompatibleFlag(f string) bool {
for _, existing := range allowedBackwardsCompatibleFlags {
if existing == f {
return true
}
}
return false
}

func AddAllowedBackwardsCompatibleFlag(f string) {
if isAllowedBackwardsCompatibleFlag(f) {
return
}
allowedBackwardsCompatibleFlags = append(allowedBackwardsCompatibleFlags, f)
}

func ConvertFlagsForBackwardsCompatibility() {
// backwards compatibility workaround, convert -flags to --flags:
for i, arg := range os.Args[1:] {
if strings.HasPrefix(arg, "-") && !strings.HasPrefix(arg, "--") && len(arg) > 2 {
if strings.HasPrefix(arg, "-") && !strings.HasPrefix(arg, "--") && isAllowedBackwardsCompatibleFlag(strings.TrimPrefix(arg, "-")) {
os.Args[1+i] = "-" + arg
}
}
Expand Down
4 changes: 4 additions & 0 deletions libbeat/cmd/export/dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

"github.com/spf13/cobra"

"github.com/elastic/beats/v7/libbeat/cfgfile"
"github.com/elastic/beats/v7/libbeat/cmd/instance"
"github.com/elastic/beats/v7/libbeat/dashboards"
"github.com/elastic/beats/v7/libbeat/version"
Expand Down Expand Up @@ -101,8 +102,11 @@ func GenDashboardCmd(settings instance.Settings) *cobra.Command {
}

genTemplateConfigCmd.Flags().String("id", "", "Dashboard id")
cfgfile.AddAllowedBackwardsCompatibleFlag("id")
genTemplateConfigCmd.Flags().String("yml", "", "Yaml file containing list of dashboard ID and filename pairs")
cfgfile.AddAllowedBackwardsCompatibleFlag("yml")
genTemplateConfigCmd.Flags().String("folder", "", "Target folder to save exported assets")
cfgfile.AddAllowedBackwardsCompatibleFlag("folder")

return genTemplateConfigCmd
}
3 changes: 3 additions & 0 deletions libbeat/cmd/export/ilm_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package export
import (
"github.com/spf13/cobra"

"github.com/elastic/beats/v7/libbeat/cfgfile"
"github.com/elastic/beats/v7/libbeat/cmd/instance"
"github.com/elastic/beats/v7/libbeat/idxmgmt"
"github.com/elastic/beats/v7/libbeat/idxmgmt/lifecycle"
Expand Down Expand Up @@ -57,7 +58,9 @@ func GenGetILMPolicyCmd(settings instance.Settings) *cobra.Command {
}

genTemplateConfigCmd.Flags().String("es.version", settings.Version, "Elasticsearch version")
cfgfile.AddAllowedBackwardsCompatibleFlag("es.version")
genTemplateConfigCmd.Flags().String("dir", "", "Specify directory for printing policy files. By default policies are printed to stdout.")
cfgfile.AddAllowedBackwardsCompatibleFlag("dir")

return genTemplateConfigCmd
}
2 changes: 2 additions & 0 deletions libbeat/cmd/export/index_pattern.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

"github.com/spf13/cobra"

"github.com/elastic/beats/v7/libbeat/cfgfile"
"github.com/elastic/beats/v7/libbeat/cmd/instance"
"github.com/elastic/beats/v7/libbeat/kibana"
libversion "github.com/elastic/elastic-agent-libs/version"
Expand Down Expand Up @@ -67,6 +68,7 @@ func GenIndexPatternConfigCmd(settings instance.Settings) *cobra.Command {
}

genTemplateConfigCmd.Flags().String("es.version", settings.Version, "Elasticsearch version")
cfgfile.AddAllowedBackwardsCompatibleFlag("es.version")

return genTemplateConfigCmd
}
4 changes: 4 additions & 0 deletions libbeat/cmd/export/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package export
import (
"github.com/spf13/cobra"

"github.com/elastic/beats/v7/libbeat/cfgfile"
"github.com/elastic/beats/v7/libbeat/cmd/instance"
"github.com/elastic/beats/v7/libbeat/idxmgmt"
"github.com/elastic/beats/v7/libbeat/idxmgmt/lifecycle"
Expand Down Expand Up @@ -59,8 +60,11 @@ func GenTemplateConfigCmd(settings instance.Settings) *cobra.Command {
}

genTemplateConfigCmd.Flags().String("es.version", settings.Version, "Elasticsearch version")
cfgfile.AddAllowedBackwardsCompatibleFlag("es.version")
genTemplateConfigCmd.Flags().Bool("noilm", false, "Generate template with ILM disabled")
cfgfile.AddAllowedBackwardsCompatibleFlag("noilm")
genTemplateConfigCmd.Flags().String("dir", "", "Specify directory for printing template files. By default templates are printed to stdout.")
cfgfile.AddAllowedBackwardsCompatibleFlag("dir")

return genTemplateConfigCmd
}
4 changes: 4 additions & 0 deletions libbeat/cmd/keystore.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/spf13/cobra"
tml "golang.org/x/crypto/ssh/terminal"

Check failure on line 30 in libbeat/cmd/keystore.go

View workflow job for this annotation

GitHub Actions / lint (linux)

SA1019: "golang.org/x/crypto/ssh/terminal" is deprecated: this package moved to golang.org/x/term. (staticcheck)

"github.com/elastic/beats/v7/libbeat/cfgfile"
"github.com/elastic/beats/v7/libbeat/cmd/instance"
"github.com/elastic/beats/v7/libbeat/common/cli"
"github.com/elastic/beats/v7/libbeat/common/terminal"
Expand Down Expand Up @@ -74,6 +75,7 @@ func genCreateKeystoreCmd(settings instance.Settings) *cobra.Command {
}),
}
command.Flags().BoolVar(&flagForce, "force", false, "override the existing keystore")
cfgfile.AddAllowedBackwardsCompatibleFlag("force")
return command
}

Expand All @@ -92,7 +94,9 @@ func genAddKeystoreCmd(settings instance.Settings) *cobra.Command {
}),
}
command.Flags().BoolVar(&flagStdin, "stdin", false, "Use the stdin as the source of the secret")
cfgfile.AddAllowedBackwardsCompatibleFlag("stdin")
command.Flags().BoolVar(&flagForce, "force", false, "Override the existing key")
cfgfile.AddAllowedBackwardsCompatibleFlag("force")
return command
}

Expand Down
12 changes: 12 additions & 0 deletions libbeat/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,18 +87,30 @@ func GenRootCmdWithSettings(beatCreator beat.Creator, settings instance.Settings

// Persistent flags, common across all subcommands
rootCmd.PersistentFlags().AddGoFlag(flag.CommandLine.Lookup("E"))
cfgfile.AddAllowedBackwardsCompatibleFlag("E")
rootCmd.PersistentFlags().AddGoFlag(flag.CommandLine.Lookup("c"))
cfgfile.AddAllowedBackwardsCompatibleFlag("c")
rootCmd.PersistentFlags().AddGoFlag(flag.CommandLine.Lookup("d"))
cfgfile.AddAllowedBackwardsCompatibleFlag("d")
rootCmd.PersistentFlags().AddGoFlag(flag.CommandLine.Lookup("v"))
cfgfile.AddAllowedBackwardsCompatibleFlag("v")
rootCmd.PersistentFlags().AddGoFlag(flag.CommandLine.Lookup("e"))
cfgfile.AddAllowedBackwardsCompatibleFlag("e")
rootCmd.PersistentFlags().AddGoFlag(flag.CommandLine.Lookup("environment"))
cfgfile.AddAllowedBackwardsCompatibleFlag("environment")
rootCmd.PersistentFlags().AddGoFlag(flag.CommandLine.Lookup("path.config"))
cfgfile.AddAllowedBackwardsCompatibleFlag("path.config")
rootCmd.PersistentFlags().AddGoFlag(flag.CommandLine.Lookup("path.data"))
cfgfile.AddAllowedBackwardsCompatibleFlag("path.data")
rootCmd.PersistentFlags().AddGoFlag(flag.CommandLine.Lookup("path.logs"))
cfgfile.AddAllowedBackwardsCompatibleFlag("path.logs")
rootCmd.PersistentFlags().AddGoFlag(flag.CommandLine.Lookup("path.home"))
cfgfile.AddAllowedBackwardsCompatibleFlag("path.home")
rootCmd.PersistentFlags().AddGoFlag(flag.CommandLine.Lookup("strict.perms"))
cfgfile.AddAllowedBackwardsCompatibleFlag("strict.perms")
if f := flag.CommandLine.Lookup("plugin"); f != nil {
rootCmd.PersistentFlags().AddGoFlag(f)
cfgfile.AddAllowedBackwardsCompatibleFlag("plugin")
}

// Inherit root flags from run command
Expand Down
5 changes: 5 additions & 0 deletions libbeat/cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/spf13/cobra"

"github.com/elastic/beats/v7/libbeat/beat"
"github.com/elastic/beats/v7/libbeat/cfgfile"
"github.com/elastic/beats/v7/libbeat/cmd/instance"
)

Expand All @@ -42,9 +43,13 @@ func genRunCmd(settings instance.Settings, beatCreator beat.Creator) *cobra.Comm

// Run subcommand flags, only available to *beat run
runCmd.Flags().AddGoFlag(flag.CommandLine.Lookup("N"))
cfgfile.AddAllowedBackwardsCompatibleFlag("N")
runCmd.Flags().AddGoFlag(flag.CommandLine.Lookup("httpprof"))
cfgfile.AddAllowedBackwardsCompatibleFlag("httpprof")
runCmd.Flags().AddGoFlag(flag.CommandLine.Lookup("cpuprofile"))
cfgfile.AddAllowedBackwardsCompatibleFlag("cpuprofile")
runCmd.Flags().AddGoFlag(flag.CommandLine.Lookup("memprofile"))
cfgfile.AddAllowedBackwardsCompatibleFlag("memprofile")

if settings.RunFlags != nil {
runCmd.Flags().AddFlagSet(settings.RunFlags)
Expand Down
6 changes: 6 additions & 0 deletions libbeat/cmd/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/spf13/cobra"

"github.com/elastic/beats/v7/libbeat/beat"
"github.com/elastic/beats/v7/libbeat/cfgfile"
"github.com/elastic/beats/v7/libbeat/cmd/instance"
)

Expand Down Expand Up @@ -111,11 +112,16 @@ func genSetupCmd(settings instance.Settings, beatCreator beat.Creator) *cobra.Co
}

setup.Flags().Bool(DashboardKey, false, "Setup dashboards")
cfgfile.AddAllowedBackwardsCompatibleFlag(DashboardKey)
setup.Flags().Bool(PipelineKey, false, "Setup Ingest pipelines")
cfgfile.AddAllowedBackwardsCompatibleFlag(PipelineKey)
setup.Flags().Bool(IndexManagementKey, false,
"Setup all components related to Elasticsearch index management, including template, ilm policy and rollover alias")
cfgfile.AddAllowedBackwardsCompatibleFlag(IndexManagementKey)
setup.Flags().Bool("enable-all-filesets", false, "Behave as if all modules and filesets had been enabled")
cfgfile.AddAllowedBackwardsCompatibleFlag("enable-all-filesets")
setup.Flags().Bool("force-enable-module-filesets", false, "Behave as if all filesets, within enabled modules, are enabled")
cfgfile.AddAllowedBackwardsCompatibleFlag("force-enable-module-filesets")

return &setup
}
2 changes: 2 additions & 0 deletions libbeat/libbeat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ func init() {
systemTest = flag.Bool("systemTest", false, "Set to true when running system tests")

RootCmd.PersistentFlags().AddGoFlag(flag.CommandLine.Lookup("systemTest"))
cfgfile.AddAllowedBackwardsCompatibleFlag("systemTest")
RootCmd.PersistentFlags().AddGoFlag(flag.CommandLine.Lookup("test.coverprofile"))
cfgfile.AddAllowedBackwardsCompatibleFlag("test.coverprofile")
}

// Test started when the test binary is started
Expand Down
2 changes: 2 additions & 0 deletions metricbeat/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (

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

"github.com/elastic/beats/v7/libbeat/cfgfile"
"github.com/elastic/beats/v7/libbeat/cmd"
"github.com/elastic/beats/v7/libbeat/cmd/instance"
"github.com/elastic/beats/v7/libbeat/ecs"
Expand Down Expand Up @@ -61,6 +62,7 @@ func MetricbeatSettings(moduleNameSpace string) instance.Settings {
}
var runFlags = pflag.NewFlagSet(Name, pflag.ExitOnError)
runFlags.AddGoFlag(flag.CommandLine.Lookup("system.hostfs"))
cfgfile.AddAllowedBackwardsCompatibleFlag("system.hostfs")
return instance.Settings{
RunFlags: runFlags,
Name: Name,
Expand Down
2 changes: 2 additions & 0 deletions metricbeat/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ func init() {
testing.Init()
systemTest = flag.Bool("systemTest", false, "Set to true when running system tests")
cmd.RootCmd.PersistentFlags().AddGoFlag(flag.CommandLine.Lookup("systemTest"))
cfgfile.AddAllowedBackwardsCompatibleFlag("systemTest")
cmd.RootCmd.PersistentFlags().AddGoFlag(flag.CommandLine.Lookup("test.coverprofile"))
cfgfile.AddAllowedBackwardsCompatibleFlag("test.coverprofile")
}

// Test started when the test binary is started. Only calls main.
Expand Down
Loading

0 comments on commit 1fb1489

Please sign in to comment.