2019-08-02 20:34:54 +00:00
|
|
|
package consul
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
metrics "github.com/armon/go-metrics"
|
2021-04-20 18:55:24 +00:00
|
|
|
memdb "github.com/hashicorp/go-memdb"
|
|
|
|
|
2019-08-02 20:34:54 +00:00
|
|
|
"github.com/hashicorp/consul/acl"
|
|
|
|
"github.com/hashicorp/consul/agent/consul/discoverychain"
|
|
|
|
"github.com/hashicorp/consul/agent/consul/state"
|
|
|
|
"github.com/hashicorp/consul/agent/structs"
|
|
|
|
)
|
|
|
|
|
|
|
|
type DiscoveryChain struct {
|
|
|
|
srv *Server
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *DiscoveryChain) Get(args *structs.DiscoveryChainRequest, reply *structs.DiscoveryChainResponse) error {
|
2019-08-19 18:03:03 +00:00
|
|
|
// Exit early if Connect hasn't been enabled.
|
|
|
|
if !c.srv.config.ConnectEnabled {
|
|
|
|
return ErrConnectNotEnabled
|
|
|
|
}
|
|
|
|
|
2021-04-20 18:55:24 +00:00
|
|
|
if done, err := c.srv.ForwardRPC("DiscoveryChain.Get", args, reply); done {
|
2019-08-02 20:34:54 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer metrics.MeasureSince([]string{"discovery_chain", "get"}, time.Now())
|
|
|
|
|
|
|
|
// Fetch the ACL token, if any.
|
2020-01-24 15:04:58 +00:00
|
|
|
entMeta := args.GetEnterpriseMeta()
|
|
|
|
var authzContext acl.AuthorizerContext
|
2021-08-04 21:51:19 +00:00
|
|
|
authz, err := c.srv.ResolveTokenAndDefaultMeta(args.Token, entMeta, &authzContext)
|
2019-08-02 20:34:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-08-04 21:51:19 +00:00
|
|
|
if authz.ServiceRead(args.Name, &authzContext) != acl.Allow {
|
2019-08-02 20:34:54 +00:00
|
|
|
return acl.ErrPermissionDenied
|
|
|
|
}
|
|
|
|
|
|
|
|
if args.Name == "" {
|
|
|
|
return fmt.Errorf("Must provide service name")
|
|
|
|
}
|
|
|
|
|
|
|
|
evalDC := args.EvaluateInDatacenter
|
|
|
|
if evalDC == "" {
|
|
|
|
evalDC = c.srv.config.Datacenter
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.srv.blockingQuery(
|
|
|
|
&args.QueryOptions,
|
|
|
|
&reply.QueryMeta,
|
|
|
|
func(ws memdb.WatchSet, state *state.Store) error {
|
2020-09-22 22:05:09 +00:00
|
|
|
req := discoverychain.CompileRequest{
|
2019-08-02 20:34:54 +00:00
|
|
|
ServiceName: args.Name,
|
2020-01-24 15:04:58 +00:00
|
|
|
EvaluateInNamespace: entMeta.NamespaceOrDefault(),
|
2021-08-20 16:57:45 +00:00
|
|
|
EvaluateInPartition: entMeta.PartitionOrDefault(),
|
2019-08-05 18:30:35 +00:00
|
|
|
EvaluateInDatacenter: evalDC,
|
|
|
|
UseInDatacenter: c.srv.config.Datacenter,
|
2019-08-02 20:34:54 +00:00
|
|
|
OverrideMeshGateway: args.OverrideMeshGateway,
|
|
|
|
OverrideProtocol: args.OverrideProtocol,
|
|
|
|
OverrideConnectTimeout: args.OverrideConnectTimeout,
|
2020-09-22 22:05:09 +00:00
|
|
|
}
|
|
|
|
index, chain, err := state.ServiceDiscoveryChain(ws, args.Name, entMeta, req)
|
2019-08-02 20:34:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
reply.Index = index
|
|
|
|
reply.Chain = chain
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|