diff --git a/pkg/loop/cmd/loopinstall/README.md b/pkg/loop/cmd/loopinstall/README.md index ff07ba95fd..9c77a2e671 100644 --- a/pkg/loop/cmd/loopinstall/README.md +++ b/pkg/loop/cmd/loopinstall/README.md @@ -51,6 +51,8 @@ plugins: The `installPath` is relative to the root of the downloaded module but it can also be an absolute path like: `github.com/smartcontractkit/chainlink-cosmos/pkg/cosmos/cmd/chainlink-cosmos`. The absolute path will be stripped to the relative path (it is supported for backwards compatibility). +By default the installed binary is named after the base of `installPath` (or of `moduleURI` when `installPath` is the module root). When the package that builds the binary is not named after the binary itself, set `binaryName` to override the installed name; the build target stays whatever `installPath` points at. + The `libs` field is an array of strings representing directory paths, which can include glob patterns for library files that need to be included with the plugin. Docker build will use these paths to copy the libraries into the final container image. ## Private Repository Access diff --git a/pkg/loop/cmd/loopinstall/install.go b/pkg/loop/cmd/loopinstall/install.go index 90999c23ed..02d284dcad 100644 --- a/pkg/loop/cmd/loopinstall/install.go +++ b/pkg/loop/cmd/loopinstall/install.go @@ -268,6 +268,12 @@ func downloadAndInstallPlugin(pluginType string, pluginIdx int, plugin PluginDef binaryName = filepath.Base(filepath.Clean(moduleURI)) } + // An explicit binaryName decouples the package that builds the binary from + // the name it is installed under. + if plugin.BinaryName != "" { + binaryName = plugin.BinaryName + } + // Determine output directory (GOBIN, or GOPATH/bin, or $HOME/go/bin). outputDir := os.Getenv("GOBIN") if outputDir == "" { @@ -337,6 +343,7 @@ func writeBuildManifest(tasks []PluginInstallTask, outputFile string) error { ModuleURI: task.Plugin.ModuleURI, GitRef: task.Plugin.GitRef, InstallPath: task.Plugin.InstallPath, + BinaryName: task.Plugin.BinaryName, } if len(task.Plugin.Libs) > 0 { diff --git a/pkg/loop/cmd/loopinstall/main_test.go b/pkg/loop/cmd/loopinstall/main_test.go index 61fca86736..d44b57e2dc 100644 --- a/pkg/loop/cmd/loopinstall/main_test.go +++ b/pkg/loop/cmd/loopinstall/main_test.go @@ -500,6 +500,23 @@ func TestValidationFunctions(t *testing.T) { "github.com/example/module\nls", }, }, + { + name: "validateBinaryName", + validator: validateBinaryName, + valid: []string{ + "cron", + "cronv2", + "chainlink-evm", + "tool.v2", + }, + invalid: []string{ + "cron/../../etc", + "../escape", + "cron;rm -rf /", + "cron\nls", + "", + }, + }, } for _, tc := range tests { @@ -727,6 +744,48 @@ func TestDownloadAndInstallPlugin(t *testing.T) { }, expectError: false, }, + { + name: "binary name override", + plugin: PluginDef{ + ModuleURI: "github.com/example/capabilities/scheduler/cron", + GitRef: "v1.0.0", + InstallPath: "./legacy", // Package dir is not named after the binary + BinaryName: "cron", + }, + defaults: DefaultsConfig{}, + mockDownload: func(cmd *exec.Cmd) error { + if stdout, ok := cmd.Stdout.(*bytes.Buffer); ok { + parts := strings.Split("github.com/example/capabilities/scheduler/cron", "/") + moduleDir := filepath.Join(append([]string{tempDir, "modules"}, parts...)...) + fmt.Fprintf(stdout, `{"Dir":"%s"}`, moduleDir) + } + return nil + }, + mockBuild: func(cmd *exec.Cmd) error { + if len(cmd.Args) < 2 { + return fmt.Errorf("install command has too few arguments: %v", cmd.Args) + } + // args: build -o [flags...] + outIdx := -1 + for i, a := range cmd.Args { + if a == "-o" { + outIdx = i + break + } + } + if outIdx == -1 || outIdx+1 >= len(cmd.Args) { + return fmt.Errorf("expected -o in install command: %v", cmd.Args) + } + if base := filepath.Base(cmd.Args[outIdx+1]); base != "cron" { + return fmt.Errorf("expected output binary %q, got %q", "cron", base) + } + if packageArg := cmd.Args[len(cmd.Args)-1]; packageArg != "./legacy" { + return fmt.Errorf("expected install package %q, got %q", "./legacy", packageArg) + } + return nil + }, + expectError: false, + }, } for _, tc := range tests { diff --git a/pkg/loop/cmd/loopinstall/models.go b/pkg/loop/cmd/loopinstall/models.go index 18ac4a34f1..bb1e5bfd0a 100644 --- a/pkg/loop/cmd/loopinstall/models.go +++ b/pkg/loop/cmd/loopinstall/models.go @@ -21,6 +21,12 @@ type PluginDef struct { Libs []string `yaml:"libs"` Flags string `yaml:"flags,omitempty"` EnvVars []string `yaml:"envvars,omitempty"` + // BinaryName overrides the name of the installed binary. By default the + // name is the base of installPath (or of moduleURI when installPath is the + // module root), which breaks when the package that builds the binary is + // not named after the binary itself (e.g. "./legacy" building the binary + // a node looks up as "cron"). + BinaryName string `yaml:"binaryName,omitempty"` } // ModDownloadResult represents the JSON directory (dir) output from 'go mod download -json' @@ -55,4 +61,5 @@ type PluginManifest struct { GitRef string `json:"gitRef"` InstallPath string `json:"installPath"` Libs []string `json:"libs,omitempty"` + BinaryName string `json:"binaryName,omitempty"` } diff --git a/pkg/loop/cmd/loopinstall/plugins.example.yaml b/pkg/loop/cmd/loopinstall/plugins.example.yaml index fb49630a60..1793a68805 100644 --- a/pkg/loop/cmd/loopinstall/plugins.example.yaml +++ b/pkg/loop/cmd/loopinstall/plugins.example.yaml @@ -25,3 +25,12 @@ plugins: gitRef: "9a780650af4708e4bd9b75495feff2c5b4054e46" installPath: "github.com/smartcontractkit/chainlink-starknet/relayer/pkg/chainlink/cmd/chainlink-starknet" libs: [] + + legacy-cron: + # Example of a binary name override: the package that builds the binary is + # not named after it, so the installed name is set explicitly. + - moduleURI: "github.com/smartcontractkit/capabilities/scheduler/cron" + gitRef: "9777ffcac0b966d2488cb1e63e155da8b98bd8d6" + installPath: "./legacy" + binaryName: "cron" + flags: "-tags timetzdata" diff --git a/pkg/loop/cmd/loopinstall/validation.go b/pkg/loop/cmd/loopinstall/validation.go index 453019017f..c3af92e55f 100644 --- a/pkg/loop/cmd/loopinstall/validation.go +++ b/pkg/loop/cmd/loopinstall/validation.go @@ -18,6 +18,11 @@ func (plugin PluginDef) Validate() error { if err := validateInstallPath(plugin.InstallPath); err != nil { return err } + if plugin.BinaryName != "" { + if err := validateBinaryName(plugin.BinaryName); err != nil { + return err + } + } return nil } @@ -49,6 +54,15 @@ func validateInstallPath(path string) error { return nil } +// validateBinaryName ensures an overridden binary name is a plain file name, +// so it cannot escape the output directory or smuggle path separators. +func validateBinaryName(name string) error { + if !regexp.MustCompile(`^[\w.-]+$`).MatchString(name) { + return fmt.Errorf("invalid binary name: %s", name) + } + return nil +} + // validateGoFlags ensures flags are safe and prevents command injection func validateGoFlags(flags string) error { // Check for potentially dangerous characters or substrings that could enable command injection