Skip to content

Commit caab1fa

Browse files
wxiaoguangGiteaBot
authored andcommitted
Remove sub-path from container registry realm (go-gitea#31293)
Container registry requires that the "/v2" must be in the root, so the sub-path in AppURL should be removed
1 parent bbe98a3 commit caab1fa

File tree

5 files changed

+23
-14
lines changed

5 files changed

+23
-14
lines changed

modules/setting/packages.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ package setting
66
import (
77
"fmt"
88
"math"
9-
"net/url"
109
"os"
1110
"path/filepath"
1211

@@ -19,7 +18,6 @@ var (
1918
Storage *Storage
2019
Enabled bool
2120
ChunkedUploadPath string
22-
RegistryHost string
2321

2422
LimitTotalOwnerCount int64
2523
LimitTotalOwnerSize int64
@@ -66,9 +64,6 @@ func loadPackagesFrom(rootCfg ConfigProvider) (err error) {
6664
return err
6765
}
6866

69-
appURL, _ := url.Parse(AppURL)
70-
Packages.RegistryHost = appURL.Host
71-
7267
Packages.ChunkedUploadPath = filepath.ToSlash(sec.Key("CHUNKED_UPLOAD_PATH").MustString("tmp/package-upload"))
7368
if !filepath.IsAbs(Packages.ChunkedUploadPath) {
7469
Packages.ChunkedUploadPath = filepath.ToSlash(filepath.Join(AppDataPath, Packages.ChunkedUploadPath))

modules/test/utils.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,10 @@ func IsNormalPageCompleted(s string) bool {
3434
return strings.Contains(s, `<footer class="page-footer"`) && strings.Contains(s, `</html>`)
3535
}
3636

37-
func MockVariableValue[T any](p *T, v T) (reset func()) {
37+
func MockVariableValue[T any](p *T, v ...T) (reset func()) {
3838
old := *p
39-
*p = v
39+
if len(v) > 0 {
40+
*p = v[0]
41+
}
4042
return func() { *p = old }
4143
}

routers/api/packages/container/container.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,9 @@ func apiErrorDefined(ctx *context.Context, err *namedError) {
116116
}
117117

118118
func apiUnauthorizedError(ctx *context.Context) {
119-
// TODO: it doesn't seem quite right but it doesn't really cause problem at the moment.
120-
// container registry requires that the "/v2" must be in the root, so the sub-path in AppURL should be removed, ideally.
121-
ctx.Resp.Header().Add("WWW-Authenticate", `Bearer realm="`+httplib.GuessCurrentAppURL(ctx)+`v2/token",service="container_registry",scope="*"`)
119+
// container registry requires that the "/v2" must be in the root, so the sub-path in AppURL should be removed
120+
realmURL := strings.TrimSuffix(httplib.GuessCurrentAppURL(ctx), setting.AppSubURL+"/") + "/v2/token"
121+
ctx.Resp.Header().Add("WWW-Authenticate", `Bearer realm="`+realmURL+`",service="container_registry",scope="*"`)
122122
apiErrorDefined(ctx, errUnauthorized)
123123
}
124124

routers/web/user/package.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package user
55

66
import (
77
"net/http"
8+
"net/url"
89

910
"code.gitea.io/gitea/models/db"
1011
org_model "code.gitea.io/gitea/models/organization"
@@ -15,6 +16,7 @@ import (
1516
repo_model "code.gitea.io/gitea/models/repo"
1617
"code.gitea.io/gitea/modules/base"
1718
"code.gitea.io/gitea/modules/container"
19+
"code.gitea.io/gitea/modules/httplib"
1820
"code.gitea.io/gitea/modules/log"
1921
"code.gitea.io/gitea/modules/optional"
2022
alpine_module "code.gitea.io/gitea/modules/packages/alpine"
@@ -178,7 +180,11 @@ func ViewPackageVersion(ctx *context.Context) {
178180

179181
switch pd.Package.Type {
180182
case packages_model.TypeContainer:
181-
ctx.Data["RegistryHost"] = setting.Packages.RegistryHost
183+
registryAppURL, err := url.Parse(httplib.GuessCurrentAppURL(ctx))
184+
if err != nil {
185+
registryAppURL, _ = url.Parse(setting.AppURL)
186+
}
187+
ctx.Data["RegistryHost"] = registryAppURL.Host
182188
case packages_model.TypeAlpine:
183189
branches := make(container.Set[string])
184190
repositories := make(container.Set[string])

tests/integration/api_packages_container_test.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,15 +84,15 @@ func TestPackageContainer(t *testing.T) {
8484
Token string `json:"token"`
8585
}
8686

87-
authenticate := []string{`Bearer realm="` + setting.AppURL + `v2/token",service="container_registry",scope="*"`}
87+
defaultAuthenticateValues := []string{`Bearer realm="` + setting.AppURL + `v2/token",service="container_registry",scope="*"`}
8888

8989
t.Run("Anonymous", func(t *testing.T) {
9090
defer tests.PrintCurrentTest(t)()
9191

9292
req := NewRequest(t, "GET", fmt.Sprintf("%sv2", setting.AppURL))
9393
resp := MakeRequest(t, req, http.StatusUnauthorized)
9494

95-
assert.ElementsMatch(t, authenticate, resp.Header().Values("WWW-Authenticate"))
95+
assert.ElementsMatch(t, defaultAuthenticateValues, resp.Header().Values("WWW-Authenticate"))
9696

9797
req = NewRequest(t, "GET", fmt.Sprintf("%sv2/token", setting.AppURL))
9898
resp = MakeRequest(t, req, http.StatusOK)
@@ -115,6 +115,12 @@ func TestPackageContainer(t *testing.T) {
115115

116116
req = NewRequest(t, "GET", fmt.Sprintf("%sv2/token", setting.AppURL))
117117
MakeRequest(t, req, http.StatusUnauthorized)
118+
119+
defer test.MockVariableValue(&setting.AppURL, "https://domain:8443/sub-path/")()
120+
defer test.MockVariableValue(&setting.AppSubURL, "/sub-path")()
121+
req = NewRequest(t, "GET", "/v2")
122+
resp = MakeRequest(t, req, http.StatusUnauthorized)
123+
assert.Equal(t, `Bearer realm="https://domain:8443/v2/token",service="container_registry",scope="*"`, resp.Header().Get("WWW-Authenticate"))
118124
})
119125

120126
t.Run("User", func(t *testing.T) {
@@ -123,7 +129,7 @@ func TestPackageContainer(t *testing.T) {
123129
req := NewRequest(t, "GET", fmt.Sprintf("%sv2", setting.AppURL))
124130
resp := MakeRequest(t, req, http.StatusUnauthorized)
125131

126-
assert.ElementsMatch(t, authenticate, resp.Header().Values("WWW-Authenticate"))
132+
assert.ElementsMatch(t, defaultAuthenticateValues, resp.Header().Values("WWW-Authenticate"))
127133

128134
req = NewRequest(t, "GET", fmt.Sprintf("%sv2/token", setting.AppURL)).
129135
AddBasicAuth(user.Name)

0 commit comments

Comments
 (0)