From 0471d4e4c8fdef779aad75f2ffe3eb33c6813028 Mon Sep 17 00:00:00 2001 From: Andrew Cholakian Date: Mon, 22 Feb 2021 17:27:39 -0600 Subject: [PATCH 1/4] Add sandbox options, defaulted to off --- x-pack/heartbeat/monitors/browser/browser.go | 11 +++++++++-- x-pack/heartbeat/monitors/browser/config.go | 9 ++++++++- x-pack/heartbeat/monitors/browser/suite_runner.go | 2 +- .../heartbeat/monitors/browser/synthexec/synthexec.go | 8 ++++---- 4 files changed, 22 insertions(+), 8 deletions(-) diff --git a/x-pack/heartbeat/monitors/browser/browser.go b/x-pack/heartbeat/monitors/browser/browser.go index 76e02f1ff28..04e0f82fafd 100644 --- a/x-pack/heartbeat/monitors/browser/browser.go +++ b/x-pack/heartbeat/monitors/browser/browser.go @@ -52,16 +52,23 @@ func create(name string, cfg *common.Config) (p plugin.Plugin, err error) { return plugin.Plugin{}, err } + extraArgs := []string{} + if ss.suiteCfg.Sandbox { + extraArgs = append(extraArgs, "--sandbox") + } else { + extraArgs = append(extraArgs, "--no-sandbox") + } + var j jobs.Job if src, ok := ss.InlineSource(); ok { - j = synthexec.InlineJourneyJob(context.TODO(), src, ss.Params()) + j = synthexec.InlineJourneyJob(context.TODO(), src, ss.Params(), extraArgs) } else { j = func(event *beat.Event) ([]jobs.Job, error) { err := ss.Fetch() if err != nil { return nil, fmt.Errorf("could not fetch for suite job: %w", err) } - sj, err := synthexec.SuiteJob(context.TODO(), ss.Workdir(), ss.Params()) + sj, err := synthexec.SuiteJob(context.TODO(), ss.Workdir(), ss.Params(), extraArgs) if err != nil { return nil, err } diff --git a/x-pack/heartbeat/monitors/browser/config.go b/x-pack/heartbeat/monitors/browser/config.go index 0cbb699da88..1615db1a331 100644 --- a/x-pack/heartbeat/monitors/browser/config.go +++ b/x-pack/heartbeat/monitors/browser/config.go @@ -11,6 +11,12 @@ import ( "github.com/elastic/beats/v7/x-pack/heartbeat/monitors/browser/source" ) +func DefaultConfig() *Config { + return &Config{ + Sandbox: false, + } +} + type Config struct { Schedule string `config:"schedule"` Params map[string]interface{} `config:"params"` @@ -19,7 +25,8 @@ type Config struct { // Name is optional for lightweight checks but required for browsers Name string `config:"name"` // Id is optional for lightweight checks but required for browsers - Id string `config:"id"` + Id string `config:"id"` + Sandbox bool `config:"sandbox"` } var ErrNameRequired = fmt.Errorf("config 'name' must be specified for this monitor") diff --git a/x-pack/heartbeat/monitors/browser/suite_runner.go b/x-pack/heartbeat/monitors/browser/suite_runner.go index 73b597766ff..6e58ae932f1 100644 --- a/x-pack/heartbeat/monitors/browser/suite_runner.go +++ b/x-pack/heartbeat/monitors/browser/suite_runner.go @@ -23,7 +23,7 @@ type SyntheticSuite struct { func NewSuite(rawCfg *common.Config) (*SyntheticSuite, error) { ss := &SyntheticSuite{ rawCfg: rawCfg, - suiteCfg: &Config{}, + suiteCfg: DefaultConfig(), } err := rawCfg.Unpack(ss.suiteCfg) if err != nil { diff --git a/x-pack/heartbeat/monitors/browser/synthexec/synthexec.go b/x-pack/heartbeat/monitors/browser/synthexec/synthexec.go index 59a44bbe59a..da67e917f1c 100644 --- a/x-pack/heartbeat/monitors/browser/synthexec/synthexec.go +++ b/x-pack/heartbeat/monitors/browser/synthexec/synthexec.go @@ -27,10 +27,10 @@ import ( const debugSelector = "synthexec" // SuiteJob will run a single journey by name from the given suite. -func SuiteJob(ctx context.Context, suitePath string, params common.MapStr) (jobs.Job, error) { +func SuiteJob(ctx context.Context, suitePath string, params common.MapStr, extraArgs ...string) (jobs.Job, error) { // Run the command in the given suitePath, use '.' as the first arg since the command runs // in the correct dir - newCmd, err := suiteCommandFactory(suitePath, ".", "--screenshots") + newCmd, err := suiteCommandFactory(suitePath, append(extraArgs, ".", "--screenshots")...) if err != nil { return nil, err } @@ -55,9 +55,9 @@ func suiteCommandFactory(suitePath string, args ...string) (func() *exec.Cmd, er } // InlineJourneyJob returns a job that runs the given source as a single journey. -func InlineJourneyJob(ctx context.Context, script string, params common.MapStr) jobs.Job { +func InlineJourneyJob(ctx context.Context, script string, params common.MapStr, extraArgs ...string) jobs.Job { newCmd := func() *exec.Cmd { - return exec.Command("elastic-synthetics", "--inline", "--screenshots") + return exec.Command("elastic-synthetics", append(extraArgs, "--inline", "--screenshots")...) } return startCmdJob(ctx, newCmd, &script, params) From b0a512595aa29cb4ce6c7beae1cdf6a257db0092 Mon Sep 17 00:00:00 2001 From: Andrew Cholakian Date: Tue, 23 Feb 2021 16:40:31 -0600 Subject: [PATCH 2/4] Only turn sandbox on --- x-pack/heartbeat/monitors/browser/browser.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/x-pack/heartbeat/monitors/browser/browser.go b/x-pack/heartbeat/monitors/browser/browser.go index 04e0f82fafd..ad0f4d60640 100644 --- a/x-pack/heartbeat/monitors/browser/browser.go +++ b/x-pack/heartbeat/monitors/browser/browser.go @@ -55,8 +55,6 @@ func create(name string, cfg *common.Config) (p plugin.Plugin, err error) { extraArgs := []string{} if ss.suiteCfg.Sandbox { extraArgs = append(extraArgs, "--sandbox") - } else { - extraArgs = append(extraArgs, "--no-sandbox") } var j jobs.Job From cc9a41dbec1968cc3cc570891ada4a8dec7a3f26 Mon Sep 17 00:00:00 2001 From: Andrew Cholakian Date: Tue, 2 Mar 2021 17:50:46 -0600 Subject: [PATCH 3/4] Changelog --- CHANGELOG.next.asciidoc | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.next.asciidoc b/CHANGELOG.next.asciidoc index c5dfe0cac9c..faa696fb020 100644 --- a/CHANGELOG.next.asciidoc +++ b/CHANGELOG.next.asciidoc @@ -872,6 +872,7 @@ https://github.com/elastic/beats/compare/v7.0.0-alpha2...master[Check the HEAD d - Add mime type detection for http responses. {pull}22976[22976] - Bundle synthetics deps with heartbeat docker image. {pull}23274[23274] +- Add --sandbox option for browser monitor. {pull}24172[24172] *Journalbeat* From 069abbea9cd9cacb89251d1e6a78d98457d0f5cb Mon Sep 17 00:00:00 2001 From: Andrew Cholakian Date: Wed, 3 Mar 2021 08:17:44 -0600 Subject: [PATCH 4/4] Fix vararg --- x-pack/heartbeat/monitors/browser/browser.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/x-pack/heartbeat/monitors/browser/browser.go b/x-pack/heartbeat/monitors/browser/browser.go index ad0f4d60640..def6e0f4915 100644 --- a/x-pack/heartbeat/monitors/browser/browser.go +++ b/x-pack/heartbeat/monitors/browser/browser.go @@ -59,14 +59,14 @@ func create(name string, cfg *common.Config) (p plugin.Plugin, err error) { var j jobs.Job if src, ok := ss.InlineSource(); ok { - j = synthexec.InlineJourneyJob(context.TODO(), src, ss.Params(), extraArgs) + j = synthexec.InlineJourneyJob(context.TODO(), src, ss.Params(), extraArgs...) } else { j = func(event *beat.Event) ([]jobs.Job, error) { err := ss.Fetch() if err != nil { return nil, fmt.Errorf("could not fetch for suite job: %w", err) } - sj, err := synthexec.SuiteJob(context.TODO(), ss.Workdir(), ss.Params(), extraArgs) + sj, err := synthexec.SuiteJob(context.TODO(), ss.Workdir(), ss.Params(), extraArgs...) if err != nil { return nil, err }