2023-03-28 18:39:22 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
2023-08-11 13:12:13 +00:00
|
|
|
// SPDX-License-Identifier: BUSL-1.1
|
2023-03-28 18:39:22 +00:00
|
|
|
|
2017-07-26 18:03:43 +00:00
|
|
|
package token
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
2019-02-27 19:28:31 +00:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
2017-07-26 18:03:43 +00:00
|
|
|
)
|
|
|
|
|
2017-08-03 22:39:31 +00:00
|
|
|
func TestStore_RegularTokens(t *testing.T) {
|
|
|
|
type tokens struct {
|
2023-01-10 16:24:02 +00:00
|
|
|
userSource TokenSource
|
|
|
|
user string
|
|
|
|
agent string
|
|
|
|
agentSource TokenSource
|
|
|
|
recovery string
|
|
|
|
recoverySource TokenSource
|
|
|
|
repl string
|
|
|
|
replSource TokenSource
|
|
|
|
registration string
|
|
|
|
registrationSource TokenSource
|
2017-08-03 22:39:31 +00:00
|
|
|
}
|
|
|
|
|
2017-07-26 18:03:43 +00:00
|
|
|
tests := []struct {
|
2017-08-03 22:39:31 +00:00
|
|
|
name string
|
2019-02-27 19:28:31 +00:00
|
|
|
set tokens
|
|
|
|
raw tokens
|
|
|
|
effective tokens
|
2017-07-26 18:03:43 +00:00
|
|
|
}{
|
2017-08-03 22:39:31 +00:00
|
|
|
{
|
2019-02-27 19:28:31 +00:00
|
|
|
name: "set user - config",
|
|
|
|
set: tokens{user: "U", userSource: TokenSourceConfig},
|
|
|
|
raw: tokens{user: "U", userSource: TokenSourceConfig},
|
|
|
|
effective: tokens{user: "U", agent: "U"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "set user - api",
|
|
|
|
set: tokens{user: "U", userSource: TokenSourceAPI},
|
|
|
|
raw: tokens{user: "U", userSource: TokenSourceAPI},
|
|
|
|
effective: tokens{user: "U", agent: "U"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "set agent - config",
|
|
|
|
set: tokens{agent: "A", agentSource: TokenSourceConfig},
|
|
|
|
raw: tokens{agent: "A", agentSource: TokenSourceConfig},
|
|
|
|
effective: tokens{agent: "A"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "set agent - api",
|
|
|
|
set: tokens{agent: "A", agentSource: TokenSourceAPI},
|
|
|
|
raw: tokens{agent: "A", agentSource: TokenSourceAPI},
|
|
|
|
effective: tokens{agent: "A"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "set user and agent",
|
|
|
|
set: tokens{agent: "A", user: "U"},
|
|
|
|
raw: tokens{agent: "A", user: "U"},
|
|
|
|
effective: tokens{agent: "A", user: "U"},
|
2017-08-03 22:39:31 +00:00
|
|
|
},
|
|
|
|
{
|
2019-02-27 19:28:31 +00:00
|
|
|
name: "set repl - config",
|
|
|
|
set: tokens{repl: "R", replSource: TokenSourceConfig},
|
|
|
|
raw: tokens{repl: "R", replSource: TokenSourceConfig},
|
|
|
|
effective: tokens{repl: "R"},
|
2017-08-03 22:39:31 +00:00
|
|
|
},
|
|
|
|
{
|
2019-02-27 19:28:31 +00:00
|
|
|
name: "set repl - api",
|
|
|
|
set: tokens{repl: "R", replSource: TokenSourceAPI},
|
|
|
|
raw: tokens{repl: "R", replSource: TokenSourceAPI},
|
|
|
|
effective: tokens{repl: "R"},
|
2017-08-03 22:39:31 +00:00
|
|
|
},
|
|
|
|
{
|
2021-12-07 12:12:47 +00:00
|
|
|
name: "set recovery - config",
|
|
|
|
set: tokens{recovery: "M", recoverySource: TokenSourceConfig},
|
|
|
|
raw: tokens{recovery: "M", recoverySource: TokenSourceConfig},
|
|
|
|
effective: tokens{recovery: "M"},
|
2017-08-03 22:39:31 +00:00
|
|
|
},
|
|
|
|
{
|
2021-12-07 12:12:47 +00:00
|
|
|
name: "set recovery - api",
|
|
|
|
set: tokens{recovery: "M", recoverySource: TokenSourceAPI},
|
|
|
|
raw: tokens{recovery: "M", recoverySource: TokenSourceAPI},
|
|
|
|
effective: tokens{recovery: "M"},
|
2019-02-27 19:28:31 +00:00
|
|
|
},
|
2023-01-10 16:24:02 +00:00
|
|
|
{
|
|
|
|
name: "set registration - config",
|
|
|
|
set: tokens{registration: "G", registrationSource: TokenSourceConfig},
|
|
|
|
raw: tokens{registration: "G", registrationSource: TokenSourceConfig},
|
|
|
|
effective: tokens{registration: "G"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "set registration - api",
|
|
|
|
set: tokens{registration: "G", registrationSource: TokenSourceAPI},
|
|
|
|
raw: tokens{registration: "G", registrationSource: TokenSourceAPI},
|
|
|
|
effective: tokens{registration: "G"},
|
|
|
|
},
|
2019-02-27 19:28:31 +00:00
|
|
|
{
|
|
|
|
name: "set all",
|
2023-01-10 16:24:02 +00:00
|
|
|
set: tokens{user: "U", agent: "A", repl: "R", recovery: "M", registration: "G"},
|
|
|
|
raw: tokens{user: "U", agent: "A", repl: "R", recovery: "M", registration: "G"},
|
|
|
|
effective: tokens{user: "U", agent: "A", repl: "R", recovery: "M", registration: "G"},
|
2017-08-03 22:39:31 +00:00
|
|
|
},
|
2017-07-26 18:03:43 +00:00
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
2017-08-03 22:39:31 +00:00
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
s := new(Store)
|
2020-08-24 21:46:55 +00:00
|
|
|
if tt.set.user != "" {
|
|
|
|
require.True(t, s.UpdateUserToken(tt.set.user, tt.set.userSource))
|
|
|
|
}
|
|
|
|
|
|
|
|
if tt.set.agent != "" {
|
|
|
|
require.True(t, s.UpdateAgentToken(tt.set.agent, tt.set.agentSource))
|
|
|
|
}
|
|
|
|
|
|
|
|
if tt.set.repl != "" {
|
|
|
|
require.True(t, s.UpdateReplicationToken(tt.set.repl, tt.set.replSource))
|
|
|
|
}
|
|
|
|
|
2021-12-07 12:12:47 +00:00
|
|
|
if tt.set.recovery != "" {
|
|
|
|
require.True(t, s.UpdateAgentRecoveryToken(tt.set.recovery, tt.set.recoverySource))
|
2020-08-24 21:46:55 +00:00
|
|
|
}
|
2019-10-04 18:37:34 +00:00
|
|
|
|
2023-01-10 16:24:02 +00:00
|
|
|
if tt.set.registration != "" {
|
|
|
|
require.True(t, s.UpdateConfigFileRegistrationToken(tt.set.registration, tt.set.registrationSource))
|
|
|
|
}
|
|
|
|
|
2019-10-04 18:37:34 +00:00
|
|
|
// If they don't change then they return false.
|
|
|
|
require.False(t, s.UpdateUserToken(tt.set.user, tt.set.userSource))
|
|
|
|
require.False(t, s.UpdateAgentToken(tt.set.agent, tt.set.agentSource))
|
|
|
|
require.False(t, s.UpdateReplicationToken(tt.set.repl, tt.set.replSource))
|
2021-12-07 12:12:47 +00:00
|
|
|
require.False(t, s.UpdateAgentRecoveryToken(tt.set.recovery, tt.set.recoverySource))
|
2023-01-10 16:24:02 +00:00
|
|
|
require.False(t, s.UpdateConfigFileRegistrationToken(tt.set.registration, tt.set.registrationSource))
|
2019-02-27 19:28:31 +00:00
|
|
|
|
|
|
|
require.Equal(t, tt.effective.user, s.UserToken())
|
|
|
|
require.Equal(t, tt.effective.agent, s.AgentToken())
|
2021-12-07 12:12:47 +00:00
|
|
|
require.Equal(t, tt.effective.recovery, s.AgentRecoveryToken())
|
2019-02-27 19:28:31 +00:00
|
|
|
require.Equal(t, tt.effective.repl, s.ReplicationToken())
|
2023-01-10 16:24:02 +00:00
|
|
|
require.Equal(t, tt.effective.registration, s.ConfigFileRegistrationToken())
|
2019-02-27 19:28:31 +00:00
|
|
|
|
|
|
|
tok, src := s.UserTokenAndSource()
|
|
|
|
require.Equal(t, tt.raw.user, tok)
|
|
|
|
require.Equal(t, tt.raw.userSource, src)
|
|
|
|
|
|
|
|
tok, src = s.AgentTokenAndSource()
|
|
|
|
require.Equal(t, tt.raw.agent, tok)
|
|
|
|
require.Equal(t, tt.raw.agentSource, src)
|
|
|
|
|
2021-12-07 12:12:47 +00:00
|
|
|
tok, src = s.AgentRecoveryTokenAndSource()
|
|
|
|
require.Equal(t, tt.raw.recovery, tok)
|
|
|
|
require.Equal(t, tt.raw.recoverySource, src)
|
2019-02-27 19:28:31 +00:00
|
|
|
|
|
|
|
tok, src = s.ReplicationTokenAndSource()
|
|
|
|
require.Equal(t, tt.raw.repl, tok)
|
|
|
|
require.Equal(t, tt.raw.replSource, src)
|
2023-01-10 16:24:02 +00:00
|
|
|
|
|
|
|
tok, src = s.ConfigFileRegistrationTokenAndSource()
|
|
|
|
require.Equal(t, tt.raw.registration, tok)
|
|
|
|
require.Equal(t, tt.raw.registrationSource, src)
|
2017-08-03 22:39:31 +00:00
|
|
|
})
|
2017-07-26 18:03:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-07 12:12:47 +00:00
|
|
|
func TestStore_AgentRecoveryToken(t *testing.T) {
|
2017-08-03 22:39:31 +00:00
|
|
|
s := new(Store)
|
2017-07-26 18:03:43 +00:00
|
|
|
|
|
|
|
verify := func(want bool, toks ...string) {
|
|
|
|
for _, tok := range toks {
|
2021-12-07 12:12:47 +00:00
|
|
|
require.Equal(t, want, s.IsAgentRecoveryToken(tok))
|
2017-07-26 18:03:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
verify(false, "", "nope")
|
|
|
|
|
2021-12-07 12:12:47 +00:00
|
|
|
s.UpdateAgentRecoveryToken("recovery", TokenSourceConfig)
|
|
|
|
verify(true, "recovery")
|
2017-07-26 18:03:43 +00:00
|
|
|
verify(false, "", "nope")
|
|
|
|
|
2021-12-07 12:12:47 +00:00
|
|
|
s.UpdateAgentRecoveryToken("another", TokenSourceConfig)
|
2017-07-26 18:03:43 +00:00
|
|
|
verify(true, "another")
|
2021-12-07 12:12:47 +00:00
|
|
|
verify(false, "", "nope", "recovery")
|
2017-07-26 18:03:43 +00:00
|
|
|
|
2021-12-07 12:12:47 +00:00
|
|
|
s.UpdateAgentRecoveryToken("", TokenSourceConfig)
|
|
|
|
verify(false, "", "nope", "recovery", "another")
|
2017-07-26 18:03:43 +00:00
|
|
|
}
|
2020-07-14 13:53:55 +00:00
|
|
|
|
|
|
|
func TestStore_Notify(t *testing.T) {
|
|
|
|
s := new(Store)
|
|
|
|
|
|
|
|
newNotification := func(t *testing.T, s *Store, kind TokenKind) Notifier {
|
|
|
|
n := s.Notify(kind)
|
|
|
|
require.NotNil(t, n.Ch)
|
|
|
|
return n
|
|
|
|
}
|
|
|
|
|
|
|
|
requireNotNotified := func(t *testing.T, ch <-chan struct{}) {
|
|
|
|
require.Empty(t, ch)
|
|
|
|
}
|
|
|
|
|
|
|
|
requireNotifiedOnce := func(t *testing.T, ch <-chan struct{}) {
|
|
|
|
require.Len(t, ch, 1)
|
|
|
|
// drain the channel
|
|
|
|
<-ch
|
|
|
|
// just to be safe
|
|
|
|
require.Empty(t, ch)
|
|
|
|
}
|
|
|
|
|
|
|
|
agentNotifier := newNotification(t, s, TokenKindAgent)
|
|
|
|
userNotifier := newNotification(t, s, TokenKindUser)
|
2021-12-07 12:12:47 +00:00
|
|
|
agentRecoveryNotifier := newNotification(t, s, TokenKindAgentRecovery)
|
2020-07-14 13:53:55 +00:00
|
|
|
replicationNotifier := newNotification(t, s, TokenKindReplication)
|
|
|
|
replicationNotifier2 := newNotification(t, s, TokenKindReplication)
|
2023-01-10 16:24:02 +00:00
|
|
|
registrationNotifier := newNotification(t, s, TokenKindConfigFileRegistration)
|
2020-07-14 13:53:55 +00:00
|
|
|
|
|
|
|
// perform an update of the user token
|
|
|
|
require.True(t, s.UpdateUserToken("edcae2a2-3b51-4864-b412-c7a568f49cb1", TokenSourceConfig))
|
|
|
|
// do it again to ensure it doesn't block even though nothing has read from the 1 buffered chan yet
|
|
|
|
require.True(t, s.UpdateUserToken("47788919-f944-476a-bda5-446d64be1df8", TokenSourceAPI))
|
|
|
|
|
2023-01-10 16:24:02 +00:00
|
|
|
// ensure notifications were sent to the user notifier and all other notifiers were not notified.
|
2020-07-14 13:53:55 +00:00
|
|
|
requireNotNotified(t, agentNotifier.Ch)
|
|
|
|
requireNotifiedOnce(t, userNotifier.Ch)
|
|
|
|
requireNotNotified(t, replicationNotifier.Ch)
|
2021-12-07 12:12:47 +00:00
|
|
|
requireNotNotified(t, agentRecoveryNotifier.Ch)
|
2020-07-14 13:53:55 +00:00
|
|
|
requireNotNotified(t, replicationNotifier2.Ch)
|
2023-01-10 16:24:02 +00:00
|
|
|
requireNotNotified(t, registrationNotifier.Ch)
|
2020-07-14 13:53:55 +00:00
|
|
|
|
2023-01-10 16:24:02 +00:00
|
|
|
// update the agent token which should send a notification to the agent notifier.
|
2020-07-14 13:53:55 +00:00
|
|
|
require.True(t, s.UpdateAgentToken("5d748ec2-d536-461f-8e2a-1f7eae98d559", TokenSourceAPI))
|
|
|
|
|
|
|
|
requireNotifiedOnce(t, agentNotifier.Ch)
|
|
|
|
requireNotNotified(t, userNotifier.Ch)
|
|
|
|
requireNotNotified(t, replicationNotifier.Ch)
|
2021-12-07 12:12:47 +00:00
|
|
|
requireNotNotified(t, agentRecoveryNotifier.Ch)
|
2020-07-14 13:53:55 +00:00
|
|
|
requireNotNotified(t, replicationNotifier2.Ch)
|
2023-01-10 16:24:02 +00:00
|
|
|
requireNotNotified(t, registrationNotifier.Ch)
|
2020-07-14 13:53:55 +00:00
|
|
|
|
2023-01-10 16:24:02 +00:00
|
|
|
// update the agent recovery token which should send a notification to the agent recovery notifier.
|
2021-12-07 12:12:47 +00:00
|
|
|
require.True(t, s.UpdateAgentRecoveryToken("789badc8-f850-43e1-8742-9b9f484957cc", TokenSourceAPI))
|
2020-07-14 13:53:55 +00:00
|
|
|
|
|
|
|
requireNotNotified(t, agentNotifier.Ch)
|
|
|
|
requireNotNotified(t, userNotifier.Ch)
|
|
|
|
requireNotNotified(t, replicationNotifier.Ch)
|
2021-12-07 12:12:47 +00:00
|
|
|
requireNotifiedOnce(t, agentRecoveryNotifier.Ch)
|
2020-07-14 13:53:55 +00:00
|
|
|
requireNotNotified(t, replicationNotifier2.Ch)
|
2023-01-10 16:24:02 +00:00
|
|
|
requireNotNotified(t, registrationNotifier.Ch)
|
2020-07-14 13:53:55 +00:00
|
|
|
|
2023-01-10 16:24:02 +00:00
|
|
|
// update the replication token which should send a notification to the replication notifier.
|
2020-07-14 13:53:55 +00:00
|
|
|
require.True(t, s.UpdateReplicationToken("789badc8-f850-43e1-8742-9b9f484957cc", TokenSourceAPI))
|
|
|
|
|
|
|
|
requireNotNotified(t, agentNotifier.Ch)
|
|
|
|
requireNotNotified(t, userNotifier.Ch)
|
|
|
|
requireNotifiedOnce(t, replicationNotifier.Ch)
|
2021-12-07 12:12:47 +00:00
|
|
|
requireNotNotified(t, agentRecoveryNotifier.Ch)
|
2020-07-14 13:53:55 +00:00
|
|
|
requireNotifiedOnce(t, replicationNotifier2.Ch)
|
2023-01-10 16:24:02 +00:00
|
|
|
requireNotNotified(t, registrationNotifier.Ch)
|
2020-07-14 13:53:55 +00:00
|
|
|
|
|
|
|
s.StopNotify(replicationNotifier2)
|
|
|
|
|
2023-01-10 16:24:02 +00:00
|
|
|
// update the replication token which should send a notification to the replication notifier.
|
2020-07-14 13:53:55 +00:00
|
|
|
require.True(t, s.UpdateReplicationToken("eb0b56b9-fa65-4ae1-902a-c64457c62ac6", TokenSourceAPI))
|
|
|
|
|
|
|
|
requireNotNotified(t, agentNotifier.Ch)
|
|
|
|
requireNotNotified(t, userNotifier.Ch)
|
|
|
|
requireNotifiedOnce(t, replicationNotifier.Ch)
|
2021-12-07 12:12:47 +00:00
|
|
|
requireNotNotified(t, agentRecoveryNotifier.Ch)
|
2020-07-14 13:53:55 +00:00
|
|
|
requireNotNotified(t, replicationNotifier2.Ch)
|
2023-01-10 16:24:02 +00:00
|
|
|
requireNotNotified(t, registrationNotifier.Ch)
|
|
|
|
|
|
|
|
// update the config file registration token which should send a notification to the replication notifier.
|
|
|
|
require.True(t, s.UpdateConfigFileRegistrationToken("82fe7362-7d83-4f43-bb27-c35f1f15083c", TokenSourceAPI))
|
|
|
|
|
|
|
|
requireNotNotified(t, agentNotifier.Ch)
|
|
|
|
requireNotNotified(t, userNotifier.Ch)
|
|
|
|
requireNotNotified(t, replicationNotifier.Ch)
|
|
|
|
requireNotNotified(t, agentRecoveryNotifier.Ch)
|
|
|
|
requireNotNotified(t, replicationNotifier2.Ch)
|
|
|
|
requireNotifiedOnce(t, registrationNotifier.Ch)
|
2020-07-14 13:53:55 +00:00
|
|
|
|
2023-01-10 16:24:02 +00:00
|
|
|
// request updates that are not changes
|
2020-07-14 13:53:55 +00:00
|
|
|
require.False(t, s.UpdateAgentToken("5d748ec2-d536-461f-8e2a-1f7eae98d559", TokenSourceAPI))
|
2021-12-07 12:12:47 +00:00
|
|
|
require.False(t, s.UpdateAgentRecoveryToken("789badc8-f850-43e1-8742-9b9f484957cc", TokenSourceAPI))
|
2020-07-14 13:53:55 +00:00
|
|
|
require.False(t, s.UpdateUserToken("47788919-f944-476a-bda5-446d64be1df8", TokenSourceAPI))
|
|
|
|
require.False(t, s.UpdateReplicationToken("eb0b56b9-fa65-4ae1-902a-c64457c62ac6", TokenSourceAPI))
|
2023-01-10 16:24:02 +00:00
|
|
|
require.False(t, s.UpdateConfigFileRegistrationToken("82fe7362-7d83-4f43-bb27-c35f1f15083c", TokenSourceAPI))
|
2020-07-14 13:53:55 +00:00
|
|
|
|
|
|
|
// ensure that notifications were not sent
|
|
|
|
requireNotNotified(t, agentNotifier.Ch)
|
|
|
|
requireNotNotified(t, userNotifier.Ch)
|
|
|
|
requireNotNotified(t, replicationNotifier.Ch)
|
2021-12-07 12:12:47 +00:00
|
|
|
requireNotNotified(t, agentRecoveryNotifier.Ch)
|
2023-01-10 16:24:02 +00:00
|
|
|
requireNotNotified(t, registrationNotifier.Ch)
|
2020-07-14 13:53:55 +00:00
|
|
|
}
|