2019-04-08 17:05:51 +00:00
|
|
|
package consul
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/hashicorp/consul/agent/structs"
|
|
|
|
"golang.org/x/time/rate"
|
|
|
|
)
|
|
|
|
|
2019-10-04 17:08:45 +00:00
|
|
|
func (s *Server) reapExpiredTokens(ctx context.Context) error {
|
|
|
|
limiter := rate.NewLimiter(aclTokenReapingRateLimit, aclTokenReapingBurst)
|
|
|
|
for {
|
|
|
|
if err := limiter.Wait(ctx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-04-08 17:05:51 +00:00
|
|
|
|
2019-10-04 17:08:45 +00:00
|
|
|
if s.LocalTokensEnabled() {
|
|
|
|
if _, err := s.reapExpiredLocalACLTokens(); err != nil {
|
2020-01-28 23:50:41 +00:00
|
|
|
s.logger.Error("error reaping expired local ACL tokens", "error", err)
|
2019-10-04 17:08:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if s.InACLDatacenter() {
|
|
|
|
if _, err := s.reapExpiredGlobalACLTokens(); err != nil {
|
2020-01-28 23:50:41 +00:00
|
|
|
s.logger.Error("error reaping expired global ACL tokens", "error", err)
|
2019-10-04 17:08:45 +00:00
|
|
|
}
|
|
|
|
}
|
2019-04-08 17:05:51 +00:00
|
|
|
}
|
2019-10-04 17:08:45 +00:00
|
|
|
}
|
2019-04-08 17:05:51 +00:00
|
|
|
|
2019-10-04 17:08:45 +00:00
|
|
|
func (s *Server) startACLTokenReaping() {
|
2019-04-08 17:05:51 +00:00
|
|
|
// Do a quick check for config settings that would imply the goroutine
|
|
|
|
// below will just spin forever.
|
|
|
|
//
|
|
|
|
// We can only check the config settings here that cannot change without a
|
|
|
|
// restart, so we omit the check for a non-empty replication token as that
|
|
|
|
// can be changed at runtime.
|
|
|
|
if !s.InACLDatacenter() && !s.config.ACLTokenReplication {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-10-04 17:08:45 +00:00
|
|
|
s.leaderRoutineManager.Start(aclTokenReapingRoutineName, s.reapExpiredTokens)
|
2019-04-08 17:05:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) stopACLTokenReaping() {
|
2019-10-04 17:08:45 +00:00
|
|
|
s.leaderRoutineManager.Stop(aclTokenReapingRoutineName)
|
2019-04-08 17:05:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) reapExpiredGlobalACLTokens() (int, error) {
|
|
|
|
return s.reapExpiredACLTokens(false, true)
|
|
|
|
}
|
|
|
|
func (s *Server) reapExpiredLocalACLTokens() (int, error) {
|
|
|
|
return s.reapExpiredACLTokens(true, false)
|
|
|
|
}
|
|
|
|
func (s *Server) reapExpiredACLTokens(local, global bool) (int, error) {
|
2020-07-03 20:52:08 +00:00
|
|
|
if !s.config.ACLsEnabled {
|
2019-04-08 17:05:51 +00:00
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
if s.UseLegacyACLs() {
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
if local == global {
|
|
|
|
return 0, fmt.Errorf("cannot reap both local and global tokens in the same request")
|
|
|
|
}
|
|
|
|
|
|
|
|
locality := localityName(local)
|
|
|
|
|
|
|
|
minExpiredTime, err := s.fsm.State().ACLTokenMinExpirationTime(local)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
now := time.Now()
|
|
|
|
|
|
|
|
if minExpiredTime.After(now) {
|
|
|
|
return 0, nil // nothing to do
|
|
|
|
}
|
|
|
|
|
|
|
|
tokens, _, err := s.fsm.State().ACLTokenListExpired(local, now, aclBatchDeleteSize)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(tokens) == 0 {
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
secretIDs []string
|
|
|
|
req structs.ACLTokenBatchDeleteRequest
|
|
|
|
)
|
|
|
|
for _, token := range tokens {
|
|
|
|
if token.Local != local {
|
|
|
|
return 0, fmt.Errorf("expired index for local=%v returned a mismatched token with local=%v: %s", local, token.Local, token.AccessorID)
|
|
|
|
}
|
|
|
|
req.TokenIDs = append(req.TokenIDs, token.AccessorID)
|
|
|
|
secretIDs = append(secretIDs, token.SecretID)
|
|
|
|
}
|
|
|
|
|
2020-01-28 23:50:41 +00:00
|
|
|
s.logger.Info("deleting expired ACL tokens",
|
|
|
|
"amount", len(req.TokenIDs),
|
|
|
|
"locality", locality,
|
|
|
|
)
|
2019-04-08 17:05:51 +00:00
|
|
|
resp, err := s.raftApply(structs.ACLTokenDeleteRequestType, &req)
|
|
|
|
if err != nil {
|
|
|
|
return 0, fmt.Errorf("Failed to apply token expiration deletions: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Purge the identities from the cache
|
|
|
|
for _, secretID := range secretIDs {
|
2019-10-25 17:05:43 +00:00
|
|
|
s.acls.cache.RemoveIdentity(tokenSecretCacheID(secretID))
|
2019-04-08 17:05:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if respErr, ok := resp.(error); ok {
|
|
|
|
return 0, respErr
|
|
|
|
}
|
|
|
|
|
|
|
|
return len(req.TokenIDs), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func localityName(local bool) string {
|
|
|
|
if local {
|
|
|
|
return "local"
|
|
|
|
}
|
|
|
|
return "global"
|
|
|
|
}
|