From 3e1d9025604fa86ef4e43ecbc8c8888488d251af Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Tue, 25 Jun 2013 14:27:12 -0500 Subject: [PATCH] packer/plugin: Require the magic cookie to be present to run This is just a silly check to make sure people aren't executing the plugins directly. If they are, a nicer error message is shown. --- packer/plugin/client.go | 1 + packer/plugin/plugin.go | 8 ++++++++ 2 files changed, 9 insertions(+) diff --git a/packer/plugin/client.go b/packer/plugin/client.go index 29011e741..daf24a8af 100644 --- a/packer/plugin/client.go +++ b/packer/plugin/client.go @@ -205,6 +205,7 @@ func (c *Client) Start() (address string, err error) { } env := []string{ + fmt.Sprintf("%s=%s", MagicCookieKey, MagicCookieValue), fmt.Sprintf("PACKER_PLUGIN_MIN_PORT=%d", c.config.MinPort), fmt.Sprintf("PACKER_PLUGIN_MAX_PORT=%d", c.config.MaxPort), } diff --git a/packer/plugin/plugin.go b/packer/plugin/plugin.go index 91ed6231a..72629359c 100644 --- a/packer/plugin/plugin.go +++ b/packer/plugin/plugin.go @@ -8,6 +8,7 @@ package plugin import ( + "errors" "fmt" "github.com/mitchellh/packer/packer" packrpc "github.com/mitchellh/packer/packer/rpc" @@ -21,9 +22,16 @@ import ( "strings" ) +const MagicCookieKey = "PACKER_PLUGIN_MAGIC_COOKIE" +const MagicCookieValue = "d602bf8f470bc67ca7faa0386276bbdd4330efaf76d1a219cb4d6991ca9872b2" + // This serves a single RPC connection on the given RPC server on // a random port. func serve(server *rpc.Server) (err error) { + if os.Getenv(MagicCookieKey) != MagicCookieValue { + return errors.New("Please do not execute plugins directly. Packer will execute these for you.") + } + // If there is no explicit number of Go threads to use, then set it if os.Getenv("GOMAXPROCS") == "" { runtime.GOMAXPROCS(runtime.NumCPU())