Skip to content

feat: Add initial AuthzQuerier implementation #5919

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 384 commits into from
Feb 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
384 commits
Select commit Hold shift + click to select a range
7d0fad4
Fix typo
Emyrk Jan 27, 2023
efe7f93
Merge remote-tracking branch 'origin/main' into authzquerier_layer
johnstcn Jan 30, 2023
923219a
make RecordingAuthorizer wrap another rbac.Authorizer
johnstcn Jan 30, 2023
f97ca2a
fix FakeAuthorizer
johnstcn Jan 30, 2023
ad6ff52
skip TestAuthorizeAllEndpoints if authz_querier experiment is enabled
johnstcn Jan 30, 2023
0e3b9ff
lock more things
johnstcn Jan 30, 2023
feb7689
Merge remote-tracking branch 'origin/main' into authzquerier_layer
johnstcn Jan 30, 2023
083bcf2
rbac/builtin.go: remove consts
johnstcn Jan 30, 2023
161842d
extract getAgentSubject()
johnstcn Jan 30, 2023
11983ab
Merge remote-tracking branch 'origin/main' into authzquerier_layer
johnstcn Jan 31, 2023
ab9c049
use systemCtx in API.oauthLogin()
johnstcn Jan 31, 2023
04e32bc
workspaceagents: fetch request ctx after httpmw.WorkspaceAgent sets a…
johnstcn Jan 31, 2023
21d0f97
httpmw: pass systemCtx to getAgentSubject, add OwnerID to workspace a…
johnstcn Jan 31, 2023
76a490e
authzquery: workspace: fix GetWorkspaceAppByAgentIDAndSlug and GetWor…
johnstcn Jan 31, 2023
fa399d6
steven said its ok to remove this
johnstcn Jan 31, 2023
cb9a2c5
Fix recursive test
Emyrk Jan 31, 2023
9aa7835
Move experiment init below authz init
Emyrk Jan 31, 2023
8f6265b
add httpmw.SystemAuthCtx to api.handleSubdomainApplications
johnstcn Jan 31, 2023
bfa91c1
REVERT THIS COMMIT BEFORE MERGING !!!!
johnstcn Jan 31, 2023
13710c6
ALSO DO NOT MERGE THIS COMMIT
johnstcn Jan 31, 2023
467646d
authzquery: fix InsertAgentStat
johnstcn Jan 31, 2023
32c8af1
activitybump: use systemCtx for activityBumpWorkspace
johnstcn Jan 31, 2023
11ef507
Merge remote-tracking branch 'origin/main' into authzquerier_layer
Emyrk Jan 31, 2023
b08fc44
remove unused function
Emyrk Jan 31, 2023
69a6346
authzquery: fixes to templates and parameters
johnstcn Feb 1, 2023
4967fe6
Fix fetch dry run template version from job id
Emyrk Feb 1, 2023
6a7b053
Pass actor to follow logs for subscriber listen
Emyrk Feb 1, 2023
fc992cd
gerge remote-tracking branch 'origin/main' into authzquerier_layer
johnstcn Feb 2, 2023
d599753
rbac: add IsUnauthorizedError, return 404 if UnauthorizedError in org…
johnstcn Feb 2, 2023
0ce75c6
goimports
johnstcn Feb 2, 2023
357b05d
Implemented first draft testing framework
Emyrk Feb 2, 2023
6bb2e1c
authzquery: fixes in workspaces.go
johnstcn Feb 2, 2023
8a8ce06
Merge remote-tracking branch 'origin/authzquerier_layer' into authzqu…
johnstcn Feb 2, 2023
300f6dc
Add test method accounting to ensure all functions are called
Emyrk Feb 2, 2023
9f7d276
fixup! authzquery: fixes in workspaces.go
johnstcn Feb 2, 2023
d37379d
Merge remote-tracking branch 'origin/authzquerier_layer' into authzqu…
johnstcn Feb 2, 2023
6cc14b4
Add rbac checks
Emyrk Feb 2, 2023
2107b74
Fix scim unit tests
Emyrk Feb 2, 2023
53f7a5d
authzquery: update UpdateTemplateDeletedByID to call SoftDeleteTempla…
johnstcn Feb 2, 2023
44ca906
Merge remote-tracking branch 'origin/authzquerier_layer' into authzqu…
johnstcn Feb 2, 2023
73655ab
Fix scim and workspace agent unit tests
Emyrk Feb 2, 2023
0d6f6a0
Fix getTemplateVersionsByID
Emyrk Feb 2, 2023
32a9e12
Fix more unit tests
Emyrk Feb 2, 2023
85ff5f1
Fix license unit test
Emyrk Feb 2, 2023
e152d5f
authzquery: add some more convenience methods, comments etc.
johnstcn Feb 2, 2023
ef1deb5
Merge remote-tracking branch 'origin/authzquerier_layer' into authzqu…
johnstcn Feb 2, 2023
4848481
Add sentinel errors for unauth authz errors
Emyrk Feb 2, 2023
b583a1e
Use sentinal error that returns a 404
Emyrk Feb 2, 2023
75747f5
Use sentinel error always
Emyrk Feb 2, 2023
add77c6
add slice.New util function
johnstcn Feb 2, 2023
4357a3c
RecordingAuthorizer: AllAsserted: provide more information on missed …
johnstcn Feb 2, 2023
9dbc6bf
Merge remote-tracking branch 'origin/authzquerier_layer' into authzqu…
johnstcn Feb 2, 2023
c285f6f
skip GetAuthorizedWorkspaces
johnstcn Feb 2, 2023
58261fe
Add admin context to provisonerd
Emyrk Feb 2, 2023
a4a2994
Merge remote-tracking branch 'origin/main' into authzquerier_layer
Emyrk Feb 2, 2023
874e9da
Fix Delte group
Emyrk Feb 2, 2023
d878e71
remove excess comments
Emyrk Feb 2, 2023
10ac765
typos and lint
Emyrk Feb 2, 2023
e353c4d
Fix template admin permissions
Emyrk Feb 2, 2023
db647ba
Fix rbac unit test
Emyrk Feb 2, 2023
f45a170
Call compileToSQL in getWorkspaces
Emyrk Feb 2, 2023
b4beb38
Call compileToSQL in getWorkspaces
Emyrk Feb 2, 2023
d9d23b6
Fix compile issue
Emyrk Feb 2, 2023
8780e4e
Handle nil prepared case
Emyrk Feb 2, 2023
e6d5c2f
Linting
Emyrk Feb 2, 2023
672b2e0
fix GetLatestWorkspaceBuildsByWorkspaceIDs
johnstcn Feb 2, 2023
5a0e5a2
add existing workspace tests
johnstcn Feb 2, 2023
016c56d
Check returned error from db call
Emyrk Feb 2, 2023
e086e51
Fix build number to be 1 indexed
Emyrk Feb 2, 2023
390a284
more tests
johnstcn Feb 2, 2023
53fcf79
generate random AuthInstanceID, more unit tests
johnstcn Feb 2, 2023
0add01a
Test all api key methods
Emyrk Feb 2, 2023
6191561
Test audit methods
Emyrk Feb 2, 2023
e8ab762
Add group and file unit tests
Emyrk Feb 2, 2023
837f66a
Add template unit test
Emyrk Feb 2, 2023
88d422f
Add system functions
Emyrk Feb 2, 2023
a32b4f3
Merge remote-tracking branch 'origin/main' into authzquerier_layer
Emyrk Feb 2, 2023
d3affdc
Fix merge compile issues
Emyrk Feb 2, 2023
338e300
Jobs, orgs, and extra methods implemented
Emyrk Feb 2, 2023
f5c4040
Merge remote-tracking branch 'origin/main' into authzquerier_layer
Emyrk Feb 3, 2023
a7899cf
:
Emyrk Feb 3, 2023
0da03c6
Implement parameters tests
Emyrk Feb 3, 2023
4415b6b
Start license unit tests
Emyrk Feb 3, 2023
fb8973c
Merge remote-tracking branch 'origin/main' into authzquerier_layer
Emyrk Feb 3, 2023
6763fbf
Finish license tests
Emyrk Feb 3, 2023
d1b948d
Add workspace tests
Emyrk Feb 3, 2023
13a4fab
chore: Add WorkspaceApps to dbgen
Emyrk Feb 3, 2023
607e428
Add user unit tests
Emyrk Feb 3, 2023
592a62b
GitSSHKey, UserLink, GitAuthLink
Emyrk Feb 3, 2023
102af8a
Fix user unit tests
Emyrk Feb 3, 2023
d2b1f41
Merge remote-tracking branch 'origin/main' into authzquerier_layer
johnstcn Feb 3, 2023
b6afc2a
rm unused-import
johnstcn Feb 3, 2023
d1cfa73
authzquery: implement group and system methods
johnstcn Feb 3, 2023
b7cd5a5
fixup! authzquery: implement group and system methods
johnstcn Feb 3, 2023
f34c61b
fixup! authzquery: implement group and system methods
johnstcn Feb 3, 2023
e53d709
ineffasign
johnstcn Feb 3, 2023
cb4d92f
unshadow, unused-reciever
johnstcn Feb 3, 2023
13a8445
unused-param
johnstcn Feb 3, 2023
e1ce04e
finish testing template methods
johnstcn Feb 3, 2023
7fde8fb
Rename logger-> log, database->db, authorizer->auth, remove "authoriz…
Emyrk Feb 3, 2023
7ba3482
Rename fetchSet to fetchWithPostFilter
Emyrk Feb 3, 2023
cf763cb
Verify the correct error is returned on disallow auth
Emyrk Feb 3, 2023
64e80fb
Linting
Emyrk Feb 3, 2023
432a261
database: add missing argument to GetAuthorizedWorkspaces
johnstcn Feb 3, 2023
8134d1b
Refactor recording authorizer
Emyrk Feb 3, 2023
29e7c46
Address incorrect errors
Emyrk Feb 3, 2023
a37fead
Support asserting outputs in authzquery test
Emyrk Feb 3, 2023
2e435cf
Require outputs to be asserted
Emyrk Feb 3, 2023
792cbb6
Fix comment
Emyrk Feb 3, 2023
1336e28
allow skipping outputs
Emyrk Feb 3, 2023
0923780
Fix user tests to expect outputs
Emyrk Feb 3, 2023
92f89ec
fix api key unit tests to expect outputs
Emyrk Feb 3, 2023
acae52b
values audit_test.go
johnstcn Feb 3, 2023
764b0a0
Implement outputs for workspace tests
Emyrk Feb 3, 2023
0cee453
Some system outputs
Emyrk Feb 3, 2023
d1e3214
values file_test.go
johnstcn Feb 3, 2023
e799713
values group_test.go
johnstcn Feb 3, 2023
cbb4502
Template outputs
Emyrk Feb 3, 2023
83a31cb
System outputs
Emyrk Feb 3, 2023
9010ad7
values job_test.go, methods_test.go
johnstcn Feb 3, 2023
912c97a
Add organization output
Emyrk Feb 3, 2023
a3f67bb
values license_test.go
johnstcn Feb 3, 2023
7d31209
Merge remote-tracking branch 'origin/authzquerier_layer' into authzqu…
johnstcn Feb 3, 2023
2c906e5
Add parameters ooutput
Emyrk Feb 3, 2023
5e92648
Api key and audit fix
Emyrk Feb 3, 2023
04cce68
Fix file outputs
Emyrk Feb 3, 2023
712c0f4
Fix groups
Emyrk Feb 3, 2023
8f92a77
Fix job, license, and org
Emyrk Feb 3, 2023
3df9848
System done
Emyrk Feb 3, 2023
90a9d87
Fix templates
Emyrk Feb 3, 2023
8b39d7e
Fix most users
Emyrk Feb 3, 2023
a621743
Linting
Emyrk Feb 3, 2023
2c002bd
workspace_test.go values fix
johnstcn Feb 3, 2023
cbd5cb4
nolint unreachable
johnstcn Feb 3, 2023
6fed479
Fix all user method tests
Emyrk Feb 3, 2023
5928c37
Add unit tests for InTx and Ping
Emyrk Feb 3, 2023
46b8366
Add AuthorizedXX tests
Emyrk Feb 3, 2023
21a6f6a
api: skip Authorize if codersdk.ExperimentAuthzQuerier enabled
johnstcn Feb 3, 2023
d6810de
Merge remote-tracking branch 'origin/authzquerier_layer' into authzqu…
johnstcn Feb 3, 2023
889b650
Only abort early on checks that should be removed
Emyrk Feb 3, 2023
72ed503
remove authorizedQuery
Emyrk Feb 3, 2023
94ff5ef
authzquery: use GetProvisionerJobById to auth GetWorkspaceResourceByID
johnstcn Feb 3, 2023
38a90de
Merge remote-tracking branch 'origin/authzquerier_layer' into authzqu…
johnstcn Feb 3, 2023
c962897
All insert generic functions use rbac.ActionCreate
Emyrk Feb 3, 2023
62e3fa0
Fix unit tests that use create over update
Emyrk Feb 3, 2023
a0725b9
un-skip TestAuthorizeAllEndpoints and remove always-true conditional …
johnstcn Feb 3, 2023
a4c4489
Merge remote-tracking branch 'origin/authzquerier_layer' into authzqu…
johnstcn Feb 3, 2023
567cfa4
Merge remote-tracking branch 'origin/main' into authzquerier_layer
johnstcn Feb 3, 2023
91910af
fixup! un-skip TestAuthorizeAllEndpoints and remove always-true condi…
johnstcn Feb 3, 2023
dce10b5
where my members at yo
johnstcn Feb 3, 2023
58b71f9
Allow out of order slicing
Emyrk Feb 3, 2023
833bbc2
Use slice.New()
Emyrk Feb 3, 2023
fcfdb4e
paralalalaleleleel
johnstcn Feb 3, 2023
8858fd3
Ordering of users in fetch
Emyrk Feb 3, 2023
64e0f8c
Add actual scope to workspace agent ctx
Emyrk Feb 6, 2023
9d6ab90
Merge remote-tracking branch 'origin/main' into authzquerier_layer
Emyrk Feb 6, 2023
1821dcb
RBAC UserData should use the correct rbac resource
Emyrk Feb 7, 2023
7c9f686
Remove workspace IDs filter arg
Emyrk Feb 7, 2023
eda4e0a
rename authzquery.NewAuthzQuerier to authzquery.New
johnstcn Feb 7, 2023
073aa2c
Start removing QueryByRelated
Emyrk Feb 7, 2023
4fe26e9
Start removing QueryByRelated
Emyrk Feb 7, 2023
13f1c9f
remove queryWithRelated
johnstcn Feb 7, 2023
ba172ea
Fixup generic func comments
Emyrk Feb 7, 2023
509ebdc
fixup! remove queryWithRelated
johnstcn Feb 7, 2023
802272b
remove todo
Emyrk Feb 7, 2023
57cde94
Improve readability of generics and arguments
Emyrk Feb 7, 2023
4daa878
Update fetchAndQuery comment
Emyrk Feb 7, 2023
4608462
Fix comment about system functions
Emyrk Feb 7, 2023
2767264
remove insert() function
johnstcn Feb 7, 2023
fc3ae4b
insertWithReturn is the new insert
johnstcn Feb 7, 2023
bf653b6
Merge remote-tracking branch 'origin/authzquerier_layer' into authzqu…
johnstcn Feb 7, 2023
ca68db2
Remove duplicate workspace agent scope
Emyrk Feb 7, 2023
f1f05cc
Pass agent ctx into activityBumpWorkspace
Emyrk Feb 7, 2023
eb38c0d
remove panic
johnstcn Feb 7, 2023
b96bb21
Merge remote-tracking branch 'origin/authzquerier_layer' into authzqu…
johnstcn Feb 7, 2023
0a061be
Remove uneeded comments
Emyrk Feb 7, 2023
8295eb3
Use 's' for all suite methods
Emyrk Feb 7, 2023
c2bc20e
Reduce LoC by using setup and teardown test
Emyrk Feb 7, 2023
3bd3e89
Remove nested "RunMethodTest", use new assertions
Emyrk Feb 7, 2023
052c531
Start converting tests to the new format
Emyrk Feb 7, 2023
6aa55ac
refactor out error test
Emyrk Feb 7, 2023
72d0a4e
Update unit test teardown to include NoActorError
Emyrk Feb 7, 2023
4c68562
Attempt a new style of subtest
Emyrk Feb 7, 2023
fdfdd73
Fix user tests to use new subtest strategy
Emyrk Feb 7, 2023
c902715
Fix unit tests names
Emyrk Feb 7, 2023
f5dbd3e
Convert more tests to new format
Emyrk Feb 7, 2023
97ad3df
Convert all unit tests
Emyrk Feb 7, 2023
b369c99
Add comments
Emyrk Feb 7, 2023
03d42d3
remove unused code
Emyrk Feb 7, 2023
69d1aa3
rename MethodCase to expects
Emyrk Feb 7, 2023
3861a43
Merge remote-tracking branch 'origin/main' into authzquerier_layer
Emyrk Feb 7, 2023
9e7ff9a
DB function was renamed/changed
Emyrk Feb 7, 2023
9dc357e
imports
johnstcn Feb 8, 2023
ad6ad36
authzquery -> database/dbauthz
johnstcn Feb 8, 2023
0985060
conditionally skip TestAuthorizeAllEndpoints
johnstcn Feb 8, 2023
d4e1124
userauth: use systemCtx when setting user groups
johnstcn Feb 8, 2023
4e6b43f
Merge branch 'cj/dbauthz' into authzquerier_layer
johnstcn Feb 8, 2023
22e1057
fixup! authzquery -> database/dbauthz
johnstcn Feb 8, 2023
c5346ad
rm todo
johnstcn Feb 8, 2023
7a14b64
Condense into 1 file
Emyrk Feb 8, 2023
b89b430
doc.go
Emyrk Feb 8, 2023
21532a6
Update coderd/database/dbauthz/doc.go
Emyrk Feb 8, 2023
6a7970f
Move files around, consolidate to dbauthz.go
Emyrk Feb 8, 2023
399241a
Merge remote-tracking branch 'origin/main' into authzquerier_layer
Emyrk Feb 8, 2023
924ef9c
fix unit test to work with dbauthz
Emyrk Feb 8, 2023
2cf0fb2
Consolidate files
Emyrk Feb 8, 2023
d1bb7cf
goimports
johnstcn Feb 9, 2023
ef97e4b
rename methods.go -> querier.go
johnstcn Feb 9, 2023
951d74f
Do not export the authzQuerier
Emyrk Feb 9, 2023
2cf1cad
Rename to "querier", add unit test for double wrap protection
Emyrk Feb 9, 2023
a9f2581
remove duplicate dbauthz init
johnstcn Feb 10, 2023
832d91a
use codersdk experiment value instead of hard-coded string
johnstcn Feb 10, 2023
0ddee07
Merge remote-tracking branch 'origin/main' into authzquerier_layer
johnstcn Feb 10, 2023
cc76887
Merge remote-tracking branch 'origin/main' into authzquerier_layer
johnstcn Feb 10, 2023
002f354
Remove rbac ctx from provisionerd
Emyrk Feb 10, 2023
039e1e2
fixup! Remove rbac ctx from provisionerd
Emyrk Feb 10, 2023
b509b8f
wip: dbauthz.WithAuthorizeSystemContext -> dbauthz.AsSystem()
johnstcn Feb 10, 2023
524394f
Add lint rule to prevent system ctx abuse
Emyrk Feb 10, 2023
f666e13
fixup! wip: dbauthz.WithAuthorizeSystemContext -> dbauthz.AsSystem()
johnstcn Feb 10, 2023
1a97843
Merge remote-tracking branch 'origin/authzquerier_layer' into authzqu…
johnstcn Feb 10, 2023
4b292e2
fix autobuild/executor unit tests
johnstcn Feb 10, 2023
bebe638
Add middleware for using system ctx in middlewares
Emyrk Feb 10, 2023
f99c778
fix compile errors
johnstcn Feb 10, 2023
84bc12f
set system ctx in provisionerdserver
johnstcn Feb 10, 2023
c5e69fa
Unit test the AsAuthzSystem mw
Emyrk Feb 10, 2023
a93c2d5
Update unit tests to cover the no actor case
Emyrk Feb 10, 2023
f7023a4
Typo
Emyrk Feb 10, 2023
035609b
remove todo
Emyrk Feb 10, 2023
bbe4f18
User proper rbac errors in unit test
Emyrk Feb 10, 2023
f0bbaaf
Add unit test to cover prepareSQL error case
Emyrk Feb 10, 2023
51a2dae
NullUUID is empty, so takeFirst fails
Emyrk Feb 10, 2023
00955e0
Add AsSystem
Emyrk Feb 10, 2023
2289f4d
Fix internal error logging
Emyrk Feb 10, 2023
106d58b
Remove error noise in unit tests
Emyrk Feb 10, 2023
2724dfd
Use AsSystem for decrypting encrypted api keys
Emyrk Feb 10, 2023
2c34f6d
fix linter errors
johnstcn Feb 13, 2023
c54afc5
userauth: create API key as user instead of as system
johnstcn Feb 13, 2023
d282e9c
Merge remote-tracking branch 'origin/main' into authzquerier_layer
johnstcn Feb 13, 2023
7334046
Remove unused file
Emyrk Feb 13, 2023
3dbbc71
Use system context to set a disconnected agent
Emyrk Feb 13, 2023
cd6096f
Log error on failed agent disconnect update
Emyrk Feb 13, 2023
eb2497a
Merge remote-tracking branch 'origin/main' into authzquerier_layer
Emyrk Feb 14, 2023
d2c7a1f
Unit tests do not handle error log well
Emyrk Feb 14, 2023
99fa810
Merge remote-tracking branch 'origin/main' into authzquerier_layer
Emyrk Feb 14, 2023
1dfa287
Fix license uuid in merge
Emyrk Feb 14, 2023
57ab200
Fix unit test error logging
Emyrk Feb 14, 2023
306c591
Correct the returned error from not authorized
Emyrk Feb 14, 2023
f39cee0
Fix if/else logic
Emyrk Feb 14, 2023
2ed5588
fixup! Fix if/else logic
Emyrk Feb 14, 2023
c09b077
Merge remote-tracking branch 'origin/main' into authzquerier_layer
johnstcn Feb 14, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions coderd/activitybump.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ import (

// activityBumpWorkspace automatically bumps the workspace's auto-off timer
// if it is set to expire soon.
func activityBumpWorkspace(log slog.Logger, db database.Store, workspaceID uuid.UUID) {
func activityBumpWorkspace(ctx context.Context, log slog.Logger, db database.Store, workspaceID uuid.UUID) {
// We set a short timeout so if the app is under load, these
// low priority operations fail first.
ctx, cancel := context.WithTimeout(context.Background(), time.Second*15)
ctx, cancel := context.WithTimeout(ctx, time.Second*15)
defer cancel()

err := db.InTx(func(s database.Store) error {
Expand Down Expand Up @@ -82,9 +82,12 @@ func activityBumpWorkspace(log slog.Logger, db database.Store, workspaceID uuid.
return nil
}, nil)
if err != nil {
log.Error(ctx, "bump failed", slog.Error(err),
slog.F("workspace_id", workspaceID),
)
if !xerrors.Is(err, context.Canceled) {
// Bump will fail if the context is cancelled, but this is ok.
log.Error(ctx, "bump failed", slog.Error(err),
slog.F("workspace_id", workspaceID),
)
}
return
}

Expand Down
22 changes: 22 additions & 0 deletions coderd/authorize.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,28 @@ type HTTPAuthorizer struct {
// return
// }
func (api *API) Authorize(r *http.Request, action rbac.Action, object rbac.Objecter) bool {
// The experiment does not replace ALL rbac checks, but does replace most.
// This statement aborts early on the checks that will be removed in the
// future when this experiment is default.
if api.Experiments.Enabled(codersdk.ExperimentAuthzQuerier) {
// Some resource types do not interact with the persistent layer and
// we need to keep these checks happening in the API layer.
switch object.RBACObject().Type {
case rbac.ResourceWorkspaceExecution.Type:
// This is not a db resource, always in API layer
case rbac.ResourceDeploymentConfig.Type:
// For metric cache items like DAU, we do not hit the DB.
// Some db actions are in asserted in the authz layer.
case rbac.ResourceReplicas.Type:
// Replica rbac is checked for adding and removing replicas.
case rbac.ResourceProvisionerDaemon.Type:
// Provisioner rbac is checked for adding and removing provisioners.
case rbac.ResourceDebugInfo.Type:
// This is not a db resource, always in API layer.
default:
return true
}
}
return api.HTTPAuth.Authorize(r, action, object)
}

Expand Down
4 changes: 3 additions & 1 deletion coderd/autobuild/executor/lifecycle_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"cdr.dev/slog"
"github.com/coder/coder/coderd/autobuild/schedule"
"github.com/coder/coder/coderd/database"
"github.com/coder/coder/coderd/database/dbauthz"
)

// Executor automatically starts or stops workspaces.
Expand All @@ -33,7 +34,8 @@ type Stats struct {
// New returns a new autobuild executor.
func New(ctx context.Context, db database.Store, log slog.Logger, tick <-chan time.Time) *Executor {
le := &Executor{
ctx: ctx,
//nolint:gocritic // TODO: make an autostart role instead of using System
ctx: dbauthz.AsSystem(ctx),
db: db,
tick: tick,
log: log,
Expand Down
34 changes: 20 additions & 14 deletions coderd/coderd.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import (
"github.com/coder/coder/coderd/audit"
"github.com/coder/coder/coderd/awsidentity"
"github.com/coder/coder/coderd/database"
"github.com/coder/coder/coderd/database/dbauthz"
"github.com/coder/coder/coderd/database/dbtype"
"github.com/coder/coder/coderd/gitauth"
"github.com/coder/coder/coderd/gitsshkey"
Expand Down Expand Up @@ -157,13 +158,6 @@ func New(options *Options) *API {
options = &Options{}
}
experiments := initExperiments(options.Logger, options.DeploymentConfig.Experiments.Value, options.DeploymentConfig.Experimental.Value)
// TODO: remove this once we promote authz_querier out of experiments.
if experiments.Enabled(codersdk.ExperimentAuthzQuerier) {
panic("Coming soon!")
// if _, ok := (options.Database).(*authzquery.AuthzQuerier); !ok {
// options.Database = authzquery.NewAuthzQuerier(options.Database, options.Authorizer)
// }
}
if options.AppHostname != "" && options.AppHostnameRegex == nil || options.AppHostname == "" && options.AppHostnameRegex != nil {
panic("coderd: both AppHostname and AppHostnameRegex must be set or unset")
}
Expand Down Expand Up @@ -204,6 +198,14 @@ func New(options *Options) *API {
if options.Auditor == nil {
options.Auditor = audit.NewNop()
}
// TODO: remove this once we promote authz_querier out of experiments.
if experiments.Enabled(codersdk.ExperimentAuthzQuerier) {
options.Database = dbauthz.New(
options.Database,
options.Authorizer,
options.Logger.Named("authz_querier"),
)
}
if options.SetUserGroups == nil {
options.SetUserGroups = func(context.Context, database.Store, uuid.UUID, []string) error { return nil }
}
Expand Down Expand Up @@ -304,8 +306,10 @@ func New(options *Options) *API {
DisableSessionExpiryRefresh: options.DeploymentConfig.DisableSessionExpiryRefresh.Value,
Optional: true,
}),
httpmw.ExtractUserParam(api.Database, false),
httpmw.ExtractWorkspaceAndAgentParam(api.Database),
httpmw.AsAuthzSystem(
httpmw.ExtractUserParam(api.Database, false),
httpmw.ExtractWorkspaceAndAgentParam(api.Database),
),
),
// Build-Version is helpful for debugging.
func(next http.Handler) http.Handler {
Expand All @@ -332,11 +336,13 @@ func New(options *Options) *API {
DisableSessionExpiryRefresh: options.DeploymentConfig.DisableSessionExpiryRefresh.Value,
Optional: true,
}),
// Redirect to the login page if the user tries to open an app with
// "me" as the username and they are not logged in.
httpmw.ExtractUserParam(api.Database, true),
// Extracts the <workspace.agent> from the url
httpmw.ExtractWorkspaceAndAgentParam(api.Database),
httpmw.AsAuthzSystem(
// Redirect to the login page if the user tries to open an app with
// "me" as the username and they are not logged in.
httpmw.ExtractUserParam(api.Database, true),
// Extracts the <workspace.agent> from the url
httpmw.ExtractWorkspaceAndAgentParam(api.Database),
),
)
r.HandleFunc("/*", api.workspaceAppsProxyPath)
}
Expand Down
17 changes: 6 additions & 11 deletions coderd/coderdtest/authorize.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@ import (
"testing"
"time"

"github.com/coder/coder/cryptorand"
"github.com/go-chi/chi/v5"
"github.com/google/uuid"
"github.com/moby/moby/pkg/namesgenerator"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"golang.org/x/xerrors"

"github.com/coder/coder/cryptorand"

"github.com/coder/coder/coderd"
"github.com/coder/coder/coderd/database/dbfake"
"github.com/coder/coder/coderd/rbac"
"github.com/coder/coder/coderd/rbac/regosql"
"github.com/coder/coder/codersdk"
Expand All @@ -30,12 +30,6 @@ import (
)

func AGPLRoutes(a *AuthTester) (map[string]string, map[string]RouteCheck) {
// For any route using SQL filters, we need to know if the database is an
// in memory fake. This is because the in memory fake does not use SQL, and
// still uses rego. So this boolean indicates how to assert the expected
// behavior.
_, isMemoryDB := a.api.Database.(dbfake.FakeDatabase)

// Some quick reused objects
workspaceRBACObj := rbac.ResourceWorkspace.WithID(a.Workspace.ID).InOrg(a.Organization.ID).WithOwner(a.Workspace.OwnerID.String())
workspaceExecObj := rbac.ResourceWorkspaceExecution.WithID(a.Workspace.ID).InOrg(a.Organization.ID).WithOwner(a.Workspace.OwnerID.String())
Expand Down Expand Up @@ -269,16 +263,17 @@ func AGPLRoutes(a *AuthTester) (map[string]string, map[string]RouteCheck) {
"POST:/api/v2/workspaces/{workspace}/builds": {StatusCode: http.StatusBadRequest, NoAuthorize: true},
"POST:/api/v2/organizations/{organization}/templateversions": {StatusCode: http.StatusBadRequest, NoAuthorize: true},

// Endpoints that use the SQLQuery filter.
// For any route using SQL filters, we do not check authorization.
// This is because the in memory fake does not use SQL.
"GET:/api/v2/workspaces/": {
StatusCode: http.StatusOK,
NoAuthorize: !isMemoryDB,
NoAuthorize: true,
AssertAction: rbac.ActionRead,
AssertObject: rbac.ResourceWorkspace,
},
"GET:/api/v2/organizations/{organization}/templates": {
StatusCode: http.StatusOK,
NoAuthorize: !isMemoryDB,
NoAuthorize: true,
AssertAction: rbac.ActionRead,
AssertObject: rbac.ResourceTemplate,
},
Expand Down
6 changes: 6 additions & 0 deletions coderd/coderdtest/authorize_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,21 @@ package coderdtest_test

import (
"context"
"os"
"strings"
"testing"

"github.com/stretchr/testify/require"

"github.com/coder/coder/coderd/coderdtest"
"github.com/coder/coder/coderd/rbac"
"github.com/coder/coder/codersdk"
)

func TestAuthorizeAllEndpoints(t *testing.T) {
if strings.Contains(os.Getenv("CODER_EXPERIMENTS_TEST"), string(codersdk.ExperimentAuthzQuerier)) {
t.Skip("Skipping TestAuthorizeAllEndpoints for authz_querier experiment")
}
t.Parallel()
client, _, api := coderdtest.NewWithAPI(t, &coderdtest.Options{
// Required for any subdomain-based proxy tests to pass.
Expand Down
15 changes: 9 additions & 6 deletions coderd/coderdtest/coderdtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (
"github.com/golang-jwt/jwt"
"github.com/google/uuid"
"github.com/moby/moby/pkg/namesgenerator"
"github.com/prometheus/client_golang/prometheus"
"github.com/spf13/afero"
"github.com/spf13/pflag"
"github.com/stretchr/testify/assert"
Expand All @@ -58,6 +59,7 @@ import (
"github.com/coder/coder/coderd/autobuild/executor"
"github.com/coder/coder/coderd/awsidentity"
"github.com/coder/coder/coderd/database"
"github.com/coder/coder/coderd/database/dbauthz"
"github.com/coder/coder/coderd/database/dbtestutil"
"github.com/coder/coder/coderd/gitauth"
"github.com/coder/coder/coderd/gitsshkey"
Expand Down Expand Up @@ -179,12 +181,13 @@ func NewOptions(t *testing.T, options *Options) (func(http.Handler), context.Can
options.Database, options.Pubsub = dbtestutil.NewDB(t)
}
// TODO: remove this once we're ready to enable authz querier by default.
if strings.Contains(os.Getenv("CODER_EXPERIMENTS_TEST"), "authz_querier") {
panic("Coming soon!")
// if options.Authorizer != nil {
// options.Authorizer = &RecordingAuthorizer{}
// }
// options.Database = authzquery.NewAuthzQuerier(options.Database, options.Authorizer)
if strings.Contains(os.Getenv("CODER_EXPERIMENTS_TEST"), string(codersdk.ExperimentAuthzQuerier)) {
if options.Authorizer == nil {
options.Authorizer = &RecordingAuthorizer{
Wrapped: rbac.NewAuthorizer(prometheus.NewRegistry()),
}
}
options.Database = dbauthz.New(options.Database, options.Authorizer, slogtest.Make(t, nil).Leveled(slog.LevelDebug))
}
if options.DeploymentConfig == nil {
options.DeploymentConfig = DeploymentConfig(t)
Expand Down
Loading