@@ -51,7 +51,7 @@ type HTTPAuthorizer struct {
51
51
// return
52
52
// }
53
53
func (api * API ) Authorize (r * http.Request , action rbac.Action , object rbac.Objecter ) bool {
54
- return api .HTTPAuth .Authorize (r , action , object )
54
+ return api .HTTPAuth .Authorize (r , action , object , true )
55
55
}
56
56
57
57
// Authorize will return false if the user is not authorized to do the action.
@@ -63,27 +63,33 @@ func (api *API) Authorize(r *http.Request, action rbac.Action, object rbac.Objec
63
63
// httpapi.Forbidden(rw)
64
64
// return
65
65
// }
66
- func (h * HTTPAuthorizer ) Authorize (r * http.Request , action rbac.Action , object rbac.Objecter ) bool {
66
+ func (h * HTTPAuthorizer ) Authorize (r * http.Request , action rbac.Action , object rbac.Objecter , logUnauthorized bool ) bool {
67
67
roles := httpmw .UserAuthorization (r )
68
68
err := h .Authorizer .Authorize (r .Context (), roles .Actor , action , object .RBACObject ())
69
69
if err != nil {
70
- // Log the errors for debugging
71
- internalError := new (rbac.UnauthorizedError )
72
- logger := h .Logger
73
- if xerrors .As (err , internalError ) {
74
- logger = h .Logger .With (slog .F ("internal" , internalError .Internal ()))
70
+ // Sometimes we do not want to log the unauthorized errors.
71
+ // Example: If an endpoint expects the normal case to return unauthorized
72
+ // to check a user is not an admin, we do not want to log that since it is
73
+ // the expected path.
74
+ if logUnauthorized {
75
+ // Log the errors for debugging
76
+ internalError := new (rbac.UnauthorizedError )
77
+ logger := h .Logger
78
+ if xerrors .As (err , internalError ) {
79
+ logger = h .Logger .With (slog .F ("internal" , internalError .Internal ()))
80
+ }
81
+ // Log information for debugging. This will be very helpful
82
+ // in the early days
83
+ logger .Warn (r .Context (), "unauthorized" ,
84
+ slog .F ("roles" , roles .Actor .SafeRoleNames ()),
85
+ slog .F ("actor_id" , roles .Actor .ID ),
86
+ slog .F ("actor_name" , roles .ActorName ),
87
+ slog .F ("scope" , roles .Actor .SafeScopeName ()),
88
+ slog .F ("route" , r .URL .Path ),
89
+ slog .F ("action" , action ),
90
+ slog .F ("object" , object ),
91
+ )
75
92
}
76
- // Log information for debugging. This will be very helpful
77
- // in the early days
78
- logger .Warn (r .Context (), "unauthorized" ,
79
- slog .F ("roles" , roles .Actor .SafeRoleNames ()),
80
- slog .F ("actor_id" , roles .Actor .ID ),
81
- slog .F ("actor_name" , roles .ActorName ),
82
- slog .F ("scope" , roles .Actor .SafeScopeName ()),
83
- slog .F ("route" , r .URL .Path ),
84
- slog .F ("action" , action ),
85
- slog .F ("object" , object ),
86
- )
87
93
88
94
return false
89
95
}
0 commit comments