Skip to content

Commit

Permalink
removing more constants
Browse files Browse the repository at this point in the history
  • Loading branch information
mitchellh committed Jan 21, 2016
1 parent 39f9a7e commit 47e680b
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 30 deletions.
14 changes: 5 additions & 9 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,11 @@ type Client struct {
// plugin client. After being used to initialize a plugin client,
// that configuration must not be modified again.
type ClientConfig struct {
// Plugins are the plugins that support being dispensed. This should
// match the server side. If it doesn't, the ProtocolVersion should
// be incremented.
Plugins map[string]Plugin
// HandshakeConfig is the configuration that must match servers.
HandshakeConfig

// ProtocolVersion is the version that the client expects the
// server to be speaking at a protocol layer. This should be set
// and match the plugin side.
ProtocolVersion uint
// Plugins are the plugins that can be consumed.
Plugins map[string]Plugin

// The unstarted subprocess for starting the plugin.
Cmd *exec.Cmd
Expand Down Expand Up @@ -238,7 +234,7 @@ func (c *Client) Start() (addr net.Addr, err error) {
c.doneLogging = make(chan struct{})

env := []string{
fmt.Sprintf("%s=%s", MagicCookieKey, MagicCookieValue),
fmt.Sprintf("%s=%s", c.config.MagicCookieKey, c.config.MagicCookieValue),
fmt.Sprintf("PLUGIN_MIN_PORT=%d", c.config.MinPort),
fmt.Sprintf("PLUGIN_MAX_PORT=%d", c.config.MaxPort),
}
Expand Down
10 changes: 5 additions & 5 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

func TestClient(t *testing.T) {
process := helperProcess("mock")
c := NewClient(&ClientConfig{Cmd: process, ProtocolVersion: testAPIVersion})
c := NewClient(&ClientConfig{Cmd: process, HandshakeConfig: testHandshake})
defer c.Kill()

// Test that it parses the proper address
Expand Down Expand Up @@ -45,7 +45,7 @@ func TestClientStart_badVersion(t *testing.T) {
config := &ClientConfig{
Cmd: helperProcess("bad-version"),
StartTimeout: 50 * time.Millisecond,
ProtocolVersion: testAPIVersion,
HandshakeConfig: testHandshake,
}

c := NewClient(config)
Expand All @@ -61,7 +61,7 @@ func TestClient_Start_Timeout(t *testing.T) {
config := &ClientConfig{
Cmd: helperProcess("start-timeout"),
StartTimeout: 50 * time.Millisecond,
ProtocolVersion: testAPIVersion,
HandshakeConfig: testHandshake,
}

c := NewClient(config)
Expand All @@ -79,7 +79,7 @@ func TestClient_Stderr(t *testing.T) {
c := NewClient(&ClientConfig{
Cmd: process,
Stderr: stderr,
ProtocolVersion: testAPIVersion,
HandshakeConfig: testHandshake,
})
defer c.Kill()

Expand Down Expand Up @@ -126,7 +126,7 @@ func TestClient_Stdin(t *testing.T) {
os.Stdin = tf

process := helperProcess("stdin")
c := NewClient(&ClientConfig{Cmd: process, ProtocolVersion: testAPIVersion})
c := NewClient(&ClientConfig{Cmd: process, HandshakeConfig: testHandshake})
defer c.Kill()

_, err = c.Start()
Expand Down
14 changes: 9 additions & 5 deletions plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ import (
)

// testAPIVersion is the ProtocolVersion we use for testing.
const testAPIVersion uint = 1
var testHandshake = HandshakeConfig{
ProtocolVersion: 1,
MagicCookieKey: "TEST_MAGIC_COOKIE",
MagicCookieValue: "test",
}

// testInterface is the test interface we use for plugins.
type testInterface interface {
Expand Down Expand Up @@ -102,22 +106,22 @@ func TestHelperProcess(*testing.T) {
cmd, args := args[0], args[1:]
switch cmd {
case "bad-version":
fmt.Printf("%d1|tcp|:1234\n", testAPIVersion)
fmt.Printf("%d1|tcp|:1234\n", testHandshake.ProtocolVersion)
<-make(chan int)
case "invalid-rpc-address":
fmt.Println("lolinvalid")
case "mock":
fmt.Printf("%d|tcp|:1234\n", testAPIVersion)
fmt.Printf("%d|tcp|:1234\n", testHandshake.ProtocolVersion)
<-make(chan int)
case "start-timeout":
time.Sleep(1 * time.Minute)
os.Exit(1)
case "stderr":
fmt.Printf("%d|tcp|:1234\n", testAPIVersion)
fmt.Printf("%d|tcp|:1234\n", testHandshake.ProtocolVersion)
log.Println("HELLO")
log.Println("WORLD")
case "stdin":
fmt.Printf("%d|tcp|:1234\n", testAPIVersion)
fmt.Printf("%d|tcp|:1234\n", testHandshake.ProtocolVersion)
data := make([]byte, 5)
if _, err := os.Stdin.Read(data); err != nil {
log.Printf("stdin read error: %s", err)
Expand Down
34 changes: 23 additions & 11 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,33 @@ import (
"sync/atomic"
)

// The "magic cookie" is used to verify that the user intended to
// actually run this binary. If this cookie isn't present as an
// environmental variable, then we bail out early with an error.
const MagicCookieKey = "OTTO_PLUGIN_MAGIC_COOKIE"
const MagicCookieValue = "11aab7ff21cb9ff7b0e9975d53f17a8dab571eac9b5ff0191730046698f07b7f"
// HandshakeConfig is the configuration used by client and servers to
// handshake before starting a plugin connection. This is embedded by
// both ServeConfig and ClientConfig.
//
// In practice, the plugin host creates a HandshakeConfig that is exported
// and plugins then can easily consume it.
type HandshakeConfig struct {
// ProtocolVersion is the version that clients must match on to
// agree they can communicate. This should match the ProtocolVersion
// set on ClientConfig when using a plugin.
ProtocolVersion uint

// MagicCookieKey and value are used as a very basic verification
// that a plugin is intended to be launched. This is not a security
// measure, just a UX feature. If the magic cookie doesn't match,
// we show human-friendly output.
MagicCookieKey string
MagicCookieValue string
}

// ServeConfig configures what sorts of plugins are served.
type ServeConfig struct {
// HandshakeConfig is the configuration that must match clients.
HandshakeConfig

// Plugins are the plugins that are served.
Plugins map[string]Plugin

// ProtocolVersion is the version that clients must match on to
// agree they can communicate. This should match the ProtocolVersion
// set on ClientConfig when using a plugin.
ProtocolVersion uint
}

// Serve serves the plugins given by ServeConfig.
Expand All @@ -36,7 +48,7 @@ type ServeConfig struct {
// errors will be outputted to the log.
func Serve(opts *ServeConfig) {
// First check the cookie
if os.Getenv(MagicCookieKey) != MagicCookieValue {
if os.Getenv(opts.MagicCookieKey) != opts.MagicCookieValue {
fmt.Fprintf(os.Stderr,
"This binary is a plugin. These are not meant to be executed directly.\n"+
"Please execute the program that consumes these plugins, which will\n"+
Expand Down

0 comments on commit 47e680b

Please sign in to comment.