Skip to content

Commit

Permalink
Simplify the ssh.Conn parameter passing
Browse files Browse the repository at this point in the history
  • Loading branch information
iawia002 committed Oct 27, 2022
1 parent f9d1a2b commit c8e6c0e
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 23 deletions.
9 changes: 5 additions & 4 deletions pkg/proxy/http_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ import (
"fmt"
"net/http"

"golang.org/x/crypto/ssh"
"k8s.io/klog"

"kubesphere.io/tower/pkg/agent"
"kubesphere.io/tower/pkg/utils"
)

type HTTPProxy struct {
Expand All @@ -22,8 +23,8 @@ type HTTPProxy struct {
kubesphereAPIServerProxy *Server
}

func NewHTTPProxy(ssh utils.GetSSHConn, kubernetesPort uint16, kubespherePort uint16, config *agent.Config, ca, serverCert, serverKey []byte) (*HTTPProxy, *http.Transport, *http.Transport, error) {
k8stransPort, useBearerToken, servertlsConfig, err := buildServerData(ssh, config.KubernetesSvcHost, config.CAData, config.CertData, config.KeyData, ca, serverCert, serverKey)
func NewHTTPProxy(sshConn ssh.Conn, kubernetesPort uint16, kubespherePort uint16, config *agent.Config, ca, serverCert, serverKey []byte) (*HTTPProxy, *http.Transport, *http.Transport, error) {
k8stransPort, useBearerToken, servertlsConfig, err := buildServerData(sshConn, config.KubernetesSvcHost, config.CAData, config.CertData, config.KeyData, ca, serverCert, serverKey)
if err != nil {
return nil, nil, nil, err
}
Expand All @@ -33,7 +34,7 @@ func NewHTTPProxy(ssh utils.GetSSHConn, kubernetesPort uint16, kubespherePort ui
return nil, nil, nil, err
}

kstransPort, useBearerToken, _, err := buildServerData(ssh, config.KubeSphereSvcHost, nil, nil, nil, nil, nil, nil)
kstransPort, useBearerToken, _, err := buildServerData(sshConn, config.KubeSphereSvcHost, nil, nil, nil, nil, nil, nil)
if err != nil {
return nil, nil, nil, err
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ func (s *Proxy) handleWebsocket(w http.ResponseWriter, req *http.Request) {
// if the agent has connected the server with the same cluster name, we don't need to create HttpProxy anymore
// we only create two new httpTransport objects, then put them into the server's httpClient set.
if proxy, ok = s.sessions[c.Name]; !ok {
proxy, k8sTransport, ksTransport, err = NewHTTPProxy(func() ssh.Conn { return sshConn }, client.Spec.Connection.KubernetesAPIServerPort, client.Spec.Connection.KubeSphereAPIServerPort, c, s.caCert, cert, key)
proxy, k8sTransport, ksTransport, err = NewHTTPProxy(sshConn, client.Spec.Connection.KubernetesAPIServerPort, client.Spec.Connection.KubeSphereAPIServerPort, c, s.caCert, cert, key)
if err != nil {
failed(err)
return
Expand All @@ -229,13 +229,13 @@ func (s *Proxy) handleWebsocket(w http.ResponseWriter, req *http.Request) {

s.sessions[c.Name] = proxy
} else {
k8sTransport, _, _, err = buildServerData(func() ssh.Conn { return sshConn }, c.KubernetesSvcHost, c.CAData, c.CertData, c.KeyData, s.caCert, cert, key)
k8sTransport, _, _, err = buildServerData(sshConn, c.KubernetesSvcHost, c.CAData, c.CertData, c.KeyData, s.caCert, cert, key)
if err != nil {
failed(err)
return
}

ksTransport, _, _, err = buildServerData(func() ssh.Conn { return sshConn }, c.KubeSphereSvcHost, c.CAData, c.CertData, c.KeyData, s.caCert, cert, key)
ksTransport, _, _, err = buildServerData(sshConn, c.KubeSphereSvcHost, c.CAData, c.CertData, c.KeyData, s.caCert, cert, key)
if err != nil {
failed(err)
return
Expand Down
7 changes: 2 additions & 5 deletions pkg/proxy/proxy_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"sync"
"time"

"golang.org/x/crypto/ssh"
utilnet "k8s.io/apimachinery/pkg/util/net"
k8sproxy "k8s.io/apimachinery/pkg/util/proxy"
"k8s.io/klog"
Expand Down Expand Up @@ -68,15 +69,11 @@ func newProxyServer(name, host, scheme string, port uint16, useBearerToken bool,
}

// buildServerData returns http.Transport and tlsConfig, which are necessary for creating proxy server.
func buildServerData(sshConn utils.GetSSHConn, host string, ca, cert, key, serverCa, serverCert, serverKey []byte) (*http.Transport, bool, *tls.Config, error) {
func buildServerData(sshConn ssh.Conn, host string, ca, cert, key, serverCa, serverCert, serverKey []byte) (*http.Transport, bool, *tls.Config, error) {
useBearerToken := true

transport := &http.Transport{
DialContext: func(ctx context.Context, network, addr string) (conn net.Conn, err error) {
c := sshConn()
if c == nil {
return nil, fmt.Errorf("no remote connetion available")
}
return utils.NewSshConn(sshConn, host)
},
}
Expand Down
17 changes: 6 additions & 11 deletions pkg/utils/ssh_conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,25 @@ package utils

import (
"errors"
"golang.org/x/crypto/ssh"
"io"
"net"
"time"

"golang.org/x/crypto/ssh"
)

var ErrorInvalidConnection = errors.New("invalid connection")

//ErrorNoAvailableConn means there haven't available shh connection.
var ErrorNoAvailableConn = errors.New("no available ssh connection")

type GetSSHConn func() ssh.Conn

type SshConn struct {
dst io.ReadWriteCloser
}

func NewSshConn(conn GetSSHConn, remote string) (net.Conn, error) {
c := conn()
if c == nil {
return nil, ErrorNoAvailableConn
func NewSshConn(conn ssh.Conn, remote string) (net.Conn, error) {
if conn == nil {
return nil, errors.New("the ssh connection is nil")
}

dst, reqs, err := c.OpenChannel("kubesphere", []byte(remote))
dst, reqs, err := conn.OpenChannel("kubesphere", []byte(remote))
if err != nil {
return nil, err
}
Expand Down

0 comments on commit c8e6c0e

Please