Skip to content

feat: Add workspace application support #1773

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 42 commits into from
Jun 4, 2022
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 proxying based on path
  • Loading branch information
kylecarbs committed May 27, 2022
commit 866eeed5970424939bec2b1618ce25a573ff7ad3
14 changes: 5 additions & 9 deletions coderd/coderd.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package coderd

import (
"context"
"crypto/cipher"
"crypto/x509"
"fmt"
"net/http"
Expand Down Expand Up @@ -36,11 +35,10 @@ import (

// Options are requires parameters for Coder to start.
type Options struct {
AccessURL *url.URL
WildcardURL *url.URL
Logger slog.Logger
Database database.Store
Pubsub database.Pubsub
AccessURL *url.URL
Logger slog.Logger
Database database.Store
Pubsub database.Pubsub

AgentConnectionUpdateFrequency time.Duration
// APIRateLimit is the minutely throughput rate limit per user or ip.
Expand All @@ -57,9 +55,6 @@ type Options struct {
SSHKeygenAlgorithm gitsshkey.Algorithm
TURNServer *turnconn.Server
TracerProvider *sdktrace.TracerProvider
// WildcardCipher is used to encrypt session tokens so that authentication
// can be securely transferred to the wildcard host.
WildcardCipher cipher.AEAD
}

// New constructs a Coder API handler.
Expand Down Expand Up @@ -109,6 +104,7 @@ func New(options *Options) *API {
httpmw.ExtractUserParam(api.Database),
authRolesMiddleware,
)
r.Get("/", api.workspaceAppsProxyPath)
})

r.Route("/api/v2", func(r chi.Router) {
Expand Down
24 changes: 0 additions & 24 deletions coderd/httpmw/wildcard.go

This file was deleted.

38 changes: 0 additions & 38 deletions coderd/httpmw/wildcard_test.go

This file was deleted.

9 changes: 4 additions & 5 deletions coderd/workspaceagents.go
Original file line number Diff line number Diff line change
Expand Up @@ -462,11 +462,10 @@ func convertApps(dbApps []database.WorkspaceApp) []codersdk.WorkspaceApp {
apps := make([]codersdk.WorkspaceApp, 0)
for _, dbApp := range dbApps {
apps = append(apps, codersdk.WorkspaceApp{
ID: dbApp.ID,
Name: dbApp.Name,
Command: dbApp.Command.String,
AccessURL: dbApp.Url.String,
Icon: dbApp.Icon,
ID: dbApp.ID,
Name: dbApp.Name,
Command: dbApp.Command.String,
Icon: dbApp.Icon,
})
}
return apps
Expand Down
126 changes: 113 additions & 13 deletions coderd/workspaceapps.go
Original file line number Diff line number Diff line change
@@ -1,33 +1,133 @@
package coderd

import (
"database/sql"
"errors"
"fmt"
"net/http"
"net/http/httputil"
"net/url"
"strings"

"github.com/coder/coder/coderd/database"
"github.com/go-chi/chi/v5"
"github.com/google/uuid"
)

// workspaceAppsAuthWildcard authenticates the wildcard domain.
func (api *API) workspaceAppsAuthWildcard(rw http.ResponseWriter, r *http.Request) {
// r.URL.Query().Get("redirect")
"github.com/coder/coder/coderd/database"
"github.com/coder/coder/coderd/httpapi"
"github.com/coder/coder/coderd/httpmw"
)

}
func (api *API) workspaceAppsProxyPath(rw http.ResponseWriter, r *http.Request) {
user := httpmw.UserParam(r)
// This can be in the form of: "<workspace-name>.[workspace-agent]" or "<workspace-name>"
workspaceWithAgent := chi.URLParam(r, "workspaceagent")
workspaceParts := strings.Split(workspaceWithAgent, ".")

func (api *API) workspaceAppsProxyWildcard(rw http.ResponseWriter, r *http.Request) {
workspace, err := api.Database.GetWorkspaceByOwnerIDAndName(r.Context(), database.GetWorkspaceByOwnerIDAndNameParams{
OwnerID: user.ID,
Name: workspaceParts[0],
})
if errors.Is(err, sql.ErrNoRows) {
httpapi.Write(rw, http.StatusNotFound, httpapi.Response{
Message: "workspace not found",
})
return
}
if err != nil {
httpapi.Write(rw, http.StatusInternalServerError, httpapi.Response{
Message: fmt.Sprintf("get workspace: %s", err),
})
return
}

}
build, err := api.Database.GetLatestWorkspaceBuildByWorkspaceID(r.Context(), workspace.ID)
if err != nil {
httpapi.Write(rw, http.StatusInternalServerError, httpapi.Response{
Message: fmt.Sprintf("get workspace build: %s", err),
})
return
}

func (api *API) workspaceAppsProxyPath(rw http.ResponseWriter, r *http.Request) {
conn, err := api.dialWorkspaceAgent(r, uuid.Nil)
resources, err := api.Database.GetWorkspaceResourcesByJobID(r.Context(), build.JobID)
if err != nil {
httpapi.Write(rw, http.StatusInternalServerError, httpapi.Response{
Message: fmt.Sprintf("get workspace resources: %s", err),
})
return
}
resourceIDs := make([]uuid.UUID, 0)
for _, resource := range resources {
resourceIDs = append(resourceIDs, resource.ID)
}
agents, err := api.Database.GetWorkspaceAgentsByResourceIDs(r.Context(), resourceIDs)
if err != nil {
httpapi.Write(rw, http.StatusInternalServerError, httpapi.Response{
Message: fmt.Sprintf("get workspace agents: %s", err),
})
return
}
if len(agents) == 0 {
httpapi.Write(rw, http.StatusBadRequest, httpapi.Response{
Message: "no agents exist",
})
}

agent := agents[0]
if len(workspaceParts) > 1 {
for _, otherAgent := range agents {
if otherAgent.Name == workspaceParts[1] {
agent = otherAgent
break
}
}
}

app, err := api.Database.GetWorkspaceAppByAgentIDAndName(r.Context(), database.GetWorkspaceAppByAgentIDAndNameParams{
AgentID: uuid.Nil,
Name: "something",
AgentID: agent.ID,
Name: chi.URLParam(r, "application"),
})
if errors.Is(err, sql.ErrNoRows) {
httpapi.Write(rw, http.StatusNotFound, httpapi.Response{
Message: "application not found",
})
return
}
if err != nil {
httpapi.Write(rw, http.StatusInternalServerError, httpapi.Response{
Message: fmt.Sprintf("get workspace app: %s", err),
})
return
}
conn.DialContext(r.Context(), "tcp", "localhost:3000")
if !app.Url.Valid {
httpapi.Write(rw, http.StatusBadRequest, httpapi.Response{
Message: fmt.Sprintf("application does not have a url: %s", err),
})
return
}

appURL, err := url.Parse(app.Url.String)
if err != nil {
httpapi.Write(rw, http.StatusInternalServerError, httpapi.Response{
Message: fmt.Sprintf("parse app url: %s", err),
})
return
}

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

proxy := httputil.NewSingleHostReverseProxy(appURL)
defaultTransport, valid := http.DefaultTransport.(*http.Transport)
if !valid {
panic("dev error: default transport isn't a transport")
}
transport := defaultTransport.Clone()
transport.DialContext = conn.DialContext
proxy.Transport = transport
proxy.ServeHTTP(rw, r)
}
37 changes: 35 additions & 2 deletions coderd/workspaceapps_test.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,43 @@
package coderd_test

import (
"context"
"fmt"
"io"
"net"
"net/http"
"testing"

"github.com/google/uuid"
"github.com/stretchr/testify/require"

"cdr.dev/slog/sloggers/slogtest"
"github.com/coder/coder/agent"
"github.com/coder/coder/coderd/coderdtest"
"github.com/coder/coder/codersdk"
"github.com/coder/coder/provisioner/echo"
"github.com/coder/coder/provisionersdk/proto"
"github.com/google/uuid"
)

func TestWorkspaceAppsProxyPath(t *testing.T) {
t.Parallel()
t.Run("Proxies", func(t *testing.T) {
t.Parallel()
// #nosec
ln, err := net.Listen("tcp", ":0")
require.NoError(t, err)
server := http.Server{
Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
}),
}
t.Cleanup(func() {
_ = server.Close()
_ = ln.Close()
})
go server.Serve(ln)
tcpAddr, _ := ln.Addr().(*net.TCPAddr)

client, coderAPI := coderdtest.NewWithAPI(t, nil)
user := coderdtest.CreateFirstUser(t, client)
daemonCloser := coderdtest.NewProvisionerDaemon(t, coderAPI)
Expand All @@ -34,6 +56,10 @@ func TestWorkspaceAppsProxyPath(t *testing.T) {
Auth: &proto.Agent_Token{
Token: authToken,
},
Apps: []*proto.App{{
Name: "example",
Url: fmt.Sprintf("http://127.0.0.1:%d", tcpAddr.Port),
}},
}},
}},
},
Expand All @@ -54,6 +80,13 @@ func TestWorkspaceAppsProxyPath(t *testing.T) {
t.Cleanup(func() {
_ = agentCloser.Close()
})
resources := coderdtest.AwaitWorkspaceAgents(t, client, workspace.LatestBuild.ID)
coderdtest.AwaitWorkspaceAgents(t, client, workspace.LatestBuild.ID)

resp, err := client.Request(context.Background(), http.MethodGet, "/me/"+workspace.Name+"/example", nil)
require.NoError(t, err)
body, err := io.ReadAll(resp.Body)
require.NoError(t, err)
require.Equal(t, "", string(body))
require.Equal(t, http.StatusOK, resp.StatusCode)
})
}
2 changes: 1 addition & 1 deletion coderd/workspaceresources_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func TestWorkspaceResource(t *testing.T) {

t.Run("Apps", func(t *testing.T) {
t.Parallel()
_, client, coderd := coderdtest.NewWithServer(t, nil)
client, coderd := coderdtest.NewWithAPI(t, nil)
user := coderdtest.CreateFirstUser(t, client)
coderdtest.NewProvisionerDaemon(t, coderd)
app := &proto.App{
Expand Down