From 8c077cefae3bfe0ad3725ffcd5488c5cb4612784 Mon Sep 17 00:00:00 2001 From: Lucas Pinheiro Date: Tue, 17 Dec 2024 08:17:32 -0300 Subject: [PATCH] fix: handle custom domain URL (#62) --- pkg/api/signed_url.go | 20 ++++++++++++++++++++ pkg/api/signed_url_test.go | 18 ++++++++++++++++-- 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/pkg/api/signed_url.go b/pkg/api/signed_url.go index bd716f5..523cb4f 100644 --- a/pkg/api/signed_url.go +++ b/pkg/api/signed_url.go @@ -14,6 +14,10 @@ import ( log "github.com/sirupsen/logrus" ) +var ( + customDomainRegex = regexp.MustCompile(`https:\/\/[a-z0-9\-\.]+\/[a-z0-9\-]+\/[a-z0-9\-]+\/([^?]+)\?`) +) + type SignedURL struct { URL string `json:"url,omitempty"` Method string `json:"method,omitempty"` @@ -221,6 +225,10 @@ func (u *SignedURL) GetObject() (string, error) { log.Debugf("Parsing localhost URL: %s\n", u.URL) return parseLocalhostURL(URL) + case customDomainRegex.Match([]byte(URL.String())): + log.Debugf("Parsing custom domain URL: %s\n", u.URL) + return parseCustomDomainURL(URL) + default: log.Warnf("Failed to parse URL '%s' - unrecognized host '%s'\n", u.URL, host) return "", fmt.Errorf("unrecognized host %s", host) @@ -257,6 +265,18 @@ func parseS3URL(URL *url.URL) (string, error) { return parsed[3], nil } +// Custom domain URLs are used when minio or some other s3-compatible storage is being used. +// The URL will be of the form: https:///// +// We are only interested in the part here. +func parseCustomDomainURL(URL *url.URL) (string, error) { + parsed := customDomainRegex.FindStringSubmatch(URL.String()) + if len(parsed) < 2 { + return "", fmt.Errorf("Failed to parse custom domain URL '%s'\n", URL) + } + + return parsed[1], nil +} + // Localhost URLs are used during tests func parseLocalhostURL(URL *url.URL) (string, error) { // we don't want the leading slash diff --git a/pkg/api/signed_url_test.go b/pkg/api/signed_url_test.go index 4fe5604..32b24c4 100644 --- a/pkg/api/signed_url_test.go +++ b/pkg/api/signed_url_test.go @@ -63,8 +63,22 @@ func Test__GetObject(t *testing.T) { assert.Equal(t, "artifacts/project/projectid/mydir/myfile.txt", obj) }) - t.Run("bad host", func(t *testing.T) { - signedURL := SignedURL{URL: "https://somehost.com/projectid/artifacts/project/projectid/myfile.txt"} + t.Run("custom domain - file", func(t *testing.T) { + signedURL := SignedURL{URL: "https://artifacts.somedomain.com/my-bucket1/projectid/artifacts/project/projectid/myfile.txt?X-Amz-Algorithm"} + obj, err := signedURL.GetObject() + assert.Nil(t, err) + assert.Equal(t, "artifacts/project/projectid/myfile.txt", obj) + }) + + t.Run("custom domain - file inside directory", func(t *testing.T) { + signedURL := SignedURL{URL: "https://artifacts.somedomain.com/my-bucket1/projectid/artifacts/project/projectid/mydir/myfile.txt?Expires=231256754712"} + obj, err := signedURL.GetObject() + assert.Nil(t, err) + assert.Equal(t, "artifacts/project/projectid/mydir/myfile.txt", obj) + }) + + t.Run("bad URL", func(t *testing.T) { + signedURL := SignedURL{URL: "http://somehost.com/projectid/artifacts/project/projectid/myfile.txt"} _, err := signedURL.GetObject() assert.NotNil(t, err) })