Skip to content

Commit bcfd424

Browse files
authored
Merge pull request #637 from fluxcd/fix-build-panics
2 parents ac4594d + 6854ff5 commit bcfd424

File tree

4 files changed

+57
-1
lines changed

4 files changed

+57
-1
lines changed

controllers/kustomization_generator.go

+10-1
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ var kustomizeBuildMutex sync.Mutex
247247
// - load files from outside the kustomization dir path
248248
// (but not outside root)
249249
// - disable plugins except for the builtin ones
250-
func secureBuildKustomization(root, dirPath string) (resmap.ResMap, error) {
250+
func secureBuildKustomization(root, dirPath string) (_ resmap.ResMap, err error) {
251251
// Create secure FS for root
252252
fs, err := securefs.MakeFsOnDiskSecureBuild(root)
253253
if err != nil {
@@ -259,6 +259,15 @@ func secureBuildKustomization(root, dirPath string) (resmap.ResMap, error) {
259259
kustomizeBuildMutex.Lock()
260260
defer kustomizeBuildMutex.Unlock()
261261

262+
// Kustomize tends to panic in unpredicted ways due to (accidental)
263+
// invalid object data; recover when this happens to ensure continuity of
264+
// operations
265+
defer func() {
266+
if r := recover(); r != nil {
267+
err = fmt.Errorf("recovered from kustomize build panic: %v", r)
268+
}
269+
}()
270+
262271
buildOptions := &krusty.Options{
263272
LoadRestrictions: kustypes.LoadRestrictionsNone,
264273
PluginConfig: kustypes.DisabledPluginConfig(),

controllers/kustomization_generator_test.go

+13
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,16 @@ func Test_secureBuildKustomization(t *testing.T) {
3030
g.Expect(err).ToNot(HaveOccurred())
3131
})
3232
}
33+
34+
func Test_secureBuildKustomization_panic(t *testing.T) {
35+
t.Run("build panic", func(t *testing.T) {
36+
g := NewWithT(t)
37+
38+
_, err := secureBuildKustomization("testdata/panic", "testdata/panic")
39+
g.Expect(err).To(HaveOccurred())
40+
g.Expect(err.Error()).To(ContainSubstring("recovered from kustomize build panic"))
41+
// Run again to ensure the lock is released
42+
_, err = secureBuildKustomization("testdata/panic", "testdata/panic")
43+
g.Expect(err).To(HaveOccurred())
44+
})
45+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
apiVersion: kustomize.config.k8s.io/v1beta1
3+
kind: Kustomization
4+
namespace: test
5+
namePrefix: test
6+
resources:
7+
- secret.age.yaml
+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
apiVersion: ENC[AES256_GCM,data:RwzrBF8wy16SpfbQoeADeKyz,iv:DuJce2Ebx1Y49DaLCOJ74OOkgiv21roxhz/sZqKCSSs=,tag:Gg9XHapZI5q+rvtgeY6nrg==,type:str]
2+
kind: ENC[AES256_GCM,data:RwzrBF8wy16SpfbQoeADeKyz,iv:DuJce2Ebx1Y49DaLCOJ74OOkgiv21roxhz/sZqKCSSs=,tag:Gg9XHapZI5q+rvtgeY6nrg==,type:str]
3+
metadata:
4+
name: ENC[AES256_GCM,data:RwzrBF8wy16SpfbQoeADeKyz,iv:DuJce2Ebx1Y49DaLCOJ74OOkgiv21roxhz/sZqKCSSs=,tag:Gg9XHapZI5q+rvtgeY6nrg==,type:str]
5+
namespace: ENC[AES256_GCM,data:RwzrBF8wy16SpfbQoeADeKyz,iv:DuJce2Ebx1Y49DaLCOJ74OOkgiv21roxhz/sZqKCSSs=,tag:Gg9XHapZI5q+rvtgeY6nrg==,type:str]
6+
stringData:
7+
secret: ENC[AES256_GCM,data:RwzrBF8wy16SpfbQoeADeKyz,iv:DuJce2Ebx1Y49DaLCOJ74OOkgiv21roxhz/sZqKCSSs=,tag:Gg9XHapZI5q+rvtgeY6nrg==,type:str]
8+
sops:
9+
kms: []
10+
gcp_kms: []
11+
azure_kv: []
12+
hc_vault: []
13+
age:
14+
- recipient: age1l44xcng8dqj32nlv6d930qvvrny05hglzcv9qpc7kxjc6902ma4qufys29
15+
enc: |
16+
-----BEGIN AGE ENCRYPTED FILE-----
17+
YWdlLWVuY3J5cHRpb24ub3JnL3YxCi0+IFgyNTUxOSBNeGduOFZjRWw2WTFQdWdu
18+
OS83OEZaN1E1aU1zSThhMlNEZzd0aEYvdURFCnE3bmJ5c3J2cDNEbXhselFPVC9v
19+
NFhMRjZjOHZOdEpoYjdiS0ZPd2pvN1kKLS0tIDZUVEFoblpDNWhnaWxYRTBjaktk
20+
bHRXV0o1K2ZDNm5Mem5SdzNBMTNuNFUKylE2cRLqydjj6e4+4Giwn4y8mIPej+CM
21+
Bab3UWiK1da2rFNTOEnoHl6QDAVxNrWdrrIa5k22SzApT88VtJ4xuQ==
22+
-----END AGE ENCRYPTED FILE-----
23+
lastmodified: "2021-04-06T09:07:05Z"
24+
mac: ENC[AES256_GCM,data:oaM8qFtEP8dOCd/Tr5yb08uetsnDtZO8o1rCayN53ncQ1HUAdhRBrFdmbYx1YTh1mwQVVN6sGYqFZU1LBMVv5pTqvpwd41biJZEg8NznXQWx0GA2Z6HOrblGhFZKrqky3P5xN+6j63zkJizXWgBMKzRvBnsVKxjZGr/lk1vVVv4=,iv:p4y9Fo3SArkEMuoK2d9sQYgNdc0iw/StFhg/5LnhcXM=,tag:61JGbnEw35tv6WnGj46JOw==,type:str]
25+
pgp: []
26+
encrypted_regex: ^(data|stringData)$
27+
version: 3.7.0

0 commit comments

Comments
 (0)