Skip to content

Commit d7280ba

Browse files
committed
Ensure that spec.hmacSecretData's value honoured
Previously, upon the first application of a VaultStaticSecret instance that had spec.hmacSecretData explicitly set to false, the K8s API would replace that value with default as is defined in the CRD's schema. This fix makes HMACSecretData a pointer receiver, which make K8s do the right thing.
1 parent 17f8448 commit d7280ba

File tree

4 files changed

+27
-6
lines changed

4 files changed

+27
-6
lines changed

api/v1beta1/vaultstaticsecret_types.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ type VaultStaticSecretSpec struct {
4242
// and during incoming Vault secret comparison.
4343
// Enabling this feature is recommended to ensure that Secret's data stays consistent with Vault.
4444
// +kubebuilder:default=true
45-
HMACSecretData bool `json:"hmacSecretData,omitempty"`
45+
HMACSecretData *bool `json:"hmacSecretData,omitempty"`
4646
// RolloutRestartTargets should be configured whenever the application(s) consuming the Vault secret does
4747
// not support dynamically reloading a rotated secret.
4848
// In that case one, or more RolloutRestartTarget(s) can be configured here. The Operator will

api/v1beta1/zz_generated.deepcopy.go

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

controllers/vaultstaticsecret_controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ func (r *VaultStaticSecretReconciler) Reconcile(ctx context.Context, req ctrl.Re
127127

128128
var doRolloutRestart bool
129129
doSync := true
130-
if o.Spec.HMACSecretData {
130+
if o.Spec.HMACSecretData != nil && *o.Spec.HMACSecretData {
131131
// we want to ensure that requeueAfter is set so that we can perform the proper drift detection during each reconciliation.
132132
// setting up a watcher on the Secret is also possibility, but polling seems to be the simplest approach for now.
133133
if requeueAfter == 0 {

test/integration/vaultstaticsecret_integration_test.go

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717

1818
"github.com/cenkalti/backoff/v4"
1919
"github.com/gruntwork-io/terratest/modules/terraform"
20+
"github.com/hashicorp/vault/sdk/helper/pointerutil"
2021
"github.com/stretchr/testify/assert"
2122
"github.com/stretchr/testify/require"
2223
corev1 "k8s.io/api/core/v1"
@@ -215,7 +216,7 @@ func TestVaultStaticSecret(t *testing.T) {
215216
Name: "secretkv",
216217
Create: false,
217218
},
218-
HMACSecretData: true,
219+
HMACSecretData: pointerutil.BoolPtr(true),
219220
RefreshAfter: "5s",
220221
RolloutRestartTargets: []secretsv1beta1.RolloutRestartTarget{
221222
{
@@ -243,7 +244,7 @@ func TestVaultStaticSecret(t *testing.T) {
243244
Create: false,
244245
},
245246
RefreshAfter: "5s",
246-
HMACSecretData: false,
247+
HMACSecretData: pointerutil.BoolPtr(false),
247248
},
248249
},
249250
}
@@ -343,8 +344,23 @@ func TestVaultStaticSecret(t *testing.T) {
343344
assertSync := func(t *testing.T, obj *secretsv1beta1.VaultStaticSecret, expected expectedData, expectInitial bool) {
344345
var data map[string]interface{}
345346
if expectInitial {
347+
require.Empty(t, obj.UID,
348+
"obj %s has UID %s, expected empty", obj.Name, obj.UID)
349+
var expectSpecHMACData *bool
350+
if obj.Spec.HMACSecretData == nil {
351+
// default value as defined in the CRD schema
352+
expectSpecHMACData = pointerutil.BoolPtr(true)
353+
} else if *obj.Spec.HMACSecretData {
354+
// explicitly set to true
355+
expectSpecHMACData = pointerutil.BoolPtr(true)
356+
} else {
357+
// explicitly set to false
358+
expectSpecHMACData = pointerutil.BoolPtr(false)
359+
}
346360
putKV(t, obj, expected.initial)
347361
require.NoError(t, crdClient.Create(ctx, obj))
362+
require.Equal(t, obj.Spec.HMACSecretData, expectSpecHMACData,
363+
"expected initial value for spec.hmacSecretData to be honoured after apply")
348364
data = expected.initial
349365
} else {
350366
putKV(t, obj, expected.update)
@@ -360,7 +376,7 @@ func TestVaultStaticSecret(t *testing.T) {
360376
obj.ObjectMeta.Namespace, data)
361377
if assert.NoError(t, err) {
362378
assertSyncableSecret(t, crdClient, obj, secret)
363-
if obj.Spec.HMACSecretData {
379+
if obj.Spec.HMACSecretData != nil && *obj.Spec.HMACSecretData {
364380
assertHMAC(t, ctx, crdClient, obj, expectInitial)
365381
} else {
366382
assertNoHMAC(t, obj)
@@ -444,7 +460,7 @@ func TestVaultStaticSecret(t *testing.T) {
444460
Create: true,
445461
},
446462
RefreshAfter: "5s",
447-
HMACSecretData: true,
463+
HMACSecretData: pointerutil.BoolPtr(true),
448464
},
449465
}
450466
if tt.version != 0 {

0 commit comments

Comments
 (0)