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 allow list to API keys#19972

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

Open
ThomasK33 wants to merge1 commit intothomask33/09-25-feat_add_allow_list_field_api_keys
base:thomask33/09-25-feat_add_allow_list_field_api_keys
Choose a base branch
Loading
fromthomask33/09-25-resource_scoped_api_keys_in_codersdk
Open
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
6 changes: 6 additions & 0 deletionscoderd/apidoc/docs.go
View file
Open in desktop

Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.

6 changes: 6 additions & 0 deletionscoderd/apidoc/swagger.json
View file
Open in desktop

Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.

7 changes: 7 additions & 0 deletionscoderd/apikey/apikey.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -12,6 +12,7 @@ import (

"github.com/coder/coder/v2/coderd/database"
"github.com/coder/coder/v2/coderd/database/dbtime"
"github.com/coder/coder/v2/coderd/rbac"
"github.com/coder/coder/v2/coderd/rbac/policy"
"github.com/coder/coder/v2/cryptorand"
)
Expand DownExpand Up@@ -102,6 +103,12 @@ func Generate(params CreateParams) (database.InsertAPIKeyParams, string, error)
}
}

iflen(params.AllowList)==0 {
params.AllowList= database.AllowList{
rbac.AllowListElement{Type:rbac.ResourceWildcard.Type,ID:policy.WildcardSymbol},
}
}

token:=fmt.Sprintf("%s-%s",keyID,keySecret)

return database.InsertAPIKeyParams{
Expand Down
6 changes: 6 additions & 0 deletionscoderd/apikey_test.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -51,6 +51,8 @@ func TestTokenCRUD(t *testing.T) {
require.Greater(t,keys[0].ExpiresAt,time.Now().Add(time.Hour*24*6))
require.Less(t,keys[0].ExpiresAt,time.Now().Add(time.Hour*24*8))
require.Equal(t,codersdk.APIKeyScopeAll,keys[0].Scope)
require.Len(t,keys[0].AllowList,1)
require.Equal(t,"*:*",keys[0].AllowList[0].String())

// no update

Expand DownExpand Up@@ -86,6 +88,8 @@ func TestTokenScoped(t *testing.T) {
require.EqualValues(t,len(keys),1)
require.Contains(t,res.Key,keys[0].ID)
require.Equal(t,keys[0].Scope,codersdk.APIKeyScopeApplicationConnect)
require.Len(t,keys[0].AllowList,1)
require.Equal(t,"*:*",keys[0].AllowList[0].String())
}

// Ensure backward-compat: when a token is created using the legacy singular
Expand DownExpand Up@@ -132,6 +136,8 @@ func TestTokenLegacySingularScopeCompat(t *testing.T) {
require.Len(t,keys,1)
require.Equal(t,tc.scope,keys[0].Scope)
require.ElementsMatch(t,keys[0].Scopes,tc.scopes)
require.Len(t,keys[0].AllowList,1)
require.Equal(t,"*:*",keys[0].AllowList[0].String())
})
}
}
Expand Down
11 changes: 11 additions & 0 deletionscoderd/database/db2sdk/db2sdk.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -51,6 +51,17 @@ func ListLazy[F any, T any](convert func(F) T) func(list []F) []T {
}
}

func APIAllowListTarget(entry rbac.AllowListElement) codersdk.APIAllowListTarget {
target := codersdk.AllowAllTarget()
if entry.Type != "" {
target.Type = codersdk.RBACResource(entry.Type)
}
if entry.ID != "" {
target.ID = entry.ID
}
return target
}
Comment on lines +54 to +63
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

I know we have to figure out the semantics of an empty string for theid, but it should not be treated as a wildcard imo.

I think we should just pass the rbac entry to the SDK as is. Do not do:target := codersdk.AllowAllTarget() as the default.


type ExternalAuthMeta struct {
Authenticated bool
ValidateError string
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@@ -1596,6 +1596,11 @@ func convertAPIKey(k database.APIKey) codersdk.APIKey {
scopes = append(scopes, codersdk.APIKeyScope(s))
}

allowList := db2sdk.List(k.AllowList, db2sdk.APIAllowListTarget)
if len(allowList) == 0 {
allowList = append(allowList, codersdk.AllowAllTarget())
Comment on lines +1599 to +1601
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

I still think we should never have an empty allow list. The db migration handles this iirc, so defaulting toall if the list is empty feels like it's hiding some other issue.

}

return codersdk.APIKey{
ID: k.ID,
UserID: k.UserID,
Expand All@@ -1608,5 +1613,6 @@ func convertAPIKey(k database.APIKey) codersdk.APIKey {
Scopes: scopes,
LifetimeSeconds: k.LifetimeSeconds,
TokenName: k.TokenName,
AllowList: allowList,
}
}
23 changes: 12 additions & 11 deletionscodersdk/apikey.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -12,17 +12,18 @@ import (

// APIKey: do not ever return the HashedSecret
typeAPIKeystruct {
IDstring`json:"id" validate:"required"`
UserID uuid.UUID`json:"user_id" validate:"required" format:"uuid"`
LastUsed time.Time`json:"last_used" validate:"required" format:"date-time"`
ExpiresAt time.Time`json:"expires_at" validate:"required" format:"date-time"`
CreatedAt time.Time`json:"created_at" validate:"required" format:"date-time"`
UpdatedAt time.Time`json:"updated_at" validate:"required" format:"date-time"`
LoginTypeLoginType`json:"login_type" validate:"required" enums:"password,github,oidc,token"`
ScopeAPIKeyScope`json:"scope" enums:"all,application_connect"`// Deprecated: use Scopes instead.
Scopes []APIKeyScope`json:"scopes"`
TokenNamestring`json:"token_name" validate:"required"`
LifetimeSecondsint64`json:"lifetime_seconds" validate:"required"`
IDstring`json:"id" validate:"required"`
UserID uuid.UUID`json:"user_id" validate:"required" format:"uuid"`
LastUsed time.Time`json:"last_used" validate:"required" format:"date-time"`
ExpiresAt time.Time`json:"expires_at" validate:"required" format:"date-time"`
CreatedAt time.Time`json:"created_at" validate:"required" format:"date-time"`
UpdatedAt time.Time`json:"updated_at" validate:"required" format:"date-time"`
LoginTypeLoginType`json:"login_type" validate:"required" enums:"password,github,oidc,token"`
ScopeAPIKeyScope`json:"scope" enums:"all,application_connect"`// Deprecated: use Scopes instead.
Scopes []APIKeyScope`json:"scopes"`
TokenNamestring`json:"token_name" validate:"required"`
LifetimeSecondsint64`json:"lifetime_seconds" validate:"required"`
AllowList []APIAllowListTarget`json:"allow_list"`
}

// LoginType is the type of login used to create the API key.
Expand Down
33 changes: 20 additions & 13 deletionsdocs/reference/api/schemas.md
View file
Open in desktop

Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.

106 changes: 84 additions & 22 deletionsdocs/reference/api/users.md
View file
Open in desktop

Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.

1 change: 1 addition & 0 deletionssite/src/api/typesGenerated.ts
View file
Open in desktop

Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.

2 changes: 2 additions & 0 deletionssite/src/testHelpers/entities.ts
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -85,6 +85,7 @@ export const MockToken: TypesGen.APIKeyWithOwner = {
login_type:"token",
scope:"all",
scopes:["coder:all"],
allow_list:[{type:"*",id:"*"}],
lifetime_seconds:2592000,
token_name:"token-one",
username:"admin",
Expand All@@ -102,6 +103,7 @@ export const MockTokens: TypesGen.APIKeyWithOwner[] = [
login_type:"token",
scope:"all",
scopes:["coder:all"],
allow_list:[{type:"*",id:"*"}],
lifetime_seconds:2592000,
token_name:"token-two",
username:"admin",
Expand Down
Loading

[8]ページ先頭

©2009-2025 Movatter.jp