Skip to content

Commit 2e2018f

Browse files
author
Prince Rachit Sinha
committed
[image-builder] replace reference to aliases with actual repo for cross mount blobs
1 parent e148325 commit 2e2018f

File tree

2 files changed

+104
-3
lines changed

2 files changed

+104
-3
lines changed

components/image-builder-bob/pkg/proxy/proxy.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ type Repo struct {
5252
Auth func() docker.Authorizer
5353
}
5454

55-
func rewriteURL(u *url.URL, fromRepo, toRepo, host, tag string) {
55+
func (proxy *Proxy) rewriteURL(u *url.URL, fromRepo, toRepo, host, tag string) {
5656
var (
5757
from = "/v2/" + strings.Trim(fromRepo, "/") + "/"
5858
to = "/v2/" + strings.Trim(toRepo, "/") + "/"
@@ -76,6 +76,13 @@ func rewriteURL(u *url.URL, fromRepo, toRepo, host, tag string) {
7676
}
7777
}
7878

79+
if u.RawQuery != "" {
80+
// As per OCI distribution spec this (from=) should be the only possible reference to a cross repo
81+
// https://github.com/opencontainers/distribution-spec/blob/main/spec.md#push
82+
for k, v := range proxy.Aliases {
83+
u.RawQuery = strings.Replace(u.RawQuery, "from="+k, "from="+v.Repo, 1)
84+
}
85+
}
7986
u.Host = host
8087
}
8188

@@ -99,7 +106,7 @@ func (proxy *Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
99106
return
100107
}
101108

102-
rewriteURL(r.URL, alias, repo.Repo, repo.Host, repo.Tag)
109+
proxy.rewriteURL(r.URL, alias, repo.Repo, repo.Host, repo.Tag)
103110
r.Host = r.URL.Host
104111

105112
auth := repo.Auth()
@@ -192,7 +199,7 @@ func (proxy *Proxy) reverse(alias string) *httputil.ReverseProxy {
192199
return err
193200
}
194201

195-
rewriteURL(lurl, repo.Repo, alias, proxy.Host.Host, "")
202+
proxy.rewriteURL(lurl, repo.Repo, alias, proxy.Host.Host, "")
196203
lurl.Host = proxy.Host.Host
197204
// force scheme to http assuming this proxy never runs as https
198205
lurl.Scheme = proxy.Host.Scheme
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
// Copyright (c) 2022 Gitpod GmbH. All rights reserved.
2+
// Licensed under the GNU Affero General Public License (AGPL).
3+
// See License-AGPL.txt in the project root for license information.
4+
5+
package proxy
6+
7+
import (
8+
"net/url"
9+
"testing"
10+
)
11+
12+
func TestRewriteURL(t *testing.T) {
13+
type input struct {
14+
proxy *Proxy
15+
u url.URL
16+
fromRepo string
17+
toRepo string
18+
host string
19+
tag string
20+
}
21+
tests := []struct {
22+
Name string
23+
in input
24+
u url.URL
25+
}{
26+
{
27+
Name: "updates alias reference when it is cross blob mount",
28+
in: input{
29+
proxy: &Proxy{
30+
Host: url.URL{Host: "localhost:8080", Scheme: "http"},
31+
Aliases: map[string]Repo{
32+
"base": {
33+
Repo: "/gitpod/base-images",
34+
},
35+
"target": {
36+
Repo: "/gitpod/workspace-images",
37+
},
38+
},
39+
proxies: nil,
40+
},
41+
fromRepo: "base",
42+
toRepo: "/gitpod/base-images",
43+
host: "registry.gitlab.com",
44+
tag: "tag12345",
45+
u: url.URL{
46+
Host: "localhost.com",
47+
RawQuery: "/mounts/uploads/?mount=sha:12345?from=base",
48+
},
49+
},
50+
u: url.URL{
51+
Host: "registry.gitlab.com",
52+
RawQuery: "/mounts/uploads/?mount=sha:12345?from=/gitpod/base-images",
53+
},
54+
},
55+
{
56+
Name: "does not update alias reference when it is not cross blob mount",
57+
in: input{
58+
proxy: &Proxy{
59+
Host: url.URL{Host: "localhost:8080", Scheme: "http"},
60+
Aliases: map[string]Repo{
61+
"base": {
62+
Repo: "/gitpod/base-images",
63+
},
64+
"target": {
65+
Repo: "/gitpod/workspace-images",
66+
},
67+
},
68+
},
69+
fromRepo: "base",
70+
toRepo: "/gitpod/base-images",
71+
host: "registry.gitlab.com",
72+
tag: "tag12345",
73+
u: url.URL{
74+
Host: "localhost.com",
75+
RawQuery: "/blobs/sha:12345base",
76+
},
77+
},
78+
u: url.URL{
79+
Host: "registry.gitlab.com",
80+
RawQuery: "/blobs/sha:12345base",
81+
},
82+
},
83+
}
84+
85+
for _, test := range tests {
86+
t.Run(test.Name, func(t *testing.T) {
87+
test.in.proxy.rewriteURL(&test.in.u, test.in.fromRepo, test.in.toRepo, test.in.host, test.in.tag)
88+
if test.in.u.RawQuery != test.u.RawQuery {
89+
t.Errorf("expected raw query: %s but got %s", test.u.RawQuery, test.in.u.RawQuery)
90+
}
91+
})
92+
}
93+
94+
}

0 commit comments

Comments
 (0)