2022-05-20 16:16:21 +00:00
|
|
|
package agent
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/base64"
|
|
|
|
"math/rand"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
|
|
|
"github.com/hashicorp/nomad/api"
|
|
|
|
"github.com/hashicorp/nomad/ci"
|
|
|
|
"github.com/hashicorp/nomad/helper/uuid"
|
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestHTTP_Keyring_CRUD(t *testing.T) {
|
|
|
|
ci.Parallel(t)
|
|
|
|
|
|
|
|
httpTest(t, nil, func(s *TestAgent) {
|
|
|
|
|
|
|
|
respW := httptest.NewRecorder()
|
|
|
|
|
|
|
|
// Rotate
|
|
|
|
|
|
|
|
req, err := http.NewRequest(http.MethodPut, "/v1/operator/keyring/rotate", nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
obj, err := s.Server.KeyringRequest(respW, req)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotZero(t, respW.HeaderMap.Get("X-Nomad-Index"))
|
|
|
|
rotateResp := obj.(structs.KeyringRotateRootKeyResponse)
|
|
|
|
require.NotNil(t, rotateResp.Key)
|
2022-07-07 17:48:38 +00:00
|
|
|
require.True(t, rotateResp.Key.Active())
|
2022-05-31 12:43:51 +00:00
|
|
|
newID1 := rotateResp.Key.KeyID
|
2022-05-20 16:16:21 +00:00
|
|
|
|
|
|
|
// List
|
|
|
|
|
|
|
|
req, err = http.NewRequest(http.MethodGet, "/v1/operator/keyring/keys", nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
obj, err = s.Server.KeyringRequest(respW, req)
|
|
|
|
require.NoError(t, err)
|
|
|
|
listResp := obj.([]*structs.RootKeyMeta)
|
2022-05-31 12:43:51 +00:00
|
|
|
require.Len(t, listResp, 2)
|
|
|
|
for _, key := range listResp {
|
|
|
|
if key.KeyID == newID1 {
|
2022-07-07 17:48:38 +00:00
|
|
|
require.True(t, key.Active(), "new key should be active")
|
2022-05-31 12:43:51 +00:00
|
|
|
} else {
|
2022-07-07 17:48:38 +00:00
|
|
|
require.False(t, key.Active(), "initial key should be inactive")
|
2022-05-31 12:43:51 +00:00
|
|
|
}
|
|
|
|
}
|
2022-05-20 16:16:21 +00:00
|
|
|
|
|
|
|
// Update
|
|
|
|
|
|
|
|
keyMeta := rotateResp.Key
|
2022-05-25 19:05:30 +00:00
|
|
|
keyBuf := make([]byte, 32)
|
2022-05-20 16:16:21 +00:00
|
|
|
rand.Read(keyBuf)
|
2022-06-02 17:41:59 +00:00
|
|
|
encodedKey := base64.StdEncoding.EncodeToString(keyBuf)
|
2022-05-20 16:16:21 +00:00
|
|
|
|
2022-05-31 12:43:51 +00:00
|
|
|
newID2 := uuid.Generate()
|
2022-05-20 16:16:21 +00:00
|
|
|
|
|
|
|
key := &api.RootKey{
|
|
|
|
Meta: &api.RootKeyMeta{
|
2022-07-07 17:48:38 +00:00
|
|
|
State: api.RootKeyStateActive,
|
2022-06-02 17:41:59 +00:00
|
|
|
KeyID: newID2,
|
|
|
|
Algorithm: api.EncryptionAlgorithm(keyMeta.Algorithm),
|
2022-05-20 16:16:21 +00:00
|
|
|
},
|
2022-06-02 17:41:59 +00:00
|
|
|
Key: encodedKey,
|
2022-05-20 16:16:21 +00:00
|
|
|
}
|
|
|
|
reqBuf := encodeReq(key)
|
|
|
|
|
|
|
|
req, err = http.NewRequest(http.MethodPut, "/v1/operator/keyring/keys", reqBuf)
|
|
|
|
require.NoError(t, err)
|
|
|
|
obj, err = s.Server.KeyringRequest(respW, req)
|
|
|
|
require.NoError(t, err)
|
|
|
|
updateResp := obj.(structs.KeyringUpdateRootKeyResponse)
|
|
|
|
require.NotNil(t, updateResp)
|
|
|
|
|
|
|
|
// Delete the old key and verify its gone
|
|
|
|
|
|
|
|
id := rotateResp.Key.KeyID
|
|
|
|
req, err = http.NewRequest(http.MethodDelete, "/v1/operator/keyring/key/"+id, nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
obj, err = s.Server.KeyringRequest(respW, req)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
req, err = http.NewRequest(http.MethodGet, "/v1/operator/keyring/keys", nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
obj, err = s.Server.KeyringRequest(respW, req)
|
|
|
|
require.NoError(t, err)
|
|
|
|
listResp = obj.([]*structs.RootKeyMeta)
|
2022-05-31 12:43:51 +00:00
|
|
|
require.Len(t, listResp, 2)
|
|
|
|
|
|
|
|
for _, key := range listResp {
|
|
|
|
require.NotEqual(t, newID1, key.KeyID)
|
|
|
|
if key.KeyID == newID2 {
|
2022-07-07 17:48:38 +00:00
|
|
|
require.True(t, key.Active(), "new key should be active")
|
2022-05-31 12:43:51 +00:00
|
|
|
} else {
|
2022-07-07 17:48:38 +00:00
|
|
|
require.False(t, key.Active(), "initial key should be inactive")
|
2022-05-31 12:43:51 +00:00
|
|
|
}
|
|
|
|
}
|
2022-05-20 16:16:21 +00:00
|
|
|
})
|
|
|
|
}
|