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-26 00:39:18 +00:00
|
|
|
package connect
|
|
|
|
|
|
|
|
import (
|
2022-06-10 21:15:22 +00:00
|
|
|
"fmt"
|
2018-03-26 00:39:18 +00:00
|
|
|
"net/url"
|
2021-06-25 21:47:47 +00:00
|
|
|
|
2022-04-05 21:10:06 +00:00
|
|
|
"github.com/hashicorp/consul/acl"
|
2023-09-07 15:37:15 +00:00
|
|
|
"github.com/hashicorp/consul/proto-public/pbresource"
|
2018-03-26 00:39:18 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// SpiffeIDService is the structure to represent the SPIFFE ID for a service.
|
|
|
|
type SpiffeIDService struct {
|
|
|
|
Host string
|
2021-06-25 21:47:47 +00:00
|
|
|
Partition string
|
2018-03-26 00:39:18 +00:00
|
|
|
Namespace string
|
|
|
|
Datacenter string
|
|
|
|
Service string
|
|
|
|
}
|
|
|
|
|
2021-06-25 21:47:47 +00:00
|
|
|
func (id SpiffeIDService) NamespaceOrDefault() string {
|
2022-04-05 21:10:06 +00:00
|
|
|
return acl.NamespaceOrDefault(id.Namespace)
|
2021-06-25 21:47:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (id SpiffeIDService) MatchesPartition(partition string) bool {
|
2022-04-05 21:10:06 +00:00
|
|
|
return id.PartitionOrDefault() == acl.PartitionOrDefault(partition)
|
2021-06-25 21:47:47 +00:00
|
|
|
}
|
|
|
|
|
2018-03-26 00:39:18 +00:00
|
|
|
// URI returns the *url.URL for this SPIFFE ID.
|
2021-06-25 21:47:47 +00:00
|
|
|
func (id SpiffeIDService) URI() *url.URL {
|
2018-03-26 00:39:18 +00:00
|
|
|
var result url.URL
|
|
|
|
result.Scheme = "spiffe"
|
|
|
|
result.Host = id.Host
|
2021-06-25 21:47:47 +00:00
|
|
|
result.Path = id.uriPath()
|
2018-03-26 00:39:18 +00:00
|
|
|
return &result
|
|
|
|
}
|
2022-06-10 21:15:22 +00:00
|
|
|
|
|
|
|
func (id SpiffeIDService) uriPath() string {
|
|
|
|
path := fmt.Sprintf("/ns/%s/dc/%s/svc/%s",
|
|
|
|
id.NamespaceOrDefault(),
|
|
|
|
id.Datacenter,
|
|
|
|
id.Service,
|
|
|
|
)
|
|
|
|
|
2023-08-22 14:46:03 +00:00
|
|
|
// Although CE has no support for partitions, it still needs to be able to
|
2022-06-10 21:15:22 +00:00
|
|
|
// handle exportedPartition from peered Consul Enterprise clusters in order
|
|
|
|
// to generate the correct SpiffeID.
|
2023-08-22 14:46:03 +00:00
|
|
|
// We intentionally avoid using pbpartition.DefaultName here to be CE friendly.
|
2022-06-10 21:15:22 +00:00
|
|
|
if ap := id.PartitionOrDefault(); ap != "" && ap != "default" {
|
|
|
|
return "/ap/" + ap + path
|
|
|
|
}
|
|
|
|
return path
|
|
|
|
}
|
2023-09-07 15:37:15 +00:00
|
|
|
|
|
|
|
// SpiffeIDFromIdentityRef creates the SPIFFE ID from a workload identity.
|
|
|
|
// TODO (ishustava): make sure ref type is workload identity.
|
|
|
|
func SpiffeIDFromIdentityRef(trustDomain string, ref *pbresource.Reference) string {
|
|
|
|
return SpiffeIDWorkloadIdentity{
|
2023-09-12 19:56:43 +00:00
|
|
|
TrustDomain: trustDomain,
|
|
|
|
Partition: ref.Tenancy.Partition,
|
|
|
|
Namespace: ref.Tenancy.Namespace,
|
|
|
|
WorkloadIdentity: ref.Name,
|
2023-09-07 15:37:15 +00:00
|
|
|
}.URI().String()
|
|
|
|
}
|