-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Heartbeat: add support for pushed monitor source (#31428)
* Heartbeat: add support for pushed monitors * link to global synthetics pkg * add tests and link global path * fix lint issues * fix tests * add changelog * use install instead of link * handle zipslip vul
- Loading branch information
1 parent
ad92b20
commit 2696fb2
Showing
5 changed files
with
283 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
// or more contributor license agreements. Licensed under the Elastic License; | ||
// you may not use this file except in compliance with the Elastic License. | ||
|
||
package source | ||
|
||
import ( | ||
"bytes" | ||
"encoding/base64" | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"io/ioutil" | ||
"os" | ||
"os/exec" | ||
"path/filepath" | ||
"regexp" | ||
"strings" | ||
|
||
"github.com/elastic/elastic-agent-libs/mapstr" | ||
) | ||
|
||
type PushedSource struct { | ||
Content string `config:"content" json:"content"` | ||
TargetDirectory string | ||
} | ||
|
||
var ErrNoContent = fmt.Errorf("no 'content' value specified for pushed monitor source") | ||
|
||
func (p *PushedSource) Validate() error { | ||
if !regexp.MustCompile(`\S`).MatchString(p.Content) { | ||
return ErrNoContent | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (p *PushedSource) Fetch() error { | ||
decodedBytes, err := base64.StdEncoding.DecodeString(p.Content) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
tf, err := ioutil.TempFile("/tmp", "elastic-synthetics-zip-") | ||
if err != nil { | ||
return fmt.Errorf("could not create tmpfile for pushed monitor source: %w", err) | ||
} | ||
defer os.Remove(tf.Name()) | ||
|
||
// copy the encoded contents in to a temp file for unzipping later | ||
_, err = io.Copy(tf, bytes.NewReader(decodedBytes)) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
p.TargetDirectory, err = ioutil.TempDir("/tmp", "elastic-synthetics-unzip-") | ||
if err != nil { | ||
return fmt.Errorf("could not make temp dir for unzipping pushed source: %w", err) | ||
} | ||
|
||
err = unzip(tf, p.Workdir(), "") | ||
if err != nil { | ||
p.Close() | ||
return err | ||
} | ||
|
||
// Offline is not required for pushed resources as we are only linking | ||
// to the globally installed agent, but useful for testing purposes | ||
if !Offline() { | ||
// set up npm project and ensure synthetics is installed | ||
err = setupProjectDir(p.Workdir()) | ||
if err != nil { | ||
return fmt.Errorf("setting up project dir failed: %w", err) | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
type PackageJSON struct { | ||
Name string `json:"name"` | ||
Private bool `json:"private"` | ||
Dependencies mapstr.M `json:"dependencies"` | ||
} | ||
|
||
// setupProjectDir sets ups the required package.json file and | ||
// links the synthetics dependency to the globally installed one that is | ||
// baked in to the Heartbeat image to maintain compatibility and | ||
// allows us to control the synthetics agent version | ||
func setupProjectDir(workdir string) error { | ||
fname, err := exec.LookPath("elastic-synthetics") | ||
if err == nil { | ||
fname, err = filepath.Abs(fname) | ||
} | ||
if err != nil { | ||
return fmt.Errorf("cannot resolve global synthetics library: %w", err) | ||
} | ||
|
||
globalPath := strings.Replace(fname, "bin/elastic-synthetics", "lib/node_modules/@elastic/synthetics", 1) | ||
symlinkPath := fmt.Sprintf("file:%s", globalPath) | ||
pkgJson := PackageJSON{ | ||
Name: "pushed-journey", | ||
Private: true, | ||
Dependencies: mapstr.M{ | ||
"@elastic/synthetics": symlinkPath, | ||
}, | ||
} | ||
pkgJsonContent, err := json.MarshalIndent(pkgJson, "", " ") | ||
if err != nil { | ||
return err | ||
} | ||
//nolint:gosec //for permission | ||
err = ioutil.WriteFile(filepath.Join(workdir, "package.json"), pkgJsonContent, 0755) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// setup the project linking to the global synthetics library | ||
return runSimpleCommand(exec.Command("npm", "install"), workdir) | ||
} | ||
|
||
func (p *PushedSource) Workdir() string { | ||
return p.TargetDirectory | ||
} | ||
|
||
func (p *PushedSource) Close() error { | ||
if p.TargetDirectory != "" { | ||
return os.RemoveAll(p.TargetDirectory) | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
// or more contributor license agreements. Licensed under the Elastic License; | ||
// you may not use this file except in compliance with the Elastic License. | ||
|
||
package source | ||
|
||
import ( | ||
"os" | ||
"path" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"gopkg.in/yaml.v2" | ||
|
||
"github.com/elastic/elastic-agent-libs/config" | ||
"github.com/elastic/elastic-agent-libs/mapstr" | ||
) | ||
|
||
func setUpTests() func() { | ||
GoOffline() | ||
return func() { | ||
GoOnline() | ||
} | ||
} | ||
|
||
func TestPushedSource(t *testing.T) { | ||
teardown := setUpTests() | ||
defer teardown() | ||
|
||
type testCase struct { | ||
name string | ||
cfg mapstr.M | ||
wantErr bool | ||
} | ||
testCases := []testCase{ | ||
{ | ||
"decode pushed content", | ||
mapstr.M{ | ||
"content": "UEsDBBQACAAIAJ27qVQAAAAAAAAAAAAAAAAiAAAAZXhhbXBsZXMvdG9kb3MvYWR2YW5jZWQuam91cm5leS50c5VRPW/CMBDd+RWnLA0Sigt0KqJqpbZTN+iEGKzkIC6JbfkuiBTx3+uEEAGlgi7Rnf38viIESCLkR/FJ6Eis1VIjpanATBKrWFCpOUU/kcCNzG2GJNgkhoRM1lLHmERfpnAay4ipo3JrHMMWmjPYwcKZ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6HF3h5LleimS7GkoynWVL5+KWS6h/AFBLBwgvDHpj+wEAAC8FAABQSwECLQMUAAgACACdu6lUVYIEHGwBAADRAwAAIgAAAAAAAAAAACAApIEAAAAAZXhhbXBsZXMvdG9kb3MvYWR2YW5jZWQuam91cm5leS50c1BLAQItAxQACAAIAJ27qVQvDHpj+wEAAC8FAAAZAAAAAAAAAAAAIACkgbwBAABleGFtcGxlcy90b2Rvcy9oZWxwZXJzLnRzUEsFBgAAAAACAAIAlwAAAP4DAAAAAA==", | ||
}, | ||
false, | ||
}, | ||
{ | ||
"bad encoded content", | ||
mapstr.M{ | ||
"content": "12312edasd", | ||
}, | ||
true, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
psrc, err := dummyPSource(tc.cfg) | ||
if tc.wantErr { | ||
err = psrc.Fetch() | ||
require.Error(t, err) | ||
return | ||
} | ||
require.NoError(t, err) | ||
fetchAndValidate(t, psrc) | ||
}) | ||
} | ||
} | ||
|
||
func validateFileContents(t *testing.T, dir string) { | ||
expected := []string{ | ||
"examples/todos/helpers.ts", | ||
"examples/todos/advanced.journey.ts", | ||
} | ||
for _, file := range expected { | ||
_, err := os.Stat(path.Join(dir, file)) | ||
assert.NoError(t, err) | ||
} | ||
} | ||
|
||
func fetchAndValidate(t *testing.T, psrc *PushedSource) { | ||
err := psrc.Fetch() | ||
require.NoError(t, err) | ||
|
||
validateFileContents(t, psrc.Workdir()) | ||
// check if the working directory is deleted | ||
require.NoError(t, psrc.Close()) | ||
_, err = os.Stat(psrc.TargetDirectory) | ||
require.True(t, os.IsNotExist(err), "TargetDirectory %s should have been deleted", psrc.TargetDirectory) | ||
} | ||
|
||
func dummyPSource(conf map[string]interface{}) (*PushedSource, error) { | ||
psrc := &PushedSource{} | ||
y, _ := yaml.Marshal(conf) | ||
c, err := config.NewConfigWithYAML(y, string(y)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
err = c.Unpack(psrc) | ||
return psrc, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters