2023-03-28 18:39:22 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
2023-08-11 13:12:13 +00:00
|
|
|
// SPDX-License-Identifier: BUSL-1.1
|
2023-03-28 18:39:22 +00:00
|
|
|
|
2018-03-19 20:53:57 +00:00
|
|
|
package connect
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2021-06-25 21:47:47 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2018-03-19 20:53:57 +00:00
|
|
|
|
2021-12-01 19:32:34 +00:00
|
|
|
"github.com/hashicorp/consul/agent/structs"
|
2021-06-25 21:47:47 +00:00
|
|
|
"github.com/hashicorp/consul/sdk/testutil"
|
|
|
|
)
|
2018-03-19 20:53:57 +00:00
|
|
|
|
2021-06-25 21:47:47 +00:00
|
|
|
func TestParseCertURIFromString(t *testing.T) {
|
2021-12-01 19:32:34 +00:00
|
|
|
defaultEntMeta := structs.DefaultEnterpriseMetaInDefaultPartition()
|
|
|
|
|
2021-06-25 21:47:47 +00:00
|
|
|
var cases = []struct {
|
|
|
|
Name string
|
|
|
|
URI string
|
|
|
|
Struct interface{}
|
|
|
|
ParseError string
|
|
|
|
}{
|
|
|
|
{
|
2022-09-06 23:58:13 +00:00
|
|
|
Name: "invalid scheme",
|
|
|
|
URI: "http://google.com/",
|
|
|
|
Struct: nil,
|
|
|
|
ParseError: "scheme",
|
2018-03-19 20:53:57 +00:00
|
|
|
},
|
2021-06-25 21:47:47 +00:00
|
|
|
{
|
2022-09-06 23:58:13 +00:00
|
|
|
Name: "basic service ID",
|
|
|
|
URI: "spiffe://1234.consul/ns/default/dc/dc01/svc/web",
|
|
|
|
Struct: &SpiffeIDService{
|
2021-06-25 21:47:47 +00:00
|
|
|
Host: "1234.consul",
|
2021-12-01 19:32:34 +00:00
|
|
|
Partition: defaultEntMeta.PartitionOrDefault(),
|
2021-06-25 21:47:47 +00:00
|
|
|
Namespace: "default",
|
|
|
|
Datacenter: "dc01",
|
|
|
|
Service: "web",
|
|
|
|
},
|
2022-09-06 23:58:13 +00:00
|
|
|
ParseError: "",
|
2019-06-27 20:22:07 +00:00
|
|
|
},
|
2021-06-25 21:47:47 +00:00
|
|
|
{
|
2022-09-06 23:58:13 +00:00
|
|
|
Name: "basic service ID with partition",
|
|
|
|
URI: "spiffe://1234.consul/ap/bizdev/ns/default/dc/dc01/svc/web",
|
|
|
|
Struct: &SpiffeIDService{
|
2021-06-25 21:47:47 +00:00
|
|
|
Host: "1234.consul",
|
|
|
|
Partition: "bizdev",
|
|
|
|
Namespace: "default",
|
|
|
|
Datacenter: "dc01",
|
|
|
|
Service: "web",
|
|
|
|
},
|
2022-09-06 23:58:13 +00:00
|
|
|
ParseError: "",
|
2018-03-27 03:31:17 +00:00
|
|
|
},
|
2023-09-12 19:56:43 +00:00
|
|
|
{
|
|
|
|
Name: "basic workload ID",
|
|
|
|
URI: "spiffe://1234.consul/ap/default/ns/default/identity/web",
|
|
|
|
Struct: &SpiffeIDWorkloadIdentity{
|
|
|
|
TrustDomain: "1234.consul",
|
|
|
|
Partition: defaultEntMeta.PartitionOrDefault(),
|
|
|
|
Namespace: "default",
|
|
|
|
WorkloadIdentity: "web",
|
|
|
|
},
|
|
|
|
ParseError: "",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "basic workload ID with nondefault partition",
|
|
|
|
URI: "spiffe://1234.consul/ap/bizdev/ns/default/identity/web",
|
|
|
|
Struct: &SpiffeIDWorkloadIdentity{
|
|
|
|
TrustDomain: "1234.consul",
|
|
|
|
Partition: "bizdev",
|
|
|
|
Namespace: "default",
|
|
|
|
WorkloadIdentity: "web",
|
|
|
|
},
|
|
|
|
ParseError: "",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "workload ID error - missing identity",
|
|
|
|
URI: "spiffe://1234.consul/ns/default",
|
|
|
|
Struct: &SpiffeIDWorkloadIdentity{
|
|
|
|
TrustDomain: "1234.consul",
|
|
|
|
Partition: defaultEntMeta.PartitionOrDefault(),
|
|
|
|
Namespace: "default",
|
|
|
|
WorkloadIdentity: "web",
|
|
|
|
},
|
|
|
|
ParseError: "SPIFFE ID is not in the expected format",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "workload ID error - missing partition",
|
|
|
|
URI: "spiffe://1234.consul/ns/default/identity/web",
|
|
|
|
Struct: &SpiffeIDWorkloadIdentity{
|
|
|
|
TrustDomain: "1234.consul",
|
|
|
|
Partition: defaultEntMeta.PartitionOrDefault(),
|
|
|
|
Namespace: "default",
|
|
|
|
WorkloadIdentity: "web",
|
|
|
|
},
|
|
|
|
ParseError: "SPIFFE ID is not in the expected format",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "workload ID error - missing namespace",
|
|
|
|
URI: "spiffe://1234.consul/ap/default/identity/web",
|
|
|
|
Struct: &SpiffeIDWorkloadIdentity{
|
|
|
|
TrustDomain: "1234.consul",
|
|
|
|
Partition: defaultEntMeta.PartitionOrDefault(),
|
|
|
|
Namespace: "default",
|
|
|
|
WorkloadIdentity: "web",
|
|
|
|
},
|
|
|
|
ParseError: "SPIFFE ID is not in the expected format",
|
|
|
|
},
|
2021-06-25 21:47:47 +00:00
|
|
|
{
|
2022-09-06 23:58:13 +00:00
|
|
|
Name: "basic agent ID",
|
|
|
|
URI: "spiffe://1234.consul/agent/client/dc/dc1/id/uuid",
|
|
|
|
Struct: &SpiffeIDAgent{
|
2021-06-25 21:47:47 +00:00
|
|
|
Host: "1234.consul",
|
2021-12-01 19:32:34 +00:00
|
|
|
Partition: defaultEntMeta.PartitionOrDefault(),
|
2021-06-25 21:47:47 +00:00
|
|
|
Datacenter: "dc1",
|
|
|
|
Agent: "uuid",
|
|
|
|
},
|
2022-09-06 23:58:13 +00:00
|
|
|
ParseError: "",
|
2018-03-24 18:46:12 +00:00
|
|
|
},
|
2021-06-25 21:47:47 +00:00
|
|
|
{
|
2022-09-06 23:58:13 +00:00
|
|
|
Name: "basic agent ID with partition",
|
|
|
|
URI: "spiffe://1234.consul/ap/bizdev/agent/client/dc/dc1/id/uuid",
|
|
|
|
Struct: &SpiffeIDAgent{
|
2021-06-25 21:47:47 +00:00
|
|
|
Host: "1234.consul",
|
|
|
|
Partition: "bizdev",
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Agent: "uuid",
|
|
|
|
},
|
2022-09-06 23:58:13 +00:00
|
|
|
ParseError: "",
|
2021-06-25 21:47:47 +00:00
|
|
|
},
|
2022-06-15 19:36:18 +00:00
|
|
|
{
|
2022-09-06 23:58:13 +00:00
|
|
|
Name: "basic server",
|
|
|
|
URI: "spiffe://1234.consul/agent/server/dc/dc1",
|
|
|
|
Struct: &SpiffeIDServer{
|
|
|
|
Host: "1234.consul",
|
|
|
|
Datacenter: "dc1",
|
|
|
|
},
|
|
|
|
ParseError: "",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "mesh-gateway with no partition",
|
|
|
|
URI: "spiffe://1234.consul/gateway/mesh/dc/dc1",
|
|
|
|
Struct: &SpiffeIDMeshGateway{
|
2022-06-15 19:36:18 +00:00
|
|
|
Host: "1234.consul",
|
|
|
|
Partition: "default",
|
|
|
|
Datacenter: "dc1",
|
|
|
|
},
|
2022-09-06 23:58:13 +00:00
|
|
|
ParseError: "",
|
2022-06-15 19:36:18 +00:00
|
|
|
},
|
|
|
|
{
|
2022-09-06 23:58:13 +00:00
|
|
|
Name: "mesh-gateway with partition",
|
|
|
|
URI: "spiffe://1234.consul/ap/bizdev/gateway/mesh/dc/dc1",
|
|
|
|
Struct: &SpiffeIDMeshGateway{
|
2022-06-15 19:36:18 +00:00
|
|
|
Host: "1234.consul",
|
|
|
|
Partition: "bizdev",
|
|
|
|
Datacenter: "dc1",
|
|
|
|
},
|
2022-09-06 23:58:13 +00:00
|
|
|
ParseError: "",
|
2022-06-15 19:36:18 +00:00
|
|
|
},
|
2021-06-25 21:47:47 +00:00
|
|
|
{
|
2022-09-06 23:58:13 +00:00
|
|
|
Name: "service with URL-encoded values",
|
|
|
|
URI: "spiffe://1234.consul/ns/foo%2Fbar/dc/bar%2Fbaz/svc/baz%2Fqux",
|
|
|
|
Struct: &SpiffeIDService{
|
2021-06-25 21:47:47 +00:00
|
|
|
Host: "1234.consul",
|
2021-12-01 19:32:34 +00:00
|
|
|
Partition: defaultEntMeta.PartitionOrDefault(),
|
2021-06-25 21:47:47 +00:00
|
|
|
Namespace: "foo/bar",
|
|
|
|
Datacenter: "bar/baz",
|
|
|
|
Service: "baz/qux",
|
|
|
|
},
|
2022-09-06 23:58:13 +00:00
|
|
|
ParseError: "",
|
2021-06-25 21:47:47 +00:00
|
|
|
},
|
|
|
|
{
|
2022-09-06 23:58:13 +00:00
|
|
|
Name: "service with URL-encoded values with partition",
|
|
|
|
URI: "spiffe://1234.consul/ap/biz%2Fdev/ns/foo%2Fbar/dc/bar%2Fbaz/svc/baz%2Fqux",
|
|
|
|
Struct: &SpiffeIDService{
|
2021-06-25 21:47:47 +00:00
|
|
|
Host: "1234.consul",
|
|
|
|
Partition: "biz/dev",
|
|
|
|
Namespace: "foo/bar",
|
|
|
|
Datacenter: "bar/baz",
|
|
|
|
Service: "baz/qux",
|
|
|
|
},
|
2022-09-06 23:58:13 +00:00
|
|
|
ParseError: "",
|
2021-06-25 21:47:47 +00:00
|
|
|
},
|
|
|
|
{
|
2022-09-06 23:58:13 +00:00
|
|
|
Name: "signing ID",
|
|
|
|
URI: "spiffe://1234.consul",
|
|
|
|
Struct: &SpiffeIDSigning{
|
2021-06-25 21:47:47 +00:00
|
|
|
ClusterID: "1234",
|
|
|
|
Domain: "consul",
|
|
|
|
},
|
2022-09-06 23:58:13 +00:00
|
|
|
ParseError: "",
|
2021-06-25 21:47:47 +00:00
|
|
|
},
|
|
|
|
}
|
2018-03-19 20:53:57 +00:00
|
|
|
|
2021-06-25 21:47:47 +00:00
|
|
|
for _, tc := range cases {
|
2018-03-19 20:53:57 +00:00
|
|
|
t.Run(tc.Name, func(t *testing.T) {
|
2018-10-03 18:18:55 +00:00
|
|
|
actual, err := ParseCertURIFromString(tc.URI)
|
2021-06-25 21:47:47 +00:00
|
|
|
if tc.ParseError != "" {
|
|
|
|
require.Error(t, err)
|
|
|
|
require.Contains(t, err.Error(), tc.ParseError)
|
|
|
|
testutil.RequireErrorContains(t, err, tc.ParseError)
|
|
|
|
} else {
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, tc.Struct, actual)
|
2018-03-19 20:53:57 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2022-09-06 23:58:13 +00:00
|
|
|
|
|
|
|
func TestSpiffeIDServer_URI(t *testing.T) {
|
|
|
|
srv := &SpiffeIDServer{
|
|
|
|
Host: "1234.consul",
|
|
|
|
Datacenter: "dc1",
|
|
|
|
}
|
|
|
|
|
|
|
|
require.Equal(t, "spiffe://1234.consul/agent/server/dc/dc1", srv.URI().String())
|
|
|
|
}
|
2022-09-09 18:57:30 +00:00
|
|
|
|
|
|
|
func TestServerSAN(t *testing.T) {
|
|
|
|
san := PeeringServerSAN("dc1", TestTrustDomain)
|
|
|
|
expect := "server.dc1.peering." + TestTrustDomain
|
|
|
|
require.Equal(t, expect, san)
|
|
|
|
}
|