2020-09-08 21:31:47 +00:00
|
|
|
package consul
|
|
|
|
|
|
|
|
import (
|
2020-10-02 15:58:18 +00:00
|
|
|
"google.golang.org/grpc"
|
|
|
|
|
2020-09-08 21:31:47 +00:00
|
|
|
"github.com/hashicorp/consul/acl"
|
|
|
|
"github.com/hashicorp/consul/agent/consul/stream"
|
2020-10-02 15:58:18 +00:00
|
|
|
"github.com/hashicorp/consul/agent/rpc/subscribe"
|
2020-10-21 20:41:22 +00:00
|
|
|
"github.com/hashicorp/consul/agent/structs"
|
2020-09-08 21:31:47 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type subscribeBackend struct {
|
|
|
|
srv *Server
|
2021-04-20 22:14:46 +00:00
|
|
|
connPool GRPCClientConner
|
2020-09-08 21:31:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: refactor Resolve methods to an ACLBackend that can be used by all
|
|
|
|
// the endpoints.
|
2020-10-21 20:08:33 +00:00
|
|
|
func (s subscribeBackend) ResolveTokenAndDefaultMeta(
|
|
|
|
token string,
|
|
|
|
entMeta *structs.EnterpriseMeta,
|
|
|
|
authzContext *acl.AuthorizerContext,
|
|
|
|
) (acl.Authorizer, error) {
|
|
|
|
return s.srv.ResolveTokenAndDefaultMeta(token, entMeta, authzContext)
|
2020-09-08 21:31:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var _ subscribe.Backend = (*subscribeBackend)(nil)
|
|
|
|
|
|
|
|
// Forward requests to a remote datacenter by calling f if the target dc does not
|
|
|
|
// match the config. Does nothing but return handled=false if dc is not specified,
|
|
|
|
// or if it matches the Datacenter in config.
|
|
|
|
//
|
|
|
|
// TODO: extract this so that it can be used with other grpc services.
|
|
|
|
func (s subscribeBackend) Forward(dc string, f func(*grpc.ClientConn) error) (handled bool, err error) {
|
|
|
|
if dc == "" || dc == s.srv.config.Datacenter {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
conn, err := s.connPool.ClientConn(dc)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
return true, f(conn)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s subscribeBackend) Subscribe(req *stream.SubscribeRequest) (*stream.Subscription, error) {
|
|
|
|
return s.srv.fsm.State().EventPublisher().Subscribe(req)
|
|
|
|
}
|