Skip to content

feat: Add vscodeipc subcommand for VS Code Extension #5326

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 15 commits into from
Dec 18, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix IPC tests
  • Loading branch information
kylecarbs committed Dec 18, 2022
commit 6bad9aeb1e2d49c63844e622912fd535cd20b48f
6 changes: 4 additions & 2 deletions cli/vscodeipc/vscodeipc.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import (
"github.com/coder/coder/codersdk"
)

const AuthHeader = "Coder-IPC-Token"

// New creates a VS Code IPC client that can be used to communicate with workspaces.
//
// Creating this IPC was required instead of using SSH, because we're unable to get
Expand Down Expand Up @@ -290,10 +292,10 @@ func (e *execWriter) Write(data []byte) (int, error) {
func sessionTokenMiddleware(sessionToken string) func(h http.Handler) http.Handler {
return func(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
token := r.Header.Get("Coder-IPC-Token")
token := r.Header.Get(AuthHeader)
if token == "" {
httpapi.Write(r.Context(), w, http.StatusUnauthorized, codersdk.Response{
Message: "A session token must be provided in the `Coder-IPC-Token` header.",
Message: fmt.Sprintf("A session token must be provided in the `%s` header.", AuthHeader),
})
return
}
Expand Down
18 changes: 9 additions & 9 deletions cli/vscodeipc/vscodeipc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@ func TestVSCodeIPC(t *testing.T) {
// Ensure that we're actually connected!
require.Eventually(t, func() bool {
res := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodGet, "/network", nil)
req.Header.Set("Coder-Session-Token", token)
req := httptest.NewRequest(http.MethodGet, "/v1/network", nil)
req.Header.Set(vscodeipc.AuthHeader, token)
handler.ServeHTTP(res, req)
network := &vscodeipc.NetworkResponse{}
err = json.NewDecoder(res.Body).Decode(&network)
Expand All @@ -125,16 +125,16 @@ func TestVSCodeIPC(t *testing.T) {
t.Run("NoSessionToken", func(t *testing.T) {
t.Parallel()
res := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodGet, fmt.Sprintf("/port/%s", port), nil)
req := httptest.NewRequest(http.MethodGet, fmt.Sprintf("/v1/port/%s", port), nil)
handler.ServeHTTP(res, req)
require.Equal(t, http.StatusUnauthorized, res.Code)
})

t.Run("MismatchedSessionToken", func(t *testing.T) {
t.Parallel()
res := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodGet, fmt.Sprintf("/port/%s", port), nil)
req.Header.Set("Coder-Session-Token", uuid.NewString())
req := httptest.NewRequest(http.MethodGet, fmt.Sprintf("/v1/port/%s", port), nil)
req.Header.Set(vscodeipc.AuthHeader, uuid.NewString())
handler.ServeHTTP(res, req)
require.Equal(t, http.StatusUnauthorized, res.Code)
})
Expand All @@ -147,8 +147,8 @@ func TestVSCodeIPC(t *testing.T) {
defer input.Close()
defer output.Close()
res := &hijackable{httptest.NewRecorder(), output}
req := httptest.NewRequest(http.MethodGet, fmt.Sprintf("/port/%s", port), nil)
req.Header.Set("Coder-Session-Token", token)
req := httptest.NewRequest(http.MethodGet, fmt.Sprintf("/v1/port/%s", port), nil)
req.Header.Set(vscodeipc.AuthHeader, token)
go handler.ServeHTTP(res, req)

req, err := http.NewRequestWithContext(ctx, http.MethodGet, "http://127.0.0.1/", nil)
Expand Down Expand Up @@ -177,8 +177,8 @@ func TestVSCodeIPC(t *testing.T) {
data, _ := json.Marshal(vscodeipc.ExecuteRequest{
Command: "echo test",
})
req := httptest.NewRequest(http.MethodPost, "/execute", bytes.NewReader(data))
req.Header.Set("Coder-Session-Token", token)
req := httptest.NewRequest(http.MethodPost, "/v1/execute", bytes.NewReader(data))
req.Header.Set(vscodeipc.AuthHeader, token)
handler.ServeHTTP(res, req)

decoder := json.NewDecoder(res.Body)
Expand Down