2017-02-02 19:24:20 +00:00
|
|
|
package transit
|
|
|
|
|
|
|
|
import (
|
2018-01-08 18:31:38 +00:00
|
|
|
"context"
|
2021-05-27 19:05:20 +00:00
|
|
|
"encoding/json"
|
2020-06-10 17:31:46 +00:00
|
|
|
"reflect"
|
2021-07-15 05:56:37 +00:00
|
|
|
"strings"
|
2017-02-02 19:24:20 +00:00
|
|
|
"testing"
|
|
|
|
|
2022-01-27 18:06:34 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/helper/keysutil"
|
|
|
|
|
2019-04-12 21:54:35 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/logical"
|
2017-02-02 19:24:20 +00:00
|
|
|
"github.com/mitchellh/mapstructure"
|
|
|
|
)
|
|
|
|
|
2022-02-22 21:00:25 +00:00
|
|
|
func TestTransit_MissingPlaintext(t *testing.T) {
|
|
|
|
var resp *logical.Response
|
|
|
|
var err error
|
|
|
|
|
|
|
|
b, s := createBackendWithStorage(t)
|
|
|
|
|
|
|
|
// Create the policy
|
|
|
|
policyReq := &logical.Request{
|
|
|
|
Operation: logical.UpdateOperation,
|
|
|
|
Path: "keys/existing_key",
|
|
|
|
Storage: s,
|
|
|
|
}
|
|
|
|
resp, err = b.HandleRequest(context.Background(), policyReq)
|
|
|
|
if err != nil || (resp != nil && resp.IsError()) {
|
|
|
|
t.Fatalf("err:%v resp:%#v", err, resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
encReq := &logical.Request{
|
|
|
|
Operation: logical.UpdateOperation,
|
|
|
|
Path: "encrypt/existing_key",
|
|
|
|
Storage: s,
|
2022-03-18 20:10:38 +00:00
|
|
|
Data: map[string]interface{}{},
|
2022-02-22 21:00:25 +00:00
|
|
|
}
|
|
|
|
resp, err = b.HandleRequest(context.Background(), encReq)
|
|
|
|
if resp == nil || !resp.IsError() {
|
|
|
|
t.Fatalf("expected error due to missing plaintext in request, err:%v resp:%#v", err, resp)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTransit_MissingPlaintextInBatchInput(t *testing.T) {
|
|
|
|
var resp *logical.Response
|
|
|
|
var err error
|
|
|
|
|
|
|
|
b, s := createBackendWithStorage(t)
|
|
|
|
|
|
|
|
// Create the policy
|
|
|
|
policyReq := &logical.Request{
|
|
|
|
Operation: logical.UpdateOperation,
|
|
|
|
Path: "keys/existing_key",
|
|
|
|
Storage: s,
|
|
|
|
}
|
|
|
|
resp, err = b.HandleRequest(context.Background(), policyReq)
|
|
|
|
if err != nil || (resp != nil && resp.IsError()) {
|
|
|
|
t.Fatalf("err:%v resp:%#v", err, resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
batchInput := []interface{}{
|
|
|
|
map[string]interface{}{}, // Note that there is no map entry for plaintext
|
|
|
|
}
|
|
|
|
|
|
|
|
batchData := map[string]interface{}{
|
|
|
|
"batch_input": batchInput,
|
|
|
|
}
|
|
|
|
batchReq := &logical.Request{
|
|
|
|
Operation: logical.CreateOperation,
|
|
|
|
Path: "encrypt/upserted_key",
|
|
|
|
Storage: s,
|
|
|
|
Data: batchData,
|
|
|
|
}
|
|
|
|
resp, err = b.HandleRequest(context.Background(), batchReq)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("expected error due to missing plaintext in request, err:%v resp:%#v", err, resp)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-02 19:24:20 +00:00
|
|
|
// Case1: Ensure that batch encryption did not affect the normal flow of
|
|
|
|
// encrypting the plaintext with a pre-existing key.
|
|
|
|
func TestTransit_BatchEncryptionCase1(t *testing.T) {
|
|
|
|
var resp *logical.Response
|
|
|
|
var err error
|
2017-02-06 19:56:16 +00:00
|
|
|
|
2017-02-02 19:24:20 +00:00
|
|
|
b, s := createBackendWithStorage(t)
|
|
|
|
|
|
|
|
// Create the policy
|
|
|
|
policyReq := &logical.Request{
|
|
|
|
Operation: logical.UpdateOperation,
|
|
|
|
Path: "keys/existing_key",
|
|
|
|
Storage: s,
|
|
|
|
}
|
2018-01-08 18:31:38 +00:00
|
|
|
resp, err = b.HandleRequest(context.Background(), policyReq)
|
2017-02-02 19:24:20 +00:00
|
|
|
if err != nil || (resp != nil && resp.IsError()) {
|
|
|
|
t.Fatalf("err:%v resp:%#v", err, resp)
|
|
|
|
}
|
|
|
|
|
2017-11-03 14:45:53 +00:00
|
|
|
plaintext := "dGhlIHF1aWNrIGJyb3duIGZveA==" // "the quick brown fox"
|
2017-02-02 19:24:20 +00:00
|
|
|
|
|
|
|
encData := map[string]interface{}{
|
|
|
|
"plaintext": plaintext,
|
|
|
|
}
|
|
|
|
|
|
|
|
encReq := &logical.Request{
|
|
|
|
Operation: logical.UpdateOperation,
|
|
|
|
Path: "encrypt/existing_key",
|
|
|
|
Storage: s,
|
|
|
|
Data: encData,
|
|
|
|
}
|
2018-01-08 18:31:38 +00:00
|
|
|
resp, err = b.HandleRequest(context.Background(), encReq)
|
2017-02-02 19:24:20 +00:00
|
|
|
if err != nil || (resp != nil && resp.IsError()) {
|
|
|
|
t.Fatalf("err:%v resp:%#v", err, resp)
|
|
|
|
}
|
|
|
|
|
2020-06-01 17:16:01 +00:00
|
|
|
keyVersion := resp.Data["key_version"].(int)
|
|
|
|
if keyVersion != 1 {
|
|
|
|
t.Fatalf("unexpected key version; got: %d, expected: %d", keyVersion, 1)
|
|
|
|
}
|
|
|
|
|
2017-02-02 19:24:20 +00:00
|
|
|
ciphertext := resp.Data["ciphertext"]
|
|
|
|
|
|
|
|
decData := map[string]interface{}{
|
|
|
|
"ciphertext": ciphertext,
|
|
|
|
}
|
|
|
|
decReq := &logical.Request{
|
|
|
|
Operation: logical.UpdateOperation,
|
|
|
|
Path: "decrypt/existing_key",
|
|
|
|
Storage: s,
|
|
|
|
Data: decData,
|
|
|
|
}
|
2018-01-08 18:31:38 +00:00
|
|
|
resp, err = b.HandleRequest(context.Background(), decReq)
|
2017-02-02 19:24:20 +00:00
|
|
|
if err != nil || (resp != nil && resp.IsError()) {
|
|
|
|
t.Fatalf("err:%v resp:%#v", err, resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp.Data["plaintext"] != plaintext {
|
|
|
|
t.Fatalf("bad: plaintext. Expected: %q, Actual: %q", plaintext, resp.Data["plaintext"])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Case2: Ensure that batch encryption did not affect the normal flow of
|
|
|
|
// encrypting the plaintext with the key upserted.
|
|
|
|
func TestTransit_BatchEncryptionCase2(t *testing.T) {
|
|
|
|
var resp *logical.Response
|
|
|
|
var err error
|
|
|
|
b, s := createBackendWithStorage(t)
|
|
|
|
|
|
|
|
// Upsert the key and encrypt the data
|
|
|
|
plaintext := "dGhlIHF1aWNrIGJyb3duIGZveA=="
|
|
|
|
|
|
|
|
encData := map[string]interface{}{
|
|
|
|
"plaintext": plaintext,
|
|
|
|
}
|
|
|
|
|
|
|
|
encReq := &logical.Request{
|
|
|
|
Operation: logical.CreateOperation,
|
|
|
|
Path: "encrypt/upserted_key",
|
|
|
|
Storage: s,
|
|
|
|
Data: encData,
|
|
|
|
}
|
2018-01-08 18:31:38 +00:00
|
|
|
resp, err = b.HandleRequest(context.Background(), encReq)
|
2017-02-02 19:24:20 +00:00
|
|
|
if err != nil || (resp != nil && resp.IsError()) {
|
|
|
|
t.Fatalf("err:%v resp:%#v", err, resp)
|
|
|
|
}
|
|
|
|
|
2020-06-01 17:16:01 +00:00
|
|
|
keyVersion := resp.Data["key_version"].(int)
|
|
|
|
if keyVersion != 1 {
|
|
|
|
t.Fatalf("unexpected key version; got: %d, expected: %d", keyVersion, 1)
|
|
|
|
}
|
|
|
|
|
2017-02-02 19:24:20 +00:00
|
|
|
ciphertext := resp.Data["ciphertext"]
|
|
|
|
decData := map[string]interface{}{
|
|
|
|
"ciphertext": ciphertext,
|
|
|
|
}
|
|
|
|
|
|
|
|
policyReq := &logical.Request{
|
|
|
|
Operation: logical.ReadOperation,
|
|
|
|
Path: "keys/upserted_key",
|
|
|
|
Storage: s,
|
|
|
|
}
|
|
|
|
|
2018-01-08 18:31:38 +00:00
|
|
|
resp, err = b.HandleRequest(context.Background(), policyReq)
|
2017-02-02 19:24:20 +00:00
|
|
|
if err != nil || (resp != nil && resp.IsError()) {
|
|
|
|
t.Fatalf("err:%v resp:%#v", err, resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
decReq := &logical.Request{
|
|
|
|
Operation: logical.UpdateOperation,
|
|
|
|
Path: "decrypt/upserted_key",
|
|
|
|
Storage: s,
|
|
|
|
Data: decData,
|
|
|
|
}
|
2018-01-08 18:31:38 +00:00
|
|
|
resp, err = b.HandleRequest(context.Background(), decReq)
|
2017-02-02 19:24:20 +00:00
|
|
|
if err != nil || (resp != nil && resp.IsError()) {
|
|
|
|
t.Fatalf("err:%v resp:%#v", err, resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp.Data["plaintext"] != plaintext {
|
|
|
|
t.Fatalf("bad: plaintext. Expected: %q, Actual: %q", plaintext, resp.Data["plaintext"])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Case3: If batch encryption input is not base64 encoded, it should fail.
|
|
|
|
func TestTransit_BatchEncryptionCase3(t *testing.T) {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
b, s := createBackendWithStorage(t)
|
|
|
|
|
2017-02-06 19:56:16 +00:00
|
|
|
batchInput := `[{"plaintext":"dGhlIHF1aWNrIGJyb3duIGZveA=="}]`
|
2017-02-02 19:24:20 +00:00
|
|
|
batchData := map[string]interface{}{
|
|
|
|
"batch_input": batchInput,
|
|
|
|
}
|
|
|
|
|
|
|
|
batchReq := &logical.Request{
|
|
|
|
Operation: logical.CreateOperation,
|
|
|
|
Path: "encrypt/upserted_key",
|
|
|
|
Storage: s,
|
|
|
|
Data: batchData,
|
|
|
|
}
|
2018-01-08 18:31:38 +00:00
|
|
|
_, err = b.HandleRequest(context.Background(), batchReq)
|
2017-02-02 19:24:20 +00:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("expected an error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Case4: Test batch encryption with an existing key
|
|
|
|
func TestTransit_BatchEncryptionCase4(t *testing.T) {
|
|
|
|
var resp *logical.Response
|
|
|
|
var err error
|
|
|
|
|
|
|
|
b, s := createBackendWithStorage(t)
|
|
|
|
|
|
|
|
policyReq := &logical.Request{
|
|
|
|
Operation: logical.UpdateOperation,
|
|
|
|
Path: "keys/existing_key",
|
|
|
|
Storage: s,
|
|
|
|
}
|
2018-01-08 18:31:38 +00:00
|
|
|
resp, err = b.HandleRequest(context.Background(), policyReq)
|
2017-02-02 19:24:20 +00:00
|
|
|
if err != nil || (resp != nil && resp.IsError()) {
|
|
|
|
t.Fatalf("err:%v resp:%#v", err, resp)
|
|
|
|
}
|
|
|
|
|
2017-02-06 19:56:16 +00:00
|
|
|
batchInput := []interface{}{
|
|
|
|
map[string]interface{}{"plaintext": "dGhlIHF1aWNrIGJyb3duIGZveA=="},
|
|
|
|
map[string]interface{}{"plaintext": "dGhlIHF1aWNrIGJyb3duIGZveA=="},
|
|
|
|
}
|
|
|
|
|
2017-02-02 19:24:20 +00:00
|
|
|
batchData := map[string]interface{}{
|
2017-02-06 19:56:16 +00:00
|
|
|
"batch_input": batchInput,
|
2017-02-02 19:24:20 +00:00
|
|
|
}
|
|
|
|
batchReq := &logical.Request{
|
|
|
|
Operation: logical.UpdateOperation,
|
|
|
|
Path: "encrypt/existing_key",
|
|
|
|
Storage: s,
|
|
|
|
Data: batchData,
|
|
|
|
}
|
2018-01-08 18:31:38 +00:00
|
|
|
resp, err = b.HandleRequest(context.Background(), batchReq)
|
2017-02-02 19:24:20 +00:00
|
|
|
if err != nil || (resp != nil && resp.IsError()) {
|
|
|
|
t.Fatalf("err:%v resp:%#v", err, resp)
|
|
|
|
}
|
|
|
|
|
2020-09-22 13:43:07 +00:00
|
|
|
batchResponseItems := resp.Data["batch_results"].([]EncryptBatchResponseItem)
|
2017-02-02 19:24:20 +00:00
|
|
|
|
|
|
|
decReq := &logical.Request{
|
|
|
|
Operation: logical.UpdateOperation,
|
|
|
|
Path: "decrypt/existing_key",
|
|
|
|
Storage: s,
|
|
|
|
}
|
|
|
|
|
|
|
|
plaintext := "dGhlIHF1aWNrIGJyb3duIGZveA=="
|
|
|
|
|
2017-02-06 19:56:16 +00:00
|
|
|
for _, item := range batchResponseItems {
|
2020-06-01 17:16:01 +00:00
|
|
|
if item.KeyVersion != 1 {
|
|
|
|
t.Fatalf("unexpected key version; got: %d, expected: %d", item.KeyVersion, 1)
|
|
|
|
}
|
|
|
|
|
2017-02-02 19:24:20 +00:00
|
|
|
decReq.Data = map[string]interface{}{
|
|
|
|
"ciphertext": item.Ciphertext,
|
|
|
|
}
|
2018-01-08 18:31:38 +00:00
|
|
|
resp, err = b.HandleRequest(context.Background(), decReq)
|
2017-02-02 19:24:20 +00:00
|
|
|
if err != nil || (resp != nil && resp.IsError()) {
|
|
|
|
t.Fatalf("err:%v resp:%#v", err, resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp.Data["plaintext"] != plaintext {
|
|
|
|
t.Fatalf("bad: plaintext. Expected: %q, Actual: %q", plaintext, resp.Data["plaintext"])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Case5: Test batch encryption with an existing derived key
|
|
|
|
func TestTransit_BatchEncryptionCase5(t *testing.T) {
|
|
|
|
var resp *logical.Response
|
|
|
|
var err error
|
|
|
|
|
|
|
|
b, s := createBackendWithStorage(t)
|
|
|
|
|
|
|
|
policyData := map[string]interface{}{
|
|
|
|
"derived": true,
|
|
|
|
}
|
|
|
|
|
|
|
|
policyReq := &logical.Request{
|
|
|
|
Operation: logical.UpdateOperation,
|
|
|
|
Path: "keys/existing_key",
|
|
|
|
Storage: s,
|
|
|
|
Data: policyData,
|
|
|
|
}
|
|
|
|
|
2018-01-08 18:31:38 +00:00
|
|
|
resp, err = b.HandleRequest(context.Background(), policyReq)
|
2017-02-02 19:24:20 +00:00
|
|
|
if err != nil || (resp != nil && resp.IsError()) {
|
|
|
|
t.Fatalf("err:%v resp:%#v", err, resp)
|
|
|
|
}
|
|
|
|
|
2017-02-06 19:56:16 +00:00
|
|
|
batchInput := []interface{}{
|
|
|
|
map[string]interface{}{"plaintext": "dGhlIHF1aWNrIGJyb3duIGZveA==", "context": "dmlzaGFsCg=="},
|
|
|
|
map[string]interface{}{"plaintext": "dGhlIHF1aWNrIGJyb3duIGZveA==", "context": "dmlzaGFsCg=="},
|
|
|
|
}
|
2017-02-02 19:24:20 +00:00
|
|
|
|
|
|
|
batchData := map[string]interface{}{
|
2017-02-06 19:56:16 +00:00
|
|
|
"batch_input": batchInput,
|
2017-02-02 19:24:20 +00:00
|
|
|
}
|
2017-02-06 19:56:16 +00:00
|
|
|
|
2017-02-02 19:24:20 +00:00
|
|
|
batchReq := &logical.Request{
|
|
|
|
Operation: logical.UpdateOperation,
|
|
|
|
Path: "encrypt/existing_key",
|
|
|
|
Storage: s,
|
|
|
|
Data: batchData,
|
|
|
|
}
|
2018-01-08 18:31:38 +00:00
|
|
|
resp, err = b.HandleRequest(context.Background(), batchReq)
|
2017-02-02 19:24:20 +00:00
|
|
|
if err != nil || (resp != nil && resp.IsError()) {
|
|
|
|
t.Fatalf("err:%v resp:%#v", err, resp)
|
|
|
|
}
|
|
|
|
|
2020-09-22 13:43:07 +00:00
|
|
|
batchResponseItems := resp.Data["batch_results"].([]EncryptBatchResponseItem)
|
2017-02-02 19:24:20 +00:00
|
|
|
|
|
|
|
decReq := &logical.Request{
|
|
|
|
Operation: logical.UpdateOperation,
|
|
|
|
Path: "decrypt/existing_key",
|
|
|
|
Storage: s,
|
|
|
|
}
|
|
|
|
|
|
|
|
plaintext := "dGhlIHF1aWNrIGJyb3duIGZveA=="
|
|
|
|
|
2017-02-06 19:56:16 +00:00
|
|
|
for _, item := range batchResponseItems {
|
2020-06-01 17:16:01 +00:00
|
|
|
if item.KeyVersion != 1 {
|
|
|
|
t.Fatalf("unexpected key version; got: %d, expected: %d", item.KeyVersion, 1)
|
|
|
|
}
|
|
|
|
|
2017-02-02 19:24:20 +00:00
|
|
|
decReq.Data = map[string]interface{}{
|
|
|
|
"ciphertext": item.Ciphertext,
|
|
|
|
"context": "dmlzaGFsCg==",
|
|
|
|
}
|
2018-01-08 18:31:38 +00:00
|
|
|
resp, err = b.HandleRequest(context.Background(), decReq)
|
2017-02-02 19:24:20 +00:00
|
|
|
if err != nil || (resp != nil && resp.IsError()) {
|
|
|
|
t.Fatalf("err:%v resp:%#v", err, resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp.Data["plaintext"] != plaintext {
|
|
|
|
t.Fatalf("bad: plaintext. Expected: %q, Actual: %q", plaintext, resp.Data["plaintext"])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Case6: Test batch encryption with an upserted non-derived key
|
|
|
|
func TestTransit_BatchEncryptionCase6(t *testing.T) {
|
|
|
|
var resp *logical.Response
|
|
|
|
var err error
|
|
|
|
|
|
|
|
b, s := createBackendWithStorage(t)
|
|
|
|
|
2017-02-06 19:56:16 +00:00
|
|
|
batchInput := []interface{}{
|
|
|
|
map[string]interface{}{"plaintext": "dGhlIHF1aWNrIGJyb3duIGZveA=="},
|
|
|
|
map[string]interface{}{"plaintext": "dGhlIHF1aWNrIGJyb3duIGZveA=="},
|
|
|
|
}
|
|
|
|
|
2017-02-02 19:24:20 +00:00
|
|
|
batchData := map[string]interface{}{
|
2017-02-06 19:56:16 +00:00
|
|
|
"batch_input": batchInput,
|
2017-02-02 19:24:20 +00:00
|
|
|
}
|
|
|
|
batchReq := &logical.Request{
|
|
|
|
Operation: logical.CreateOperation,
|
|
|
|
Path: "encrypt/upserted_key",
|
|
|
|
Storage: s,
|
|
|
|
Data: batchData,
|
|
|
|
}
|
2018-01-08 18:31:38 +00:00
|
|
|
resp, err = b.HandleRequest(context.Background(), batchReq)
|
2017-02-02 19:24:20 +00:00
|
|
|
if err != nil || (resp != nil && resp.IsError()) {
|
|
|
|
t.Fatalf("err:%v resp:%#v", err, resp)
|
|
|
|
}
|
|
|
|
|
2020-09-22 13:43:07 +00:00
|
|
|
batchResponseItems := resp.Data["batch_results"].([]EncryptBatchResponseItem)
|
2017-02-02 19:24:20 +00:00
|
|
|
|
|
|
|
decReq := &logical.Request{
|
|
|
|
Operation: logical.UpdateOperation,
|
|
|
|
Path: "decrypt/upserted_key",
|
|
|
|
Storage: s,
|
|
|
|
}
|
|
|
|
|
|
|
|
plaintext := "dGhlIHF1aWNrIGJyb3duIGZveA=="
|
|
|
|
|
2017-02-06 19:56:16 +00:00
|
|
|
for _, responseItem := range batchResponseItems {
|
2020-09-22 13:43:07 +00:00
|
|
|
var item EncryptBatchResponseItem
|
2017-02-02 19:24:20 +00:00
|
|
|
if err := mapstructure.Decode(responseItem, &item); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2020-06-01 17:16:01 +00:00
|
|
|
|
|
|
|
if item.KeyVersion != 1 {
|
|
|
|
t.Fatalf("unexpected key version; got: %d, expected: %d", item.KeyVersion, 1)
|
|
|
|
}
|
|
|
|
|
2017-02-02 19:24:20 +00:00
|
|
|
decReq.Data = map[string]interface{}{
|
|
|
|
"ciphertext": item.Ciphertext,
|
|
|
|
}
|
2018-01-08 18:31:38 +00:00
|
|
|
resp, err = b.HandleRequest(context.Background(), decReq)
|
2017-02-02 19:24:20 +00:00
|
|
|
if err != nil || (resp != nil && resp.IsError()) {
|
|
|
|
t.Fatalf("err:%v resp:%#v", err, resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp.Data["plaintext"] != plaintext {
|
|
|
|
t.Fatalf("bad: plaintext. Expected: %q, Actual: %q", plaintext, resp.Data["plaintext"])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Case7: Test batch encryption with an upserted derived key
|
|
|
|
func TestTransit_BatchEncryptionCase7(t *testing.T) {
|
|
|
|
var resp *logical.Response
|
|
|
|
var err error
|
|
|
|
|
|
|
|
b, s := createBackendWithStorage(t)
|
|
|
|
|
2017-02-06 19:56:16 +00:00
|
|
|
batchInput := []interface{}{
|
|
|
|
map[string]interface{}{"plaintext": "dGhlIHF1aWNrIGJyb3duIGZveA==", "context": "dmlzaGFsCg=="},
|
|
|
|
map[string]interface{}{"plaintext": "dGhlIHF1aWNrIGJyb3duIGZveA==", "context": "dmlzaGFsCg=="},
|
|
|
|
}
|
2017-02-02 19:24:20 +00:00
|
|
|
|
|
|
|
batchData := map[string]interface{}{
|
2017-02-06 19:56:16 +00:00
|
|
|
"batch_input": batchInput,
|
2017-02-02 19:24:20 +00:00
|
|
|
}
|
|
|
|
batchReq := &logical.Request{
|
|
|
|
Operation: logical.CreateOperation,
|
|
|
|
Path: "encrypt/upserted_key",
|
|
|
|
Storage: s,
|
|
|
|
Data: batchData,
|
|
|
|
}
|
2018-01-08 18:31:38 +00:00
|
|
|
resp, err = b.HandleRequest(context.Background(), batchReq)
|
2017-02-02 19:24:20 +00:00
|
|
|
if err != nil || (resp != nil && resp.IsError()) {
|
|
|
|
t.Fatalf("err:%v resp:%#v", err, resp)
|
|
|
|
}
|
|
|
|
|
2020-09-22 13:43:07 +00:00
|
|
|
batchResponseItems := resp.Data["batch_results"].([]EncryptBatchResponseItem)
|
2017-02-02 19:24:20 +00:00
|
|
|
|
|
|
|
decReq := &logical.Request{
|
|
|
|
Operation: logical.UpdateOperation,
|
|
|
|
Path: "decrypt/upserted_key",
|
|
|
|
Storage: s,
|
|
|
|
}
|
|
|
|
|
|
|
|
plaintext := "dGhlIHF1aWNrIGJyb3duIGZveA=="
|
|
|
|
|
2017-02-06 19:56:16 +00:00
|
|
|
for _, item := range batchResponseItems {
|
2020-06-01 17:16:01 +00:00
|
|
|
if item.KeyVersion != 1 {
|
|
|
|
t.Fatalf("unexpected key version; got: %d, expected: %d", item.KeyVersion, 1)
|
|
|
|
}
|
|
|
|
|
2017-02-02 19:24:20 +00:00
|
|
|
decReq.Data = map[string]interface{}{
|
|
|
|
"ciphertext": item.Ciphertext,
|
|
|
|
"context": "dmlzaGFsCg==",
|
|
|
|
}
|
2018-01-08 18:31:38 +00:00
|
|
|
resp, err = b.HandleRequest(context.Background(), decReq)
|
2017-02-02 19:24:20 +00:00
|
|
|
if err != nil || (resp != nil && resp.IsError()) {
|
|
|
|
t.Fatalf("err:%v resp:%#v", err, resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp.Data["plaintext"] != plaintext {
|
|
|
|
t.Fatalf("bad: plaintext. Expected: %q, Actual: %q", plaintext, resp.Data["plaintext"])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Case8: If plaintext is not base64 encoded, encryption should fail
|
|
|
|
func TestTransit_BatchEncryptionCase8(t *testing.T) {
|
|
|
|
var resp *logical.Response
|
|
|
|
var err error
|
|
|
|
|
|
|
|
b, s := createBackendWithStorage(t)
|
|
|
|
|
|
|
|
// Create the policy
|
|
|
|
policyReq := &logical.Request{
|
|
|
|
Operation: logical.UpdateOperation,
|
|
|
|
Path: "keys/existing_key",
|
|
|
|
Storage: s,
|
|
|
|
}
|
2018-01-08 18:31:38 +00:00
|
|
|
resp, err = b.HandleRequest(context.Background(), policyReq)
|
2017-02-02 19:24:20 +00:00
|
|
|
if err != nil || (resp != nil && resp.IsError()) {
|
|
|
|
t.Fatalf("err:%v resp:%#v", err, resp)
|
|
|
|
}
|
|
|
|
|
2017-02-06 19:56:16 +00:00
|
|
|
batchInput := []interface{}{
|
|
|
|
map[string]interface{}{"plaintext": "simple_plaintext"},
|
|
|
|
}
|
2017-02-02 19:24:20 +00:00
|
|
|
batchData := map[string]interface{}{
|
2017-02-06 19:56:16 +00:00
|
|
|
"batch_input": batchInput,
|
2017-02-02 19:24:20 +00:00
|
|
|
}
|
|
|
|
batchReq := &logical.Request{
|
|
|
|
Operation: logical.UpdateOperation,
|
|
|
|
Path: "encrypt/existing_key",
|
|
|
|
Storage: s,
|
|
|
|
Data: batchData,
|
|
|
|
}
|
2018-01-08 18:31:38 +00:00
|
|
|
resp, err = b.HandleRequest(context.Background(), batchReq)
|
2017-02-02 19:24:20 +00:00
|
|
|
if err != nil || (resp != nil && resp.IsError()) {
|
|
|
|
t.Fatalf("err:%v resp:%#v", err, resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
plaintext := "simple plaintext"
|
|
|
|
|
|
|
|
encData := map[string]interface{}{
|
|
|
|
"plaintext": plaintext,
|
|
|
|
}
|
|
|
|
|
|
|
|
encReq := &logical.Request{
|
|
|
|
Operation: logical.UpdateOperation,
|
|
|
|
Path: "encrypt/existing_key",
|
|
|
|
Storage: s,
|
|
|
|
Data: encData,
|
|
|
|
}
|
2018-01-08 18:31:38 +00:00
|
|
|
resp, err = b.HandleRequest(context.Background(), encReq)
|
2017-02-02 19:24:20 +00:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("expected an error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Case9: If both plaintext and batch inputs are supplied, plaintext should be
|
|
|
|
// ignored.
|
|
|
|
func TestTransit_BatchEncryptionCase9(t *testing.T) {
|
|
|
|
var resp *logical.Response
|
|
|
|
var err error
|
|
|
|
|
|
|
|
b, s := createBackendWithStorage(t)
|
|
|
|
|
2017-02-06 19:56:16 +00:00
|
|
|
batchInput := []interface{}{
|
|
|
|
map[string]interface{}{"plaintext": "dGhlIHF1aWNrIGJyb3duIGZveA=="},
|
|
|
|
map[string]interface{}{"plaintext": "dGhlIHF1aWNrIGJyb3duIGZveA=="},
|
|
|
|
}
|
2017-02-02 19:24:20 +00:00
|
|
|
plaintext := "dGhlIHF1aWNrIGJyb3duIGZveA=="
|
|
|
|
batchData := map[string]interface{}{
|
2017-02-06 19:56:16 +00:00
|
|
|
"batch_input": batchInput,
|
2017-02-02 19:24:20 +00:00
|
|
|
"plaintext": plaintext,
|
|
|
|
}
|
|
|
|
batchReq := &logical.Request{
|
|
|
|
Operation: logical.CreateOperation,
|
|
|
|
Path: "encrypt/upserted_key",
|
|
|
|
Storage: s,
|
|
|
|
Data: batchData,
|
|
|
|
}
|
2018-01-08 18:31:38 +00:00
|
|
|
resp, err = b.HandleRequest(context.Background(), batchReq)
|
2017-02-02 19:24:20 +00:00
|
|
|
if err != nil || (resp != nil && resp.IsError()) {
|
|
|
|
t.Fatalf("err:%v resp:%#v", err, resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, ok := resp.Data["ciphertext"]
|
|
|
|
if ok {
|
|
|
|
t.Fatal("ciphertext field should not be set")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Case10: Inconsistent presence of 'context' in batch input should be caught
|
|
|
|
func TestTransit_BatchEncryptionCase10(t *testing.T) {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
b, s := createBackendWithStorage(t)
|
|
|
|
|
2017-02-06 19:56:16 +00:00
|
|
|
batchInput := []interface{}{
|
|
|
|
map[string]interface{}{"plaintext": "dGhlIHF1aWNrIGJyb3duIGZveA=="},
|
|
|
|
map[string]interface{}{"plaintext": "dGhlIHF1aWNrIGJyb3duIGZveA==", "context": "dmlzaGFsCg=="},
|
|
|
|
}
|
2017-02-02 19:24:20 +00:00
|
|
|
|
|
|
|
batchData := map[string]interface{}{
|
2017-02-06 19:56:16 +00:00
|
|
|
"batch_input": batchInput,
|
2017-02-02 19:24:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
batchReq := &logical.Request{
|
|
|
|
Operation: logical.CreateOperation,
|
|
|
|
Path: "encrypt/upserted_key",
|
|
|
|
Storage: s,
|
|
|
|
Data: batchData,
|
|
|
|
}
|
2018-01-08 18:31:38 +00:00
|
|
|
_, err = b.HandleRequest(context.Background(), batchReq)
|
2017-02-02 19:24:20 +00:00
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("expected an error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-06 19:56:16 +00:00
|
|
|
// Case11: Incorrect inputs for context and nonce should not fail the operation
|
2017-02-02 19:24:20 +00:00
|
|
|
func TestTransit_BatchEncryptionCase11(t *testing.T) {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
b, s := createBackendWithStorage(t)
|
|
|
|
|
2017-02-06 19:56:16 +00:00
|
|
|
batchInput := []interface{}{
|
|
|
|
map[string]interface{}{"plaintext": "dGhlIHF1aWNrIGJyb3duIGZveA==", "context": "dmlzaGFsCg=="},
|
|
|
|
map[string]interface{}{"plaintext": "dGhlIHF1aWNrIGJyb3duIGZveA==", "context": "not-encoded"},
|
|
|
|
}
|
2017-02-02 19:24:20 +00:00
|
|
|
|
|
|
|
batchData := map[string]interface{}{
|
2017-02-06 19:56:16 +00:00
|
|
|
"batch_input": batchInput,
|
2017-02-02 19:24:20 +00:00
|
|
|
}
|
|
|
|
batchReq := &logical.Request{
|
|
|
|
Operation: logical.CreateOperation,
|
|
|
|
Path: "encrypt/upserted_key",
|
|
|
|
Storage: s,
|
|
|
|
Data: batchData,
|
|
|
|
}
|
2018-01-08 18:31:38 +00:00
|
|
|
_, err = b.HandleRequest(context.Background(), batchReq)
|
2017-02-06 19:56:16 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
2017-02-02 19:24:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Case12: Invalid batch input
|
|
|
|
func TestTransit_BatchEncryptionCase12(t *testing.T) {
|
|
|
|
var err error
|
|
|
|
b, s := createBackendWithStorage(t)
|
|
|
|
|
2017-02-06 19:56:16 +00:00
|
|
|
batchInput := []interface{}{
|
|
|
|
map[string]interface{}{},
|
|
|
|
"unexpected_interface",
|
|
|
|
}
|
|
|
|
|
2017-02-02 19:24:20 +00:00
|
|
|
batchData := map[string]interface{}{
|
2017-02-06 19:56:16 +00:00
|
|
|
"batch_input": batchInput,
|
2017-02-02 19:24:20 +00:00
|
|
|
}
|
|
|
|
batchReq := &logical.Request{
|
|
|
|
Operation: logical.CreateOperation,
|
|
|
|
Path: "encrypt/upserted_key",
|
|
|
|
Storage: s,
|
|
|
|
Data: batchData,
|
|
|
|
}
|
2018-01-08 18:31:38 +00:00
|
|
|
_, err = b.HandleRequest(context.Background(), batchReq)
|
2017-02-02 19:24:20 +00:00
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("expected an error")
|
|
|
|
}
|
|
|
|
}
|
2020-06-10 17:31:46 +00:00
|
|
|
|
2022-01-19 07:32:49 +00:00
|
|
|
// Case13: Incorrect input for nonce when we aren't in convergent encryption should fail the operation
|
|
|
|
func TestTransit_BatchEncryptionCase13(t *testing.T) {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
b, s := createBackendWithStorage(t)
|
|
|
|
|
|
|
|
batchInput := []interface{}{
|
|
|
|
map[string]interface{}{"plaintext": "bXkgc2VjcmV0IGRhdGE=", "nonce": "YmFkbm9uY2U="},
|
|
|
|
}
|
|
|
|
|
|
|
|
batchData := map[string]interface{}{
|
|
|
|
"batch_input": batchInput,
|
|
|
|
}
|
|
|
|
batchReq := &logical.Request{
|
|
|
|
Operation: logical.CreateOperation,
|
|
|
|
Path: "encrypt/my-key",
|
|
|
|
Storage: s,
|
|
|
|
Data: batchData,
|
|
|
|
}
|
|
|
|
_, err = b.HandleRequest(context.Background(), batchReq)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-10 17:31:46 +00:00
|
|
|
// Test that the fast path function decodeBatchRequestItems behave like mapstructure.Decode() to decode []BatchRequestItem.
|
|
|
|
func TestTransit_decodeBatchRequestItems(t *testing.T) {
|
|
|
|
tests := []struct {
|
2022-02-22 21:00:25 +00:00
|
|
|
name string
|
|
|
|
src interface{}
|
|
|
|
requirePlaintext bool
|
|
|
|
requireCiphertext bool
|
|
|
|
dest []BatchRequestItem
|
|
|
|
wantErrContains string
|
2020-06-10 17:31:46 +00:00
|
|
|
}{
|
|
|
|
// basic edge cases of nil values
|
|
|
|
{name: "nil-nil", src: nil, dest: nil},
|
|
|
|
{name: "nil-empty", src: nil, dest: []BatchRequestItem{}},
|
|
|
|
{name: "empty-nil", src: []interface{}{}, dest: nil},
|
|
|
|
{
|
|
|
|
name: "src-nil",
|
|
|
|
src: []interface{}{map[string]interface{}{}},
|
|
|
|
dest: nil,
|
|
|
|
},
|
|
|
|
// empty src & dest
|
|
|
|
{
|
|
|
|
name: "src-dest",
|
|
|
|
src: []interface{}{map[string]interface{}{}},
|
|
|
|
dest: []BatchRequestItem{},
|
|
|
|
},
|
|
|
|
// empty src but with already populated dest, mapstructure discard pre-populated data.
|
|
|
|
{
|
|
|
|
name: "src-dest_pre_filled",
|
|
|
|
src: []interface{}{map[string]interface{}{}},
|
|
|
|
dest: []BatchRequestItem{{}},
|
|
|
|
},
|
|
|
|
// two test per properties to test valid and invalid input
|
|
|
|
{
|
|
|
|
name: "src_plaintext-dest",
|
|
|
|
src: []interface{}{map[string]interface{}{"plaintext": "dGhlIHF1aWNrIGJyb3duIGZveA=="}},
|
|
|
|
dest: []BatchRequestItem{},
|
|
|
|
},
|
|
|
|
{
|
2021-07-15 05:56:37 +00:00
|
|
|
name: "src_plaintext_invalid-dest",
|
|
|
|
src: []interface{}{map[string]interface{}{"plaintext": 666}},
|
|
|
|
dest: []BatchRequestItem{},
|
|
|
|
wantErrContains: "expected type 'string', got unconvertible type 'int'",
|
2020-06-10 17:31:46 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "src_ciphertext-dest",
|
|
|
|
src: []interface{}{map[string]interface{}{"ciphertext": "dGhlIHF1aWNrIGJyb3duIGZveA=="}},
|
|
|
|
dest: []BatchRequestItem{},
|
|
|
|
},
|
|
|
|
{
|
2021-07-15 05:56:37 +00:00
|
|
|
name: "src_ciphertext_invalid-dest",
|
|
|
|
src: []interface{}{map[string]interface{}{"ciphertext": 666}},
|
|
|
|
dest: []BatchRequestItem{},
|
|
|
|
wantErrContains: "expected type 'string', got unconvertible type 'int'",
|
2020-06-10 17:31:46 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "src_key_version-dest",
|
|
|
|
src: []interface{}{map[string]interface{}{"key_version": 1}},
|
|
|
|
dest: []BatchRequestItem{},
|
|
|
|
},
|
|
|
|
{
|
2021-07-15 05:56:37 +00:00
|
|
|
name: "src_key_version_invalid-dest",
|
|
|
|
src: []interface{}{map[string]interface{}{"key_version": "666"}},
|
|
|
|
dest: []BatchRequestItem{},
|
|
|
|
wantErrContains: "expected type 'int', got unconvertible type 'string'",
|
2020-06-10 17:31:46 +00:00
|
|
|
},
|
2021-05-27 19:05:20 +00:00
|
|
|
{
|
2021-07-15 05:56:37 +00:00
|
|
|
name: "src_key_version_invalid-number-dest",
|
|
|
|
src: []interface{}{map[string]interface{}{"plaintext": "dGhlIHF1aWNrIGJyb3duIGZveA==", "key_version": json.Number("1.1")}},
|
|
|
|
dest: []BatchRequestItem{},
|
|
|
|
wantErrContains: "error decoding json.Number into [0].key_version",
|
2021-05-27 19:05:20 +00:00
|
|
|
},
|
2020-06-10 17:31:46 +00:00
|
|
|
{
|
|
|
|
name: "src_nonce-dest",
|
|
|
|
src: []interface{}{map[string]interface{}{"nonce": "dGVzdGNvbnRleHQ="}},
|
|
|
|
dest: []BatchRequestItem{},
|
|
|
|
},
|
|
|
|
{
|
2021-07-15 05:56:37 +00:00
|
|
|
name: "src_nonce_invalid-dest",
|
|
|
|
src: []interface{}{map[string]interface{}{"nonce": 666}},
|
|
|
|
dest: []BatchRequestItem{},
|
|
|
|
wantErrContains: "expected type 'string', got unconvertible type 'int'",
|
2020-06-10 17:31:46 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "src_context-dest",
|
|
|
|
src: []interface{}{map[string]interface{}{"context": "dGVzdGNvbnRleHQ="}},
|
|
|
|
dest: []BatchRequestItem{},
|
|
|
|
},
|
|
|
|
{
|
2021-07-15 05:56:37 +00:00
|
|
|
name: "src_context_invalid-dest",
|
|
|
|
src: []interface{}{map[string]interface{}{"context": 666}},
|
|
|
|
dest: []BatchRequestItem{},
|
|
|
|
wantErrContains: "expected type 'string', got unconvertible type 'int'",
|
2020-06-10 17:31:46 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "src_multi_order-dest",
|
|
|
|
src: []interface{}{
|
|
|
|
map[string]interface{}{"context": "1"},
|
|
|
|
map[string]interface{}{"context": "2"},
|
|
|
|
map[string]interface{}{"context": "3"},
|
|
|
|
},
|
|
|
|
dest: []BatchRequestItem{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "src_multi_with_invalid-dest",
|
|
|
|
src: []interface{}{
|
|
|
|
map[string]interface{}{"context": "1"},
|
|
|
|
map[string]interface{}{"context": "2", "key_version": "666"},
|
|
|
|
map[string]interface{}{"context": "3"},
|
|
|
|
},
|
2021-07-15 05:56:37 +00:00
|
|
|
dest: []BatchRequestItem{},
|
|
|
|
wantErrContains: "expected type 'int', got unconvertible type 'string'",
|
2020-06-10 17:31:46 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "src_multi_with_multi_invalid-dest",
|
|
|
|
src: []interface{}{
|
|
|
|
map[string]interface{}{"context": "1"},
|
|
|
|
map[string]interface{}{"context": "2", "key_version": "666"},
|
|
|
|
map[string]interface{}{"context": "3", "key_version": "1337"},
|
|
|
|
},
|
2021-07-15 05:56:37 +00:00
|
|
|
dest: []BatchRequestItem{},
|
|
|
|
wantErrContains: "expected type 'int', got unconvertible type 'string'",
|
2020-06-10 17:31:46 +00:00
|
|
|
},
|
2020-11-23 19:55:08 +00:00
|
|
|
{
|
|
|
|
name: "src_plaintext-nil-nonce",
|
|
|
|
src: []interface{}{map[string]interface{}{"plaintext": "dGhlIHF1aWNrIGJyb3duIGZveA==", "nonce": "null"}},
|
|
|
|
dest: []BatchRequestItem{},
|
|
|
|
},
|
2022-02-22 21:00:25 +00:00
|
|
|
// required fields
|
|
|
|
{
|
|
|
|
name: "required_plaintext_present",
|
|
|
|
src: []interface{}{map[string]interface{}{"plaintext": ""}},
|
|
|
|
requirePlaintext: true,
|
|
|
|
dest: []BatchRequestItem{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "required_plaintext_missing",
|
|
|
|
src: []interface{}{map[string]interface{}{}},
|
|
|
|
requirePlaintext: true,
|
|
|
|
dest: []BatchRequestItem{},
|
|
|
|
wantErrContains: "missing plaintext",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "required_ciphertext_present",
|
|
|
|
src: []interface{}{map[string]interface{}{"ciphertext": "dGhlIHF1aWNrIGJyb3duIGZveA=="}},
|
|
|
|
requireCiphertext: true,
|
|
|
|
dest: []BatchRequestItem{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "required_ciphertext_missing",
|
|
|
|
src: []interface{}{map[string]interface{}{}},
|
|
|
|
requireCiphertext: true,
|
|
|
|
dest: []BatchRequestItem{},
|
|
|
|
wantErrContains: "missing ciphertext",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "required_plaintext_and_ciphertext_missing",
|
|
|
|
src: []interface{}{map[string]interface{}{}},
|
|
|
|
requirePlaintext: true,
|
|
|
|
requireCiphertext: true,
|
|
|
|
dest: []BatchRequestItem{},
|
|
|
|
wantErrContains: "missing ciphertext",
|
|
|
|
},
|
2020-06-10 17:31:46 +00:00
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
expectedDest := append(tt.dest[:0:0], tt.dest...) // copy of the dest state
|
2022-02-22 21:00:25 +00:00
|
|
|
expectedErr := mapstructure.Decode(tt.src, &expectedDest) != nil || tt.wantErrContains != ""
|
2020-06-10 17:31:46 +00:00
|
|
|
|
2022-02-22 21:00:25 +00:00
|
|
|
gotErr := decodeBatchRequestItems(tt.src, tt.requirePlaintext, tt.requireCiphertext, &tt.dest)
|
2020-06-10 17:31:46 +00:00
|
|
|
gotDest := tt.dest
|
|
|
|
|
2022-02-22 21:00:25 +00:00
|
|
|
if expectedErr {
|
2021-07-15 05:56:37 +00:00
|
|
|
if gotErr == nil {
|
|
|
|
t.Fatal("decodeBatchRequestItems unexpected error value; expected error but got none")
|
|
|
|
}
|
|
|
|
if tt.wantErrContains == "" {
|
|
|
|
t.Fatal("missing error condition")
|
|
|
|
}
|
|
|
|
if !strings.Contains(gotErr.Error(), tt.wantErrContains) {
|
|
|
|
t.Errorf("decodeBatchRequestItems unexpected error value, want err contains: '%v', got: '%v'", tt.wantErrContains, gotErr)
|
|
|
|
}
|
2020-06-10 17:31:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(expectedDest, gotDest) {
|
|
|
|
t.Errorf("decodeBatchRequestItems unexpected dest value, want: '%v', got: '%v'", expectedDest, gotDest)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2021-12-08 19:37:25 +00:00
|
|
|
|
|
|
|
func TestShouldWarnAboutNonceUsage(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
keyTypes []keysutil.KeyType
|
|
|
|
nonce []byte
|
|
|
|
convergentEncryption bool
|
|
|
|
convergentVersion int
|
|
|
|
expected bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "-NoConvergent-WithNonce",
|
|
|
|
keyTypes: []keysutil.KeyType{keysutil.KeyType_AES256_GCM96, keysutil.KeyType_AES128_GCM96, keysutil.KeyType_ChaCha20_Poly1305},
|
|
|
|
nonce: []byte("testnonce"),
|
|
|
|
convergentEncryption: false,
|
|
|
|
convergentVersion: -1,
|
|
|
|
expected: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "-NoConvergent-NoNonce",
|
|
|
|
keyTypes: []keysutil.KeyType{keysutil.KeyType_AES256_GCM96, keysutil.KeyType_AES128_GCM96, keysutil.KeyType_ChaCha20_Poly1305},
|
|
|
|
nonce: []byte{},
|
|
|
|
convergentEncryption: false,
|
|
|
|
convergentVersion: -1,
|
|
|
|
expected: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "-Convergentv1-WithNonce",
|
|
|
|
keyTypes: []keysutil.KeyType{keysutil.KeyType_AES256_GCM96, keysutil.KeyType_AES128_GCM96, keysutil.KeyType_ChaCha20_Poly1305},
|
|
|
|
nonce: []byte("testnonce"),
|
|
|
|
convergentEncryption: true,
|
|
|
|
convergentVersion: 1,
|
|
|
|
expected: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "-Convergentv2-WithNonce",
|
|
|
|
keyTypes: []keysutil.KeyType{keysutil.KeyType_AES256_GCM96, keysutil.KeyType_AES128_GCM96, keysutil.KeyType_ChaCha20_Poly1305},
|
|
|
|
nonce: []byte("testnonce"),
|
|
|
|
convergentEncryption: true,
|
|
|
|
convergentVersion: 2,
|
|
|
|
expected: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "-Convergentv3-WithNonce",
|
|
|
|
keyTypes: []keysutil.KeyType{keysutil.KeyType_AES256_GCM96, keysutil.KeyType_AES128_GCM96, keysutil.KeyType_ChaCha20_Poly1305},
|
|
|
|
nonce: []byte("testnonce"),
|
|
|
|
convergentEncryption: true,
|
|
|
|
convergentVersion: 3,
|
|
|
|
expected: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "-NoConvergent-WithNonce",
|
|
|
|
keyTypes: []keysutil.KeyType{keysutil.KeyType_RSA2048, keysutil.KeyType_RSA4096},
|
|
|
|
nonce: []byte("testnonce"),
|
|
|
|
convergentEncryption: false,
|
|
|
|
convergentVersion: -1,
|
|
|
|
expected: false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
for _, keyType := range tt.keyTypes {
|
|
|
|
testName := keyType.String() + tt.name
|
|
|
|
t.Run(testName, func(t *testing.T) {
|
|
|
|
p := keysutil.Policy{
|
|
|
|
ConvergentEncryption: tt.convergentEncryption,
|
|
|
|
ConvergentVersion: tt.convergentVersion,
|
|
|
|
Type: keyType,
|
|
|
|
}
|
|
|
|
|
|
|
|
actual := shouldWarnAboutNonceUsage(&p, tt.nonce)
|
|
|
|
|
|
|
|
if actual != tt.expected {
|
|
|
|
t.Errorf("Expected actual '%v' but got '%v'", tt.expected, actual)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|