PR-3051: Fix download deploy package out of k8s cluster (#3136)

* Fix download deploy package out of k8s cluster

Signed-off-by: LiuXiang <lx1036@126.com>

* Fix pkg getdeploy cli command to fetch pkgs stored in storagesvc and remote pkgs

Signed-off-by: Md Soharab Ansari <soharab.ansari@infracloud.io>

* Instead of Contains, parse url and check path for better validation

Signed-off-by: Md Soharab Ansari <soharab.ansari@infracloud.io>

---------

Signed-off-by: LiuXiang <lx1036@126.com>
Signed-off-by: Md Soharab Ansari <soharab.ansari@infracloud.io>
Co-authored-by: LiuXiang <lx1036@126.com>
This commit is contained in:
soharab-ic
2025-01-13 12:55:50 +05:30
committed by GitHub
co-authored by LiuXiang
parent cd23fc6f63
commit a8157e94a9
2 changed files with 66 additions and 2 deletions
+34 -2
View File
@@ -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
}
@@ -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)
}
})
}
}