2017-06-06 20:02:54 +00:00
|
|
|
package transit
|
|
|
|
|
|
|
|
import (
|
2018-01-08 18:31:38 +00:00
|
|
|
"context"
|
2022-01-20 15:10:15 +00:00
|
|
|
"encoding/hex"
|
2022-02-15 20:05:58 +00:00
|
|
|
"encoding/json"
|
2022-01-20 15:10:15 +00:00
|
|
|
"fmt"
|
2017-06-06 20:02:54 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
2022-01-20 15:10:15 +00:00
|
|
|
"time"
|
2017-06-06 20:02:54 +00:00
|
|
|
|
2022-01-20 15:10:15 +00:00
|
|
|
uuid "github.com/hashicorp/go-uuid"
|
|
|
|
"github.com/hashicorp/vault/api"
|
|
|
|
vaulthttp "github.com/hashicorp/vault/http"
|
2019-04-12 21:54:35 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/logical"
|
2022-01-20 15:10:15 +00:00
|
|
|
"github.com/hashicorp/vault/vault"
|
2017-06-06 20:02:54 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestTransit_ConfigSettings(t *testing.T) {
|
2018-06-12 16:24:12 +00:00
|
|
|
b, storage := createBackendWithSysView(t)
|
2017-06-06 20:02:54 +00:00
|
|
|
|
|
|
|
doReq := func(req *logical.Request) *logical.Response {
|
2018-01-08 18:31:38 +00:00
|
|
|
resp, err := b.HandleRequest(context.Background(), req)
|
2018-10-17 16:05:05 +00:00
|
|
|
if err != nil || (resp != nil && resp.IsError()) {
|
2017-06-06 20:02:54 +00:00
|
|
|
t.Fatalf("got err:\n%#v\nreq:\n%#v\n", err, *req)
|
|
|
|
}
|
|
|
|
return resp
|
|
|
|
}
|
|
|
|
doErrReq := func(req *logical.Request) {
|
2018-01-08 18:31:38 +00:00
|
|
|
resp, err := b.HandleRequest(context.Background(), req)
|
2017-06-06 20:02:54 +00:00
|
|
|
if err == nil {
|
|
|
|
if resp == nil || !resp.IsError() {
|
|
|
|
t.Fatalf("expected error; req:\n%#v\n", *req)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// First create a key
|
|
|
|
req := &logical.Request{
|
|
|
|
Storage: storage,
|
|
|
|
Operation: logical.UpdateOperation,
|
2019-10-03 20:11:43 +00:00
|
|
|
Path: "keys/aes256",
|
2017-06-06 20:02:54 +00:00
|
|
|
Data: map[string]interface{}{
|
|
|
|
"derived": true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
doReq(req)
|
|
|
|
|
2019-10-03 20:11:43 +00:00
|
|
|
req.Path = "keys/aes128"
|
|
|
|
req.Data["type"] = "aes128-gcm96"
|
|
|
|
doReq(req)
|
|
|
|
|
2017-06-06 20:02:54 +00:00
|
|
|
req.Path = "keys/ed"
|
|
|
|
req.Data["type"] = "ed25519"
|
|
|
|
doReq(req)
|
|
|
|
|
|
|
|
delete(req.Data, "derived")
|
|
|
|
|
|
|
|
req.Path = "keys/p256"
|
|
|
|
req.Data["type"] = "ecdsa-p256"
|
|
|
|
doReq(req)
|
|
|
|
|
2019-10-03 16:32:43 +00:00
|
|
|
req.Path = "keys/p384"
|
|
|
|
req.Data["type"] = "ecdsa-p384"
|
|
|
|
doReq(req)
|
|
|
|
|
|
|
|
req.Path = "keys/p521"
|
|
|
|
req.Data["type"] = "ecdsa-p521"
|
|
|
|
doReq(req)
|
|
|
|
|
2017-06-06 20:02:54 +00:00
|
|
|
delete(req.Data, "type")
|
|
|
|
|
2019-10-03 20:11:43 +00:00
|
|
|
req.Path = "keys/aes128/rotate"
|
|
|
|
doReq(req)
|
|
|
|
doReq(req)
|
|
|
|
doReq(req)
|
|
|
|
doReq(req)
|
|
|
|
|
|
|
|
req.Path = "keys/aes256/rotate"
|
2017-06-06 20:02:54 +00:00
|
|
|
doReq(req)
|
|
|
|
doReq(req)
|
|
|
|
doReq(req)
|
|
|
|
doReq(req)
|
|
|
|
|
|
|
|
req.Path = "keys/ed/rotate"
|
|
|
|
doReq(req)
|
|
|
|
doReq(req)
|
|
|
|
doReq(req)
|
|
|
|
doReq(req)
|
|
|
|
|
|
|
|
req.Path = "keys/p256/rotate"
|
|
|
|
doReq(req)
|
|
|
|
doReq(req)
|
|
|
|
doReq(req)
|
|
|
|
doReq(req)
|
|
|
|
|
2019-10-03 16:32:43 +00:00
|
|
|
req.Path = "keys/p384/rotate"
|
|
|
|
doReq(req)
|
|
|
|
doReq(req)
|
|
|
|
doReq(req)
|
|
|
|
doReq(req)
|
|
|
|
|
|
|
|
req.Path = "keys/p521/rotate"
|
|
|
|
doReq(req)
|
|
|
|
doReq(req)
|
|
|
|
doReq(req)
|
|
|
|
doReq(req)
|
|
|
|
|
2019-10-03 20:11:43 +00:00
|
|
|
req.Path = "keys/aes256/config"
|
2017-06-06 20:02:54 +00:00
|
|
|
// Too high
|
|
|
|
req.Data["min_decryption_version"] = 7
|
|
|
|
doErrReq(req)
|
|
|
|
// Too low
|
|
|
|
req.Data["min_decryption_version"] = -1
|
|
|
|
doErrReq(req)
|
|
|
|
|
|
|
|
delete(req.Data, "min_decryption_version")
|
|
|
|
// Too high
|
|
|
|
req.Data["min_encryption_version"] = 7
|
|
|
|
doErrReq(req)
|
|
|
|
// Too low
|
|
|
|
req.Data["min_encryption_version"] = 7
|
|
|
|
doErrReq(req)
|
|
|
|
|
|
|
|
// Not allowed, cannot decrypt
|
|
|
|
req.Data["min_decryption_version"] = 3
|
|
|
|
req.Data["min_encryption_version"] = 2
|
|
|
|
doErrReq(req)
|
|
|
|
|
|
|
|
// Allowed
|
|
|
|
req.Data["min_decryption_version"] = 2
|
|
|
|
req.Data["min_encryption_version"] = 3
|
|
|
|
doReq(req)
|
2019-10-03 20:11:43 +00:00
|
|
|
req.Path = "keys/aes128/config"
|
|
|
|
doReq(req)
|
2017-06-06 20:02:54 +00:00
|
|
|
req.Path = "keys/ed/config"
|
|
|
|
doReq(req)
|
|
|
|
req.Path = "keys/p256/config"
|
|
|
|
doReq(req)
|
2019-10-03 16:32:43 +00:00
|
|
|
req.Path = "keys/p384/config"
|
|
|
|
doReq(req)
|
|
|
|
|
|
|
|
req.Path = "keys/p521/config"
|
|
|
|
doReq(req)
|
2017-06-06 20:02:54 +00:00
|
|
|
|
|
|
|
req.Data = map[string]interface{}{
|
|
|
|
"plaintext": "abcd",
|
2019-03-04 20:26:20 +00:00
|
|
|
"input": "abcd",
|
2017-06-06 20:02:54 +00:00
|
|
|
"context": "abcd",
|
|
|
|
}
|
|
|
|
|
|
|
|
maxKeyVersion := 5
|
2019-10-03 20:11:43 +00:00
|
|
|
key := "aes256"
|
2017-06-06 20:02:54 +00:00
|
|
|
|
|
|
|
testHMAC := func(ver int, valid bool) {
|
|
|
|
req.Path = "hmac/" + key
|
|
|
|
delete(req.Data, "hmac")
|
|
|
|
if ver == maxKeyVersion {
|
|
|
|
delete(req.Data, "key_version")
|
|
|
|
} else {
|
|
|
|
req.Data["key_version"] = ver
|
|
|
|
}
|
|
|
|
|
|
|
|
if !valid {
|
|
|
|
doErrReq(req)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
resp := doReq(req)
|
|
|
|
ct := resp.Data["hmac"].(string)
|
|
|
|
if strings.Split(ct, ":")[1] != "v"+strconv.Itoa(ver) {
|
|
|
|
t.Fatal("wrong hmac version")
|
|
|
|
}
|
|
|
|
|
|
|
|
req.Path = "verify/" + key
|
|
|
|
delete(req.Data, "key_version")
|
|
|
|
req.Data["hmac"] = resp.Data["hmac"]
|
|
|
|
doReq(req)
|
|
|
|
}
|
|
|
|
|
|
|
|
testEncryptDecrypt := func(ver int, valid bool) {
|
|
|
|
req.Path = "encrypt/" + key
|
|
|
|
delete(req.Data, "ciphertext")
|
|
|
|
if ver == maxKeyVersion {
|
|
|
|
delete(req.Data, "key_version")
|
|
|
|
} else {
|
|
|
|
req.Data["key_version"] = ver
|
|
|
|
}
|
|
|
|
|
|
|
|
if !valid {
|
|
|
|
doErrReq(req)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
resp := doReq(req)
|
|
|
|
ct := resp.Data["ciphertext"].(string)
|
|
|
|
if strings.Split(ct, ":")[1] != "v"+strconv.Itoa(ver) {
|
|
|
|
t.Fatal("wrong encryption version")
|
|
|
|
}
|
|
|
|
|
|
|
|
req.Path = "decrypt/" + key
|
|
|
|
delete(req.Data, "key_version")
|
|
|
|
req.Data["ciphertext"] = resp.Data["ciphertext"]
|
|
|
|
doReq(req)
|
|
|
|
}
|
|
|
|
testEncryptDecrypt(5, true)
|
|
|
|
testEncryptDecrypt(4, true)
|
|
|
|
testEncryptDecrypt(3, true)
|
|
|
|
testEncryptDecrypt(2, false)
|
|
|
|
testHMAC(5, true)
|
|
|
|
testHMAC(4, true)
|
|
|
|
testHMAC(3, true)
|
|
|
|
testHMAC(2, false)
|
|
|
|
|
2019-10-03 20:11:43 +00:00
|
|
|
key = "aes128"
|
|
|
|
testEncryptDecrypt(5, true)
|
|
|
|
testEncryptDecrypt(4, true)
|
|
|
|
testEncryptDecrypt(3, true)
|
|
|
|
testEncryptDecrypt(2, false)
|
|
|
|
testHMAC(5, true)
|
|
|
|
testHMAC(4, true)
|
|
|
|
testHMAC(3, true)
|
|
|
|
testHMAC(2, false)
|
|
|
|
|
2017-06-06 20:02:54 +00:00
|
|
|
delete(req.Data, "plaintext")
|
|
|
|
req.Data["input"] = "abcd"
|
|
|
|
key = "ed"
|
|
|
|
testSignVerify := func(ver int, valid bool) {
|
|
|
|
req.Path = "sign/" + key
|
|
|
|
delete(req.Data, "signature")
|
|
|
|
if ver == maxKeyVersion {
|
|
|
|
delete(req.Data, "key_version")
|
|
|
|
} else {
|
|
|
|
req.Data["key_version"] = ver
|
|
|
|
}
|
|
|
|
|
|
|
|
if !valid {
|
|
|
|
doErrReq(req)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
resp := doReq(req)
|
|
|
|
ct := resp.Data["signature"].(string)
|
|
|
|
if strings.Split(ct, ":")[1] != "v"+strconv.Itoa(ver) {
|
|
|
|
t.Fatal("wrong signature version")
|
|
|
|
}
|
|
|
|
|
|
|
|
req.Path = "verify/" + key
|
|
|
|
delete(req.Data, "key_version")
|
|
|
|
req.Data["signature"] = resp.Data["signature"]
|
|
|
|
doReq(req)
|
|
|
|
}
|
|
|
|
testSignVerify(5, true)
|
|
|
|
testSignVerify(4, true)
|
|
|
|
testSignVerify(3, true)
|
|
|
|
testSignVerify(2, false)
|
|
|
|
testHMAC(5, true)
|
|
|
|
testHMAC(4, true)
|
|
|
|
testHMAC(3, true)
|
|
|
|
testHMAC(2, false)
|
|
|
|
|
|
|
|
delete(req.Data, "context")
|
|
|
|
key = "p256"
|
|
|
|
testSignVerify(5, true)
|
|
|
|
testSignVerify(4, true)
|
|
|
|
testSignVerify(3, true)
|
|
|
|
testSignVerify(2, false)
|
|
|
|
testHMAC(5, true)
|
|
|
|
testHMAC(4, true)
|
|
|
|
testHMAC(3, true)
|
|
|
|
testHMAC(2, false)
|
2019-10-03 16:32:43 +00:00
|
|
|
|
|
|
|
key = "p384"
|
|
|
|
testSignVerify(5, true)
|
|
|
|
testSignVerify(4, true)
|
|
|
|
testSignVerify(3, true)
|
|
|
|
testSignVerify(2, false)
|
|
|
|
testHMAC(5, true)
|
|
|
|
testHMAC(4, true)
|
|
|
|
testHMAC(3, true)
|
|
|
|
testHMAC(2, false)
|
|
|
|
|
|
|
|
key = "p521"
|
|
|
|
testSignVerify(5, true)
|
|
|
|
testSignVerify(4, true)
|
|
|
|
testSignVerify(3, true)
|
|
|
|
testSignVerify(2, false)
|
|
|
|
testHMAC(5, true)
|
|
|
|
testHMAC(4, true)
|
|
|
|
testHMAC(3, true)
|
|
|
|
testHMAC(2, false)
|
2017-06-06 20:02:54 +00:00
|
|
|
}
|
2022-01-20 15:10:15 +00:00
|
|
|
|
|
|
|
func TestTransit_UpdateKeyConfigWithAutorotation(t *testing.T) {
|
|
|
|
tests := map[string]struct {
|
2022-02-17 20:17:59 +00:00
|
|
|
initialAutoRotatePeriod interface{}
|
|
|
|
newAutoRotatePeriod interface{}
|
|
|
|
shouldError bool
|
|
|
|
expectedValue time.Duration
|
2022-01-20 15:10:15 +00:00
|
|
|
}{
|
|
|
|
"default (no value)": {
|
2022-02-17 20:17:59 +00:00
|
|
|
initialAutoRotatePeriod: "5h",
|
|
|
|
shouldError: false,
|
|
|
|
expectedValue: 5 * time.Hour,
|
2022-01-20 15:10:15 +00:00
|
|
|
},
|
|
|
|
"0 (int)": {
|
2022-02-17 20:17:59 +00:00
|
|
|
initialAutoRotatePeriod: "5h",
|
|
|
|
newAutoRotatePeriod: 0,
|
|
|
|
shouldError: false,
|
|
|
|
expectedValue: 0,
|
2022-01-20 15:10:15 +00:00
|
|
|
},
|
|
|
|
"0 (string)": {
|
2022-02-17 20:17:59 +00:00
|
|
|
initialAutoRotatePeriod: "5h",
|
|
|
|
newAutoRotatePeriod: 0,
|
|
|
|
shouldError: false,
|
|
|
|
expectedValue: 0,
|
2022-01-20 15:10:15 +00:00
|
|
|
},
|
|
|
|
"5 seconds": {
|
2022-02-17 20:17:59 +00:00
|
|
|
newAutoRotatePeriod: "5s",
|
|
|
|
shouldError: true,
|
2022-01-20 15:10:15 +00:00
|
|
|
},
|
|
|
|
"5 hours": {
|
2022-02-17 20:17:59 +00:00
|
|
|
newAutoRotatePeriod: "5h",
|
|
|
|
shouldError: false,
|
|
|
|
expectedValue: 5 * time.Hour,
|
2022-01-20 15:10:15 +00:00
|
|
|
},
|
|
|
|
"negative value": {
|
2022-02-17 20:17:59 +00:00
|
|
|
newAutoRotatePeriod: "-1800s",
|
|
|
|
shouldError: true,
|
2022-01-20 15:10:15 +00:00
|
|
|
},
|
|
|
|
"invalid string": {
|
2022-02-17 20:17:59 +00:00
|
|
|
newAutoRotatePeriod: "this shouldn't work",
|
|
|
|
shouldError: true,
|
2022-01-20 15:10:15 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
coreConfig := &vault.CoreConfig{
|
|
|
|
LogicalBackends: map[string]logical.Factory{
|
|
|
|
"transit": Factory,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
cluster := vault.NewTestCluster(t, coreConfig, &vault.TestClusterOptions{
|
|
|
|
HandlerFunc: vaulthttp.Handler,
|
|
|
|
})
|
|
|
|
cluster.Start()
|
|
|
|
defer cluster.Cleanup()
|
|
|
|
cores := cluster.Cores
|
|
|
|
vault.TestWaitActive(t, cores[0].Core)
|
|
|
|
client := cores[0].Client
|
|
|
|
err := client.Sys().Mount("transit", &api.MountInput{
|
|
|
|
Type: "transit",
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, test := range tests {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
keyNameBytes, err := uuid.GenerateRandomBytes(16)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
keyName := hex.EncodeToString(keyNameBytes)
|
|
|
|
|
|
|
|
_, err = client.Logical().Write(fmt.Sprintf("transit/keys/%s", keyName), map[string]interface{}{
|
2022-02-17 20:17:59 +00:00
|
|
|
"auto_rotate_period": test.initialAutoRotatePeriod,
|
2022-01-20 15:10:15 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
resp, err := client.Logical().Write(fmt.Sprintf("transit/keys/%s/config", keyName), map[string]interface{}{
|
2022-02-17 20:17:59 +00:00
|
|
|
"auto_rotate_period": test.newAutoRotatePeriod,
|
2022-01-20 15:10:15 +00:00
|
|
|
})
|
|
|
|
switch {
|
|
|
|
case test.shouldError && err == nil:
|
|
|
|
t.Fatal("expected non-nil error")
|
|
|
|
case !test.shouldError && err != nil:
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !test.shouldError {
|
|
|
|
resp, err = client.Logical().Read(fmt.Sprintf("transit/keys/%s", keyName))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if resp == nil {
|
|
|
|
t.Fatal("expected non-nil response")
|
|
|
|
}
|
2022-02-17 20:17:59 +00:00
|
|
|
gotRaw, ok := resp.Data["auto_rotate_period"].(json.Number)
|
2022-02-15 20:05:58 +00:00
|
|
|
if !ok {
|
|
|
|
t.Fatal("returned value is of unexpected type")
|
|
|
|
}
|
|
|
|
got, err := gotRaw.Int64()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
want := int64(test.expectedValue.Seconds())
|
2022-01-20 15:10:15 +00:00
|
|
|
if got != want {
|
2022-02-17 20:17:59 +00:00
|
|
|
t.Fatalf("incorrect auto_rotate_period returned, got: %d, want: %d", got, want)
|
2022-01-20 15:10:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|