Skip to content

feat: coder-attach: add support for external workspaces #19178

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 34 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
3ea541e
Add attach command and API endpoints for init-script and external age…
kacpersaw Jul 23, 2025
4818df1
add has_external_agents column to template_versions table
kacpersaw Jul 24, 2025
4bfdb83
add external workspace creation and agent instruction commands to cli
kacpersaw Jul 28, 2025
1044051
add has_external_agent to workspace builds
kacpersaw Jul 29, 2025
fd2458b
add list command for external workspaces
kacpersaw Jul 30, 2025
0c39f50
add AgentExternal component to display external agent connection deta…
kacpersaw Jul 30, 2025
23e555a
Merge remote-tracking branch 'origin/main' into kacpersaw/feat-coder-…
kacpersaw Jul 31, 2025
f9f5be1
add tests
kacpersaw Jul 31, 2025
e281f0e
Delete coder attach golden
kacpersaw Aug 5, 2025
d77522d
Hide agent apps when connecting & is external agent
kacpersaw Aug 5, 2025
451c806
Merge remote-tracking branch 'origin/main' into kacpersaw/feat-coder-…
kacpersaw Aug 5, 2025
f9274fe
Reformat code
kacpersaw Aug 5, 2025
00b6f26
Merge remote-tracking branch 'origin/main' into kacpersaw/feat-coder-…
kacpersaw Aug 6, 2025
c019a31
bump provisionerd proto version to v1.9
kacpersaw Aug 6, 2025
7d07857
Add beforeCreate and afterCreate to create handler, apply review sugg…
kacpersaw Aug 6, 2025
c462a69
Refactor init-script endpoint to use path params instead of query params
kacpersaw Aug 6, 2025
2d2dfec
Refactor init-script endpoint, apply review suggestions for db
kacpersaw Aug 6, 2025
387fc04
Apply FE review suggestions
kacpersaw Aug 6, 2025
c2588ea
Return 404 if workspace agent is authenticated through instance id
kacpersaw Aug 6, 2025
33dd778
Merge UpdateTemplateVersionAITaskByJobID and UpdateTemplateVersionExt…
kacpersaw Aug 7, 2025
c413479
update external agent credentials to include command in response
kacpersaw Aug 7, 2025
3c1d694
Bump terraform-provider-coder to v2.10.0
kacpersaw Aug 8, 2025
73acd0f
Merge remote-tracking branch 'origin/main' into kacpersaw/feat-coder-…
kacpersaw Aug 8, 2025
7cc6861
Regenerate sql
kacpersaw Aug 8, 2025
da68c20
Fix lint & tests
kacpersaw Aug 8, 2025
2e24741
Regenerate dump.sql
kacpersaw Aug 8, 2025
682ea60
Fix provision test
kacpersaw Aug 8, 2025
51967a5
update external agent credentials summary and adjust authorization ch…
kacpersaw Aug 8, 2025
f060324
merge UpdateWorkspaceBuildAITaskByID with UpdateWorkspaceBuildExterna…
kacpersaw Aug 8, 2025
ff6e8fa
Apply suggestions from code review
kacpersaw Aug 8, 2025
e2a7182
Apply review suggestions
kacpersaw Aug 8, 2025
141bc54
Merge branch 'main' into kacpersaw/feat-coder-attach
kacpersaw Aug 8, 2025
a75c1f4
make gen
kacpersaw Aug 8, 2025
22f2c00
Merge remote-tracking branch 'origin/main' into kacpersaw/feat-coder-…
kacpersaw Aug 11, 2025
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
Prev Previous commit
Next Next commit
Refactor init-script endpoint, apply review suggestions for db
  • Loading branch information
kacpersaw committed Aug 6, 2025
commit 2d2dfec8e9b498bbbe463a9c98d29c0c4496d059
2 changes: 1 addition & 1 deletion coderd/database/dbauthz/dbauthz.go
Original file line number Diff line number Diff line change
Expand Up @@ -4707,7 +4707,7 @@ func (q *querier) UpdateTemplateVersionDescriptionByJobID(ctx context.Context, a
}

func (q *querier) UpdateTemplateVersionExternalAgentByJobID(ctx context.Context, arg database.UpdateTemplateVersionExternalAgentByJobIDParams) error {
// An actor is allowed to update the template version AI task flag if they are authorized to update the template.
// An actor is allowed to update the template version external agent flag if they are authorized to update the template.
tv, err := q.db.GetTemplateVersionByJobID(ctx, arg.JobID)
if err != nil {
return err
Expand Down
1 change: 0 additions & 1 deletion coderd/database/migrations/000356_external_agents.down.sql
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ ALTER TABLE workspace_builds DROP COLUMN has_external_agent;

DROP VIEW workspace_build_with_user;

-- We're adding the has_external_agent column.
CREATE VIEW workspace_build_with_user AS
SELECT
workspace_builds.id,
Expand Down
4 changes: 2 additions & 2 deletions coderd/database/migrations/000356_external_agents.up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ SELECT
template_versions.archived,
template_versions.source_example_id,
template_versions.has_ai_task,
template_versions.has_external_agent,
template_versions.has_external_agent,
COALESCE(visible_users.avatar_url, '' :: text) AS created_by_avatar_url,
COALESCE(visible_users.username, '' :: text) AS created_by_username,
COALESCE(visible_users.name, '' :: text) AS created_by_name
Expand Down Expand Up @@ -66,7 +66,7 @@ SELECT
workspace_builds.template_version_preset_id,
workspace_builds.has_ai_task,
workspace_builds.ai_task_sidebar_app_id,
workspace_builds.has_external_agent,
workspace_builds.has_external_agent,
COALESCE(
visible_users.avatar_url,
'' :: text
Expand Down
11 changes: 1 addition & 10 deletions coderd/init_script.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,9 @@ func (api *API) initScript(rw http.ResponseWriter, r *http.Request) {
os := strings.ToLower(chi.URLParam(r, "os"))
arch := strings.ToLower(chi.URLParam(r, "arch"))

if os != "linux" && os != "darwin" && os != "windows" {
rw.WriteHeader(http.StatusBadRequest)
return
}
if arch != "amd64" && arch != "arm64" && arch != "armv7" {
rw.WriteHeader(http.StatusBadRequest)
return
}

script, exists := provisionersdk.AgentScriptEnv()[fmt.Sprintf("CODER_AGENT_SCRIPT_%s_%s", os, arch)]
if !exists {
rw.WriteHeader(http.StatusNotFound)
rw.WriteHeader(http.StatusBadRequest)
Copy link
Member

Choose a reason for hiding this comment

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

You should probably write a message here too, using httpapi.Write(..., codersdk.Response{}) is probably fine.

Copy link
Member

Choose a reason for hiding this comment

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

Unknown os/arch: %s/%s

return
}
script = strings.ReplaceAll(script, "${ACCESS_URL}", api.AccessURL.String()+"/")
Expand Down
31 changes: 31 additions & 0 deletions coderd/init_script_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package coderd_test

import (
"context"
"fmt"
"testing"

"github.com/stretchr/testify/require"

"github.com/coder/coder/v2/coderd/coderdtest"
)

func TestInitScript(t *testing.T) {
t.Parallel()

t.Run("OK", func(t *testing.T) {
t.Parallel()
client := coderdtest.New(t, nil)
script, err := client.InitScript(context.Background(), "windows", "amd64")
require.NoError(t, err)
require.NotEmpty(t, script)
Copy link
Member

Choose a reason for hiding this comment

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

Should test the replacements as well, probably search for substrings like $env:CODER_AGENT_AUTH_TYPE="token" (or whatever it's supposed to look like) etc.

})

t.Run("BadRequest", func(t *testing.T) {
t.Parallel()
client := coderdtest.New(t, nil)
_, err := client.InitScript(context.Background(), "darwin", "armv7")
require.Error(t, err)
fmt.Printf("err: %+v\n", err)
})
}
28 changes: 28 additions & 0 deletions codersdk/init_script.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package codersdk
Copy link
Member

Choose a reason for hiding this comment

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

nit: This file should probably be renamed just initscript.go to match other files in the dir like provisionerdaemons.go. Files ending with _xyz.go are usually for showing stuff similar build tags in the filename


import (
"context"
"fmt"
"io"
"net/http"
)

func (c *Client) InitScript(ctx context.Context, os, arch string) (string, error) {
url := fmt.Sprintf("/api/v2/init-script/%s/%s", os, arch)
res, err := c.Request(ctx, http.MethodGet, url, nil)
if err != nil {
return "", err
}
defer res.Body.Close()

if res.StatusCode != http.StatusOK {
return "", ReadBodyAsError(res)
}

script, err := io.ReadAll(res.Body)
if err != nil {
return "", err
}

return string(script), nil
}