Files
coder/coderd/apikey_scopes_validation_test.go
Thomas Kosiewski d0db9ec88f feat: add multi-scope support to API keys (#19917)
# Canonicalize API Key Scopes

This PR introduces canonical API key scopes with a `coder:` namespace prefix to avoid collisions with low-level resource:action names. It:

1. Renames special API key scopes in the database:
   - `all` → `coder:all`
   - `application_connect` → `coder:application_connect`

2. Adds support for a new `scopes` field in the API key creation request, allowing multiple scopes to be specified while maintaining backward compatibility with the singular `scope` field.

3. Updates the API documentation to reflect these changes, including the new endpoint for listing public API key scopes.

4. Ensures backward compatibility by mapping between legacy and canonical scope names in relevant code paths.
2025-09-26 11:56:34 +02:00

65 lines
1.7 KiB
Go

package coderd_test
import (
"context"
"testing"
"github.com/stretchr/testify/require"
"github.com/coder/coder/v2/coderd/coderdtest"
"github.com/coder/coder/v2/codersdk"
"github.com/coder/coder/v2/testutil"
)
func TestTokenCreation_ScopeValidation(t *testing.T) {
t.Parallel()
cases := []struct {
name string
scope codersdk.APIKeyScope
wantErr bool
}{
{name: "AllowsPublicLowLevelScope", scope: "workspace:read", wantErr: false},
{name: "RejectsInternalOnlyScope", scope: "debug_info:read", wantErr: true},
{name: "AllowsLegacyScopes", scope: "application_connect", wantErr: false},
{name: "AllowsLegacyScopes2", scope: "all", wantErr: false},
{name: "AllowsCanonicalSpecialScope", scope: "coder:all", wantErr: false},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
client := coderdtest.New(t, nil)
_ = coderdtest.CreateFirstUser(t, client)
ctx, cancel := context.WithTimeout(t.Context(), testutil.WaitShort)
defer cancel()
resp, err := client.CreateToken(ctx, codersdk.Me, codersdk.CreateTokenRequest{Scope: tc.scope})
if tc.wantErr {
require.Error(t, err)
return
}
require.NoError(t, err)
require.NotEmpty(t, resp.Key)
// Fetch and verify the stored scopes match expectation.
keys, err := client.Tokens(ctx, codersdk.Me, codersdk.TokensFilter{})
require.NoError(t, err)
require.Len(t, keys, 1)
// Normalize legacy singular scopes to canonical coder:* values.
expected := tc.scope
switch tc.scope {
case codersdk.APIKeyScopeAll:
expected = codersdk.APIKeyScopeCoderAll
case codersdk.APIKeyScopeApplicationConnect:
expected = codersdk.APIKeyScopeCoderApplicationConnect
}
require.Contains(t, keys[0].Scopes, expected)
})
}
}