Skip to content

Commit 70097b6

Browse files
committed
[ws-proxy] make https default and redirect http
1 parent 2b2702f commit 70097b6

12 files changed

+77
-56
lines changed

chart/config/proxy/lib.workspace-locations.conf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,5 @@ location / {
1515
error_log off;
1616

1717
proxy_set_header x-wsproxy-host $host;
18-
proxy_pass http://wsproxy$request_uri;
18+
proxy_pass https://wsproxy$request_uri;
1919
}

chart/config/proxy/lib.workspace-port-locations.conf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ location / {
1919
error_log off;
2020

2121
proxy_set_header x-wsproxy-host $host;
22-
proxy_pass http://wsproxy$request_uri;
22+
proxy_pass https://wsproxy$request_uri;
2323
}
2424
{{- else }}
2525

chart/config/proxy/vhost.upstreams.conf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ upstream dashboard {
2222
{{- $wsProxy := .Values.components.wsProxy -}}
2323
{{- if (and $wsProxy (not $wsProxy.disabled)) }}
2424
upstream wsproxy {
25-
server ws-proxy.${KUBE_NAMESPACE}.svc.cluster.local:8080;
25+
server ws-proxy.${KUBE_NAMESPACE}.svc.cluster.local:9090;
2626

2727
# Keep up to 100 connections to upstream alive and re-use them for faster responses
2828
keepalive 100;

chart/templates/ws-proxy-configmap.yaml

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ data:
1818
config.json: |-
1919
{
2020
"ingress": {
21-
"address": ":{{- $comp.ports.httpProxy.containerPort -}}",
22-
"https": {{ $comp.useHTTPS -}},
21+
"httpAddress": ":{{- $comp.ports.httpProxy.containerPort -}}",
22+
"httpsAddress": ":{{- $comp.ports.httpsProxy.containerPort -}}",
2323
"header": "{{- $comp.hostHeader -}}"
2424
},
2525
"workspaceInfoProviderConfig": {
@@ -32,13 +32,10 @@ data:
3232
}
3333
},
3434
"proxy": {
35-
{{- if and $comp.useHTTPS $.Values.certificatesSecret.secretName }}
3635
"https": {
37-
"enabled": true,
3836
"crt": "/mnt/certificates/tls.crt",
3937
"key": "/mnt/certificates/tls.key"
4038
},
41-
{{- end }}
4239
"transportConfig": {
4340
"connectTimeout": "10s",
4441
"idleConnTimeout": "60s",

chart/templates/ws-proxy-networkpolicy.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,12 @@ spec:
2020
policyTypes:
2121
- Ingress
2222
ingress:
23-
# Allow access to HTTP proxy port from everywhere
23+
# Allow access to HTTP/HTTPS proxy ports from everywhere
2424
- ports:
2525
- protocol: TCP
2626
port: {{ $comp.ports.httpProxy.containerPort }}
27+
- protocol: TCP
28+
port: {{ $comp.ports.httpsProxy.containerPort }}
2729
{{ if $comp.ports.wsManagerProxy }}
2830
- protocol: TCP
2931
port: {{ $comp.ports.wsManagerProxy.containerPort }}

chart/values.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,6 @@ components:
466466
cpu: 100m
467467
memory: 64Mi
468468
replicas: 1
469-
useHTTPS: false
470469
hostHeader: "x-wsproxy-host"
471470
wsManagerProxy:
472471
enabled: false
@@ -478,6 +477,9 @@ components:
478477
httpProxy:
479478
expose: true
480479
containerPort: 8080
480+
httpsProxy:
481+
expose: true
482+
containerPort: 9090
481483
metrics:
482484
expose: false
483485
containerPort: 9500

components/ws-proxy/cmd/root.go

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"fmt"
1010
"os"
1111

12-
validation "github.com/go-ozzo/ozzo-validation"
1312
"github.com/spf13/cobra"
1413
"golang.org/x/xerrors"
1514

@@ -55,7 +54,7 @@ func init() {
5554

5655
// Config configures this servuce
5756
type Config struct {
58-
Ingress HostBasedIngressConfig `json:"ingress"`
57+
Ingress proxy.HostBasedIngressConfig `json:"ingress"`
5958
Proxy proxy.Config `json:"proxy"`
6059
WorkspaceInfoProviderConfig proxy.WorkspaceInfoProviderConfig `json:"workspaceInfoProviderConfig"`
6160
PProfAddr string `json:"pprofAddr"`
@@ -82,23 +81,6 @@ func (c *Config) Validate() error {
8281
return nil
8382
}
8483

85-
// HostBasedIngressConfig configures the host-based ingress
86-
type HostBasedIngressConfig struct {
87-
Address string `json:"address"`
88-
Header string `json:"header"`
89-
}
90-
91-
// Validate validates this config
92-
func (c *HostBasedIngressConfig) Validate() error {
93-
if c == nil {
94-
return xerrors.Errorf("host based ingress config is mandatory")
95-
}
96-
return validation.ValidateStruct(c,
97-
validation.Field(&c.Address, validation.Required),
98-
validation.Field(&c.Header, validation.Required),
99-
)
100-
}
101-
10284
// WSManagerProxyConfig configures the ws-manager TCP proxy
10385
type WSManagerProxyConfig struct {
10486
ListenAddress string `json:"listenAddress"`

components/ws-proxy/cmd/run.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,8 @@ var runCmd = &cobra.Command{
5656
}
5757
log.Infof("workspace info provider started")
5858

59-
addr := cfg.Ingress.Address
60-
go proxy.NewWorkspaceProxy(addr, cfg.Proxy, proxy.HostBasedRouter(cfg.Ingress.Header, cfg.Proxy.GitpodInstallation.WorkspaceHostSuffix), workspaceInfoProvider).MustServe()
61-
log.Infof("started proxying on %s", addr)
59+
go proxy.NewWorkspaceProxy(cfg.Ingress, cfg.Proxy, proxy.HostBasedRouter(cfg.Ingress.Header, cfg.Proxy.GitpodInstallation.WorkspaceHostSuffix), workspaceInfoProvider).MustServe()
60+
log.Infof("started proxying on %s", cfg.Ingress.HttpAddress)
6261

6362
if cfg.PProfAddr != "" {
6463
go pprof.Serve(cfg.PProfAddr)

components/ws-proxy/pkg/proxy/config.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import (
1818
// Config is the configuration for a WorkspaceProxy
1919
type Config struct {
2020
HTTPS struct {
21-
Enabled bool `json:"enabled"`
2221
Key string `json:"key"`
2322
Certificate string `json:"crt"`
2423
} `json:"https,omitempty"`
@@ -51,6 +50,25 @@ func (c *Config) Validate() error {
5150
return nil
5251
}
5352

53+
// HostBasedIngressConfig configures the host-based ingress
54+
type HostBasedIngressConfig struct {
55+
HttpAddress string `json:"httpAddress"`
56+
HttpsAddress string `json:"httpsAddress"`
57+
Header string `json:"header"`
58+
}
59+
60+
// Validate validates this config
61+
func (c *HostBasedIngressConfig) Validate() error {
62+
if c == nil {
63+
return xerrors.Errorf("host based ingress config is mandatory")
64+
}
65+
return validation.ValidateStruct(c,
66+
validation.Field(&c.HttpAddress, validation.Required),
67+
validation.Field(&c.HttpsAddress, validation.Required),
68+
validation.Field(&c.Header, validation.Required),
69+
)
70+
}
71+
5472
// WorkspacePodConfig contains config around the workspace pod
5573
type WorkspacePodConfig struct {
5674
ServiceTemplate string `json:"serviceTemplate"`

components/ws-proxy/pkg/proxy/proxy.go

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,45 +16,56 @@ import (
1616

1717
// WorkspaceProxy is the entity which forwards all inbound requests to the relevant workspace pods
1818
type WorkspaceProxy struct {
19-
Address string
19+
Ingress HostBasedIngressConfig
2020
Config Config
2121
WorkspaceRouter WorkspaceRouter
2222
WorkspaceInfoProvider WorkspaceInfoProvider
2323
}
2424

2525
// NewWorkspaceProxy creates a new workspace proxy
26-
func NewWorkspaceProxy(address string, config Config, workspaceRouter WorkspaceRouter, workspaceInfoProvider WorkspaceInfoProvider) *WorkspaceProxy {
26+
func NewWorkspaceProxy(ingress HostBasedIngressConfig, config Config, workspaceRouter WorkspaceRouter, workspaceInfoProvider WorkspaceInfoProvider) *WorkspaceProxy {
2727
return &WorkspaceProxy{
28-
Address: address,
28+
Ingress: ingress,
2929
Config: config,
3030
WorkspaceRouter: workspaceRouter,
3131
WorkspaceInfoProvider: workspaceInfoProvider,
3232
}
3333
}
3434

35+
func redirectToHttps(w http.ResponseWriter, r *http.Request) {
36+
target := "https://" + r.Host + r.URL.Path
37+
if len(r.URL.RawQuery) > 0 {
38+
target += "?" + r.URL.RawQuery
39+
}
40+
log.WithField("target", target).Debug("redirect to https")
41+
http.Redirect(w, r, target, http.StatusTemporaryRedirect)
42+
}
43+
3544
// MustServe starts the proxy and ends the process if doing so fails
3645
func (p *WorkspaceProxy) MustServe() {
3746
handler, err := p.Handler()
3847
if err != nil {
3948
log.WithError(err).Fatal("cannot initialize proxy - this is likely a configuration issue")
4049
return
4150
}
42-
srv := &http.Server{Addr: p.Address, Handler: handler}
51+
srv := &http.Server{Addr: p.Ingress.HttpsAddress, Handler: handler}
4352

44-
if p.Config.HTTPS.Enabled {
45-
var (
46-
crt = p.Config.HTTPS.Certificate
47-
key = p.Config.HTTPS.Key
48-
)
49-
if tproot := os.Getenv("TELEPRESENCE_ROOT"); tproot != "" {
50-
crt = filepath.Join(tproot, crt)
51-
key = filepath.Join(tproot, key)
52-
}
53-
err = srv.ListenAndServeTLS(crt, key)
54-
} else {
55-
err = srv.ListenAndServe()
53+
var (
54+
crt = p.Config.HTTPS.Certificate
55+
key = p.Config.HTTPS.Key
56+
)
57+
if tproot := os.Getenv("TELEPRESENCE_ROOT"); tproot != "" {
58+
crt = filepath.Join(tproot, crt)
59+
key = filepath.Join(tproot, key)
5660
}
61+
go func() {
62+
err := http.ListenAndServe(p.Ingress.HttpAddress, http.HandlerFunc(redirectToHttps))
63+
if err != nil {
64+
log.WithError(err).Fatal("cannot start http proxy")
65+
}
66+
}()
5767

68+
err = srv.ListenAndServeTLS(crt, key)
5869
if err != nil {
5970
log.WithError(err).Fatal("cannot start proxy")
6071
return

components/ws-proxy/pkg/proxy/routes.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -257,12 +257,16 @@ func installWorkspacePortRoutes(r *mux.Router, config *RouteHandlerConfig) error
257257

258258
// forward request to workspace port
259259
r.NewRoute().HandlerFunc(
260-
proxyPass(
261-
config,
262-
workspacePodPortResolver,
263-
withHTTPErrorHandler(showPortNotFoundPage),
264-
withXFrameOptionsFilter(),
265-
),
260+
func(rw http.ResponseWriter, r *http.Request) {
261+
r.Header.Add("X-Forwarded-Proto", "https")
262+
r.Header.Add("X-Forwarded-Host", r.Host+":443")
263+
proxyPass(
264+
config,
265+
workspacePodPortResolver,
266+
withHTTPErrorHandler(showPortNotFoundPage),
267+
withXFrameOptionsFilter(),
268+
)(rw, r)
269+
},
266270
)
267271

268272
return nil

components/ws-proxy/pkg/proxy/routes_test.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -605,7 +605,13 @@ func TestRoutes(t *testing.T) {
605605
router = test.Router(&cfg)
606606
}
607607

608-
proxy := NewWorkspaceProxy(":8080", cfg, router, &fakeWsInfoProvider{infos: workspaces})
608+
ingress := HostBasedIngressConfig{
609+
HttpAddress: "8080",
610+
HttpsAddress: "9090",
611+
Header: "",
612+
}
613+
614+
proxy := NewWorkspaceProxy(ingress, cfg, router, &fakeWsInfoProvider{infos: workspaces})
609615
handler, err := proxy.Handler()
610616
if err != nil {
611617
t.Fatalf("cannot create proxy handler: %q", err)

0 commit comments

Comments
 (0)