2014-09-21 18:21:54 +00:00
|
|
|
package agent
|
|
|
|
|
|
|
|
import (
|
2014-10-09 22:28:38 +00:00
|
|
|
"fmt"
|
2014-10-04 20:43:10 +00:00
|
|
|
"io/ioutil"
|
2014-09-21 18:21:54 +00:00
|
|
|
"os"
|
2014-10-04 20:43:10 +00:00
|
|
|
"path/filepath"
|
2015-07-07 21:14:06 +00:00
|
|
|
"strings"
|
2014-09-21 18:21:54 +00:00
|
|
|
"testing"
|
2015-07-07 21:14:06 +00:00
|
|
|
|
2017-05-12 13:41:13 +00:00
|
|
|
"github.com/hashicorp/consul/testutil"
|
2014-09-21 18:21:54 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestAgent_LoadKeyrings(t *testing.T) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2014-09-21 18:21:54 +00:00
|
|
|
key := "tbLJg26ZJyJ9pK3qhc9jig=="
|
|
|
|
|
|
|
|
// Should be no configured keyring file by default
|
2017-05-21 07:11:09 +00:00
|
|
|
a1 := NewTestAgent(t.Name(), nil)
|
|
|
|
defer a1.Shutdown()
|
2014-09-21 18:21:54 +00:00
|
|
|
|
2017-05-21 07:11:09 +00:00
|
|
|
c1 := a1.Config.ConsulConfig
|
|
|
|
if c1.SerfLANConfig.KeyringFile != "" {
|
|
|
|
t.Fatalf("bad: %#v", c1.SerfLANConfig.KeyringFile)
|
2014-09-21 18:21:54 +00:00
|
|
|
}
|
2017-05-21 07:11:09 +00:00
|
|
|
if c1.SerfLANConfig.MemberlistConfig.Keyring != nil {
|
2014-09-21 18:21:54 +00:00
|
|
|
t.Fatalf("keyring should not be loaded")
|
|
|
|
}
|
2017-05-21 07:11:09 +00:00
|
|
|
if c1.SerfWANConfig.KeyringFile != "" {
|
|
|
|
t.Fatalf("bad: %#v", c1.SerfLANConfig.KeyringFile)
|
2014-09-21 18:21:54 +00:00
|
|
|
}
|
2017-05-21 07:11:09 +00:00
|
|
|
if c1.SerfWANConfig.MemberlistConfig.Keyring != nil {
|
2014-09-21 18:21:54 +00:00
|
|
|
t.Fatalf("keyring should not be loaded")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Server should auto-load LAN and WAN keyring files
|
2017-05-21 07:11:09 +00:00
|
|
|
a2 := &TestAgent{Name: t.Name(), Key: key}
|
|
|
|
a2.Start()
|
|
|
|
defer a2.Shutdown()
|
2014-09-21 18:21:54 +00:00
|
|
|
|
2017-05-21 07:11:09 +00:00
|
|
|
c2 := a2.Config.ConsulConfig
|
|
|
|
if c2.SerfLANConfig.KeyringFile == "" {
|
2014-09-21 18:21:54 +00:00
|
|
|
t.Fatalf("should have keyring file")
|
|
|
|
}
|
2017-05-21 07:11:09 +00:00
|
|
|
if c2.SerfLANConfig.MemberlistConfig.Keyring == nil {
|
2014-09-21 18:21:54 +00:00
|
|
|
t.Fatalf("keyring should be loaded")
|
|
|
|
}
|
2017-05-21 07:11:09 +00:00
|
|
|
if c2.SerfWANConfig.KeyringFile == "" {
|
2014-09-21 18:21:54 +00:00
|
|
|
t.Fatalf("should have keyring file")
|
|
|
|
}
|
2017-05-21 07:11:09 +00:00
|
|
|
if c2.SerfWANConfig.MemberlistConfig.Keyring == nil {
|
2014-09-21 18:21:54 +00:00
|
|
|
t.Fatalf("keyring should be loaded")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Client should auto-load only the LAN keyring file
|
2017-05-21 07:11:09 +00:00
|
|
|
conf3 := TestConfig()
|
2014-09-21 18:21:54 +00:00
|
|
|
conf3.Server = false
|
2017-05-21 07:11:09 +00:00
|
|
|
a3 := &TestAgent{Name: t.Name(), Config: conf3, Key: key}
|
|
|
|
a3.Start()
|
|
|
|
defer a3.Shutdown()
|
2014-09-21 18:21:54 +00:00
|
|
|
|
2017-05-21 07:11:09 +00:00
|
|
|
c3 := a3.Config.ConsulConfig
|
|
|
|
if c3.SerfLANConfig.KeyringFile == "" {
|
2014-09-21 18:21:54 +00:00
|
|
|
t.Fatalf("should have keyring file")
|
|
|
|
}
|
2017-05-21 07:11:09 +00:00
|
|
|
if c3.SerfLANConfig.MemberlistConfig.Keyring == nil {
|
2014-09-21 18:21:54 +00:00
|
|
|
t.Fatalf("keyring should be loaded")
|
|
|
|
}
|
2017-05-21 07:11:09 +00:00
|
|
|
if c3.SerfWANConfig.KeyringFile != "" {
|
|
|
|
t.Fatalf("bad: %#v", c3.SerfWANConfig.KeyringFile)
|
2014-09-21 18:21:54 +00:00
|
|
|
}
|
2017-05-21 07:11:09 +00:00
|
|
|
if c3.SerfWANConfig.MemberlistConfig.Keyring != nil {
|
2014-09-21 18:21:54 +00:00
|
|
|
t.Fatalf("keyring should not be loaded")
|
|
|
|
}
|
|
|
|
}
|
2014-10-04 20:43:10 +00:00
|
|
|
|
|
|
|
func TestAgent_InitKeyring(t *testing.T) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2014-10-04 20:43:10 +00:00
|
|
|
key1 := "tbLJg26ZJyJ9pK3qhc9jig=="
|
|
|
|
key2 := "4leC33rgtXKIVUr9Nr0snQ=="
|
2014-10-09 22:28:38 +00:00
|
|
|
expected := fmt.Sprintf(`["%s"]`, key1)
|
2014-10-04 20:43:10 +00:00
|
|
|
|
2017-05-12 13:41:13 +00:00
|
|
|
dir := testutil.TempDir(t, "consul")
|
2014-10-04 20:43:10 +00:00
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
|
|
|
|
file := filepath.Join(dir, "keyring")
|
|
|
|
|
|
|
|
// First initialize the keyring
|
2014-10-10 18:13:30 +00:00
|
|
|
if err := initKeyring(file, key1); err != nil {
|
2014-10-04 20:43:10 +00:00
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2014-10-09 22:28:38 +00:00
|
|
|
content, err := ioutil.ReadFile(file)
|
2014-10-04 20:43:10 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
2014-10-09 22:28:38 +00:00
|
|
|
if string(content) != expected {
|
|
|
|
t.Fatalf("bad: %s", content)
|
2014-10-04 20:43:10 +00:00
|
|
|
}
|
|
|
|
|
2014-10-09 22:28:38 +00:00
|
|
|
// Try initializing again with a different key
|
2014-10-10 18:13:30 +00:00
|
|
|
if err := initKeyring(file, key2); err != nil {
|
2014-10-04 20:43:10 +00:00
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2014-10-10 18:13:30 +00:00
|
|
|
// Content should still be the same
|
2014-10-09 22:28:38 +00:00
|
|
|
content, err = ioutil.ReadFile(file)
|
|
|
|
if err != nil {
|
2014-10-04 20:43:10 +00:00
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
2014-10-09 22:28:38 +00:00
|
|
|
if string(content) != expected {
|
|
|
|
t.Fatalf("bad: %s", content)
|
2014-10-04 20:43:10 +00:00
|
|
|
}
|
|
|
|
}
|
2015-07-07 21:14:06 +00:00
|
|
|
|
|
|
|
func TestAgentKeyring_ACL(t *testing.T) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2015-07-07 21:14:06 +00:00
|
|
|
key1 := "tbLJg26ZJyJ9pK3qhc9jig=="
|
|
|
|
key2 := "4leC33rgtXKIVUr9Nr0snQ=="
|
|
|
|
|
2017-05-21 07:11:09 +00:00
|
|
|
conf := TestACLConfig()
|
2015-07-07 21:14:06 +00:00
|
|
|
conf.ACLDatacenter = "dc1"
|
|
|
|
conf.ACLMasterToken = "root"
|
|
|
|
conf.ACLDefaultPolicy = "deny"
|
2017-05-21 07:11:09 +00:00
|
|
|
a := &TestAgent{Name: t.Name(), Config: conf, Key: key1}
|
|
|
|
a.Start()
|
|
|
|
defer a.Shutdown()
|
2015-07-07 21:14:06 +00:00
|
|
|
|
|
|
|
// List keys without access fails
|
2017-05-21 07:11:09 +00:00
|
|
|
_, err := a.ListKeys("", 0)
|
2015-07-07 21:14:06 +00:00
|
|
|
if err == nil || !strings.Contains(err.Error(), "denied") {
|
|
|
|
t.Fatalf("expected denied error, got: %#v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// List keys with access works
|
2017-05-21 07:11:09 +00:00
|
|
|
_, err = a.ListKeys("root", 0)
|
2015-07-07 21:14:06 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Install without access fails
|
2017-05-21 07:11:09 +00:00
|
|
|
_, err = a.InstallKey(key2, "", 0)
|
2015-07-07 21:14:06 +00:00
|
|
|
if err == nil || !strings.Contains(err.Error(), "denied") {
|
|
|
|
t.Fatalf("expected denied error, got: %#v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Install with access works
|
2017-05-21 07:11:09 +00:00
|
|
|
_, err = a.InstallKey(key2, "root", 0)
|
2015-07-07 21:14:06 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Use without access fails
|
2017-05-21 07:11:09 +00:00
|
|
|
_, err = a.UseKey(key2, "", 0)
|
2015-07-07 21:14:06 +00:00
|
|
|
if err == nil || !strings.Contains(err.Error(), "denied") {
|
|
|
|
t.Fatalf("expected denied error, got: %#v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Use with access works
|
2017-05-21 07:11:09 +00:00
|
|
|
_, err = a.UseKey(key2, "root", 0)
|
2015-07-07 21:14:06 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove without access fails
|
2017-05-21 07:11:09 +00:00
|
|
|
_, err = a.RemoveKey(key1, "", 0)
|
2015-07-07 21:14:06 +00:00
|
|
|
if err == nil || !strings.Contains(err.Error(), "denied") {
|
|
|
|
t.Fatalf("expected denied error, got: %#v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove with access works
|
2017-05-21 07:11:09 +00:00
|
|
|
_, err = a.RemoveKey(key1, "root", 0)
|
2015-07-07 21:14:06 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
}
|