From a8157e94a9ad4ffbd8919c9bbbeee24b8153bcc4 Mon Sep 17 00:00:00 2001 From: soharab-ic <156293296+soharab-ic@users.noreply.github.com> Date: Mon, 13 Jan 2025 12:55:50 +0530 Subject: [PATCH] PR-3051: Fix download deploy package out of k8s cluster (#3136) * Fix download deploy package out of k8s cluster Signed-off-by: LiuXiang * Fix pkg getdeploy cli command to fetch pkgs stored in storagesvc and remote pkgs Signed-off-by: Md Soharab Ansari * Instead of Contains, parse url and check path for better validation Signed-off-by: Md Soharab Ansari --------- Signed-off-by: LiuXiang Signed-off-by: Md Soharab Ansari Co-authored-by: LiuXiang --- pkg/fission-cli/cmd/package/util/util.go | 36 +++++++++++++++++-- pkg/fission-cli/cmd/package/util/util_test.go | 32 +++++++++++++++++ 2 files changed, 66 insertions(+), 2 deletions(-) diff --git a/pkg/fission-cli/cmd/package/util/util.go b/pkg/fission-cli/cmd/package/util/util.go index 1f6e7005..28d08489 100644 --- a/pkg/fission-cli/cmd/package/util/util.go +++ b/pkg/fission-cli/cmd/package/util/util.go @@ -165,12 +165,19 @@ func DownloadURL(fileUrl string) (io.ReadCloser, error) { func DownloadStrorageURL(ctx context.Context, client cmd.Client, fileUrl string) (io.ReadCloser, error) { var resp *http.Response - storagesvcURL, err := util.GetStorageURL(ctx, client) + var err error + + valid, err := validArchiveURL(fileUrl) if err != nil { return nil, err } - if strings.HasPrefix(fileUrl, storagesvcURL.String()+"/v1/archive?id=") { + if valid { + storagesvcURL, err := util.GetStorageURL(ctx, client) + if err != nil { + return nil, err + } + url, err := url.Parse(fileUrl) if err != nil { return nil, err @@ -229,3 +236,28 @@ func PrintPackageSummary(writer io.Writer, pkg *fv1.Package) { fmt.Fprintf(w, "%v\n%v", "Build Logs:", buildlog) w.Flush() } + +// validArchiveURL checks if the given URL is a valid archive URL +func validArchiveURL(urlStr string) (bool, error) { + // Parse the URL string into a URL object + parsedURL, err := url.Parse(urlStr) + if err != nil { + return false, fmt.Errorf("failed to parse URL: %v", err) + } + + // Check if the path starts with /v1/archive + if !strings.HasPrefix(parsedURL.Path, "/v1/archive") { + return false, nil + } + + // Get query parameters + queryParams := parsedURL.Query() + + // Check if 'id' parameter exists + if queryParams.Get("id") == "" { + return false, nil + } + + // URL matches all criteria + return true, nil +} diff --git a/pkg/fission-cli/cmd/package/util/util_test.go b/pkg/fission-cli/cmd/package/util/util_test.go index b26408a1..e5e8eaa3 100644 --- a/pkg/fission-cli/cmd/package/util/util_test.go +++ b/pkg/fission-cli/cmd/package/util/util_test.go @@ -32,3 +32,35 @@ func TestPrintPackageSummary(t *testing.T) { t.Errorf("PrintPackageBuildLog() = %v, want %v", gotWriter, expected) } } + +func TestValidArchiveURL(t *testing.T) { + tests := []struct { + name string + url string + expected bool + }{ + { + name: "Valid Archive URL", + url: "http://storagesvc.fission/v1/archive?id=/fission/fission-functions/Fc4c15f47-bb49-47c5-b382-526a6539841d", + expected: true, + }, + { + name: "Invalid Archive URL", + url: "https://raw.githubusercontent.com/imaginery/training/refs/heads/fission/hello-go?token=ABCD", + expected: false, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := validArchiveURL(tt.url) + if err != nil { + t.Errorf("got error %v", err) + } + + if got != tt.expected { + t.Errorf("expected %t got %t", got, tt.expected) + } + }) + } +}