Skip to content

Commit c413479

Browse files
committed
update external agent credentials to include command in response
1 parent 33dd778 commit c413479

File tree

10 files changed

+50
-28
lines changed

10 files changed

+50
-28
lines changed

cli/external_workspaces.go

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -128,18 +128,17 @@ func (r *RootCmd) externalWorkspaceAgentInstructions() *serpent.Command {
128128
return xerrors.Errorf("find workspace and agent: %w", err)
129129
}
130130

131-
credential, err := client.WorkspaceExternalAgentCredentials(inv.Context(), workspace.ID, workspaceAgent.Name)
131+
credentials, err := client.WorkspaceExternalAgentCredentials(inv.Context(), workspace.ID, workspaceAgent.Name)
132132
if err != nil {
133133
return xerrors.Errorf("get external agent token for agent %q: %w", workspaceAgent.Name, err)
134134
}
135135

136-
initScriptURL := fmt.Sprintf("%s/api/v2/init-script/%s/%s", client.URL, workspaceAgent.OperatingSystem, workspaceAgent.Architecture)
137136
agentInfo := externalAgent{
138137
WorkspaceName: workspace.Name,
139138
AgentName: workspaceAgent.Name,
140139
AuthType: "token",
141-
AuthToken: credential.AgentToken,
142-
InitScript: initScriptURL,
140+
AuthToken: credentials.AgentToken,
141+
InitScript: credentials.Command,
143142
}
144143

145144
out, err := formatter.Format(inv.Context(), agentInfo)
@@ -235,17 +234,16 @@ func fetchExternalAgents(inv *serpent.Invocation, client *codersdk.Client, works
235234
}
236235

237236
agent := resource.Agents[0]
238-
credential, err := client.WorkspaceExternalAgentCredentials(inv.Context(), workspace.ID, agent.Name)
237+
credentials, err := client.WorkspaceExternalAgentCredentials(inv.Context(), workspace.ID, agent.Name)
239238
if err != nil {
240239
return nil, xerrors.Errorf("get external agent token for agent %q: %w", agent.Name, err)
241240
}
242241

243-
initScriptURL := fmt.Sprintf("%s/api/v2/init-script/%s/%s", client.URL, agent.OperatingSystem, agent.Architecture)
244242
externalAgents = append(externalAgents, externalAgent{
245243
AgentName: agent.Name,
246244
AuthType: "token",
247-
AuthToken: credential.AgentToken,
248-
InitScript: initScriptURL,
245+
AuthToken: credentials.AgentToken,
246+
InitScript: credentials.Command,
249247
})
250248
}
251249

cli/external_workspaces_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ func TestExternalWorkspaces(t *testing.T) {
374374
require.NoError(t, json.Unmarshal(out.Bytes(), &agentInfo))
375375
assert.Equal(t, "token", agentInfo["auth_type"])
376376
assert.NotEmpty(t, agentInfo["auth_token"])
377-
assert.Contains(t, agentInfo["init_script"], "/api/v2/init-script/linux/amd64")
377+
assert.NotEmpty(t, agentInfo["init_script"])
378378
})
379379

380380
t.Run("AgentInstructionsNonExistentWorkspace", func(t *testing.T) {

coderd/apidoc/docs.go

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/apidoc/swagger.json

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/workspaceagents.go

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2221,23 +2221,35 @@ func (api *API) workspaceExternalAgentCredentials(rw http.ResponseWriter, r *htt
22212221
return
22222222
}
22232223

2224-
for _, agent := range agents {
2225-
if agent.Name == agentName {
2226-
if agent.AuthInstanceID.Valid {
2227-
httpapi.Write(ctx, rw, http.StatusNotFound, codersdk.Response{
2228-
Message: "External agent is authenticated with an instance ID.",
2229-
})
2230-
return
2231-
}
2232-
2233-
httpapi.Write(ctx, rw, http.StatusOK, codersdk.ExternalAgentCredentials{
2234-
AgentToken: agent.AuthToken.String(),
2235-
})
2236-
return
2224+
var agent *database.WorkspaceAgent
2225+
for i := range agents {
2226+
if agents[i].Name == agentName {
2227+
agent = &agents[i]
2228+
break
22372229
}
22382230
}
2231+
if agent == nil {
2232+
httpapi.Write(ctx, rw, http.StatusNotFound, codersdk.Response{
2233+
Message: fmt.Sprintf("External agent '%s' not found in workspace.", agentName),
2234+
})
2235+
return
2236+
}
2237+
2238+
if agent.AuthInstanceID.Valid {
2239+
httpapi.Write(ctx, rw, http.StatusNotFound, codersdk.Response{
2240+
Message: "External agent is authenticated with an instance ID.",
2241+
})
2242+
return
2243+
}
2244+
2245+
initScriptURL := fmt.Sprintf("%s/api/v2/init-script/%s/%s", api.AccessURL.String(), agent.OperatingSystem, agent.Architecture)
2246+
command := fmt.Sprintf("CODER_AGENT_TOKEN=%q curl -fsSL %q | sh", agent.AuthToken.String(), initScriptURL)
2247+
if agent.OperatingSystem == "windows" {
2248+
command = fmt.Sprintf("$env:CODER_AGENT_TOKEN=%q; iwr -useb %q | iex", agent.AuthToken.String(), initScriptURL)
2249+
}
22392250

2240-
httpapi.Write(ctx, rw, http.StatusNotFound, codersdk.Response{
2241-
Message: fmt.Sprintf("External agent '%s' not found in workspace.", agentName),
2251+
httpapi.Write(ctx, rw, http.StatusOK, codersdk.ExternalAgentCredentials{
2252+
AgentToken: agent.AuthToken.String(),
2253+
Command: command,
22422254
})
22432255
}

codersdk/workspaces.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -692,6 +692,7 @@ func (c *Client) UpdateWorkspaceACL(ctx context.Context, workspaceID uuid.UUID,
692692

693693
// ExternalAgentCredentials contains the credentials needed for an external agent to connect to Coder.
694694
type ExternalAgentCredentials struct {
695+
Command string `json:"command"`
695696
AgentToken string `json:"agent_token"`
696697
}
697698

docs/reference/api/agents.md

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/reference/api/schemas.md

Lines changed: 3 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

site/src/api/typesGenerated.ts

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

site/src/modules/resources/AgentExternal.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,17 @@ export const AgentExternal: FC<AgentExternalProps> = ({
1818
const [externalAgentToken, setExternalAgentToken] = useState<string | null>(
1919
null,
2020
);
21+
const [command, setCommand] = useState<string | null>(null);
2122

2223
const origin = isChromatic() ? "https://example.com" : window.location.origin;
23-
const initScriptURL = `${origin}/api/v2/init-script/${agent.operating_system}/${agent.architecture}`;
2424
useEffect(() => {
2525
if (
2626
isExternalAgent &&
2727
(agent.status === "timeout" || agent.status === "connecting")
2828
) {
2929
API.getWorkspaceAgentCredentials(workspace.id, agent.name).then((res) => {
3030
setExternalAgentToken(res.agent_token);
31+
setCommand(res.command);
3132
});
3233
}
3334
}, [isExternalAgent, agent.status, workspace.id, agent.name]);
@@ -39,7 +40,7 @@ export const AgentExternal: FC<AgentExternalProps> = ({
3940
{workspace.name} workspace:
4041
</p>
4142
<CodeExample
42-
code={`CODER_AGENT_TOKEN="${externalAgentToken}" curl -fsSL "${initScriptURL}" | sh`}
43+
code={command ?? ""}
4344
secret={false}
4445
redactPattern={/CODER_AGENT_TOKEN="([^"]+)"/g}
4546
redactReplacement={`CODER_AGENT_TOKEN="********"`}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy