Skip to content

The cross-harness, self-improving memory layer for AI agents. Join our community: https://discord.com/invite/zdNChS2fBu

fix: replace panic() with error returns in pack asset loading

Clauded492aee33eb9
diff --git a/cli/beacon/cmd/endpoint.go b/cli/beacon/cmd/endpoint.go
index aa9bf8a6..257b2b75 100644
--- a/cli/beacon/cmd/endpoint.go
+++ b/cli/beacon/cmd/endpoint.go
@@ -408,11 +408,17 @@ var endpointWazuhValidateCmd = &cobra.Command{
}
var endpointElasticPrintConfigCmd = &cobra.Command{
- Use: "print-config",
- Short: "Print a Filebeat input for Beacon endpoint events",
- Run: func(cmd *cobra.Command, args []string) {
+ Use: "print-config",
+ Short: "Print a Filebeat input for Beacon endpoint events",
+ SilenceUsage: true,
+ RunE: func(cmd *cobra.Command, args []string) error {
cfg := loadOrDefaultConfig()
- fmt.Print(elastic.InputSnippet(cfg.LogPath))
+ snippet, err := elastic.InputSnippet(cfg.LogPath)
+ if err != nil {
+ return err
+ }
+ fmt.Print(snippet)
+ return nil
},
}
@@ -442,11 +448,17 @@ var endpointElasticDownCmd = &cobra.Command{
}
var endpointDatadogPrintConfigCmd = &cobra.Command{
- Use: "print-config",
- Short: "Print a Datadog Agent custom log config for Beacon endpoint events",
- Run: func(cmd *cobra.Command, args []string) {
+ Use: "print-config",
+ Short: "Print a Datadog Agent custom log config for Beacon endpoint events",
+ SilenceUsage: true,
+ RunE: func(cmd *cobra.Command, args []string) error {
cfg := loadOrDefaultConfig()
- fmt.Print(datadog.ConfigSnippet(cfg.LogPath))
+ snippet, err := datadog.ConfigSnippet(cfg.LogPath)
+ if err != nil {
+ return err
+ }
+ fmt.Print(snippet)
+ return nil
},
}
@@ -465,11 +477,17 @@ var endpointDatadogValidateCmd = &cobra.Command{
}
var endpointSumoPrintConfigCmd = &cobra.Command{
- Use: "print-config",
- Short: "Print a Sumo HTTP Source smoke-test uploader for Beacon endpoint events",
- Run: func(cmd *cobra.Command, args []string) {
+ Use: "print-config",
+ Short: "Print a Sumo HTTP Source smoke-test uploader for Beacon endpoint events",
+ SilenceUsage: true,
+ RunE: func(cmd *cobra.Command, args []string) error {
cfg := loadOrDefaultConfig()
- fmt.Print(sumo.UploadSmokeTest(cfg.LogPath))
+ snippet, err := sumo.UploadSmokeTest(cfg.LogPath)
+ if err != nil {
+ return err
+ }
+ fmt.Print(snippet)
+ return nil
},
}
@@ -488,11 +506,17 @@ var endpointSumoValidateCmd = &cobra.Command{
}
var endpointRapid7PrintConfigCmd = &cobra.Command{
- Use: "print-config",
- Short: "Print a Rapid7 Custom Logs webhook smoke-test uploader for Beacon endpoint events",
- Run: func(cmd *cobra.Command, args []string) {
+ Use: "print-config",
+ Short: "Print a Rapid7 Custom Logs webhook smoke-test uploader for Beacon endpoint events",
+ SilenceUsage: true,
+ RunE: func(cmd *cobra.Command, args []string) error {
cfg := loadOrDefaultConfig()
- fmt.Print(rapid7.UploadSmokeTest(cfg.LogPath))
+ snippet, err := rapid7.UploadSmokeTest(cfg.LogPath)
+ if err != nil {
+ return err
+ }
+ fmt.Print(snippet)
+ return nil
},
}
diff --git a/cli/beacon/internal/endpoint/datadog/datadog.go b/cli/beacon/internal/endpoint/datadog/datadog.go
index f230e0d2..2141fc88 100644
--- a/cli/beacon/internal/endpoint/datadog/datadog.go
+++ b/cli/beacon/internal/endpoint/datadog/datadog.go
@@ -3,9 +3,12 @@ package datadog
import (
"embed"
"fmt"
+ "io/fs"
"os"
"path/filepath"
"strings"
+
+ "github.com/asymptote-labs/agent-beacon/cli/beacon/internal/endpoint/siempack"
)
//go:embed pack/*
@@ -21,21 +24,71 @@ type File struct {
Content string
}
-func Files() []File {
- return []File{
- {Name: "README.md", Content: mustRead("pack/README.md")},
- {Name: "conf.yaml", Content: ConfigSnippet(DefaultLogPath)},
- {Name: "sample-event.jsonl", Content: mustRead("pack/sample-event.jsonl")},
+// readAsset reads an embedded asset by path and returns its contents or an error.
+func readAsset(path string) (string, error) {
+ data, err := packFS.ReadFile(path)
+ if err != nil {
+ return "", fmt.Errorf("datadog pack asset %q: %w", path, err)
+ }
+ return string(data), nil
+}
+
+// mustRead returns the embedded asset at path or panics. Retained for test use
+// where the embedded FS is always present; all production code uses readAsset.
+func mustRead(path string) string {
+ s, err := readAsset(path)
+ if err != nil {
+ panic(err.Error())
}
+ return s
}
-func ConfigSnippet(logPath string) string {
+// configSnippetFromFS renders the Datadog Agent conf.yaml snippet using the supplied FS.
+func configSnippetFromFS(fsys fs.FS, logPath string) (string, error) {
if logPath == "" {
logPath = DefaultLogPath
}
- return strings.ReplaceAll(mustRead("pack/conf.yaml.tmpl"), "{{LOG_PATH}}", logPath)
+ content, err := siempack.ReadFile(fsys, "pack/conf.yaml.tmpl")
+ if err != nil {
+ return "", fmt.Errorf("datadog pack asset %q: %w", "pack/conf.yaml.tmpl", err)
+ }
+ return strings.ReplaceAll(content, "{{LOG_PATH}}", logPath), nil
}
+// filesFromFS builds the full file list from the supplied FS, propagating any
+// read errors instead of panicking.
+func filesFromFS(fsys fs.FS, logPath string) ([]File, error) {
+ readme, err := siempack.ReadFile(fsys, "pack/README.md")
+ if err != nil {
+ return nil, fmt.Errorf("datadog pack asset %q: %w", "pack/README.md", err)
+ }
+ conf, err := configSnippetFromFS(fsys, logPath)
+ if err != nil {
+ return nil, err
+ }
+ sample, err := siempack.ReadFile(fsys, "pack/sample-event.jsonl")
+ if err != nil {
+ return nil, fmt.Errorf("datadog pack asset %q: %w", "pack/sample-event.jsonl", err)
+ }
+ return []File{
+ {Name: "README.md", Content: readme},
+ {Name: "conf.yaml", Content: conf},
+ {Name: "sample-event.jsonl", Content: sample},
+ }, nil
+}
+
+// Files returns all pack files rendered with DefaultLogPath, propagating any
+// embedded asset read error.
+func Files() ([]File, error) {
+ return filesFromFS(packFS, DefaultLogPath)
+}
+
+// ConfigSnippet returns the Datadog Agent conf.yaml snippet with logPath substituted.
+func ConfigSnippet(logPath string) (string, error) {
+ return configSnippetFromFS(packFS, logPath)
+}
+
+// InstallPack writes the pack files to outputDir with logPath substituted.
func InstallPack(outputDir, logPath string) error {
if outputDir == "" {
outputDir = DefaultOutputDir
@@ -43,10 +96,18 @@ func InstallPack(outputDir, logPath string) error {
if err := os.MkdirAll(outputDir, 0755); err != nil {
return err
}
- for _, file := range Files() {
+ files, err := filesFromFS(packFS, logPath)
+ if err != nil {
+ return err
+ }
+ for _, file := range files {
content := file.Content
if file.Name == "conf.yaml" {
- content = ConfigSnippet(logPath)
+ s, err := ConfigSnippet(logPath)
+ if err != nil {
+ return err
+ }
+ content = s
}
if err := os.WriteFile(filepath.Join(outputDir, file.Name), []byte(content), 0644); err != nil {
return err
@@ -54,11 +115,3 @@ func InstallPack(outputDir, logPath string) error {
}
return nil
}
-
-func mustRead(path string) string {
- data, err := packFS.ReadFile(path)
- if err != nil {
- panic(fmt.Sprintf("datadog pack asset %s: %v", path, err))
- }
- return string(data)
-}
diff --git a/cli/beacon/internal/endpoint/datadog/datadog_test.go b/cli/beacon/internal/endpoint/datadog/datadog_test.go
index 1f560165..141802fd 100644
--- a/cli/beacon/internal/endpoint/datadog/datadog_test.go
+++ b/cli/beacon/internal/endpoint/datadog/datadog_test.go
@@ -7,10 +7,14 @@ import (
"path/filepath"
"strings"
"testing"
+ "testing/fstest"
)
func TestConfigSnippetUsesConfiguredPath(t *testing.T) {
- got := ConfigSnippet("/tmp/beacon/runtime.jsonl")
+ got, err := ConfigSnippet("/tmp/beacon/runtime.jsonl")
+ if err != nil {
+ t.Fatalf("ConfigSnippet returned unexpected error: %v", err)
+ }
if !strings.Contains(got, "/tmp/beacon/runtime.jsonl") {
t.Fatalf("snippet did not include configured path: %s", got)
}
@@ -91,3 +95,80 @@ func TestPackREADMEMentionsValidationRetentionAndMacOSPermissions(t *testing.T)
}
}
}
+
+func TestFiles_NoError(t *testing.T) {
+ files, err := Files()
+ if err != nil {
+ t.Fatalf("Files() returned unexpected error: %v", err)
+ }
+ if len(files) == 0 {
+ t.Fatal("Files() returned no files")
+ }
+}
+
+func TestFiles_ContainsAllRequiredNames(t *testing.T) {
+ files, err := Files()
+ if err != nil {
+ t.Fatal(err)
+ }
+ names := make(map[string]bool)
+ for _, f := range files {
+ names[f.Name] = true
+ }
+ for _, required := range []string{
+ "README.md", "conf.yaml", "sample-event.jsonl",
+ } {
+ if !names[required] {
+ t.Errorf("Files() missing required file %q", required)
+ }
+ }
+}
+
+func TestFilesFromFS_PropagatesReadError(t *testing.T) {
+ emptyFS := fstest.MapFS{}
+ _, err := filesFromFS(emptyFS, DefaultLogPath)
+ if err == nil {
+ t.Fatal("filesFromFS with empty FS should return an error")
+ }
+ if !strings.Contains(err.Error(), "datadog pack asset") {
+ t.Fatalf("error should identify the pack source, got: %v", err)
+ }
+}
+
+func TestConfigSnippetFromFS_ErrorOnMissingAsset(t *testing.T) {
+ emptyFS := fstest.MapFS{}
+ _, err := configSnippetFromFS(emptyFS, "/some/path.jsonl")
+ if err == nil {
+ t.Fatal("configSnippetFromFS with empty FS should return error")
+ }
+ if !strings.Contains(err.Error(), "datadog pack asset") {
+ t.Fatalf("error should identify the pack source, got: %v", err)
+ }
+}
+
+func TestInstallPack_ErrorOnWriteFailure(t *testing.T) {
+ if os.Getuid() == 0 {
+ t.Skip("running as root: filesystem permission restrictions do not apply")
+ }
+ dir := t.TempDir()
+ if err := os.Chmod(dir, 0555); err != nil {
+ t.Skip("cannot set directory permissions:", err)
+ }
+ defer os.Chmod(dir, 0755)
+
+ subdir := filepath.Join(dir, "output")
+ err := InstallPack(subdir, DefaultLogPath)
+ if err == nil {
+ t.Fatal("InstallPack into read-only directory should return error")
+ }
+}
+
+func TestConfigSnippet_DefaultLogPath(t *testing.T) {
+ got, err := ConfigSnippet("")
+ if err != nil {
+ t.Fatalf("ConfigSnippet with empty path returned error: %v", err)
+ }
+ if !strings.Contains(got, DefaultLogPath) {
+ t.Fatalf("ConfigSnippet with empty logPath should use DefaultLogPath %q, got: %s", DefaultLogPath, got)
+ }
+}
diff --git a/cli/beacon/internal/endpoint/elastic/elastic.go b/cli/beacon/internal/endpoint/elastic/elastic.go
index 246ad948..e1268883 100644
--- a/cli/beacon/internal/endpoint/elastic/elastic.go
+++ b/cli/beacon/internal/endpoint/elastic/elastic.go
@@ -3,9 +3,12 @@ package elastic
import (
"embed"
"fmt"
+ "io/fs"
"os"
"path/filepath"
"strings"
+
+ "github.com/asymptote-labs/agent-beacon/cli/beacon/internal/endpoint/siempack"
)
//go:embed pack/*
@@ -21,29 +24,128 @@ type File struct {
Content string
}
-func Files() []File {
- return []File{
- {Name: "README.md", Content: mustRead("pack/README.md")},
- {Name: "filebeat.yml", Content: InputSnippet(DefaultLogPath)},
- {Name: "elastic-agent-standalone.yml", Content: standaloneAgentConfig(DefaultLogPath)},
- {Name: "ilm-policy.json", Content: mustRead("pack/ilm-policy.json")},
- {Name: "component-template-mappings.json", Content: mustRead("pack/component-template-mappings.json")},
- {Name: "component-template-settings.json", Content: mustRead("pack/component-template-settings.json")},
- {Name: "index-template.json", Content: mustRead("pack/index-template.json")},
- {Name: "ingest-pipeline.json", Content: mustRead("pack/ingest-pipeline.json")},
- {Name: "kibana-assets.ndjson", Content: mustRead("pack/kibana-assets.ndjson")},
- {Name: "docker-compose.yml", Content: mustRead("pack/docker-compose.yml")},
- {Name: "sample-event.jsonl", Content: mustRead("pack/sample-event.jsonl")},
+// readAsset reads an embedded asset by path and returns its contents or an error.
+func readAsset(path string) (string, error) {
+ data, err := packFS.ReadFile(path)
+ if err != nil {
+ return "", fmt.Errorf("elastic pack asset %q: %w", path, err)
+ }
+ return string(data), nil
+}
+
+// mustRead returns the embedded asset at path or panics. Retained for test use
+// where the embedded FS is always present; all production code uses readAsset.
+func mustRead(path string) string {
+ s, err := readAsset(path)
+ if err != nil {
+ panic(err.Error())
+ }
+ return s
+}
+
+// inputSnippetFromFS renders the filebeat input snippet using the supplied FS.
+func inputSnippetFromFS(fsys fs.FS, logPath string) (string, error) {
+ if logPath == "" {
+ logPath = DefaultLogPath
+ }
+ content, err := siempack.ReadFile(fsys, "pack/filebeat.yml.tmpl")
+ if err != nil {
+ return "", fmt.Errorf("elastic pack asset %q: %w", "pack/filebeat.yml.tmpl", err)
}
+ return strings.ReplaceAll(content, "{{LOG_PATH}}", logPath), nil
}
-func InputSnippet(logPath string) string {
+// standaloneAgentConfigFromFS renders the elastic-agent standalone config using the supplied FS.
+func standaloneAgentConfigFromFS(fsys fs.FS, logPath string) (string, error) {
if logPath == "" {
logPath = DefaultLogPath
}
- return strings.ReplaceAll(mustRead("pack/filebeat.yml.tmpl"), "{{LOG_PATH}}", logPath)
+ content, err := siempack.ReadFile(fsys, "pack/elastic-agent-standalone.yml.tmpl")
+ if err != nil {
+ return "", fmt.Errorf("elastic pack asset %q: %w", "pack/elastic-agent-standalone.yml.tmpl", err)
+ }
+ return strings.ReplaceAll(content, "{{LOG_PATH}}", logPath), nil
}
+// filesFromFS builds the full file list from the supplied FS, propagating any
+// read errors instead of panicking.
+func filesFromFS(fsys fs.FS, logPath string) ([]File, error) {
+ readme, err := siempack.ReadFile(fsys, "pack/README.md")
+ if err != nil {
+ return nil, fmt.Errorf("elastic pack asset %q: %w", "pack/README.md", err)
+ }
+ fbSnippet, err := inputSnippetFromFS(fsys, logPath)
+ if err != nil {
+ return nil, err
+ }
+ agentConfig, err := standaloneAgentConfigFromFS(fsys, logPath)
+ if err != nil {
+ return nil, err
+ }
+ ilm, err := siempack.ReadFile(fsys, "pack/ilm-policy.json")
+ if err != nil {
+ return nil, fmt.Errorf("elastic pack asset %q: %w", "pack/ilm-policy.json", err)
+ }
+ compMappings, err := siempack.ReadFile(fsys, "pack/component-template-mappings.json")
+ if err != nil {
+ return nil, fmt.Errorf("elastic pack asset %q: %w", "pack/component-template-mappings.json", err)
+ }
+ compSettings, err := siempack.ReadFile(fsys, "pack/component-template-settings.json")
+ if err != nil {
+ return nil, fmt.Errorf("elastic pack asset %q: %w", "pack/component-template-settings.json", err)
+ }
+ idxTemplate, err := siempack.ReadFile(fsys, "pack/index-template.json")
+ if err != nil {
+ return nil, fmt.Errorf("elastic pack asset %q: %w", "pack/index-template.json", err)
+ }
+ ingestPipeline, err := siempack.ReadFile(fsys, "pack/ingest-pipeline.json")
+ if err != nil {
+ return nil, fmt.Errorf("elastic pack asset %q: %w", "pack/ingest-pipeline.json", err)
+ }
+ kibana, err := siempack.ReadFile(fsys, "pack/kibana-assets.ndjson")
+ if err != nil {
+ return nil, fmt.Errorf("elastic pack asset %q: %w", "pack/kibana-assets.ndjson", err)
+ }
+ compose, err := siempack.ReadFile(fsys, "pack/docker-compose.yml")
+ if err != nil {
+ return nil, fmt.Errorf("elastic pack asset %q: %w", "pack/docker-compose.yml", err)
+ }
+ sample, err := siempack.ReadFile(fsys, "pack/sample-event.jsonl")
+ if err != nil {
+ return nil, fmt.Errorf("elastic pack asset %q: %w", "pack/sample-event.jsonl", err)
+ }
+ return []File{
+ {Name: "README.md", Content: readme},
+ {Name: "filebeat.yml", Content: fbSnippet},
+ {Name: "elastic-agent-standalone.yml", Content: agentConfig},
+ {Name: "ilm-policy.json", Content: ilm},
+ {Name: "component-template-mappings.json", Content: compMappings},
+ {Name: "component-template-settings.json", Content: compSettings},
+ {Name: "index-template.json", Content: idxTemplate},
+ {Name: "ingest-pipeline.json", Content: ingestPipeline},
+ {Name: "kibana-assets.ndjson", Content: kibana},
+ {Name: "docker-compose.yml", Content: compose},
+ {Name: "sample-event.jsonl", Content: sample},
+ }, nil
+}
+
+// Files returns all pack files rendered with DefaultLogPath, propagating any
+// embedded asset read error.
+func Files() ([]File, error) {
+ return filesFromFS(packFS, DefaultLogPath)
+}
+
+// InputSnippet returns the Filebeat input snippet with logPath substituted.
+func InputSnippet(logPath string) (string, error) {
+ return inputSnippetFromFS(packFS, logPath)
+}
+
+// standaloneAgentConfig returns the elastic-agent standalone config with logPath substituted.
+func standaloneAgentConfig(logPath string) (string, error) {
+ return standaloneAgentConfigFromFS(packFS, logPath)
+}
+
+// InstallPack writes the pack files to outputDir with logPath substituted.
func InstallPack(outputDir, logPath string) error {
if outputDir == "" {
outputDir = DefaultOutputDir
@@ -51,13 +153,25 @@ func InstallPack(outputDir, logPath string) error {
if err := os.MkdirAll(outputDir, 0755); err != nil {
return err
}
- for _, file := range Files() {
+ files, err := filesFromFS(packFS, logPath)
+ if err != nil {
+ return err
+ }
+ for _, file := range files {
content := file.Content
switch file.Name {
case "filebeat.yml":
- content = InputSnippet(logPath)
+ s, err := InputSnippet(logPath)
+ if err != nil {
+ return err
+ }
+ content = s
case "elastic-agent-standalone.yml":
- content = standaloneAgentConfig(logPath)
+ s, err := standaloneAgentConfig(logPath)
+ if err != nil {
+ return err
+ }
+ content = s
}
if err := os.WriteFile(filepath.Join(outputDir, file.Name), []byte(content), 0644); err != nil {
return err
@@ -65,18 +179,3 @@ func InstallPack(outputDir, logPath string) error {
}
return nil
}
-
-func standaloneAgentConfig(logPath string) string {
- if logPath == "" {
- logPath = DefaultLogPath
- }
- return strings.ReplaceAll(mustRead("pack/elastic-agent-standalone.yml.tmpl"), "{{LOG_PATH}}", logPath)
-}
-
-func mustRead(path string) string {
- data, err := packFS.ReadFile(path)
- if err != nil {
- panic(fmt.Sprintf("elastic pack asset %s: %v", path, err))
- }
- return string(data)
-}
diff --git a/cli/beacon/internal/endpoint/elastic/elastic_test.go b/cli/beacon/internal/endpoint/elastic/elastic_test.go
index 31868fe8..623eddb8 100644
--- a/cli/beacon/internal/endpoint/elastic/elastic_test.go
+++ b/cli/beacon/internal/endpoint/elastic/elastic_test.go
@@ -7,10 +7,14 @@ import (
"path/filepath"
"strings"
"testing"
+ "testing/fstest"
)
func TestInputSnippetUsesConfiguredPath(t *testing.T) {
- got := InputSnippet("/tmp/beacon/runtime.jsonl")
+ got, err := InputSnippet("/tmp/beacon/runtime.jsonl")
+ if err != nil {
+ t.Fatalf("InputSnippet returned unexpected error: %v", err)
+ }
if !strings.Contains(got, "/tmp/beacon/runtime.jsonl") {
t.Fatalf("snippet did not include configured path: %s", got)
}
@@ -182,3 +186,82 @@ func TestIngestPipelineMentionsKeyMappings(t *testing.T) {
t.Fatal("ingest pipeline should preserve Beacon file actions instead of deriving lossy file operation actions")
}
}
+
+func TestFiles_NoError(t *testing.T) {
+ files, err := Files()
+ if err != nil {
+ t.Fatalf("Files() returned unexpected error: %v", err)
+ }
+ if len(files) == 0 {
+ t.Fatal("Files() returned no files")
+ }
+}
+
+func TestFiles_ContainsAllRequiredNames(t *testing.T) {
+ files, err := Files()
+ if err != nil {
+ t.Fatal(err)
+ }
+ names := make(map[string]bool)
+ for _, f := range files {
+ names[f.Name] = true
+ }
+ for _, required := range []string{
+ "README.md", "filebeat.yml", "elastic-agent-standalone.yml",
+ "ilm-policy.json", "kibana-assets.ndjson", "docker-compose.yml",
+ "sample-event.jsonl",
+ } {
+ if !names[required] {
+ t.Errorf("Files() missing required file %q", required)
+ }
+ }
+}
+
+func TestFilesFromFS_PropagatesReadError(t *testing.T) {
+ emptyFS := fstest.MapFS{}
+ _, err := filesFromFS(emptyFS, DefaultLogPath)
+ if err == nil {
+ t.Fatal("filesFromFS with empty FS should return an error")
+ }
+ if !strings.Contains(err.Error(), "elastic pack asset") {
+ t.Fatalf("error should identify the pack source, got: %v", err)
+ }
+}
+
+func TestInputSnippetFromFS_ErrorOnMissingAsset(t *testing.T) {
+ emptyFS := fstest.MapFS{}
+ _, err := inputSnippetFromFS(emptyFS, "/some/path.jsonl")
+ if err == nil {
+ t.Fatal("inputSnippetFromFS with empty FS should return error")
+ }
+ if !strings.Contains(err.Error(), "elastic pack asset") {
+ t.Fatalf("error should identify the pack source, got: %v", err)
+ }
+}
+
+func TestInstallPack_ErrorOnWriteFailure(t *testing.T) {
+ if os.Getuid() == 0 {
+ t.Skip("running as root: filesystem permission restrictions do not apply")
+ }
+ dir := t.TempDir()
+ if err := os.Chmod(dir, 0555); err != nil {
+ t.Skip("cannot set directory permissions:", err)
+ }
+ defer os.Chmod(dir, 0755)
+
+ subdir := filepath.Join(dir, "output")
+ err := InstallPack(subdir, DefaultLogPath)
+ if err == nil {
+ t.Fatal("InstallPack into read-only directory should return error")
+ }
+}
+
+func TestInputSnippet_DefaultLogPath(t *testing.T) {
+ got, err := InputSnippet("")
+ if err != nil {
+ t.Fatalf("InputSnippet with empty path returned error: %v", err)
+ }
+ if !strings.Contains(got, DefaultLogPath) {
+ t.Fatalf("InputSnippet with empty logPath should use DefaultLogPath %q, got: %s", DefaultLogPath, got)
+ }
+}
diff --git a/cli/beacon/internal/endpoint/rapid7/rapid7.go b/cli/beacon/internal/endpoint/rapid7/rapid7.go
index 8b305dce..e77f8527 100644
--- a/cli/beacon/internal/endpoint/rapid7/rapid7.go
+++ b/cli/beacon/internal/endpoint/rapid7/rapid7.go
@@ -3,6 +3,7 @@ package rapid7
import (
"embed"
"fmt"
+ "io/fs"
"github.com/asymptote-labs/agent-beacon/cli/beacon/internal/endpoint/siempack"
)
@@ -21,22 +22,76 @@ type File struct {
TemplateLogPath bool
}
-func Files() []File {
- return []File{
- {Name: "README.md", Content: mustRead("pack/README.md")},
- {Name: "rapid7-upload-smoke-test.sh", Content: mustRead("pack/rapid7-upload-smoke-test.sh.tmpl"), TemplateLogPath: true},
- {Name: "sample-event.jsonl", Content: mustRead("pack/sample-event.jsonl")},
- {Name: "vector.toml", Content: mustRead("pack/vector.toml.tmpl"), TemplateLogPath: true},
+// readAsset reads an embedded asset using the module-level packFS and returns its
+// contents or an error.
+func readAsset(path string) (string, error) {
+ data, err := siempack.ReadFile(packFS, path)
+ if err != nil {
+ return "", fmt.Errorf("rapid7 pack asset %q: %w", path, err)
+ }
+ return data, nil
+}
+
+// mustRead returns the embedded asset at path or panics. Retained for test use
+// where the embedded FS is always present; all production code uses readAsset.
+func mustRead(path string) string {
+ s, err := readAsset(path)
+ if err != nil {
+ panic(err.Error())
}
+ return s
}
-func UploadSmokeTest(logPath string) string {
+// uploadSmokeTestFromFS renders the Rapid7 upload smoke-test script using the supplied FS.
+func uploadSmokeTestFromFS(fsys fs.FS, logPath string) (string, error) {
if logPath == "" {
logPath = DefaultLogPath
}
- return siempack.RenderLogPath(mustRead("pack/rapid7-upload-smoke-test.sh.tmpl"), logPath)
+ content, err := siempack.ReadFile(fsys, "pack/rapid7-upload-smoke-test.sh.tmpl")
+ if err != nil {
+ return "", fmt.Errorf("rapid7 pack asset %q: %w", "pack/rapid7-upload-smoke-test.sh.tmpl", err)
+ }
+ return siempack.RenderLogPath(content, logPath), nil
}
+// filesFromFS builds the full file list from the supplied FS, propagating any
+// read errors instead of panicking.
+func filesFromFS(fsys fs.FS, logPath string) ([]File, error) {
+ readme, err := siempack.ReadFile(fsys, "pack/README.md")
+ if err != nil {
+ return nil, fmt.Errorf("rapid7 pack asset %q: %w", "pack/README.md", err)
+ }
+ smokeTest, err := siempack.ReadFile(fsys, "pack/rapid7-upload-smoke-test.sh.tmpl")
+ if err != nil {
+ return nil, fmt.Errorf("rapid7 pack asset %q: %w", "pack/rapid7-upload-smoke-test.sh.tmpl", err)
+ }
+ sample, err := siempack.ReadFile(fsys, "pack/sample-event.jsonl")
+ if err != nil {
+ return nil, fmt.Errorf("rapid7 pack asset %q: %w", "pack/sample-event.jsonl", err)
+ }
+ vector, err := siempack.ReadFile(fsys, "pack/vector.toml.tmpl")
+ if err != nil {
+ return nil, fmt.Errorf("rapid7 pack asset %q: %w", "pack/vector.toml.tmpl", err)
+ }
+ return []File{
+ {Name: "README.md", Content: readme},
+ {Name: "rapid7-upload-smoke-test.sh", Content: smokeTest, TemplateLogPath: true},
+ {Name: "sample-event.jsonl", Content: sample},
+ {Name: "vector.toml", Content: vector, TemplateLogPath: true},
+ }, nil
+}
+
+// Files returns all pack files, propagating any embedded asset read error.
+func Files() ([]File, error) {
+ return filesFromFS(packFS, DefaultLogPath)
+}
+
+// UploadSmokeTest returns the Rapid7 upload smoke-test script with logPath substituted.
+func UploadSmokeTest(logPath string) (string, error) {
+ return uploadSmokeTestFromFS(packFS, logPath)
+}
+
+// InstallPack writes the pack files to outputDir with logPath substituted.
func InstallPack(outputDir, logPath string) error {
if outputDir == "" {
outputDir = DefaultOutputDir
@@ -44,17 +99,13 @@ func InstallPack(outputDir, logPath string) error {
if logPath == "" {
logPath = DefaultLogPath
}
- files := make([]siempack.File, 0, len(Files()))
- for _, file := range Files() {
- files = append(files, siempack.File(file))
- }
- return siempack.Install(outputDir, files, logPath)
-}
-
-func mustRead(path string) string {
- data, err := siempack.ReadFile(packFS, path)
+ files, err := filesFromFS(packFS, logPath)
if err != nil {
- panic(fmt.Sprintf("rapid7 pack asset %s: %v", path, err))
+ return err
+ }
+ sfiles := make([]siempack.File, 0, len(files))
+ for _, file := range files {
+ sfiles = append(sfiles, siempack.File(file))
}
- return data
+ return siempack.Install(outputDir, sfiles, logPath)
}
diff --git a/cli/beacon/internal/endpoint/rapid7/rapid7_test.go b/cli/beacon/internal/endpoint/rapid7/rapid7_test.go
index 4a40e825..138851e8 100644
--- a/cli/beacon/internal/endpoint/rapid7/rapid7_test.go
+++ b/cli/beacon/internal/endpoint/rapid7/rapid7_test.go
@@ -7,10 +7,14 @@ import (
"path/filepath"
"strings"
"testing"
+ "testing/fstest"
)
func TestUploadSmokeTestUsesConfiguredPath(t *testing.T) {
- got := UploadSmokeTest("/tmp/beacon/runtime.jsonl")
+ got, err := UploadSmokeTest("/tmp/beacon/runtime.jsonl")
+ if err != nil {
+ t.Fatalf("UploadSmokeTest returned unexpected error: %v", err)
+ }
if !strings.Contains(got, "/tmp/beacon/runtime.jsonl") {
t.Fatalf("script did not include configured path: %s", got)
}
@@ -138,3 +142,80 @@ func TestPackREADMEMentionsRapid7SetupAndProductionForwarding(t *testing.T) {
}
}
}
+
+func TestFiles_NoError(t *testing.T) {
+ files, err := Files()
+ if err != nil {
+ t.Fatalf("Files() returned unexpected error: %v", err)
+ }
+ if len(files) == 0 {
+ t.Fatal("Files() returned no files")
+ }
+}
+
+func TestFiles_ContainsAllRequiredNames(t *testing.T) {
+ files, err := Files()
+ if err != nil {
+ t.Fatal(err)
+ }
+ names := make(map[string]bool)
+ for _, f := range files {
+ names[f.Name] = true
+ }
+ for _, required := range []string{
+ "README.md", "rapid7-upload-smoke-test.sh", "sample-event.jsonl", "vector.toml",
+ } {
+ if !names[required] {
+ t.Errorf("Files() missing required file %q", required)
+ }
+ }
+}
+
+func TestFilesFromFS_PropagatesReadError(t *testing.T) {
+ emptyFS := fstest.MapFS{}
+ _, err := filesFromFS(emptyFS, DefaultLogPath)
+ if err == nil {
+ t.Fatal("filesFromFS with empty FS should return an error")
+ }
+ if !strings.Contains(err.Error(), "rapid7 pack asset") {
+ t.Fatalf("error should identify the pack source, got: %v", err)
+ }
+}
+
+func TestUploadSmokeTestFromFS_ErrorOnMissingAsset(t *testing.T) {
+ emptyFS := fstest.MapFS{}
+ _, err := uploadSmokeTestFromFS(emptyFS, "/some/path.jsonl")
+ if err == nil {
+ t.Fatal("uploadSmokeTestFromFS with empty FS should return error")
+ }
+ if !strings.Contains(err.Error(), "rapid7 pack asset") {
+ t.Fatalf("error should identify the pack source, got: %v", err)
+ }
+}
+
+func TestInstallPack_ErrorOnWriteFailure(t *testing.T) {
+ if os.Getuid() == 0 {
+ t.Skip("running as root: filesystem permission restrictions do not apply")
+ }
+ dir := t.TempDir()
+ if err := os.Chmod(dir, 0555); err != nil {
+ t.Skip("cannot set directory permissions:", err)
+ }
+ defer os.Chmod(dir, 0755)
+
+ subdir := filepath.Join(dir, "output")
+ err := InstallPack(subdir, DefaultLogPath)
+ if err == nil {
+ t.Fatal("InstallPack into read-only directory should return error")
+ }
+}
+
+func TestUploadSmokeTest_DefaultLogPath(t *testing.T) {
+ got, err := UploadSmokeTest("")
+ if err != nil {
+ t.Fatalf("UploadSmokeTest with empty path returned error: %v", err)
+ }
+ if !strings.Contains(got, DefaultLogPath) {
+ t.Fatalf("UploadSmokeTest with empty logPath should use DefaultLogPath %q, got: %s", DefaultLogPath, got)
+ }
+}
diff --git a/cli/beacon/internal/endpoint/sumo/sumo.go b/cli/beacon/internal/endpoint/sumo/sumo.go
index 199823b3..37848de6 100644
--- a/cli/beacon/internal/endpoint/sumo/sumo.go
+++ b/cli/beacon/internal/endpoint/sumo/sumo.go
@@ -3,6 +3,7 @@ package sumo
import (
"embed"
"fmt"
+ "io/fs"
"github.com/asymptote-labs/agent-beacon/cli/beacon/internal/endpoint/siempack"
)
@@ -21,22 +22,76 @@ type File struct {
TemplateLogPath bool
}
-func Files() []File {
- return []File{
- {Name: "README.md", Content: mustRead("pack/README.md")},
- {Name: "sumo-upload-smoke-test.sh", Content: mustRead("pack/sumo-upload-smoke-test.sh.tmpl"), TemplateLogPath: true},
- {Name: "sample-event.jsonl", Content: mustRead("pack/sample-event.jsonl")},
- {Name: "vector.toml", Content: mustRead("pack/vector.toml.tmpl"), TemplateLogPath: true},
+// readAsset reads an embedded asset using the module-level packFS and returns its
+// contents or an error.
+func readAsset(path string) (string, error) {
+ data, err := siempack.ReadFile(packFS, path)
+ if err != nil {
+ return "", fmt.Errorf("sumo pack asset %q: %w", path, err)
+ }
+ return data, nil
+}
+
+// mustRead returns the embedded asset at path or panics. Retained for test use
+// where the embedded FS is always present; all production code uses readAsset.
+func mustRead(path string) string {
+ s, err := readAsset(path)
+ if err != nil {
+ panic(err.Error())
}
+ return s
}
-func UploadSmokeTest(logPath string) string {
+// uploadSmokeTestFromFS renders the Sumo upload smoke-test script using the supplied FS.
+func uploadSmokeTestFromFS(fsys fs.FS, logPath string) (string, error) {
if logPath == "" {
logPath = DefaultLogPath
}
- return siempack.RenderLogPath(mustRead("pack/sumo-upload-smoke-test.sh.tmpl"), logPath)
+ content, err := siempack.ReadFile(fsys, "pack/sumo-upload-smoke-test.sh.tmpl")
+ if err != nil {
+ return "", fmt.Errorf("sumo pack asset %q: %w", "pack/sumo-upload-smoke-test.sh.tmpl", err)
+ }
+ return siempack.RenderLogPath(content, logPath), nil
}
+// filesFromFS builds the full file list from the supplied FS, propagating any
+// read errors instead of panicking.
+func filesFromFS(fsys fs.FS, logPath string) ([]File, error) {
+ readme, err := siempack.ReadFile(fsys, "pack/README.md")
+ if err != nil {
+ return nil, fmt.Errorf("sumo pack asset %q: %w", "pack/README.md", err)
+ }
+ smokeTest, err := siempack.ReadFile(fsys, "pack/sumo-upload-smoke-test.sh.tmpl")
+ if err != nil {
+ return nil, fmt.Errorf("sumo pack asset %q: %w", "pack/sumo-upload-smoke-test.sh.tmpl", err)
+ }
+ sample, err := siempack.ReadFile(fsys, "pack/sample-event.jsonl")
+ if err != nil {
+ return nil, fmt.Errorf("sumo pack asset %q: %w", "pack/sample-event.jsonl", err)
+ }
+ vector, err := siempack.ReadFile(fsys, "pack/vector.toml.tmpl")
+ if err != nil {
+ return nil, fmt.Errorf("sumo pack asset %q: %w", "pack/vector.toml.tmpl", err)
+ }
+ return []File{
+ {Name: "README.md", Content: readme},
+ {Name: "sumo-upload-smoke-test.sh", Content: smokeTest, TemplateLogPath: true},
+ {Name: "sample-event.jsonl", Content: sample},
+ {Name: "vector.toml", Content: vector, TemplateLogPath: true},
+ }, nil
+}
+
+// Files returns all pack files, propagating any embedded asset read error.
+func Files() ([]File, error) {
+ return filesFromFS(packFS, DefaultLogPath)
+}
+
+// UploadSmokeTest returns the Sumo upload smoke-test script with logPath substituted.
+func UploadSmokeTest(logPath string) (string, error) {
+ return uploadSmokeTestFromFS(packFS, logPath)
+}
+
+// InstallPack writes the pack files to outputDir with logPath substituted.
func InstallPack(outputDir, logPath string) error {
if outputDir == "" {
outputDir = DefaultOutputDir
@@ -44,17 +99,13 @@ func InstallPack(outputDir, logPath string) error {
if logPath == "" {
logPath = DefaultLogPath
}
- files := make([]siempack.File, 0, len(Files()))
- for _, file := range Files() {
- files = append(files, siempack.File(file))
- }
- return siempack.Install(outputDir, files, logPath)
-}
-
-func mustRead(path string) string {
- data, err := siempack.ReadFile(packFS, path)
+ files, err := filesFromFS(packFS, logPath)
if err != nil {
- panic(fmt.Sprintf("sumo pack asset %s: %v", path, err))
+ return err
+ }
+ sfiles := make([]siempack.File, 0, len(files))
+ for _, file := range files {
+ sfiles = append(sfiles, siempack.File(file))
}
- return data
+ return siempack.Install(outputDir, sfiles, logPath)
}
diff --git a/cli/beacon/internal/endpoint/sumo/sumo_test.go b/cli/beacon/internal/endpoint/sumo/sumo_test.go
index 47cb5e2f..2e99b320 100644
--- a/cli/beacon/internal/endpoint/sumo/sumo_test.go
+++ b/cli/beacon/internal/endpoint/sumo/sumo_test.go
@@ -7,10 +7,14 @@ import (
"path/filepath"
"strings"
"testing"
+ "testing/fstest"
)
func TestUploadSmokeTestUsesConfiguredPath(t *testing.T) {
- got := UploadSmokeTest("/tmp/beacon/runtime.jsonl")
+ got, err := UploadSmokeTest("/tmp/beacon/runtime.jsonl")
+ if err != nil {
+ t.Fatalf("UploadSmokeTest returned unexpected error: %v", err)
+ }
if !strings.Contains(got, "/tmp/beacon/runtime.jsonl") {
t.Fatalf("script did not include configured path: %s", got)
}
@@ -144,3 +148,80 @@ func TestPackREADMEMentionsSumoSetupAndProductionForwarding(t *testing.T) {
}
}
}
+
+func TestFiles_NoError(t *testing.T) {
+ files, err := Files()
+ if err != nil {
+ t.Fatalf("Files() returned unexpected error: %v", err)
+ }
+ if len(files) == 0 {
+ t.Fatal("Files() returned no files")
+ }
+}
+
+func TestFiles_ContainsAllRequiredNames(t *testing.T) {
+ files, err := Files()
+ if err != nil {
+ t.Fatal(err)
+ }
+ names := make(map[string]bool)
+ for _, f := range files {
+ names[f.Name] = true
+ }
+ for _, required := range []string{
+ "README.md", "sumo-upload-smoke-test.sh", "sample-event.jsonl", "vector.toml",
+ } {
+ if !names[required] {
+ t.Errorf("Files() missing required file %q", required)
+ }
+ }
+}
+
+func TestFilesFromFS_PropagatesReadError(t *testing.T) {
+ emptyFS := fstest.MapFS{}
+ _, err := filesFromFS(emptyFS, DefaultLogPath)
+ if err == nil {
+ t.Fatal("filesFromFS with empty FS should return an error")
+ }
+ if !strings.Contains(err.Error(), "sumo pack asset") {
+ t.Fatalf("error should identify the pack source, got: %v", err)
+ }
+}
+
+func TestUploadSmokeTestFromFS_ErrorOnMissingAsset(t *testing.T) {
+ emptyFS := fstest.MapFS{}
+ _, err := uploadSmokeTestFromFS(emptyFS, "/some/path.jsonl")
+ if err == nil {
+ t.Fatal("uploadSmokeTestFromFS with empty FS should return error")
+ }
+ if !strings.Contains(err.Error(), "sumo pack asset") {
+ t.Fatalf("error should identify the pack source, got: %v", err)
+ }
+}
+
+func TestInstallPack_ErrorOnWriteFailure(t *testing.T) {
+ if os.Getuid() == 0 {
+ t.Skip("running as root: filesystem permission restrictions do not apply")
+ }
+ dir := t.TempDir()
+ if err := os.Chmod(dir, 0555); err != nil {
+ t.Skip("cannot set directory permissions:", err)
+ }
+ defer os.Chmod(dir, 0755)
+
+ subdir := filepath.Join(dir, "output")
+ err := InstallPack(subdir, DefaultLogPath)
+ if err == nil {
+ t.Fatal("InstallPack into read-only directory should return error")
+ }
+}
+
+func TestUploadSmokeTest_DefaultLogPath(t *testing.T) {
+ got, err := UploadSmokeTest("")
+ if err != nil {
+ t.Fatalf("UploadSmokeTest with empty path returned error: %v", err)
+ }
+ if !strings.Contains(got, DefaultLogPath) {
+ t.Fatalf("UploadSmokeTest with empty logPath should use DefaultLogPath %q, got: %s", DefaultLogPath, got)
+ }
+}