2016-10-17 17:48:04 +00:00
|
|
|
package agent
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
2020-10-01 15:13:06 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/go-hclog"
|
2016-10-17 17:48:04 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestAgent_LoadKeyrings(t *testing.T) {
|
2017-07-20 05:42:15 +00:00
|
|
|
t.Parallel()
|
2016-10-17 17:48:04 +00:00
|
|
|
key := "tbLJg26ZJyJ9pK3qhc9jig=="
|
|
|
|
|
|
|
|
// Should be no configured keyring file by default
|
2017-10-19 04:45:18 +00:00
|
|
|
agent1 := NewTestAgent(t, t.Name(), nil)
|
2016-10-17 17:48:04 +00:00
|
|
|
defer agent1.Shutdown()
|
|
|
|
|
|
|
|
c := agent1.server.GetConfig()
|
|
|
|
if c.SerfConfig.KeyringFile != "" {
|
|
|
|
t.Fatalf("bad: %#v", c.SerfConfig.KeyringFile)
|
|
|
|
}
|
|
|
|
if c.SerfConfig.MemberlistConfig.Keyring != nil {
|
|
|
|
t.Fatalf("keyring should not be loaded")
|
|
|
|
}
|
|
|
|
|
2017-07-20 05:42:15 +00:00
|
|
|
// Server should auto-load WAN keyring files
|
|
|
|
agent2 := &TestAgent{
|
2017-10-19 04:45:18 +00:00
|
|
|
T: t,
|
2017-07-20 05:42:15 +00:00
|
|
|
Name: t.Name() + "2",
|
|
|
|
Key: key,
|
|
|
|
}
|
|
|
|
agent2.Start()
|
2016-10-17 17:48:04 +00:00
|
|
|
defer agent2.Shutdown()
|
|
|
|
|
|
|
|
c = agent2.server.GetConfig()
|
|
|
|
if c.SerfConfig.KeyringFile == "" {
|
|
|
|
t.Fatalf("should have keyring file")
|
|
|
|
}
|
|
|
|
if c.SerfConfig.MemberlistConfig.Keyring == nil {
|
|
|
|
t.Fatalf("keyring should be loaded")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAgent_InitKeyring(t *testing.T) {
|
2017-07-20 05:42:15 +00:00
|
|
|
t.Parallel()
|
2016-10-17 17:48:04 +00:00
|
|
|
key1 := "tbLJg26ZJyJ9pK3qhc9jig=="
|
|
|
|
key2 := "4leC33rgtXKIVUr9Nr0snQ=="
|
|
|
|
expected := fmt.Sprintf(`["%s"]`, key1)
|
|
|
|
|
|
|
|
dir, err := ioutil.TempDir("", "nomad")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
|
|
|
|
file := filepath.Join(dir, "keyring")
|
|
|
|
|
2020-10-01 15:13:06 +00:00
|
|
|
logger := hclog.NewNullLogger()
|
|
|
|
|
2016-10-17 17:48:04 +00:00
|
|
|
// First initialize the keyring
|
2020-10-01 15:13:06 +00:00
|
|
|
if err := initKeyring(file, key1, logger); err != nil {
|
2016-10-17 17:48:04 +00:00
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
content, err := ioutil.ReadFile(file)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if string(content) != expected {
|
|
|
|
t.Fatalf("bad: %s", content)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try initializing again with a different key
|
2020-10-01 15:13:06 +00:00
|
|
|
if err := initKeyring(file, key2, logger); err != nil {
|
2016-10-17 17:48:04 +00:00
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Content should still be the same
|
|
|
|
content, err = ioutil.ReadFile(file)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if string(content) != expected {
|
|
|
|
t.Fatalf("bad: %s", content)
|
|
|
|
}
|
|
|
|
}
|