mirror of
https://github.com/optim-enterprises-bv/vault.git
synced 2025-10-29 17:52:32 +00:00
* Adding explicit MPL license for sub-package. This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository. * Adding explicit MPL license for sub-package. This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository. * Updating the license from MPL to Business Source License. Going forward, this project will be licensed under the Business Source License v1.1. Please see our blog post for more details at https://hashi.co/bsl-blog, FAQ at www.hashicorp.com/licensing-faq, and details of the license at www.hashicorp.com/bsl. * add missing license headers * Update copyright file headers to BUS-1.1 * Fix test that expected exact offset on hcl file --------- Co-authored-by: hashicorp-copywrite[bot] <110428419+hashicorp-copywrite[bot]@users.noreply.github.com> Co-authored-by: Sarah Thompson <sthompson@hashicorp.com> Co-authored-by: Brian Kassouf <bkassouf@hashicorp.com>
76 lines
2.0 KiB
Go
76 lines
2.0 KiB
Go
// Copyright (c) HashiCorp, Inc.
|
|
// SPDX-License-Identifier: BUSL-1.1
|
|
|
|
package transit
|
|
|
|
import (
|
|
"context"
|
|
"crypto/rsa"
|
|
"crypto/x509"
|
|
"encoding/pem"
|
|
"testing"
|
|
|
|
"github.com/hashicorp/vault/sdk/logical"
|
|
)
|
|
|
|
const (
|
|
storagePath = "policy/import/" + WrappingKeyName
|
|
)
|
|
|
|
func TestTransit_WrappingKey(t *testing.T) {
|
|
// Set up shared backend for subtests
|
|
b, s := createBackendWithStorage(t)
|
|
|
|
// Ensure the key does not exist before requesting it.
|
|
keyEntry, err := s.Get(context.Background(), storagePath)
|
|
if err != nil {
|
|
t.Fatalf("error retrieving wrapping key from storage: %s", err)
|
|
}
|
|
if keyEntry != nil {
|
|
t.Fatal("wrapping key unexpectedly exists")
|
|
}
|
|
|
|
// Generate the key pair by requesting the public key.
|
|
req := &logical.Request{
|
|
Storage: s,
|
|
Operation: logical.ReadOperation,
|
|
Path: "wrapping_key",
|
|
}
|
|
resp, err := b.HandleRequest(context.Background(), req)
|
|
if err != nil {
|
|
t.Fatalf("unexpected request error: %s", err)
|
|
}
|
|
if resp == nil || resp.Data == nil || resp.Data["public_key"] == nil {
|
|
t.Fatal("expected non-nil response")
|
|
}
|
|
pubKeyPEM := resp.Data["public_key"]
|
|
|
|
// Ensure the returned key is a 4096-bit RSA key.
|
|
pubKeyBlock, _ := pem.Decode([]byte(pubKeyPEM.(string)))
|
|
rawPubKey, err := x509.ParsePKIXPublicKey(pubKeyBlock.Bytes)
|
|
if err != nil {
|
|
t.Fatalf("failed to parse public wrapping key: %s", err)
|
|
}
|
|
wrappingKey, ok := rawPubKey.(*rsa.PublicKey)
|
|
if !ok || wrappingKey.Size() != 512 {
|
|
t.Fatal("public wrapping key is not a 4096-bit RSA key")
|
|
}
|
|
|
|
// Request the wrapping key again to ensure it isn't regenerated.
|
|
req = &logical.Request{
|
|
Storage: s,
|
|
Operation: logical.ReadOperation,
|
|
Path: "wrapping_key",
|
|
}
|
|
resp, err = b.HandleRequest(context.Background(), req)
|
|
if err != nil {
|
|
t.Fatalf("unexpected request error: %s", err)
|
|
}
|
|
if resp == nil || resp.Data == nil || resp.Data["public_key"] == nil {
|
|
t.Fatal("expected non-nil response")
|
|
}
|
|
if resp.Data["public_key"] != pubKeyPEM {
|
|
t.Fatal("wrapping key public component changed between requests")
|
|
}
|
|
}
|