2017-02-02 19:24:20 +00:00
|
|
|
package transit
|
|
|
|
|
|
|
|
import (
|
2018-01-08 18:31:38 +00:00
|
|
|
"context"
|
2020-09-22 13:43:07 +00:00
|
|
|
"encoding/json"
|
2019-04-12 21:54:35 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/logical"
|
2020-09-22 13:43:07 +00:00
|
|
|
"testing"
|
2017-02-02 19:24:20 +00:00
|
|
|
)
|
|
|
|
|
2018-10-09 16:40:47 +00:00
|
|
|
func TestTransit_BatchDecryption(t *testing.T) {
|
2017-02-02 19:24:20 +00:00
|
|
|
var resp *logical.Response
|
|
|
|
var err error
|
|
|
|
|
|
|
|
b, s := createBackendWithStorage(t)
|
|
|
|
|
2017-02-06 19:56:16 +00:00
|
|
|
batchEncryptionInput := []interface{}{
|
2020-09-22 13:43:07 +00:00
|
|
|
map[string]interface{}{"plaintext": ""}, // empty string
|
|
|
|
map[string]interface{}{"plaintext": "Cg=="}, // newline
|
2017-02-06 19:56:16 +00:00
|
|
|
map[string]interface{}{"plaintext": "dGhlIHF1aWNrIGJyb3duIGZveA=="},
|
|
|
|
}
|
2017-02-02 19:24:20 +00:00
|
|
|
batchEncryptionData := map[string]interface{}{
|
2017-02-06 19:56:16 +00:00
|
|
|
"batch_input": batchEncryptionInput,
|
2017-02-02 19:24:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
batchEncryptionReq := &logical.Request{
|
|
|
|
Operation: logical.CreateOperation,
|
|
|
|
Path: "encrypt/upserted_key",
|
|
|
|
Storage: s,
|
|
|
|
Data: batchEncryptionData,
|
|
|
|
}
|
2018-01-08 18:31:38 +00:00
|
|
|
resp, err = b.HandleRequest(context.Background(), batchEncryptionReq)
|
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-06 19:56:16 +00:00
|
|
|
batchDecryptionInput := make([]interface{}, len(batchResponseItems))
|
|
|
|
for i, item := range batchResponseItems {
|
|
|
|
batchDecryptionInput[i] = map[string]interface{}{"ciphertext": item.Ciphertext}
|
|
|
|
}
|
2017-02-02 19:24:20 +00:00
|
|
|
batchDecryptionData := map[string]interface{}{
|
2017-02-06 19:56:16 +00:00
|
|
|
"batch_input": batchDecryptionInput,
|
2017-02-02 19:24:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
batchDecryptionReq := &logical.Request{
|
|
|
|
Operation: logical.UpdateOperation,
|
|
|
|
Path: "decrypt/upserted_key",
|
|
|
|
Storage: s,
|
|
|
|
Data: batchDecryptionData,
|
|
|
|
}
|
2018-01-08 18:31:38 +00:00
|
|
|
resp, err = b.HandleRequest(context.Background(), batchDecryptionReq)
|
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
|
|
|
batchDecryptionResponseItems := resp.Data["batch_results"].([]DecryptBatchResponseItem)
|
|
|
|
expectedResult := "[{\"plaintext\":\"\"},{\"plaintext\":\"Cg==\"},{\"plaintext\":\"dGhlIHF1aWNrIGJyb3duIGZveA==\"}]"
|
2017-02-02 19:24:20 +00:00
|
|
|
|
2020-09-22 13:43:07 +00:00
|
|
|
jsonResponse, err := json.Marshal(batchDecryptionResponseItems)
|
|
|
|
if err != nil || err == nil && string(jsonResponse) != expectedResult {
|
|
|
|
t.Fatalf("bad: expected json response [%s]", jsonResponse)
|
2017-02-02 19:24:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-09 16:40:47 +00:00
|
|
|
func TestTransit_BatchDecryption_DerivedKey(t *testing.T) {
|
2017-02-02 19:24:20 +00:00
|
|
|
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": "dGVzdGNvbnRleHQ="},
|
|
|
|
map[string]interface{}{"plaintext": "dGhlIHF1aWNrIGJyb3duIGZveA==", "context": "dGVzdGNvbnRleHQ="},
|
|
|
|
}
|
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
|
|
|
batchDecryptionInputItems := resp.Data["batch_results"].([]EncryptBatchResponseItem)
|
2017-02-02 19:24:20 +00:00
|
|
|
|
2017-02-06 19:56:16 +00:00
|
|
|
batchDecryptionInput := make([]interface{}, len(batchDecryptionInputItems))
|
|
|
|
for i, item := range batchDecryptionInputItems {
|
|
|
|
batchDecryptionInput[i] = map[string]interface{}{"ciphertext": item.Ciphertext, "context": "dGVzdGNvbnRleHQ="}
|
2017-02-02 19:24:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
batchDecryptionData := map[string]interface{}{
|
2017-02-06 19:56:16 +00:00
|
|
|
"batch_input": batchDecryptionInput,
|
2017-02-02 19:24:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
batchDecryptionReq := &logical.Request{
|
|
|
|
Operation: logical.UpdateOperation,
|
|
|
|
Path: "decrypt/existing_key",
|
|
|
|
Storage: s,
|
|
|
|
Data: batchDecryptionData,
|
|
|
|
}
|
2018-01-08 18:31:38 +00:00
|
|
|
resp, err = b.HandleRequest(context.Background(), batchDecryptionReq)
|
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
|
|
|
batchDecryptionResponseItems := resp.Data["batch_results"].([]DecryptBatchResponseItem)
|
2017-02-02 19:24:20 +00:00
|
|
|
|
|
|
|
plaintext := "dGhlIHF1aWNrIGJyb3duIGZveA=="
|
2017-02-06 19:56:16 +00:00
|
|
|
for _, item := range batchDecryptionResponseItems {
|
2017-02-02 19:24:20 +00:00
|
|
|
if item.Plaintext != plaintext {
|
|
|
|
t.Fatalf("bad: plaintext. Expected: %q, Actual: %q", plaintext, item.Plaintext)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|