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.
This commit is contained in:
Mitchell Hashimoto 2013-06-25 14:27:12 -05:00
parent d31137d01c
commit df72fd0b0e
2 changed files with 9 additions and 0 deletions

View File

@ -205,6 +205,7 @@ func (c *Client) Start() (address string, err error) {
} }
env := []string{ env := []string{
fmt.Sprintf("%s=%s", MagicCookieKey, MagicCookieValue),
fmt.Sprintf("PACKER_PLUGIN_MIN_PORT=%d", c.config.MinPort), fmt.Sprintf("PACKER_PLUGIN_MIN_PORT=%d", c.config.MinPort),
fmt.Sprintf("PACKER_PLUGIN_MAX_PORT=%d", c.config.MaxPort), fmt.Sprintf("PACKER_PLUGIN_MAX_PORT=%d", c.config.MaxPort),
} }

View File

@ -8,6 +8,7 @@
package plugin package plugin
import ( import (
"errors"
"fmt" "fmt"
"github.com/mitchellh/packer/packer" "github.com/mitchellh/packer/packer"
packrpc "github.com/mitchellh/packer/packer/rpc" packrpc "github.com/mitchellh/packer/packer/rpc"
@ -21,9 +22,16 @@ import (
"strings" "strings"
) )
const MagicCookieKey = "PACKER_PLUGIN_MAGIC_COOKIE"
const MagicCookieValue = "d602bf8f470bc67ca7faa0386276bbdd4330efaf76d1a219cb4d6991ca9872b2"
// This serves a single RPC connection on the given RPC server on // This serves a single RPC connection on the given RPC server on
// a random port. // a random port.
func serve(server *rpc.Server) (err error) { 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 there is no explicit number of Go threads to use, then set it
if os.Getenv("GOMAXPROCS") == "" { if os.Getenv("GOMAXPROCS") == "" {
runtime.GOMAXPROCS(runtime.NumCPU()) runtime.GOMAXPROCS(runtime.NumCPU())