Skip to content

Commit e3195b4

Browse files
committed
Add annotation to disable decryption on a per object basis
Signed-off-by: danhubern <[email protected]>
1 parent a8c7cc1 commit e3195b4

File tree

4 files changed

+33
-2
lines changed

4 files changed

+33
-2
lines changed

docs/spec/v1/kustomizations.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1264,6 +1264,18 @@ data:
12641264
sops.vault-token: <BASE64>
12651265
```
12661266

1267+
#### Controlling the decryption behavior of resources
1268+
1269+
To change the decryption behaviour for specific Kubernetes resources, you can annotate them with:
1270+
1271+
| Annotation | Default | Values | Role |
1272+
|-------------------------------------|------------|----------------------------------------------------------------|-----------------|
1273+
| `kustomize.toolkit.fluxcd.io/decrypt` | `Enabled` | - `Enabled`<br/>- `Disabled` | Decryption policy |
1274+
1275+
##### Disabled
1276+
1277+
The `Disabled` policy instructs the controller to not decrypt Kubernetes resources. This might be useful if there is another entity that is going to decrpyt the resource later.
1278+
12671279
## Working with Kustomizations
12681280

12691281
### Recommended settings

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: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,12 @@ 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+
return annotations != nil &&
193+
strings.EqualFold(annotations[fmt.Sprintf("%s/decrypt", kustomizev1.GroupVersion.Group)], kustomizev1.DisabledValue)
194+
}
195+
190196
// IsEncryptedSecret checks if the given object is a Kubernetes Secret encrypted
191197
// with Mozilla SOPS.
192198
func IsEncryptedSecret(object *unstructured.Unstructured) bool {
@@ -436,7 +442,10 @@ func (d *Decryptor) SopsDecryptWithFormat(data []byte, inputFormat, outputFormat
436442
// while decrypting with DecryptionProviderSOPS, to allow individual data entries
437443
// injected by e.g. a Kustomize secret generator to be decrypted
438444
func (d *Decryptor) DecryptResource(res *resource.Resource) (*resource.Resource, error) {
439-
if res == nil || d.kustomization.Spec.Decryption == nil || d.kustomization.Spec.Decryption.Provider == "" {
445+
if res == nil ||
446+
d.kustomization.Spec.Decryption == nil ||
447+
d.kustomization.Spec.Decryption.Provider == "" ||
448+
IsDecryptionDisabled(res.GetAnnotations()) {
440449
return nil, nil
441450
}
442451

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)