Skip to content

Commit

Permalink
Avoid writing empty strings to Vault when creating managed keys (#1803)
Browse files Browse the repository at this point in the history
  • Loading branch information
vinay-gopalan authored Mar 21, 2023
1 parent 9089df7 commit 194f2c1
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 2 deletions.
5 changes: 5 additions & 0 deletions testutil/testutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,11 @@ func GetTestNomadCreds(t *testing.T) (string, string) {
return v[0], v[1]
}

func GetTestPKCSCreds(t *testing.T) (string, string, string) {
v := SkipTestEnvUnset(t, "PKCS_KEY_LIBRARY", "PKCS_KEY_SLOT", "PKCS_KEY_PIN")
return v[0], v[1], v[2]
}

func TestCheckResourceAttrJSON(name, key, expectedValue string) resource.TestCheckFunc {
return func(s *terraform.State) error {
resourceState, ok := s.RootModule().Resources[name]
Expand Down
10 changes: 8 additions & 2 deletions vault/resource_managed_keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,12 @@ func getManagedKeysConfigData(config map[string]interface{}, sm schemaMap) (stri

for blockKey := range sm {
if v, ok := config[blockKey]; ok {
// ensure empty strings are not written
// to vault as part of the data
if s, ok := v.(string); ok && s == "" {
continue
}

data[blockKey] = v

if blockKey == consts.FieldName {
Expand Down Expand Up @@ -605,7 +611,7 @@ func readAndSetManagedKeys(d *schema.ResourceData, client *api.Client, providerT
}

func readAWSManagedKeys(d *schema.ResourceData, client *api.Client) error {
redacted := []string{"access_key", "secret_key"}
redacted := []string{consts.FieldAccessKey, consts.FieldSecretKey}
if err := readAndSetManagedKeys(d, client, consts.FieldAWS,
map[string]string{consts.FieldUUID: "UUID"}, redacted); err != nil {
return err
Expand All @@ -625,7 +631,7 @@ func readAzureManagedKeys(d *schema.ResourceData, client *api.Client) error {
}

func readPKCSManagedKeys(d *schema.ResourceData, client *api.Client) error {
redacted := []string{"pin"}
redacted := []string{consts.FieldPin, consts.FieldKeyID}
if err := readAndSetManagedKeys(d, client, consts.FieldPKCS,
map[string]string{consts.FieldUUID: "UUID"}, redacted); err != nil {
return err
Expand Down
64 changes: 64 additions & 0 deletions vault/resource_managed_keys_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,53 @@ func TestManagedKeys(t *testing.T) {
})
}

// The following test requires a Vault server to be set up with a specific server configuration
// (kms_library needs to be defined). We need not dedicate an entire server setup just for one
// test, and hence this test is meant to be run locally
//
// The following test requires a PKCS#11 key to be set up and needs the following
// environment variables to operate successfully:
// * PKCS_KEY_LIBRARY
// * PKCS_KEY_SLOT
// * PKCS_KEY_PIN
// * TF_ACC_LOCAL=1
//
// The final variable specifies that this test can only be run locally
func TestManagedKeysPKCS(t *testing.T) {
testutil.SkipTestEnvUnset(t, "TF_ACC_LOCAL")

name := acctest.RandomWithPrefix("pkcs-keys")
resourceName := "vault_managed_keys.test"

library, slot, pin := testutil.GetTestPKCSCreds(t)

resource.Test(t, resource.TestCase{
PreCheck: func() { testutil.TestEntPreCheck(t) },
ProviderFactories: providerFactories,
Steps: []resource.TestStep{
{
Config: testManagedKeysConfig_pkcs(name, library, slot, pin),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "pkcs.#", "1"),
resource.TestCheckResourceAttr(resourceName, "pkcs.0.library", library),
resource.TestCheckResourceAttr(resourceName, "pkcs.0.key_label", "kms-intermediate"),
resource.TestCheckResourceAttr(resourceName, "pkcs.0.key_id", "kms-intermediate"),
resource.TestCheckResourceAttr(resourceName, "pkcs.0.key_bits", "4096"),
resource.TestCheckResourceAttr(resourceName, "pkcs.0.slot", slot),
resource.TestCheckResourceAttr(resourceName, "pkcs.0.pin", pin),
resource.TestCheckResourceAttr(resourceName, "pkcs.0.mechanism", "1"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"pkcs.0.pin", "pkcs.0.key_id"},
},
},
})
}

func testManagedKeysConfig_basic(name0, name1 string) string {
return fmt.Sprintf(`
resource "vault_managed_keys" "test" {
Expand Down Expand Up @@ -196,3 +243,20 @@ resource "vault_managed_keys" "test" {
}
`, name)
}

func testManagedKeysConfig_pkcs(name, library, slot, pin string) string {
return fmt.Sprintf(`
resource "vault_managed_keys" "test" {
pkcs {
name = "%s"
library = "%s"
key_label = "kms-intermediate"
key_id = "kms-intermediate"
key_bits = "4096"
slot = "%s"
pin = "%s"
mechanism = "0x0001"
}
}
`, name, library, slot, pin)
}

0 comments on commit 194f2c1

Please sign in to comment.