Skip to content

Commit b32007d

Browse files
committed
Add support to not decrypt encrypted objects
1 parent a8c7cc1 commit b32007d

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

internal/controller/kustomization_controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -890,7 +890,7 @@ func (r *KustomizationReconciler) apply(ctx context.Context,
890890
}
891891

892892
for _, u := range objects {
893-
if decryptor.IsEncryptedSecret(u) {
893+
if decryptor.IsEncryptedSecret(u) && !decryptor.IsDecryptionDisabled(u.GetAnnotations()) {
894894
return false, nil,
895895
fmt.Errorf("%s is SOPS encrypted, configuring decryption is required for this secret to be reconciled",
896896
ssautil.FmtUnstructured(u))

internal/decryptor/decryptor.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,14 @@ func New(client client.Client, kustomization *kustomizev1.Kustomization, opts ..
187187
return d, cleanup, nil
188188
}
189189

190+
// IsDecryptionDisabled checks if the given object has the decrypt: disabled annotation set
191+
func IsDecryptionDisabled(annotations map[string]string) bool {
192+
if annotations != nil && annotations[fmt.Sprintf("%s/decrypt", kustomizev1.GroupVersion.Group)] == kustomizev1.DisabledValue {
193+
return true
194+
}
195+
return false
196+
}
197+
190198
// IsEncryptedSecret checks if the given object is a Kubernetes Secret encrypted
191199
// with Mozilla SOPS.
192200
func IsEncryptedSecret(object *unstructured.Unstructured) bool {
@@ -436,7 +444,10 @@ func (d *Decryptor) SopsDecryptWithFormat(data []byte, inputFormat, outputFormat
436444
// while decrypting with DecryptionProviderSOPS, to allow individual data entries
437445
// injected by e.g. a Kustomize secret generator to be decrypted
438446
func (d *Decryptor) DecryptResource(res *resource.Resource) (*resource.Resource, error) {
439-
if res == nil || d.kustomization.Spec.Decryption == nil || d.kustomization.Spec.Decryption.Provider == "" {
447+
if res == nil ||
448+
d.kustomization.Spec.Decryption == nil ||
449+
d.kustomization.Spec.Decryption.Provider == "" ||
450+
IsDecryptionDisabled(res.GetAnnotations()) {
440451
return nil, nil
441452
}
442453

internal/decryptor/decryptor_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -632,8 +632,18 @@ func TestDecryptor_DecryptResource(t *testing.T) {
632632
g.Expect(secret.UnmarshalJSON(encData)).To(Succeed())
633633
g.Expect(isSOPSEncryptedResource(secret)).To(BeTrue())
634634

635+
secret.SetAnnotations(map[string]string{
636+
"kustomize.toolkit.fluxcd.io/decrypt": "disabled",
637+
})
638+
635639
got, err := d.DecryptResource(secret)
636640
g.Expect(err).ToNot(HaveOccurred())
641+
g.Expect(got).To(BeNil())
642+
643+
secret.SetAnnotations(map[string]string{})
644+
645+
got, err = d.DecryptResource(secret)
646+
g.Expect(err).ToNot(HaveOccurred())
637647
g.Expect(got).ToNot(BeNil())
638648
g.Expect(got.MarshalJSON()).To(Equal(secretData))
639649
})

0 commit comments

Comments
 (0)