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"
|
2021-11-15 21:53:22 +00:00
|
|
|
"net/http"
|
|
|
|
"reflect"
|
2020-09-22 13:43:07 +00:00
|
|
|
"testing"
|
2021-04-08 16:43:39 +00:00
|
|
|
|
2021-11-15 21:53:22 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/helper/jsonutil"
|
2021-04-08 16:43:39 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/logical"
|
2021-11-15 21:53:22 +00:00
|
|
|
"github.com/mitchellh/mapstructure"
|
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{}{
|
2022-12-13 18:03:40 +00:00
|
|
|
map[string]interface{}{"plaintext": "", "reference": "foo"}, // empty string
|
|
|
|
map[string]interface{}{"plaintext": "Cg==", "reference": "bar"}, // newline
|
|
|
|
map[string]interface{}{"plaintext": "dGhlIHF1aWNrIGJyb3duIGZveA==", "reference": "baz"},
|
2017-02-06 19:56:16 +00:00
|
|
|
}
|
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 {
|
2022-12-13 18:03:40 +00:00
|
|
|
batchDecryptionInput[i] = map[string]interface{}{"ciphertext": item.Ciphertext, "reference": item.Reference}
|
2017-02-06 19:56:16 +00:00
|
|
|
}
|
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)
|
2022-12-13 18:03:40 +00:00
|
|
|
// This seems fragile
|
|
|
|
expectedResult := "[{\"plaintext\":\"\",\"reference\":\"foo\"},{\"plaintext\":\"Cg==\",\"reference\":\"bar\"},{\"plaintext\":\"dGhlIHF1aWNrIGJyb3duIGZveA==\",\"reference\":\"baz\"}]"
|
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) {
|
2021-11-15 21:53:22 +00:00
|
|
|
var req *logical.Request
|
2017-02-02 19:24:20 +00:00
|
|
|
var resp *logical.Response
|
|
|
|
var err error
|
|
|
|
|
|
|
|
b, s := createBackendWithStorage(t)
|
|
|
|
|
2021-11-15 21:53:22 +00:00
|
|
|
// Create a derived key.
|
|
|
|
req = &logical.Request{
|
2017-02-02 19:24:20 +00:00
|
|
|
Operation: logical.UpdateOperation,
|
|
|
|
Path: "keys/existing_key",
|
|
|
|
Storage: s,
|
2021-11-15 21:53:22 +00:00
|
|
|
Data: map[string]interface{}{
|
|
|
|
"derived": true,
|
|
|
|
},
|
2017-02-02 19:24:20 +00:00
|
|
|
}
|
2021-11-15 21:53:22 +00:00
|
|
|
resp, err = b.HandleRequest(context.Background(), req)
|
2017-02-02 19:24:20 +00:00
|
|
|
if err != nil || (resp != nil && resp.IsError()) {
|
|
|
|
t.Fatalf("err:%v resp:%#v", err, resp)
|
|
|
|
}
|
|
|
|
|
2021-11-15 21:53:22 +00:00
|
|
|
// Encrypt some values for use in test cases.
|
|
|
|
plaintextItems := []struct {
|
|
|
|
plaintext, context string
|
|
|
|
}{
|
|
|
|
{plaintext: "dGhlIHF1aWNrIGJyb3duIGZveA==", context: "dGVzdGNvbnRleHQ="},
|
|
|
|
{plaintext: "anVtcGVkIG92ZXIgdGhlIGxhenkgZG9n", context: "dGVzdGNvbnRleHQy"},
|
2017-02-06 19:56:16 +00:00
|
|
|
}
|
2021-11-15 21:53:22 +00:00
|
|
|
req = &logical.Request{
|
2017-02-02 19:24:20 +00:00
|
|
|
Operation: logical.UpdateOperation,
|
|
|
|
Path: "encrypt/existing_key",
|
|
|
|
Storage: s,
|
2021-11-15 21:53:22 +00:00
|
|
|
Data: map[string]interface{}{
|
|
|
|
"batch_input": []interface{}{
|
|
|
|
map[string]interface{}{"plaintext": plaintextItems[0].plaintext, "context": plaintextItems[0].context},
|
|
|
|
map[string]interface{}{"plaintext": plaintextItems[1].plaintext, "context": plaintextItems[1].context},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
resp, err = b.HandleRequest(context.Background(), req)
|
2017-02-02 19:24:20 +00:00
|
|
|
if err != nil || (resp != nil && resp.IsError()) {
|
|
|
|
t.Fatalf("err:%v resp:%#v", err, resp)
|
|
|
|
}
|
|
|
|
|
2021-11-15 21:53:22 +00:00
|
|
|
encryptedItems := resp.Data["batch_results"].([]EncryptBatchResponseItem)
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
in []interface{}
|
|
|
|
want []DecryptBatchResponseItem
|
|
|
|
shouldErr bool
|
|
|
|
wantHTTPStatus int
|
2022-09-13 17:51:09 +00:00
|
|
|
params map[string]interface{}
|
2021-11-15 21:53:22 +00:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "nil-input",
|
|
|
|
in: nil,
|
|
|
|
shouldErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "empty-input",
|
|
|
|
in: []interface{}{},
|
|
|
|
shouldErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "single-item-success",
|
|
|
|
in: []interface{}{
|
|
|
|
map[string]interface{}{"ciphertext": encryptedItems[0].Ciphertext, "context": plaintextItems[0].context},
|
|
|
|
},
|
|
|
|
want: []DecryptBatchResponseItem{
|
|
|
|
{Plaintext: plaintextItems[0].plaintext},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "single-item-invalid-ciphertext",
|
|
|
|
in: []interface{}{
|
|
|
|
map[string]interface{}{"ciphertext": "xxx", "context": plaintextItems[0].context},
|
|
|
|
},
|
|
|
|
want: []DecryptBatchResponseItem{
|
|
|
|
{Error: "invalid ciphertext: no prefix"},
|
|
|
|
},
|
|
|
|
wantHTTPStatus: http.StatusBadRequest,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "single-item-wrong-context",
|
|
|
|
in: []interface{}{
|
|
|
|
map[string]interface{}{"ciphertext": encryptedItems[0].Ciphertext, "context": plaintextItems[1].context},
|
|
|
|
},
|
|
|
|
want: []DecryptBatchResponseItem{
|
|
|
|
{Error: "cipher: message authentication failed"},
|
|
|
|
},
|
|
|
|
wantHTTPStatus: http.StatusBadRequest,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "batch-full-success",
|
|
|
|
in: []interface{}{
|
|
|
|
map[string]interface{}{"ciphertext": encryptedItems[0].Ciphertext, "context": plaintextItems[0].context},
|
|
|
|
map[string]interface{}{"ciphertext": encryptedItems[1].Ciphertext, "context": plaintextItems[1].context},
|
|
|
|
},
|
|
|
|
want: []DecryptBatchResponseItem{
|
|
|
|
{Plaintext: plaintextItems[0].plaintext},
|
|
|
|
{Plaintext: plaintextItems[1].plaintext},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "batch-partial-success",
|
|
|
|
in: []interface{}{
|
|
|
|
map[string]interface{}{"ciphertext": encryptedItems[0].Ciphertext, "context": plaintextItems[1].context},
|
|
|
|
map[string]interface{}{"ciphertext": encryptedItems[1].Ciphertext, "context": plaintextItems[1].context},
|
|
|
|
},
|
|
|
|
want: []DecryptBatchResponseItem{
|
|
|
|
{Error: "cipher: message authentication failed"},
|
|
|
|
{Plaintext: plaintextItems[1].plaintext},
|
|
|
|
},
|
|
|
|
wantHTTPStatus: http.StatusBadRequest,
|
|
|
|
},
|
2022-09-13 17:51:09 +00:00
|
|
|
{
|
|
|
|
name: "batch-partial-success-overridden-response",
|
|
|
|
in: []interface{}{
|
|
|
|
map[string]interface{}{"ciphertext": encryptedItems[0].Ciphertext, "context": plaintextItems[1].context},
|
|
|
|
map[string]interface{}{"ciphertext": encryptedItems[1].Ciphertext, "context": plaintextItems[1].context},
|
|
|
|
},
|
|
|
|
want: []DecryptBatchResponseItem{
|
|
|
|
{Error: "cipher: message authentication failed"},
|
|
|
|
{Plaintext: plaintextItems[1].plaintext},
|
|
|
|
},
|
|
|
|
params: map[string]interface{}{"partial_failure_response_code": http.StatusAccepted},
|
|
|
|
wantHTTPStatus: http.StatusAccepted,
|
|
|
|
},
|
2021-11-15 21:53:22 +00:00
|
|
|
{
|
|
|
|
name: "batch-full-failure",
|
|
|
|
in: []interface{}{
|
|
|
|
map[string]interface{}{"ciphertext": encryptedItems[0].Ciphertext, "context": plaintextItems[1].context},
|
|
|
|
map[string]interface{}{"ciphertext": encryptedItems[1].Ciphertext, "context": plaintextItems[0].context},
|
|
|
|
},
|
|
|
|
want: []DecryptBatchResponseItem{
|
|
|
|
{Error: "cipher: message authentication failed"},
|
|
|
|
{Error: "cipher: message authentication failed"},
|
|
|
|
},
|
|
|
|
wantHTTPStatus: http.StatusBadRequest,
|
|
|
|
},
|
2022-09-13 17:51:09 +00:00
|
|
|
{
|
|
|
|
name: "batch-full-failure-overridden-response",
|
|
|
|
in: []interface{}{
|
|
|
|
map[string]interface{}{"ciphertext": encryptedItems[0].Ciphertext, "context": plaintextItems[1].context},
|
|
|
|
map[string]interface{}{"ciphertext": encryptedItems[1].Ciphertext, "context": plaintextItems[0].context},
|
|
|
|
},
|
|
|
|
want: []DecryptBatchResponseItem{
|
|
|
|
{Error: "cipher: message authentication failed"},
|
|
|
|
{Error: "cipher: message authentication failed"},
|
|
|
|
},
|
|
|
|
params: map[string]interface{}{"partial_failure_response_code": http.StatusAccepted},
|
|
|
|
// Full failure, shouldn't affect status code
|
|
|
|
wantHTTPStatus: http.StatusBadRequest,
|
|
|
|
},
|
2021-11-15 21:53:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
req = &logical.Request{
|
|
|
|
Operation: logical.UpdateOperation,
|
|
|
|
Path: "decrypt/existing_key",
|
|
|
|
Storage: s,
|
|
|
|
Data: map[string]interface{}{
|
|
|
|
"batch_input": tt.in,
|
|
|
|
},
|
|
|
|
}
|
2022-09-13 17:51:09 +00:00
|
|
|
for k, v := range tt.params {
|
|
|
|
req.Data[k] = v
|
|
|
|
}
|
2021-11-15 21:53:22 +00:00
|
|
|
resp, err = b.HandleRequest(context.Background(), req)
|
|
|
|
|
|
|
|
didErr := err != nil || (resp != nil && resp.IsError())
|
|
|
|
if didErr {
|
|
|
|
if !tt.shouldErr {
|
|
|
|
t.Fatalf("unexpected error err:%v, resp:%#v", err, resp)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if tt.shouldErr {
|
|
|
|
t.Fatal("expected error, but none occurred")
|
|
|
|
}
|
|
|
|
|
|
|
|
if rawRespBody, ok := resp.Data[logical.HTTPRawBody]; ok {
|
|
|
|
httpResp := &logical.HTTPResponse{}
|
|
|
|
err = jsonutil.DecodeJSON([]byte(rawRespBody.(string)), httpResp)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to unmarshal nested response: err:%v, resp:%#v", err, resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
if respStatus, ok := resp.Data[logical.HTTPStatusCode]; !ok || respStatus != tt.wantHTTPStatus {
|
|
|
|
t.Fatalf("HTTP response status code mismatch, want:%d, got:%d", tt.wantHTTPStatus, respStatus)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp = logical.HTTPResponseToLogicalResponse(httpResp)
|
|
|
|
}
|
|
|
|
|
|
|
|
var respItems []DecryptBatchResponseItem
|
|
|
|
err = mapstructure.Decode(resp.Data["batch_results"], &respItems)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("problem decoding response items: err:%v, resp:%#v", err, resp)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(tt.want, respItems) {
|
|
|
|
t.Fatalf("response items mismatch, want:%#v, got:%#v", tt.want, respItems)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2017-02-02 19:24:20 +00:00
|
|
|
}
|
|
|
|
}
|