-
Notifications
You must be signed in to change notification settings - Fork 47
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
Include rotated secrets for cloud vault secrets VAULT-22307 #850
Changes from all commits
0b797ae
7aec075
2705021
f37d2de
e2ed1ce
98f0f84
8212430
ceb498d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
```release-note:feature | ||
Allows users to fetch rotating secrets using the hcp_vault_secrets_app and hcp_vault_secrets_secret data sources | ||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
package clients | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"net/http" | ||
"time" | ||
|
||
sharedmodels "github.com/hashicorp/hcp-sdk-go/clients/cloud-shared/v1/models" | ||
"github.com/hashicorp/hcp-sdk-go/clients/cloud-vault-secrets/preview/2023-11-28/client/secret_service" | ||
secretmodels "github.com/hashicorp/hcp-sdk-go/clients/cloud-vault-secrets/preview/2023-11-28/models" | ||
"github.com/hashicorp/terraform-plugin-log/tflog" | ||
) | ||
|
||
// OpenVaultSecretsAppSecret will retrieve the latest secret for a Vault Secrets app, including it's value. | ||
func OpenVaultSecretsAppSecret(ctx context.Context, client *Client, loc *sharedmodels.HashicorpCloudLocationLocation, appName, secretName string) (*secretmodels.Secrets20231128OpenSecret, error) { | ||
getParams := secret_service.NewOpenAppSecretParamsWithContext(ctx). | ||
WithAppName(appName). | ||
WithSecretName(secretName). | ||
WithOrganizationID(loc.OrganizationID). | ||
WithProjectID(loc.ProjectID) | ||
|
||
var getResp *secret_service.OpenAppSecretOK | ||
var err error | ||
for attempt := 0; attempt < retryCount; attempt++ { | ||
getResp, err = client.VaultSecretsPreview.OpenAppSecret(getParams, nil) | ||
if err != nil { | ||
var serviceErr *secret_service.OpenAppSecretDefault | ||
ok := errors.As(err, &serviceErr) | ||
if !ok { | ||
return nil, err | ||
} | ||
|
||
if shouldRetryErrorCode(serviceErr.Code(), []int{http.StatusTooManyRequests}) { | ||
backOffDuration := getAPIBackoffDuration(serviceErr.Error()) | ||
tflog.Debug(ctx, fmt.Sprintf("The api rate limit has been exceeded, retrying in %d seconds, attempt: %d", int64(backOffDuration.Seconds()), (attempt+1))) | ||
time.Sleep(backOffDuration) | ||
continue | ||
} | ||
return nil, err | ||
} | ||
break | ||
} | ||
|
||
if getResp == nil { | ||
return nil, errors.New("unable to get secret") | ||
} | ||
|
||
return getResp.GetPayload().Secret, nil | ||
} | ||
|
||
func OpenVaultSecretsAppSecrets(ctx context.Context, client *Client, loc *sharedmodels.HashicorpCloudLocationLocation, appName string) ([]*secretmodels.Secrets20231128OpenSecret, error) { | ||
params := secret_service.NewOpenAppSecretsParamsWithContext(ctx). | ||
WithAppName(appName). | ||
WithOrganizationID(loc.OrganizationID). | ||
WithProjectID(loc.ProjectID) | ||
|
||
var secrets *secret_service.OpenAppSecretsOK | ||
var err error | ||
for attempt := 0; attempt < retryCount; attempt++ { | ||
secrets, err = client.VaultSecretsPreview.OpenAppSecrets(params, nil) | ||
if err != nil { | ||
var serviceErr *secret_service.OpenAppSecretDefault | ||
ok := errors.As(err, &serviceErr) | ||
if !ok { | ||
return nil, err | ||
} | ||
if shouldRetryWithSleep(ctx, serviceErr, attempt, []int{http.StatusTooManyRequests}) { | ||
continue | ||
} | ||
return nil, err | ||
} | ||
break | ||
} | ||
|
||
if secrets == nil { | ||
return nil, errors.New("unable to get secrets") | ||
} | ||
|
||
return secrets.GetPayload().Secrets, nil | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -106,8 +106,20 @@ func (d *DataSourceVaultSecretsApp) Read(ctx context.Context, req datasource.Rea | |
|
||
openAppSecrets := map[string]string{} | ||
for _, appSecret := range appSecrets { | ||
secretName := appSecret.Name | ||
openAppSecrets[secretName] = appSecret.Version.Value | ||
switch { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would it be cleaner to switch on secret.Type instead? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. cleaner: yes was concerned about the string type, are we going to switch to enum at some point? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe 🤷 It's OK to leave as is for now |
||
case appSecret.StaticVersion != nil: | ||
openAppSecrets[appSecret.Name] = appSecret.StaticVersion.Value | ||
case appSecret.RotatingVersion != nil: | ||
for name, value := range appSecret.RotatingVersion.Values { | ||
openAppSecrets[appSecret.Name+"_"+name] = value | ||
} | ||
default: | ||
resp.Diagnostics.AddError( | ||
"Unsupported HCP Secret type", | ||
fmt.Sprintf("HCP Secrets secret type %q is not currently supported by terraform-provider-hcp", appSecret.Type), | ||
) | ||
return | ||
} | ||
} | ||
|
||
data.ID = data.AppName | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ package vaultsecrets | |
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
|
||
sharedmodels "github.com/hashicorp/hcp-sdk-go/clients/cloud-shared/v1/models" | ||
|
@@ -31,11 +32,11 @@ func NewVaultSecretsSecretDataSource() datasource.DataSource { | |
return &DataSourceVaultSecretsSecret{} | ||
} | ||
|
||
func (d *DataSourceVaultSecretsSecret) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { | ||
func (d *DataSourceVaultSecretsSecret) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { | ||
resp.TypeName = req.ProviderTypeName + "_vault_secrets_secret" | ||
} | ||
|
||
func (d *DataSourceVaultSecretsSecret) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) { | ||
func (d *DataSourceVaultSecretsSecret) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) { | ||
resp.Schema = schema.Schema{ | ||
MarkdownDescription: "The Vault Secrets secret data source retrieves a singular secret and its latest version.", | ||
Attributes: map[string]schema.Attribute{ | ||
|
@@ -107,7 +108,31 @@ func (d *DataSourceVaultSecretsSecret) Read(ctx context.Context, req datasource. | |
resp.Diagnostics.AddError(err.Error(), "Unable to open secret") | ||
return | ||
} | ||
secretValue := openSecret.Version.Value | ||
|
||
// NOTE: for backwards compatibility purposes, if the secret is not a static secret (a string) | ||
// encode the complex secret as a JSON string | ||
var secretValue string | ||
switch { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above, switching on secret.Type might be cleaner |
||
case openSecret.StaticVersion != nil: | ||
secretValue = openSecret.StaticVersion.Value | ||
case openSecret.RotatingVersion != nil: | ||
secretData, err := json.Marshal(openSecret.RotatingVersion.Values) | ||
if err != nil { | ||
resp.Diagnostics.AddError(err.Error(), "could not encode rotating secret as json") | ||
return | ||
} | ||
resp.Diagnostics.AddWarning( | ||
"HCP Vault Secrets mismatched type", | ||
"Attempted to get a rotating secret in a KV secret data source, encoding the secret values as JSON", | ||
) | ||
Comment on lines
+124
to
+127
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm a bit unsure about this warning. We may want to keep this data source as an equivalent of vault_generic_secret data source. In this case, it should be perfectly valid to use it to fetch a rotating secret. I think it's OK to leave it here as well. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. lets keep, was in the original discussion when we planned this out |
||
secretValue = string(secretData) | ||
default: | ||
resp.Diagnostics.AddError( | ||
"Unsupported HCP Secret type", | ||
fmt.Sprintf("HCP Secrets secret type %q is not currently supported by terraform-provider-hcp", openSecret.Type), | ||
) | ||
return | ||
} | ||
|
||
data.ID = data.AppName | ||
data.SecretValue = types.StringValue(secretValue) | ||
|
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.
Nice improvement!