consul: use rpc layer only for key management functions, add rpc commands

This commit is contained in:
Ryan Uber 2014-09-08 18:09:51 -07:00
parent ed3562b809
commit 4dd1b42477
5 changed files with 111 additions and 18 deletions

View File

@ -739,3 +739,19 @@ func loadKeyringFile(keyringFile string) *memberlist.Keyring {
// Success! // Success!
return keyring return keyring
} }
// ListKeysLAN returns the keys installed on the LAN gossip pool
func (a *Agent) ListKeysLAN() map[string]int {
if a.server != nil {
return a.server.ListKeysLAN()
}
return a.client.ListKeysLAN()
}
// ListKeysWAN returns the keys installed on the WAN gossip pool
func (a *Agent) ListKeysWAN() map[string]int {
if a.server != nil {
return a.server.ListKeysWAN()
}
return nil
}

View File

@ -41,16 +41,24 @@ const (
) )
const ( const (
handshakeCommand = "handshake" handshakeCommand = "handshake"
forceLeaveCommand = "force-leave" forceLeaveCommand = "force-leave"
joinCommand = "join" joinCommand = "join"
membersLANCommand = "members-lan" membersLANCommand = "members-lan"
membersWANCommand = "members-wan" membersWANCommand = "members-wan"
stopCommand = "stop" stopCommand = "stop"
monitorCommand = "monitor" monitorCommand = "monitor"
leaveCommand = "leave" leaveCommand = "leave"
statsCommand = "stats" statsCommand = "stats"
reloadCommand = "reload" reloadCommand = "reload"
listKeysLANCommand = "list-keys-lan"
listKeysWANCommand = "list-keys-wan"
installKeyLANCommand = "install-key-lan"
installKeyWANCommand = "install-key-wan"
useKeyLANCommand = "use-key-lan"
useKeyWANCommand = "use-key-wan"
removeKeyLANCommand = "remove-key-lan"
removeKeyWANCommand = "remove-key-wan"
) )
const ( const (
@ -103,6 +111,13 @@ type joinResponse struct {
Num int32 Num int32
} }
type keysResponse struct {
Messages map[string]string
NumNodes int
NumResp int
Keys map[string]int
}
type membersResponse struct { type membersResponse struct {
Members []Member Members []Member
} }
@ -373,6 +388,32 @@ func (i *AgentRPC) handleRequest(client *rpcClient, reqHeader *requestHeader) er
case reloadCommand: case reloadCommand:
return i.handleReload(client, seq) return i.handleReload(client, seq)
case listKeysLANCommand:
return i.handleListKeysWAN(client, seq)
case listKeysWANCommand:
return i.handleListKeysLAN(client, seq)
/*
case installKeyLANCommand:
return i.handleInstallKeyLAN(client, seq)
case installKeyWANCommand:
return i.handleInstallKeyWAN(client, seq)
case useKeyLANCommand:
return i.handleUseKeyLAN(client, seq)
case useKeyWANCommand:
return i.handleUseKeyWAN(client, seq)
case removeKeyLANCommand:
return i.handleRemoveKeyLAN(client, seq)
case removeKeyWANCommand:
return i.handleRemoveKeyWAN(client, seq)
*/
default: default:
respHeader := responseHeader{Seq: seq, Error: unsupportedCommand} respHeader := responseHeader{Seq: seq, Error: unsupportedCommand}
client.Send(&respHeader, nil) client.Send(&respHeader, nil)
@ -583,6 +624,24 @@ func (i *AgentRPC) handleReload(client *rpcClient, seq uint64) error {
return client.Send(&resp, nil) return client.Send(&resp, nil)
} }
func (i *AgentRPC) handleListKeysLAN(client *rpcClient, seq uint64) error {
header := responseHeader{
Seq: seq,
Error: "",
}
resp := i.agent.ListKeysLAN()
return client.Send(&header, resp)
}
func (i *AgentRPC) handleListKeysWAN(client *rpcClient, seq uint64) error {
header := responseHeader{
Seq: seq,
Error: "",
}
resp := i.agent.ListKeysWAN()
return client.Send(&header, resp)
}
// Used to convert an error to a string representation // Used to convert an error to a string representation
func errToString(err error) string { func errToString(err error) string {
if err == nil { if err == nil {

View File

@ -3,8 +3,9 @@ package command
import ( import (
"flag" "flag"
"fmt" "fmt"
"github.com/mitchellh/cli"
"strings" "strings"
"github.com/mitchellh/cli"
) )
// KeysCommand is a Command implementation that handles querying, installing, // KeysCommand is a Command implementation that handles querying, installing,
@ -30,6 +31,13 @@ func (c *KeysCommand) Run(args []string) int {
return 1 return 1
} }
client, err := RPCClient(*rpcAddr)
if err != nil {
c.Ui.Error(fmt.Sprintf("Error connecting to Consul agent: %s", err))
return 1
}
defer client.Close()
// Only accept a single argument // Only accept a single argument
found := listKeys found := listKeys
for _, arg := range []string{installKey, useKey, removeKey} { for _, arg := range []string{installKey, useKey, removeKey} {
@ -40,14 +48,9 @@ func (c *KeysCommand) Run(args []string) int {
found = found || len(arg) > 0 found = found || len(arg) > 0
} }
client, err := RPCClient(*rpcAddr)
if err != nil {
c.Ui.Error(fmt.Sprintf("Error connecting to Consul agent: %s", err))
return 1
}
defer client.Close()
if listKeys { if listKeys {
km := client.KeyManager()
fmt.Println(km.ListKeys())
return 0 return 0
} }

View File

@ -206,6 +206,11 @@ func (c *Client) UserEvent(name string, payload []byte) error {
return c.serf.UserEvent(userEventName(name), payload, false) return c.serf.UserEvent(userEventName(name), payload, false)
} }
// KeyManager returns the Serf keyring manager
func (c *Client) KeyManager() *serf.KeyManager {
return c.serf.KeyManager()
}
// lanEventHandler is used to handle events from the lan Serf cluster // lanEventHandler is used to handle events from the lan Serf cluster
func (c *Client) lanEventHandler() { func (c *Client) lanEventHandler() {
for { for {

View File

@ -551,6 +551,16 @@ func (s *Server) IsLeader() bool {
return s.raft.State() == raft.Leader return s.raft.State() == raft.Leader
} }
// KeyManagerLAN returns the LAN Serf keyring manager
func (s *Server) KeyManagerLAN() *serf.KeyManager {
return s.serfLAN.KeyManager()
}
// KeyManagerWAN returns the WAN Serf keyring manager
func (s *Server) KeyManagerWAN() *serf.KeyManager {
return s.serfWAN.KeyManager()
}
// inmemCodec is used to do an RPC call without going over a network // inmemCodec is used to do an RPC call without going over a network
type inmemCodec struct { type inmemCodec struct {
method string method string