2022-07-12 10:39:27 +00:00
|
|
|
package proxycfgglue
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
|
|
|
|
"github.com/hashicorp/go-memdb"
|
|
|
|
|
|
|
|
"github.com/hashicorp/consul/acl"
|
|
|
|
"github.com/hashicorp/consul/agent/cache"
|
|
|
|
cachetype "github.com/hashicorp/consul/agent/cache-types"
|
|
|
|
"github.com/hashicorp/consul/agent/consul/state"
|
|
|
|
"github.com/hashicorp/consul/agent/consul/watch"
|
|
|
|
"github.com/hashicorp/consul/agent/proxycfg"
|
|
|
|
"github.com/hashicorp/consul/agent/structs"
|
|
|
|
"github.com/hashicorp/consul/proto/pbpeering"
|
|
|
|
)
|
|
|
|
|
|
|
|
// CacheTrustBundle satisfies the proxycfg.TrustBundle interface by sourcing
|
|
|
|
// data from the agent cache.
|
|
|
|
func CacheTrustBundle(c *cache.Cache) proxycfg.TrustBundle {
|
2022-07-12 23:18:05 +00:00
|
|
|
return &cacheProxyDataSource[*cachetype.TrustBundleReadRequest]{c, cachetype.TrustBundleReadName}
|
2022-07-12 10:39:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ServerTrustBundle satisfies the proxycfg.TrustBundle interface by sourcing
|
|
|
|
// data from a blocking query against the server's state store.
|
|
|
|
func ServerTrustBundle(deps ServerDataSourceDeps) proxycfg.TrustBundle {
|
|
|
|
return &serverTrustBundle{deps}
|
|
|
|
}
|
|
|
|
|
|
|
|
type serverTrustBundle struct {
|
|
|
|
deps ServerDataSourceDeps
|
|
|
|
}
|
|
|
|
|
2022-07-12 23:18:05 +00:00
|
|
|
func (s *serverTrustBundle) Notify(ctx context.Context, req *cachetype.TrustBundleReadRequest, correlationID string, ch chan<- proxycfg.UpdateEvent) error {
|
2022-07-12 10:39:27 +00:00
|
|
|
// TODO(peering): ACL check.
|
|
|
|
return watch.ServerLocalNotify(ctx, correlationID, s.deps.GetStore,
|
|
|
|
func(ws memdb.WatchSet, store Store) (uint64, *pbpeering.TrustBundleReadResponse, error) {
|
|
|
|
index, bundle, err := store.PeeringTrustBundleRead(ws, state.Query{
|
2022-07-12 23:18:05 +00:00
|
|
|
Value: req.Request.Name,
|
|
|
|
EnterpriseMeta: *structs.NodeEnterpriseMetaInPartition(req.Request.Partition),
|
2022-07-12 10:39:27 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return 0, nil, err
|
|
|
|
}
|
|
|
|
return index, &pbpeering.TrustBundleReadResponse{
|
|
|
|
Index: index,
|
|
|
|
Bundle: bundle,
|
|
|
|
}, nil
|
|
|
|
},
|
|
|
|
dispatchBlockingQueryUpdate[*pbpeering.TrustBundleReadResponse](ch),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// CacheTrustBundleList satisfies the proxycfg.TrustBundleList interface by sourcing
|
|
|
|
// data from the agent cache.
|
|
|
|
func CacheTrustBundleList(c *cache.Cache) proxycfg.TrustBundleList {
|
2022-07-12 23:18:05 +00:00
|
|
|
return &cacheProxyDataSource[*cachetype.TrustBundleListRequest]{c, cachetype.TrustBundleListName}
|
2022-07-12 10:39:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ServerTrustBundleList satisfies the proxycfg.TrustBundle interface by
|
|
|
|
// sourcing data from a blocking query against the server's state store.
|
|
|
|
func ServerTrustBundleList(deps ServerDataSourceDeps) proxycfg.TrustBundleList {
|
|
|
|
return &serverTrustBundleList{deps}
|
|
|
|
}
|
|
|
|
|
|
|
|
type serverTrustBundleList struct {
|
|
|
|
deps ServerDataSourceDeps
|
|
|
|
}
|
|
|
|
|
2022-07-12 23:18:05 +00:00
|
|
|
func (s *serverTrustBundleList) Notify(ctx context.Context, req *cachetype.TrustBundleListRequest, correlationID string, ch chan<- proxycfg.UpdateEvent) error {
|
|
|
|
entMeta := acl.NewEnterpriseMetaWithPartition(req.Request.Partition, req.Request.Namespace)
|
2022-07-12 10:39:27 +00:00
|
|
|
|
|
|
|
// TODO(peering): ACL check.
|
|
|
|
return watch.ServerLocalNotify(ctx, correlationID, s.deps.GetStore,
|
|
|
|
func(ws memdb.WatchSet, store Store) (uint64, *pbpeering.TrustBundleListByServiceResponse, error) {
|
|
|
|
var (
|
|
|
|
index uint64
|
|
|
|
bundles []*pbpeering.PeeringTrustBundle
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
switch {
|
2022-07-12 23:18:05 +00:00
|
|
|
case req.Request.Kind == string(structs.ServiceKindMeshGateway):
|
2022-07-12 10:39:27 +00:00
|
|
|
index, bundles, err = store.PeeringTrustBundleList(ws, entMeta)
|
2022-07-12 23:18:05 +00:00
|
|
|
case req.Request.ServiceName != "":
|
|
|
|
index, bundles, err = store.TrustBundleListByService(ws, req.Request.ServiceName, s.deps.Datacenter, entMeta)
|
|
|
|
case req.Request.Kind != "":
|
2022-07-12 10:39:27 +00:00
|
|
|
err = errors.New("kind must be mesh-gateway if set")
|
|
|
|
default:
|
|
|
|
err = errors.New("one of service or kind is required")
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return 0, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return index, &pbpeering.TrustBundleListByServiceResponse{
|
|
|
|
Index: index,
|
|
|
|
Bundles: bundles,
|
|
|
|
}, nil
|
|
|
|
},
|
|
|
|
dispatchBlockingQueryUpdate[*pbpeering.TrustBundleListByServiceResponse](ch),
|
|
|
|
)
|
|
|
|
}
|