@@ -15,7 +15,7 @@ import (
15
15
"github.com/coder/coder/coderd/rbac"
16
16
)
17
17
18
- var _ database.Store = (* AuthzQuerier )(nil )
18
+ var _ database.Store = (* authzQuerier )(nil )
19
19
20
20
var (
21
21
// NoActorError wraps ErrNoRows for the api to return a 404. This is the correct
@@ -40,7 +40,7 @@ func (NotAuthorizedError) Unwrap() error {
40
40
return sql .ErrNoRows
41
41
}
42
42
43
- func LogNotAuthorizedError (ctx context.Context , logger slog.Logger , err error ) error {
43
+ func logNotAuthorizedError (ctx context.Context , logger slog.Logger , err error ) error {
44
44
// Only log the errors if it is an UnauthorizedError error.
45
45
internalError := new (rbac.UnauthorizedError )
46
46
if err != nil && xerrors .As (err , internalError ) {
@@ -55,37 +55,42 @@ func LogNotAuthorizedError(ctx context.Context, logger slog.Logger, err error) e
55
55
}
56
56
}
57
57
58
- // AuthzQuerier is a wrapper around the database store that performs authorization
59
- // checks before returning data. All AuthzQuerier methods expect an authorization
58
+ // authzQuerier is a wrapper around the database store that performs authorization
59
+ // checks before returning data. All authzQuerier methods expect an authorization
60
60
// subject present in the context. If no subject is present, most methods will
61
61
// fail.
62
62
//
63
63
// Use WithAuthorizeContext to set the authorization subject in the context for
64
64
// the common user case.
65
- type AuthzQuerier struct {
65
+ type authzQuerier struct {
66
66
db database.Store
67
67
auth rbac.Authorizer
68
68
log slog.Logger
69
69
}
70
70
71
- func New (db database.Store , authorizer rbac.Authorizer , logger slog.Logger ) * AuthzQuerier {
72
- return & AuthzQuerier {
71
+ func New (db database.Store , authorizer rbac.Authorizer , logger slog.Logger ) database.Store {
72
+ // If the underlying db store is already an authzquerier, return it.
73
+ // Do not double wrap.
74
+ if _ , ok := db .(* authzQuerier ); ok {
75
+ return db
76
+ }
77
+ return & authzQuerier {
73
78
db : db ,
74
79
auth : authorizer ,
75
80
log : logger ,
76
81
}
77
82
}
78
83
79
84
// authorizeContext is a helper function to authorize an action on an object.
80
- func (q * AuthzQuerier ) authorizeContext (ctx context.Context , action rbac.Action , object rbac.Objecter ) error {
85
+ func (q * authzQuerier ) authorizeContext (ctx context.Context , action rbac.Action , object rbac.Objecter ) error {
81
86
act , ok := ActorFromContext (ctx )
82
87
if ! ok {
83
88
return NoActorError
84
89
}
85
90
86
91
err := q .auth .Authorize (ctx , act , action , object .RBACObject ())
87
92
if err != nil {
88
- return LogNotAuthorizedError (ctx , q .log , err )
93
+ return logNotAuthorizedError (ctx , q .log , err )
89
94
}
90
95
return nil
91
96
}
@@ -143,7 +148,7 @@ func insert[
143
148
// Authorize the action
144
149
err = authorizer .Authorize (ctx , act , rbac .ActionCreate , object .RBACObject ())
145
150
if err != nil {
146
- return empty , LogNotAuthorizedError (ctx , logger , err )
151
+ return empty , logNotAuthorizedError (ctx , logger , err )
147
152
}
148
153
149
154
// Insert the database object
@@ -226,7 +231,7 @@ func fetch[
226
231
// Authorize the action
227
232
err = authorizer .Authorize (ctx , act , rbac .ActionRead , object .RBACObject ())
228
233
if err != nil {
229
- return empty , LogNotAuthorizedError (ctx , logger , err )
234
+ return empty , logNotAuthorizedError (ctx , logger , err )
230
235
}
231
236
232
237
return object , nil
@@ -290,7 +295,7 @@ func fetchAndQuery[
290
295
// Authorize the action
291
296
err = authorizer .Authorize (ctx , act , action , object .RBACObject ())
292
297
if err != nil {
293
- return empty , LogNotAuthorizedError (ctx , logger , err )
298
+ return empty , logNotAuthorizedError (ctx , logger , err )
294
299
}
295
300
296
301
return queryFunc (ctx , arg )
0 commit comments