Skip to content
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

Support setting kv-v2 version for VaultStaticSecret #200

Merged
merged 3 commits into from
May 23, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Features:
Improvements:
* VaultDynamicSecrets (VDS): Generate new credentials if lease renewal TTL is truncated [GH-170](https://github.com/hashicorp/vault-secrets-operator/pull/170)
* VaultDynamicSecrets (VDS): Replace `Spec.Role` with `Spec.Path` (**breaking change**) [GH-172](https://github.com/hashicorp/vault-secrets-operator/pull/172)
* VaultStaticSecrets (VSS): Add `Spec.Version` field to support fetching a specific kv-v2 secret version [GH-200](https://github.com/hashicorp/vault-secrets-operator/pull/200)

## 0.1.0-beta (March 29th, 2023)

Expand Down
4 changes: 4 additions & 0 deletions api/v1alpha1/vaultstaticsecret_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ type VaultStaticSecretSpec struct {
Mount string `json:"mount"`
// Name of the secret in Vault
Name string `json:"name"`
// Version of the secret to fetch. Only valid for type kv-v2. Corresponds to version query parameter:
// https://developer.hashicorp.com/vault/api-docs/secret/kv/kv-v2#version
// +kubebuilder:validation:Minimum=0
Version int `json:"version,omitempty"`
// Type of the Vault static secret
// +kubebuilder:validation:Enum={kv-v1,kv-v2}
Type string `json:"type"`
Expand Down
5 changes: 5 additions & 0 deletions chart/crds/secrets.hashicorp.com_vaultstaticsecrets.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,11 @@ spec:
specified the Operator will default to the `default` VaultAuth,
configured in its own Kubernetes namespace.
type: string
version:
description: 'Version of the secret to fetch. Only valid for type
kv-v2. Corresponds to version query parameter: https://developer.hashicorp.com/vault/api-docs/secret/kv/kv-v2#version'
minimum: 0
type: integer
required:
- destination
- mount
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,11 @@ spec:
specified the Operator will default to the `default` VaultAuth,
configured in its own Kubernetes namespace.
type: string
version:
description: 'Version of the secret to fetch. Only valid for type
kv-v2. Corresponds to version query parameter: https://developer.hashicorp.com/vault/api-docs/secret/kv/kv-v2#version'
minimum: 0
type: integer
required:
- destination
- mount
Expand Down
6 changes: 5 additions & 1 deletion controllers/vaultstaticsecret_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,11 @@ func (r *VaultStaticSecretReconciler) Reconcile(ctx context.Context, req ctrl.Re
if err != nil {
return ctrl.Result{}, err
}
resp, err = w.Get(ctx, o.Spec.Name)
if o.Spec.Version == 0 {
resp, err = w.Get(ctx, o.Spec.Name)
} else {
resp, err = w.GetVersion(ctx, o.Spec.Name, o.Spec.Version)
}
default:
err = fmt.Errorf("unsupported secret type %q", o.Spec.Type)
logger.Error(err, "")
Expand Down
16 changes: 15 additions & 1 deletion test/integration/vaultstaticsecret_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,7 @@ func TestVaultStaticSecret_kv(t *testing.T) {
expectedExisting []expectedData
create int
createTypes []string
version int
}{
{
name: "existing",
Expand All @@ -300,6 +301,12 @@ func TestVaultStaticSecret_kv(t *testing.T) {
create: 2,
createTypes: []string{consts.KVSecretTypeV2},
},
{
name: "create-kv-v2-fixed-version",
create: 2,
createTypes: []string{consts.KVSecretTypeV2},
version: 1,
},
{
name: "create-both",
create: 2,
Expand Down Expand Up @@ -355,7 +362,11 @@ func TestVaultStaticSecret_kv(t *testing.T) {
} else {
putKV(t, obj, expected.update)

data = expected.update
if obj.Spec.Version == 1 {
data = expected.initial
} else {
data = expected.update
}
}

secret, err := waitForSecretData(t, ctx, crdClient, 30, 1*time.Second, obj.Spec.Destination.Name,
Expand Down Expand Up @@ -442,6 +453,9 @@ func TestVaultStaticSecret_kv(t *testing.T) {
HMACSecretData: true,
},
}
if tt.version != 0 {
vssObj.Spec.Version = tt.version
}

if !skipCleanup {
t.Cleanup(func() {
Expand Down