2020-06-05 19:56:19 +00:00
|
|
|
package consul
|
|
|
|
|
|
|
|
import (
|
2022-09-13 19:21:47 +00:00
|
|
|
"bytes"
|
|
|
|
"crypto"
|
2023-02-17 20:04:12 +00:00
|
|
|
"crypto/rand"
|
2020-07-28 19:31:48 +00:00
|
|
|
"crypto/x509"
|
2020-06-05 19:56:19 +00:00
|
|
|
"encoding/base64"
|
2022-09-13 19:21:47 +00:00
|
|
|
"encoding/pem"
|
2020-06-05 19:56:19 +00:00
|
|
|
"fmt"
|
|
|
|
"net"
|
2022-09-13 19:21:47 +00:00
|
|
|
"net/url"
|
2022-11-10 16:26:01 +00:00
|
|
|
"os"
|
2020-06-05 19:56:19 +00:00
|
|
|
"path"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
peering: initial sync (#12842)
- Add endpoints related to peering: read, list, generate token, initiate peering
- Update node/service/check table indexing to account for peers
- Foundational changes for pushing service updates to a peer
- Plumb peer name through Health.ServiceNodes path
see: ENT-1765, ENT-1280, ENT-1283, ENT-1283, ENT-1756, ENT-1739, ENT-1750, ENT-1679,
ENT-1709, ENT-1704, ENT-1690, ENT-1689, ENT-1702, ENT-1701, ENT-1683, ENT-1663,
ENT-1650, ENT-1678, ENT-1628, ENT-1658, ENT-1640, ENT-1637, ENT-1597, ENT-1634,
ENT-1613, ENT-1616, ENT-1617, ENT-1591, ENT-1588, ENT-1596, ENT-1572, ENT-1555
Co-authored-by: R.B. Boyer <rb@hashicorp.com>
Co-authored-by: freddygv <freddy@hashicorp.com>
Co-authored-by: Chris S. Kim <ckim@hashicorp.com>
Co-authored-by: Evan Culver <eculver@hashicorp.com>
Co-authored-by: Nitya Dhanushkodi <nitya@hashicorp.com>
2022-04-21 22:34:40 +00:00
|
|
|
msgpackrpc "github.com/hashicorp/consul-net-rpc/net-rpc-msgpackrpc"
|
2021-04-20 18:55:24 +00:00
|
|
|
"github.com/hashicorp/memberlist"
|
|
|
|
"github.com/stretchr/testify/mock"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2020-07-28 19:31:48 +00:00
|
|
|
"github.com/hashicorp/consul/agent/connect"
|
2020-06-05 19:56:19 +00:00
|
|
|
"github.com/hashicorp/consul/agent/structs"
|
|
|
|
"github.com/hashicorp/consul/internal/go-sso/oidcauth/oidcauthtest"
|
2023-02-17 21:14:46 +00:00
|
|
|
"github.com/hashicorp/consul/proto/private/pbautoconf"
|
|
|
|
"github.com/hashicorp/consul/proto/private/pbconfig"
|
|
|
|
"github.com/hashicorp/consul/proto/private/pbconnect"
|
|
|
|
"github.com/hashicorp/consul/proto/private/prototest"
|
2020-06-05 19:56:19 +00:00
|
|
|
"github.com/hashicorp/consul/sdk/testutil"
|
|
|
|
"github.com/hashicorp/consul/tlsutil"
|
2022-03-24 19:32:25 +00:00
|
|
|
"github.com/hashicorp/consul/types"
|
2020-06-05 19:56:19 +00:00
|
|
|
|
|
|
|
"gopkg.in/square/go-jose.v2/jwt"
|
|
|
|
)
|
|
|
|
|
2020-07-07 19:39:04 +00:00
|
|
|
type mockAutoConfigBackend struct {
|
|
|
|
mock.Mock
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockAutoConfigBackend) CreateACLToken(template *structs.ACLToken) (*structs.ACLToken, error) {
|
|
|
|
ret := m.Called(template)
|
|
|
|
// this handles converting an untyped nil to a typed nil
|
|
|
|
token, _ := ret.Get(0).(*structs.ACLToken)
|
|
|
|
return token, ret.Error(1)
|
|
|
|
}
|
|
|
|
|
2021-12-01 19:32:34 +00:00
|
|
|
func (m *mockAutoConfigBackend) DatacenterJoinAddresses(partition, segment string) ([]string, error) {
|
|
|
|
ret := m.Called(partition, segment)
|
2020-07-07 19:39:04 +00:00
|
|
|
// this handles converting an untyped nil to a typed nil
|
|
|
|
addrs, _ := ret.Get(0).([]string)
|
|
|
|
return addrs, ret.Error(1)
|
|
|
|
}
|
|
|
|
|
2021-04-20 18:55:24 +00:00
|
|
|
func (m *mockAutoConfigBackend) ForwardRPC(method string, req structs.RPCInfo, reply interface{}) (bool, error) {
|
|
|
|
ret := m.Called(method, req, reply)
|
2020-07-07 19:39:04 +00:00
|
|
|
return ret.Bool(0), ret.Error(1)
|
|
|
|
}
|
|
|
|
|
2020-07-28 19:31:48 +00:00
|
|
|
func (m *mockAutoConfigBackend) GetCARoots() (*structs.IndexedCARoots, error) {
|
|
|
|
ret := m.Called()
|
|
|
|
roots, _ := ret.Get(0).(*structs.IndexedCARoots)
|
|
|
|
return roots, ret.Error(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockAutoConfigBackend) SignCertificate(csr *x509.CertificateRequest, id connect.CertURI) (*structs.IssuedCert, error) {
|
|
|
|
ret := m.Called(csr, id)
|
|
|
|
cert, _ := ret.Get(0).(*structs.IssuedCert)
|
|
|
|
return cert, ret.Error(1)
|
|
|
|
}
|
|
|
|
|
2020-06-05 19:56:19 +00:00
|
|
|
func testJWTStandardClaims() jwt.Claims {
|
|
|
|
now := time.Now()
|
|
|
|
|
|
|
|
return jwt.Claims{
|
|
|
|
Subject: "consul",
|
|
|
|
Issuer: "consul",
|
|
|
|
Audience: jwt.Audience{"consul"},
|
|
|
|
NotBefore: jwt.NewNumericDate(now.Add(-1 * time.Second)),
|
|
|
|
Expiry: jwt.NewNumericDate(now.Add(10 * time.Minute)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func signJWT(t *testing.T, privKey string, claims jwt.Claims, privateClaims interface{}) string {
|
|
|
|
t.Helper()
|
|
|
|
token, err := oidcauthtest.SignJWT(privKey, claims, privateClaims)
|
|
|
|
require.NoError(t, err)
|
|
|
|
return token
|
|
|
|
}
|
|
|
|
|
|
|
|
func signJWTWithStandardClaims(t *testing.T, privKey string, claims interface{}) string {
|
|
|
|
t.Helper()
|
|
|
|
return signJWT(t, privKey, testJWTStandardClaims(), claims)
|
|
|
|
}
|
|
|
|
|
2020-07-07 19:39:04 +00:00
|
|
|
// TestAutoConfigInitialConfiguration is really an integration test of all the moving parts of the AutoConfig.InitialConfiguration RPC.
|
2020-06-05 19:56:19 +00:00
|
|
|
// Full testing of the individual parts will not be done in this test:
|
|
|
|
//
|
2022-09-13 14:15:20 +00:00
|
|
|
// - Any implementations of the AutoConfigAuthorizer interface (although these test do use the jwtAuthorizer)
|
|
|
|
// - Each of the individual config generation functions. These can be unit tested separately and should NOT
|
|
|
|
// require running test servers
|
2020-07-07 19:39:04 +00:00
|
|
|
func TestAutoConfigInitialConfiguration(t *testing.T) {
|
2020-12-07 18:42:55 +00:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("too slow for testing.Short")
|
|
|
|
}
|
|
|
|
|
2020-06-05 19:56:19 +00:00
|
|
|
gossipKey := make([]byte, 32)
|
|
|
|
// this is not cryptographic randomness and is not secure but for the sake of this test its all we need.
|
|
|
|
n, err := rand.Read(gossipKey)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 32, n)
|
|
|
|
|
|
|
|
gossipKeyEncoded := base64.StdEncoding.EncodeToString(gossipKey)
|
|
|
|
|
|
|
|
// generate a test certificate for the server serving out the insecure RPC
|
|
|
|
cert, key, cacert, err := testTLSCertificates("server.dc1.consul")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// generate a JWT signer
|
|
|
|
pub, priv, err := oidcauthtest.GenerateKey()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
_, altpriv, err := oidcauthtest.GenerateKey()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-07-28 19:31:48 +00:00
|
|
|
// this CSR is what gets sent in the request
|
|
|
|
csrID := connect.SpiffeIDAgent{
|
|
|
|
Host: "dummy.trustdomain",
|
|
|
|
Agent: "test-node",
|
|
|
|
Datacenter: "dc1",
|
|
|
|
}
|
|
|
|
csr, _ := connect.TestCSR(t, &csrID)
|
|
|
|
|
|
|
|
altCSRID := connect.SpiffeIDAgent{
|
|
|
|
Host: "dummy.trustdomain",
|
|
|
|
Agent: "alt",
|
|
|
|
Datacenter: "dc1",
|
|
|
|
}
|
|
|
|
|
|
|
|
altCSR, _ := connect.TestCSR(t, &altCSRID)
|
|
|
|
|
|
|
|
_, s, _ := testACLServerWithConfig(t, func(c *Config) {
|
2021-07-09 22:17:42 +00:00
|
|
|
c.TLSConfig.Domain = "consul"
|
2020-07-28 19:31:48 +00:00
|
|
|
c.AutoConfigAuthzEnabled = true
|
|
|
|
c.AutoConfigAuthzAuthMethod = structs.ACLAuthMethod{
|
|
|
|
Name: "Auth Config Authorizer",
|
|
|
|
Type: "jwt",
|
2021-07-22 18:20:45 +00:00
|
|
|
EnterpriseMeta: *structs.DefaultEnterpriseMetaInDefaultPartition(),
|
2020-07-28 19:31:48 +00:00
|
|
|
Config: map[string]interface{}{
|
|
|
|
"BoundAudiences": []string{"consul"},
|
|
|
|
"BoundIssuer": "consul",
|
|
|
|
"JWTValidationPubKeys": []string{pub},
|
|
|
|
"ClaimMappings": map[string]string{
|
|
|
|
"consul_node_name": "node",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
c.AutoConfigAuthzClaimAssertions = []string{
|
|
|
|
`value.node == "${node}"`,
|
|
|
|
}
|
|
|
|
c.AutoConfigAuthzAllowReuse = true
|
|
|
|
|
|
|
|
cafile := path.Join(c.DataDir, "cacert.pem")
|
2022-11-10 16:26:01 +00:00
|
|
|
err := os.WriteFile(cafile, []byte(cacert), 0600)
|
2020-07-28 19:31:48 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
certfile := path.Join(c.DataDir, "cert.pem")
|
2022-11-10 16:26:01 +00:00
|
|
|
err = os.WriteFile(certfile, []byte(cert), 0600)
|
2020-07-28 19:31:48 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
keyfile := path.Join(c.DataDir, "key.pem")
|
2022-11-10 16:26:01 +00:00
|
|
|
err = os.WriteFile(keyfile, []byte(key), 0600)
|
2020-07-28 19:31:48 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-03-18 10:46:58 +00:00
|
|
|
c.TLSConfig.InternalRPC.CAFile = cafile
|
|
|
|
c.TLSConfig.InternalRPC.CertFile = certfile
|
|
|
|
c.TLSConfig.InternalRPC.KeyFile = keyfile
|
|
|
|
c.TLSConfig.InternalRPC.VerifyOutgoing = true
|
|
|
|
c.TLSConfig.InternalRPC.VerifyIncoming = true
|
|
|
|
c.TLSConfig.InternalRPC.VerifyServerHostname = true
|
2022-03-24 19:32:25 +00:00
|
|
|
c.TLSConfig.InternalRPC.TLSMinVersion = types.TLSv1_2
|
2020-07-28 19:31:48 +00:00
|
|
|
|
|
|
|
c.ConnectEnabled = true
|
|
|
|
c.AutoEncryptAllowTLS = true
|
|
|
|
c.SerfLANConfig.MemberlistConfig.GossipVerifyIncoming = true
|
|
|
|
c.SerfLANConfig.MemberlistConfig.GossipVerifyOutgoing = true
|
|
|
|
|
|
|
|
keyring, err := memberlist.NewKeyring(nil, gossipKey)
|
|
|
|
require.NoError(t, err)
|
|
|
|
c.SerfLANConfig.MemberlistConfig.Keyring = keyring
|
|
|
|
}, false)
|
|
|
|
|
2021-07-09 22:17:42 +00:00
|
|
|
// TODO: use s.config.TLSConfig directly instead of creating a new one?
|
2020-07-28 19:31:48 +00:00
|
|
|
conf := tlsutil.Config{
|
2022-03-18 10:46:58 +00:00
|
|
|
InternalRPC: tlsutil.ProtocolConfig{
|
|
|
|
CAFile: s.config.TLSConfig.InternalRPC.CAFile,
|
|
|
|
VerifyServerHostname: s.config.TLSConfig.InternalRPC.VerifyServerHostname,
|
|
|
|
VerifyOutgoing: s.config.TLSConfig.InternalRPC.VerifyOutgoing,
|
|
|
|
},
|
|
|
|
Domain: s.config.TLSConfig.Domain,
|
2020-07-28 19:31:48 +00:00
|
|
|
}
|
|
|
|
codec, err := insecureRPCClient(s, conf)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
waitForLeaderEstablishment(t, s)
|
|
|
|
|
2021-06-23 21:14:28 +00:00
|
|
|
roots, err := s.getCARoots(nil, s.fsm.State())
|
2020-07-28 19:31:48 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-03-16 16:12:29 +00:00
|
|
|
pbroots, err := pbconnect.NewCARootsFromStructs(roots)
|
2020-07-28 19:31:48 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
joinAddr := &net.TCPAddr{IP: net.IPv4(127, 0, 0, 1), Port: s.config.SerfLANConfig.MemberlistConfig.AdvertisePort}
|
|
|
|
|
|
|
|
// -------------------------------------------------------------------------
|
|
|
|
// Common test setup is now complete
|
|
|
|
// -------------------------------------------------------------------------
|
|
|
|
|
|
|
|
type testCase struct {
|
2022-03-30 16:51:56 +00:00
|
|
|
request *pbautoconf.AutoConfigRequest
|
|
|
|
expected *pbautoconf.AutoConfigResponse
|
2020-07-28 19:31:48 +00:00
|
|
|
patchResponse func(t *testing.T, srv *Server, resp *pbautoconf.AutoConfigResponse)
|
|
|
|
err string
|
|
|
|
}
|
|
|
|
|
2021-12-01 19:32:34 +00:00
|
|
|
defaultEntMeta := structs.DefaultEnterpriseMetaInDefaultPartition()
|
|
|
|
|
2020-06-05 19:56:19 +00:00
|
|
|
cases := map[string]testCase{
|
|
|
|
"wrong-datacenter": {
|
2022-03-30 16:51:56 +00:00
|
|
|
request: &pbautoconf.AutoConfigRequest{
|
2020-06-05 19:56:19 +00:00
|
|
|
Datacenter: "no-such-dc",
|
|
|
|
},
|
|
|
|
err: `invalid datacenter "no-such-dc" - agent auto configuration cannot target a remote datacenter`,
|
|
|
|
},
|
|
|
|
"unverifiable": {
|
2022-03-30 16:51:56 +00:00
|
|
|
request: &pbautoconf.AutoConfigRequest{
|
2020-06-05 19:56:19 +00:00
|
|
|
Node: "test-node",
|
|
|
|
// this is signed using an incorrect private key
|
|
|
|
JWT: signJWTWithStandardClaims(t, altpriv, map[string]interface{}{"consul_node_name": "test-node"}),
|
|
|
|
},
|
|
|
|
err: "Permission denied: Failed JWT authorization: no known key successfully validated the token signature",
|
|
|
|
},
|
2022-09-13 14:15:20 +00:00
|
|
|
"bad-req-node": {
|
|
|
|
request: &pbautoconf.AutoConfigRequest{
|
|
|
|
Node: "bad node",
|
|
|
|
JWT: signJWTWithStandardClaims(t, priv, map[string]interface{}{"consul_node_name": "test-node"}),
|
|
|
|
},
|
|
|
|
err: "Invalid request field. node =",
|
|
|
|
},
|
|
|
|
"bad-req-segment": {
|
|
|
|
request: &pbautoconf.AutoConfigRequest{
|
|
|
|
Node: "test-node",
|
|
|
|
Segment: "bad segment",
|
|
|
|
JWT: signJWTWithStandardClaims(t, priv, map[string]interface{}{"consul_node_name": "test-node"}),
|
|
|
|
},
|
|
|
|
err: "Invalid request field. segment =",
|
|
|
|
},
|
|
|
|
"bad-req-partition": {
|
|
|
|
request: &pbautoconf.AutoConfigRequest{
|
|
|
|
Node: "test-node",
|
|
|
|
Partition: "bad partition",
|
|
|
|
JWT: signJWTWithStandardClaims(t, priv, map[string]interface{}{"consul_node_name": "test-node"}),
|
|
|
|
},
|
|
|
|
err: "Invalid request field. partition =",
|
|
|
|
},
|
2020-06-05 19:56:19 +00:00
|
|
|
"claim-assertion-failed": {
|
2022-03-30 16:51:56 +00:00
|
|
|
request: &pbautoconf.AutoConfigRequest{
|
2020-06-05 19:56:19 +00:00
|
|
|
Node: "test-node",
|
|
|
|
JWT: signJWTWithStandardClaims(t, priv, map[string]interface{}{"wrong_claim": "test-node"}),
|
|
|
|
},
|
|
|
|
err: "Permission denied: Failed JWT claim assertion",
|
|
|
|
},
|
2020-07-28 19:31:48 +00:00
|
|
|
"bad-csr-id": {
|
2022-03-30 16:51:56 +00:00
|
|
|
request: &pbautoconf.AutoConfigRequest{
|
2020-07-28 19:31:48 +00:00
|
|
|
Node: "test-node",
|
|
|
|
JWT: signJWTWithStandardClaims(t, priv, map[string]interface{}{"consul_node_name": "test-node"}),
|
|
|
|
CSR: altCSR,
|
|
|
|
},
|
|
|
|
err: "Spiffe ID agent name (alt) of the certificate signing request is not for the correct node (test-node)",
|
|
|
|
},
|
2020-06-05 19:56:19 +00:00
|
|
|
"good": {
|
2022-03-30 16:51:56 +00:00
|
|
|
request: &pbautoconf.AutoConfigRequest{
|
2020-06-05 19:56:19 +00:00
|
|
|
Node: "test-node",
|
|
|
|
JWT: signJWTWithStandardClaims(t, priv, map[string]interface{}{"consul_node_name": "test-node"}),
|
2020-07-28 19:31:48 +00:00
|
|
|
CSR: csr,
|
2020-06-05 19:56:19 +00:00
|
|
|
},
|
2022-03-30 16:51:56 +00:00
|
|
|
expected: &pbautoconf.AutoConfigResponse{
|
2020-07-28 19:31:48 +00:00
|
|
|
CARoots: pbroots,
|
|
|
|
ExtraCACertificates: []string{cacert},
|
2020-07-23 15:24:20 +00:00
|
|
|
Config: &pbconfig.Config{
|
2020-06-05 19:56:19 +00:00
|
|
|
Datacenter: "dc1",
|
|
|
|
PrimaryDatacenter: "dc1",
|
|
|
|
NodeName: "test-node",
|
2020-07-23 15:24:20 +00:00
|
|
|
ACL: &pbconfig.ACL{
|
2020-06-05 19:56:19 +00:00
|
|
|
Enabled: true,
|
|
|
|
PolicyTTL: "30s",
|
|
|
|
TokenTTL: "30s",
|
|
|
|
RoleTTL: "30s",
|
|
|
|
DownPolicy: "extend-cache",
|
|
|
|
DefaultPolicy: "deny",
|
2020-07-23 15:24:20 +00:00
|
|
|
Tokens: &pbconfig.ACLTokens{
|
2020-06-05 19:56:19 +00:00
|
|
|
Agent: "patched-secret",
|
|
|
|
},
|
|
|
|
},
|
2020-07-23 15:24:20 +00:00
|
|
|
Gossip: &pbconfig.Gossip{
|
|
|
|
Encryption: &pbconfig.GossipEncryption{
|
2020-06-05 19:56:19 +00:00
|
|
|
Key: gossipKeyEncoded,
|
|
|
|
VerifyIncoming: true,
|
|
|
|
VerifyOutgoing: true,
|
|
|
|
},
|
2020-07-28 19:31:48 +00:00
|
|
|
RetryJoinLAN: []string{joinAddr.String()},
|
2020-06-05 19:56:19 +00:00
|
|
|
},
|
2020-07-23 15:24:20 +00:00
|
|
|
TLS: &pbconfig.TLS{
|
2022-03-18 10:46:58 +00:00
|
|
|
VerifyOutgoing: true,
|
|
|
|
VerifyServerHostname: true,
|
|
|
|
MinVersion: "tls12",
|
2020-06-05 19:56:19 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2020-11-05 16:18:59 +00:00
|
|
|
patchResponse: func(t *testing.T, _ *Server, resp *pbautoconf.AutoConfigResponse) {
|
2020-06-05 19:56:19 +00:00
|
|
|
// we are expecting an ACL token but cannot check anything for equality
|
|
|
|
// so here we check that it was set and overwrite it
|
|
|
|
require.NotNil(t, resp.Config)
|
|
|
|
require.NotNil(t, resp.Config.ACL)
|
|
|
|
require.NotNil(t, resp.Config.ACL.Tokens)
|
|
|
|
require.NotEmpty(t, resp.Config.ACL.Tokens.Agent)
|
|
|
|
resp.Config.ACL.Tokens.Agent = "patched-secret"
|
|
|
|
|
2020-07-28 19:31:48 +00:00
|
|
|
require.NotNil(t, resp.Certificate)
|
|
|
|
require.NotEmpty(t, resp.Certificate.SerialNumber)
|
|
|
|
require.NotEmpty(t, resp.Certificate.CertPEM)
|
|
|
|
require.Empty(t, resp.Certificate.Service)
|
|
|
|
require.Empty(t, resp.Certificate.ServiceURI)
|
|
|
|
require.Equal(t, "test-node", resp.Certificate.Agent)
|
|
|
|
|
|
|
|
expectedID := connect.SpiffeIDAgent{
|
|
|
|
Host: roots.TrustDomain,
|
|
|
|
Agent: "test-node",
|
2021-12-01 19:32:34 +00:00
|
|
|
Partition: defaultEntMeta.PartitionOrDefault(),
|
2020-07-28 19:31:48 +00:00
|
|
|
Datacenter: "dc1",
|
|
|
|
}
|
2020-06-05 19:56:19 +00:00
|
|
|
|
2020-07-28 19:31:48 +00:00
|
|
|
require.Equal(t, expectedID.URI().String(), resp.Certificate.AgentURI)
|
2020-06-05 19:56:19 +00:00
|
|
|
|
2020-07-28 19:31:48 +00:00
|
|
|
// nil this out so we don't check it for equality
|
|
|
|
resp.Certificate = nil
|
|
|
|
},
|
|
|
|
},
|
2020-06-05 19:56:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for testName, tcase := range cases {
|
|
|
|
t.Run(testName, func(t *testing.T) {
|
2022-03-30 16:51:56 +00:00
|
|
|
reply := &pbautoconf.AutoConfigResponse{}
|
|
|
|
err := msgpackrpc.CallWithCodec(codec, "AutoConfig.InitialConfiguration", &tcase.request, reply)
|
2020-06-05 19:56:19 +00:00
|
|
|
if tcase.err != "" {
|
|
|
|
testutil.RequireErrorContains(t, err, tcase.err)
|
|
|
|
} else {
|
|
|
|
require.NoError(t, err)
|
|
|
|
if tcase.patchResponse != nil {
|
2022-03-30 16:51:56 +00:00
|
|
|
tcase.patchResponse(t, s, reply)
|
2020-06-05 19:56:19 +00:00
|
|
|
}
|
2022-03-30 16:51:56 +00:00
|
|
|
prototest.AssertDeepEqual(t, tcase.expected, reply)
|
2020-06-05 19:56:19 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-07 19:39:04 +00:00
|
|
|
func TestAutoConfig_baseConfig(t *testing.T) {
|
2020-06-05 19:56:19 +00:00
|
|
|
type testCase struct {
|
|
|
|
serverConfig Config
|
|
|
|
opts AutoConfigOptions
|
2022-03-30 16:51:56 +00:00
|
|
|
expected *pbautoconf.AutoConfigResponse
|
2020-06-05 19:56:19 +00:00
|
|
|
err string
|
|
|
|
}
|
|
|
|
|
|
|
|
cases := map[string]testCase{
|
|
|
|
"ok": {
|
|
|
|
serverConfig: Config{
|
|
|
|
Datacenter: "oSWzfhnU",
|
|
|
|
PrimaryDatacenter: "53XO9mx4",
|
|
|
|
},
|
|
|
|
opts: AutoConfigOptions{
|
|
|
|
NodeName: "lBdc0lsH",
|
|
|
|
SegmentName: "HZiwlWpi",
|
|
|
|
},
|
2022-03-30 16:51:56 +00:00
|
|
|
expected: &pbautoconf.AutoConfigResponse{
|
2020-07-28 19:31:48 +00:00
|
|
|
Config: &pbconfig.Config{
|
|
|
|
Datacenter: "oSWzfhnU",
|
|
|
|
PrimaryDatacenter: "53XO9mx4",
|
|
|
|
NodeName: "lBdc0lsH",
|
|
|
|
SegmentName: "HZiwlWpi",
|
|
|
|
},
|
2020-06-05 19:56:19 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
"no-node-name": {
|
|
|
|
serverConfig: Config{
|
|
|
|
Datacenter: "oSWzfhnU",
|
|
|
|
PrimaryDatacenter: "53XO9mx4",
|
|
|
|
},
|
|
|
|
err: "Cannot generate auto config response without a node name",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tcase := range cases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
2020-07-07 19:39:04 +00:00
|
|
|
ac := AutoConfig{
|
2020-07-08 16:36:11 +00:00
|
|
|
config: &tcase.serverConfig,
|
2020-06-05 19:56:19 +00:00
|
|
|
}
|
|
|
|
|
2022-03-30 16:51:56 +00:00
|
|
|
actual := &pbautoconf.AutoConfigResponse{Config: &pbconfig.Config{}}
|
|
|
|
err := ac.baseConfig(tcase.opts, actual)
|
2020-06-05 19:56:19 +00:00
|
|
|
if tcase.err == "" {
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, tcase.expected, actual)
|
|
|
|
} else {
|
|
|
|
testutil.RequireErrorContains(t, err, tcase.err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-07 19:39:04 +00:00
|
|
|
func TestAutoConfig_updateTLSSettingsInConfig(t *testing.T) {
|
2020-06-05 19:56:19 +00:00
|
|
|
_, _, cacert, err := testTLSCertificates("server.dc1.consul")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
dir := testutil.TempDir(t, "auto-config-tls-settings")
|
|
|
|
cafile := path.Join(dir, "cacert.pem")
|
2022-11-10 16:26:01 +00:00
|
|
|
err = os.WriteFile(cafile, []byte(cacert), 0600)
|
2020-06-05 19:56:19 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
type testCase struct {
|
|
|
|
tlsConfig tlsutil.Config
|
2022-03-30 16:51:56 +00:00
|
|
|
expected *pbautoconf.AutoConfigResponse
|
2020-06-05 19:56:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cases := map[string]testCase{
|
|
|
|
"secure": {
|
|
|
|
tlsConfig: tlsutil.Config{
|
2022-03-18 10:46:58 +00:00
|
|
|
InternalRPC: tlsutil.ProtocolConfig{
|
|
|
|
VerifyServerHostname: true,
|
|
|
|
VerifyOutgoing: true,
|
2022-03-24 19:32:25 +00:00
|
|
|
TLSMinVersion: types.TLSv1_2,
|
2022-03-18 10:46:58 +00:00
|
|
|
CAFile: cafile,
|
2022-03-24 19:32:25 +00:00
|
|
|
CipherSuites: []types.TLSCipherSuite{"TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256", "TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384", "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256", "TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384"},
|
2022-03-18 10:46:58 +00:00
|
|
|
},
|
2020-06-05 19:56:19 +00:00
|
|
|
},
|
2022-03-30 16:51:56 +00:00
|
|
|
expected: &pbautoconf.AutoConfigResponse{
|
2020-07-28 19:31:48 +00:00
|
|
|
Config: &pbconfig.Config{
|
|
|
|
TLS: &pbconfig.TLS{
|
2022-03-18 10:46:58 +00:00
|
|
|
VerifyOutgoing: true,
|
|
|
|
VerifyServerHostname: true,
|
|
|
|
MinVersion: "tls12",
|
|
|
|
CipherSuites: "TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384",
|
2020-07-28 19:31:48 +00:00
|
|
|
},
|
2020-06-05 19:56:19 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"less-secure": {
|
|
|
|
tlsConfig: tlsutil.Config{
|
2022-03-18 10:46:58 +00:00
|
|
|
InternalRPC: tlsutil.ProtocolConfig{
|
|
|
|
VerifyServerHostname: false,
|
|
|
|
VerifyOutgoing: true,
|
2022-03-24 19:32:25 +00:00
|
|
|
TLSMinVersion: types.TLSv1_0,
|
2022-03-18 10:46:58 +00:00
|
|
|
CAFile: cafile,
|
2022-03-24 19:32:25 +00:00
|
|
|
CipherSuites: []types.TLSCipherSuite{"TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256", "TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384"},
|
2022-03-18 10:46:58 +00:00
|
|
|
},
|
2020-06-05 19:56:19 +00:00
|
|
|
},
|
2022-03-30 16:51:56 +00:00
|
|
|
expected: &pbautoconf.AutoConfigResponse{
|
2020-07-28 19:31:48 +00:00
|
|
|
Config: &pbconfig.Config{
|
|
|
|
TLS: &pbconfig.TLS{
|
2022-03-18 10:46:58 +00:00
|
|
|
VerifyOutgoing: true,
|
|
|
|
VerifyServerHostname: false,
|
|
|
|
MinVersion: "tls10",
|
|
|
|
CipherSuites: "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384",
|
2020-07-28 19:31:48 +00:00
|
|
|
},
|
2020-06-05 19:56:19 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tcase := range cases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
logger := testutil.Logger(t)
|
|
|
|
configurator, err := tlsutil.NewConfigurator(tcase.tlsConfig, logger)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-07-07 19:39:04 +00:00
|
|
|
ac := &AutoConfig{
|
2020-07-08 16:36:11 +00:00
|
|
|
tlsConfigurator: configurator,
|
2020-06-05 19:56:19 +00:00
|
|
|
}
|
|
|
|
|
2022-03-30 16:51:56 +00:00
|
|
|
actual := &pbautoconf.AutoConfigResponse{Config: &pbconfig.Config{}}
|
|
|
|
err = ac.updateTLSSettingsInConfig(AutoConfigOptions{}, actual)
|
2020-06-05 19:56:19 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, tcase.expected, actual)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAutoConfig_updateGossipEncryptionInConfig(t *testing.T) {
|
|
|
|
type testCase struct {
|
|
|
|
conf memberlist.Config
|
2022-03-30 16:51:56 +00:00
|
|
|
expected *pbautoconf.AutoConfigResponse
|
2020-06-05 19:56:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
gossipKey := make([]byte, 32)
|
|
|
|
// this is not cryptographic randomness and is not secure but for the sake of this test its all we need.
|
|
|
|
n, err := rand.Read(gossipKey)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 32, n)
|
|
|
|
gossipKeyEncoded := base64.StdEncoding.EncodeToString(gossipKey)
|
|
|
|
|
|
|
|
keyring, err := memberlist.NewKeyring(nil, gossipKey)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
cases := map[string]testCase{
|
|
|
|
"encryption-required": {
|
|
|
|
conf: memberlist.Config{
|
|
|
|
Keyring: keyring,
|
|
|
|
GossipVerifyIncoming: true,
|
|
|
|
GossipVerifyOutgoing: true,
|
|
|
|
},
|
2022-03-30 16:51:56 +00:00
|
|
|
expected: &pbautoconf.AutoConfigResponse{
|
2020-07-28 19:31:48 +00:00
|
|
|
Config: &pbconfig.Config{
|
|
|
|
Gossip: &pbconfig.Gossip{
|
|
|
|
Encryption: &pbconfig.GossipEncryption{
|
|
|
|
Key: gossipKeyEncoded,
|
|
|
|
VerifyIncoming: true,
|
|
|
|
VerifyOutgoing: true,
|
|
|
|
},
|
2020-06-05 19:56:19 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"encryption-allowed": {
|
|
|
|
conf: memberlist.Config{
|
|
|
|
Keyring: keyring,
|
|
|
|
GossipVerifyIncoming: false,
|
|
|
|
GossipVerifyOutgoing: false,
|
|
|
|
},
|
2022-03-30 16:51:56 +00:00
|
|
|
expected: &pbautoconf.AutoConfigResponse{
|
2020-07-28 19:31:48 +00:00
|
|
|
Config: &pbconfig.Config{
|
|
|
|
Gossip: &pbconfig.Gossip{
|
|
|
|
Encryption: &pbconfig.GossipEncryption{
|
|
|
|
Key: gossipKeyEncoded,
|
|
|
|
VerifyIncoming: false,
|
|
|
|
VerifyOutgoing: false,
|
|
|
|
},
|
2020-06-05 19:56:19 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"encryption-disabled": {
|
|
|
|
// zero values all around - if no keyring is configured then the gossip
|
|
|
|
// encryption settings should not be set.
|
2022-03-30 16:51:56 +00:00
|
|
|
expected: &pbautoconf.AutoConfigResponse{
|
2020-07-28 19:31:48 +00:00
|
|
|
Config: &pbconfig.Config{},
|
|
|
|
},
|
2020-06-05 19:56:19 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tcase := range cases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
2020-07-07 19:39:04 +00:00
|
|
|
cfg := DefaultConfig()
|
|
|
|
cfg.SerfLANConfig.MemberlistConfig = &tcase.conf
|
|
|
|
|
|
|
|
ac := AutoConfig{
|
2020-07-08 16:36:11 +00:00
|
|
|
config: cfg,
|
2020-07-07 19:39:04 +00:00
|
|
|
}
|
2020-06-05 19:56:19 +00:00
|
|
|
|
2022-03-30 16:51:56 +00:00
|
|
|
actual := &pbautoconf.AutoConfigResponse{Config: &pbconfig.Config{}}
|
|
|
|
err := ac.updateGossipEncryptionInConfig(AutoConfigOptions{}, actual)
|
2020-06-05 19:56:19 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, tcase.expected, actual)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAutoConfig_updateTLSCertificatesInConfig(t *testing.T) {
|
2020-07-28 19:31:48 +00:00
|
|
|
now := time.Now()
|
|
|
|
later := now.Add(time.Hour)
|
|
|
|
|
|
|
|
// Generate a Test CA
|
|
|
|
ca := connect.TestCA(t, nil)
|
|
|
|
|
|
|
|
// roots will be returned by the mock backend
|
|
|
|
roots := structs.IndexedCARoots{
|
|
|
|
ActiveRootID: ca.ID,
|
|
|
|
TrustDomain: connect.TestClusterID + ".consul",
|
|
|
|
Roots: []*structs.CARoot{
|
|
|
|
ca,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// this CSR is what gets put into the opts for the
|
|
|
|
// function to look at an process
|
|
|
|
csrID := connect.SpiffeIDAgent{
|
|
|
|
Host: roots.TrustDomain,
|
|
|
|
Agent: "test",
|
|
|
|
Datacenter: "dc1",
|
|
|
|
}
|
|
|
|
csrStr, _ := connect.TestCSR(t, &csrID)
|
|
|
|
|
|
|
|
csr, err := connect.ParseCSR(csrStr)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// fake certificate response for the backend
|
|
|
|
fakeCert := structs.IssuedCert{
|
|
|
|
SerialNumber: "1",
|
|
|
|
CertPEM: "not-currently-decoded",
|
|
|
|
ValidAfter: now,
|
|
|
|
ValidBefore: later,
|
2021-07-22 18:20:45 +00:00
|
|
|
EnterpriseMeta: *structs.DefaultEnterpriseMetaInDefaultPartition(),
|
2020-07-28 19:31:48 +00:00
|
|
|
RaftIndex: structs.RaftIndex{
|
|
|
|
ModifyIndex: 10,
|
|
|
|
CreateIndex: 10,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// translate the fake cert to the protobuf equivalent
|
|
|
|
// for embedding in expected results
|
2022-03-16 16:12:29 +00:00
|
|
|
pbcert, err := pbconnect.NewIssuedCertFromStructs(&fakeCert)
|
2020-07-28 19:31:48 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// generate a CA certificate to use for specifying non-Connect
|
|
|
|
// certificates which come back differently in the response
|
|
|
|
_, _, cacert, err := testTLSCertificates("server.dc1.consul")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// write out that ca cert to disk - it is unfortunate that
|
|
|
|
// this is necessary but creation of the tlsutil.Configurator
|
|
|
|
// will error if it cannot load the CA certificate from disk
|
|
|
|
dir := testutil.TempDir(t, "auto-config-tls-certificate")
|
|
|
|
cafile := path.Join(dir, "cacert.pem")
|
2022-11-10 16:26:01 +00:00
|
|
|
err = os.WriteFile(cafile, []byte(cacert), 0600)
|
2020-07-28 19:31:48 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// translate the roots response to protobuf to be embedded
|
|
|
|
// into the expected results
|
2022-03-16 16:12:29 +00:00
|
|
|
pbroots, err := pbconnect.NewCARootsFromStructs(&roots)
|
2020-07-28 19:31:48 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-06-05 19:56:19 +00:00
|
|
|
type testCase struct {
|
|
|
|
serverConfig Config
|
2020-07-28 19:31:48 +00:00
|
|
|
tlsConfig tlsutil.Config
|
|
|
|
|
|
|
|
opts AutoConfigOptions
|
2022-03-30 16:51:56 +00:00
|
|
|
expected *pbautoconf.AutoConfigResponse
|
2020-06-05 19:56:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cases := map[string]testCase{
|
2020-07-28 19:31:48 +00:00
|
|
|
"no-csr": {
|
2020-06-05 19:56:19 +00:00
|
|
|
serverConfig: Config{
|
2020-07-28 19:31:48 +00:00
|
|
|
ConnectEnabled: true,
|
2020-06-05 19:56:19 +00:00
|
|
|
},
|
2020-07-28 19:31:48 +00:00
|
|
|
tlsConfig: tlsutil.Config{
|
2022-03-18 10:46:58 +00:00
|
|
|
InternalRPC: tlsutil.ProtocolConfig{
|
|
|
|
VerifyServerHostname: true,
|
|
|
|
VerifyOutgoing: true,
|
2022-03-24 19:32:25 +00:00
|
|
|
TLSMinVersion: types.TLSv1_2,
|
2022-03-18 10:46:58 +00:00
|
|
|
CAFile: cafile,
|
2022-03-24 19:32:25 +00:00
|
|
|
CipherSuites: []types.TLSCipherSuite{"TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256", "TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384", "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256", "TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384"},
|
2022-03-18 10:46:58 +00:00
|
|
|
},
|
2020-07-28 19:31:48 +00:00
|
|
|
},
|
2022-03-30 16:51:56 +00:00
|
|
|
expected: &pbautoconf.AutoConfigResponse{
|
2020-07-28 19:31:48 +00:00
|
|
|
CARoots: pbroots,
|
|
|
|
ExtraCACertificates: []string{cacert},
|
|
|
|
Config: &pbconfig.Config{},
|
2020-06-05 19:56:19 +00:00
|
|
|
},
|
|
|
|
},
|
2020-07-28 19:31:48 +00:00
|
|
|
"signed-certificate": {
|
2020-06-05 19:56:19 +00:00
|
|
|
serverConfig: Config{
|
2020-07-28 19:31:48 +00:00
|
|
|
ConnectEnabled: true,
|
2020-06-05 19:56:19 +00:00
|
|
|
},
|
2020-07-28 19:31:48 +00:00
|
|
|
tlsConfig: tlsutil.Config{
|
2022-03-18 10:46:58 +00:00
|
|
|
InternalRPC: tlsutil.ProtocolConfig{
|
|
|
|
VerifyServerHostname: true,
|
|
|
|
VerifyOutgoing: true,
|
2022-03-24 19:32:25 +00:00
|
|
|
TLSMinVersion: types.TLSv1_2,
|
2022-03-18 10:46:58 +00:00
|
|
|
CAFile: cafile,
|
2022-03-24 19:32:25 +00:00
|
|
|
CipherSuites: []types.TLSCipherSuite{"TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256", "TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384", "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256", "TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384"},
|
2022-03-18 10:46:58 +00:00
|
|
|
},
|
2020-07-28 19:31:48 +00:00
|
|
|
},
|
|
|
|
opts: AutoConfigOptions{
|
|
|
|
NodeName: "test",
|
|
|
|
CSR: csr,
|
|
|
|
SpiffeID: &csrID,
|
|
|
|
},
|
2022-03-30 16:51:56 +00:00
|
|
|
expected: &pbautoconf.AutoConfigResponse{
|
2020-07-28 19:31:48 +00:00
|
|
|
Config: &pbconfig.Config{},
|
|
|
|
CARoots: pbroots,
|
|
|
|
ExtraCACertificates: []string{cacert},
|
|
|
|
Certificate: pbcert,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"connect-disabled": {
|
|
|
|
serverConfig: Config{
|
|
|
|
ConnectEnabled: false,
|
|
|
|
},
|
2022-03-30 16:51:56 +00:00
|
|
|
expected: &pbautoconf.AutoConfigResponse{
|
2020-07-28 19:31:48 +00:00
|
|
|
Config: &pbconfig.Config{},
|
2020-06-05 19:56:19 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tcase := range cases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
2020-07-28 19:31:48 +00:00
|
|
|
backend := &mockAutoConfigBackend{}
|
|
|
|
backend.On("GetCARoots").Return(&roots, nil)
|
|
|
|
backend.On("SignCertificate", tcase.opts.CSR, tcase.opts.SpiffeID).Return(&fakeCert, nil)
|
|
|
|
|
|
|
|
tlsConfigurator, err := tlsutil.NewConfigurator(tcase.tlsConfig, testutil.Logger(t))
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-07-07 19:39:04 +00:00
|
|
|
ac := AutoConfig{
|
2020-07-28 19:31:48 +00:00
|
|
|
config: &tcase.serverConfig,
|
|
|
|
tlsConfigurator: tlsConfigurator,
|
|
|
|
backend: backend,
|
2020-06-05 19:56:19 +00:00
|
|
|
}
|
|
|
|
|
2022-03-30 16:51:56 +00:00
|
|
|
actual := &pbautoconf.AutoConfigResponse{Config: &pbconfig.Config{}}
|
|
|
|
err = ac.updateTLSCertificatesInConfig(tcase.opts, actual)
|
2020-06-05 19:56:19 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, tcase.expected, actual)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAutoConfig_updateACLsInConfig(t *testing.T) {
|
|
|
|
type testCase struct {
|
2020-07-07 19:39:04 +00:00
|
|
|
config Config
|
2022-03-30 16:51:56 +00:00
|
|
|
expected *pbautoconf.AutoConfigResponse
|
2020-07-07 19:39:04 +00:00
|
|
|
expectACLToken bool
|
|
|
|
err error
|
2020-06-05 19:56:19 +00:00
|
|
|
}
|
|
|
|
|
2020-07-07 19:39:04 +00:00
|
|
|
const (
|
|
|
|
tokenAccessor = "b98761aa-c0ee-445b-9b0c-f54b56b47778"
|
|
|
|
tokenSecret = "1c96448a-ab04-4caa-982a-e8b095a111e2"
|
|
|
|
)
|
|
|
|
|
|
|
|
testDC := "dc1"
|
|
|
|
|
2020-06-05 19:56:19 +00:00
|
|
|
cases := map[string]testCase{
|
|
|
|
"enabled": {
|
2020-07-07 19:39:04 +00:00
|
|
|
config: Config{
|
2021-08-06 22:39:39 +00:00
|
|
|
Datacenter: testDC,
|
|
|
|
PrimaryDatacenter: testDC,
|
|
|
|
ACLsEnabled: true,
|
|
|
|
ACLResolverSettings: ACLResolverSettings{
|
|
|
|
ACLPolicyTTL: 7 * time.Second,
|
|
|
|
ACLRoleTTL: 10 * time.Second,
|
|
|
|
ACLTokenTTL: 12 * time.Second,
|
|
|
|
ACLDefaultPolicy: "allow",
|
|
|
|
ACLDownPolicy: "deny",
|
|
|
|
},
|
2020-07-07 19:39:04 +00:00
|
|
|
ACLEnableKeyListPolicy: true,
|
2020-06-05 19:56:19 +00:00
|
|
|
},
|
2020-07-07 19:39:04 +00:00
|
|
|
expectACLToken: true,
|
2022-03-30 16:51:56 +00:00
|
|
|
expected: &pbautoconf.AutoConfigResponse{
|
2020-07-28 19:31:48 +00:00
|
|
|
Config: &pbconfig.Config{
|
|
|
|
ACL: &pbconfig.ACL{
|
|
|
|
Enabled: true,
|
|
|
|
PolicyTTL: "7s",
|
|
|
|
RoleTTL: "10s",
|
|
|
|
TokenTTL: "12s",
|
|
|
|
DownPolicy: "deny",
|
|
|
|
DefaultPolicy: "allow",
|
|
|
|
EnableKeyListPolicy: true,
|
|
|
|
Tokens: &pbconfig.ACLTokens{
|
|
|
|
Agent: tokenSecret,
|
|
|
|
},
|
2020-07-07 19:39:04 +00:00
|
|
|
},
|
2020-06-05 19:56:19 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"disabled": {
|
2020-07-07 19:39:04 +00:00
|
|
|
config: Config{
|
2021-08-06 22:39:39 +00:00
|
|
|
Datacenter: testDC,
|
|
|
|
PrimaryDatacenter: testDC,
|
|
|
|
ACLsEnabled: false,
|
|
|
|
ACLResolverSettings: ACLResolverSettings{
|
|
|
|
ACLPolicyTTL: 7 * time.Second,
|
|
|
|
ACLRoleTTL: 10 * time.Second,
|
|
|
|
ACLTokenTTL: 12 * time.Second,
|
|
|
|
ACLDefaultPolicy: "allow",
|
|
|
|
ACLDownPolicy: "deny",
|
|
|
|
},
|
2020-07-07 19:39:04 +00:00
|
|
|
ACLEnableKeyListPolicy: true,
|
2020-06-05 19:56:19 +00:00
|
|
|
},
|
2020-07-07 19:39:04 +00:00
|
|
|
expectACLToken: false,
|
2022-03-30 16:51:56 +00:00
|
|
|
expected: &pbautoconf.AutoConfigResponse{
|
2020-07-28 19:31:48 +00:00
|
|
|
Config: &pbconfig.Config{
|
|
|
|
ACL: &pbconfig.ACL{
|
|
|
|
Enabled: false,
|
|
|
|
PolicyTTL: "7s",
|
|
|
|
RoleTTL: "10s",
|
|
|
|
TokenTTL: "12s",
|
|
|
|
DownPolicy: "deny",
|
|
|
|
DefaultPolicy: "allow",
|
|
|
|
EnableKeyListPolicy: true,
|
|
|
|
},
|
2020-06-05 19:56:19 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"local-tokens-disabled": {
|
2020-07-07 19:39:04 +00:00
|
|
|
config: Config{
|
|
|
|
Datacenter: testDC,
|
|
|
|
PrimaryDatacenter: "somewhere-else",
|
|
|
|
ACLsEnabled: true,
|
2020-06-05 19:56:19 +00:00
|
|
|
},
|
2020-07-07 19:39:04 +00:00
|
|
|
expectACLToken: true,
|
|
|
|
err: fmt.Errorf("Agent Auto Configuration requires local token usage to be enabled in this datacenter"),
|
2020-06-05 19:56:19 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
for name, tcase := range cases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
2020-07-07 19:39:04 +00:00
|
|
|
backend := &mockAutoConfigBackend{}
|
|
|
|
expectedTemplate := &structs.ACLToken{
|
|
|
|
Description: `Auto Config Token for Node "something"`,
|
|
|
|
Local: true,
|
|
|
|
NodeIdentities: []*structs.ACLNodeIdentity{
|
|
|
|
{
|
|
|
|
NodeName: "something",
|
|
|
|
Datacenter: testDC,
|
|
|
|
},
|
|
|
|
},
|
2021-07-22 18:20:45 +00:00
|
|
|
EnterpriseMeta: *structs.DefaultEnterpriseMetaInDefaultPartition(),
|
2020-07-07 19:39:04 +00:00
|
|
|
}
|
2020-06-05 19:56:19 +00:00
|
|
|
|
2020-07-07 19:39:04 +00:00
|
|
|
testToken := &structs.ACLToken{
|
|
|
|
AccessorID: tokenAccessor,
|
|
|
|
SecretID: tokenSecret,
|
|
|
|
Description: `Auto Config Token for Node "something"`,
|
|
|
|
Local: true,
|
|
|
|
NodeIdentities: []*structs.ACLNodeIdentity{
|
|
|
|
{
|
|
|
|
NodeName: "something",
|
|
|
|
Datacenter: testDC,
|
|
|
|
},
|
|
|
|
},
|
2021-07-22 18:20:45 +00:00
|
|
|
EnterpriseMeta: *structs.DefaultEnterpriseMetaInDefaultPartition(),
|
2020-07-07 19:39:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if tcase.expectACLToken {
|
|
|
|
backend.On("CreateACLToken", expectedTemplate).Return(testToken, tcase.err).Once()
|
|
|
|
}
|
2020-06-05 19:56:19 +00:00
|
|
|
|
2020-07-08 16:36:11 +00:00
|
|
|
ac := AutoConfig{config: &tcase.config, backend: backend}
|
2020-06-05 19:56:19 +00:00
|
|
|
|
2022-03-30 16:51:56 +00:00
|
|
|
actual := &pbautoconf.AutoConfigResponse{Config: &pbconfig.Config{}}
|
|
|
|
err := ac.updateACLsInConfig(AutoConfigOptions{NodeName: "something"}, actual)
|
2020-07-07 19:39:04 +00:00
|
|
|
if tcase.err != nil {
|
|
|
|
testutil.RequireErrorContains(t, err, tcase.err.Error())
|
2020-06-05 19:56:19 +00:00
|
|
|
} else {
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, tcase.expected, actual)
|
|
|
|
}
|
2020-07-07 19:39:04 +00:00
|
|
|
|
|
|
|
backend.AssertExpectations(t)
|
2020-06-05 19:56:19 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAutoConfig_updateJoinAddressesInConfig(t *testing.T) {
|
2020-07-07 19:39:04 +00:00
|
|
|
addrs := []string{"198.18.0.7:8300", "198.18.0.1:8300"}
|
|
|
|
backend := &mockAutoConfigBackend{}
|
2021-12-01 19:32:34 +00:00
|
|
|
backend.On("DatacenterJoinAddresses", "", "").Return(addrs, nil).Once()
|
2020-06-05 19:56:19 +00:00
|
|
|
|
2020-07-07 19:39:04 +00:00
|
|
|
ac := AutoConfig{backend: backend}
|
2020-06-05 19:56:19 +00:00
|
|
|
|
2020-07-28 19:31:48 +00:00
|
|
|
actual := pbautoconf.AutoConfigResponse{Config: &pbconfig.Config{}}
|
2020-07-07 19:39:04 +00:00
|
|
|
err := ac.updateJoinAddressesInConfig(AutoConfigOptions{}, &actual)
|
2020-06-05 19:56:19 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-07-28 19:31:48 +00:00
|
|
|
require.NotNil(t, actual.Config.Gossip)
|
|
|
|
require.ElementsMatch(t, addrs, actual.Config.Gossip.RetryJoinLAN)
|
2020-07-07 19:39:04 +00:00
|
|
|
|
|
|
|
backend.AssertExpectations(t)
|
2020-06-05 19:56:19 +00:00
|
|
|
}
|
2022-09-13 14:15:20 +00:00
|
|
|
|
2022-09-13 19:21:47 +00:00
|
|
|
func TestAutoConfig_parseAutoConfigCSR(t *testing.T) {
|
|
|
|
// createCSR copies the behavior of connect.CreateCSR with some
|
|
|
|
// customizations to allow for better unit testing.
|
|
|
|
createCSR := func(tmpl *x509.CertificateRequest, privateKey crypto.Signer) (string, error) {
|
|
|
|
connect.HackSANExtensionForCSR(tmpl)
|
2023-02-17 20:04:12 +00:00
|
|
|
bs, err := x509.CreateCertificateRequest(rand.Reader, tmpl, privateKey)
|
2022-09-13 19:21:47 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
var csrBuf bytes.Buffer
|
|
|
|
err = pem.Encode(&csrBuf, &pem.Block{Type: "CERTIFICATE REQUEST", Bytes: bs})
|
|
|
|
require.NoError(t, err)
|
|
|
|
return csrBuf.String(), nil
|
|
|
|
}
|
|
|
|
pk, _, err := connect.GeneratePrivateKey()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
agentURI := connect.SpiffeIDAgent{
|
|
|
|
Host: "test-host",
|
|
|
|
Datacenter: "tdc1",
|
|
|
|
Agent: "test-agent",
|
|
|
|
}.URI()
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
setup func() string
|
|
|
|
expectErr string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "err_garbage_data",
|
|
|
|
expectErr: "Failed to parse CSR",
|
|
|
|
setup: func() string { return "garbage" },
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "err_not_one_uri",
|
|
|
|
expectErr: "CSR SAN contains an invalid number of URIs",
|
|
|
|
setup: func() string {
|
|
|
|
tmpl := &x509.CertificateRequest{
|
|
|
|
URIs: []*url.URL{agentURI, agentURI},
|
|
|
|
SignatureAlgorithm: connect.SigAlgoForKey(pk),
|
|
|
|
}
|
|
|
|
csr, err := createCSR(tmpl, pk)
|
|
|
|
require.NoError(t, err)
|
|
|
|
return csr
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "err_email",
|
|
|
|
expectErr: "CSR SAN does not allow specifying email addresses",
|
|
|
|
setup: func() string {
|
|
|
|
tmpl := &x509.CertificateRequest{
|
|
|
|
URIs: []*url.URL{agentURI},
|
|
|
|
EmailAddresses: []string{"test@example.com"},
|
|
|
|
SignatureAlgorithm: connect.SigAlgoForKey(pk),
|
|
|
|
}
|
|
|
|
csr, err := createCSR(tmpl, pk)
|
|
|
|
require.NoError(t, err)
|
|
|
|
return csr
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "err_spiffe_parse_uri",
|
|
|
|
expectErr: "Failed to parse the SPIFFE URI",
|
|
|
|
setup: func() string {
|
|
|
|
tmpl := &x509.CertificateRequest{
|
|
|
|
URIs: []*url.URL{connect.SpiffeIDAgent{}.URI()},
|
|
|
|
SignatureAlgorithm: connect.SigAlgoForKey(pk),
|
|
|
|
}
|
|
|
|
csr, err := createCSR(tmpl, pk)
|
|
|
|
require.NoError(t, err)
|
|
|
|
return csr
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "err_not_agent",
|
|
|
|
expectErr: "SPIFFE ID is not an Agent ID",
|
|
|
|
setup: func() string {
|
|
|
|
spiffe := connect.SpiffeIDService{
|
|
|
|
Namespace: "tns",
|
|
|
|
Datacenter: "tdc1",
|
|
|
|
Service: "test-service",
|
|
|
|
}
|
|
|
|
tmpl := &x509.CertificateRequest{
|
|
|
|
URIs: []*url.URL{spiffe.URI()},
|
|
|
|
SignatureAlgorithm: connect.SigAlgoForKey(pk),
|
|
|
|
}
|
|
|
|
csr, err := createCSR(tmpl, pk)
|
|
|
|
require.NoError(t, err)
|
|
|
|
return csr
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "success",
|
|
|
|
setup: func() string {
|
|
|
|
tmpl := &x509.CertificateRequest{
|
|
|
|
URIs: []*url.URL{agentURI},
|
|
|
|
SignatureAlgorithm: connect.SigAlgoForKey(pk),
|
|
|
|
}
|
|
|
|
csr, err := createCSR(tmpl, pk)
|
|
|
|
require.NoError(t, err)
|
|
|
|
return csr
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tests {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
req, spif, err := parseAutoConfigCSR(tc.setup())
|
|
|
|
if tc.expectErr != "" {
|
|
|
|
require.Error(t, err)
|
|
|
|
require.Contains(t, err.Error(), tc.expectErr)
|
|
|
|
} else {
|
|
|
|
require.NoError(t, err)
|
|
|
|
// TODO better verification of these
|
|
|
|
require.NotNil(t, req)
|
|
|
|
require.NotNil(t, spif)
|
|
|
|
}
|
|
|
|
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-13 14:15:20 +00:00
|
|
|
func TestAutoConfig_invalidSegmentName(t *testing.T) {
|
|
|
|
invalid := []string{
|
|
|
|
"\n",
|
|
|
|
"\r",
|
|
|
|
"\t",
|
|
|
|
"`",
|
|
|
|
`'`,
|
|
|
|
`"`,
|
|
|
|
` `,
|
|
|
|
`a b`,
|
|
|
|
`a'b`,
|
|
|
|
`a or b`,
|
|
|
|
`a and b`,
|
|
|
|
`segment name`,
|
|
|
|
`segment"name`,
|
|
|
|
`"segment"name`,
|
|
|
|
`"segment" name`,
|
|
|
|
`segment'name'`,
|
|
|
|
}
|
|
|
|
valid := []string{
|
|
|
|
``,
|
|
|
|
`a`,
|
|
|
|
`a.b`,
|
|
|
|
`a.b.c`,
|
|
|
|
`a-b-c`,
|
|
|
|
`segment.name`,
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, s := range invalid {
|
|
|
|
require.True(t, invalidSegmentName.MatchString(s), "incorrect match: %v", s)
|
|
|
|
}
|
|
|
|
for _, s := range valid {
|
|
|
|
require.False(t, invalidSegmentName.MatchString(s), "incorrect match: %v", s)
|
|
|
|
}
|
|
|
|
}
|