Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

feat: add legacy API key scope compatibility#20021

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Closed
Show file tree
Hide file tree
Changes fromall commits
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
60 changes: 60 additions & 0 deletionscoderd/apikey_legacy_test.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
package coderd_test

import (
"context"
"testing"

"github.com/stretchr/testify/require"

"github.com/coder/coder/v2/coderd/coderdtest"
"github.com/coder/coder/v2/coderd/util/ptr"
"github.com/coder/coder/v2/codersdk"
"github.com/coder/coder/v2/testutil"
)

funcTestLegacyTokenCompatibility(t*testing.T) {
t.Parallel()

t.Run("exposes legacy scope defaults",func(t*testing.T) {
t.Parallel()

client,db:=coderdtest.NewWithDatabase(t,nil)
first:=coderdtest.CreateFirstUser(t,client)
legacyKey,_:=coderdtest.LegacyToken(t,db,first.UserID)

ctx,cancel:=context.WithTimeout(t.Context(),testutil.WaitShort)
defercancel()

keys,err:=client.Tokens(ctx,codersdk.Me, codersdk.TokensFilter{})
require.NoErrorf(t,err,"tokens request failed: %v",err)
require.Len(t,keys,1)

key:=keys[0]
require.Equal(t,legacyKey.TokenName,key.TokenName)
require.Equal(t,codersdk.APIKeyScopeAll,key.Scope)
require.Contains(t,key.Scopes,codersdk.APIKeyScopeCoderAll)
require.Len(t,key.AllowList,1)
require.Equal(t,"*:*",key.AllowList[0].String())
})

t.Run("update via legacy scope field",func(t*testing.T) {
t.Parallel()

client,db:=coderdtest.NewWithDatabase(t,nil)
first:=coderdtest.CreateFirstUser(t,client)
legacyKey,_:=coderdtest.LegacyToken(t,db,first.UserID)

ctx,cancel:=context.WithTimeout(t.Context(),testutil.WaitShort)
defercancel()

_,err:=client.UpdateToken(ctx,codersdk.Me,legacyKey.TokenName, codersdk.UpdateTokenRequest{
Scope:ptr.Ref(codersdk.APIKeyScopeApplicationConnect),
})
require.NoErrorf(t,err,"update token failed: %v",err)

refreshed,err:=client.APIKeyByName(ctx,codersdk.Me,legacyKey.TokenName)
require.NoErrorf(t,err,"fetch token failed: %v",err)
require.Equal(t,codersdk.APIKeyScopeApplicationConnect,refreshed.Scope)
require.Contains(t,refreshed.Scopes,codersdk.APIKeyScopeCoderApplicationConnect)
})
}
31 changes: 31 additions & 0 deletionscoderd/coderdtest/legacy_tokens.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
package coderdtest

import (
"fmt"
"testing"

"github.com/google/uuid"

"github.com/coder/coder/v2/coderd/database"
"github.com/coder/coder/v2/coderd/database/dbgen"
)

// LegacyToken inserts an API key fixture whose database row mimics the
// pre-scopes migration shape where scopes/allow_list columns were empty. The
// returned API key behaves like a legacy token that should expand to coder:all.
func LegacyToken(t testing.TB, db database.Store, userID uuid.UUID) (database.APIKey, string) {
t.Helper()

tokenName := fmt.Sprintf("legacy-token-%s", uuid.NewString()[:8])

key, secret := dbgen.APIKey(t, db, database.APIKey{
UserID: userID,
TokenName: tokenName,
LoginType: database.LoginTypeToken,
}, func(params *database.InsertAPIKeyParams) {
params.Scopes = database.APIKeyScopes{}
params.AllowList = database.AllowList{}
})

return key, secret
}
39 changes: 39 additions & 0 deletionscoderd/httpmw/apikey_test.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -19,6 +19,7 @@ import (
"golang.org/x/exp/slices"
"golang.org/x/oauth2"

"github.com/coder/coder/v2/coderd/coderdtest"
"github.com/coder/coder/v2/coderd/database"
"github.com/coder/coder/v2/coderd/database/dbauthz"
"github.com/coder/coder/v2/coderd/database/dbgen"
Expand All@@ -27,6 +28,7 @@ import (
"github.com/coder/coder/v2/coderd/httpapi"
"github.com/coder/coder/v2/coderd/httpmw"
"github.com/coder/coder/v2/coderd/rbac"
"github.com/coder/coder/v2/coderd/rbac/policy"
"github.com/coder/coder/v2/codersdk"
"github.com/coder/coder/v2/cryptorand"
"github.com/coder/coder/v2/testutil"
Expand DownExpand Up@@ -79,6 +81,43 @@ func TestAPIKey(t *testing.T) {
})
})

t.Run("LegacyScopeExpandsToAll", func(t *testing.T) {
t.Parallel()
db, _ := dbtestutil.NewDB(t)
user := dbgen.User(t, db, database.User{})
_ = dbgen.UserLink(t, db, database.UserLink{
UserID: user.ID,
LoginType: database.LoginTypeToken,
})

_, token := coderdtest.LegacyToken(t, db, user.ID)

assertHandler := http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
auth, ok := httpmw.UserAuthorizationOptional(r.Context())
require.True(t, ok)
scope, err := auth.Scope.Expand()
require.NoError(t, err)
require.Len(t, scope.AllowIDList, 1)
require.Equal(t, policy.WildcardSymbol, scope.AllowIDList[0].ID)
require.Equal(t, policy.WildcardSymbol, scope.AllowIDList[0].Type)

httpapi.Write(context.Background(), rw, http.StatusOK, codersdk.Response{Message: "legacy ok"})
})

r := httptest.NewRequest("GET", "/", nil)
r.Header.Set(codersdk.SessionTokenHeader, token)
rw := httptest.NewRecorder()

httpmw.ExtractAPIKeyMW(httpmw.ExtractAPIKeyConfig{
DB: db,
RedirectToLogin: false,
})(assertHandler).ServeHTTP(rw, r)

res := rw.Result()
defer res.Body.Close()
require.Equal(t, http.StatusOK, res.StatusCode)
})

t.Run("NoCookie", func(t *testing.T) {
t.Parallel()
var (
Expand Down
6 changes: 6 additions & 0 deletionscoderd/users.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -1584,6 +1584,12 @@ func convertAPIKey(k database.APIKey) codersdk.APIKey {
// "application_connect". Continue to return those for clients even
// though the database stores canonical values (e.g. "coder:all")
// and may include low-level scopes.
if len(k.Scopes) == 0 {
k.Scopes = database.APIKeyScopes{database.ApiKeyScopeCoderAll}
}
if len(k.AllowList) == 0 {
k.AllowList = database.AllowList{rbac.AllowListAll()}
}
var legacyScope codersdk.APIKeyScope
if k.Scopes.Has(database.ApiKeyScopeCoderApplicationConnect) {
legacyScope = codersdk.APIKeyScopeApplicationConnect
Expand Down
Loading

[8]ページ先頭

©2009-2025 Movatter.jp