Skip to content

Commit bb6c670

Browse files
authored
Add actions support to package auth verification (#23729)
Partly fixes #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 fd9d072 commit bb6c670

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
@@ -44,35 +44,38 @@ func reqPackageAccess(accessMode perm.AccessMode) func(ctx *context.Context) {
4444
}
4545
}
4646

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

7780
r.Group("/{username}", func() {
7881
r.Group("/cargo", func() {
@@ -437,24 +440,9 @@ func ContainerRoutes(ctx gocontext.Context) *web.Route {
437440

438441
r.Use(context.PackageContexter(ctx))
439442

440-
authMethods := []auth.Method{
443+
verifyAuth(r, []auth.Method{
441444
&auth.Basic{},
442445
&container.Auth{},
443-
}
444-
if setting.Service.EnableReverseProxyAuth {
445-
authMethods = append(authMethods, &auth.ReverseProxy{})
446-
}
447-
448-
authGroup := auth.NewGroup(authMethods...)
449-
r.Use(func(ctx *context.Context) {
450-
var err error
451-
ctx.Doer, err = authGroup.Verify(ctx.Req, ctx.Resp, ctx, ctx.Session)
452-
if err != nil {
453-
log.Error("Failed to verify user: %v", err)
454-
ctx.Error(http.StatusUnauthorized, "Verify")
455-
return
456-
}
457-
ctx.IsSigned = ctx.Doer != nil
458446
})
459447

460448
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)