Skip to content

Commit 6fc1f52

Browse files
authored
refactor(coderd/httpapi): remove database, dbauthz and rbac imports (#9481)
Ref: #9380
1 parent d211594 commit 6fc1f52

File tree

5 files changed

+40
-7
lines changed

5 files changed

+40
-7
lines changed

coderd/database/dbauthz/dbauthz.go

+9
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"cdr.dev/slog"
1818
"github.com/coder/coder/v2/coderd/database"
1919
"github.com/coder/coder/v2/coderd/database/dbtime"
20+
"github.com/coder/coder/v2/coderd/httpapi/httpapiconstraints"
2021
"github.com/coder/coder/v2/coderd/rbac"
2122
"github.com/coder/coder/v2/coderd/util/slice"
2223
)
@@ -36,10 +37,18 @@ type NotAuthorizedError struct {
3637
Err error
3738
}
3839

40+
// Ensure we implement the IsUnauthorized interface.
41+
var _ httpapiconstraints.IsUnauthorizedError = (*NotAuthorizedError)(nil)
42+
3943
func (e NotAuthorizedError) Error() string {
4044
return fmt.Sprintf("unauthorized: %s", e.Err.Error())
4145
}
4246

47+
// IsUnauthorized implements the IsUnauthorized interface.
48+
func (NotAuthorizedError) IsUnauthorized() bool {
49+
return true
50+
}
51+
4352
// Unwrap will always unwrap to a sql.ErrNoRows so the API returns a 404.
4453
// So 'errors.Is(err, sql.ErrNoRows)' will always be true.
4554
func (e NotAuthorizedError) Unwrap() error {

coderd/httpapi/httpapi.go

+8-3
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@ import (
1616
"github.com/go-playground/validator/v10"
1717
"golang.org/x/xerrors"
1818

19-
"github.com/coder/coder/v2/coderd/database/dbauthz"
20-
"github.com/coder/coder/v2/coderd/rbac"
19+
"github.com/coder/coder/v2/coderd/httpapi/httpapiconstraints"
2120
"github.com/coder/coder/v2/coderd/tracing"
2221
"github.com/coder/coder/v2/codersdk"
2322
)
@@ -90,7 +89,13 @@ func Is404Error(err error) bool {
9089
if err == nil {
9190
return false
9291
}
93-
return xerrors.Is(err, sql.ErrNoRows) || dbauthz.IsNotAuthorizedError(err) || rbac.IsUnauthorizedError(err)
92+
93+
// This tests for dbauthz.IsNotAuthorizedError and rbac.IsUnauthorizedError.
94+
var unauthorized httpapiconstraints.IsUnauthorizedError
95+
if errors.As(err, &unauthorized) && unauthorized.IsUnauthorized() {
96+
return true
97+
}
98+
return xerrors.Is(err, sql.ErrNoRows)
9499
}
95100

96101
// Convenience error functions don't take contexts since their responses are
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Package httpapiconstraints contain types that can be used and implemented
2+
// across the application to return specific HTTP status codes without pulling
3+
// in large dependency trees.
4+
package httpapiconstraints
5+
6+
// IsUnauthorizedError is an interface that can be implemented in other packages
7+
// in order to return 404.
8+
type IsUnauthorizedError interface {
9+
IsUnauthorized() bool
10+
}

coderd/httpapi/queryparams.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"github.com/google/uuid"
1111
"golang.org/x/xerrors"
1212

13-
"github.com/coder/coder/v2/coderd/database"
1413
"github.com/coder/coder/v2/codersdk"
1514
)
1615

@@ -158,10 +157,10 @@ func (p *QueryParamParser) Strings(vals url.Values, def []string, queryParam str
158157
})
159158
}
160159

161-
// ValidEnum parses enum query params. Add more to the list as needed.
160+
// ValidEnum represents an enum that can be parsed and validated.
162161
type ValidEnum interface {
163-
database.ResourceType | database.AuditAction | database.BuildReason | database.UserStatus |
164-
database.WorkspaceStatus
162+
// Add more types as needed (avoid importing large dependency trees).
163+
~string
165164

166165
// Valid is required on the enum type to be used with ParseEnum.
167166
Valid() bool

coderd/rbac/error.go

+10
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import (
99
"github.com/open-policy-agent/opa/rego"
1010
"github.com/open-policy-agent/opa/topdown"
1111
"golang.org/x/xerrors"
12+
13+
"github.com/coder/coder/v2/coderd/httpapi/httpapiconstraints"
1214
)
1315

1416
const (
@@ -33,6 +35,14 @@ type UnauthorizedError struct {
3335
output rego.ResultSet
3436
}
3537

38+
// Ensure we implement the IsUnauthorized interface.
39+
var _ httpapiconstraints.IsUnauthorizedError = (*UnauthorizedError)(nil)
40+
41+
// IsUnauthorized implements the IsUnauthorized interface.
42+
func (UnauthorizedError) IsUnauthorized() bool {
43+
return true
44+
}
45+
3646
// IsUnauthorizedError is a convenience function to check if err is UnauthorizedError.
3747
// It is equivalent to errors.As(err, &UnauthorizedError{}).
3848
func IsUnauthorizedError(err error) bool {

0 commit comments

Comments
 (0)