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

Commitab61e4e

Browse files
committed
feat: add backwards compatibility for legacy API keys
This change introduces backwards compatibility for API keys createdbefore the scopes and allow-lists feature.Previously, these legacy keys had empty scope and allow-listdefinitions in the database. They are now dynamically interpreted ashaving `coder:all` scope, granting them full access. This ensures thatold, unscoped tokens continue to function as they did before theintroduction of fine-grained permissions.
1 parent4c9762e commitab61e4e

File tree

4 files changed

+136
-0
lines changed

4 files changed

+136
-0
lines changed

‎coderd/apikey_legacy_test.go‎

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package coderd_test
2+
3+
import (
4+
"context"
5+
"testing"
6+
7+
"github.com/stretchr/testify/require"
8+
9+
"github.com/coder/coder/v2/coderd/coderdtest"
10+
"github.com/coder/coder/v2/coderd/util/ptr"
11+
"github.com/coder/coder/v2/codersdk"
12+
"github.com/coder/coder/v2/testutil"
13+
)
14+
15+
funcTestLegacyTokenCompatibility(t*testing.T) {
16+
t.Parallel()
17+
18+
t.Run("exposes legacy scope defaults",func(t*testing.T) {
19+
t.Parallel()
20+
21+
client,db:=coderdtest.NewWithDatabase(t,nil)
22+
first:=coderdtest.CreateFirstUser(t,client)
23+
legacyKey,_:=coderdtest.LegacyToken(t,db,first.UserID)
24+
25+
ctx,cancel:=context.WithTimeout(t.Context(),testutil.WaitShort)
26+
defercancel()
27+
28+
keys,err:=client.Tokens(ctx,codersdk.Me, codersdk.TokensFilter{})
29+
require.NoErrorf(t,err,"tokens request failed: %v",err)
30+
require.Len(t,keys,1)
31+
32+
key:=keys[0]
33+
require.Equal(t,legacyKey.TokenName,key.TokenName)
34+
require.Equal(t,codersdk.APIKeyScopeAll,key.Scope)
35+
require.Contains(t,key.Scopes,codersdk.APIKeyScopeCoderAll)
36+
require.Len(t,key.AllowList,1)
37+
require.Equal(t,"*:*",key.AllowList[0].String())
38+
})
39+
40+
t.Run("update via legacy scope field",func(t*testing.T) {
41+
t.Parallel()
42+
43+
client,db:=coderdtest.NewWithDatabase(t,nil)
44+
first:=coderdtest.CreateFirstUser(t,client)
45+
legacyKey,_:=coderdtest.LegacyToken(t,db,first.UserID)
46+
47+
ctx,cancel:=context.WithTimeout(t.Context(),testutil.WaitShort)
48+
defercancel()
49+
50+
_,err:=client.UpdateToken(ctx,codersdk.Me,legacyKey.TokenName, codersdk.UpdateTokenRequest{
51+
Scope:ptr.Ref(codersdk.APIKeyScopeApplicationConnect),
52+
})
53+
require.NoErrorf(t,err,"update token failed: %v",err)
54+
55+
refreshed,err:=client.APIKeyByName(ctx,codersdk.Me,legacyKey.TokenName)
56+
require.NoErrorf(t,err,"fetch token failed: %v",err)
57+
require.Equal(t,codersdk.APIKeyScopeApplicationConnect,refreshed.Scope)
58+
require.Contains(t,refreshed.Scopes,codersdk.APIKeyScopeCoderApplicationConnect)
59+
})
60+
}

‎coderd/coderdtest/legacy_tokens.go‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package coderdtest
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/google/uuid"
8+
9+
"github.com/coder/coder/v2/coderd/database"
10+
"github.com/coder/coder/v2/coderd/database/dbgen"
11+
)
12+
13+
// LegacyToken inserts an API key fixture whose database row mimics the
14+
// pre-scopes migration shape where scopes/allow_list columns were empty. The
15+
// returned API key behaves like a legacy token that should expand to coder:all.
16+
funcLegacyToken(t testing.TB,db database.Store,userID uuid.UUID) (database.APIKey,string) {
17+
t.Helper()
18+
19+
tokenName:=fmt.Sprintf("legacy-token-%s",uuid.NewString()[:8])
20+
21+
key,secret:=dbgen.APIKey(t,db, database.APIKey{
22+
UserID:userID,
23+
TokenName:tokenName,
24+
LoginType:database.LoginTypeToken,
25+
},func(params*database.InsertAPIKeyParams) {
26+
params.Scopes= database.APIKeyScopes{}
27+
params.AllowList= database.AllowList{}
28+
})
29+
30+
returnkey,secret
31+
}

‎coderd/httpmw/apikey_test.go‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"golang.org/x/exp/slices"
2020
"golang.org/x/oauth2"
2121

22+
"github.com/coder/coder/v2/coderd/coderdtest"
2223
"github.com/coder/coder/v2/coderd/database"
2324
"github.com/coder/coder/v2/coderd/database/dbauthz"
2425
"github.com/coder/coder/v2/coderd/database/dbgen"
@@ -27,6 +28,7 @@ import (
2728
"github.com/coder/coder/v2/coderd/httpapi"
2829
"github.com/coder/coder/v2/coderd/httpmw"
2930
"github.com/coder/coder/v2/coderd/rbac"
31+
"github.com/coder/coder/v2/coderd/rbac/policy"
3032
"github.com/coder/coder/v2/codersdk"
3133
"github.com/coder/coder/v2/cryptorand"
3234
"github.com/coder/coder/v2/testutil"
@@ -79,6 +81,43 @@ func TestAPIKey(t *testing.T) {
7981
})
8082
})
8183

84+
t.Run("LegacyScopeExpandsToAll",func(t*testing.T) {
85+
t.Parallel()
86+
db,_:=dbtestutil.NewDB(t)
87+
user:=dbgen.User(t,db, database.User{})
88+
_=dbgen.UserLink(t,db, database.UserLink{
89+
UserID:user.ID,
90+
LoginType:database.LoginTypeToken,
91+
})
92+
93+
_,token:=coderdtest.LegacyToken(t,db,user.ID)
94+
95+
assertHandler:=http.HandlerFunc(func(rw http.ResponseWriter,r*http.Request) {
96+
auth,ok:=httpmw.UserAuthorizationOptional(r.Context())
97+
require.True(t,ok)
98+
scope,err:=auth.Scope.Expand()
99+
require.NoError(t,err)
100+
require.Len(t,scope.AllowIDList,1)
101+
require.Equal(t,policy.WildcardSymbol,scope.AllowIDList[0].ID)
102+
require.Equal(t,policy.WildcardSymbol,scope.AllowIDList[0].Type)
103+
104+
httpapi.Write(context.Background(),rw,http.StatusOK, codersdk.Response{Message:"legacy ok"})
105+
})
106+
107+
r:=httptest.NewRequest("GET","/",nil)
108+
r.Header.Set(codersdk.SessionTokenHeader,token)
109+
rw:=httptest.NewRecorder()
110+
111+
httpmw.ExtractAPIKeyMW(httpmw.ExtractAPIKeyConfig{
112+
DB:db,
113+
RedirectToLogin:false,
114+
})(assertHandler).ServeHTTP(rw,r)
115+
116+
res:=rw.Result()
117+
deferres.Body.Close()
118+
require.Equal(t,http.StatusOK,res.StatusCode)
119+
})
120+
82121
t.Run("NoCookie",func(t*testing.T) {
83122
t.Parallel()
84123
var (

‎coderd/users.go‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1584,6 +1584,12 @@ func convertAPIKey(k database.APIKey) codersdk.APIKey {
15841584
// "application_connect". Continue to return those for clients even
15851585
// though the database stores canonical values (e.g. "coder:all")
15861586
// and may include low-level scopes.
1587+
iflen(k.Scopes)==0 {
1588+
k.Scopes= database.APIKeyScopes{database.ApiKeyScopeCoderAll}
1589+
}
1590+
iflen(k.AllowList)==0 {
1591+
k.AllowList= database.AllowList{database.AllowListTarget{}}
1592+
}
15871593
varlegacyScope codersdk.APIKeyScope
15881594
ifk.Scopes.Has(database.ApiKeyScopeCoderApplicationConnect) {
15891595
legacyScope=codersdk.APIKeyScopeApplicationConnect

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp