Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 57f1735

Browse files
committed
review improvements
Change-Id: I7868299308cdf4fca36092025bf2692cfe652640 Signed-off-by: Thomas Kosiewski <[email protected]>
1 parent b5ed0bd commit 57f1735

14 files changed

+66
-74
lines changed

coderd/coderd.go

+2-12
Original file line numberDiff line numberDiff line change
@@ -804,11 +804,6 @@ func New(options *Options) *API {
804804
DB: options.Database,
805805
Optional: false,
806806
})
807-
// Same as above but optional
808-
workspaceAgentInfoOptional := httpmw.ExtractWorkspaceAgentAndLatestBuild(httpmw.ExtractWorkspaceAgentAndLatestBuildConfig{
809-
DB: options.Database,
810-
Optional: true,
811-
})
812807

813808
// API rate limit middleware. The counter is local and not shared between
814809
// replicas or instances of this middleware.
@@ -1029,7 +1024,6 @@ func New(options *Options) *API {
10291024
r.Route("/external-auth", func(r chi.Router) {
10301025
r.Use(
10311026
apiKeyMiddleware,
1032-
workspaceAgentInfoOptional,
10331027
)
10341028
// Get without a specific external auth ID will return all external auths.
10351029
r.Get("/", api.listUserExternalAuths)
@@ -1265,12 +1259,8 @@ func New(options *Options) *API {
12651259
r.Get("/", api.workspaceByOwnerAndName)
12661260
r.Get("/builds/{buildnumber}", api.workspaceBuildByBuildNumber)
12671261
})
1268-
r.With(
1269-
workspaceAgentInfoOptional,
1270-
).Get("/gitsshkey", api.gitSSHKey)
1271-
r.With(
1272-
workspaceAgentInfoOptional,
1273-
).Put("/gitsshkey", api.regenerateGitSSHKey)
1262+
r.Get("/gitsshkey", api.gitSSHKey)
1263+
r.Put("/gitsshkey", api.regenerateGitSSHKey)
12741264
r.Route("/notifications", func(r chi.Router) {
12751265
r.Route("/preferences", func(r chi.Router) {
12761266
r.Get("/", api.userNotificationPreferences)

coderd/database/dbauthz/dbauthz_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -3988,7 +3988,7 @@ func (s *MethodTestSuite) TestSystemFunctions() {
39883988
check.Args(database.InsertWorkspaceAgentParams{
39893989
ID: uuid.New(),
39903990
Name: "dev",
3991-
APIKeyScope: database.ApiKeyScopeEnumDefault,
3991+
APIKeyScope: database.AgentKeyScopeEnumAll,
39923992
}).Asserts(rbac.ResourceSystem, policy.ActionCreate)
39933993
}))
39943994
s.Run("InsertWorkspaceApp", s.Subtest(func(db database.Store, check *expects) {

coderd/database/dbgen/dbgen.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ func WorkspaceAgent(t testing.TB, db database.Store, orig database.WorkspaceAgen
210210
MOTDFile: takeFirst(orig.TroubleshootingURL, ""),
211211
DisplayApps: append([]database.DisplayApp{}, orig.DisplayApps...),
212212
DisplayOrder: takeFirst(orig.DisplayOrder, 1),
213-
APIKeyScope: takeFirst(orig.APIKeyScope, database.ApiKeyScopeEnumDefault),
213+
APIKeyScope: takeFirst(orig.APIKeyScope, database.AgentKeyScopeEnumAll),
214214
})
215215
require.NoError(t, err, "insert workspace agent")
216216
return agt

coderd/database/dump.sql

+7-7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/migrations/000320_add_api_key_scope_to_workspace_agents.down.sql

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ ALTER TABLE workspace_agents
33
DROP COLUMN IF EXISTS api_key_scope;
44

55
-- Drop the enum type for API key scope
6-
DROP TYPE IF EXISTS api_key_scope_enum;
6+
DROP TYPE IF EXISTS agent_key_scope_enum;
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
-- Create the enum type for API key scope
2-
CREATE TYPE api_key_scope_enum AS ENUM ('default', 'no_user_data');
2+
CREATE TYPE agent_key_scope_enum AS ENUM ('all', 'no_user_data');
33

44
-- Add the api_key_scope column to the workspace_agents table
5-
-- It defaults to 'default' to maintain existing behavior for current agents.
5+
-- It defaults to 'all' to maintain existing behavior for current agents.
66
ALTER TABLE workspace_agents
7-
ADD COLUMN api_key_scope api_key_scope_enum NOT NULL DEFAULT 'default';
7+
ADD COLUMN api_key_scope agent_key_scope_enum NOT NULL DEFAULT 'all';
88

99
-- Add a comment explaining the purpose of the column
10-
COMMENT ON COLUMN workspace_agents.api_key_scope IS 'Defines the scope of the API key associated with the agent. ''default'' allows access to everything, ''no_user_data'' restricts it to exclude user data.';
10+
COMMENT ON COLUMN workspace_agents.api_key_scope IS 'Defines the scope of the API key associated with the agent. ''all'' allows access to everything, ''no_user_data'' restricts it to exclude user data.';

coderd/database/models.go

+24-24
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/queries.sql.go

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/externalauth_test.go

+13-13
Original file line numberDiff line numberDiff line change
@@ -713,7 +713,7 @@ func TestExternalAuthCallback(t *testing.T) {
713713
apiKeyScope string
714714
expectsError bool
715715
}{
716-
{apiKeyScope: "default", expectsError: false},
716+
{apiKeyScope: "all", expectsError: false},
717717
{apiKeyScope: "no_user_data", expectsError: true},
718718
} {
719719
t.Run(tt.apiKeyScope, func(t *testing.T) {
@@ -746,8 +746,16 @@ func TestExternalAuthCallback(t *testing.T) {
746746
token, err := agentClient.ExternalAuth(t.Context(), agentsdk.ExternalAuthRequest{
747747
Match: "github.com/asd/asd",
748748
})
749-
require.NoError(t, err)
750-
require.NotEmpty(t, token.URL)
749+
if tt.expectsError {
750+
require.Error(t, err)
751+
var sdkErr *codersdk.Error
752+
require.ErrorAs(t, err, &sdkErr)
753+
require.Equal(t, http.StatusForbidden, sdkErr.StatusCode())
754+
return
755+
} else {
756+
require.NoError(t, err)
757+
require.NotEmpty(t, token.URL)
758+
}
751759

752760
// Start waiting for the token callback...
753761
tokenChan := make(chan agentsdk.ExternalAuthResponse, 1)
@@ -756,13 +764,8 @@ func TestExternalAuthCallback(t *testing.T) {
756764
Match: "github.com/asd/asd",
757765
Listen: true,
758766
})
759-
if tt.expectsError {
760-
assert.Error(t, err)
761-
close(tokenChan)
762-
} else {
763-
assert.NoError(t, err)
764-
tokenChan <- token
765-
}
767+
assert.NoError(t, err)
768+
tokenChan <- token
766769
}()
767770

768771
time.Sleep(250 * time.Millisecond)
@@ -771,9 +774,6 @@ func TestExternalAuthCallback(t *testing.T) {
771774
require.Equal(t, http.StatusTemporaryRedirect, resp.StatusCode)
772775

773776
token = <-tokenChan
774-
if tt.expectsError {
775-
return
776-
}
777777
require.Equal(t, "access_token", token.Username)
778778

779779
token, err = agentClient.ExternalAuth(t.Context(), agentsdk.ExternalAuthRequest{

coderd/gitsshkey_test.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package coderd_test
22

33
import (
44
"context"
5+
"net/http"
56
"testing"
67

78
"github.com/google/uuid"
@@ -12,6 +13,7 @@ import (
1213
"github.com/coder/coder/v2/coderd/coderdtest"
1314
"github.com/coder/coder/v2/coderd/database"
1415
"github.com/coder/coder/v2/coderd/gitsshkey"
16+
"github.com/coder/coder/v2/codersdk"
1517
"github.com/coder/coder/v2/codersdk/agentsdk"
1618
"github.com/coder/coder/v2/provisioner/echo"
1719
"github.com/coder/coder/v2/testutil"
@@ -134,7 +136,7 @@ func TestAgentGitSSHKey_APIKeyScopes(t *testing.T) {
134136
apiKeyScope string
135137
expectError bool
136138
}{
137-
{apiKeyScope: "default", expectError: false},
139+
{apiKeyScope: "all", expectError: false},
138140
{apiKeyScope: "no_user_data", expectError: true},
139141
} {
140142
t.Run(tt.apiKeyScope, func(t *testing.T) {
@@ -165,6 +167,9 @@ func TestAgentGitSSHKey_APIKeyScopes(t *testing.T) {
165167

166168
if tt.expectError {
167169
require.Error(t, err)
170+
var sdkErr *codersdk.Error
171+
require.ErrorAs(t, err, &sdkErr)
172+
require.Equal(t, http.StatusForbidden, sdkErr.StatusCode())
168173
} else {
169174
require.NoError(t, err)
170175
}

coderd/httpmw/workspaceagent.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ func ExtractWorkspaceAgentAndLatestBuild(opts ExtractWorkspaceAgentAndLatestBuil
118118
OwnerID: row.WorkspaceTable.OwnerID,
119119
TemplateID: row.WorkspaceTable.TemplateID,
120120
VersionID: row.WorkspaceBuild.TemplateVersionID,
121-
BlockUserData: row.WorkspaceAgent.APIKeyScope == database.ApiKeyScopeEnumNoUserData,
121+
BlockUserData: row.WorkspaceAgent.APIKeyScope == database.AgentKeyScopeEnumNoUserData,
122122
}),
123123
)
124124
if err != nil {

coderd/provisionerdserver/provisionerdserver.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -2003,9 +2003,9 @@ func InsertWorkspaceResource(ctx context.Context, db database.Store, jobID uuid.
20032003
}
20042004
}
20052005

2006-
apiKeyScope := database.ApiKeyScopeEnumDefault
2007-
if prAgent.ApiKeyScope == string(database.ApiKeyScopeEnumNoUserData) {
2008-
apiKeyScope = database.ApiKeyScopeEnumNoUserData
2006+
apiKeyScope := database.AgentKeyScopeEnumAll
2007+
if prAgent.ApiKeyScope == string(database.AgentKeyScopeEnumNoUserData) {
2008+
apiKeyScope = database.AgentKeyScopeEnumNoUserData
20092009
}
20102010

20112011
agentID := uuid.New()

coderd/rbac/scopes.go

+1-4
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,7 @@ var builtinScopes = map[ScopeName]Scope{
101101
// Workspace dormancy and workspace are omitted.
102102
// Workspace is specifically handled based on the opts.NoOwnerWorkspaceExec
103103
allPermsExcept(ResourceUser),
104-
// This adds back in the Workspace permissions.
105-
Permissions(map[string][]policy.Action{
106-
ResourceUser.Type: {policy.ActionRead},
107-
})...),
104+
),
108105
Org: map[string][]Permission{},
109106
User: []Permission{},
110107
},

site/e2e/helpers.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -644,7 +644,7 @@ const createTemplateVersionTar = async (
644644
troubleshootingUrl: "",
645645
token: randomUUID(),
646646
devcontainers: [],
647-
apiKeyScope: "default",
647+
apiKeyScope: "all",
648648
...agent,
649649
} as Agent;
650650

0 commit comments

Comments
 (0)