-
Notifications
You must be signed in to change notification settings - Fork 548
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
Add PKI EST configuration support #2246
Merged
stevendpclark
merged 11 commits into
main
from
stevendpclark/vault-25831-pki-est-support
Jun 4, 2024
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
54b6f50
Add support for configuring the PKI EST feature
stevendpclark 3cd9325
Add missing audit_fields field
stevendpclark 6c34d9f
PR feedback
stevendpclark b3b473b
Fix bug introduced in PR feedback
stevendpclark 13aff17
Make all input fields optional, only set in API call if provided
stevendpclark cb70fec
Add misssing last_updated attribute field
stevendpclark 2dc02f3
Disable testing of EST authenticator cert_role parameter
stevendpclark 1ebec24
Merge branch 'main' into stevendpclark/vault-25831-pki-est-support
stevendpclark c187909
Fix gofmt issue
stevendpclark 357514e
Add entry to changelog
stevendpclark 5f5961a
Additional PR feedback
stevendpclark 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
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,165 @@ | ||
package vault | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"strings" | ||
|
||
"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" | ||
"github.com/hashicorp/vault/api" | ||
) | ||
|
||
func pkiSecretBackendConfigEstDataSource() *schema.Resource { | ||
return &schema.Resource{ | ||
Description: "Reads Vault PKI EST configuration", | ||
ReadContext: provider.ReadContextWrapper(readPKISecretBackendConfigEst), | ||
Schema: map[string]*schema.Schema{ | ||
consts.FieldBackend: { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
Description: "Path where PKI engine is mounted", | ||
}, | ||
consts.FieldEnabled: { | ||
Type: schema.TypeBool, | ||
Computed: true, | ||
Description: "Specifies whether EST is enabled", | ||
}, | ||
consts.FieldDefaultMount: { | ||
Type: schema.TypeBool, | ||
Computed: true, | ||
Description: "If set, this mount is registered as the default `.well-known/est` URL path. Only a single mount can enable this across a Vault cluster", | ||
}, | ||
consts.FieldDefaultPathPolicy: { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: "Required to be set if default_mount is enabled. Specifies the behavior for requests using the default EST label. Can be sign-verbatim or a role given by role:<role_name>", | ||
}, | ||
consts.FieldLabelToPathPolicy: { | ||
Type: schema.TypeMap, | ||
Computed: true, | ||
Description: "A pairing of an EST label with the redirected behavior for requests hitting that role. The path policy can be sign-verbatim or a role given by role:<role_name>. Labels must be unique across Vault cluster, and will register .well-known/est/<label> URL paths", | ||
}, | ||
consts.FieldAuthenticators: { | ||
Type: schema.TypeList, | ||
Computed: true, | ||
Description: "Lists the mount accessors EST should delegate authentication requests towards", | ||
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"cert": { | ||
Type: schema.TypeMap, | ||
Optional: true, | ||
Description: "The accessor and cert_role properties for cert auth backends", | ||
}, | ||
"userpass": { | ||
Type: schema.TypeMap, | ||
Optional: true, | ||
Description: "The accessor property for user pass auth backends", | ||
}, | ||
}, | ||
}, | ||
}, | ||
consts.FieldEnableSentinelParsing: { | ||
Type: schema.TypeBool, | ||
Computed: true, | ||
Description: "If set, parse out fields from the provided CSR making them available for Sentinel policies", | ||
}, | ||
consts.FieldAuditFields: { | ||
Type: schema.TypeList, | ||
Computed: true, | ||
Description: "Fields parsed from the CSR that appear in the audit and can be used by sentinel policies", | ||
Elem: &schema.Schema{ | ||
Type: schema.TypeString, | ||
}, | ||
}, | ||
consts.FieldLastUpdated: { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: "A read-only timestamp representing the last time the configuration was updated", | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func readPKISecretBackendConfigEst(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
if err := verifyPkiEstFeatureSupported(meta); err != nil { | ||
return diag.FromErr(err) | ||
} | ||
|
||
client, err := provider.GetClient(d, meta) | ||
if err != nil { | ||
return diag.FromErr(fmt.Errorf("failed getting client: %w", err)) | ||
} | ||
|
||
backend := d.Get(consts.FieldBackend).(string) | ||
path := pkiSecretBackendConfigEstPath(backend) | ||
|
||
if err := readEstConfig(ctx, d, client, path); err != nil { | ||
return diag.FromErr(err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func readEstConfig(ctx context.Context, d *schema.ResourceData, client *api.Client, path string) error { | ||
resp, err := client.Logical().ReadWithContext(ctx, path) | ||
if err != nil { | ||
return fmt.Errorf("error reading from Vault: %w", err) | ||
} | ||
if resp == nil { | ||
return fmt.Errorf("got nil response from Vault from path: %q", path) | ||
} | ||
|
||
d.SetId(path) | ||
|
||
keyComputedFields := []string{ | ||
consts.FieldEnabled, | ||
consts.FieldDefaultMount, | ||
consts.FieldDefaultPathPolicy, | ||
consts.FieldLabelToPathPolicy, | ||
consts.FieldEnableSentinelParsing, | ||
consts.FieldAuditFields, | ||
consts.FieldLastUpdated, | ||
} | ||
|
||
for _, k := range keyComputedFields { | ||
if fieldVal, ok := resp.Data[k]; ok { | ||
if err := d.Set(k, fieldVal); err != nil { | ||
return fmt.Errorf("failed setting field [%s] with val [%s]: %w", k, fieldVal, err) | ||
} | ||
} | ||
} | ||
|
||
if authenticators, authOk := resp.Data[consts.FieldAuthenticators]; authOk { | ||
if err := d.Set(consts.FieldAuthenticators, []interface{}{authenticators}); err != nil { | ||
return fmt.Errorf("failed setting field [%s] with val [%s]: %w", consts.FieldAuthenticators, authenticators, err) | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// verifyPkiEstFeatureSupported verifies that we are talking to a Vault enterprise edition | ||
// and its version 1.16.0 or higher, returns nil if the above is met, otherwise an error | ||
func verifyPkiEstFeatureSupported(meta interface{}) error { | ||
currentVersion := meta.(*provider.ProviderMeta).GetVaultVersion() | ||
|
||
minVersion := provider.VaultVersion116 | ||
if !provider.IsAPISupported(meta, minVersion) { | ||
return fmt.Errorf("feature not enabled on current Vault version. min version required=%s; "+ | ||
"current vault version=%s", minVersion, currentVersion) | ||
} | ||
|
||
if !provider.IsEnterpriseSupported(meta) { | ||
return errors.New("feature requires Vault Enterprise") | ||
} | ||
return nil | ||
} | ||
|
||
func pkiSecretBackendConfigEstPath(backend string) string { | ||
return strings.Trim(backend, "/") + "/config/est" | ||
} |
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,52 @@ | ||
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 TestAccDataSourcePKISecretConfigEst(t *testing.T) { | ||
backend := acctest.RandomWithPrefix("tf-test-pki-backend") | ||
dataName := "data.vault_pki_secret_backend_config_est.test" | ||
resource.Test(t, resource.TestCase{ | ||
ProviderFactories: providerFactories, | ||
PreCheck: func() { | ||
testutil.TestAccPreCheck(t) | ||
testutil.TestEntPreCheck(t) | ||
SkipIfAPIVersionLT(t, testProvider.Meta(), provider.VaultVersion116) | ||
}, | ||
Steps: []resource.TestStep{ | ||
{ | ||
// Note this is more thoroughly tested within TestAccPKISecretBackendConfigEst_basic | ||
// we don't want to start having test failures if Vault changes default values. | ||
Config: testPKISecretEmptyEstConfigDataSource(backend), | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttr(dataName, consts.FieldBackend, backend), | ||
resource.TestCheckResourceAttrSet(dataName, consts.FieldEnabled), | ||
vinay-gopalan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
resource.TestCheckResourceAttrSet(dataName, consts.FieldDefaultMount), | ||
resource.TestCheckResourceAttrSet(dataName, consts.FieldEnableSentinelParsing), | ||
vinay-gopalan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
resource.TestCheckResourceAttrSet(dataName, consts.FieldLastUpdated), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testPKISecretEmptyEstConfigDataSource(path string) string { | ||
return fmt.Sprintf(` | ||
resource "vault_mount" "test" { | ||
path = "%s" | ||
type = "pki" | ||
description = "PKI secret engine mount" | ||
} | ||
|
||
data "vault_pki_secret_backend_config_est" "test" { | ||
backend = vault_mount.test.path | ||
}`, path) | ||
} |
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
Oops, something went wrong.
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.
Why is this marked as Computed? If we don't set the value in the config, does Vault return a default?
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 just realized this is a data source so please disregard. :)
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.
No worries, but that is the reason on FieldAuditFields within the resource Computed is set, we get back a default set of values from Vault if not set.