-
Notifications
You must be signed in to change notification settings - Fork 552
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: data_source vault_transit_secret_backend_key #2327
Open
simonostendorf
wants to merge
3
commits into
hashicorp:main
Choose a base branch
from
simonostendorf:feat/data_source/vault_transit_secret_backend_key
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
2173beb
feat: data_source vault_transit_secret_backend_key
simonostendorf 8e7ea19
Merge branch 'main' into feat/data_source/vault_transit_secret_backen…
simonostendorf a014361
Merge branch 'main' into feat/data_source/vault_transit_secret_backen…
simonostendorf File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,176 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
package vault | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
|
||
"github.com/hashicorp/terraform-provider-vault/internal/consts" | ||
"github.com/hashicorp/terraform-provider-vault/internal/provider" | ||
) | ||
|
||
func transitSecretBackendKeyDataSource() *schema.Resource { | ||
return &schema.Resource{ | ||
ReadContext: provider.ReadContextWrapper(readTransitSecretBackendKey), | ||
Schema: map[string]*schema.Schema{ | ||
consts.FieldBackend: { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
Description: "Full path where transit backend is mounted.", | ||
}, | ||
consts.FieldName: { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
Description: "Name of the key.", | ||
}, | ||
"type": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: "Specifies the type of the key.", | ||
}, | ||
"deletion_allowed": { | ||
Type: schema.TypeBool, | ||
Computed: true, | ||
Description: "Specifies if the key is allowed to be deleted.", | ||
}, | ||
"derived": { | ||
Type: schema.TypeBool, | ||
Computed: true, | ||
Description: "Specifies if key derivation is used.", | ||
}, | ||
"exportable": { | ||
Type: schema.TypeBool, | ||
Computed: true, | ||
Description: "Speficies if keys is exportable.", | ||
}, | ||
"allow_plaintext_backup": { | ||
Type: schema.TypeBool, | ||
Computed: true, | ||
Description: "Specifies if taking backup of named key in the plaintext format is enabled.", | ||
}, | ||
"keys": { | ||
Type: schema.TypeList, | ||
Computed: true, | ||
Description: "List of key versions in the keyring with the corresponding creation time, name and public key.", | ||
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"creation_time": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"name": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"public_key": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"certificate_chain": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
}, | ||
}, | ||
}, | ||
"min_decryption_version": { | ||
Type: schema.TypeInt, | ||
Computed: true, | ||
Description: "Minimum key version to use for decryption.", | ||
}, | ||
"min_encryption_version": { | ||
Type: schema.TypeInt, | ||
Computed: true, | ||
Description: "Minimum key version to use for encryption.", | ||
}, | ||
"supports_encryption": { | ||
Type: schema.TypeBool, | ||
Computed: true, | ||
Description: "Whether or not the key supports encryption, based on key type.", | ||
}, | ||
"supports_decryption": { | ||
Type: schema.TypeBool, | ||
Computed: true, | ||
Description: "Whether or not the key supports decryption, based on key type.", | ||
}, | ||
"supports_derivation": { | ||
Type: schema.TypeBool, | ||
Computed: true, | ||
Description: "Whether or not the key supports derivation, based on key type.", | ||
}, | ||
"supports_signing": { | ||
Type: schema.TypeBool, | ||
Computed: true, | ||
Description: "Whether or not the key supports signing, based on key type.", | ||
}, | ||
"imported": { | ||
Type: schema.TypeBool, | ||
Computed: true, | ||
Description: "Specifies if the key is imported.", | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func readTransitSecretBackendKey(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
client, err := provider.GetClient(d, meta) | ||
if err != nil { | ||
return diag.FromErr(err) | ||
} | ||
|
||
backend := d.Get(consts.FieldBackend).(string) | ||
keyName := d.Get(consts.FieldName).(string) | ||
path := fmt.Sprintf("%s/keys/%s", backend, keyName) | ||
|
||
resp, err := client.Logical().ReadWithContext(ctx, path) | ||
if err != nil { | ||
return diag.FromErr(fmt.Errorf("error reading from Vault: %s", err)) | ||
} | ||
log.Printf("[DEBUG] Read %q from Vault", path) | ||
if resp == nil { | ||
return diag.FromErr(fmt.Errorf("no key found at %q", path)) | ||
} | ||
|
||
d.SetId(path) | ||
|
||
keyComputedFields := []string{ | ||
"type", | ||
"deletion_allowed", | ||
"derived", | ||
"exportable", | ||
"allow_plaintext_backup", | ||
"min_decryption_version", | ||
"min_encryption_version", | ||
"supports_encryption", | ||
"supports_decryption", | ||
"supports_derivation", | ||
"supports_signing", | ||
"imported", | ||
} | ||
|
||
for _, k := range keyComputedFields { | ||
if err := d.Set(k, resp.Data[k]); err != nil { | ||
return diag.FromErr(err) | ||
} | ||
} | ||
|
||
var keys []interface{} | ||
for _, keyData := range resp.Data["keys"].(map[string]interface{}) { | ||
keyMap := keyData.(map[string]interface{}) | ||
keys = append(keys, keyMap) | ||
} | ||
|
||
if err := d.Set("keys", keys); err != nil { | ||
return diag.FromErr(err) | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
package vault | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
|
||
"github.com/hashicorp/terraform-provider-vault/internal/consts" | ||
"github.com/hashicorp/terraform-provider-vault/internal/provider" | ||
"github.com/hashicorp/terraform-provider-vault/testutil" | ||
) | ||
|
||
func TestAccDataSourceTransitSecretKey(t *testing.T) { | ||
backend := acctest.RandomWithPrefix("tf-test-transit-backend") | ||
keyName := acctest.RandomWithPrefix("tf-test-transit-key") | ||
dataName := "data.vault_transit_secret_backend_key.test" | ||
resource.Test(t, resource.TestCase{ | ||
ProviderFactories: providerFactories, | ||
PreCheck: func() { | ||
testutil.TestAccPreCheck(t) | ||
SkipIfAPIVersionLT(t, testProvider.Meta(), provider.VaultVersion111) | ||
}, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testTransitSecretKeyDataSource(backend, keyName), | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttr(dataName, consts.FieldBackend, backend), | ||
resource.TestCheckResourceAttr(dataName, consts.FieldName, keyName), | ||
resource.TestCheckResourceAttr(dataName, "type", "rsa-4096"), | ||
resource.TestCheckResourceAttr(dataName, "deletion_allowed", "true"), | ||
resource.TestCheckResourceAttr(dataName, "exportable", "true"), | ||
resource.TestCheckResourceAttr(dataName, "keys.0.name", "rsa-4096"), | ||
resource.TestCheckResourceAttr(dataName, "keys.0.certificate_chain", ""), | ||
resource.TestCheckResourceAttrSet(dataName, "keys.0.creation_time"), | ||
resource.TestCheckResourceAttrSet(dataName, "keys.0.public_key"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testTransitSecretKeyDataSource(path, keyName string) string { | ||
return fmt.Sprintf(` | ||
resource "vault_mount" "test" { | ||
path = "%s" | ||
type = "transit" | ||
description = "Transit engine mount" | ||
} | ||
|
||
resource "vault_transit_secret_backend_key" "test" { | ||
backend = vault_mount.test.path | ||
name = "%s" | ||
type = "rsa-4096" | ||
deletion_allowed = true | ||
exportable = true | ||
} | ||
|
||
data "vault_transit_secret_backend_key" "test" { | ||
backend = vault_mount.test.path | ||
name = vault_transit_secret_backend_key.test.name | ||
}`, path, keyName) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are a few keys missing from this
There are also a few optional keys depending on options set in the response returned see formatKeyPolicy
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry I used an old vault release without some of these keys and after testing it with the new version I forgot to add them.