connectToCoordinatorAndFetchResumeToken connects to the tailnet coordinator with a given resume token. It returns an error if the connection is rejected. If the connection is accepted, it is immediately closed and no error is returned.
(ctx context.Context, logger slog.Logger, sdkClient *codersdk.Client, agentID uuid.UUID, resumeToken string)
| 1125 | // If the connection is accepted, it is immediately closed and no error is |
| 1126 | // returned. |
| 1127 | func connectToCoordinatorAndFetchResumeToken(ctx context.Context, logger slog.Logger, sdkClient *codersdk.Client, agentID uuid.UUID, resumeToken string) (string, error) { |
| 1128 | u, err := sdkClient.URL.Parse(fmt.Sprintf("/api/v2/workspaceagents/%s/coordinate", agentID)) |
| 1129 | if err != nil { |
| 1130 | return "", xerrors.Errorf("parse URL: %w", err) |
| 1131 | } |
| 1132 | q := u.Query() |
| 1133 | q.Set("version", "2.0") |
| 1134 | if resumeToken != "" { |
| 1135 | q.Set("resume_token", resumeToken) |
| 1136 | } |
| 1137 | u.RawQuery = q.Encode() |
| 1138 | |
| 1139 | //nolint:bodyclose |
| 1140 | wsConn, resp, err := websocket.Dial(ctx, u.String(), &websocket.DialOptions{ |
| 1141 | HTTPHeader: http.Header{ |
| 1142 | "Coder-Session-Token": []string{sdkClient.SessionToken()}, |
| 1143 | }, |
| 1144 | }) |
| 1145 | if err != nil { |
| 1146 | if resp.StatusCode != http.StatusSwitchingProtocols { |
| 1147 | err = codersdk.ReadBodyAsError(resp) |
| 1148 | } |
| 1149 | return "", xerrors.Errorf("websocket dial: %w", err) |
| 1150 | } |
| 1151 | defer wsConn.Close(websocket.StatusNormalClosure, "done") |
| 1152 | |
| 1153 | // Send a request to the server to ensure that we're plumbed all the way |
| 1154 | // through. |
| 1155 | rpcClient, err := tailnet.NewDRPCClient( |
| 1156 | websocket.NetConn(ctx, wsConn, websocket.MessageBinary), |
| 1157 | logger, |
| 1158 | ) |
| 1159 | if err != nil { |
| 1160 | return "", xerrors.Errorf("new dRPC client: %w", err) |
| 1161 | } |
| 1162 | |
| 1163 | // Fetch a resume token. |
| 1164 | newResumeToken, err := rpcClient.RefreshResumeToken(ctx, &tailnetproto.RefreshResumeTokenRequest{}) |
| 1165 | if err != nil { |
| 1166 | return "", xerrors.Errorf("fetch resume token: %w", err) |
| 1167 | } |
| 1168 | return newResumeToken.Token, nil |
| 1169 | } |
| 1170 | |
| 1171 | func TestWorkspaceAgentTailnetDirectDisabled(t *testing.T) { |
| 1172 | t.Parallel() |
no test coverage detected