From c3e85a7d98994538d795eac2cca3f594ffccfdd4 Mon Sep 17 00:00:00 2001 From: Ernst von Oelsen Date: Tue, 3 Dec 2024 19:31:40 +0100 Subject: [PATCH 1/3] Parse http URLs properly. Signed-off-by: Ernst von Oelsen --- pkg/files/file.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/pkg/files/file.go b/pkg/files/file.go index accfe7cd..ea4a3199 100644 --- a/pkg/files/file.go +++ b/pkg/files/file.go @@ -47,6 +47,12 @@ func NewSortedFilesFromPaths(paths []string, opts SymlinkAllowOpts) ([]*File, er relativePath := "" pathPieces := strings.Split(path, "=") + if strings.HasPrefix(pathPieces[0], "http://") || strings.HasPrefix(pathPieces[0], "https://") { + pathPieces = []string{path} + } else if len(pathPieces) > 1 && (strings.HasPrefix(pathPieces[1], "http://") || strings.HasPrefix(pathPieces[1], "https://")) { + pathPieces[1] = strings.Join(pathPieces[1:], "=") + pathPieces = pathPieces[0:2] + } switch len(pathPieces) { case 1: @@ -71,6 +77,7 @@ func NewSortedFilesFromPaths(paths []string, opts SymlinkAllowOpts) ([]*File, er case strings.HasPrefix(path, "http://") || strings.HasPrefix(path, "https://"): file, err := NewFileFromSource(NewCachedSource(NewHTTPSource(path))) + file.relPath = strings.Split(file.relPath, "?")[0] if err != nil { return nil, err } From d6fbd0e2d8a55335be488b2649b06092774d7e83 Mon Sep 17 00:00:00 2001 From: Ernst von Oelsen Date: Wed, 4 Dec 2024 11:28:47 +0100 Subject: [PATCH 2/3] Parse relPath without potential query params. Signed-off-by: Ernst von Oelsen --- pkg/files/file.go | 1 - pkg/files/sources.go | 4 +++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/pkg/files/file.go b/pkg/files/file.go index ea4a3199..6a956919 100644 --- a/pkg/files/file.go +++ b/pkg/files/file.go @@ -77,7 +77,6 @@ func NewSortedFilesFromPaths(paths []string, opts SymlinkAllowOpts) ([]*File, er case strings.HasPrefix(path, "http://") || strings.HasPrefix(path, "https://"): file, err := NewFileFromSource(NewCachedSource(NewHTTPSource(path))) - file.relPath = strings.Split(file.relPath, "?")[0] if err != nil { return nil, err } diff --git a/pkg/files/sources.go b/pkg/files/sources.go index 9c01f945..47ec54e3 100644 --- a/pkg/files/sources.go +++ b/pkg/files/sources.go @@ -97,7 +97,9 @@ func (s HTTPSource) Description() string { return fmt.Sprintf("HTTP URL '%s'", s.url) } -func (s HTTPSource) RelativePath() (string, error) { return path.Base(s.url), nil } +func (s HTTPSource) RelativePath() (string, error) { + return path.Base(strings.Split(s.url, "?")[0]), nil +} func (s HTTPSource) Bytes() ([]byte, error) { resp, err := s.Client.Get(s.url) From 04ac63b55e2d3434c0d8e59b234f4c599f8e50b3 Mon Sep 17 00:00:00 2001 From: Ernst von Oelsen Date: Wed, 29 Jan 2025 15:53:42 +0100 Subject: [PATCH 3/3] Test the file path handling. Signed-off-by: Ernst von Oelsen --- pkg/files/file_test.go | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 pkg/files/file_test.go diff --git a/pkg/files/file_test.go b/pkg/files/file_test.go new file mode 100644 index 00000000..8e05aac5 --- /dev/null +++ b/pkg/files/file_test.go @@ -0,0 +1,27 @@ +// Copyright 2024 The Carvel Authors. +// SPDX-License-Identifier: Apache-2.0 + +package files_test + +import ( + "carvel.dev/ytt/pkg/files" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "testing" +) + +func TestNewSortedFilesFromPaths(t *testing.T) { + inputPaths := []string{ + "../yamltemplate/filetests/def.tpltest", + "https://example.org/test.yaml?hello=world&foo=bar", + "an/overwritten/path/to/file.yaml=https://example.org/test.yaml?hello=world&foo=bar"} + filesToProcess, err := files.NewSortedFilesFromPaths(inputPaths, files.SymlinkAllowOpts{true, []string{}}) + + require.NoError(t, err) + + assert.Equal(t, filesToProcess[0].RelativePath(), "def.tpltest") + assert.Equal(t, filesToProcess[1].RelativePath(), "test.yaml") + assert.Equal(t, filesToProcess[1].Description(), "HTTP URL 'https://example.org/test.yaml?hello=world&foo=bar'") + assert.Equal(t, filesToProcess[2].RelativePath(), "an/overwritten/path/to/file.yaml") + assert.Equal(t, filesToProcess[2].Description(), "HTTP URL 'https://example.org/test.yaml?hello=world&foo=bar'") +}