diff --git a/components/image-builder-bob/pkg/proxy/proxy.go b/components/image-builder-bob/pkg/proxy/proxy.go index dbabc0d353fb48..f842aa83ef5f73 100644 --- a/components/image-builder-bob/pkg/proxy/proxy.go +++ b/components/image-builder-bob/pkg/proxy/proxy.go @@ -60,7 +60,7 @@ func rewriteDockerAPIURL(u *url.URL, fromRepo, toRepo, host, tag string) { from = "/v2/" + strings.Trim(fromRepo, "/") + "/" to = "/v2/" + strings.Trim(toRepo, "/") + "/" ) - u.Path = to + strings.TrimPrefix(u.Path, from) + u.Path = to + strings.TrimPrefix(strings.TrimPrefix(u.Path, from), "/") // we reset the escaped encoding hint, because EscapedPath will produce a valid encoding. u.RawPath = "" @@ -95,7 +95,7 @@ func rewriteNonDockerAPIURL(u *url.URL, fromPrefix, toPrefix, host string) { if toPrefix == "" { to = "/" } - u.Path = to + strings.TrimPrefix(u.Path, from) + u.Path = to + strings.TrimPrefix(strings.TrimPrefix(u.Path, from), "/") // we reset the escaped encoding hint, because EscapedPath will produce a valid encoding. u.RawPath = "" diff --git a/components/image-builder-bob/pkg/proxy/proxy_test.go b/components/image-builder-bob/pkg/proxy/proxy_test.go index 78352b4e9ebd07..32628213d72f20 100644 --- a/components/image-builder-bob/pkg/proxy/proxy_test.go +++ b/components/image-builder-bob/pkg/proxy/proxy_test.go @@ -69,6 +69,22 @@ func TestRewriteNonDockerAPIURL(t *testing.T) { Path: "/to/some/random/path", }, }, + { + Name: "fromPrefix and toPrefix are not empty and origin url is not start with fromPrefix", + in: input{ + fromPrefix: "from", + toPrefix: "to", + host: "localhost.com", + u: url.URL{ + Host: "example.com", + Path: "/other-string/some/random/path", + }, + }, + u: url.URL{ + Host: "localhost.com", + Path: "/to/other-string/some/random/path", + }, + }, { Name: "fromPrefix and toPrefix are empty", in: input{ @@ -134,6 +150,23 @@ func TestRewriteDockerAPIURL(t *testing.T) { Path: "/v2/base/some/random/path", }, }, + { + Name: "remote to localhost without repo", + in: input{ + fromRepo: "base-images", + toRepo: "base", + host: "localhost.com", + tag: "", + u: url.URL{ + Host: "prince.azurecr.io", + Path: "/v2/other-string/some/random/path", + }, + }, + u: url.URL{ + Host: "localhost.com", + Path: "/v2/base/v2/other-string/some/random/path", + }, + }, { Name: "localhost to remote", in: input{