mirror of
https://github.com/coder/coder.git
synced 2026-06-03 21:18:24 +00:00
ed90ecf00e
# Add API key allow_list for resource-scoped tokens This PR adds support for API key allow lists, enabling tokens to be scoped to specific resources. The implementation: 1. Adds a new `allow_list` field to the `CreateTokenRequest` struct, allowing clients to specify resource-specific scopes when creating API tokens 2. Implements `APIAllowListTarget` type to represent resource targets in the format `<type>:<id>` with support for wildcards 3. Adds validation and normalization logic for allow lists to handle wildcards and deduplication 4. Integrates with RBAC by creating an `APIKeyEffectiveScope` that merges API key scopes with allow list restrictions 5. Updates API documentation and TypeScript types to reflect the new functionality This feature enables creating tokens that are limited to specific resources (like workspaces or templates) by ID, making it possible to create more granular API tokens with limited access.
41 lines
1.1 KiB
Go
41 lines
1.1 KiB
Go
package codersdk_test
|
|
|
|
import (
|
|
"encoding/json"
|
|
"testing"
|
|
|
|
"github.com/google/uuid"
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/coder/coder/v2/coderd/rbac/policy"
|
|
"github.com/coder/coder/v2/codersdk"
|
|
)
|
|
|
|
func TestAPIAllowListTarget_JSONRoundTrip(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
all := codersdk.AllowAllTarget()
|
|
b, err := json.Marshal(all)
|
|
require.NoError(t, err)
|
|
require.JSONEq(t, `"*:*"`, string(b))
|
|
var rt codersdk.APIAllowListTarget
|
|
require.NoError(t, json.Unmarshal(b, &rt))
|
|
require.Equal(t, codersdk.ResourceWildcard, rt.Type)
|
|
require.Equal(t, policy.WildcardSymbol, rt.ID)
|
|
|
|
ty := codersdk.AllowTypeTarget(codersdk.ResourceWorkspace)
|
|
b, err = json.Marshal(ty)
|
|
require.NoError(t, err)
|
|
require.JSONEq(t, `"workspace:*"`, string(b))
|
|
require.NoError(t, json.Unmarshal(b, &rt))
|
|
require.Equal(t, codersdk.ResourceWorkspace, rt.Type)
|
|
require.Equal(t, policy.WildcardSymbol, rt.ID)
|
|
|
|
id := uuid.New()
|
|
res := codersdk.AllowResourceTarget(codersdk.ResourceTemplate, id)
|
|
b, err = json.Marshal(res)
|
|
require.NoError(t, err)
|
|
exp := `"template:` + id.String() + `"`
|
|
require.JSONEq(t, exp, string(b))
|
|
}
|