Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
Prev Previous commit
Next Next commit
refactor(coderd/apikey): say clone with slices.Clone and fix a comment
The test cloned params.Scopes with the pre-1.21 append idiom; the repo
already uses slices.Clone in 59 other places, and it names the operation
instead of leaving the reader to recognize it.

The comment above the canonicalization loop said Generate leaves params
as the caller passed it, which is not true: it also assigns ExpiresAt,
LifetimeSeconds and AllowList on its local copy. State the property that
actually matters, which is that params.Scopes shares a backing array with
the caller, so canonicalizing in place would rewrite the caller's slice.
  • Loading branch information
BobbyHo committed Aug 24, 2026
commit 5add97a5af420c68eddecd1e6a432ef151eddd44
6 changes: 3 additions & 3 deletions coderd/apikey/apikey.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,9 @@ func Generate(params CreateParams) (database.InsertAPIKeyParams, string, error)
}

// Callers may pass an alias spelling such as "all", which is not an
Comment thread
BobbyHo marked this conversation as resolved.
Outdated
// api_key_scope enum member and so would fail the validity check. Build a new
// slice rather than canonicalizing in place, so params is left as the caller
// passed it.
// api_key_scope enum member and so would fail the validity check.
// params.Scopes shares its backing array with the caller, so canonicalize
// into a new slice rather than in place.
scopes := make(database.APIKeyScopes, 0, len(requested))
for _, s := range requested {
canonical := database.APIKeyScope(rbac.CanonicalScopeName(rbac.ScopeName(s)))
Expand Down
3 changes: 2 additions & 1 deletion coderd/apikey/apikey_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package apikey_test

import (
"slices"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -227,7 +228,7 @@ func TestGenerateScopeNames(t *testing.T) {
params.LoginType = database.LoginTypePassword
params.DefaultLifetime = time.Hour

requested := append(database.APIKeyScopes(nil), params.Scopes...)
requested := slices.Clone(params.Scopes)

key, _, err := apikey.Generate(params)
if tc.fail {
Expand Down