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
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"invalid scheme",
|
|
|
|
"http://google.com/",
|
|
|
|
nil,
|
|
|
|
"scheme",
|
2018-03-19 20:53:57 +00:00
|
|
|
},
|
2021-06-25 21:47:47 +00:00
|
|
|
{
|
|
|
|
"basic service ID",
|
|
|
|
"spiffe://1234.consul/ns/default/dc/dc01/svc/web",
|
|
|
|
&SpiffeIDService{
|
|
|
|
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",
|
|
|
|
},
|
|
|
|
"",
|
2019-06-27 20:22:07 +00:00
|
|
|
},
|
2021-06-25 21:47:47 +00:00
|
|
|
{
|
|
|
|
"basic service ID with partition",
|
|
|
|
"spiffe://1234.consul/ap/bizdev/ns/default/dc/dc01/svc/web",
|
|
|
|
&SpiffeIDService{
|
|
|
|
Host: "1234.consul",
|
|
|
|
Partition: "bizdev",
|
|
|
|
Namespace: "default",
|
|
|
|
Datacenter: "dc01",
|
|
|
|
Service: "web",
|
|
|
|
},
|
|
|
|
"",
|
2018-03-27 03:31:17 +00:00
|
|
|
},
|
2021-06-25 21:47:47 +00:00
|
|
|
{
|
|
|
|
"basic agent ID",
|
|
|
|
"spiffe://1234.consul/agent/client/dc/dc1/id/uuid",
|
|
|
|
&SpiffeIDAgent{
|
|
|
|
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",
|
|
|
|
},
|
|
|
|
"",
|
2018-03-24 18:46:12 +00:00
|
|
|
},
|
2021-06-25 21:47:47 +00:00
|
|
|
{
|
|
|
|
"basic agent ID with partition",
|
|
|
|
"spiffe://1234.consul/ap/bizdev/agent/client/dc/dc1/id/uuid",
|
|
|
|
&SpiffeIDAgent{
|
|
|
|
Host: "1234.consul",
|
|
|
|
Partition: "bizdev",
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Agent: "uuid",
|
|
|
|
},
|
|
|
|
"",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"service with URL-encoded values",
|
|
|
|
"spiffe://1234.consul/ns/foo%2Fbar/dc/bar%2Fbaz/svc/baz%2Fqux",
|
|
|
|
&SpiffeIDService{
|
|
|
|
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",
|
|
|
|
},
|
|
|
|
"",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"service with URL-encoded values with partition",
|
|
|
|
"spiffe://1234.consul/ap/biz%2Fdev/ns/foo%2Fbar/dc/bar%2Fbaz/svc/baz%2Fqux",
|
|
|
|
&SpiffeIDService{
|
|
|
|
Host: "1234.consul",
|
|
|
|
Partition: "biz/dev",
|
|
|
|
Namespace: "foo/bar",
|
|
|
|
Datacenter: "bar/baz",
|
|
|
|
Service: "baz/qux",
|
|
|
|
},
|
|
|
|
"",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"signing ID",
|
|
|
|
"spiffe://1234.consul",
|
|
|
|
&SpiffeIDSigning{
|
|
|
|
ClusterID: "1234",
|
|
|
|
Domain: "consul",
|
|
|
|
},
|
|
|
|
"",
|
|
|
|
},
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|