-
Notifications
You must be signed in to change notification settings - Fork 890
chore: avoid depending on rbac in slim builds #17959
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,27 @@ | ||
package cli | ||
|
||
import ( | ||
"fmt" | ||
"slices" | ||
"sort" | ||
"strings" | ||
|
||
"golang.org/x/xerrors" | ||
|
||
"github.com/coder/coder/v2/cli/cliui" | ||
"github.com/coder/coder/v2/coderd/rbac" | ||
"github.com/coder/coder/v2/codersdk" | ||
"github.com/coder/serpent" | ||
) | ||
|
||
func (r *RootCmd) userEditRoles() *serpent.Command { | ||
client := new(codersdk.Client) | ||
|
||
roles := rbac.SiteRoles() | ||
|
||
siteRoles := make([]string, 0) | ||
for _, role := range roles { | ||
siteRoles = append(siteRoles, role.Identifier.Name) | ||
} | ||
sort.Strings(siteRoles) | ||
|
||
var givenRoles []string | ||
|
||
cmd := &serpent.Command{ | ||
Use: "edit-roles <username|user_id>", | ||
Short: "Edit a user's roles by username or id", | ||
Options: []serpent.Option{ | ||
cliui.SkipPromptOption(), | ||
{ | ||
Name: "roles", | ||
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, ", ")), | ||
Description: "A list of roles to give to the user. This removes any existing roles the user may have.", | ||
Flag: "roles", | ||
Value: serpent.StringArrayOf(&givenRoles), | ||
}, | ||
|
@@ -52,13 +39,21 @@ func (r *RootCmd) userEditRoles() *serpent.Command { | |
if err != nil { | ||
return xerrors.Errorf("fetch user roles: %w", err) | ||
} | ||
siteRoles, err := client.ListSiteRoles(ctx) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
EDIT: See below. |
||
if err != nil { | ||
return xerrors.Errorf("fetch site roles: %w", err) | ||
} | ||
siteRoleNames := make([]string, 0, len(siteRoles)) | ||
for _, role := range siteRoles { | ||
siteRoleNames = append(siteRoleNames, role.Name) | ||
} | ||
|
||
var selectedRoles []string | ||
if len(givenRoles) > 0 { | ||
// Make sure all of the given roles are valid site roles | ||
for _, givenRole := range givenRoles { | ||
if !slices.Contains(siteRoles, givenRole) { | ||
siteRolesPretty := strings.Join(siteRoles, ", ") | ||
if !slices.Contains(siteRoleNames, givenRole) { | ||
siteRolesPretty := strings.Join(siteRoleNames, ", ") | ||
return xerrors.Errorf("The role %s is not valid. Please use one or more of the following roles: %s\n", givenRole, siteRolesPretty) | ||
} | ||
} | ||
|
@@ -67,7 +62,7 @@ func (r *RootCmd) userEditRoles() *serpent.Command { | |
} else { | ||
selectedRoles, err = cliui.MultiSelect(inv, cliui.MultiSelectOptions{ | ||
Message: "Select the roles you'd like to assign to the user", | ||
Options: siteRoles, | ||
Options: siteRoleNames, | ||
Defaults: userRoles.Roles, | ||
}) | ||
if err != nil { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,9 @@ | ||
//go:build slim | ||
|
||
package database | ||
|
||
const ( | ||
// This declaration protects against imports in slim builds, see | ||
// no_slim_slim.go. | ||
//nolint:revive,unused | ||
_DO_NOT_IMPORT_THIS_PACKAGE_IN_SLIM_BUILDS = "DO_NOT_IMPORT_THIS_PACKAGE_IN_SLIM_BUILDS" | ||
// This line fails to compile, preventing this package from being imported | ||
// in slim builds. | ||
_DO_NOT_IMPORT_THIS_PACKAGE_IN_SLIM_BUILDS = _DO_NOT_IMPORT_THIS_PACKAGE_IN_SLIM_BUILDS | ||
) |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
//go:build !slim | ||
|
||
package httpapi | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
|
||
"github.com/coder/coder/v2/coderd/rbac" | ||
) | ||
|
||
// This is defined separately in slim builds to avoid importing the rbac | ||
// package, which is a large dependency. | ||
func SetAuthzCheckRecorderHeader(ctx context.Context, rw http.ResponseWriter) { | ||
if rec, ok := rbac.GetAuthzCheckRecorder(ctx); ok { | ||
// If you're here because you saw this header in a response, and you're | ||
// trying to investigate the code, here are a couple of notable things | ||
// for you to know: | ||
// - If any of the checks are `false`, they might not represent the whole | ||
// picture. There could be additional checks that weren't performed, | ||
// because processing stopped after the failure. | ||
// - The checks are recorded by the `authzRecorder` type, which is | ||
// configured on server startup for development and testing builds. | ||
// - If this header is missing from a response, make sure the response is | ||
// being written by calling `httpapi.Write`! | ||
rw.Header().Set("x-authz-checks", rec.String()) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
//go:build slim | ||
|
||
package httpapi | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
) | ||
|
||
func SetAuthzCheckRecorderHeader(ctx context.Context, rw http.ResponseWriter) { | ||
// There's no RBAC on the agent API, so this is separately defined to | ||
// avoid importing the RBAC package, which is a large dependency. | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
//go:build !slim | ||
|
||
package httpmw | ||
|
||
import ( | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
//go:build slim | ||
|
||
package rbac | ||
|
||
const ( | ||
// This line fails to compile, preventing this package from being imported | ||
// in slim builds. | ||
_DO_NOT_IMPORT_THIS_PACKAGE_IN_SLIM_BUILDS = _DO_NOT_IMPORT_THIS_PACKAGE_IN_SLIM_BUILDS | ||
) |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Uh oh!
There was an error while loading. Please reload this page.