Skip to content

Commit d6c7570

Browse files
committed
import order
1 parent 901d695 commit d6c7570

File tree

3 files changed

+106
-0
lines changed

3 files changed

+106
-0
lines changed

cli/server.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,8 @@ func (r *RootCmd) Server(newAPI func(context.Context, *coderd.Options) (*coderd.
167167
devAppSecurityKey string
168168
)
169169
opts.Add(
170+
// This should be temporary until we support sending this over to the
171+
// proxy via some authenticated api call.
170172
clibase.Option{
171173
Name: "App Security Key (Development Only)",
172174
Description: "Used to override the app security key stored in the database. This should never be used in production.",

coderd/httpmw/workspaceproxy_test.go

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"net/http/httptest"
88
"testing"
99

10+
"github.com/go-chi/chi/v5"
1011
"github.com/google/uuid"
1112
"github.com/stretchr/testify/require"
1213

@@ -160,4 +161,106 @@ func TestExtractWorkspaceProxy(t *testing.T) {
160161
defer res.Body.Close()
161162
require.Equal(t, http.StatusOK, res.StatusCode)
162163
})
164+
165+
t.Run("Deleted", func(t *testing.T) {
166+
t.Parallel()
167+
var (
168+
db = dbfake.New()
169+
r = httptest.NewRequest("GET", "/", nil)
170+
rw = httptest.NewRecorder()
171+
172+
proxy, secret = dbgen.WorkspaceProxy(t, db, database.WorkspaceProxy{})
173+
)
174+
err := db.UpdateWorkspaceProxyDeleted(context.Background(), database.UpdateWorkspaceProxyDeletedParams{
175+
ID: proxy.ID,
176+
Deleted: true,
177+
})
178+
require.NoError(t, err, "failed to delete workspace proxy")
179+
180+
r.Header.Set(httpmw.WorkspaceProxyAuthTokenHeader, fmt.Sprintf("%s:%s", proxy.ID.String(), secret))
181+
182+
httpmw.ExtractWorkspaceProxy(httpmw.ExtractWorkspaceProxyConfig{
183+
DB: db,
184+
})(successHandler).ServeHTTP(rw, r)
185+
res := rw.Result()
186+
defer res.Body.Close()
187+
require.Equal(t, http.StatusUnauthorized, res.StatusCode)
188+
})
189+
}
190+
191+
func TestExtractWorkspaceProxyParam(t *testing.T) {
192+
t.Parallel()
193+
194+
successHandler := http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
195+
// Only called if the API key passes through the handler.
196+
httpapi.Write(context.Background(), rw, http.StatusOK, codersdk.Response{
197+
Message: "It worked!",
198+
})
199+
})
200+
201+
t.Run("OKName", func(t *testing.T) {
202+
t.Parallel()
203+
var (
204+
db = dbfake.New()
205+
r = httptest.NewRequest("GET", "/", nil)
206+
rw = httptest.NewRecorder()
207+
208+
proxy, _ = dbgen.WorkspaceProxy(t, db, database.WorkspaceProxy{})
209+
)
210+
211+
routeContext := chi.NewRouteContext()
212+
routeContext.URLParams.Add("workspaceproxy", proxy.Name)
213+
r = r.WithContext(context.WithValue(r.Context(), chi.RouteCtxKey, routeContext))
214+
215+
httpmw.ExtractWorkspaceProxyParam(db)(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
216+
// Checks that it exists on the context!
217+
_ = httpmw.WorkspaceProxyParam(request)
218+
successHandler.ServeHTTP(writer, request)
219+
})).ServeHTTP(rw, r)
220+
res := rw.Result()
221+
defer res.Body.Close()
222+
require.Equal(t, http.StatusOK, res.StatusCode)
223+
})
224+
225+
t.Run("OKID", func(t *testing.T) {
226+
t.Parallel()
227+
var (
228+
db = dbfake.New()
229+
r = httptest.NewRequest("GET", "/", nil)
230+
rw = httptest.NewRecorder()
231+
232+
proxy, _ = dbgen.WorkspaceProxy(t, db, database.WorkspaceProxy{})
233+
)
234+
235+
routeContext := chi.NewRouteContext()
236+
routeContext.URLParams.Add("workspaceproxy", proxy.ID.String())
237+
r = r.WithContext(context.WithValue(r.Context(), chi.RouteCtxKey, routeContext))
238+
239+
httpmw.ExtractWorkspaceProxyParam(db)(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
240+
// Checks that it exists on the context!
241+
_ = httpmw.WorkspaceProxyParam(request)
242+
successHandler.ServeHTTP(writer, request)
243+
})).ServeHTTP(rw, r)
244+
res := rw.Result()
245+
defer res.Body.Close()
246+
require.Equal(t, http.StatusOK, res.StatusCode)
247+
})
248+
249+
t.Run("NotFound", func(t *testing.T) {
250+
t.Parallel()
251+
var (
252+
db = dbfake.New()
253+
r = httptest.NewRequest("GET", "/", nil)
254+
rw = httptest.NewRecorder()
255+
)
256+
257+
routeContext := chi.NewRouteContext()
258+
routeContext.URLParams.Add("workspaceproxy", uuid.NewString())
259+
r = r.WithContext(context.WithValue(r.Context(), chi.RouteCtxKey, routeContext))
260+
261+
httpmw.ExtractWorkspaceProxyParam(db)(successHandler).ServeHTTP(rw, r)
262+
res := rw.Result()
263+
defer res.Body.Close()
264+
require.Equal(t, http.StatusNotFound, res.StatusCode)
265+
})
163266
}

enterprise/cli/workspaceproxy.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ func (r *RootCmd) createProxy() *clibase.Cmd {
7171
return fmt.Sprintf("Workspace Proxy %q registered successfully\nToken: %s", response.Proxy.Name, response.ProxyToken), nil
7272
}),
7373
cliui.JSONFormat(),
74+
// Table formatter expects a slice, make a slice of one.
7475
cliui.ChangeFormatterData(cliui.TableFormat([]codersdk.CreateWorkspaceProxyResponse{}, []string{"proxy name", "proxy url", "proxy token"}),
7576
func(data any) (any, error) {
7677
response, ok := data.(codersdk.CreateWorkspaceProxyResponse)

0 commit comments

Comments
 (0)