Skip to content

Commit d237b19

Browse files
committed
chore: Add endpoint to register workspace proxies
1 parent daff0ab commit d237b19

File tree

10 files changed

+111
-33
lines changed

10 files changed

+111
-33
lines changed

coderd/database/dbauthz/querier.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1709,11 +1709,11 @@ func (q *querier) InsertWorkspaceProxy(ctx context.Context, arg database.InsertW
17091709
return insert(q.log, q.auth, rbac.ResourceWorkspaceProxy, q.db.InsertWorkspaceProxy)(ctx, arg)
17101710
}
17111711

1712-
func (q *querier) UpdateWorkspaceProxy(ctx context.Context, arg database.UpdateWorkspaceProxyParams) (database.WorkspaceProxy, error) {
1713-
fetch := func(ctx context.Context, arg database.UpdateWorkspaceProxyParams) (database.WorkspaceProxy, error) {
1712+
func (q *querier) RegisterWorkspaceProxy(ctx context.Context, arg database.RegisterWorkspaceProxyParams) (database.WorkspaceProxy, error) {
1713+
fetch := func(ctx context.Context, arg database.RegisterWorkspaceProxyParams) (database.WorkspaceProxy, error) {
17141714
return q.db.GetWorkspaceProxyByID(ctx, arg.ID)
17151715
}
1716-
return updateWithReturn(q.log, q.auth, fetch, q.db.UpdateWorkspaceProxy)(ctx, arg)
1716+
return updateWithReturn(q.log, q.auth, fetch, q.db.RegisterWorkspaceProxy)(ctx, arg)
17171717
}
17181718

17191719
func (q *querier) UpdateWorkspaceProxyDeleted(ctx context.Context, arg database.UpdateWorkspaceProxyDeletedParams) error {

coderd/database/dbauthz/querier_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -444,9 +444,9 @@ func (s *MethodTestSuite) TestWorkspaceProxy() {
444444
ID: uuid.New(),
445445
}).Asserts(rbac.ResourceWorkspaceProxy, rbac.ActionCreate)
446446
}))
447-
s.Run("UpdateWorkspaceProxy", s.Subtest(func(db database.Store, check *expects) {
447+
s.Run("RegisterWorkspaceProxy", s.Subtest(func(db database.Store, check *expects) {
448448
p, _ := dbgen.WorkspaceProxy(s.T(), db, database.WorkspaceProxy{})
449-
check.Args(database.UpdateWorkspaceProxyParams{
449+
check.Args(database.RegisterWorkspaceProxyParams{
450450
ID: p.ID,
451451
}).Asserts(p, rbac.ActionUpdate)
452452
}))

coderd/database/dbfake/databasefake.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5172,14 +5172,12 @@ func (q *fakeQuerier) InsertWorkspaceProxy(_ context.Context, arg database.Inser
51725172
return p, nil
51735173
}
51745174

5175-
func (q *fakeQuerier) UpdateWorkspaceProxy(_ context.Context, arg database.UpdateWorkspaceProxyParams) (database.WorkspaceProxy, error) {
5175+
func (q *fakeQuerier) RegisterWorkspaceProxy(_ context.Context, arg database.RegisterWorkspaceProxyParams) (database.WorkspaceProxy, error) {
51765176
q.mutex.Lock()
51775177
defer q.mutex.Unlock()
51785178

51795179
for i, p := range q.workspaceProxies {
51805180
if p.ID == arg.ID {
5181-
p.Name = arg.Name
5182-
p.Icon = arg.Icon
51835181
p.Url = arg.Url
51845182
p.WildcardHostname = arg.WildcardHostname
51855183
p.UpdatedAt = database.Now()

coderd/database/querier.go

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

coderd/database/queries.sql.go

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

coderd/database/queries/proxies.sql

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,12 @@ INSERT INTO
1515
VALUES
1616
($1, $2, $3, $4, $5, $6, $7, $8, $9, false) RETURNING *;
1717

18-
-- name: UpdateWorkspaceProxy :one
18+
-- name: RegisterWorkspaceProxy :one
1919
UPDATE
2020
workspace_proxies
2121
SET
22-
name = @name,
23-
display_name = @display_name,
2422
url = @url,
2523
wildcard_hostname = @wildcard_hostname,
26-
icon = @icon,
2724
updated_at = Now()
2825
WHERE
2926
id = @id

coderd/workspaceapps/token.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ func (t SignedToken) MatchesRequest(req Request) bool {
5454
// two keys.
5555
type SecurityKey [96]byte
5656

57+
func (k SecurityKey) String() string {
58+
return hex.EncodeToString(k[:])
59+
}
60+
5761
func (k SecurityKey) signingKey() []byte {
5862
return k[:64]
5963
}

enterprise/coderd/coderd.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ func New(ctx context.Context, options *Options) (*API, error) {
100100
}),
101101
)
102102
r.Post("/issue-signed-app-token", api.workspaceProxyIssueSignedAppToken)
103+
r.Post("/register", api.workspaceProxyRegister)
103104
})
104105
r.Route("/{workspaceproxy}", func(r chi.Router) {
105106
r.Use(

enterprise/coderd/workspaceproxy.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,3 +251,66 @@ func (api *API) workspaceProxyIssueSignedAppToken(rw http.ResponseWriter, r *htt
251251
SignedTokenStr: tokenStr,
252252
})
253253
}
254+
255+
// workspaceProxyRegister is used to register a new workspace proxy. When a proxy
256+
// comes online, it will announce itself to this endpoint. This updates its values
257+
// in the database and returns a signed token that can be used to authenticate
258+
// tokens.
259+
//
260+
// @Summary Register workspace proxy
261+
// @ID register-workspace-proxy
262+
// @Security CoderSessionToken
263+
// @Accept json
264+
// @Produce json
265+
// @Tags Enterprise
266+
// @Param request body wsproxysdk.RegisterWorkspaceProxyRequest true "Issue signed app token request"
267+
// @Success 201 {object} wsproxysdk.RegisterWorkspaceProxyResponse
268+
// @Router /workspaceproxies/me/register [post]
269+
// @x-apidocgen {"skip": true}
270+
func (api *API) workspaceProxyRegister(rw http.ResponseWriter, r *http.Request) {
271+
var (
272+
ctx = r.Context()
273+
proxy = httpmw.WorkspaceProxy(r)
274+
)
275+
276+
var req wsproxysdk.RegisterWorkspaceProxyRequest
277+
if !httpapi.Read(ctx, rw, r, &req) {
278+
return
279+
}
280+
281+
if err := validateProxyURL(req.AccessURL); err != nil {
282+
httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{
283+
Message: "URL is invalid.",
284+
Detail: err.Error(),
285+
})
286+
return
287+
}
288+
289+
if req.WildcardHostname != "" {
290+
if _, err := httpapi.CompileHostnamePattern(req.WildcardHostname); err != nil {
291+
httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{
292+
Message: "Wildcard URL is invalid.",
293+
Detail: err.Error(),
294+
})
295+
return
296+
}
297+
}
298+
299+
_, err := api.Database.RegisterWorkspaceProxy(ctx, database.RegisterWorkspaceProxyParams{
300+
ID: proxy.ID,
301+
Url: req.AccessURL,
302+
WildcardHostname: req.WildcardHostname,
303+
})
304+
if httpapi.Is404Error(err) {
305+
httpapi.ResourceNotFound(rw)
306+
return
307+
}
308+
if err != nil {
309+
httpapi.InternalServerError(rw, err)
310+
return
311+
}
312+
313+
httpapi.Write(ctx, rw, http.StatusCreated, wsproxysdk.RegisterWorkspaceProxyResponse{
314+
AppSecurityKey: api.AppSecurityKey.String(),
315+
})
316+
}

enterprise/wsproxy/wsproxysdk/wsproxysdk.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,3 +142,31 @@ func (c *Client) IssueSignedAppTokenHTML(ctx context.Context, rw http.ResponseWr
142142
}
143143
return res, true
144144
}
145+
146+
type RegisterWorkspaceProxyRequest struct {
147+
// AccessURL that hits the workspace proxy api.
148+
AccessURL string `json:"access_url"`
149+
// WildcardHostname that the workspace proxy api is serving for subdomain apps.
150+
WildcardHostname string `json:"wildcard_hostname"`
151+
}
152+
153+
type RegisterWorkspaceProxyResponse struct {
154+
AppSecurityKey string `json:"app_security_key"`
155+
}
156+
157+
func (c *Client) RegisterWorkspaceProxy(ctx context.Context, req RegisterWorkspaceProxyRequest) (RegisterWorkspaceProxyResponse, error) {
158+
res, err := c.Request(ctx, http.MethodPost,
159+
"/api/v2/workspaceproxies/me/register",
160+
req,
161+
)
162+
if err != nil {
163+
return RegisterWorkspaceProxyResponse{}, xerrors.Errorf("make request: %w", err)
164+
}
165+
defer res.Body.Close()
166+
167+
if res.StatusCode != http.StatusCreated {
168+
return RegisterWorkspaceProxyResponse{}, codersdk.ReadBodyAsError(res)
169+
}
170+
var resp RegisterWorkspaceProxyResponse
171+
return resp, json.NewDecoder(res.Body).Decode(&resp)
172+
}

0 commit comments

Comments
 (0)