Skip to content

Commit 6a3c585

Browse files
authored
Merge pull request #211 from fluxcd/safe-rel-path
2 parents 8296b8e + d7a0dea commit 6a3c585

File tree

9 files changed

+52
-27
lines changed

9 files changed

+52
-27
lines changed

api/v1beta1/kustomization_types.go

+6-5
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ type KustomizationSpec struct {
4545
// +optional
4646
Decryption *Decryption `json:"decryption,omitempty"`
4747

48-
// The interval at which to reconcile the kustomization.
48+
// The interval at which to reconcile the Kustomization.
4949
// +required
5050
Interval metav1.Duration `json:"interval"`
5151

@@ -54,10 +54,11 @@ type KustomizationSpec struct {
5454
// +optional
5555
KubeConfig *KubeConfig `json:"kubeConfig,omitempty"`
5656

57-
// Path to the directory containing the kustomization file.
58-
// +kubebuilder:validation:Pattern="^\\./"
59-
// +required
60-
Path string `json:"path"`
57+
// Path to the directory containing the kustomization.yaml file, or the
58+
// set of plain YAMLs a kustomization.yaml should be generated for.
59+
// Defaults to 'None', which translates to the root path of the SourceRef.
60+
// +optional
61+
Path string `json:"path,omitempty"`
6162

6263
// Prune enables garbage collection.
6364
// +required

config/crd/bases/kustomize.toolkit.fluxcd.io_kustomizations.yaml

+5-4
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ spec:
135135
type: object
136136
type: array
137137
interval:
138-
description: The interval at which to reconcile the kustomization.
138+
description: The interval at which to reconcile the Kustomization.
139139
type: string
140140
kubeConfig:
141141
description: The KubeConfig for reconciling the Kustomization on a
@@ -159,8 +159,10 @@ spec:
159159
type: object
160160
type: object
161161
path:
162-
description: Path to the directory containing the kustomization file.
163-
pattern: ^\./
162+
description: Path to the directory containing the kustomization.yaml
163+
file, or the set of plain YAMLs a kustomization.yaml should be generated
164+
for. Defaults to 'None', which translates to the root path of the
165+
SourceRef.
164166
type: string
165167
prune:
166168
description: Prune enables garbage collection.
@@ -219,7 +221,6 @@ spec:
219221
type: string
220222
required:
221223
- interval
222-
- path
223224
- prune
224225
- sourceRef
225226
type: object

controllers/kustomization_controller.go

+17-6
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ import (
2424
"net/http"
2525
"os"
2626
"os/exec"
27-
"path"
2827
"path/filepath"
2928
"strings"
3029
"time"
3130

31+
securejoin "github.com/cyphar/filepath-securejoin"
3232
"github.com/go-logr/logr"
3333
corev1 "k8s.io/api/core/v1"
3434
apierrors "k8s.io/apimachinery/pkg/api/errors"
@@ -306,8 +306,16 @@ func (r *KustomizationReconciler) reconcile(
306306
), err
307307
}
308308

309-
dirPath := path.Join(tmpDir, kustomization.Spec.Path)
310309
// check build path exists
310+
dirPath, err := securejoin.SecureJoin(tmpDir, kustomization.Spec.Path)
311+
if err != nil {
312+
return kustomizev1.KustomizationNotReady(
313+
kustomization,
314+
source.GetArtifact().Revision,
315+
kustomizev1.ArtifactFailedReason,
316+
err.Error(),
317+
), err
318+
}
311319
if _, err := os.Stat(dirPath); err != nil {
312320
err = fmt.Errorf("kustomization path not found: %w", err)
313321
return kustomizev1.KustomizationNotReady(
@@ -606,12 +614,15 @@ func (r *KustomizationReconciler) writeKubeConfig(kustomization kustomizev1.Kust
606614
return "", err
607615
}
608616

609-
kubeConfigPath := path.Join(dirPath, secretName.Name)
610-
if err := ioutil.WriteFile(kubeConfigPath, kubeConfig, os.ModePerm); err != nil {
617+
f, err := ioutil.TempFile(dirPath, "kubeconfig")
618+
defer f.Close()
619+
if err != nil {
611620
return "", fmt.Errorf("unable to write KubeConfig secret '%s' to storage: %w", secretName.String(), err)
612621
}
613-
614-
return secretName.Name, nil
622+
if _, err := f.Write(kubeConfig); err != nil {
623+
return "", fmt.Errorf("unable to write KubeConfig secret '%s' to storage: %w", secretName.String(), err)
624+
}
625+
return f.Name(), nil
615626
}
616627

617628
func (r *KustomizationReconciler) getKubeConfig(kustomization kustomizev1.Kustomization) ([]byte, error) {

controllers/kustomization_decryptor.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ import (
2323
"io/ioutil"
2424
"os"
2525
"os/exec"
26-
"path"
2726

27+
securejoin "github.com/cyphar/filepath-securejoin"
2828
"go.mozilla.org/sops/v3/aes"
2929
"go.mozilla.org/sops/v3/cmd/sops/common"
3030
"go.mozilla.org/sops/v3/cmd/sops/formats"
@@ -133,7 +133,10 @@ func (kd *KustomizeDecryptor) ImportKeys(ctx context.Context) error {
133133
defer os.RemoveAll(tmpDir)
134134

135135
for name, key := range secret.Data {
136-
keyPath := path.Join(tmpDir, name)
136+
keyPath, err := securejoin.SecureJoin(tmpDir, name)
137+
if err != nil {
138+
return err
139+
}
137140
if err := ioutil.WriteFile(keyPath, key, os.ModePerm); err != nil {
138141
return fmt.Errorf("unable to write key to storage: %w", err)
139142
}

docs/api/kustomize.md

+10-4
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ Kubernetes meta/v1.Duration
108108
</em>
109109
</td>
110110
<td>
111-
<p>The interval at which to reconcile the kustomization.</p>
111+
<p>The interval at which to reconcile the Kustomization.</p>
112112
</td>
113113
</tr>
114114
<tr>
@@ -134,7 +134,10 @@ string
134134
</em>
135135
</td>
136136
<td>
137-
<p>Path to the directory containing the kustomization file.</p>
137+
<em>(Optional)</em>
138+
<p>Path to the directory containing the kustomization.yaml file, or the
139+
set of plain YAMLs a kustomization.yaml should be generated for.
140+
Defaults to &lsquo;None&rsquo;, which translates to the root path of the SourceRef.</p>
138141
</td>
139142
</tr>
140143
<tr>
@@ -609,7 +612,7 @@ Kubernetes meta/v1.Duration
609612
</em>
610613
</td>
611614
<td>
612-
<p>The interval at which to reconcile the kustomization.</p>
615+
<p>The interval at which to reconcile the Kustomization.</p>
613616
</td>
614617
</tr>
615618
<tr>
@@ -635,7 +638,10 @@ string
635638
</em>
636639
</td>
637640
<td>
638-
<p>Path to the directory containing the kustomization file.</p>
641+
<em>(Optional)</em>
642+
<p>Path to the directory containing the kustomization.yaml file, or the
643+
set of plain YAMLs a kustomization.yaml should be generated for.
644+
Defaults to &lsquo;None&rsquo;, which translates to the root path of the SourceRef.</p>
639645
</td>
640646
</tr>
641647
<tr>

docs/spec/v1alpha1/kustomization.md

-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ type KustomizationSpec struct {
3030
KubeConfig *KubeConfig `json:"kubeConfig,omitempty"`
3131

3232
// Path to the directory containing the kustomization file.
33-
// +kubebuilder:validation:Pattern="^\\./"
3433
// +required
3534
Path string `json:"path"`
3635

docs/spec/v1beta1/kustomization.md

+6-5
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ type KustomizationSpec struct {
2121
// +optional
2222
Decryption *Decryption `json:"decryption,omitempty"`
2323

24-
// The interval at which to apply the kustomization.
24+
// The interval at which to reconcile the Kustomization.
2525
// +required
2626
Interval metav1.Duration `json:"interval"`
2727

@@ -30,10 +30,11 @@ type KustomizationSpec struct {
3030
// +optional
3131
KubeConfig *KubeConfig `json:"kubeConfig,omitempty"`
3232

33-
// Path to the directory containing the kustomization.yaml file.
34-
// +kubebuilder:validation:Pattern="^\\./"
35-
// +required
36-
Path string `json:"path"`
33+
// Path to the directory containing the kustomization.yaml file, or the
34+
// set of plain YAMLs a kustomization.yaml should be generated for.
35+
// Defaults to 'None', which translates to the root path of the SourceRef.
36+
// +optional
37+
Path string `json:"path,omitempty"`
3738

3839
// Enables garbage collection.
3940
// +required

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ go 1.15
55
replace github.com/fluxcd/kustomize-controller/api => ./api
66

77
require (
8+
github.com/cyphar/filepath-securejoin v0.2.2
89
github.com/fluxcd/kustomize-controller/api v0.5.1
910
github.com/fluxcd/pkg/apis/meta v0.5.0
1011
github.com/fluxcd/pkg/runtime v0.4.0

go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,8 @@ github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f/go.mod h1:E3G3o1h8I7cfc
120120
github.com/cpuguy83/go-md2man v1.0.10/go.mod h1:SmD6nW6nTyfqj6ABTjUi3V3JVMnlJmwcJI5acqYI6dE=
121121
github.com/cpuguy83/go-md2man/v2 v2.0.0/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
122122
github.com/creack/pty v1.1.7/go.mod h1:lj5s0c3V2DBrqTV7llrYr5NG6My20zk30Fl46Y7DoTY=
123+
github.com/cyphar/filepath-securejoin v0.2.2 h1:jCwT2GTP+PY5nBz3c/YL5PAIbusElVrPujOBSCj8xRg=
124+
github.com/cyphar/filepath-securejoin v0.2.2/go.mod h1:FpkQEhXnPnOthhzymB7CGsFk2G9VLXONKD9G7QGMM+4=
123125
github.com/davecgh/go-spew v0.0.0-20151105211317-5215b55f46b2/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
124126
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
125127
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=

0 commit comments

Comments
 (0)