Skip to content

chore: Dynamic CSP connect-src to support terminals connecting to workspace proxies #7352

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 6 commits into from
May 2, 2023
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
Rename to host
  • Loading branch information
Emyrk committed May 1, 2023
commit a31072a923c3c51b17cf95a6d7b48bc1e4b1c5ff
1 change: 1 addition & 0 deletions coderd/httpmw/csp.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ func CSPHeaders(websocketHosts func() []string) func(next http.Handler) http.Han
extraConnect := websocketHosts()
if len(extraConnect) > 0 {
for _, extraHost := range extraConnect {
fmt.Println("extraHost", extraHost)
cspSrcs.Append(cspDirectiveConnectSrc, fmt.Sprintf("wss://%[1]s ws://%[1]s", extraHost))
}
}
Expand Down
2 changes: 1 addition & 1 deletion enterprise/coderd/coderd.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ func New(ctx context.Context, options *Options) (*API, error) {
go api.forceWorkspaceProxyHealthUpdate(ctx)

// Use proxy health to return the healthy workspace proxy hostnames.
f := api.ProxyHealth.HealthyHostnames
f := api.ProxyHealth.HealthyHosts
api.AGPL.HealthyWorkspaceProxyHosts.Store(&f)
}

Expand Down
19 changes: 10 additions & 9 deletions enterprise/coderd/proxyhealth/proxyhealth.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,12 +146,13 @@ func (p *ProxyHealth) storeProxyHealth(statuses map[uuid.UUID]ProxyStatus) {
var healthyHosts []string
for _, s := range statuses {
if s.Status == Healthy {
healthyHosts = append(healthyHosts, s.ProxyHostname)
healthyHosts = append(healthyHosts, s.ProxyHost)
}
}

// Store the statuses in the cache before any other quick values.
p.cache.Store(&statuses)
fmt.Println(healthyHosts)
p.heathyHosts.Store(&healthyHosts)
}

Expand All @@ -178,11 +179,11 @@ func (p *ProxyHealth) HealthStatus() map[uuid.UUID]ProxyStatus {
return *ptr
}

// HealthyHostnames returns the hostnames of all healthy proxies.
// HealthyHosts returns the host:port of all healthy proxies.
// This can be computed from HealthStatus, but is cached to avoid the
// caller needing to loop over all proxies to compute this on all
// static web requests.
func (p *ProxyHealth) HealthyHostnames() []string {
func (p *ProxyHealth) HealthyHosts() []string {
ptr := p.heathyHosts.Load()
if ptr == nil {
return []string{}
Expand All @@ -196,13 +197,13 @@ type ProxyStatus struct {
// then the proxy in hand. AKA if the proxy was updated, and the status was for
// an older proxy.
Proxy database.WorkspaceProxy
// ProxyHostname is the hostname of the proxy url. This is included in the status
// ProxyHost is the host:port of the proxy url. This is included in the status
// to make sure the proxy url is a valid URL. It also makes it easier to
// escalate errors if the url.Parse errors (should never happen).
ProxyHostname string
Status Status
Report codersdk.ProxyHealthReport
CheckedAt time.Time
ProxyHost string
Status Status
Report codersdk.ProxyHealthReport
CheckedAt time.Time
}

// runOnce runs the health check for all workspace proxies. If there is an
Expand Down Expand Up @@ -288,7 +289,7 @@ func (p *ProxyHealth) runOnce(ctx context.Context, now time.Time) (map[uuid.UUID
break
}
status.Status = Healthy
status.ProxyHostname = u.Hostname()
status.ProxyHost = u.Host
case err == nil && resp.StatusCode != http.StatusOK:
// Unhealthy as we did reach the proxy but it got an unexpected response.
status.Status = Unhealthy
Expand Down