2022-05-20 16:16:21 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/hashicorp/nomad/api/internal/testutil"
|
2023-01-01 18:57:26 +00:00
|
|
|
"github.com/shoenig/test/must"
|
2022-05-20 16:16:21 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestKeyring_CRUD(t *testing.T) {
|
|
|
|
testutil.Parallel(t)
|
2023-01-01 18:57:26 +00:00
|
|
|
|
2022-05-20 16:16:21 +00:00
|
|
|
c, s := makeClient(t, nil, nil)
|
|
|
|
defer s.Stop()
|
|
|
|
|
|
|
|
kr := c.Keyring()
|
|
|
|
|
2022-09-09 12:50:35 +00:00
|
|
|
// Find the bootstrap key
|
|
|
|
keys, qm, err := kr.List(nil)
|
2023-01-01 18:57:26 +00:00
|
|
|
must.NoError(t, err)
|
2022-09-09 12:50:35 +00:00
|
|
|
assertQueryMeta(t, qm)
|
2023-01-01 18:57:26 +00:00
|
|
|
must.Len(t, 1, keys)
|
2022-09-09 12:50:35 +00:00
|
|
|
oldKeyID := keys[0].KeyID
|
|
|
|
|
2022-05-20 16:16:21 +00:00
|
|
|
// Create a key by requesting a rotation
|
|
|
|
key, wm, err := kr.Rotate(nil, nil)
|
2023-01-01 18:57:26 +00:00
|
|
|
must.NoError(t, err)
|
|
|
|
must.NotNil(t, key)
|
2022-05-20 16:16:21 +00:00
|
|
|
assertWriteMeta(t, wm)
|
|
|
|
|
|
|
|
// Read all the keys
|
2022-09-09 12:50:35 +00:00
|
|
|
keys, qm, err = kr.List(&QueryOptions{WaitIndex: key.CreateIndex})
|
2023-01-01 18:57:26 +00:00
|
|
|
must.NoError(t, err)
|
2022-05-20 16:16:21 +00:00
|
|
|
assertQueryMeta(t, qm)
|
2023-01-01 18:57:26 +00:00
|
|
|
must.Len(t, 2, keys)
|
2022-05-20 16:16:21 +00:00
|
|
|
|
|
|
|
// Delete the old key
|
2022-09-09 12:50:35 +00:00
|
|
|
wm, err = kr.Delete(&KeyringDeleteOptions{KeyID: oldKeyID}, nil)
|
2023-01-01 18:57:26 +00:00
|
|
|
must.NoError(t, err)
|
2022-05-20 16:16:21 +00:00
|
|
|
assertWriteMeta(t, wm)
|
|
|
|
|
|
|
|
// Read all the keys back
|
|
|
|
keys, qm, err = kr.List(&QueryOptions{WaitIndex: key.CreateIndex})
|
2023-01-01 18:57:26 +00:00
|
|
|
must.NoError(t, err)
|
2022-05-20 16:16:21 +00:00
|
|
|
assertQueryMeta(t, qm)
|
2023-01-01 18:57:26 +00:00
|
|
|
must.Len(t, 1, keys)
|
|
|
|
must.Eq(t, key.KeyID, keys[0].KeyID)
|
|
|
|
must.Eq(t, RootKeyState(RootKeyStateActive), keys[0].State)
|
2022-05-20 16:16:21 +00:00
|
|
|
}
|