2023-03-28 18:39:22 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2022-09-01 14:45:07 +00:00
|
|
|
package proxycfgglue
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/hashicorp/go-memdb"
|
|
|
|
|
2023-05-02 20:23:42 +00:00
|
|
|
"github.com/hashicorp/consul/acl"
|
2022-09-01 14:45:07 +00:00
|
|
|
"github.com/hashicorp/consul/agent/cache"
|
|
|
|
cachetype "github.com/hashicorp/consul/agent/cache-types"
|
|
|
|
"github.com/hashicorp/consul/agent/configentry"
|
|
|
|
"github.com/hashicorp/consul/agent/consul/watch"
|
|
|
|
"github.com/hashicorp/consul/agent/proxycfg"
|
|
|
|
"github.com/hashicorp/consul/agent/structs"
|
|
|
|
)
|
|
|
|
|
|
|
|
// CacheResolvedServiceConfig satisfies the proxycfg.ResolvedServiceConfig
|
|
|
|
// interface by sourcing data from the agent cache.
|
|
|
|
func CacheResolvedServiceConfig(c *cache.Cache) proxycfg.ResolvedServiceConfig {
|
|
|
|
return &cacheProxyDataSource[*structs.ServiceConfigRequest]{c, cachetype.ResolvedServiceConfigName}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ServerResolvedServiceConfig satisfies the proxycfg.ResolvedServiceConfig
|
|
|
|
// interface by sourcing data from a blocking query against the server's state
|
|
|
|
// store.
|
|
|
|
func ServerResolvedServiceConfig(deps ServerDataSourceDeps, remoteSource proxycfg.ResolvedServiceConfig) proxycfg.ResolvedServiceConfig {
|
|
|
|
return &serverResolvedServiceConfig{deps, remoteSource}
|
|
|
|
}
|
|
|
|
|
|
|
|
type serverResolvedServiceConfig struct {
|
|
|
|
deps ServerDataSourceDeps
|
|
|
|
remoteSource proxycfg.ResolvedServiceConfig
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *serverResolvedServiceConfig) Notify(ctx context.Context, req *structs.ServiceConfigRequest, correlationID string, ch chan<- proxycfg.UpdateEvent) error {
|
|
|
|
if req.Datacenter != s.deps.Datacenter {
|
|
|
|
return s.remoteSource.Notify(ctx, req, correlationID, ch)
|
|
|
|
}
|
|
|
|
|
|
|
|
return watch.ServerLocalNotify(ctx, correlationID, s.deps.GetStore,
|
|
|
|
func(ws memdb.WatchSet, store Store) (uint64, *structs.ServiceConfigResponse, error) {
|
2023-05-02 20:23:42 +00:00
|
|
|
var authzContext acl.AuthorizerContext
|
|
|
|
authz, err := s.deps.ACLResolver.ResolveTokenAndDefaultMeta(req.Token, &req.EnterpriseMeta, &authzContext)
|
2022-09-01 14:45:07 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, nil, err
|
|
|
|
}
|
|
|
|
|
2023-05-02 20:23:42 +00:00
|
|
|
if err := authz.ToAllowAuthorizer().ServiceReadAllowed(req.Name, &authzContext); err != nil {
|
2022-09-01 14:45:07 +00:00
|
|
|
return 0, nil, err
|
|
|
|
}
|
|
|
|
|
2023-02-03 15:51:53 +00:00
|
|
|
idx, entries, err := store.ReadResolvedServiceConfigEntries(ws, req.Name, &req.EnterpriseMeta, req.GetLocalUpstreamIDs(), req.Mode)
|
2022-09-01 14:45:07 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, nil, err
|
|
|
|
}
|
|
|
|
|
2023-02-03 15:51:53 +00:00
|
|
|
reply, err := configentry.ComputeResolvedServiceConfig(req, entries, s.deps.Logger)
|
2022-09-01 14:45:07 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, nil, err
|
|
|
|
}
|
|
|
|
reply.Index = idx
|
|
|
|
|
|
|
|
return idx, reply, nil
|
|
|
|
},
|
|
|
|
dispatchBlockingQueryUpdate[*structs.ServiceConfigResponse](ch),
|
|
|
|
)
|
|
|
|
}
|