2016-08-03 05:04:11 +00:00
|
|
|
package consul
|
|
|
|
|
|
|
|
import (
|
2018-10-19 16:04:07 +00:00
|
|
|
"bytes"
|
|
|
|
"context"
|
2019-04-15 20:43:19 +00:00
|
|
|
"errors"
|
2016-08-03 05:04:11 +00:00
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
2019-04-08 17:05:51 +00:00
|
|
|
metrics "github.com/armon/go-metrics"
|
2017-07-06 10:34:00 +00:00
|
|
|
"github.com/hashicorp/consul/agent/structs"
|
2016-08-03 05:04:11 +00:00
|
|
|
)
|
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
const (
|
|
|
|
// aclReplicationMaxRetryBackoff is the max number of seconds to sleep between ACL replication RPC errors
|
|
|
|
aclReplicationMaxRetryBackoff = 64
|
|
|
|
)
|
2016-08-09 18:08:26 +00:00
|
|
|
|
2019-04-15 20:43:19 +00:00
|
|
|
// aclTypeReplicator allows the machinery of acl replication to be shared between
|
|
|
|
// types with minimal code duplication (barring generics magically popping into
|
|
|
|
// existence).
|
|
|
|
//
|
|
|
|
// Concrete implementations of this interface should internally contain a
|
|
|
|
// pointer to the server so that data lookups can occur, and they should
|
|
|
|
// maintain the smallest quantity of type-specific state they can.
|
|
|
|
//
|
|
|
|
// Implementations of this interface are short-lived and recreated on every
|
|
|
|
// iteration.
|
|
|
|
type aclTypeReplicator interface {
|
|
|
|
// Type is variant of replication in use. Used for updating the replication
|
|
|
|
// status tracker.
|
|
|
|
Type() structs.ACLReplicationType
|
|
|
|
|
|
|
|
// SingularNoun is the singular form of the item being replicated.
|
|
|
|
SingularNoun() string
|
|
|
|
|
|
|
|
// PluralNoun is the plural form of the item being replicated.
|
|
|
|
PluralNoun() string
|
|
|
|
|
|
|
|
// FetchRemote retrieves items newer than the provided index from the
|
|
|
|
// remote datacenter (for diffing purposes).
|
|
|
|
FetchRemote(srv *Server, lastRemoteIndex uint64) (int, uint64, error)
|
|
|
|
|
|
|
|
// FetchLocal retrieves items from the current datacenter (for diffing
|
|
|
|
// purposes).
|
|
|
|
FetchLocal(srv *Server) (int, uint64, error)
|
|
|
|
|
|
|
|
// SortState sorts the internal working state output of FetchRemote and
|
|
|
|
// FetchLocal so that a sane diff can be performed.
|
|
|
|
SortState() (lenLocal, lenRemote int)
|
|
|
|
|
|
|
|
// LocalMeta allows for type-agnostic metadata from the sorted local state
|
|
|
|
// can be retrieved for the purposes of diffing.
|
|
|
|
LocalMeta(i int) (id string, modIndex uint64, hash []byte)
|
|
|
|
|
|
|
|
// RemoteMeta allows for type-agnostic metadata from the sorted remote
|
|
|
|
// state can be retrieved for the purposes of diffing.
|
|
|
|
RemoteMeta(i int) (id string, modIndex uint64, hash []byte)
|
|
|
|
|
|
|
|
// FetchUpdated retrieves the specific items from the remote (during the
|
|
|
|
// correction phase).
|
|
|
|
FetchUpdated(srv *Server, updates []string) (int, error)
|
|
|
|
|
|
|
|
// LenPendingUpdates should be the size of the data retrieved in
|
|
|
|
// FetchUpdated.
|
|
|
|
LenPendingUpdates() int
|
|
|
|
|
|
|
|
// PendingUpdateIsRedacted returns true if the update contains redacted
|
|
|
|
// data. Really only valid for tokens.
|
|
|
|
PendingUpdateIsRedacted(i int) bool
|
|
|
|
|
|
|
|
// PendingUpdateEstimatedSize is the item's EstimatedSize in the state
|
|
|
|
// populated by FetchUpdated.
|
|
|
|
PendingUpdateEstimatedSize(i int) int
|
|
|
|
|
|
|
|
// UpdateLocalBatch applies a portion of the state populated by
|
|
|
|
// FetchUpdated to the current datacenter.
|
|
|
|
UpdateLocalBatch(ctx context.Context, srv *Server, start, end int) error
|
|
|
|
|
|
|
|
// DeleteLocalBatch removes items from the current datacenter.
|
|
|
|
DeleteLocalBatch(srv *Server, batch []string) error
|
|
|
|
}
|
2018-10-19 16:04:07 +00:00
|
|
|
|
2019-04-15 20:43:19 +00:00
|
|
|
var errContainsRedactedData = errors.New("replication results contain redacted data")
|
2018-10-19 16:04:07 +00:00
|
|
|
|
2019-04-15 20:43:19 +00:00
|
|
|
func (s *Server) fetchACLRolesBatch(roleIDs []string) (*structs.ACLRoleBatchResponse, error) {
|
|
|
|
req := structs.ACLRoleBatchGetRequest{
|
|
|
|
Datacenter: s.config.ACLDatacenter,
|
|
|
|
RoleIDs: roleIDs,
|
|
|
|
QueryOptions: structs.QueryOptions{
|
|
|
|
AllowStale: true,
|
|
|
|
Token: s.tokens.ReplicationToken(),
|
|
|
|
},
|
2018-10-19 16:04:07 +00:00
|
|
|
}
|
|
|
|
|
2019-04-15 20:43:19 +00:00
|
|
|
var response structs.ACLRoleBatchResponse
|
|
|
|
if err := s.RPC("ACL.RoleBatchRead", &req, &response); err != nil {
|
|
|
|
return nil, err
|
2018-10-19 16:04:07 +00:00
|
|
|
}
|
2016-08-09 18:08:26 +00:00
|
|
|
|
2019-04-15 20:43:19 +00:00
|
|
|
return &response, nil
|
2016-08-03 05:04:11 +00:00
|
|
|
}
|
|
|
|
|
2019-04-15 20:43:19 +00:00
|
|
|
func (s *Server) fetchACLRoles(lastRemoteIndex uint64) (*structs.ACLRoleListResponse, error) {
|
|
|
|
defer metrics.MeasureSince([]string{"leader", "replication", "acl", "role", "fetch"}, time.Now())
|
2018-10-19 16:04:07 +00:00
|
|
|
|
2019-04-15 20:43:19 +00:00
|
|
|
req := structs.ACLRoleListRequest{
|
|
|
|
Datacenter: s.config.ACLDatacenter,
|
|
|
|
QueryOptions: structs.QueryOptions{
|
|
|
|
AllowStale: true,
|
|
|
|
MinQueryIndex: lastRemoteIndex,
|
|
|
|
Token: s.tokens.ReplicationToken(),
|
|
|
|
},
|
2019-12-10 02:26:41 +00:00
|
|
|
EnterpriseMeta: *s.replicationEnterpriseMeta(),
|
2018-10-19 16:04:07 +00:00
|
|
|
}
|
|
|
|
|
2019-04-15 20:43:19 +00:00
|
|
|
var response structs.ACLRoleListResponse
|
|
|
|
if err := s.RPC("ACL.RoleList", &req, &response); err != nil {
|
|
|
|
return nil, err
|
2018-10-19 16:04:07 +00:00
|
|
|
}
|
2019-04-15 20:43:19 +00:00
|
|
|
return &response, nil
|
2016-08-03 05:04:11 +00:00
|
|
|
}
|
|
|
|
|
2018-10-31 20:00:46 +00:00
|
|
|
func (s *Server) fetchACLPoliciesBatch(policyIDs []string) (*structs.ACLPolicyBatchResponse, error) {
|
|
|
|
req := structs.ACLPolicyBatchGetRequest{
|
2018-10-19 16:04:07 +00:00
|
|
|
Datacenter: s.config.ACLDatacenter,
|
|
|
|
PolicyIDs: policyIDs,
|
|
|
|
QueryOptions: structs.QueryOptions{
|
|
|
|
AllowStale: true,
|
2019-02-27 19:28:31 +00:00
|
|
|
Token: s.tokens.ReplicationToken(),
|
2018-10-19 16:04:07 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-10-31 20:00:46 +00:00
|
|
|
var response structs.ACLPolicyBatchResponse
|
2018-10-19 16:04:07 +00:00
|
|
|
if err := s.RPC("ACL.PolicyBatchRead", &req, &response); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &response, nil
|
2016-08-03 05:04:11 +00:00
|
|
|
}
|
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
func (s *Server) fetchACLPolicies(lastRemoteIndex uint64) (*structs.ACLPolicyListResponse, error) {
|
|
|
|
defer metrics.MeasureSince([]string{"leader", "replication", "acl", "policy", "fetch"}, time.Now())
|
|
|
|
|
|
|
|
req := structs.ACLPolicyListRequest{
|
|
|
|
Datacenter: s.config.ACLDatacenter,
|
|
|
|
QueryOptions: structs.QueryOptions{
|
|
|
|
AllowStale: true,
|
|
|
|
MinQueryIndex: lastRemoteIndex,
|
2019-02-27 19:28:31 +00:00
|
|
|
Token: s.tokens.ReplicationToken(),
|
2018-10-19 16:04:07 +00:00
|
|
|
},
|
2019-12-10 02:26:41 +00:00
|
|
|
EnterpriseMeta: *s.replicationEnterpriseMeta(),
|
2018-10-19 16:04:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var response structs.ACLPolicyListResponse
|
|
|
|
if err := s.RPC("ACL.PolicyList", &req, &response); err != nil {
|
|
|
|
return nil, err
|
2016-08-03 05:04:11 +00:00
|
|
|
}
|
2018-10-19 16:04:07 +00:00
|
|
|
return &response, nil
|
2016-08-03 05:04:11 +00:00
|
|
|
}
|
|
|
|
|
2019-04-15 20:43:19 +00:00
|
|
|
type itemDiffResults struct {
|
2018-11-07 15:59:44 +00:00
|
|
|
LocalDeletes []string
|
|
|
|
LocalUpserts []string
|
|
|
|
LocalSkipped int
|
|
|
|
RemoteSkipped int
|
|
|
|
}
|
|
|
|
|
2019-04-15 20:43:19 +00:00
|
|
|
func diffACLType(tr aclTypeReplicator, lastRemoteIndex uint64) itemDiffResults {
|
|
|
|
// Note: items with empty IDs will bubble up to the top (like legacy, unmigrated Tokens)
|
|
|
|
|
|
|
|
lenLocal, lenRemote := tr.SortState()
|
2018-10-19 16:04:07 +00:00
|
|
|
|
2019-04-15 20:43:19 +00:00
|
|
|
var res itemDiffResults
|
2018-10-19 16:04:07 +00:00
|
|
|
var localIdx int
|
|
|
|
var remoteIdx int
|
2019-04-15 20:43:19 +00:00
|
|
|
for localIdx, remoteIdx = 0, 0; localIdx < lenLocal && remoteIdx < lenRemote; {
|
|
|
|
localID, _, localHash := tr.LocalMeta(localIdx)
|
|
|
|
remoteID, remoteMod, remoteHash := tr.RemoteMeta(remoteIdx)
|
|
|
|
|
|
|
|
if localID == "" {
|
2018-11-07 15:59:44 +00:00
|
|
|
res.LocalSkipped++
|
|
|
|
localIdx += 1
|
|
|
|
continue
|
|
|
|
}
|
2019-04-15 20:43:19 +00:00
|
|
|
if remoteID == "" {
|
2018-11-07 15:59:44 +00:00
|
|
|
res.RemoteSkipped++
|
|
|
|
remoteIdx += 1
|
|
|
|
continue
|
|
|
|
}
|
2019-04-15 20:43:19 +00:00
|
|
|
|
|
|
|
if localID == remoteID {
|
|
|
|
// item is in both the local and remote state - need to check raft indices and the Hash
|
|
|
|
if remoteMod > lastRemoteIndex && !bytes.Equal(remoteHash, localHash) {
|
|
|
|
res.LocalUpserts = append(res.LocalUpserts, remoteID)
|
2018-10-19 16:04:07 +00:00
|
|
|
}
|
|
|
|
// increment both indices when equal
|
|
|
|
localIdx += 1
|
|
|
|
remoteIdx += 1
|
2019-04-15 20:43:19 +00:00
|
|
|
} else if localID < remoteID {
|
|
|
|
// item no longer in remote state - needs deleting
|
|
|
|
res.LocalDeletes = append(res.LocalDeletes, localID)
|
2018-10-19 16:04:07 +00:00
|
|
|
|
|
|
|
// increment just the local index
|
|
|
|
localIdx += 1
|
|
|
|
} else {
|
2019-04-15 20:43:19 +00:00
|
|
|
// local state doesn't have this item - needs updating
|
|
|
|
res.LocalUpserts = append(res.LocalUpserts, remoteID)
|
2018-10-19 16:04:07 +00:00
|
|
|
|
|
|
|
// increment just the remote index
|
|
|
|
remoteIdx += 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-15 20:43:19 +00:00
|
|
|
for ; localIdx < lenLocal; localIdx += 1 {
|
|
|
|
localID, _, _ := tr.LocalMeta(localIdx)
|
|
|
|
if localID != "" {
|
|
|
|
res.LocalDeletes = append(res.LocalDeletes, localID)
|
2018-11-07 15:59:44 +00:00
|
|
|
} else {
|
|
|
|
res.LocalSkipped++
|
|
|
|
}
|
2018-10-19 16:04:07 +00:00
|
|
|
}
|
|
|
|
|
2019-04-15 20:43:19 +00:00
|
|
|
for ; remoteIdx < lenRemote; remoteIdx += 1 {
|
|
|
|
remoteID, _, _ := tr.RemoteMeta(remoteIdx)
|
|
|
|
if remoteID != "" {
|
|
|
|
res.LocalUpserts = append(res.LocalUpserts, remoteID)
|
2018-11-07 15:59:44 +00:00
|
|
|
} else {
|
|
|
|
res.RemoteSkipped++
|
|
|
|
}
|
2018-10-19 16:04:07 +00:00
|
|
|
}
|
|
|
|
|
2018-11-07 15:59:44 +00:00
|
|
|
return res
|
2016-08-03 05:04:11 +00:00
|
|
|
}
|
|
|
|
|
2019-04-15 20:43:19 +00:00
|
|
|
func (s *Server) deleteLocalACLType(ctx context.Context, tr aclTypeReplicator, deletions []string) (bool, error) {
|
2018-10-19 16:04:07 +00:00
|
|
|
ticker := time.NewTicker(time.Second / time.Duration(s.config.ACLReplicationApplyLimit))
|
|
|
|
defer ticker.Stop()
|
|
|
|
|
|
|
|
for i := 0; i < len(deletions); i += aclBatchDeleteSize {
|
2019-04-15 20:43:19 +00:00
|
|
|
var batch []string
|
2018-10-19 16:04:07 +00:00
|
|
|
|
|
|
|
if i+aclBatchDeleteSize > len(deletions) {
|
2019-04-15 20:43:19 +00:00
|
|
|
batch = deletions[i:]
|
2018-10-19 16:04:07 +00:00
|
|
|
} else {
|
2019-04-15 20:43:19 +00:00
|
|
|
batch = deletions[i : i+aclBatchDeleteSize]
|
2018-10-19 16:04:07 +00:00
|
|
|
}
|
|
|
|
|
2019-04-15 20:43:19 +00:00
|
|
|
if err := tr.DeleteLocalBatch(s, batch); err != nil {
|
|
|
|
return false, fmt.Errorf("Failed to apply %s deletions: %v", tr.SingularNoun(), err)
|
2016-08-03 05:04:11 +00:00
|
|
|
}
|
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
if i+aclBatchDeleteSize < len(deletions) {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return true, nil
|
|
|
|
case <-ticker.C:
|
|
|
|
// do nothing - ready for the next batch
|
|
|
|
}
|
2016-08-03 05:04:11 +00:00
|
|
|
}
|
2018-10-19 16:04:07 +00:00
|
|
|
}
|
2016-08-03 05:04:11 +00:00
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
2019-04-15 20:43:19 +00:00
|
|
|
func (s *Server) updateLocalACLType(ctx context.Context, tr aclTypeReplicator) (bool, error) {
|
2018-10-19 16:04:07 +00:00
|
|
|
ticker := time.NewTicker(time.Second / time.Duration(s.config.ACLReplicationApplyLimit))
|
|
|
|
defer ticker.Stop()
|
|
|
|
|
2019-04-15 20:43:19 +00:00
|
|
|
lenPending := tr.LenPendingUpdates()
|
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
// outer loop handles submitting a batch
|
2019-04-15 20:43:19 +00:00
|
|
|
for batchStart := 0; batchStart < lenPending; {
|
2018-10-19 16:04:07 +00:00
|
|
|
// inner loop finds the last element to include in this batch.
|
|
|
|
batchSize := 0
|
|
|
|
batchEnd := batchStart
|
2019-04-15 20:43:19 +00:00
|
|
|
for ; batchEnd < lenPending && batchSize < aclBatchUpsertSize; batchEnd += 1 {
|
|
|
|
if tr.PendingUpdateIsRedacted(batchEnd) {
|
|
|
|
return false, fmt.Errorf(
|
|
|
|
"Detected redacted %s secrets: stopping %s update round - verify that the replication token in use has acl:write permissions.",
|
|
|
|
tr.SingularNoun(),
|
|
|
|
tr.SingularNoun(),
|
|
|
|
)
|
2019-03-04 14:52:45 +00:00
|
|
|
}
|
2019-04-15 20:43:19 +00:00
|
|
|
batchSize += tr.PendingUpdateEstimatedSize(batchEnd)
|
2016-08-03 05:04:11 +00:00
|
|
|
}
|
|
|
|
|
2019-04-15 20:43:19 +00:00
|
|
|
err := tr.UpdateLocalBatch(ctx, s, batchStart, batchEnd)
|
2018-10-19 16:04:07 +00:00
|
|
|
if err != nil {
|
2019-04-15 20:43:19 +00:00
|
|
|
return false, fmt.Errorf("Failed to apply %s upserts: %v", tr.SingularNoun(), err)
|
2018-10-19 16:04:07 +00:00
|
|
|
}
|
2019-04-15 20:43:19 +00:00
|
|
|
s.logger.Printf(
|
|
|
|
"[DEBUG] acl: %s replication - upserted 1 batch with %d %s of size %d",
|
|
|
|
tr.SingularNoun(),
|
|
|
|
batchEnd-batchStart,
|
|
|
|
tr.PluralNoun(),
|
|
|
|
batchSize,
|
|
|
|
)
|
2018-10-19 16:04:07 +00:00
|
|
|
|
2019-04-15 20:43:19 +00:00
|
|
|
// items[batchEnd] wasn't include as the slicing doesn't include the element at the stop index
|
2018-10-19 16:04:07 +00:00
|
|
|
batchStart = batchEnd
|
|
|
|
|
|
|
|
// prevent waiting if we are done
|
2019-04-15 20:43:19 +00:00
|
|
|
if batchEnd < lenPending {
|
2018-10-19 16:04:07 +00:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return true, nil
|
|
|
|
case <-ticker.C:
|
2019-04-15 20:43:19 +00:00
|
|
|
// nothing to do - just rate limiting
|
2018-10-19 16:04:07 +00:00
|
|
|
}
|
2016-08-03 05:04:11 +00:00
|
|
|
}
|
|
|
|
}
|
2018-10-19 16:04:07 +00:00
|
|
|
return false, nil
|
2016-08-03 05:04:11 +00:00
|
|
|
}
|
|
|
|
|
2018-10-31 20:00:46 +00:00
|
|
|
func (s *Server) fetchACLTokensBatch(tokenIDs []string) (*structs.ACLTokenBatchResponse, error) {
|
|
|
|
req := structs.ACLTokenBatchGetRequest{
|
2018-10-19 16:04:07 +00:00
|
|
|
Datacenter: s.config.ACLDatacenter,
|
|
|
|
AccessorIDs: tokenIDs,
|
|
|
|
QueryOptions: structs.QueryOptions{
|
|
|
|
AllowStale: true,
|
2019-02-27 19:28:31 +00:00
|
|
|
Token: s.tokens.ReplicationToken(),
|
2018-10-19 16:04:07 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-10-31 20:00:46 +00:00
|
|
|
var response structs.ACLTokenBatchResponse
|
2018-10-19 16:04:07 +00:00
|
|
|
if err := s.RPC("ACL.TokenBatchRead", &req, &response); err != nil {
|
2016-08-03 05:04:11 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2018-10-19 16:04:07 +00:00
|
|
|
|
|
|
|
return &response, nil
|
2016-08-03 05:04:11 +00:00
|
|
|
}
|
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
func (s *Server) fetchACLTokens(lastRemoteIndex uint64) (*structs.ACLTokenListResponse, error) {
|
|
|
|
defer metrics.MeasureSince([]string{"leader", "replication", "acl", "token", "fetch"}, time.Now())
|
2016-08-09 18:32:12 +00:00
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
req := structs.ACLTokenListRequest{
|
2016-08-03 05:04:11 +00:00
|
|
|
Datacenter: s.config.ACLDatacenter,
|
|
|
|
QueryOptions: structs.QueryOptions{
|
|
|
|
AllowStale: true,
|
2018-10-19 16:04:07 +00:00
|
|
|
MinQueryIndex: lastRemoteIndex,
|
2019-02-27 19:28:31 +00:00
|
|
|
Token: s.tokens.ReplicationToken(),
|
2016-08-03 05:04:11 +00:00
|
|
|
},
|
2019-12-10 02:26:41 +00:00
|
|
|
IncludeLocal: false,
|
|
|
|
IncludeGlobal: true,
|
|
|
|
EnterpriseMeta: *s.replicationEnterpriseMeta(),
|
2016-08-03 05:04:11 +00:00
|
|
|
}
|
2018-10-19 16:04:07 +00:00
|
|
|
|
|
|
|
var response structs.ACLTokenListResponse
|
|
|
|
if err := s.RPC("ACL.TokenList", &req, &response); err != nil {
|
2016-08-03 05:04:11 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2018-10-19 16:04:07 +00:00
|
|
|
return &response, nil
|
2016-08-03 05:04:11 +00:00
|
|
|
}
|
|
|
|
|
2019-04-15 20:43:19 +00:00
|
|
|
func (s *Server) replicateACLTokens(ctx context.Context, lastRemoteIndex uint64) (uint64, bool, error) {
|
|
|
|
tr := &aclTokenReplicator{}
|
|
|
|
return s.replicateACLType(ctx, tr, lastRemoteIndex)
|
|
|
|
}
|
2016-08-09 18:29:12 +00:00
|
|
|
|
2019-04-15 20:43:19 +00:00
|
|
|
func (s *Server) replicateACLPolicies(ctx context.Context, lastRemoteIndex uint64) (uint64, bool, error) {
|
|
|
|
tr := &aclPolicyReplicator{}
|
|
|
|
return s.replicateACLType(ctx, tr, lastRemoteIndex)
|
|
|
|
}
|
2018-10-19 16:04:07 +00:00
|
|
|
|
2019-04-15 20:43:19 +00:00
|
|
|
func (s *Server) replicateACLRoles(ctx context.Context, lastRemoteIndex uint64) (uint64, bool, error) {
|
|
|
|
tr := &aclRoleReplicator{}
|
|
|
|
return s.replicateACLType(ctx, tr, lastRemoteIndex)
|
2016-08-03 05:04:11 +00:00
|
|
|
}
|
|
|
|
|
2019-04-15 20:43:19 +00:00
|
|
|
func (s *Server) replicateACLType(ctx context.Context, tr aclTypeReplicator, lastRemoteIndex uint64) (uint64, bool, error) {
|
|
|
|
lenRemote, remoteIndex, err := tr.FetchRemote(s, lastRemoteIndex)
|
2016-08-03 05:04:11 +00:00
|
|
|
if err != nil {
|
2019-04-15 20:43:19 +00:00
|
|
|
return 0, false, fmt.Errorf("failed to retrieve remote ACL %s: %v", tr.PluralNoun(), err)
|
2016-08-03 05:04:11 +00:00
|
|
|
}
|
|
|
|
|
2019-04-15 20:43:19 +00:00
|
|
|
s.logger.Printf("[DEBUG] acl: finished fetching %s: %d", tr.PluralNoun(), lenRemote)
|
2018-10-19 16:04:07 +00:00
|
|
|
|
|
|
|
// Need to check if we should be stopping. This will be common as the fetching process is a blocking
|
|
|
|
// RPC which could have been hanging around for a long time and during that time leadership could
|
|
|
|
// have been lost.
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return 0, true, nil
|
|
|
|
default:
|
|
|
|
// do nothing
|
2016-08-03 05:04:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Measure everything after the remote query, which can block for long
|
|
|
|
// periods of time. This metric is a good measure of how expensive the
|
|
|
|
// replication process is.
|
2019-04-15 20:43:19 +00:00
|
|
|
defer metrics.MeasureSince([]string{"leader", "replication", "acl", tr.SingularNoun(), "apply"}, time.Now())
|
2016-08-03 05:04:11 +00:00
|
|
|
|
2019-04-15 20:43:19 +00:00
|
|
|
lenLocal, _, err := tr.FetchLocal(s)
|
2016-08-03 05:04:11 +00:00
|
|
|
if err != nil {
|
2019-04-15 20:43:19 +00:00
|
|
|
return 0, false, fmt.Errorf("failed to retrieve local ACL %s: %v", tr.PluralNoun(), err)
|
2016-08-03 05:04:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// If the remote index ever goes backwards, it's a good indication that
|
|
|
|
// the remote side was rebuilt and we should do a full sync since we
|
|
|
|
// can't make any assumptions about what's going on.
|
2019-04-15 20:43:19 +00:00
|
|
|
if remoteIndex < lastRemoteIndex {
|
|
|
|
s.logger.Printf(
|
|
|
|
"[WARN] consul: ACL %s replication remote index moved backwards (%d to %d), forcing a full ACL %s sync",
|
|
|
|
tr.SingularNoun(),
|
|
|
|
lastRemoteIndex,
|
|
|
|
remoteIndex,
|
|
|
|
tr.SingularNoun(),
|
|
|
|
)
|
2016-08-03 05:04:11 +00:00
|
|
|
lastRemoteIndex = 0
|
|
|
|
}
|
|
|
|
|
2019-04-15 20:43:19 +00:00
|
|
|
s.logger.Printf(
|
|
|
|
"[DEBUG] acl: %s replication - local: %d, remote: %d",
|
|
|
|
tr.SingularNoun(),
|
|
|
|
lenLocal,
|
|
|
|
lenRemote,
|
|
|
|
)
|
|
|
|
// Calculate the changes required to bring the state into sync and then apply them.
|
|
|
|
res := diffACLType(tr, lastRemoteIndex)
|
2018-11-07 15:59:44 +00:00
|
|
|
if res.LocalSkipped > 0 || res.RemoteSkipped > 0 {
|
2019-04-15 20:43:19 +00:00
|
|
|
s.logger.Printf(
|
|
|
|
"[DEBUG] acl: %s replication - deletions: %d, updates: %d, skipped: %d, skippedRemote: %d",
|
|
|
|
tr.SingularNoun(),
|
|
|
|
len(res.LocalDeletes),
|
|
|
|
len(res.LocalUpserts),
|
|
|
|
res.LocalSkipped,
|
|
|
|
res.RemoteSkipped,
|
|
|
|
)
|
2018-11-07 15:59:44 +00:00
|
|
|
} else {
|
2019-04-15 20:43:19 +00:00
|
|
|
s.logger.Printf(
|
|
|
|
"[DEBUG] acl: %s replication - deletions: %d, updates: %d",
|
|
|
|
tr.SingularNoun(),
|
|
|
|
len(res.LocalDeletes),
|
|
|
|
len(res.LocalUpserts),
|
|
|
|
)
|
2018-11-07 15:59:44 +00:00
|
|
|
}
|
2018-10-19 16:04:07 +00:00
|
|
|
|
2018-11-07 15:59:44 +00:00
|
|
|
if len(res.LocalUpserts) > 0 {
|
2019-04-15 20:43:19 +00:00
|
|
|
lenUpdated, err := tr.FetchUpdated(s, res.LocalUpserts)
|
|
|
|
if err == errContainsRedactedData {
|
|
|
|
return 0, false, fmt.Errorf("failed to retrieve unredacted %s - replication token in use does not grant acl:write", tr.PluralNoun())
|
|
|
|
} else if err != nil {
|
|
|
|
return 0, false, fmt.Errorf("failed to retrieve ACL %s updates: %v", tr.SingularNoun(), err)
|
2018-10-19 16:04:07 +00:00
|
|
|
}
|
2019-04-15 20:43:19 +00:00
|
|
|
s.logger.Printf(
|
|
|
|
"[DEBUG] acl: %s replication - downloaded %d %s",
|
|
|
|
tr.SingularNoun(),
|
|
|
|
lenUpdated,
|
|
|
|
tr.PluralNoun(),
|
|
|
|
)
|
2018-10-19 16:04:07 +00:00
|
|
|
}
|
|
|
|
|
2018-11-07 15:59:44 +00:00
|
|
|
if len(res.LocalDeletes) > 0 {
|
2019-04-15 20:43:19 +00:00
|
|
|
s.logger.Printf(
|
|
|
|
"[DEBUG] acl: %s replication - performing deletions",
|
|
|
|
tr.SingularNoun(),
|
|
|
|
)
|
2018-10-19 16:04:07 +00:00
|
|
|
|
2019-04-15 20:43:19 +00:00
|
|
|
exit, err := s.deleteLocalACLType(ctx, tr, res.LocalDeletes)
|
2018-10-19 16:04:07 +00:00
|
|
|
if exit {
|
|
|
|
return 0, true, nil
|
|
|
|
}
|
|
|
|
if err != nil {
|
2019-04-15 20:43:19 +00:00
|
|
|
return 0, false, fmt.Errorf("failed to delete local ACL %s: %v", tr.PluralNoun(), err)
|
2018-10-19 16:04:07 +00:00
|
|
|
}
|
2019-04-15 20:43:19 +00:00
|
|
|
s.logger.Printf("[DEBUG] acl: %s replication - finished deletions", tr.SingularNoun())
|
2018-10-19 16:04:07 +00:00
|
|
|
}
|
|
|
|
|
2018-11-07 15:59:44 +00:00
|
|
|
if len(res.LocalUpserts) > 0 {
|
2019-04-15 20:43:19 +00:00
|
|
|
s.logger.Printf("[DEBUG] acl: %s replication - performing updates", tr.SingularNoun())
|
|
|
|
exit, err := s.updateLocalACLType(ctx, tr)
|
2018-10-19 16:04:07 +00:00
|
|
|
if exit {
|
|
|
|
return 0, true, nil
|
|
|
|
}
|
|
|
|
if err != nil {
|
2019-04-15 20:43:19 +00:00
|
|
|
return 0, false, fmt.Errorf("failed to update local ACL %s: %v", tr.PluralNoun(), err)
|
2018-10-19 16:04:07 +00:00
|
|
|
}
|
2019-04-15 20:43:19 +00:00
|
|
|
s.logger.Printf("[DEBUG] acl: %s replication - finished updates", tr.SingularNoun())
|
2016-08-03 05:04:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Return the index we got back from the remote side, since we've synced
|
|
|
|
// up with the remote state as of that index.
|
2019-04-15 20:43:19 +00:00
|
|
|
return remoteIndex, false, nil
|
2016-08-03 05:04:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// IsACLReplicationEnabled returns true if ACL replication is enabled.
|
2018-10-19 16:04:07 +00:00
|
|
|
// DEPRECATED (ACL-Legacy-Compat) - with new ACLs at least policy replication is required
|
2016-08-03 05:04:11 +00:00
|
|
|
func (s *Server) IsACLReplicationEnabled() bool {
|
|
|
|
authDC := s.config.ACLDatacenter
|
|
|
|
return len(authDC) > 0 && (authDC != s.config.Datacenter) &&
|
2018-10-19 16:04:07 +00:00
|
|
|
s.config.ACLTokenReplication
|
2016-08-03 05:04:11 +00:00
|
|
|
}
|
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
func (s *Server) updateACLReplicationStatusError() {
|
|
|
|
s.aclReplicationStatusLock.Lock()
|
|
|
|
defer s.aclReplicationStatusLock.Unlock()
|
2016-08-05 04:32:36 +00:00
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
s.aclReplicationStatus.LastError = time.Now().Round(time.Second).UTC()
|
|
|
|
}
|
|
|
|
|
2019-04-15 20:43:19 +00:00
|
|
|
func (s *Server) updateACLReplicationStatusIndex(replicationType structs.ACLReplicationType, index uint64) {
|
2016-08-05 04:32:36 +00:00
|
|
|
s.aclReplicationStatusLock.Lock()
|
2018-10-19 16:04:07 +00:00
|
|
|
defer s.aclReplicationStatusLock.Unlock()
|
|
|
|
|
|
|
|
s.aclReplicationStatus.LastSuccess = time.Now().Round(time.Second).UTC()
|
2019-04-15 20:43:19 +00:00
|
|
|
switch replicationType {
|
|
|
|
case structs.ACLReplicateLegacy:
|
|
|
|
s.aclReplicationStatus.ReplicatedIndex = index
|
|
|
|
case structs.ACLReplicateTokens:
|
|
|
|
s.aclReplicationStatus.ReplicatedTokenIndex = index
|
|
|
|
case structs.ACLReplicatePolicies:
|
|
|
|
s.aclReplicationStatus.ReplicatedIndex = index
|
|
|
|
case structs.ACLReplicateRoles:
|
|
|
|
s.aclReplicationStatus.ReplicatedRoleIndex = index
|
|
|
|
default:
|
|
|
|
panic("unknown replication type: " + replicationType.SingularNoun())
|
|
|
|
}
|
2018-10-19 16:04:07 +00:00
|
|
|
}
|
2016-08-03 05:04:11 +00:00
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
func (s *Server) initReplicationStatus() {
|
|
|
|
s.aclReplicationStatusLock.Lock()
|
|
|
|
defer s.aclReplicationStatusLock.Unlock()
|
|
|
|
|
|
|
|
s.aclReplicationStatus.Enabled = true
|
|
|
|
s.aclReplicationStatus.Running = true
|
|
|
|
s.aclReplicationStatus.SourceDatacenter = s.config.ACLDatacenter
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) updateACLReplicationStatusStopped() {
|
|
|
|
s.aclReplicationStatusLock.Lock()
|
|
|
|
defer s.aclReplicationStatusLock.Unlock()
|
|
|
|
|
|
|
|
s.aclReplicationStatus.Running = false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) updateACLReplicationStatusRunning(replicationType structs.ACLReplicationType) {
|
|
|
|
s.aclReplicationStatusLock.Lock()
|
|
|
|
defer s.aclReplicationStatusLock.Unlock()
|
|
|
|
|
2019-04-15 20:43:19 +00:00
|
|
|
// The running state represents which type of overall replication has been
|
|
|
|
// configured. Though there are various types of internal plumbing for acl
|
|
|
|
// replication, to the end user there are only 3 distinctly configurable
|
|
|
|
// variants: legacy, policy, token. Roles replicate with policies so we
|
|
|
|
// round that up here.
|
|
|
|
if replicationType == structs.ACLReplicateRoles {
|
|
|
|
replicationType = structs.ACLReplicatePolicies
|
|
|
|
}
|
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
s.aclReplicationStatus.Running = true
|
|
|
|
s.aclReplicationStatus.ReplicationType = replicationType
|
2016-08-03 05:04:11 +00:00
|
|
|
}
|
2019-04-15 20:43:19 +00:00
|
|
|
|
|
|
|
func (s *Server) getACLReplicationStatusRunningType() (structs.ACLReplicationType, bool) {
|
|
|
|
s.aclReplicationStatusLock.RLock()
|
|
|
|
defer s.aclReplicationStatusLock.RUnlock()
|
|
|
|
return s.aclReplicationStatus.ReplicationType, s.aclReplicationStatus.Running
|
|
|
|
}
|
2019-06-18 00:52:01 +00:00
|
|
|
|
|
|
|
func (s *Server) getACLReplicationStatus() structs.ACLReplicationStatus {
|
|
|
|
s.aclReplicationStatusLock.RLock()
|
|
|
|
defer s.aclReplicationStatusLock.RUnlock()
|
|
|
|
return s.aclReplicationStatus
|
|
|
|
}
|