diff --git a/NOTICE.txt b/NOTICE.txt index b5df79133f70..e6ebacd0ffe4 100644 --- a/NOTICE.txt +++ b/NOTICE.txt @@ -25581,6 +25581,43 @@ THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +-------------------------------------------------------------------------------- +Dependency : golang.org/x/term +Version: v0.24.0 +Licence type (autodetected): BSD-3-Clause +-------------------------------------------------------------------------------- + +Contents of probable licence file $GOMODCACHE/golang.org/x/term@v0.24.0/LICENSE: + +Copyright 2009 The Go Authors. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are +met: + + * Redistributions of source code must retain the above copyright +notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above +copyright notice, this list of conditions and the following disclaimer +in the documentation and/or other materials provided with the +distribution. + * Neither the name of Google LLC nor the names of its +contributors may be used to endorse or promote products derived from +this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + + -------------------------------------------------------------------------------- Dependency : golang.org/x/text Version: v0.18.0 @@ -57052,43 +57089,6 @@ THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. --------------------------------------------------------------------------------- -Dependency : golang.org/x/term -Version: v0.24.0 -Licence type (autodetected): BSD-3-Clause --------------------------------------------------------------------------------- - -Contents of probable licence file $GOMODCACHE/golang.org/x/term@v0.24.0/LICENSE: - -Copyright 2009 The Go Authors. - -Redistribution and use in source and binary forms, with or without -modification, are permitted provided that the following conditions are -met: - - * Redistributions of source code must retain the above copyright -notice, this list of conditions and the following disclaimer. - * Redistributions in binary form must reproduce the above -copyright notice, this list of conditions and the following disclaimer -in the documentation and/or other materials provided with the -distribution. - * Neither the name of Google LLC nor the names of its -contributors may be used to endorse or promote products derived from -this software without specific prior written permission. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - - -------------------------------------------------------------------------------- Dependency : golang.org/x/xerrors Version: v0.0.0-20231012003039-104605ab7028 diff --git a/auditbeat/main_test.go b/auditbeat/main_test.go index f91bc1f94822..8b16fe63a583 100644 --- a/auditbeat/main_test.go +++ b/auditbeat/main_test.go @@ -24,6 +24,7 @@ import ( "testing" "github.com/elastic/beats/v7/auditbeat/cmd" + "github.com/elastic/beats/v7/libbeat/cfgfile" "github.com/elastic/beats/v7/libbeat/tests/system/template" ) @@ -34,11 +35,14 @@ 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. func TestSystem(*testing.T) { + cfgfile.ConvertFlagsForBackwardsCompatibility() if *systemTest { main() } diff --git a/filebeat/cmd/generate.go b/filebeat/cmd/generate.go index 582038716a1e..1e5a4b1a819b 100644 --- a/filebeat/cmd/generate.go +++ b/filebeat/cmd/generate.go @@ -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" ) @@ -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 } @@ -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 } @@ -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 } diff --git a/filebeat/cmd/root.go b/filebeat/cmd/root.go index 2420a03efdba..48e9c9d74bce 100644 --- a/filebeat/cmd/root.go +++ b/filebeat/cmd/root.go @@ -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" @@ -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, @@ -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 diff --git a/filebeat/main_test.go b/filebeat/main_test.go index 0f989a77a355..b73c88438e6f 100644 --- a/filebeat/main_test.go +++ b/filebeat/main_test.go @@ -26,6 +26,7 @@ import ( fbcmd "github.com/elastic/beats/v7/filebeat/cmd" inputs "github.com/elastic/beats/v7/filebeat/input/default-inputs" + "github.com/elastic/beats/v7/libbeat/cfgfile" cmd "github.com/elastic/beats/v7/libbeat/cmd" "github.com/elastic/beats/v7/libbeat/tests/system/template" ) @@ -40,11 +41,14 @@ 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. func TestSystem(t *testing.T) { + cfgfile.ConvertFlagsForBackwardsCompatibility() if *systemTest { if err := fbCommand.Execute(); err != nil { os.Exit(1) diff --git a/go.mod b/go.mod index 75aba4602368..252fea019974 100644 --- a/go.mod +++ b/go.mod @@ -224,6 +224,7 @@ require ( go.opentelemetry.io/collector/consumer v0.109.0 go.opentelemetry.io/collector/pdata v1.15.0 go.opentelemetry.io/collector/receiver v0.109.0 + golang.org/x/term v0.24.0 google.golang.org/genproto/googleapis/api v0.0.0-20240725223205-93522f1f2a9f gopkg.in/natefinch/lumberjack.v2 v2.2.1 ) @@ -382,7 +383,6 @@ require ( go.opentelemetry.io/otel/trace v1.29.0 // indirect go.uber.org/ratelimit v0.3.1 // indirect golang.org/x/exp v0.0.0-20240205201215-2c58cdc269a3 // indirect - golang.org/x/term v0.24.0 // indirect golang.org/x/xerrors v0.0.0-20231012003039-104605ab7028 // indirect google.golang.org/genproto/googleapis/rpc v0.0.0-20240822170219-fc7c04adadcd // indirect gopkg.in/yaml.v3 v3.0.1 // indirect diff --git a/heartbeat/cmd/root.go b/heartbeat/cmd/root.go index fd95013c6fe0..a1f80a7cda71 100644 --- a/heartbeat/cmd/root.go +++ b/heartbeat/cmd/root.go @@ -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" @@ -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 } diff --git a/heartbeat/main_test.go b/heartbeat/main_test.go index a806e1588bd0..18cc332b63af 100644 --- a/heartbeat/main_test.go +++ b/heartbeat/main_test.go @@ -24,6 +24,7 @@ import ( "testing" "github.com/elastic/beats/v7/heartbeat/cmd" + "github.com/elastic/beats/v7/libbeat/cfgfile" "github.com/elastic/beats/v7/libbeat/tests/system/template" ) @@ -33,11 +34,14 @@ 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. func TestSystem(_ *testing.T) { + cfgfile.ConvertFlagsForBackwardsCompatibility() if *systemTest { main() } diff --git a/libbeat/cfgfile/cfgfile.go b/libbeat/cfgfile/cfgfile.go index ca19af8cb9f6..14e38c5ab7de 100644 --- a/libbeat/cfgfile/cfgfile.go +++ b/libbeat/cfgfile/cfgfile.go @@ -18,9 +18,12 @@ package cfgfile import ( + "flag" "fmt" "os" "path/filepath" + "strings" + "sync" "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/common/fleetmode" @@ -28,39 +31,72 @@ import ( "github.com/elastic/elastic-agent-libs/logp" ) -// Command line flags. +// Evil package level globals var ( - // The default config cannot include the beat name as it is not initialized - // when this variable is created. See ChangeDefaultCfgfileFlag which should - // be called prior to flags.Parse(). - configfiles = config.StringArrFlag(nil, "c", "beat.yml", "Configuration file, relative to path.config") - overwrites = config.SettingFlag(nil, "E", "Configuration overwrite") - - // Additional default settings, that must be available for variable expansion - defaults = config.MustNewConfigFrom(map[string]interface{}{ - "path": map[string]interface{}{ - "home": ".", // to be initialized by beat - "config": "${path.home}", - "data": fmt.Sprint("${path.home}", string(os.PathSeparator), "data"), - "logs": fmt.Sprint("${path.home}", string(os.PathSeparator), "logs"), - }, + once sync.Once + configfiles *config.StringsFlag + overwrites *config.C + defaults *config.C + homePath *string + configPath *string + allowedBackwardsCompatibleFlags []string +) + +func Initialize() { + once.Do(func() { + // The default config cannot include the beat name as + // it is not initialized when this variable is + // 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 + "config": "${path.home}", + "data": filepath.Join("${path.home}", "data"), + "logs": filepath.Join("${path.home}", "logs"), + }, + }) + 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") + _ = config.ConfigOverwriteFlag(nil, overwrites, "path.data", "path.data", "", "Data path") + AddAllowedBackwardsCompatibleFlag("path.data") + _ = config.ConfigOverwriteFlag(nil, overwrites, "path.logs", "path.logs", "", "Logs path") + AddAllowedBackwardsCompatibleFlag("path.logs") }) +} - // home-path CLI flag (initialized in init) - homePath *string - configPath *string -) +func isAllowedBackwardsCompatibleFlag(f string) bool { + for _, existing := range allowedBackwardsCompatibleFlags { + if existing == f { + return true + } + } + return false +} -func init() { - // add '-path.x' options overwriting paths in 'overwrites' config - makePathFlag := func(name, usage string) *string { - return config.ConfigOverwriteFlag(nil, overwrites, name, name, "", usage) +func AddAllowedBackwardsCompatibleFlag(f string) { + if isAllowedBackwardsCompatibleFlag(f) { + return } + allowedBackwardsCompatibleFlags = append(allowedBackwardsCompatibleFlags, f) +} - homePath = makePathFlag("path.home", "Home path") - configPath = makePathFlag("path.config", "Configuration path") - makePathFlag("path.data", "Data path") - makePathFlag("path.logs", "Logs path") +func ConvertFlagsForBackwardsCompatibility() { + // backwards compatibility workaround, convert -flags to --flags: + for i, arg := range os.Args[1:] { + if strings.HasPrefix(arg, "-") && !strings.HasPrefix(arg, "--") { + candidate, _, _ := strings.Cut(strings.TrimPrefix(arg, "-"), "=") + if isAllowedBackwardsCompatibleFlag(candidate) { + os.Args[1+i] = "-" + arg + } + } + } } // OverrideChecker checks if a config should be overwritten. @@ -73,9 +109,11 @@ type ConditionalOverride struct { Config *config.C } -// ChangeDefaultCfgfileFlag replaces the value and default value for the `-c` -// flag so that it reflects the beat name. +// ChangeDefaultCfgfileFlag replaces the value and default value for +// the `-c` flag so that it reflects the beat name. It will call +// Initialize() to register the `-c` flags func ChangeDefaultCfgfileFlag(beatName string) error { + Initialize() configfiles.SetDefault(beatName + ".yml") return nil } @@ -96,8 +134,12 @@ func GetDefaultCfgfile() string { return cfg } -// HandleFlags adapts default config settings based on command line flags. +// HandleFlags adapts default config settings based on command line +// flags. This also stores if -E management.enabled=true was set on +// command line to determine if running the Beat under agent. It will +// call Initialize() to register the flags like `-E`. func HandleFlags() error { + Initialize() // default for the home path is the binary location home, err := filepath.Abs(filepath.Dir(os.Args[0])) if err != nil { @@ -114,6 +156,27 @@ func HandleFlags() error { common.PrintConfigDebugf(overwrites, "CLI setting overwrites (-E flag):") } + // Enable check to see if beat is running under Agent + // This is stored in a package so the modules which don't have + // access to the config can check this value. + type management struct { + Enabled bool `config:"management.enabled"` + } + var managementSettings management + cfgFlag := flag.Lookup("E") + if cfgFlag == nil { + fleetmode.SetAgentMode(false) + return nil + } + cfgObject, _ := cfgFlag.Value.(*config.SettingsFlag) + cliCfg := cfgObject.Config() + + err = cliCfg.Unpack(&managementSettings) + if err != nil { + fleetmode.SetAgentMode(false) + return nil //nolint:nilerr // unpacking failing isn't an error for this case + } + fleetmode.SetAgentMode(managementSettings.Enabled) return nil } @@ -220,8 +283,11 @@ func SetConfigPath(path string) { *configPath = path } -// GetPathConfig returns ${path.config}. If ${path.config} is not set, ${path.home} is returned. +// GetPathConfig returns ${path.config}. If ${path.config} is not set, +// ${path.home} is returned. It will call Initialize to ensure that +// `path.config` and `path.home` are set. func GetPathConfig() string { + Initialize() if *configPath != "" { return *configPath } else if *homePath != "" { diff --git a/libbeat/cmd/export/dashboard.go b/libbeat/cmd/export/dashboard.go index 7b878e00788b..4a4e13167f8c 100644 --- a/libbeat/cmd/export/dashboard.go +++ b/libbeat/cmd/export/dashboard.go @@ -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" @@ -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 } diff --git a/libbeat/cmd/export/ilm_policy.go b/libbeat/cmd/export/ilm_policy.go index 60c97920fd79..557c62c8aaef 100644 --- a/libbeat/cmd/export/ilm_policy.go +++ b/libbeat/cmd/export/ilm_policy.go @@ -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" @@ -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 } diff --git a/libbeat/cmd/export/index_pattern.go b/libbeat/cmd/export/index_pattern.go index 6b8b8c6839f6..863fc7a72cfe 100644 --- a/libbeat/cmd/export/index_pattern.go +++ b/libbeat/cmd/export/index_pattern.go @@ -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" @@ -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 } diff --git a/libbeat/cmd/export/template.go b/libbeat/cmd/export/template.go index ffd957961efe..45a83b986817 100644 --- a/libbeat/cmd/export/template.go +++ b/libbeat/cmd/export/template.go @@ -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" @@ -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 } diff --git a/libbeat/cmd/instance/beat.go b/libbeat/cmd/instance/beat.go index 1a6250fad4d5..23efa03b4897 100644 --- a/libbeat/cmd/instance/beat.go +++ b/libbeat/cmd/instance/beat.go @@ -974,9 +974,11 @@ func (b *Beat) Setup(settings Settings, bt beat.Creator, setup SetupSettings) er }()) } -// handleFlags parses the command line flags. It invokes the HandleFlags -// callback if implemented by the Beat. +// handleFlags converts -flag to --flags, parses the command line +// flags, and it invokes the HandleFlags callback if implemented by +// the Beat. func (b *Beat) handleFlags() error { + cfgfile.ConvertFlagsForBackwardsCompatibility() flag.Parse() return cfgfile.HandleFlags() } diff --git a/libbeat/cmd/instance/beat_integration_test.go b/libbeat/cmd/instance/beat_integration_test.go index baf7657665d2..1bca1400de61 100644 --- a/libbeat/cmd/instance/beat_integration_test.go +++ b/libbeat/cmd/instance/beat_integration_test.go @@ -27,6 +27,7 @@ import ( "time" "github.com/elastic/beats/v7/libbeat/beat" + "github.com/elastic/beats/v7/libbeat/cfgfile" "github.com/elastic/beats/v7/libbeat/cmd/instance" "github.com/elastic/beats/v7/libbeat/mock" "github.com/elastic/elastic-agent-libs/config" @@ -92,7 +93,9 @@ func TestMonitoringNameFromConfig(t *testing.T) { defer wg.Done() // Set the configuration file path flag so the beat can read it + cfgfile.Initialize() _ = flag.Set("c", "testdata/mockbeat.yml") + cfgfile.AddAllowedBackwardsCompatibleFlag("c") _ = instance.Run(mock.Settings, func(_ *beat.Beat, _ *config.C) (beat.Beater, error) { return &mockBeat, nil }) diff --git a/libbeat/cmd/keystore.go b/libbeat/cmd/keystore.go index 7f5ecd78c41b..cfc392020693 100644 --- a/libbeat/cmd/keystore.go +++ b/libbeat/cmd/keystore.go @@ -21,14 +21,15 @@ import ( "bufio" "errors" "fmt" - "io/ioutil" + "io" "os" "strings" "syscall" "github.com/spf13/cobra" - tml "golang.org/x/crypto/ssh/terminal" + "golang.org/x/term" + "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" @@ -38,7 +39,7 @@ import ( func getKeystore(settings instance.Settings) (keystore.Keystore, error) { b, err := instance.NewInitializedBeat(settings) if err != nil { - return nil, fmt.Errorf("error initializing beat: %s", err) + return nil, fmt.Errorf("error initializing beat: %w", err) } return b.Keystore(), nil @@ -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 } @@ -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 } @@ -132,27 +136,27 @@ func createKeystore(settings instance.Settings, force bool) error { writableKeystore, err := keystore.AsWritableKeystore(store) if err != nil { - return fmt.Errorf("error creating the keystore: %s", err) + return fmt.Errorf("error creating the keystore: %w", err) } - if store.IsPersisted() == true && force == false { + if store.IsPersisted() && !force { response := terminal.PromptYesNo("A keystore already exists, Overwrite?", false) - if response == true { + if response { err := writableKeystore.Create(true) if err != nil { - return fmt.Errorf("error creating the keystore: %s", err) + return fmt.Errorf("error creating the keystore: %w", err) } } else { - fmt.Println("Exiting without creating keystore.") + fmt.Printf("Exiting without creating %s keystore.", settings.Name) //nolint:forbidigo //needs refactor return nil } } else { err := writableKeystore.Create(true) if err != nil { - return fmt.Errorf("Error creating the keystore: %s", err) + return fmt.Errorf("Error creating the keystore: %w", err) } } - fmt.Printf("Created %s keystore\n", settings.Name) + fmt.Printf("Created %s keystore\n", settings.Name) //nolint:forbidigo //needs refactor return nil } @@ -167,32 +171,32 @@ func addKey(store keystore.Keystore, keys []string, force, stdin bool) error { writableKeystore, err := keystore.AsWritableKeystore(store) if err != nil { - return fmt.Errorf("error creating the keystore: %s", err) + return fmt.Errorf("error creating the keystore: %w", err) } - if store.IsPersisted() == false { - if force == false { + if !store.IsPersisted() { + if !force { answer := terminal.PromptYesNo("The keystore does not exist. Do you want to create it?", false) - if answer == false { + if !answer { return errors.New("exiting without creating keystore") } } err := writableKeystore.Create(true) if err != nil { - return fmt.Errorf("could not create keystore, error: %s", err) + return fmt.Errorf("could not create keystore, error: %w", err) } - fmt.Println("Created keystore") + fmt.Println("Created keystore") //nolint:forbidigo //needs refactor } key := strings.TrimSpace(keys[0]) - value, err := store.Retrieve(key) - if value != nil && force == false { - if stdin == true { + value, _ := store.Retrieve(key) + if value != nil && !force { + if stdin { return fmt.Errorf("the settings %s already exist in the keystore use `--force` to replace it", key) } answer := terminal.PromptYesNo(fmt.Sprintf("Setting %s already exists, Overwrite?", key), false) - if answer == false { - fmt.Println("Exiting without modifying keystore.") + if !answer { + fmt.Println("Exiting without modifying keystore.") //nolint:forbidigo //needs refactor return nil } } @@ -200,25 +204,25 @@ func addKey(store keystore.Keystore, keys []string, force, stdin bool) error { var keyValue []byte if stdin { reader := bufio.NewReader(os.Stdin) - keyValue, err = ioutil.ReadAll(reader) + keyValue, err = io.ReadAll(reader) if err != nil { return fmt.Errorf("could not read input from stdin") } } else { - fmt.Printf("Enter value for %s: ", key) - keyValue, err = tml.ReadPassword(int(syscall.Stdin)) - fmt.Println() + fmt.Printf("Enter value for %s: ", key) //nolint:forbidigo //needs refactor + keyValue, err = term.ReadPassword(int(syscall.Stdin)) //nolint:unconvert,nolintlint //necessary on Windows + fmt.Println() //nolint:forbidigo //needs refactor if err != nil { - return fmt.Errorf("could not read value from the input, error: %s", err) + return fmt.Errorf("could not read value from the input, error: %w", err) } } if err = writableKeystore.Store(key, keyValue); err != nil { - return fmt.Errorf("could not add the key in the keystore, error: %s", err) + return fmt.Errorf("could not add the key in the keystore, error: %w", err) } if err = writableKeystore.Save(); err != nil { - return fmt.Errorf("fail to save the keystore: %s", err) + return fmt.Errorf("fail to save the keystore: %w", err) } else { - fmt.Println("Successfully updated the keystore") + fmt.Println("Successfully updated the keystore") //nolint:forbidigo //needs refactor } return nil } @@ -230,10 +234,10 @@ func removeKey(store keystore.Keystore, keys []string) error { writableKeystore, err := keystore.AsWritableKeystore(store) if err != nil { - return fmt.Errorf("error deleting the keystore: %s", err) + return fmt.Errorf("error deleting the keystore: %w", err) } - if store.IsPersisted() == false { + if !store.IsPersisted() { return errors.New("the keystore doesn't exist. Use the 'create' command to create one") } @@ -244,12 +248,12 @@ func removeKey(store keystore.Keystore, keys []string) error { return fmt.Errorf("could not find key '%v' in the keystore", key) } - writableKeystore.Delete(key) + _ = writableKeystore.Delete(key) err = writableKeystore.Save() if err != nil { - return fmt.Errorf("could not update the keystore with the changes, key: %s, error: %v", key, err) + return fmt.Errorf("could not update the keystore with the changes, key: %s, error: %w", key, err) } - fmt.Printf("successfully removed key: %s\n", key) + fmt.Printf("successfully removed key: %s\n", key) //nolint:forbidigo //needs refactor } return nil } @@ -257,14 +261,14 @@ func removeKey(store keystore.Keystore, keys []string) error { func list(store keystore.Keystore) error { listingKeystore, err := keystore.AsListingKeystore(store) if err != nil { - return fmt.Errorf("error listing the keystore: %s", err) + return fmt.Errorf("error listing the keystore: %w", err) } keys, err := listingKeystore.List() if err != nil { - return fmt.Errorf("could not read values from the keystore, error: %s", err) + return fmt.Errorf("could not read values from the keystore, error: %w", err) } for _, key := range keys { - fmt.Println(key) + fmt.Println(key) //nolint:forbidigo //needs refactor } return nil } diff --git a/libbeat/cmd/root.go b/libbeat/cmd/root.go index 589d706fc615..cbe2f7f8f6e5 100644 --- a/libbeat/cmd/root.go +++ b/libbeat/cmd/root.go @@ -21,7 +21,6 @@ import ( "flag" "fmt" "os" - "strings" "github.com/spf13/cobra" @@ -33,15 +32,6 @@ import ( "github.com/elastic/beats/v7/libbeat/outputs/elasticsearch" ) -func init() { - // backwards compatibility workaround, convert -flags to --flags: - for i, arg := range os.Args[1:] { - if strings.HasPrefix(arg, "-") && !strings.HasPrefix(arg, "--") && len(arg) > 2 { - os.Args[1+i] = "-" + arg - } - } -} - // BeatsRootCmd handles all application command line interface, parses user // flags and runs subcommands type BeatsRootCmd struct { @@ -76,6 +66,7 @@ func GenRootCmdWithSettings(beatCreator beat.Creator, settings instance.Settings rootCmd.Use = settings.Name // Due to a dependence upon the beat name, the default config file path + cfgfile.Initialize() err := cfgfile.ChangeDefaultCfgfileFlag(settings.Name) if err != nil { panic(fmt.Errorf("failed to set default config file path: %w", err)) @@ -96,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 diff --git a/libbeat/cmd/run.go b/libbeat/cmd/run.go index b078aadaf89c..d6cadd318cee 100644 --- a/libbeat/cmd/run.go +++ b/libbeat/cmd/run.go @@ -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" ) @@ -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) diff --git a/libbeat/cmd/setup.go b/libbeat/cmd/setup.go index 64d1f41fdeaf..0b28d22f96f4 100644 --- a/libbeat/cmd/setup.go +++ b/libbeat/cmd/setup.go @@ -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" ) @@ -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 } diff --git a/libbeat/common/fleetmode/fleet_mode.go b/libbeat/common/fleetmode/fleet_mode.go index af179b887eac..97a17804f647 100644 --- a/libbeat/common/fleetmode/fleet_mode.go +++ b/libbeat/common/fleetmode/fleet_mode.go @@ -17,33 +17,18 @@ package fleetmode -import ( - "flag" - - "github.com/elastic/elastic-agent-libs/config" -) +var managementEnabled bool + +// SetAgentMode stores if the Beat is running under Elastic Agent. +// Normally this is called when the command line flags are parsed. +// This is stored as a package level variable because some components +// (like filebeat/metricbeat modules) don't have access to the +// configuration information to determine this on their own. +func SetAgentMode(enabled bool) { + managementEnabled = enabled +} -// Enabled checks to see if filebeat/metricbeat is running under Agent -// The management setting is stored in the main Beat runtime object, but we can't see that from a module -// So instead we check the CLI flags, since Agent starts filebeat/metricbeat with "-E", "management.enabled=true" +// Enabled returns true if the Beat is running under Elastic Agent. func Enabled() bool { - type management struct { - Enabled bool `config:"management.enabled"` - } - var managementSettings management - - cfgFlag := flag.Lookup("E") - if cfgFlag == nil { - return false - } - - cfgObject, _ := cfgFlag.Value.(*config.SettingsFlag) - cliCfg := cfgObject.Config() - - err := cliCfg.Unpack(&managementSettings) - if err != nil { - return false - } - - return managementSettings.Enabled + return managementEnabled } diff --git a/libbeat/libbeat_test.go b/libbeat/libbeat_test.go index e4bac5e309de..1f9e1b3ffe8b 100644 --- a/libbeat/libbeat_test.go +++ b/libbeat/libbeat_test.go @@ -21,6 +21,7 @@ import ( "flag" "testing" + "github.com/elastic/beats/v7/libbeat/cfgfile" "github.com/elastic/beats/v7/libbeat/tests/system/template" ) @@ -31,11 +32,14 @@ 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 func TestSystem(t *testing.T) { + cfgfile.ConvertFlagsForBackwardsCompatibility() if *systemTest { main() } diff --git a/metricbeat/cmd/root.go b/metricbeat/cmd/root.go index e3d308d2508c..497b71bed8ad 100644 --- a/metricbeat/cmd/root.go +++ b/metricbeat/cmd/root.go @@ -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" @@ -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, diff --git a/metricbeat/main_test.go b/metricbeat/main_test.go index 445db0353f2e..495ce5787e04 100644 --- a/metricbeat/main_test.go +++ b/metricbeat/main_test.go @@ -23,6 +23,7 @@ import ( "flag" "testing" + "github.com/elastic/beats/v7/libbeat/cfgfile" "github.com/elastic/beats/v7/libbeat/tests/system/template" "github.com/elastic/beats/v7/metricbeat/cmd" ) @@ -33,11 +34,14 @@ 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. func TestSystem(t *testing.T) { + cfgfile.ConvertFlagsForBackwardsCompatibility() if *systemTest { main() } diff --git a/packetbeat/cmd/root.go b/packetbeat/cmd/root.go index 7b1c20b34c6e..eb6d14b1692a 100644 --- a/packetbeat/cmd/root.go +++ b/packetbeat/cmd/root.go @@ -22,6 +22,7 @@ import ( "github.com/spf13/pflag" + "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" @@ -51,10 +52,15 @@ var RootCmd *cmd.BeatsRootCmd func PacketbeatSettings(globals processors.PluginConfig) instance.Settings { runFlags := pflag.NewFlagSet(Name, pflag.ExitOnError) runFlags.AddGoFlag(flag.CommandLine.Lookup("I")) + cfgfile.AddAllowedBackwardsCompatibleFlag("I") runFlags.AddGoFlag(flag.CommandLine.Lookup("t")) + cfgfile.AddAllowedBackwardsCompatibleFlag("t") runFlags.AddGoFlag(flag.CommandLine.Lookup("O")) + cfgfile.AddAllowedBackwardsCompatibleFlag("O") runFlags.AddGoFlag(flag.CommandLine.Lookup("l")) + cfgfile.AddAllowedBackwardsCompatibleFlag("l") runFlags.AddGoFlag(flag.CommandLine.Lookup("dump")) + cfgfile.AddAllowedBackwardsCompatibleFlag("dump") return instance.Settings{ RunFlags: runFlags, diff --git a/packetbeat/main_test.go b/packetbeat/main_test.go index 359e70f161b0..2dfcfb4572c5 100644 --- a/packetbeat/main_test.go +++ b/packetbeat/main_test.go @@ -23,6 +23,7 @@ import ( "flag" "testing" + "github.com/elastic/beats/v7/libbeat/cfgfile" "github.com/elastic/beats/v7/libbeat/tests/system/template" "github.com/elastic/beats/v7/packetbeat/cmd" ) @@ -33,11 +34,14 @@ 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. func TestSystem(*testing.T) { + cfgfile.ConvertFlagsForBackwardsCompatibility() if *systemTest { main() } diff --git a/winlogbeat/main_test.go b/winlogbeat/main_test.go index 0bbc2e4e5262..002c7431f99a 100644 --- a/winlogbeat/main_test.go +++ b/winlogbeat/main_test.go @@ -22,6 +22,7 @@ import ( "flag" "testing" + "github.com/elastic/beats/v7/libbeat/cfgfile" "github.com/elastic/beats/v7/libbeat/tests/system/template" "github.com/elastic/beats/v7/winlogbeat/cmd" ) @@ -32,12 +33,15 @@ 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") } // TestSystem is the function called when the test binary is started. // Only calls main. func TestSystem(*testing.T) { + cfgfile.ConvertFlagsForBackwardsCompatibility() if *systemTest { main() } diff --git a/x-pack/agentbeat/main_test.go b/x-pack/agentbeat/main_test.go index 4201d651666e..0333a1ebcdf4 100644 --- a/x-pack/agentbeat/main_test.go +++ b/x-pack/agentbeat/main_test.go @@ -10,6 +10,8 @@ import ( "testing" "github.com/spf13/cobra" + + "github.com/elastic/beats/v7/libbeat/cfgfile" ) var ( @@ -22,11 +24,14 @@ func init() { systemTest = flag.Bool("systemTest", false, "Set to true when running system tests") abCommand = AgentBeat() abCommand.PersistentFlags().AddGoFlag(flag.CommandLine.Lookup("systemTest")) + cfgfile.AddAllowedBackwardsCompatibleFlag("systemTest") abCommand.PersistentFlags().AddGoFlag(flag.CommandLine.Lookup("test.coverprofile")) + cfgfile.AddAllowedBackwardsCompatibleFlag("test.coverprofile") } // Test started when the test binary is started. Only calls main. func TestSystem(t *testing.T) { + cfgfile.ConvertFlagsForBackwardsCompatibility() if *systemTest { if err := abCommand.Execute(); err != nil { os.Exit(1) diff --git a/x-pack/auditbeat/main_test.go b/x-pack/auditbeat/main_test.go index 5d2376452712..3456231f5167 100644 --- a/x-pack/auditbeat/main_test.go +++ b/x-pack/auditbeat/main_test.go @@ -10,6 +10,7 @@ import ( "flag" "testing" + "github.com/elastic/beats/v7/libbeat/cfgfile" "github.com/elastic/beats/v7/libbeat/tests/system/template" "github.com/elastic/beats/v7/x-pack/auditbeat/cmd" ) @@ -21,11 +22,14 @@ 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. func TestSystem(*testing.T) { + cfgfile.ConvertFlagsForBackwardsCompatibility() if *systemTest { main() } diff --git a/x-pack/filebeat/main_test.go b/x-pack/filebeat/main_test.go index 3d5424c30884..71b32b9e6e86 100644 --- a/x-pack/filebeat/main_test.go +++ b/x-pack/filebeat/main_test.go @@ -9,6 +9,7 @@ import ( "os" "testing" + "github.com/elastic/beats/v7/libbeat/cfgfile" cmd "github.com/elastic/beats/v7/libbeat/cmd" "github.com/elastic/beats/v7/libbeat/tests/system/template" fbcmd "github.com/elastic/beats/v7/x-pack/filebeat/cmd" @@ -24,11 +25,14 @@ func init() { systemTest = flag.Bool("systemTest", false, "Set to true when running system tests") fbCommand = fbcmd.Filebeat() 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. func TestSystem(t *testing.T) { + cfgfile.ConvertFlagsForBackwardsCompatibility() if *systemTest { if err := fbCommand.Execute(); err != nil { os.Exit(1) diff --git a/x-pack/functionbeat/main_test.go b/x-pack/functionbeat/main_test.go index ecb5ac124355..1c21f88e2891 100644 --- a/x-pack/functionbeat/main_test.go +++ b/x-pack/functionbeat/main_test.go @@ -10,6 +10,7 @@ import ( "flag" "testing" + "github.com/elastic/beats/v7/libbeat/cfgfile" "github.com/elastic/beats/v7/libbeat/tests/system/template" "github.com/elastic/beats/v7/x-pack/functionbeat/manager/cmd" ) @@ -21,11 +22,14 @@ 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. func TestSystem(t *testing.T) { + cfgfile.ConvertFlagsForBackwardsCompatibility() if *systemTest { main() } diff --git a/x-pack/functionbeat/provider/aws/cmd/root.go b/x-pack/functionbeat/provider/aws/cmd/root.go index fc7a7e0c2515..c8e7ba17d62b 100644 --- a/x-pack/functionbeat/provider/aws/cmd/root.go +++ b/x-pack/functionbeat/provider/aws/cmd/root.go @@ -7,6 +7,7 @@ package cmd import ( "flag" + "github.com/elastic/beats/v7/libbeat/cfgfile" "github.com/elastic/beats/v7/x-pack/functionbeat/function/beater" funcmd "github.com/elastic/beats/v7/x-pack/functionbeat/function/cmd" ) @@ -20,6 +21,9 @@ var RootCmd *funcmd.FunctionCmd func init() { RootCmd = funcmd.NewFunctionCmd(Name, beater.New) 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") } diff --git a/x-pack/functionbeat/provider/aws/main_test.go b/x-pack/functionbeat/provider/aws/main_test.go index dad745420cb7..f180fd28275d 100644 --- a/x-pack/functionbeat/provider/aws/main_test.go +++ b/x-pack/functionbeat/provider/aws/main_test.go @@ -10,6 +10,7 @@ import ( "flag" "testing" + "github.com/elastic/beats/v7/libbeat/cfgfile" "github.com/elastic/beats/v7/x-pack/functionbeat/provider/aws/cmd" ) @@ -20,12 +21,14 @@ 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. func TestSystem(t *testing.T) { - + cfgfile.ConvertFlagsForBackwardsCompatibility() if *systemTest { main() } diff --git a/x-pack/functionbeat/provider/local/main_test.go b/x-pack/functionbeat/provider/local/main_test.go index cc941b40c020..7c617b300a65 100644 --- a/x-pack/functionbeat/provider/local/main_test.go +++ b/x-pack/functionbeat/provider/local/main_test.go @@ -10,6 +10,7 @@ import ( "flag" "testing" + "github.com/elastic/beats/v7/libbeat/cfgfile" "github.com/elastic/beats/v7/x-pack/functionbeat/provider/local/cmd" ) @@ -20,11 +21,14 @@ 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. func TestSystem(t *testing.T) { + cfgfile.ConvertFlagsForBackwardsCompatibility() if *systemTest { main() } diff --git a/x-pack/heartbeat/main_test.go b/x-pack/heartbeat/main_test.go index 91fe8b60ad8c..44d5882a9905 100644 --- a/x-pack/heartbeat/main_test.go +++ b/x-pack/heartbeat/main_test.go @@ -10,6 +10,7 @@ import ( "testing" "github.com/elastic/beats/v7/heartbeat/cmd" + "github.com/elastic/beats/v7/libbeat/cfgfile" "github.com/elastic/beats/v7/libbeat/tests/system/template" ) @@ -19,11 +20,14 @@ 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. func TestSystem(_ *testing.T) { + cfgfile.ConvertFlagsForBackwardsCompatibility() if *systemTest { main() } diff --git a/x-pack/libbeat/libbeat_test.go b/x-pack/libbeat/libbeat_test.go index 338ebd7e5fbd..a3df546f3f38 100644 --- a/x-pack/libbeat/libbeat_test.go +++ b/x-pack/libbeat/libbeat_test.go @@ -8,6 +8,7 @@ import ( "flag" "testing" + "github.com/elastic/beats/v7/libbeat/cfgfile" "github.com/elastic/beats/v7/libbeat/tests/system/template" ) @@ -18,11 +19,14 @@ 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 func TestSystem(t *testing.T) { + cfgfile.ConvertFlagsForBackwardsCompatibility() if *systemTest { main() } diff --git a/x-pack/metricbeat/main_test.go b/x-pack/metricbeat/main_test.go index b092682ccf34..e96a9932765c 100644 --- a/x-pack/metricbeat/main_test.go +++ b/x-pack/metricbeat/main_test.go @@ -8,6 +8,7 @@ import ( "flag" "testing" + "github.com/elastic/beats/v7/libbeat/cfgfile" "github.com/elastic/beats/v7/libbeat/tests/system/template" "github.com/elastic/beats/v7/x-pack/metricbeat/cmd" ) @@ -18,11 +19,14 @@ 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. func TestSystem(t *testing.T) { + cfgfile.ConvertFlagsForBackwardsCompatibility() if *systemTest { main() } diff --git a/x-pack/osquerybeat/main_test.go b/x-pack/osquerybeat/main_test.go index f9ed09ee1a8e..30a9b88efb64 100644 --- a/x-pack/osquerybeat/main_test.go +++ b/x-pack/osquerybeat/main_test.go @@ -10,6 +10,7 @@ import ( "flag" "testing" + "github.com/elastic/beats/v7/libbeat/cfgfile" "github.com/elastic/beats/v7/x-pack/osquerybeat/cmd" ) @@ -20,11 +21,14 @@ 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. func TestSystem(_ *testing.T) { + cfgfile.ConvertFlagsForBackwardsCompatibility() if *systemTest { main() } diff --git a/x-pack/packetbeat/main_test.go b/x-pack/packetbeat/main_test.go index 234d68f81698..a332f5c99353 100644 --- a/x-pack/packetbeat/main_test.go +++ b/x-pack/packetbeat/main_test.go @@ -8,6 +8,7 @@ import ( "flag" "testing" + "github.com/elastic/beats/v7/libbeat/cfgfile" "github.com/elastic/beats/v7/libbeat/tests/system/template" "github.com/elastic/beats/v7/x-pack/packetbeat/cmd" ) @@ -18,11 +19,14 @@ 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. func TestSystem(t *testing.T) { + cfgfile.ConvertFlagsForBackwardsCompatibility() if *systemTest { main() } diff --git a/x-pack/winlogbeat/cmd/export.go b/x-pack/winlogbeat/cmd/export.go index 54f9f02e18c8..facd2fb0e929 100644 --- a/x-pack/winlogbeat/cmd/export.go +++ b/x-pack/winlogbeat/cmd/export.go @@ -10,6 +10,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/winlogbeat/module" libversion "github.com/elastic/elastic-agent-libs/version" @@ -48,7 +49,9 @@ func GenExportPipelineCmd(settings instance.Settings) *cobra.Command { } genExportPipelineCmd.Flags().String("es.version", settings.Version, "Elasticsearch version (required)") + cfgfile.AddAllowedBackwardsCompatibleFlag("es.version") genExportPipelineCmd.Flags().String("dir", "", "Specify directory for exporting pipelines. Default is current directory.") + cfgfile.AddAllowedBackwardsCompatibleFlag("dir") return genExportPipelineCmd } diff --git a/x-pack/winlogbeat/main_test.go b/x-pack/winlogbeat/main_test.go index 2b8547a81726..b4c0f3b124a1 100644 --- a/x-pack/winlogbeat/main_test.go +++ b/x-pack/winlogbeat/main_test.go @@ -8,6 +8,7 @@ import ( "flag" "testing" + "github.com/elastic/beats/v7/libbeat/cfgfile" "github.com/elastic/beats/v7/libbeat/tests/system/template" "github.com/elastic/beats/v7/x-pack/winlogbeat/cmd" ) @@ -18,11 +19,14 @@ 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. func TestSystem(*testing.T) { + cfgfile.ConvertFlagsForBackwardsCompatibility() if *systemTest { main() }