mirror of
https://github.com/status-im/consul.git
synced 2025-01-09 21:35:52 +00:00
8cbeb29e73
* Fix theoretical cache collision bug if/when we use more cache types with same result type * Generalized fix for blocking query handling when state store methods return zero index * Refactor test retry to only affect CI * Undo make file merge * Add hint to error message returned to end-user requests if Connect is not enabled when they try to request cert * Explicit error for Roots endpoint if connect is disabled * Fix tests that were asserting old behaviour
115 lines
2.8 KiB
Go
115 lines
2.8 KiB
Go
package agent
|
|
|
|
import (
|
|
"bytes"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/hashicorp/consul/agent/connect"
|
|
ca "github.com/hashicorp/consul/agent/connect/ca"
|
|
"github.com/hashicorp/consul/agent/structs"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestConnectCARoots_empty(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
require := require.New(t)
|
|
a := NewTestAgent(t.Name(), "connect { enabled = false }")
|
|
defer a.Shutdown()
|
|
|
|
req, _ := http.NewRequest("GET", "/v1/connect/ca/roots", nil)
|
|
resp := httptest.NewRecorder()
|
|
_, err := a.srv.ConnectCARoots(resp, req)
|
|
require.Error(err)
|
|
require.Contains(err.Error(), "Connect must be enabled")
|
|
}
|
|
|
|
func TestConnectCARoots_list(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
assert := assert.New(t)
|
|
a := NewTestAgent(t.Name(), "")
|
|
defer a.Shutdown()
|
|
|
|
// Set some CAs. Note that NewTestAgent already bootstraps one CA so this just
|
|
// adds a second and makes it active.
|
|
ca2 := connect.TestCAConfigSet(t, a, nil)
|
|
|
|
// List
|
|
req, _ := http.NewRequest("GET", "/v1/connect/ca/roots", nil)
|
|
resp := httptest.NewRecorder()
|
|
obj, err := a.srv.ConnectCARoots(resp, req)
|
|
assert.NoError(err)
|
|
|
|
value := obj.(structs.IndexedCARoots)
|
|
assert.Equal(value.ActiveRootID, ca2.ID)
|
|
assert.Len(value.Roots, 2)
|
|
|
|
// We should never have the secret information
|
|
for _, r := range value.Roots {
|
|
assert.Equal("", r.SigningCert)
|
|
assert.Equal("", r.SigningKey)
|
|
}
|
|
}
|
|
|
|
func TestConnectCAConfig(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
assert := assert.New(t)
|
|
a := NewTestAgent(t.Name(), "")
|
|
defer a.Shutdown()
|
|
|
|
expected := &structs.ConsulCAProviderConfig{
|
|
RotationPeriod: 90 * 24 * time.Hour,
|
|
}
|
|
|
|
// Get the initial config.
|
|
{
|
|
req, _ := http.NewRequest("GET", "/v1/connect/ca/configuration", nil)
|
|
resp := httptest.NewRecorder()
|
|
obj, err := a.srv.ConnectCAConfiguration(resp, req)
|
|
assert.NoError(err)
|
|
|
|
value := obj.(structs.CAConfiguration)
|
|
parsed, err := ca.ParseConsulCAConfig(value.Config)
|
|
assert.NoError(err)
|
|
assert.Equal("consul", value.Provider)
|
|
assert.Equal(expected, parsed)
|
|
}
|
|
|
|
// Set the config.
|
|
{
|
|
body := bytes.NewBuffer([]byte(`
|
|
{
|
|
"Provider": "consul",
|
|
"Config": {
|
|
"RotationPeriod": 3600000000000
|
|
}
|
|
}`))
|
|
req, _ := http.NewRequest("PUT", "/v1/connect/ca/configuration", body)
|
|
resp := httptest.NewRecorder()
|
|
_, err := a.srv.ConnectCAConfiguration(resp, req)
|
|
assert.NoError(err)
|
|
}
|
|
|
|
// The config should be updated now.
|
|
{
|
|
expected.RotationPeriod = time.Hour
|
|
req, _ := http.NewRequest("GET", "/v1/connect/ca/configuration", nil)
|
|
resp := httptest.NewRecorder()
|
|
obj, err := a.srv.ConnectCAConfiguration(resp, req)
|
|
assert.NoError(err)
|
|
|
|
value := obj.(structs.CAConfiguration)
|
|
parsed, err := ca.ParseConsulCAConfig(value.Config)
|
|
assert.NoError(err)
|
|
assert.Equal("consul", value.Provider)
|
|
assert.Equal(expected, parsed)
|
|
}
|
|
}
|