Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
bdfa61a
feat: Add app support
kylecarbs May 6, 2022
8b2f6c4
Merge branch 'main' into devurls
kylecarbs May 15, 2022
e3cf488
Merge branch 'main' into devurls
kylecarbs May 24, 2022
e3ff8ad
Compare fields in apps test
kylecarbs May 24, 2022
b6e1ea6
Update Terraform provider to use relative path
kylecarbs May 25, 2022
430cfe7
Add some basic structure for routing
kylecarbs May 26, 2022
6ef781c
chore: Remove interface from coderd and lift API surface
kylecarbs May 26, 2022
f70dd17
Merge branch 'routeclean' into devurls
kylecarbs May 26, 2022
0805250
Merge branch 'main' into devurls
kylecarbs May 26, 2022
934b1ff
Add basic proxy logic
kylecarbs May 26, 2022
866eeed
Add proxying based on path
kylecarbs May 27, 2022
4b73034
Merge branch 'main' into apps
kylecarbs May 27, 2022
b4f9615
Add app proxying for wildcards
kylecarbs May 27, 2022
c88df46
Add wsconncache
kylecarbs May 31, 2022
d327df7
fix: Race when writing to a closed pipe
kylecarbs May 31, 2022
f84f5ea
Merge branch 'readclose' into apps
kylecarbs May 31, 2022
cec2de3
fix: Race when writing to a closed pipe
kylecarbs May 31, 2022
c57f8dd
Merge branch 'readclose' into apps
kylecarbs May 31, 2022
8e61cac
fix: Race when writing to a closed pipe
kylecarbs May 31, 2022
b6e6d7b
Merge branch 'readclose' into apps
kylecarbs May 31, 2022
46b24f7
fix: Race when writing to a closed pipe
kylecarbs May 31, 2022
4d8b257
Merge branch 'readclose' into apps
kylecarbs Jun 1, 2022
e9b7463
Add workspace route proxying endpoint
kylecarbs Jun 3, 2022
80b5600
Add embed errors
kylecarbs Jun 3, 2022
8b81c35
chore: Refactor site to improve testing
kylecarbs Jun 3, 2022
60ad881
Merge branch 'refactorsite' into apps
kylecarbs Jun 3, 2022
0a63bec
Add test for error handler
kylecarbs Jun 3, 2022
d3b9ab5
Remove unused access url
kylecarbs Jun 3, 2022
7a1ae15
Add RBAC tests
kylecarbs Jun 3, 2022
5b9194f
Merge branch 'main' into apps
kylecarbs Jun 3, 2022
cd2d12e
Merge branch 'main' into apps
kylecarbs Jun 3, 2022
b056400
Fix dial agent syntax
kylecarbs Jun 3, 2022
fe3aecc
Merge branch 'main' into apps
kylecarbs Jun 3, 2022
2018cdc
Fix linting errors
kylecarbs Jun 3, 2022
2d5261f
Fix gen
kylecarbs Jun 3, 2022
856f17d
Fix icon required
kylecarbs Jun 3, 2022
1a21f94
Merge branch 'main' into apps
kylecarbs Jun 3, 2022
ad90bcb
Adjust migration number
kylecarbs Jun 3, 2022
38abbb5
Fix proxy error status code
kylecarbs Jun 4, 2022
4f89642
Fix empty db lookup
kylecarbs Jun 4, 2022
637be3e
Merge branch 'main' into apps
kylecarbs Jun 4, 2022
50da4fb
Merge branch 'main' into apps
kylecarbs Jun 4, 2022
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
Add embed errors
  • Loading branch information
kylecarbs committed Jun 3, 2022
commit 80b5600a3d5c527254d5dd9eaa0ff297c5c4b907
17 changes: 12 additions & 5 deletions coderd/coderd.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,9 @@ func New(options *Options) *API {

r := chi.NewRouter()
api := &API{
Options: options,
Handler: r,
Options: options,
Handler: r,
siteHandler: site.Handler(),
}
api.workspaceAgentCache = wsconncache.New(api.dialWorkspaceAgent, 0)

Expand All @@ -95,14 +96,19 @@ func New(options *Options) *API {
tracing.HTTPMW(api.TracerProvider, "coderd.http"),
)

r.Route("/@{user}/{workspaceagent}/apps/{application}", func(r chi.Router) {
apps := func(r chi.Router) {
r.Use(
httpmw.RateLimitPerMinute(options.APIRateLimit),
apiKeyMiddleware,
httpmw.ExtractUserParam(api.Database),
)
r.Get("/*", api.workspaceAppsProxyPath)
})
}
// %40 is the encoded character of the @ symbol. VS Code Web does
// not handle character encoding properly, so it's safe to assume
// other applications might not as well.
r.Route("/%40{user}/{workspaceagent}/apps/{application}", apps)
r.Route("/@{user}/{workspaceagent}/apps/{application}", apps)

r.Route("/api/v2", func(r chi.Router) {
r.NotFound(func(rw http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -338,14 +344,15 @@ func New(options *Options) *API {
r.Get("/state", api.workspaceBuildState)
})
})
r.NotFound(site.DefaultHandler().ServeHTTP)
r.NotFound(api.siteHandler.ServeHTTP)
return api
}

type API struct {
*Options

Handler chi.Router
siteHandler http.Handler
websocketWaitMutex sync.Mutex
websocketWaitGroup sync.WaitGroup
workspaceAgentCache *wsconncache.Cache
Expand Down
42 changes: 28 additions & 14 deletions coderd/workspaceapps.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/coder/coder/coderd/database"
"github.com/coder/coder/coderd/httpapi"
"github.com/coder/coder/coderd/httpmw"
"github.com/coder/coder/site"
)

func (api *API) workspaceAppsProxyPath(rw http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -113,23 +114,15 @@ func (api *API) workspaceAppsProxyPath(rw http.ResponseWriter, r *http.Request)
return
}

conn, release, err := api.workspaceAgentCache.Acquire(r, agent.ID)
if err != nil {
httpapi.Write(rw, http.StatusInternalServerError, httpapi.Response{
Message: fmt.Sprintf("dial workspace agent: %s", err),
})
return
}
defer release()

proxy := httputil.NewSingleHostReverseProxy(appURL)
// Write the error directly using our format!
// Write an error using our embed handler
proxy.ErrorHandler = func(w http.ResponseWriter, r *http.Request, err error) {
httpapi.Write(w, http.StatusBadGateway, httpapi.Response{
Message: err.Error(),
})
r = r.WithContext(site.WithAPIResponse(r.Context(), site.APIResponse{
StatusCode: http.StatusBadGateway,
Message: err.Error(),
}))
api.siteHandler.ServeHTTP(w, r)
}
proxy.Transport = conn.HTTPTransport()
path := chi.URLParam(r, "*")
if !strings.HasSuffix(r.URL.Path, "/") && path == "" {
// Web applications typically request paths relative to the
Expand All @@ -139,6 +132,27 @@ func (api *API) workspaceAppsProxyPath(rw http.ResponseWriter, r *http.Request)
http.Redirect(rw, r, r.URL.String(), http.StatusTemporaryRedirect)
return
}
if r.URL.RawQuery == "" && appURL.RawQuery != "" {
// If the application defines a default set of query parameters,
// we should always respect them. The reverse proxy will merge
// query parameters for server-side requests, but sometimes
// client-side applications require the query parameters to render
// properly. With code-server, this is the "folder" param.
r.URL.RawQuery = appURL.RawQuery
http.Redirect(rw, r, r.URL.String(), http.StatusTemporaryRedirect)
return
}
r.URL.Path = path

conn, release, err := api.workspaceAgentCache.Acquire(r, agent.ID)
if err != nil {
httpapi.Write(rw, http.StatusInternalServerError, httpapi.Response{
Message: fmt.Sprintf("dial workspace agent: %s", err),
})
return
}
defer release()

proxy.Transport = conn.HTTPTransport()
proxy.ServeHTTP(rw, r)
}
18 changes: 14 additions & 4 deletions coderd/workspaceapps_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func TestWorkspaceAppsProxyPath(t *testing.T) {

client, coderAPI := coderdtest.NewWithAPI(t, nil)
user := coderdtest.CreateFirstUser(t, client)
daemonCloser := coderdtest.NewProvisionerDaemon(t, coderAPI)
coderdtest.NewProvisionerDaemon(t, coderAPI)
authToken := uuid.NewString()
version := coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, &echo.Responses{
Parse: echo.ParseComplete,
Expand All @@ -56,7 +56,7 @@ func TestWorkspaceAppsProxyPath(t *testing.T) {
},
Apps: []*proto.App{{
Name: "example",
Url: fmt.Sprintf("http://127.0.0.1:%d", tcpAddr.Port),
Url: fmt.Sprintf("http://127.0.0.1:%d?query=true", tcpAddr.Port),
}},
}},
}},
Expand All @@ -68,7 +68,6 @@ func TestWorkspaceAppsProxyPath(t *testing.T) {
coderdtest.AwaitTemplateVersionJob(t, client, version.ID)
workspace := coderdtest.CreateWorkspace(t, client, user.OrganizationID, template.ID)
coderdtest.AwaitWorkspaceBuildJob(t, client, workspace.LatestBuild.ID)
daemonCloser.Close()

agentClient := codersdk.New(client.URL)
agentClient.SessionToken = authToken
Expand All @@ -91,11 +90,22 @@ func TestWorkspaceAppsProxyPath(t *testing.T) {
require.Equal(t, http.StatusTemporaryRedirect, resp.StatusCode)
})

t.Run("Proxies", func(t *testing.T) {
t.Run("RedirectsWithQuery", func(t *testing.T) {
t.Parallel()
resp, err := client.Request(context.Background(), http.MethodGet, "/@me/"+workspace.Name+"/apps/example/", nil)
require.NoError(t, err)
defer resp.Body.Close()
require.Equal(t, http.StatusTemporaryRedirect, resp.StatusCode)
loc, err := resp.Location()
require.NoError(t, err)
require.Equal(t, "query=true", loc.RawQuery)
})

t.Run("Proxies", func(t *testing.T) {
t.Parallel()
resp, err := client.Request(context.Background(), http.MethodGet, "/@me/"+workspace.Name+"/apps/example/?query=true", nil)
require.NoError(t, err)
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
require.NoError(t, err)
require.Equal(t, "", string(body))
Expand Down
11 changes: 11 additions & 0 deletions examples/templates/docker-code-server/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
name: Develop code-server in Docker
description: Run code-server in a Docker development environment
tags: [local, docker]
---

# code-server in Docker

## Getting started

Run `coder templates init` and select this template. Follow the instructions that appear.
45 changes: 45 additions & 0 deletions examples/templates/docker-code-server/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
terraform {
required_providers {
coder = {
source = "coder/coder"
version = "0.4.2"
}
docker = {
source = "kreuzwerker/docker"
version = "~> 2.16.0"
}
}
}

provider "coder" {
}

data "coder_workspace" "me" {
}

resource "coder_agent" "dev" {
arch = "amd64"
os = "linux"
startup_script = "code-server --auth none"
}

resource "coder_app" "code-server" {
agent_id = coder_agent.dev.id
url = "http://localhost:8080/?folder=/home/coder"
}

resource "docker_container" "workspace" {
count = data.coder_workspace.me.start_count
image = "codercom/code-server:latest"
# Uses lower() to avoid Docker restriction on container names.
name = "coder-${data.coder_workspace.me.owner}-${lower(data.coder_workspace.me.name)}"
hostname = lower(data.coder_workspace.me.name)
dns = ["1.1.1.1"]
# Use the docker gateway if the access URL is 127.0.0.1
entrypoint = ["sh", "-c", replace(coder_agent.dev.init_script, "127.0.0.1", "host.docker.internal")]
env = ["CODER_AGENT_TOKEN=${coder_agent.dev.token}"]
host {
host = "host.docker.internal"
ip = "host-gateway"
}
}
31 changes: 26 additions & 5 deletions site/embed.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package site

import (
"bytes"
"context"
"embed"
"fmt"
"io"
Expand All @@ -28,7 +29,16 @@ import (
//go:embed out/bin/*
var site embed.FS

func DefaultHandler() http.Handler {
type apiResponseContextKey struct{}

// WithAPIResponse returns a context with the APIResponse value attached.
// This is used to inject API response data to the index.html for additional
// metadata in error pages.
func WithAPIResponse(ctx context.Context, apiResponse APIResponse) context.Context {
return context.WithValue(ctx, apiResponseContextKey{}, apiResponse)
}

func Handler() http.Handler {
// the out directory is where webpack builds are created. It is in the same
// directory as this file (package site).
siteFS, err := fs.Sub(site, "out")
Expand All @@ -38,11 +48,11 @@ func DefaultHandler() http.Handler {
panic(err)
}

return Handler(siteFS)
return HandlerWithFS(siteFS)
}

// Handler returns an HTTP handler for serving the static site.
func Handler(fileSystem fs.FS) http.Handler {
func HandlerWithFS(fileSystem fs.FS) http.Handler {
// html files are handled by a text/template. Non-html files
// are served by the default file server.
//
Expand Down Expand Up @@ -90,8 +100,14 @@ func (h *handler) exists(filePath string) bool {
}

type htmlState struct {
CSP cspState
CSRF csrfState
APIResponse APIResponse
CSP cspState
CSRF csrfState
}

type APIResponse struct {
StatusCode int
Message string
}

type cspState struct {
Expand Down Expand Up @@ -139,6 +155,11 @@ func (h *handler) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
CSRF: csrfState{Token: nosurf.Token(req)},
}

apiResponseRaw := req.Context().Value(apiResponseContextKey{})
if apiResponseRaw != nil {
state.APIResponse = apiResponseRaw.(APIResponse)
}

// First check if it's a file we have in our templates
if h.serveHTML(resp, req, reqFile, state) {
return
Expand Down
11 changes: 10 additions & 1 deletion site/embed_slim.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@ import (
"net/http"
)

func DefaultHandler() http.Handler {
type APIResponse struct {
StatusCode int
Message string
}

func Handler() http.Handler {
return http.NotFoundHandler()
}

func WithAPIResponse(ctx context.Context, _ APIResponse) context.Context {
return ctx
}
42 changes: 40 additions & 2 deletions site/embed_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package site_test

import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
Expand Down Expand Up @@ -39,7 +40,7 @@ func TestCaching(t *testing.T) {
},
}

srv := httptest.NewServer(site.Handler(rootFS))
srv := httptest.NewServer(site.HandlerWithFS(rootFS))
defer srv.Close()

// Create a context
Expand Down Expand Up @@ -98,7 +99,7 @@ func TestServingFiles(t *testing.T) {
},
}

srv := httptest.NewServer(site.Handler(rootFS))
srv := httptest.NewServer(site.HandlerWithFS(rootFS))
defer srv.Close()

// Create a context
Expand Down Expand Up @@ -172,3 +173,40 @@ func TestShouldCacheFile(t *testing.T) {
require.Equal(t, testCase.expected, got, fmt.Sprintf("Expected ShouldCacheFile(%s) to be %t", testCase.reqFile, testCase.expected))
}
}

func TestServeAPIResponse(t *testing.T) {
t.Parallel()

// Create a test server
rootFS := fstest.MapFS{
"index.html": &fstest.MapFile{
Data: []byte(`{"code":{{ .APIResponse.StatusCode }},"message":"{{ .APIResponse.Message }}"}`),
},
}

apiResponse := site.APIResponse{
StatusCode: http.StatusBadGateway,
Message: "This could be an error message!",
}
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
r = r.WithContext(site.WithAPIResponse(r.Context(), apiResponse))
site.HandlerWithFS(rootFS).ServeHTTP(w, r)
}))
defer srv.Close()

req, err := http.NewRequestWithContext(context.Background(), "GET", srv.URL, nil)
require.NoError(t, err)
resp, err := http.DefaultClient.Do(req)
require.NoError(t, err)
var body struct {
Code int `json:"code"`
Message string `json:"message"`
}
data, err := io.ReadAll(resp.Body)
require.NoError(t, err)
t.Logf("resp: %q", data)
err = json.Unmarshal(data, &body)
require.NoError(t, err)
require.Equal(t, apiResponse.StatusCode, body.Code)
require.Equal(t, apiResponse.Message, body.Message)
}
1 change: 1 addition & 0 deletions site/htmlTemplates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<meta property="og:type" content="website" />
<meta property="csp-nonce" content="{{ .CSP.Nonce }}" />
<meta property="csrf-token" content="{{ .CSRF.Token }}" />
<meta id="api-response" data-statuscode="{{ .APIResponse.StatusCode }}" data-message="{{ .APIResponse.Message }}" />
<link rel="mask-icon" href="/static/favicon.svg" color="#000000" crossorigin="use-credentials" />
<link rel="alternate icon" type="image/png" href="/favicon.png" />
<link rel="icon" type="image/svg+xml" href="/favicon.svg" />
Expand Down