2023-03-28 18:39:22 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2020-08-31 17:12:17 +00:00
|
|
|
package autoconf
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/hashicorp/consul/agent/connect"
|
|
|
|
"github.com/hashicorp/consul/agent/structs"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func newLeaf(t *testing.T, agentName, datacenter string, ca *structs.CARoot, idx uint64, expiration time.Duration) *structs.IssuedCert {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
pub, priv, err := connect.TestAgentLeaf(t, agentName, datacenter, ca, expiration)
|
|
|
|
require.NoError(t, err)
|
|
|
|
cert, err := connect.ParseCert(pub)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
spiffeID, err := connect.ParseCertURI(cert.URIs[0])
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
agentID, ok := spiffeID.(*connect.SpiffeIDAgent)
|
|
|
|
require.True(t, ok, "certificate doesn't have an agent leaf cert URI")
|
|
|
|
|
2022-03-16 16:12:29 +00:00
|
|
|
return issuedCertRoundtrip(t, &structs.IssuedCert{
|
2020-08-31 17:12:17 +00:00
|
|
|
SerialNumber: cert.SerialNumber.String(),
|
|
|
|
CertPEM: pub,
|
|
|
|
PrivateKeyPEM: priv,
|
|
|
|
ValidAfter: cert.NotBefore,
|
|
|
|
ValidBefore: cert.NotAfter,
|
|
|
|
Agent: agentID.Agent,
|
|
|
|
AgentURI: agentID.URI().String(),
|
2021-07-22 18:20:45 +00:00
|
|
|
EnterpriseMeta: *structs.DefaultEnterpriseMetaInDefaultPartition(),
|
2020-08-31 17:12:17 +00:00
|
|
|
RaftIndex: structs.RaftIndex{
|
|
|
|
CreateIndex: idx,
|
|
|
|
ModifyIndex: idx,
|
|
|
|
},
|
2022-03-16 16:12:29 +00:00
|
|
|
})
|
2020-08-31 17:12:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func testCerts(t *testing.T, agentName, datacenter string) (*structs.CARoot, *structs.IndexedCARoots, *structs.IssuedCert) {
|
|
|
|
ca := connect.TestCA(t, nil)
|
|
|
|
cert := newLeaf(t, agentName, datacenter, ca, 1, 10*time.Minute)
|
2022-03-16 16:12:29 +00:00
|
|
|
indexedRoots := caRootsRoundtrip(t, &structs.IndexedCARoots{
|
2020-08-31 17:12:17 +00:00
|
|
|
ActiveRootID: ca.ID,
|
|
|
|
TrustDomain: connect.TestClusterID,
|
|
|
|
Roots: []*structs.CARoot{
|
|
|
|
ca,
|
|
|
|
},
|
|
|
|
QueryMeta: structs.QueryMeta{Index: 1},
|
2022-03-16 16:12:29 +00:00
|
|
|
})
|
2020-08-31 17:12:17 +00:00
|
|
|
|
2022-03-16 16:12:29 +00:00
|
|
|
return ca, indexedRoots, cert
|
2020-08-31 17:12:17 +00:00
|
|
|
}
|