2018-03-27 23:50:17 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
2018-06-22 16:48:41 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/pascaldekloe/goe/verify"
|
2018-03-27 23:50:17 +00:00
|
|
|
|
2019-03-26 21:04:58 +00:00
|
|
|
"github.com/hashicorp/consul/internal/testutil"
|
|
|
|
"github.com/hashicorp/consul/internal/testutil/retry"
|
2018-03-27 23:50:17 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestAPI_ConnectCARoots_empty(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
require := require.New(t)
|
2018-05-10 16:04:33 +00:00
|
|
|
c, s := makeClientWithConfig(t, nil, func(c *testutil.TestServerConfig) {
|
|
|
|
// Don't bootstrap CA
|
|
|
|
c.Connect = nil
|
|
|
|
})
|
2018-03-27 23:50:17 +00:00
|
|
|
defer s.Stop()
|
|
|
|
|
|
|
|
connect := c.Connect()
|
2018-07-25 19:26:27 +00:00
|
|
|
_, _, err := connect.CARoots(nil)
|
|
|
|
|
|
|
|
require.Error(err)
|
|
|
|
require.Contains(err.Error(), "Connect must be enabled")
|
2018-05-10 16:04:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestAPI_ConnectCARoots_list(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
c, s := makeClient(t)
|
|
|
|
defer s.Stop()
|
|
|
|
|
|
|
|
// This fails occasionally if server doesn't have time to bootstrap CA so
|
|
|
|
// retry
|
|
|
|
retry.Run(t, func(r *retry.R) {
|
|
|
|
connect := c.Connect()
|
|
|
|
list, meta, err := connect.CARoots(nil)
|
|
|
|
r.Check(err)
|
|
|
|
if meta.LastIndex <= 0 {
|
|
|
|
r.Fatalf("expected roots raft index to be > 0")
|
|
|
|
}
|
|
|
|
if v := len(list.Roots); v != 1 {
|
|
|
|
r.Fatalf("expected 1 root, got %d", v)
|
|
|
|
}
|
|
|
|
// connect.TestClusterID causes import cycle so hard code it
|
|
|
|
if list.TrustDomain != "11111111-2222-3333-4444-555555555555.consul" {
|
|
|
|
r.Fatalf("expected fixed trust domain got '%s'", list.TrustDomain)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2018-03-27 23:50:17 +00:00
|
|
|
}
|
2018-05-23 21:44:24 +00:00
|
|
|
|
|
|
|
func TestAPI_ConnectCAConfig_get_set(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
c, s := makeClient(t)
|
|
|
|
defer s.Stop()
|
|
|
|
|
2018-06-22 16:48:41 +00:00
|
|
|
expected := &ConsulCAProviderConfig{
|
|
|
|
RotationPeriod: 90 * 24 * time.Hour,
|
|
|
|
}
|
2018-07-16 09:46:10 +00:00
|
|
|
expected.LeafCertTTL = 72 * time.Hour
|
2018-06-22 16:48:41 +00:00
|
|
|
|
2018-05-23 21:44:24 +00:00
|
|
|
// This fails occasionally if server doesn't have time to bootstrap CA so
|
|
|
|
// retry
|
|
|
|
retry.Run(t, func(r *retry.R) {
|
|
|
|
connect := c.Connect()
|
|
|
|
|
|
|
|
conf, _, err := connect.CAGetConfig(nil)
|
|
|
|
r.Check(err)
|
|
|
|
if conf.Provider != "consul" {
|
|
|
|
r.Fatalf("expected default provider, got %q", conf.Provider)
|
|
|
|
}
|
2018-06-22 16:48:41 +00:00
|
|
|
parsed, err := ParseConsulCAConfig(conf.Config)
|
2018-05-23 21:44:24 +00:00
|
|
|
r.Check(err)
|
2018-06-22 16:48:41 +00:00
|
|
|
verify.Values(r, "", parsed, expected)
|
2018-05-23 21:44:24 +00:00
|
|
|
|
|
|
|
// Change a config value and update
|
2018-06-22 16:48:41 +00:00
|
|
|
conf.Config["PrivateKey"] = ""
|
|
|
|
conf.Config["RotationPeriod"] = 120 * 24 * time.Hour
|
2018-05-23 21:44:24 +00:00
|
|
|
_, err = connect.CASetConfig(conf, nil)
|
2018-06-22 16:48:41 +00:00
|
|
|
r.Check(err)
|
|
|
|
|
|
|
|
updated, _, err := connect.CAGetConfig(nil)
|
|
|
|
r.Check(err)
|
|
|
|
expected.RotationPeriod = 120 * 24 * time.Hour
|
|
|
|
parsed, err = ParseConsulCAConfig(updated.Config)
|
|
|
|
r.Check(err)
|
|
|
|
verify.Values(r, "", parsed, expected)
|
2018-05-23 21:44:24 +00:00
|
|
|
})
|
|
|
|
}
|