Skip to content

Commit cfe484c

Browse files
committed
comments
1 parent 8508138 commit cfe484c

File tree

3 files changed

+61
-70
lines changed

3 files changed

+61
-70
lines changed

enterprise/wsproxy/wsproxysdk/client.go

Lines changed: 0 additions & 70 deletions
This file was deleted.

enterprise/wsproxy/wsproxysdk/proxyinternal.go renamed to enterprise/wsproxy/wsproxysdk/wsproxysdk.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,74 @@ import (
55
"encoding/json"
66
"io"
77
"net/http"
8+
"net/url"
89

10+
"github.com/google/uuid"
911
"golang.org/x/xerrors"
1012

13+
"github.com/coder/coder/coderd/httpmw"
1114
"github.com/coder/coder/coderd/workspaceapps"
1215
"github.com/coder/coder/codersdk"
1316
)
1417

18+
// Client is a HTTP client for a subset of Coder API routes that external
19+
// proxies need.
20+
type Client struct {
21+
SDKClient *codersdk.Client
22+
// HACK: the issue-signed-app-token requests may issue redirect responses
23+
// (which need to be forwarded to the client), so the client we use to make
24+
// those requests must ignore redirects.
25+
sdkClientIgnoreRedirects *codersdk.Client
26+
}
27+
28+
// New creates a external proxy client for the provided primary coder server
29+
// URL.
30+
func New(serverURL *url.URL) *Client {
31+
sdkClient := codersdk.New(serverURL)
32+
sdkClient.SessionTokenHeader = httpmw.WorkspaceProxyAuthTokenHeader
33+
34+
sdkClientIgnoreRedirects := codersdk.New(serverURL)
35+
sdkClientIgnoreRedirects.HTTPClient.CheckRedirect = func(req *http.Request, via []*http.Request) error {
36+
return http.ErrUseLastResponse
37+
}
38+
sdkClientIgnoreRedirects.SessionTokenHeader = httpmw.WorkspaceProxyAuthTokenHeader
39+
40+
return &Client{
41+
SDKClient: sdkClient,
42+
sdkClientIgnoreRedirects: sdkClientIgnoreRedirects,
43+
}
44+
}
45+
46+
// SetSessionToken sets the session token for the client. An error is returned
47+
// if the session token is not in the correct format for external proxies.
48+
func (c *Client) SetSessionToken(token string) error {
49+
c.SDKClient.SetSessionToken(token)
50+
c.sdkClientIgnoreRedirects.SetSessionToken(token)
51+
return nil
52+
}
53+
54+
// SessionToken returns the currently set token for the client.
55+
func (c *Client) SessionToken() string {
56+
return c.SDKClient.SessionToken()
57+
}
58+
59+
// Request wraps the underlying codersdk.Client's Request method.
60+
func (c *Client) Request(ctx context.Context, method, path string, body interface{}, opts ...codersdk.RequestOption) (*http.Response, error) {
61+
return c.SDKClient.Request(ctx, method, path, body, opts...)
62+
}
63+
64+
// RequestIgnoreRedirects wraps the underlying codersdk.Client's Request method
65+
// on the client that ignores redirects.
66+
func (c *Client) RequestIgnoreRedirects(ctx context.Context, method, path string, body interface{}, opts ...codersdk.RequestOption) (*http.Response, error) {
67+
return c.sdkClientIgnoreRedirects.Request(ctx, method, path, body, opts...)
68+
}
69+
70+
// DialWorkspaceAgent calls the underlying codersdk.Client's DialWorkspaceAgent
71+
// method.
72+
func (c *Client) DialWorkspaceAgent(ctx context.Context, agentID uuid.UUID, options *codersdk.DialWorkspaceAgentOptions) (agentConn *codersdk.WorkspaceAgentConn, err error) {
73+
return c.SDKClient.DialWorkspaceAgent(ctx, agentID, options)
74+
}
75+
1576
type IssueSignedAppTokenResponse struct {
1677
// SignedTokenStr should be set as a cookie on the response.
1778
SignedTokenStr string `json:"signed_token_str"`

0 commit comments

Comments
 (0)