2017-03-30 19:35:50 +00:00
|
|
|
package consul
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
|
2017-08-23 14:52:48 +00:00
|
|
|
"github.com/hashicorp/consul/acl"
|
2017-07-06 10:48:37 +00:00
|
|
|
"github.com/hashicorp/consul/agent/metadata"
|
2017-07-06 10:34:00 +00:00
|
|
|
"github.com/hashicorp/consul/agent/structs"
|
2017-03-30 19:35:50 +00:00
|
|
|
"github.com/hashicorp/raft"
|
|
|
|
"github.com/hashicorp/serf/serf"
|
|
|
|
)
|
|
|
|
|
|
|
|
// RaftGetConfiguration is used to retrieve the current Raft configuration.
|
|
|
|
func (op *Operator) RaftGetConfiguration(args *structs.DCSpecificRequest, reply *structs.RaftConfigurationResponse) error {
|
|
|
|
if done, err := op.srv.forward("Operator.RaftGetConfiguration", args, args, reply); done {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// This action requires operator read access.
|
2018-10-19 16:04:07 +00:00
|
|
|
rule, err := op.srv.ResolveToken(args.Token)
|
2017-03-30 19:35:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-10-15 20:58:50 +00:00
|
|
|
if rule != nil && rule.OperatorRead(nil) != acl.Allow {
|
2017-08-23 14:52:48 +00:00
|
|
|
return acl.ErrPermissionDenied
|
2017-03-30 19:35:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// We can't fetch the leader and the configuration atomically with
|
|
|
|
// the current Raft API.
|
|
|
|
future := op.srv.raft.GetConfiguration()
|
|
|
|
if err := future.Error(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Index the Consul information about the servers.
|
|
|
|
serverMap := make(map[raft.ServerAddress]serf.Member)
|
|
|
|
for _, member := range op.srv.serfLAN.Members() {
|
2017-07-06 10:48:37 +00:00
|
|
|
valid, parts := metadata.IsConsulServer(member)
|
2017-03-30 19:35:50 +00:00
|
|
|
if !valid {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
addr := (&net.TCPAddr{IP: member.Addr, Port: parts.Port}).String()
|
|
|
|
serverMap[raft.ServerAddress(addr)] = member
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fill out the reply.
|
|
|
|
leader := op.srv.raft.Leader()
|
|
|
|
reply.Index = future.Index()
|
|
|
|
for _, server := range future.Configuration().Servers {
|
|
|
|
node := "(unknown)"
|
2017-09-26 14:34:12 +00:00
|
|
|
raftProtocolVersion := "unknown"
|
2017-03-30 19:35:50 +00:00
|
|
|
if member, ok := serverMap[server.Address]; ok {
|
|
|
|
node = member.Name
|
2017-09-26 14:34:12 +00:00
|
|
|
raftProtocolVersion = member.Tags["raft_vsn"]
|
2017-03-30 19:35:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
entry := &structs.RaftServer{
|
2017-09-26 14:34:12 +00:00
|
|
|
ID: server.ID,
|
|
|
|
Node: node,
|
|
|
|
Address: server.Address,
|
|
|
|
Leader: server.Address == leader,
|
|
|
|
Voter: server.Suffrage == raft.Voter,
|
|
|
|
ProtocolVersion: raftProtocolVersion,
|
2017-03-30 19:35:50 +00:00
|
|
|
}
|
|
|
|
reply.Servers = append(reply.Servers, entry)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// RaftRemovePeerByAddress is used to kick a stale peer (one that it in the Raft
|
|
|
|
// quorum but no longer known to Serf or the catalog) by address in the form of
|
|
|
|
// "IP:port". The reply argument is not used, but it required to fulfill the RPC
|
|
|
|
// interface.
|
|
|
|
func (op *Operator) RaftRemovePeerByAddress(args *structs.RaftRemovePeerRequest, reply *struct{}) error {
|
|
|
|
if done, err := op.srv.forward("Operator.RaftRemovePeerByAddress", args, args, reply); done {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is a super dangerous operation that requires operator write
|
|
|
|
// access.
|
2018-10-19 16:04:07 +00:00
|
|
|
rule, err := op.srv.ResolveToken(args.Token)
|
2017-03-30 19:35:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-10-15 20:58:50 +00:00
|
|
|
if rule != nil && rule.OperatorWrite(nil) != acl.Allow {
|
2017-08-23 14:52:48 +00:00
|
|
|
return acl.ErrPermissionDenied
|
2017-03-30 19:35:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Since this is an operation designed for humans to use, we will return
|
|
|
|
// an error if the supplied address isn't among the peers since it's
|
|
|
|
// likely they screwed up.
|
|
|
|
{
|
|
|
|
future := op.srv.raft.GetConfiguration()
|
|
|
|
if err := future.Error(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, s := range future.Configuration().Servers {
|
|
|
|
if s.Address == args.Address {
|
|
|
|
args.ID = s.ID
|
|
|
|
goto REMOVE
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return fmt.Errorf("address %q was not found in the Raft configuration",
|
|
|
|
args.Address)
|
|
|
|
}
|
|
|
|
|
|
|
|
REMOVE:
|
|
|
|
// The Raft library itself will prevent various forms of foot-shooting,
|
|
|
|
// like making a configuration with no voters. Some consideration was
|
|
|
|
// given here to adding more checks, but it was decided to make this as
|
|
|
|
// low-level and direct as possible. We've got ACL coverage to lock this
|
|
|
|
// down, and if you are an operator, it's assumed you know what you are
|
|
|
|
// doing if you are calling this. If you remove a peer that's known to
|
|
|
|
// Serf, for example, it will come back when the leader does a reconcile
|
|
|
|
// pass.
|
2017-12-12 00:38:52 +00:00
|
|
|
minRaftProtocol, err := op.srv.autopilot.MinRaftProtocol()
|
2017-03-30 19:35:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var future raft.Future
|
|
|
|
if minRaftProtocol >= 2 {
|
|
|
|
future = op.srv.raft.RemoveServer(args.ID, 0, 0)
|
|
|
|
} else {
|
|
|
|
future = op.srv.raft.RemovePeer(args.Address)
|
|
|
|
}
|
|
|
|
if err := future.Error(); err != nil {
|
2020-01-28 23:50:41 +00:00
|
|
|
op.logger.Warn("Failed to remove Raft peer",
|
|
|
|
"peer", args.Address,
|
|
|
|
"error", err,
|
|
|
|
)
|
2017-03-30 19:35:50 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-01-28 23:50:41 +00:00
|
|
|
op.logger.Warn("Removed Raft peer", "peer", args.Address)
|
2017-03-30 19:35:50 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// RaftRemovePeerByID is used to kick a stale peer (one that is in the Raft
|
|
|
|
// quorum but no longer known to Serf or the catalog) by address in the form of
|
|
|
|
// "IP:port". The reply argument is not used, but is required to fulfill the RPC
|
|
|
|
// interface.
|
|
|
|
func (op *Operator) RaftRemovePeerByID(args *structs.RaftRemovePeerRequest, reply *struct{}) error {
|
|
|
|
if done, err := op.srv.forward("Operator.RaftRemovePeerByID", args, args, reply); done {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is a super dangerous operation that requires operator write
|
|
|
|
// access.
|
2018-10-19 16:04:07 +00:00
|
|
|
rule, err := op.srv.ResolveToken(args.Token)
|
2017-03-30 19:35:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-10-15 20:58:50 +00:00
|
|
|
if rule != nil && rule.OperatorWrite(nil) != acl.Allow {
|
2017-08-23 14:52:48 +00:00
|
|
|
return acl.ErrPermissionDenied
|
2017-03-30 19:35:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Since this is an operation designed for humans to use, we will return
|
|
|
|
// an error if the supplied id isn't among the peers since it's
|
|
|
|
// likely they screwed up.
|
|
|
|
{
|
|
|
|
future := op.srv.raft.GetConfiguration()
|
|
|
|
if err := future.Error(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, s := range future.Configuration().Servers {
|
|
|
|
if s.ID == args.ID {
|
|
|
|
args.Address = s.Address
|
|
|
|
goto REMOVE
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return fmt.Errorf("id %q was not found in the Raft configuration",
|
|
|
|
args.ID)
|
|
|
|
}
|
|
|
|
|
|
|
|
REMOVE:
|
|
|
|
// The Raft library itself will prevent various forms of foot-shooting,
|
|
|
|
// like making a configuration with no voters. Some consideration was
|
|
|
|
// given here to adding more checks, but it was decided to make this as
|
|
|
|
// low-level and direct as possible. We've got ACL coverage to lock this
|
|
|
|
// down, and if you are an operator, it's assumed you know what you are
|
|
|
|
// doing if you are calling this. If you remove a peer that's known to
|
|
|
|
// Serf, for example, it will come back when the leader does a reconcile
|
|
|
|
// pass.
|
2017-12-12 00:38:52 +00:00
|
|
|
minRaftProtocol, err := op.srv.autopilot.MinRaftProtocol()
|
2017-03-30 19:35:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var future raft.Future
|
|
|
|
if minRaftProtocol >= 2 {
|
|
|
|
future = op.srv.raft.RemoveServer(args.ID, 0, 0)
|
|
|
|
} else {
|
|
|
|
future = op.srv.raft.RemovePeer(args.Address)
|
|
|
|
}
|
|
|
|
if err := future.Error(); err != nil {
|
2020-01-28 23:50:41 +00:00
|
|
|
op.logger.Warn("Failed to remove Raft peer with id",
|
|
|
|
"peer_id", args.ID,
|
|
|
|
"error", err,
|
|
|
|
)
|
2017-03-30 19:35:50 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-01-28 23:50:41 +00:00
|
|
|
op.logger.Warn("Removed Raft peer with id", "peer_id", args.ID)
|
2017-03-30 19:35:50 +00:00
|
|
|
return nil
|
|
|
|
}
|