Skip to content

Commit cf42b7f

Browse files
authored
Merge pull request #1548 from ljakimczuk/fix-remote-patches
Fix: skip trying to decrypt remote patches as local
2 parents a8c7cc1 + a49ad57 commit cf42b7f

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

internal/decryptor/decryptor.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"errors"
2424
"fmt"
2525
"io/fs"
26+
"net/url"
2627
"os"
2728
"path/filepath"
2829
"strings"
@@ -571,6 +572,11 @@ func (d *Decryptor) decryptKustomizationSources(visited map[string]struct{}) vis
571572
if patch.Path == "" {
572573
continue
573574
}
575+
576+
if isRemoteURL(patch.Path) {
577+
continue
578+
}
579+
574580
// Determine the format for the patch, defaulting to YAML if not specified.
575581
format := formatForPath(patch.Path)
576582
// Visit the patch reference and attempt to decrypt it.
@@ -836,6 +842,17 @@ func recurseKustomizationFiles(root, path string, visit visitKustomization, visi
836842
return nil
837843
}
838844

845+
func isRemoteURL(path string) bool {
846+
u, err := url.Parse(path)
847+
if err != nil {
848+
return false
849+
}
850+
851+
// A remote URL will have a scheme (like "http", "https", "ssh")
852+
// AND a host (like "example.com").
853+
return u.Scheme != "" && u.Host != ""
854+
}
855+
839856
// isSOPSEncryptedResource detects if the given resource is a SOPS' encrypted
840857
// resource by looking for ".sops" and ".sops.mac" fields.
841858
func isSOPSEncryptedResource(res *resource.Resource) bool {

internal/decryptor/decryptor_test.go

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -818,6 +818,7 @@ func TestDecryptor_decryptKustomizationSources(t *testing.T) {
818818
wordirSuffix string
819819
path string
820820
files []file
821+
patch []kustypes.Patch
821822
secretGenerator []kustypes.SecretArgs
822823
expectVisited []string
823824
wantErr error
@@ -922,6 +923,34 @@ func TestDecryptor_decryptKustomizationSources(t *testing.T) {
922923
wantErr: &fs.PathError{Op: "lstat", Path: "data.env", Err: fmt.Errorf("")},
923924
expectVisited: []string{},
924925
},
926+
{
927+
name: "ignore remote patches",
928+
path: "subdir",
929+
files: []file{
930+
{name: "subdir/file.txt", data: []byte("file"), encrypt: true, expectData: true},
931+
{name: "subdir/patch.yaml", data: []byte("op: add\n"), encrypt: true, expectData: true},
932+
},
933+
patch: []kustypes.Patch{
934+
{
935+
Path: "patch.yaml",
936+
},
937+
{
938+
// this patch gets ignored due to being remote
939+
Path: "https://raw.githubusercontent.com/kubernetes-sigs/kustomize/master/examples/wordpress/patch.yaml",
940+
},
941+
},
942+
secretGenerator: []kustypes.SecretArgs{
943+
{
944+
GeneratorArgs: kustypes.GeneratorArgs{
945+
Name: "envSecret",
946+
KvPairSources: kustypes.KvPairSources{
947+
FileSources: []string{"file.txt"},
948+
},
949+
},
950+
},
951+
},
952+
expectVisited: []string{"subdir/patch.yaml", "subdir/file.txt"},
953+
},
925954
}
926955
for _, tt := range tests {
927956
t.Run(tt.name, func(t *testing.T) {
@@ -967,7 +996,10 @@ func TestDecryptor_decryptKustomizationSources(t *testing.T) {
967996

968997
visited := make(map[string]struct{}, 0)
969998
visit := d.decryptKustomizationSources(visited)
970-
kus := &kustypes.Kustomization{SecretGenerator: tt.secretGenerator}
999+
kus := &kustypes.Kustomization{
1000+
Patches: tt.patch,
1001+
SecretGenerator: tt.secretGenerator,
1002+
}
9711003

9721004
err = visit(root, tt.path, kus)
9731005
if tt.wantErr == nil {

0 commit comments

Comments
 (0)