open-nomad/api/keyring_test.go
Tim Gross 5a85d96322 remove end-user algorithm selection (#13190)
After internal design review, we decided to remove exposing algorithm
choice to the end-user for the initial release. We'll solve nonce
rotation by forcing rotations automatically on key GC (in a core job,
not included in this changeset). Default to AES-256 GCM for the
following criteria:

* faster implementation when hardware acceleration is available
* FIPS compliant
* implementation in pure go
* post-quantum resistance

Also fixed a bug in the decoding from keystore and switched to a 
harder-to-misuse encoding method.
2022-07-11 13:34:04 -04:00

66 lines
1.5 KiB
Go

package api
import (
"encoding/base64"
"math/rand"
"testing"
"github.com/stretchr/testify/require"
"github.com/hashicorp/nomad/api/internal/testutil"
)
func TestKeyring_CRUD(t *testing.T) {
testutil.Parallel(t)
c, s := makeClient(t, nil, nil)
defer s.Stop()
kr := c.Keyring()
// Create a key by requesting a rotation
key, wm, err := kr.Rotate(nil, nil)
require.NoError(t, err)
require.NotNil(t, key)
assertWriteMeta(t, wm)
// Read all the keys
keys, qm, err := kr.List(&QueryOptions{WaitIndex: key.CreateIndex})
require.NoError(t, err)
assertQueryMeta(t, qm)
require.Len(t, keys, 2)
// Write a new active key, forcing a rotation
id := "fd77c376-9785-4c80-8e62-4ec3ab5f8b9a"
buf := make([]byte, 32)
rand.Read(buf)
encodedKey := base64.StdEncoding.EncodeToString(buf)
wm, err = kr.Update(&RootKey{
Key: encodedKey,
Meta: &RootKeyMeta{
KeyID: id,
Active: true,
Algorithm: EncryptionAlgorithmAES256GCM,
}}, nil)
require.NoError(t, err)
assertWriteMeta(t, wm)
// Delete the old key
wm, err = kr.Delete(&KeyringDeleteOptions{KeyID: keys[0].KeyID}, nil)
require.NoError(t, err)
assertWriteMeta(t, wm)
// Read all the keys back
keys, qm, err = kr.List(&QueryOptions{WaitIndex: key.CreateIndex})
require.NoError(t, err)
assertQueryMeta(t, qm)
require.Len(t, keys, 2)
for _, key := range keys {
if key.KeyID == id {
require.True(t, key.Active, "new key should be active")
} else {
require.False(t, key.Active, "initial key should be inactive")
}
}
}