Skip to content

Commit

Permalink
Propagate service TLS config for terminal proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
sleshchenko committed May 27, 2020
1 parent 0a18759 commit 9187fcc
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 13 deletions.
5 changes: 4 additions & 1 deletion cmd/bridge/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ func main() {

k8sAuthServiceAccountBearerToken = string(bearerToken)

// If running in an OpenShift cluster, set up a proxy to the prometheus-k8s serivce running in the openshift-monitoring namespace.
// If running in an OpenShift cluster, set up a proxy to the prometheus-k8s service running in the openshift-monitoring namespace.
if *fServiceCAFile != "" {
serviceCertPEM, err := ioutil.ReadFile(*fServiceCAFile)
if err != nil {
Expand Down Expand Up @@ -344,6 +344,7 @@ func main() {
HeaderBlacklist: []string{"Cookie", "X-CSRFToken"},
Endpoint: &url.URL{Scheme: "https", Host: openshiftMeteringHost, Path: "/api"},
}
srv.TerminalProxyTLSConfig = serviceProxyTLSConfig
}

case "off-cluster":
Expand Down Expand Up @@ -401,6 +402,8 @@ func main() {
}
}

srv.TerminalProxyTLSConfig = serviceProxyTLSConfig

default:
bridge.FlagFatalf("k8s-mode", "must be one of: in-cluster, off-cluster")
}
Expand Down
12 changes: 7 additions & 5 deletions pkg/server/server.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package server

import (
"crypto/tls"
"fmt"
"html/template"
"io"
Expand Down Expand Up @@ -98,6 +99,7 @@ type Server struct {
ThanosTenancyProxyConfig *proxy.Config
AlertManagerProxyConfig *proxy.Config
MeteringProxyConfig *proxy.Config
TerminalProxyTLSConfig *tls.Config
// A lister for resource listing of a particular kind
MonitoringDashboardConfigMapLister ResourceLister
KnativeEventSourceCRDLister ResourceLister
Expand Down Expand Up @@ -221,12 +223,12 @@ func (s *Server) HTTPHandler() http.Handler {
})),
)

terminalProxy := &terminal.Proxy{
TLSClientConfig: s.K8sProxyConfig.TLSClientConfig,
ClusterEndpoint: s.K8sProxyConfig.Endpoint,
}
handle(terminal.ProxyEndpoint, authHandlerWithUser(terminalProxy.HandleProxy))
terminalProxy := terminal.NewProxy(
s.TerminalProxyTLSConfig,
s.K8sProxyConfig.TLSClientConfig,
s.K8sProxyConfig.Endpoint)

handle(terminal.ProxyEndpoint, authHandlerWithUser(terminalProxy.HandleProxy))
handleFunc(terminal.AvailableEndpoint, terminalProxy.HandleProxyEnabled)

if s.prometheusProxyEnabled() {
Expand Down
27 changes: 20 additions & 7 deletions pkg/terminal/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ import (
"strings"
"time"

"github.com/openshift/console/pkg/auth"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"

"github.com/openshift/console/pkg/auth"
)

const (
Expand All @@ -30,8 +31,21 @@ const (

// Proxy provides handlers to handle terminal related requests
type Proxy struct {
TLSClientConfig *tls.Config
ClusterEndpoint *url.URL
// A client with the correct TLS setup for communicating with servers withing cluster.
workspaceHttpClient *http.Client
TLSClientConfig *tls.Config
ClusterEndpoint *url.URL
}

func NewProxy(serviceTLS *tls.Config, TLSClientConfig *tls.Config, clusterEndpoint *url.URL) *Proxy {
return &Proxy{
workspaceHttpClient: &http.Client{
Timeout: 10 * time.Second,
Transport: &http.Transport{TLSClientConfig: serviceTLS},
},
TLSClientConfig: TLSClientConfig,
ClusterEndpoint: clusterEndpoint,
}
}

var (
Expand Down Expand Up @@ -140,6 +154,8 @@ func (p *Proxy) HandleProxy(user *auth.User, w http.ResponseWriter, r *http.Requ
p.handleExecInit(terminalHost, user.Token, r, w)
} else if path == WorkspaceActivityEndpoint {
p.handleActivity(terminalHost, user.Token, w)
} else {
http.Error(w, "Unknown path", http.StatusForbidden)
}
}

Expand Down Expand Up @@ -233,10 +249,7 @@ func (p *Proxy) getBaseTerminalHost(ws *unstructured.Unstructured) (*url.URL, er
}

func (p *Proxy) proxyToWorkspace(wkspReq *http.Request, w http.ResponseWriter) {
client := &http.Client{
Timeout: 10 * time.Second,
}
wkspResp, err := client.Do(wkspReq)
wkspResp, err := p.workspaceHttpClient.Do(wkspReq)
if err != nil {
http.Error(w, "Failed to proxy request. Cause: "+err.Error(), http.StatusInternalServerError)
return
Expand Down

0 comments on commit 9187fcc

Please sign in to comment.