2014-09-21 18:21:54 +00:00
|
|
|
package agent
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/base64"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2014-10-03 05:05:00 +00:00
|
|
|
"path/filepath"
|
2014-09-21 18:21:54 +00:00
|
|
|
|
2014-09-24 23:39:14 +00:00
|
|
|
"github.com/hashicorp/consul/consul/structs"
|
2014-09-21 18:21:54 +00:00
|
|
|
"github.com/hashicorp/memberlist"
|
|
|
|
"github.com/hashicorp/serf/serf"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2014-10-03 05:05:00 +00:00
|
|
|
serfLANKeyring = "serf/local.keyring"
|
|
|
|
serfWANKeyring = "serf/remote.keyring"
|
2014-09-21 18:21:54 +00:00
|
|
|
)
|
|
|
|
|
2014-10-10 18:13:30 +00:00
|
|
|
// initKeyring will create a keyring file at a given path.
|
|
|
|
func initKeyring(path, key string) error {
|
2014-10-04 20:43:10 +00:00
|
|
|
var keys []string
|
|
|
|
|
2016-06-29 04:19:18 +00:00
|
|
|
if keyBytes, err := base64.StdEncoding.DecodeString(key); err != nil {
|
|
|
|
return fmt.Errorf("Invalid key: %s", err)
|
|
|
|
} else if err := memberlist.ValidateKey(keyBytes); err != nil {
|
2014-10-10 18:13:30 +00:00
|
|
|
return fmt.Errorf("Invalid key: %s", err)
|
2014-10-03 05:05:00 +00:00
|
|
|
}
|
|
|
|
|
2014-10-09 22:28:38 +00:00
|
|
|
// Just exit if the file already exists.
|
2014-10-04 20:43:10 +00:00
|
|
|
if _, err := os.Stat(path); err == nil {
|
2014-10-10 18:13:30 +00:00
|
|
|
return nil
|
2014-10-04 20:43:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
keys = append(keys, key)
|
2014-10-03 05:05:00 +00:00
|
|
|
keyringBytes, err := json.Marshal(keys)
|
|
|
|
if err != nil {
|
2014-10-10 18:13:30 +00:00
|
|
|
return err
|
2014-10-03 05:05:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := os.MkdirAll(filepath.Dir(path), 0700); err != nil {
|
2014-10-10 18:13:30 +00:00
|
|
|
return err
|
2014-10-03 05:05:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fh, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0600)
|
|
|
|
if err != nil {
|
2014-10-10 18:13:30 +00:00
|
|
|
return err
|
2014-10-03 05:05:00 +00:00
|
|
|
}
|
|
|
|
defer fh.Close()
|
|
|
|
|
|
|
|
if _, err := fh.Write(keyringBytes); err != nil {
|
|
|
|
os.Remove(path)
|
2014-10-10 18:13:30 +00:00
|
|
|
return err
|
2014-10-03 05:05:00 +00:00
|
|
|
}
|
|
|
|
|
2014-10-10 18:13:30 +00:00
|
|
|
return nil
|
2014-10-03 05:05:00 +00:00
|
|
|
}
|
|
|
|
|
2014-09-21 18:21:54 +00:00
|
|
|
// loadKeyringFile will load a gossip encryption keyring out of a file. The file
|
|
|
|
// must be in JSON format and contain a list of encryption key strings.
|
|
|
|
func loadKeyringFile(c *serf.Config) error {
|
|
|
|
if c.KeyringFile == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := os.Stat(c.KeyringFile); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read in the keyring file data
|
|
|
|
keyringData, err := ioutil.ReadFile(c.KeyringFile)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Decode keyring JSON
|
|
|
|
keys := make([]string, 0)
|
|
|
|
if err := json.Unmarshal(keyringData, &keys); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Decode base64 values
|
|
|
|
keysDecoded := make([][]byte, len(keys))
|
|
|
|
for i, key := range keys {
|
|
|
|
keyBytes, err := base64.StdEncoding.DecodeString(key)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
keysDecoded[i] = keyBytes
|
|
|
|
}
|
|
|
|
|
2014-10-03 00:14:52 +00:00
|
|
|
// Guard against empty keyring
|
|
|
|
if len(keysDecoded) == 0 {
|
|
|
|
return fmt.Errorf("no keys present in keyring file: %s", c.KeyringFile)
|
|
|
|
}
|
|
|
|
|
2014-09-21 18:21:54 +00:00
|
|
|
// Create the keyring
|
|
|
|
keyring, err := memberlist.NewKeyring(keysDecoded, keysDecoded[0])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
c.MemberlistConfig.Keyring = keyring
|
|
|
|
|
|
|
|
// Success!
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-09-24 23:39:14 +00:00
|
|
|
// keyringProcess is used to abstract away the semantic similarities in
|
|
|
|
// performing various operations on the encryption keyring.
|
2014-10-06 22:14:30 +00:00
|
|
|
func (a *Agent) keyringProcess(args *structs.KeyringRequest) (*structs.KeyringResponses, error) {
|
2014-09-25 01:30:34 +00:00
|
|
|
var reply structs.KeyringResponses
|
2014-09-24 23:39:14 +00:00
|
|
|
if a.server == nil {
|
|
|
|
return nil, fmt.Errorf("keyring operations must run against a server node")
|
2014-09-21 18:21:54 +00:00
|
|
|
}
|
2014-10-06 22:14:30 +00:00
|
|
|
if err := a.RPC("Internal.KeyringOperation", args, &reply); err != nil {
|
2014-09-24 23:39:14 +00:00
|
|
|
return &reply, err
|
2014-09-21 18:21:54 +00:00
|
|
|
}
|
|
|
|
|
2014-09-24 23:39:14 +00:00
|
|
|
return &reply, nil
|
2014-09-21 18:21:54 +00:00
|
|
|
}
|
|
|
|
|
2017-02-02 02:42:41 +00:00
|
|
|
// ParseRelayFactor validates and converts the given relay factor to uint8
|
|
|
|
func ParseRelayFactor(n int) (uint8, error) {
|
|
|
|
if n < 0 || n > 5 {
|
|
|
|
return 0, fmt.Errorf("Relay factor must be in range: [0, 5]")
|
|
|
|
}
|
|
|
|
return uint8(n), nil
|
|
|
|
}
|
|
|
|
|
2014-09-24 23:39:14 +00:00
|
|
|
// ListKeys lists out all keys installed on the collective Consul cluster. This
|
|
|
|
// includes both servers and clients in all DC's.
|
2017-02-02 02:42:41 +00:00
|
|
|
func (a *Agent) ListKeys(token string, relayFactor uint8) (*structs.KeyringResponses, error) {
|
2014-10-03 01:12:01 +00:00
|
|
|
args := structs.KeyringRequest{Operation: structs.KeyringList}
|
2017-02-02 02:42:41 +00:00
|
|
|
parseKeyringRequest(&args, token, relayFactor)
|
2014-10-06 22:14:30 +00:00
|
|
|
return a.keyringProcess(&args)
|
2014-09-21 18:21:54 +00:00
|
|
|
}
|
|
|
|
|
2014-09-24 23:39:14 +00:00
|
|
|
// InstallKey installs a new gossip encryption key
|
2017-02-02 02:42:41 +00:00
|
|
|
func (a *Agent) InstallKey(key, token string, relayFactor uint8) (*structs.KeyringResponses, error) {
|
2014-10-03 01:12:01 +00:00
|
|
|
args := structs.KeyringRequest{Key: key, Operation: structs.KeyringInstall}
|
2017-02-02 02:42:41 +00:00
|
|
|
parseKeyringRequest(&args, token, relayFactor)
|
2014-10-06 22:14:30 +00:00
|
|
|
return a.keyringProcess(&args)
|
2014-09-21 18:21:54 +00:00
|
|
|
}
|
|
|
|
|
2014-09-24 23:39:14 +00:00
|
|
|
// UseKey changes the primary encryption key used to encrypt messages
|
2017-02-02 02:42:41 +00:00
|
|
|
func (a *Agent) UseKey(key, token string, relayFactor uint8) (*structs.KeyringResponses, error) {
|
2014-10-03 01:12:01 +00:00
|
|
|
args := structs.KeyringRequest{Key: key, Operation: structs.KeyringUse}
|
2017-02-02 02:42:41 +00:00
|
|
|
parseKeyringRequest(&args, token, relayFactor)
|
2014-10-06 22:14:30 +00:00
|
|
|
return a.keyringProcess(&args)
|
2014-09-21 18:21:54 +00:00
|
|
|
}
|
|
|
|
|
2014-09-24 23:39:14 +00:00
|
|
|
// RemoveKey will remove a gossip encryption key from the keyring
|
2017-02-02 02:42:41 +00:00
|
|
|
func (a *Agent) RemoveKey(key, token string, relayFactor uint8) (*structs.KeyringResponses, error) {
|
2014-10-03 01:12:01 +00:00
|
|
|
args := structs.KeyringRequest{Key: key, Operation: structs.KeyringRemove}
|
2017-02-02 02:42:41 +00:00
|
|
|
parseKeyringRequest(&args, token, relayFactor)
|
2014-10-06 22:14:30 +00:00
|
|
|
return a.keyringProcess(&args)
|
2014-09-21 18:21:54 +00:00
|
|
|
}
|
2017-02-02 02:42:41 +00:00
|
|
|
|
|
|
|
func parseKeyringRequest(req *structs.KeyringRequest, token string, relayFactor uint8) {
|
|
|
|
req.Token = token
|
|
|
|
req.RelayFactor = relayFactor
|
|
|
|
}
|