2021-10-20 19:24:18 +00:00
|
|
|
package structs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/hashicorp/consul/acl"
|
|
|
|
)
|
|
|
|
|
2021-10-22 16:11:03 +00:00
|
|
|
// PartitionExportsConfigEntry is the top-level struct for exporting a service to be exposed
|
2021-10-20 19:24:18 +00:00
|
|
|
// across other admin partitions.
|
2021-10-22 16:11:03 +00:00
|
|
|
type PartitionExportsConfigEntry struct {
|
|
|
|
Name string
|
2021-10-20 19:24:18 +00:00
|
|
|
|
|
|
|
// Services is a list of services to be exported and the list of partitions
|
|
|
|
// to expose them to.
|
|
|
|
Services []ExportedService
|
|
|
|
|
|
|
|
Meta map[string]string `json:",omitempty"`
|
|
|
|
EnterpriseMeta `hcl:",squash" mapstructure:",squash"`
|
|
|
|
RaftIndex
|
|
|
|
}
|
|
|
|
|
|
|
|
// ExportedService manages the exporting of a service in the local partition to
|
|
|
|
// other partitions.
|
|
|
|
type ExportedService struct {
|
|
|
|
// Name is the name of the service to be exported.
|
|
|
|
Name string
|
|
|
|
|
|
|
|
// Namespace is the namespace to export the service from.
|
|
|
|
Namespace string `json:",omitempty"`
|
|
|
|
|
|
|
|
// Consumers is a list of downstream consumers of the service to be exported.
|
|
|
|
Consumers []ServiceConsumer
|
|
|
|
}
|
|
|
|
|
|
|
|
// ServiceConsumer represents a downstream consumer of the service to be exported.
|
|
|
|
type ServiceConsumer struct {
|
|
|
|
// Partition is the admin partition to export the service to.
|
|
|
|
Partition string
|
|
|
|
}
|
|
|
|
|
2021-10-27 16:47:57 +00:00
|
|
|
func (e *PartitionExportsConfigEntry) ToMap() map[string]map[string][]string {
|
|
|
|
resp := make(map[string]map[string][]string)
|
|
|
|
for _, svc := range e.Services {
|
|
|
|
if _, ok := resp[svc.Namespace]; !ok {
|
|
|
|
resp[svc.Namespace] = make(map[string][]string)
|
|
|
|
}
|
|
|
|
if _, ok := resp[svc.Namespace][svc.Name]; !ok {
|
|
|
|
consumers := make([]string, 0, len(svc.Consumers))
|
|
|
|
for _, c := range svc.Consumers {
|
|
|
|
consumers = append(consumers, c.Partition)
|
|
|
|
}
|
|
|
|
resp[svc.Namespace][svc.Name] = consumers
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return resp
|
|
|
|
}
|
|
|
|
|
2021-10-22 16:11:03 +00:00
|
|
|
func (e *PartitionExportsConfigEntry) Clone() *PartitionExportsConfigEntry {
|
2021-10-20 19:24:18 +00:00
|
|
|
e2 := *e
|
|
|
|
e2.Services = make([]ExportedService, len(e.Services))
|
|
|
|
for _, svc := range e.Services {
|
|
|
|
exportedSvc := svc
|
|
|
|
exportedSvc.Consumers = make([]ServiceConsumer, len(svc.Consumers))
|
|
|
|
for _, consumer := range svc.Consumers {
|
|
|
|
exportedSvc.Consumers = append(exportedSvc.Consumers, consumer)
|
|
|
|
}
|
|
|
|
e2.Services = append(e2.Services, exportedSvc)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &e2
|
|
|
|
}
|
|
|
|
|
2021-10-22 16:11:03 +00:00
|
|
|
func (e *PartitionExportsConfigEntry) GetKind() string {
|
|
|
|
return PartitionExports
|
2021-10-20 19:24:18 +00:00
|
|
|
}
|
|
|
|
|
2021-10-22 16:11:03 +00:00
|
|
|
func (e *PartitionExportsConfigEntry) GetName() string {
|
2021-10-20 19:24:18 +00:00
|
|
|
if e == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2021-10-22 16:11:03 +00:00
|
|
|
return e.Name
|
2021-10-20 19:24:18 +00:00
|
|
|
}
|
|
|
|
|
2021-10-22 16:11:03 +00:00
|
|
|
func (e *PartitionExportsConfigEntry) GetMeta() map[string]string {
|
2021-10-20 19:24:18 +00:00
|
|
|
if e == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return e.Meta
|
|
|
|
}
|
|
|
|
|
2021-10-22 16:11:03 +00:00
|
|
|
func (e *PartitionExportsConfigEntry) Normalize() error {
|
2021-10-20 19:24:18 +00:00
|
|
|
if e == nil {
|
|
|
|
return fmt.Errorf("config entry is nil")
|
|
|
|
}
|
2021-10-22 16:11:03 +00:00
|
|
|
e.EnterpriseMeta = *DefaultEnterpriseMetaInPartition(e.Name)
|
2021-10-20 19:24:18 +00:00
|
|
|
e.EnterpriseMeta.Normalize()
|
|
|
|
|
|
|
|
for i := range e.Services {
|
|
|
|
e.Services[i].Namespace = NamespaceOrDefault(e.Services[i].Namespace)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-10-22 16:11:03 +00:00
|
|
|
func (e *PartitionExportsConfigEntry) Validate() error {
|
|
|
|
if e.Name == "" {
|
|
|
|
return fmt.Errorf("Name is required")
|
2021-10-20 19:24:18 +00:00
|
|
|
}
|
2021-10-22 16:11:03 +00:00
|
|
|
if e.Name == WildcardSpecifier {
|
|
|
|
return fmt.Errorf("partition-exports Name must be the name of a partition, and not a wildcard")
|
2021-10-20 19:24:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
validationErr := validateConfigEntryMeta(e.Meta)
|
|
|
|
|
|
|
|
for _, svc := range e.Services {
|
|
|
|
if svc.Name == "" {
|
|
|
|
return fmt.Errorf("service name cannot be empty")
|
|
|
|
}
|
|
|
|
if len(svc.Consumers) == 0 {
|
|
|
|
return fmt.Errorf("service %q must have at least one consumer", svc.Name)
|
|
|
|
}
|
|
|
|
for _, consumer := range svc.Consumers {
|
|
|
|
if consumer.Partition == WildcardSpecifier {
|
|
|
|
return fmt.Errorf("exporting to all partitions (wildcard) is not yet supported")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return validationErr
|
|
|
|
}
|
|
|
|
|
2021-10-22 16:11:03 +00:00
|
|
|
func (e *PartitionExportsConfigEntry) CanRead(authz acl.Authorizer) bool {
|
2021-10-20 19:24:18 +00:00
|
|
|
var authzContext acl.AuthorizerContext
|
|
|
|
e.FillAuthzContext(&authzContext)
|
|
|
|
return authz.MeshRead(&authzContext) == acl.Allow
|
|
|
|
}
|
|
|
|
|
2021-10-22 16:11:03 +00:00
|
|
|
func (e *PartitionExportsConfigEntry) CanWrite(authz acl.Authorizer) bool {
|
2021-10-20 19:24:18 +00:00
|
|
|
var authzContext acl.AuthorizerContext
|
|
|
|
e.FillAuthzContext(&authzContext)
|
|
|
|
return authz.MeshWrite(&authzContext) == acl.Allow
|
|
|
|
}
|
|
|
|
|
2021-10-22 16:11:03 +00:00
|
|
|
func (e *PartitionExportsConfigEntry) GetRaftIndex() *RaftIndex {
|
2021-10-20 19:24:18 +00:00
|
|
|
if e == nil {
|
|
|
|
return &RaftIndex{}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &e.RaftIndex
|
|
|
|
}
|
|
|
|
|
2021-10-22 16:11:03 +00:00
|
|
|
func (e *PartitionExportsConfigEntry) GetEnterpriseMeta() *EnterpriseMeta {
|
2021-10-20 19:24:18 +00:00
|
|
|
if e == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return &e.EnterpriseMeta
|
|
|
|
}
|