2023-03-28 18:39:22 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2017-07-26 18:03:43 +00:00
|
|
|
package token
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
2019-12-16 20:54:52 +00:00
|
|
|
|
|
|
|
"crypto/subtle"
|
2017-07-26 18:03:43 +00:00
|
|
|
)
|
|
|
|
|
2019-02-27 19:28:31 +00:00
|
|
|
type TokenSource bool
|
|
|
|
|
|
|
|
const (
|
|
|
|
TokenSourceConfig TokenSource = false
|
|
|
|
TokenSourceAPI TokenSource = true
|
|
|
|
)
|
|
|
|
|
2020-07-14 13:53:55 +00:00
|
|
|
type TokenKind int
|
|
|
|
|
|
|
|
const (
|
|
|
|
TokenKindAgent TokenKind = iota
|
2021-12-07 12:12:47 +00:00
|
|
|
TokenKindAgentRecovery
|
2020-07-14 13:53:55 +00:00
|
|
|
TokenKindUser
|
|
|
|
TokenKindReplication
|
2023-01-10 16:24:02 +00:00
|
|
|
TokenKindConfigFileRegistration
|
2020-07-14 13:53:55 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type watcher struct {
|
|
|
|
kind TokenKind
|
|
|
|
ch chan<- struct{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Notifier holds the channel used to notify a watcher
|
|
|
|
// of token updates as well as some internal tracking
|
|
|
|
// information to allow for deregistering the notifier.
|
|
|
|
type Notifier struct {
|
|
|
|
id int
|
|
|
|
Ch <-chan struct{}
|
|
|
|
}
|
|
|
|
|
2017-07-26 18:03:43 +00:00
|
|
|
// Store is used to hold the special ACL tokens used by Consul agents. It is
|
|
|
|
// designed to update the tokens on the fly, so the token store itself should be
|
|
|
|
// plumbed around and used to get tokens at runtime, don't save the resulting
|
|
|
|
// tokens.
|
|
|
|
type Store struct {
|
|
|
|
// l synchronizes access to the token store.
|
|
|
|
l sync.RWMutex
|
|
|
|
|
|
|
|
// userToken is passed along for requests when the user didn't supply a
|
|
|
|
// token, and may be left blank to use the anonymous token. This will
|
|
|
|
// also be used for agent operations if the agent token isn't set.
|
|
|
|
userToken string
|
|
|
|
|
2019-02-27 19:28:31 +00:00
|
|
|
// userTokenSource indicates where this token originated from
|
|
|
|
userTokenSource TokenSource
|
|
|
|
|
2017-07-26 18:03:43 +00:00
|
|
|
// agentToken is used for internal agent operations like self-registering
|
|
|
|
// with the catalog and anti-entropy, but should never be used for
|
|
|
|
// user-initiated operations.
|
|
|
|
agentToken string
|
|
|
|
|
2019-02-27 19:28:31 +00:00
|
|
|
// agentTokenSource indicates where this token originated from
|
|
|
|
agentTokenSource TokenSource
|
|
|
|
|
2021-12-07 12:12:47 +00:00
|
|
|
// agentRecoveryToken is a special token that's only used locally for
|
2017-07-26 18:03:43 +00:00
|
|
|
// access to the /v1/agent utility operations if the servers aren't
|
|
|
|
// available.
|
2021-12-07 12:12:47 +00:00
|
|
|
agentRecoveryToken string
|
2017-08-03 22:39:31 +00:00
|
|
|
|
2021-12-07 12:12:47 +00:00
|
|
|
// agentRecoveryTokenSource indicates where this token originated from
|
|
|
|
agentRecoveryTokenSource TokenSource
|
2019-02-27 19:28:31 +00:00
|
|
|
|
|
|
|
// replicationToken is a special token that's used by servers to
|
|
|
|
// replicate data from the primary datacenter.
|
|
|
|
replicationToken string
|
2018-10-15 16:17:48 +00:00
|
|
|
|
2019-02-27 19:28:31 +00:00
|
|
|
// replicationTokenSource indicates where this token originated from
|
|
|
|
replicationTokenSource TokenSource
|
2020-02-04 20:58:56 +00:00
|
|
|
|
2023-01-10 16:24:02 +00:00
|
|
|
// configFileRegistrationToken is used to register services and checks
|
|
|
|
// that are defined in configuration files.
|
|
|
|
configFileRegistrationToken string
|
|
|
|
|
|
|
|
// configFileRegistrationTokenSource indicates where this token originated from
|
|
|
|
configFileRegistrationTokenSource TokenSource
|
|
|
|
|
2020-07-14 13:53:55 +00:00
|
|
|
watchers map[int]watcher
|
|
|
|
watcherIndex int
|
|
|
|
|
2020-08-17 23:30:25 +00:00
|
|
|
persistence *fileStore
|
|
|
|
// persistenceLock is used to synchronize access to the persisted token store
|
|
|
|
// within the data directory. This will prevent loading while writing as well as
|
|
|
|
// multiple concurrent writes.
|
|
|
|
persistenceLock sync.RWMutex
|
|
|
|
|
2020-02-04 20:58:56 +00:00
|
|
|
// enterpriseTokens contains tokens only used in consul-enterprise
|
|
|
|
enterpriseTokens
|
2017-07-26 18:03:43 +00:00
|
|
|
}
|
|
|
|
|
2020-07-14 13:53:55 +00:00
|
|
|
// Notify will set up a watch for when tokens of the desired kind is changed
|
|
|
|
func (t *Store) Notify(kind TokenKind) Notifier {
|
|
|
|
// buffering ensures that notifications aren't missed if the watcher
|
|
|
|
// isn't already in a select and that our notifications don't
|
|
|
|
// block returning from the Update* methods.
|
|
|
|
ch := make(chan struct{}, 1)
|
|
|
|
|
|
|
|
w := watcher{
|
|
|
|
kind: kind,
|
|
|
|
ch: ch,
|
|
|
|
}
|
|
|
|
|
|
|
|
t.l.Lock()
|
|
|
|
defer t.l.Unlock()
|
|
|
|
if t.watchers == nil {
|
|
|
|
t.watchers = make(map[int]watcher)
|
|
|
|
}
|
|
|
|
// we specifically want to avoid the zero-value to prevent accidental stop-notification requests
|
|
|
|
t.watcherIndex += 1
|
|
|
|
t.watchers[t.watcherIndex] = w
|
|
|
|
|
|
|
|
return Notifier{id: t.watcherIndex, Ch: ch}
|
|
|
|
}
|
|
|
|
|
|
|
|
// StopNotify stops the token store from sending notifications to the specified notifiers chan
|
|
|
|
func (t *Store) StopNotify(n Notifier) {
|
|
|
|
t.l.Lock()
|
|
|
|
defer t.l.Unlock()
|
|
|
|
delete(t.watchers, n.id)
|
|
|
|
}
|
|
|
|
|
|
|
|
// anyKindAllowed returns true if any of the kinds in the `check` list are
|
|
|
|
// set to be allowed in the `allowed` map.
|
|
|
|
//
|
|
|
|
// Note: this is mostly just a convenience to simplify the code in
|
|
|
|
// sendNotificationLocked and prevent more nested looping with breaks/continues
|
|
|
|
// and other state tracking.
|
|
|
|
func anyKindAllowed(allowed TokenKind, check []TokenKind) bool {
|
|
|
|
for _, kind := range check {
|
|
|
|
if allowed == kind {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// sendNotificationLocked will iterate through all watchers and notify them that a
|
|
|
|
// token they are watching has been updated.
|
|
|
|
//
|
|
|
|
// NOTE: this function explicitly does not attempt to send the kind or new token value
|
|
|
|
// along through the channel. With that approach watchers could potentially miss updates
|
|
|
|
// if the buffered chan fills up. Instead with this approach we just notify that any
|
|
|
|
// token they care about has been udpated and its up to the caller to retrieve the
|
|
|
|
// new value (after receiving from the chan). With this approach its entirely possible
|
|
|
|
// for the watcher to be notified twice before actually retrieving the token after the first
|
|
|
|
// read from the chan. This is better behavior than missing events. It can cause some
|
|
|
|
// churn temporarily but in common cases its not expected that these tokens would be updated
|
|
|
|
// frequently enough to cause this to happen.
|
|
|
|
func (t *Store) sendNotificationLocked(kinds ...TokenKind) {
|
|
|
|
for _, watcher := range t.watchers {
|
|
|
|
if !anyKindAllowed(watcher.kind, kinds) {
|
|
|
|
// ignore this watcher as it doesn't want events for these kinds of token
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case watcher.ch <- struct{}{}:
|
|
|
|
default:
|
|
|
|
// its already pending a notification
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-26 18:03:43 +00:00
|
|
|
// UpdateUserToken replaces the current user token in the store.
|
2019-10-04 18:37:34 +00:00
|
|
|
// Returns true if it was changed.
|
|
|
|
func (t *Store) UpdateUserToken(token string, source TokenSource) bool {
|
2023-01-10 16:24:02 +00:00
|
|
|
return t.updateToken(token, source, &t.userToken, &t.userTokenSource, TokenKindUser)
|
2017-07-26 18:03:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateAgentToken replaces the current agent token in the store.
|
2019-10-04 18:37:34 +00:00
|
|
|
// Returns true if it was changed.
|
|
|
|
func (t *Store) UpdateAgentToken(token string, source TokenSource) bool {
|
2023-01-10 16:24:02 +00:00
|
|
|
return t.updateToken(token, source, &t.agentToken, &t.agentTokenSource, TokenKindAgent)
|
2017-07-26 18:03:43 +00:00
|
|
|
}
|
|
|
|
|
2021-12-07 12:12:47 +00:00
|
|
|
// UpdateAgentRecoveryToken replaces the current agent recovery token in the store.
|
2019-10-04 18:37:34 +00:00
|
|
|
// Returns true if it was changed.
|
2021-12-07 12:12:47 +00:00
|
|
|
func (t *Store) UpdateAgentRecoveryToken(token string, source TokenSource) bool {
|
2023-01-10 16:24:02 +00:00
|
|
|
return t.updateToken(token, source, &t.agentRecoveryToken,
|
|
|
|
&t.agentRecoveryTokenSource, TokenKindAgentRecovery)
|
2017-07-26 18:03:43 +00:00
|
|
|
}
|
|
|
|
|
2019-02-27 19:28:31 +00:00
|
|
|
// UpdateReplicationToken replaces the current replication token in the store.
|
2019-10-04 18:37:34 +00:00
|
|
|
// Returns true if it was changed.
|
|
|
|
func (t *Store) UpdateReplicationToken(token string, source TokenSource) bool {
|
2023-01-10 16:24:02 +00:00
|
|
|
return t.updateToken(token, source, &t.replicationToken,
|
|
|
|
&t.replicationTokenSource, TokenKindReplication)
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateConfigFileRegistrationToken replaces the current config file registration token
|
|
|
|
// in the store. Returns true if it was changed.
|
|
|
|
func (t *Store) UpdateConfigFileRegistrationToken(token string, source TokenSource) bool {
|
|
|
|
return t.updateToken(token, source, &t.configFileRegistrationToken,
|
|
|
|
&t.configFileRegistrationTokenSource, TokenKindConfigFileRegistration)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Store) updateToken(token string, source TokenSource, dstToken *string, dstSource *TokenSource, kind TokenKind) bool {
|
2018-10-15 16:17:48 +00:00
|
|
|
t.l.Lock()
|
2023-01-10 16:24:02 +00:00
|
|
|
changed := *dstToken != token || *dstSource != source
|
|
|
|
*dstToken = token
|
|
|
|
*dstSource = source
|
2020-07-14 13:53:55 +00:00
|
|
|
if changed {
|
2023-01-10 16:24:02 +00:00
|
|
|
t.sendNotificationLocked(kind)
|
2020-07-14 13:53:55 +00:00
|
|
|
}
|
2018-10-15 16:17:48 +00:00
|
|
|
t.l.Unlock()
|
2019-10-04 18:37:34 +00:00
|
|
|
return changed
|
2018-10-15 16:17:48 +00:00
|
|
|
}
|
|
|
|
|
2017-07-26 18:03:43 +00:00
|
|
|
// UserToken returns the best token to use for user operations.
|
|
|
|
func (t *Store) UserToken() string {
|
|
|
|
t.l.RLock()
|
|
|
|
defer t.l.RUnlock()
|
|
|
|
|
|
|
|
return t.userToken
|
|
|
|
}
|
|
|
|
|
|
|
|
// AgentToken returns the best token to use for internal agent operations.
|
|
|
|
func (t *Store) AgentToken() string {
|
|
|
|
t.l.RLock()
|
|
|
|
defer t.l.RUnlock()
|
|
|
|
|
2020-04-28 13:44:26 +00:00
|
|
|
if tok := t.enterpriseAgentToken(); tok != "" {
|
|
|
|
return tok
|
|
|
|
}
|
|
|
|
|
2017-07-26 18:03:43 +00:00
|
|
|
if t.agentToken != "" {
|
|
|
|
return t.agentToken
|
|
|
|
}
|
|
|
|
return t.userToken
|
|
|
|
}
|
|
|
|
|
2021-12-07 12:12:47 +00:00
|
|
|
func (t *Store) AgentRecoveryToken() string {
|
2019-02-27 19:28:31 +00:00
|
|
|
t.l.RLock()
|
|
|
|
defer t.l.RUnlock()
|
|
|
|
|
2021-12-07 12:12:47 +00:00
|
|
|
return t.agentRecoveryToken
|
2019-02-27 19:28:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ReplicationToken returns the replication token.
|
|
|
|
func (t *Store) ReplicationToken() string {
|
|
|
|
t.l.RLock()
|
|
|
|
defer t.l.RUnlock()
|
|
|
|
|
|
|
|
return t.replicationToken
|
|
|
|
}
|
|
|
|
|
2023-01-10 16:24:02 +00:00
|
|
|
func (t *Store) ConfigFileRegistrationToken() string {
|
|
|
|
t.l.RLock()
|
|
|
|
defer t.l.RUnlock()
|
|
|
|
|
|
|
|
return t.configFileRegistrationToken
|
|
|
|
}
|
|
|
|
|
2019-02-27 19:28:31 +00:00
|
|
|
// UserToken returns the best token to use for user operations.
|
|
|
|
func (t *Store) UserTokenAndSource() (string, TokenSource) {
|
|
|
|
t.l.RLock()
|
|
|
|
defer t.l.RUnlock()
|
|
|
|
|
|
|
|
return t.userToken, t.userTokenSource
|
|
|
|
}
|
|
|
|
|
|
|
|
// AgentToken returns the best token to use for internal agent operations.
|
|
|
|
func (t *Store) AgentTokenAndSource() (string, TokenSource) {
|
|
|
|
t.l.RLock()
|
|
|
|
defer t.l.RUnlock()
|
|
|
|
|
|
|
|
return t.agentToken, t.agentTokenSource
|
|
|
|
}
|
|
|
|
|
2021-12-07 12:12:47 +00:00
|
|
|
func (t *Store) AgentRecoveryTokenAndSource() (string, TokenSource) {
|
2017-08-03 22:39:31 +00:00
|
|
|
t.l.RLock()
|
|
|
|
defer t.l.RUnlock()
|
|
|
|
|
2021-12-07 12:12:47 +00:00
|
|
|
return t.agentRecoveryToken, t.agentRecoveryTokenSource
|
2017-08-03 22:39:31 +00:00
|
|
|
}
|
|
|
|
|
2023-01-10 16:24:02 +00:00
|
|
|
// ReplicationTokenAndSource returns the replication token and its source.
|
2019-02-27 19:28:31 +00:00
|
|
|
func (t *Store) ReplicationTokenAndSource() (string, TokenSource) {
|
2018-10-15 16:17:48 +00:00
|
|
|
t.l.RLock()
|
|
|
|
defer t.l.RUnlock()
|
|
|
|
|
2019-02-27 19:28:31 +00:00
|
|
|
return t.replicationToken, t.replicationTokenSource
|
2018-10-15 16:17:48 +00:00
|
|
|
}
|
|
|
|
|
2023-01-10 16:24:02 +00:00
|
|
|
func (t *Store) ConfigFileRegistrationTokenAndSource() (string, TokenSource) {
|
|
|
|
t.l.RLock()
|
|
|
|
defer t.l.RUnlock()
|
|
|
|
|
|
|
|
return t.configFileRegistrationToken, t.configFileRegistrationTokenSource
|
|
|
|
}
|
|
|
|
|
2021-12-07 12:12:47 +00:00
|
|
|
// IsAgentRecoveryToken checks to see if a given token is the agent recovery token.
|
2017-07-26 18:03:43 +00:00
|
|
|
// This will never match an empty token for safety.
|
2021-12-07 12:12:47 +00:00
|
|
|
func (t *Store) IsAgentRecoveryToken(token string) bool {
|
2017-07-26 18:03:43 +00:00
|
|
|
t.l.RLock()
|
|
|
|
defer t.l.RUnlock()
|
|
|
|
|
2021-12-07 12:12:47 +00:00
|
|
|
return (token != "") && (subtle.ConstantTimeCompare([]byte(token), []byte(t.agentRecoveryToken)) == 1)
|
2017-07-26 18:03:43 +00:00
|
|
|
}
|