consul/agent/structs/config_entry_exports.go

195 lines
5.1 KiB
Go
Raw Normal View History

// Copyright (c) HashiCorp, Inc.
[COMPLIANCE] License changes (#18443) * Adding explicit MPL license for sub-package This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository. * Adding explicit MPL license for sub-package This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository. * Updating the license from MPL to Business Source License Going forward, this project will be licensed under the Business Source License v1.1. Please see our blog post for more details at <Blog URL>, FAQ at www.hashicorp.com/licensing-faq, and details of the license at www.hashicorp.com/bsl. * add missing license headers * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 --------- Co-authored-by: hashicorp-copywrite[bot] <110428419+hashicorp-copywrite[bot]@users.noreply.github.com>
2023-08-11 13:12:13 +00:00
// SPDX-License-Identifier: BUSL-1.1
2021-10-20 19:24:18 +00:00
package structs
import (
"encoding/json"
2021-10-20 19:24:18 +00:00
"fmt"
"github.com/hashicorp/consul/acl"
)
// ExportedServicesConfigEntry is the top-level struct for exporting a service to be exposed
2021-10-20 19:24:18 +00:00
// across other admin partitions.
type ExportedServicesConfigEntry 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 `json:",omitempty"`
2021-10-20 19:24:18 +00:00
Meta map[string]string `json:",omitempty"`
Hash uint64 `json:",omitempty" hash:"ignore"`
acl.EnterpriseMeta `hcl:",squash" mapstructure:",squash"`
RaftIndex `hash:"ignore"`
}
func (e *ExportedServicesConfigEntry) SetHash(h uint64) {
e.Hash = h
}
func (e *ExportedServicesConfigEntry) GetHash() uint64 {
return e.Hash
2021-10-20 19:24:18 +00:00
}
// 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 `json:",omitempty"`
2021-10-20 19:24:18 +00:00
}
// ServiceConsumer represents a downstream consumer of the service to be exported.
// At most one of Partition or Peer must be specified.
2021-10-20 19:24:18 +00:00
type ServiceConsumer struct {
// Partition is the admin partition to export the service to.
Partition string `json:",omitempty"`
// Peer is the name of the peer to export the service to.
Peer string `json:",omitempty" alias:"peer_name"`
2021-10-20 19:24:18 +00:00
// SamenessGroup is the name of the sameness group to export the service to.
SamenessGroup string `json:",omitempty" alias:"sameness_group"`
2021-10-20 19:24:18 +00:00
}
func (e *ExportedServicesConfigEntry) GetKind() string {
return ExportedServices
2021-10-20 19:24:18 +00:00
}
func (e *ExportedServicesConfigEntry) GetName() string {
2021-10-20 19:24:18 +00:00
if e == nil {
return ""
}
return e.Name
2021-10-20 19:24:18 +00:00
}
func (e *ExportedServicesConfigEntry) GetMeta() map[string]string {
2021-10-20 19:24:18 +00:00
if e == nil {
return nil
}
return e.Meta
}
func (e *ExportedServicesConfigEntry) Normalize() error {
2021-10-20 19:24:18 +00:00
if e == nil {
return fmt.Errorf("config entry is nil")
}
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 = acl.NormalizeNamespace(e.Services[i].Namespace)
2021-10-20 19:24:18 +00:00
}
h, err := HashConfigEntry(e)
if err != nil {
return err
}
e.Hash = h
2021-10-20 19:24:18 +00:00
return nil
}
func (e *ExportedServicesConfigEntry) Validate() error {
if err := validateExportedServicesName(e.Name); err != nil {
return err
}
if err := validateConfigEntryMeta(e.Meta); err != nil {
return err
}
2021-10-20 19:24:18 +00:00
if err := e.validateServicesEnterprise(); err != nil {
return err
}
return e.validateServices()
}
func (e *ExportedServicesConfigEntry) validateServices() error {
for i, svc := range e.Services {
2021-10-20 19:24:18 +00:00
if svc.Name == "" {
return fmt.Errorf("Services[%d]: service name cannot be empty", i)
}
if svc.Namespace == WildcardSpecifier && svc.Name != WildcardSpecifier {
return fmt.Errorf("Services[%d]: service name must be wildcard if namespace is wildcard", i)
2021-10-20 19:24:18 +00:00
}
if len(svc.Consumers) == 0 {
return fmt.Errorf("Services[%d]: must have at least one consumer", i)
2021-10-20 19:24:18 +00:00
}
for j, consumer := range svc.Consumers {
count := 0
if consumer.Peer != "" {
count++
}
if consumer.Partition != "" {
count++
}
if consumer.SamenessGroup != "" {
count++
}
if count > 1 {
return fmt.Errorf("Services[%d].Consumers[%d]: must define at most one of Peer, Partition, or SamenessGroup", i, j)
}
2021-10-20 19:24:18 +00:00
if consumer.Partition == WildcardSpecifier {
return fmt.Errorf("Services[%d].Consumers[%d]: exporting to all partitions (wildcard) is not supported", i, j)
}
if consumer.Peer == WildcardSpecifier {
return fmt.Errorf("Services[%d].Consumers[%d]: exporting to all peers (wildcard) is not supported", i, j)
2021-10-20 19:24:18 +00:00
}
}
}
return nil
2021-10-20 19:24:18 +00:00
}
func (e *ExportedServicesConfigEntry) CanRead(authz acl.Authorizer) error {
2021-10-20 19:24:18 +00:00
var authzContext acl.AuthorizerContext
e.FillAuthzContext(&authzContext)
return authz.ToAllowAuthorizer().MeshReadAllowed(&authzContext)
2021-10-20 19:24:18 +00:00
}
func (e *ExportedServicesConfigEntry) CanWrite(authz acl.Authorizer) error {
2021-10-20 19:24:18 +00:00
var authzContext acl.AuthorizerContext
e.FillAuthzContext(&authzContext)
return authz.ToAllowAuthorizer().MeshWriteAllowed(&authzContext)
2021-10-20 19:24:18 +00:00
}
func (e *ExportedServicesConfigEntry) GetRaftIndex() *RaftIndex {
2021-10-20 19:24:18 +00:00
if e == nil {
return &RaftIndex{}
}
return &e.RaftIndex
}
func (e *ExportedServicesConfigEntry) GetEnterpriseMeta() *acl.EnterpriseMeta {
2021-10-20 19:24:18 +00:00
if e == nil {
return nil
}
return &e.EnterpriseMeta
}
// MarshalJSON adds the Kind field so that the JSON can be decoded back into the
// correct type.
// This method is implemented on the structs type (as apposed to the api type)
// because that is what the API currently uses to return a response.
func (e *ExportedServicesConfigEntry) MarshalJSON() ([]byte, error) {
type Alias ExportedServicesConfigEntry
source := &struct {
Kind string
*Alias
}{
Kind: ExportedServices,
Alias: (*Alias)(e),
}
return json.Marshal(source)
}