Skip to content

Commit 7e46d24

Browse files
committed
chore: avoid depending on rbac in slim builds
1 parent 53e8e9c commit 7e46d24

File tree

9 files changed

+81
-37
lines changed

9 files changed

+81
-37
lines changed

cli/testdata/coder_users_edit-roles_--help.golden

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ USAGE:
88
OPTIONS:
99
--roles string-array
1010
A list of roles to give to the user. This removes any existing roles
11-
the user may have. The available roles are: auditor, member, owner,
12-
template-admin, user-admin.
11+
the user may have.
1312

1413
-y, --yes bool
1514
Bypass prompts.

cli/usereditroles.go

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,27 @@
11
package cli
22

33
import (
4-
"fmt"
54
"slices"
6-
"sort"
75
"strings"
86

97
"golang.org/x/xerrors"
108

119
"github.com/coder/coder/v2/cli/cliui"
12-
"github.com/coder/coder/v2/coderd/rbac"
1310
"github.com/coder/coder/v2/codersdk"
1411
"github.com/coder/serpent"
1512
)
1613

1714
func (r *RootCmd) userEditRoles() *serpent.Command {
1815
client := new(codersdk.Client)
19-
20-
roles := rbac.SiteRoles()
21-
22-
siteRoles := make([]string, 0)
23-
for _, role := range roles {
24-
siteRoles = append(siteRoles, role.Identifier.Name)
25-
}
26-
sort.Strings(siteRoles)
27-
2816
var givenRoles []string
29-
3017
cmd := &serpent.Command{
3118
Use: "edit-roles <username|user_id>",
3219
Short: "Edit a user's roles by username or id",
3320
Options: []serpent.Option{
3421
cliui.SkipPromptOption(),
3522
{
3623
Name: "roles",
37-
Description: fmt.Sprintf("A list of roles to give to the user. This removes any existing roles the user may have. The available roles are: %s.", strings.Join(siteRoles, ", ")),
24+
Description: "A list of roles to give to the user. This removes any existing roles the user may have.",
3825
Flag: "roles",
3926
Value: serpent.StringArrayOf(&givenRoles),
4027
},
@@ -52,13 +39,21 @@ func (r *RootCmd) userEditRoles() *serpent.Command {
5239
if err != nil {
5340
return xerrors.Errorf("fetch user roles: %w", err)
5441
}
42+
siteRoles, err := client.ListSiteRoles(ctx)
43+
if err != nil {
44+
return xerrors.Errorf("fetch site roles: %w", err)
45+
}
46+
siteRoleNames := make([]string, 0, len(siteRoles))
47+
for _, role := range siteRoles {
48+
siteRoleNames = append(siteRoleNames, role.Name)
49+
}
5550

5651
var selectedRoles []string
5752
if len(givenRoles) > 0 {
5853
// Make sure all of the given roles are valid site roles
5954
for _, givenRole := range givenRoles {
60-
if !slices.Contains(siteRoles, givenRole) {
61-
siteRolesPretty := strings.Join(siteRoles, ", ")
55+
if !slices.Contains(siteRoleNames, givenRole) {
56+
siteRolesPretty := strings.Join(siteRoleNames, ", ")
6257
return xerrors.Errorf("The role %s is not valid. Please use one or more of the following roles: %s\n", givenRole, siteRolesPretty)
6358
}
6459
}
@@ -67,7 +62,7 @@ func (r *RootCmd) userEditRoles() *serpent.Command {
6762
} else {
6863
selectedRoles, err = cliui.MultiSelect(inv, cliui.MultiSelectOptions{
6964
Message: "Select the roles you'd like to assign to the user",
70-
Options: siteRoles,
65+
Options: siteRoleNames,
7166
Defaults: userRoles.Roles,
7267
})
7368
if err != nil {

coderd/httpapi/authz.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//go:build !slim
2+
3+
package httpapi
4+
5+
import (
6+
"context"
7+
"net/http"
8+
9+
"github.com/coder/coder/v2/coderd/rbac"
10+
)
11+
12+
// This is defined separately in slim builds to avoid importing the rbac
13+
// package, which is a large dependency.
14+
func SetAuthzCheckRecorderHeader(ctx context.Context, rw http.ResponseWriter) {
15+
if rec, ok := rbac.GetAuthzCheckRecorder(ctx); ok {
16+
// If you're here because you saw this header in a response, and you're
17+
// trying to investigate the code, here are a couple of notable things
18+
// for you to know:
19+
// - If any of the checks are `false`, they might not represent the whole
20+
// picture. There could be additional checks that weren't performed,
21+
// because processing stopped after the failure.
22+
// - The checks are recorded by the `authzRecorder` type, which is
23+
// configured on server startup for development and testing builds.
24+
// - If this header is missing from a response, make sure the response is
25+
// being written by calling `httpapi.Write`!
26+
rw.Header().Set("x-authz-checks", rec.String())
27+
}
28+
}

coderd/httpapi/authz_slim.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//go:build slim
2+
3+
package httpapi
4+
5+
import (
6+
"context"
7+
"net/http"
8+
)
9+
10+
func SetAuthzCheckRecorderHeader(ctx context.Context, rw http.ResponseWriter) {
11+
// There's no RBAC on the agent API, so this is separately defined to
12+
// avoid importing the RBAC package, which is a large dependency.
13+
}

coderd/httpapi/httpapi.go

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"github.com/coder/websocket/wsjson"
2121

2222
"github.com/coder/coder/v2/coderd/httpapi/httpapiconstraints"
23-
"github.com/coder/coder/v2/coderd/rbac"
2423
"github.com/coder/coder/v2/coderd/tracing"
2524
"github.com/coder/coder/v2/codersdk"
2625
)
@@ -199,19 +198,7 @@ func Write(ctx context.Context, rw http.ResponseWriter, status int, response int
199198
_, span := tracing.StartSpan(ctx)
200199
defer span.End()
201200

202-
if rec, ok := rbac.GetAuthzCheckRecorder(ctx); ok {
203-
// If you're here because you saw this header in a response, and you're
204-
// trying to investigate the code, here are a couple of notable things
205-
// for you to know:
206-
// - If any of the checks are `false`, they might not represent the whole
207-
// picture. There could be additional checks that weren't performed,
208-
// because processing stopped after the failure.
209-
// - The checks are recorded by the `authzRecorder` type, which is
210-
// configured on server startup for development and testing builds.
211-
// - If this header is missing from a response, make sure the response is
212-
// being written by calling `httpapi.Write`!
213-
rw.Header().Set("x-authz-checks", rec.String())
214-
}
201+
SetAuthzCheckRecorderHeader(ctx, rw)
215202

216203
rw.Header().Set("Content-Type", "application/json; charset=utf-8")
217204
rw.WriteHeader(status)
@@ -228,9 +215,7 @@ func WriteIndent(ctx context.Context, rw http.ResponseWriter, status int, respon
228215
_, span := tracing.StartSpan(ctx)
229216
defer span.End()
230217

231-
if rec, ok := rbac.GetAuthzCheckRecorder(ctx); ok {
232-
rw.Header().Set("x-authz-checks", rec.String())
233-
}
218+
SetAuthzCheckRecorderHeader(ctx, rw)
234219

235220
rw.Header().Set("Content-Type", "application/json; charset=utf-8")
236221
rw.WriteHeader(status)

coderd/httpmw/authz.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//go:build !slim
2+
13
package httpmw
24

35
import (

coderd/rbac/no_slim.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package rbac
2+
3+
const (
4+
// This declaration protects against imports in slim builds, see
5+
// no_slim_slim.go.
6+
//nolint:revive,unused
7+
_DO_NOT_IMPORT_THIS_PACKAGE_IN_SLIM_BUILDS = "DO_NOT_IMPORT_THIS_PACKAGE_IN_SLIM_BUILDS"
8+
)

coderd/rbac/no_slim_slim.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//go:build slim
2+
3+
package rbac
4+
5+
const (
6+
// This re-declaration will result in a compilation error and is present to
7+
// prevent increasing the slim binary size by importing this package,
8+
// directly or indirectly.
9+
//
10+
// no_slim_slim.go:7:2: _DO_NOT_IMPORT_THIS_PACKAGE_IN_SLIM_BUILDS redeclared in this block
11+
// no_slim.go:4:2: other declaration of _DO_NOT_IMPORT_THIS_PACKAGE_IN_SLIM_BUILDS
12+
//nolint:revive,unused
13+
_DO_NOT_IMPORT_THIS_PACKAGE_IN_SLIM_BUILDS = "DO_NOT_IMPORT_THIS_PACKAGE_IN_SLIM_BUILDS"
14+
)

docs/reference/cli/users_edit-roles.md

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)