Skip to content

Commit 964a4de

Browse files
committed
Fixing remote bases decryption skip.
Signed-off-by: Yuriy <[email protected]>
1 parent aefd7aa commit 964a4de

File tree

7 files changed

+56
-254
lines changed

7 files changed

+56
-254
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ endif
2424
export PATH:=$(GOBIN):${PATH}
2525

2626
# Allows for defining additional Go test args, e.g. '-tags integration'.
27-
GO_TEST_ARGS ?=
27+
GO_TEST_ARGS ?= -run ^TestKustomizationReconciler_Decryptor$
2828

2929
# Allows for defining additional Docker buildx arguments, e.g. '--push'.
3030
BUILD_ARGS ?= --load

internal/controller/kustomization_decryptor_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ func TestKustomizationReconciler_Decryptor(t *testing.T) {
151151
"sops-envs-secret",
152152
"sops-files-secret",
153153
"sops-inside-secret",
154+
"sops-remote-secret",
154155
}
155156
for _, name := range secretNames {
156157
var secret corev1.Secret
@@ -161,6 +162,7 @@ func TestKustomizationReconciler_Decryptor(t *testing.T) {
161162
configMapNames := []string{
162163
"sops-envs-configmap",
163164
"sops-files-configmap",
165+
"sops-remote-configmap",
164166
}
165167
for _, name := range configMapNames {
166168
var configMap corev1.ConfigMap

internal/controller/testdata/sops/build.yaml

Lines changed: 0 additions & 249 deletions
This file was deleted.

internal/controller/testdata/sops/kustomization.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@ resources:
77
- files
88
- patches
99
- inside
10+
- remote
1011
components:
1112
- ./component
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
key=ENC[AES256_GCM,data:3PTvx6o=,iv:74ni7B2QMB6aygdd3R7IEzNCwo1W+TpPWMJLfYCCG4U=,tag:mK2Tu7JWDdEmZUrXz3uRzw==,type:str]
2+
sops_age__list_0__map_enc=-----BEGIN AGE ENCRYPTED FILE-----\nYWdlLWVuY3J5cHRpb24ub3JnL3YxCi0+IFgyNTUxOSA5aDhVTW1IenNXQmptWnha\nMjd1UWN3dHp0QXRkSnhUSjBHVFdKSmdXYzNNClVWeXVGWndJQ1RpRUlJRy9yeHJY\nb1VhbnR2TlovSUg1MlpZdkhWdkVHTG8KLS0tIHVOSEhOVVV2cXRUQUs2Sk15eU1a\nRW92L1BWQnhNbStFekZjVVRDUFJtaWsK+wPkQAtZtTbh2WHik1ovX61ZJPpkmwuO\nnUYAn37tZELXX/alrOORRwoq+0oBQO5pZYsJBi0fvijfm9VqR/4jKg==\n-----END AGE ENCRYPTED FILE-----\n
3+
sops_age__list_0__map_recipient=age1l44xcng8dqj32nlv6d930qvvrny05hglzcv9qpc7kxjc6902ma4qufys29
4+
sops_lastmodified=2024-11-12T13:33:42Z
5+
sops_mac=ENC[AES256_GCM,data:YQHMLRk85ozeuqIvNekLAVp2DFSj+VgDG2z70uQaeCA+uxFp3k/THlANAXx+GP1Oab923Q6nG5ItV9dcG1hTXpA/NRpbM02pfNe/iYnVL7AtcXqFg/jy2T4kkqx7cHAXJi9zd+ZrISIZCNWinLoFfaAo70+epsFumUmLUaDzUPQ=,iv:TdOIRoy6Wch1/x9GlEsmArA5g461ILJZUE7tIxi9G28=,tag:miip/H0SuHqvaoxGvzheIg==,type:str]
6+
sops_unencrypted_suffix=_unencrypted
7+
sops_version=3.9.0
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
apiVersion: kustomize.config.k8s.io/v1beta1
2+
kind: Kustomization
3+
namePrefix: remote-
4+
resources:
5+
- https://raw.githubusercontent.com/fluxcd/kustomize-controller/refs/heads/main/config/default/namespace.yaml
6+
generatorOptions:
7+
disableNameSuffixHash: true
8+
secretGenerator:
9+
- name: secret
10+
envs:
11+
- env.env
12+
patches:
13+
- patch: |-
14+
apiVersion: v1
15+
kind: ConfigMap
16+
metadata:
17+
name: sops-remote-configmap
18+
data:
19+
key: value
20+
target:
21+
kind: Namespace
22+
options:
23+
allowNameChange: true
24+
allowKindChange: true

internal/decryptor/decryptor.go

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -426,14 +426,20 @@ func (d *Decryptor) decryptKustomizationSources(visited map[string]struct{}) vis
426426
if _, ok := visited[absRef]; ok {
427427
return nil
428428
}
429-
fi, err := os.Lstat(absRef)
429+
exists, err := checkPathExists(absRef)
430430
if err != nil {
431-
return securePathErr(root, err)
431+
return err
432432
}
433-
if !ignoreNotRegular || fi.Mode().IsRegular() {
434-
if err := d.sopsDecryptFile(absRef, format, format); err != nil {
433+
if !ignoreNotRegular || exists {
434+
fi, err := os.Lstat(absRef)
435+
if err != nil {
435436
return securePathErr(root, err)
436437
}
438+
if !ignoreNotRegular || fi.Mode().IsRegular() {
439+
if err := d.sopsDecryptFile(absRef, format, format); err != nil {
440+
return securePathErr(root, err)
441+
}
442+
}
437443
}
438444
// Explicitly set _after_ the decryption operation, this makes
439445
// visited work as a list of actually decrypted files
@@ -809,6 +815,17 @@ func securePathErr(root string, err error) error {
809815
return err
810816
}
811817

818+
func checkPathExists(path string) (exists bool, err error) {
819+
exists = false
820+
if _, err = os.Stat(path); err == nil {
821+
exists = true
822+
}
823+
if os.IsNotExist(err) {
824+
err = nil
825+
}
826+
return
827+
}
828+
812829
func formatForPath(path string) formats.Format {
813830
switch {
814831
case strings.HasSuffix(path, corev1.DockerConfigJsonKey):

0 commit comments

Comments
 (0)