Skip to content

Commit 7f4d60d

Browse files
yp05327Linux User
authored andcommitted
Add actions support to package auth verification (go-gitea#23729)
Partly fixes go-gitea#23642 Error info: ![image](https://user-images.githubusercontent.com/18380374/227827027-4280a368-ec9e-49e0-bb93-6b496ada7cd9.png) ActionsUser (userID -2) is used to login in to docker in action jobs. Due to we have no permission policy settings of ActionsUser now, ActionsUser can only access public registry by this quick fix.
1 parent abf0386 commit 7f4d60d

File tree

2 files changed

+22
-37
lines changed

2 files changed

+22
-37
lines changed

routers/api/packages/api.go

Lines changed: 20 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -43,35 +43,38 @@ func reqPackageAccess(accessMode perm.AccessMode) func(ctx *context.Context) {
4343
}
4444
}
4545

46-
// CommonRoutes provide endpoints for most package managers (except containers - see below)
47-
// These are mounted on `/api/packages` (not `/api/v1/packages`)
48-
func CommonRoutes(ctx gocontext.Context) *web.Route {
49-
r := web.NewRoute()
50-
51-
r.Use(context.PackageContexter(ctx))
52-
53-
authMethods := []auth.Method{
54-
&auth.OAuth2{},
55-
&auth.Basic{},
56-
&nuget.Auth{},
57-
&conan.Auth{},
58-
&chef.Auth{},
59-
}
46+
func verifyAuth(r *web.Route, authMethods []auth.Method) {
6047
if setting.Service.EnableReverseProxyAuth {
6148
authMethods = append(authMethods, &auth.ReverseProxy{})
6249
}
63-
6450
authGroup := auth.NewGroup(authMethods...)
51+
6552
r.Use(func(ctx *context.Context) {
6653
var err error
6754
ctx.Doer, err = authGroup.Verify(ctx.Req, ctx.Resp, ctx, ctx.Session)
6855
if err != nil {
69-
log.Error("Verify: %v", err)
56+
log.Error("Failed to verify user: %v", err)
7057
ctx.Error(http.StatusUnauthorized, "authGroup.Verify")
7158
return
7259
}
7360
ctx.IsSigned = ctx.Doer != nil
7461
})
62+
}
63+
64+
// CommonRoutes provide endpoints for most package managers (except containers - see below)
65+
// These are mounted on `/api/packages` (not `/api/v1/packages`)
66+
func CommonRoutes(ctx gocontext.Context) *web.Route {
67+
r := web.NewRoute()
68+
69+
r.Use(context.PackageContexter(ctx))
70+
71+
verifyAuth(r, []auth.Method{
72+
&auth.OAuth2{},
73+
&auth.Basic{},
74+
&nuget.Auth{},
75+
&conan.Auth{},
76+
&chef.Auth{},
77+
})
7578

7679
r.Group("/{username}", func() {
7780
r.Group("/cargo", func() {
@@ -401,24 +404,9 @@ func ContainerRoutes(ctx gocontext.Context) *web.Route {
401404

402405
r.Use(context.PackageContexter(ctx))
403406

404-
authMethods := []auth.Method{
407+
verifyAuth(r, []auth.Method{
405408
&auth.Basic{},
406409
&container.Auth{},
407-
}
408-
if setting.Service.EnableReverseProxyAuth {
409-
authMethods = append(authMethods, &auth.ReverseProxy{})
410-
}
411-
412-
authGroup := auth.NewGroup(authMethods...)
413-
r.Use(func(ctx *context.Context) {
414-
var err error
415-
ctx.Doer, err = authGroup.Verify(ctx.Req, ctx.Resp, ctx, ctx.Session)
416-
if err != nil {
417-
log.Error("Failed to verify user: %v", err)
418-
ctx.Error(http.StatusUnauthorized, "Verify")
419-
return
420-
}
421-
ctx.IsSigned = ctx.Doer != nil
422410
})
423411

424412
r.Get("", container.ReqContainerAccess, container.DetermineSupport)

routers/api/packages/container/auth.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,10 @@ func (a *Auth) Verify(req *http.Request, w http.ResponseWriter, store auth.DataS
3030
if uid == 0 {
3131
return nil, nil
3232
}
33-
if uid == -1 {
34-
return user_model.NewGhostUser(), nil
35-
}
3633

37-
u, err := user_model.GetUserByID(req.Context(), uid)
34+
u, err := user_model.GetPossibleUserByID(req.Context(), uid)
3835
if err != nil {
39-
log.Error("GetUserByID: %v", err)
36+
log.Error("GetPossibleUserByID: %v", err)
4037
return nil, err
4138
}
4239

0 commit comments

Comments
 (0)