2023-03-28 18:39:22 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2020-08-17 23:30:25 +00:00
|
|
|
package token
|
|
|
|
|
|
|
|
import (
|
2022-11-10 16:26:01 +00:00
|
|
|
"os"
|
2020-08-17 23:30:25 +00:00
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/hashicorp/consul/sdk/testutil"
|
|
|
|
"github.com/hashicorp/go-hclog"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestStore_Load(t *testing.T) {
|
|
|
|
dataDir := testutil.TempDir(t, "datadir")
|
|
|
|
tokenFile := filepath.Join(dataDir, tokensPath)
|
|
|
|
logger := hclog.New(nil)
|
|
|
|
store := new(Store)
|
|
|
|
|
|
|
|
t.Run("with empty store", func(t *testing.T) {
|
|
|
|
cfg := Config{
|
2023-01-10 16:24:02 +00:00
|
|
|
DataDir: dataDir,
|
|
|
|
ACLAgentToken: "alfa",
|
|
|
|
ACLAgentRecoveryToken: "bravo",
|
|
|
|
ACLDefaultToken: "charlie",
|
|
|
|
ACLReplicationToken: "delta",
|
|
|
|
ACLConfigFileRegistrationToken: "echo",
|
2020-08-17 23:30:25 +00:00
|
|
|
}
|
|
|
|
require.NoError(t, store.Load(cfg, logger))
|
|
|
|
require.Equal(t, "alfa", store.AgentToken())
|
2021-12-07 12:12:47 +00:00
|
|
|
require.Equal(t, "bravo", store.AgentRecoveryToken())
|
2020-08-17 23:30:25 +00:00
|
|
|
require.Equal(t, "charlie", store.UserToken())
|
|
|
|
require.Equal(t, "delta", store.ReplicationToken())
|
2023-01-10 16:24:02 +00:00
|
|
|
require.Equal(t, "echo", store.ConfigFileRegistrationToken())
|
2020-08-17 23:30:25 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("updated from Config", func(t *testing.T) {
|
|
|
|
cfg := Config{
|
2023-01-10 16:24:02 +00:00
|
|
|
DataDir: dataDir,
|
|
|
|
ACLDefaultToken: "echo",
|
|
|
|
ACLAgentToken: "foxtrot",
|
|
|
|
ACLAgentRecoveryToken: "golf",
|
|
|
|
ACLReplicationToken: "hotel",
|
|
|
|
ACLConfigFileRegistrationToken: "india",
|
2020-08-17 23:30:25 +00:00
|
|
|
}
|
|
|
|
// ensures no error for missing persisted tokens file
|
|
|
|
require.NoError(t, store.Load(cfg, logger))
|
|
|
|
require.Equal(t, "echo", store.UserToken())
|
|
|
|
require.Equal(t, "foxtrot", store.AgentToken())
|
2021-12-07 12:12:47 +00:00
|
|
|
require.Equal(t, "golf", store.AgentRecoveryToken())
|
2020-08-17 23:30:25 +00:00
|
|
|
require.Equal(t, "hotel", store.ReplicationToken())
|
2023-01-10 16:24:02 +00:00
|
|
|
require.Equal(t, "india", store.ConfigFileRegistrationToken())
|
2020-08-17 23:30:25 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("with persisted tokens", func(t *testing.T) {
|
|
|
|
cfg := Config{
|
2023-01-10 16:24:02 +00:00
|
|
|
DataDir: dataDir,
|
|
|
|
ACLDefaultToken: "echo",
|
|
|
|
ACLAgentToken: "foxtrot",
|
|
|
|
ACLAgentRecoveryToken: "golf",
|
|
|
|
ACLReplicationToken: "hotel",
|
|
|
|
ACLConfigFileRegistrationToken: "delta",
|
2020-08-17 23:30:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tokens := `{
|
|
|
|
"agent" : "india",
|
2021-12-07 12:12:47 +00:00
|
|
|
"agent_recovery" : "juliett",
|
2020-08-17 23:30:25 +00:00
|
|
|
"default": "kilo",
|
2023-01-10 16:24:02 +00:00
|
|
|
"replication": "lima",
|
|
|
|
"config_file_service_registration": "mike"
|
2020-08-17 23:30:25 +00:00
|
|
|
}`
|
|
|
|
|
2022-11-10 16:26:01 +00:00
|
|
|
require.NoError(t, os.WriteFile(tokenFile, []byte(tokens), 0600))
|
2020-08-17 23:30:25 +00:00
|
|
|
require.NoError(t, store.Load(cfg, logger))
|
|
|
|
|
|
|
|
// no updates since token persistence is not enabled
|
|
|
|
require.Equal(t, "echo", store.UserToken())
|
|
|
|
require.Equal(t, "foxtrot", store.AgentToken())
|
2021-12-07 12:12:47 +00:00
|
|
|
require.Equal(t, "golf", store.AgentRecoveryToken())
|
2020-08-17 23:30:25 +00:00
|
|
|
require.Equal(t, "hotel", store.ReplicationToken())
|
2023-01-10 16:24:02 +00:00
|
|
|
require.Equal(t, "delta", store.ConfigFileRegistrationToken())
|
2020-08-17 23:30:25 +00:00
|
|
|
|
|
|
|
cfg.EnablePersistence = true
|
|
|
|
require.NoError(t, store.Load(cfg, logger))
|
|
|
|
|
|
|
|
require.Equal(t, "india", store.AgentToken())
|
2021-12-07 12:12:47 +00:00
|
|
|
require.Equal(t, "juliett", store.AgentRecoveryToken())
|
2020-08-17 23:30:25 +00:00
|
|
|
require.Equal(t, "kilo", store.UserToken())
|
|
|
|
require.Equal(t, "lima", store.ReplicationToken())
|
2023-01-10 16:24:02 +00:00
|
|
|
require.Equal(t, "mike", store.ConfigFileRegistrationToken())
|
2020-08-17 23:30:25 +00:00
|
|
|
|
|
|
|
// check store persistence was enabled
|
|
|
|
require.NotNil(t, store.persistence)
|
|
|
|
})
|
|
|
|
|
2021-12-07 12:12:47 +00:00
|
|
|
t.Run("persisted tokens include pre-1.11 agent_master naming", func(t *testing.T) {
|
|
|
|
cfg := Config{
|
|
|
|
EnablePersistence: true,
|
|
|
|
DataDir: dataDir,
|
|
|
|
ACLAgentRecoveryToken: "golf",
|
|
|
|
}
|
|
|
|
|
|
|
|
tokens := `{"agent_master": "juliett"}`
|
2022-11-10 16:26:01 +00:00
|
|
|
require.NoError(t, os.WriteFile(tokenFile, []byte(tokens), 0600))
|
2021-12-07 12:12:47 +00:00
|
|
|
require.NoError(t, store.Load(cfg, logger))
|
|
|
|
|
|
|
|
require.Equal(t, "juliett", store.AgentRecoveryToken())
|
|
|
|
})
|
|
|
|
|
2020-08-17 23:30:25 +00:00
|
|
|
t.Run("with persisted tokens, persisted tokens override config", func(t *testing.T) {
|
|
|
|
tokens := `{
|
|
|
|
"agent" : "mike",
|
2021-12-07 12:12:47 +00:00
|
|
|
"agent_recovery" : "november",
|
2020-08-17 23:30:25 +00:00
|
|
|
"default": "oscar",
|
2023-01-10 16:24:02 +00:00
|
|
|
"replication" : "papa",
|
|
|
|
"config_file_service_registration" : "lima"
|
2020-08-17 23:30:25 +00:00
|
|
|
}`
|
|
|
|
|
|
|
|
cfg := Config{
|
2023-01-10 16:24:02 +00:00
|
|
|
EnablePersistence: true,
|
|
|
|
DataDir: dataDir,
|
|
|
|
ACLDefaultToken: "quebec",
|
|
|
|
ACLAgentToken: "romeo",
|
|
|
|
ACLAgentRecoveryToken: "sierra",
|
|
|
|
ACLReplicationToken: "tango",
|
|
|
|
ACLConfigFileRegistrationToken: "uniform",
|
2020-08-17 23:30:25 +00:00
|
|
|
}
|
|
|
|
|
2022-11-10 16:26:01 +00:00
|
|
|
require.NoError(t, os.WriteFile(tokenFile, []byte(tokens), 0600))
|
2020-08-17 23:30:25 +00:00
|
|
|
require.NoError(t, store.Load(cfg, logger))
|
|
|
|
|
|
|
|
require.Equal(t, "mike", store.AgentToken())
|
2021-12-07 12:12:47 +00:00
|
|
|
require.Equal(t, "november", store.AgentRecoveryToken())
|
2020-08-17 23:30:25 +00:00
|
|
|
require.Equal(t, "oscar", store.UserToken())
|
|
|
|
require.Equal(t, "papa", store.ReplicationToken())
|
2023-01-10 16:24:02 +00:00
|
|
|
require.Equal(t, "lima", store.ConfigFileRegistrationToken())
|
2020-08-17 23:30:25 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("with some persisted tokens", func(t *testing.T) {
|
|
|
|
tokens := `{
|
|
|
|
"agent" : "uniform",
|
2021-12-07 12:12:47 +00:00
|
|
|
"agent_recovery" : "victor"
|
2020-08-17 23:30:25 +00:00
|
|
|
}`
|
|
|
|
|
|
|
|
cfg := Config{
|
2023-01-10 16:24:02 +00:00
|
|
|
EnablePersistence: true,
|
|
|
|
DataDir: dataDir,
|
|
|
|
ACLDefaultToken: "whiskey",
|
|
|
|
ACLAgentToken: "xray",
|
|
|
|
ACLAgentRecoveryToken: "yankee",
|
|
|
|
ACLReplicationToken: "zulu",
|
|
|
|
ACLConfigFileRegistrationToken: "victor",
|
2020-08-17 23:30:25 +00:00
|
|
|
}
|
|
|
|
|
2022-11-10 16:26:01 +00:00
|
|
|
require.NoError(t, os.WriteFile(tokenFile, []byte(tokens), 0600))
|
2020-08-17 23:30:25 +00:00
|
|
|
require.NoError(t, store.Load(cfg, logger))
|
|
|
|
|
|
|
|
require.Equal(t, "uniform", store.AgentToken())
|
2021-12-07 12:12:47 +00:00
|
|
|
require.Equal(t, "victor", store.AgentRecoveryToken())
|
2020-08-17 23:30:25 +00:00
|
|
|
require.Equal(t, "whiskey", store.UserToken())
|
|
|
|
require.Equal(t, "zulu", store.ReplicationToken())
|
2023-01-10 16:24:02 +00:00
|
|
|
require.Equal(t, "victor", store.ConfigFileRegistrationToken())
|
2020-08-17 23:30:25 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("persisted file contains invalid data", func(t *testing.T) {
|
|
|
|
cfg := Config{
|
2023-01-10 16:24:02 +00:00
|
|
|
EnablePersistence: true,
|
|
|
|
DataDir: dataDir,
|
|
|
|
ACLDefaultToken: "one",
|
|
|
|
ACLAgentToken: "two",
|
|
|
|
ACLAgentRecoveryToken: "three",
|
|
|
|
ACLReplicationToken: "four",
|
|
|
|
ACLConfigFileRegistrationToken: "five",
|
2020-08-17 23:30:25 +00:00
|
|
|
}
|
|
|
|
|
2022-11-10 16:26:01 +00:00
|
|
|
require.NoError(t, os.WriteFile(tokenFile, []byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}, 0600))
|
2020-08-17 23:30:25 +00:00
|
|
|
err := store.Load(cfg, logger)
|
|
|
|
require.Error(t, err)
|
|
|
|
require.Contains(t, err.Error(), "failed to decode tokens file")
|
|
|
|
|
|
|
|
require.Equal(t, "one", store.UserToken())
|
|
|
|
require.Equal(t, "two", store.AgentToken())
|
2021-12-07 12:12:47 +00:00
|
|
|
require.Equal(t, "three", store.AgentRecoveryToken())
|
2020-08-17 23:30:25 +00:00
|
|
|
require.Equal(t, "four", store.ReplicationToken())
|
2023-01-10 16:24:02 +00:00
|
|
|
require.Equal(t, "five", store.ConfigFileRegistrationToken())
|
2020-08-17 23:30:25 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("persisted file contains invalid json", func(t *testing.T) {
|
|
|
|
cfg := Config{
|
2023-01-10 16:24:02 +00:00
|
|
|
EnablePersistence: true,
|
|
|
|
DataDir: dataDir,
|
|
|
|
ACLDefaultToken: "alfa",
|
|
|
|
ACLAgentToken: "bravo",
|
|
|
|
ACLAgentRecoveryToken: "charlie",
|
|
|
|
ACLReplicationToken: "foxtrot",
|
|
|
|
ACLConfigFileRegistrationToken: "golf",
|
2020-08-17 23:30:25 +00:00
|
|
|
}
|
|
|
|
|
2022-11-10 16:26:01 +00:00
|
|
|
require.NoError(t, os.WriteFile(tokenFile, []byte("[1,2,3]"), 0600))
|
2020-08-17 23:30:25 +00:00
|
|
|
err := store.Load(cfg, logger)
|
|
|
|
require.Error(t, err)
|
|
|
|
require.Contains(t, err.Error(), "failed to decode tokens file")
|
|
|
|
|
|
|
|
require.Equal(t, "alfa", store.UserToken())
|
|
|
|
require.Equal(t, "bravo", store.AgentToken())
|
2021-12-07 12:12:47 +00:00
|
|
|
require.Equal(t, "charlie", store.AgentRecoveryToken())
|
2020-08-17 23:30:25 +00:00
|
|
|
require.Equal(t, "foxtrot", store.ReplicationToken())
|
2023-01-10 16:24:02 +00:00
|
|
|
require.Equal(t, "golf", store.ConfigFileRegistrationToken())
|
2020-08-17 23:30:25 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStore_WithPersistenceLock(t *testing.T) {
|
2023-01-10 16:24:02 +00:00
|
|
|
setupStore := func() (string, *Store) {
|
|
|
|
dataDir := testutil.TempDir(t, "datadir")
|
|
|
|
store := new(Store)
|
|
|
|
cfg := Config{
|
|
|
|
EnablePersistence: true,
|
|
|
|
DataDir: dataDir,
|
|
|
|
ACLDefaultToken: "default-token",
|
|
|
|
ACLAgentToken: "agent-token",
|
|
|
|
ACLAgentRecoveryToken: "recovery-token",
|
|
|
|
ACLReplicationToken: "replication-token",
|
|
|
|
ACLConfigFileRegistrationToken: "registration-token",
|
|
|
|
}
|
|
|
|
err := store.Load(cfg, hclog.New(nil))
|
|
|
|
require.NoError(t, err)
|
2020-08-17 23:30:25 +00:00
|
|
|
|
2023-01-10 16:24:02 +00:00
|
|
|
return dataDir, store
|
|
|
|
}
|
2020-08-17 23:30:25 +00:00
|
|
|
|
2023-01-10 16:24:02 +00:00
|
|
|
requirePersistedTokens := func(t *testing.T, dataDir string, expected persistedTokens) {
|
|
|
|
t.Helper()
|
|
|
|
tokens, err := readPersistedFromFile(filepath.Join(dataDir, tokensPath))
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, expected, tokens)
|
2020-08-17 23:30:25 +00:00
|
|
|
}
|
|
|
|
|
2023-01-10 16:24:02 +00:00
|
|
|
t.Run("persist some tokens", func(t *testing.T) {
|
|
|
|
dataDir, store := setupStore()
|
|
|
|
err := store.WithPersistenceLock(func() error {
|
|
|
|
require.True(t, store.UpdateUserToken("the-new-default-token", TokenSourceAPI))
|
|
|
|
require.True(t, store.UpdateAgentRecoveryToken("the-new-recovery-token", TokenSourceAPI))
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Only API-sourced tokens are persisted.
|
|
|
|
requirePersistedTokens(t, dataDir, persistedTokens{
|
|
|
|
Default: "the-new-default-token",
|
|
|
|
AgentRecovery: "the-new-recovery-token",
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("persist all tokens", func(t *testing.T) {
|
|
|
|
dataDir, store := setupStore()
|
|
|
|
err := store.WithPersistenceLock(func() error {
|
|
|
|
require.True(t, store.UpdateUserToken("the-new-default-token", TokenSourceAPI))
|
|
|
|
require.True(t, store.UpdateAgentToken("the-new-agent-token", TokenSourceAPI))
|
|
|
|
require.True(t, store.UpdateAgentRecoveryToken("the-new-recovery-token", TokenSourceAPI))
|
|
|
|
require.True(t, store.UpdateReplicationToken("the-new-replication-token", TokenSourceAPI))
|
|
|
|
require.True(t, store.UpdateConfigFileRegistrationToken("the-new-registration-token", TokenSourceAPI))
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
requirePersistedTokens(t, dataDir, persistedTokens{
|
|
|
|
Default: "the-new-default-token",
|
|
|
|
Agent: "the-new-agent-token",
|
|
|
|
AgentRecovery: "the-new-recovery-token",
|
|
|
|
Replication: "the-new-replication-token",
|
|
|
|
ConfigFileRegistration: "the-new-registration-token",
|
|
|
|
})
|
|
|
|
})
|
2020-08-17 23:30:25 +00:00
|
|
|
|
|
|
|
}
|