Skip to content
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

http file transfer #2055

Merged
merged 1 commit into from
Jul 13, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
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
114 changes: 88 additions & 26 deletions lib/client/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import (
"github.com/gravitational/teleport/lib/services"
"github.com/gravitational/teleport/lib/session"
"github.com/gravitational/teleport/lib/shell"
"github.com/gravitational/teleport/lib/sshutils/scp"
"github.com/gravitational/teleport/lib/state"
"github.com/gravitational/teleport/lib/utils"

Expand Down Expand Up @@ -955,6 +956,58 @@ func (tc *TeleportClient) Play(ctx context.Context, namespace, sessionId string)
return trace.Wrap(err)
}

// ExecuteSCP executes SCP command. It executes scp.Command using
// lower-level API integrations that mimic SCP CLI command behavior
func (tc *TeleportClient) ExecuteSCP(ctx context.Context, cmd scp.Command) (err error) {
// connect to proxy first:
if !tc.Config.ProxySpecified() {
return trace.BadParameter("proxy server is not specified")
}

proxyClient, err := tc.ConnectToProxy(ctx)
if err != nil {
return trace.Wrap(err)
}
defer proxyClient.Close()

clusterInfo, err := proxyClient.currentCluster()
if err != nil {
return trace.Wrap(err)
}

// which nodes are we executing this commands on?
nodeAddrs, err := tc.getTargetNodes(ctx, proxyClient)
if err != nil {
return trace.Wrap(err)
}
if len(nodeAddrs) == 0 {
return trace.BadParameter("no target host specified")
}

nodeClient, err := proxyClient.ConnectToNode(
ctx,
nodeAddrs[0]+"@"+tc.Namespace+"@"+clusterInfo.Name,
tc.Config.HostLogin,
false)
if err != nil {
tc.ExitStatus = 1
return trace.Wrap(err)
}

err = nodeClient.ExecuteSCP(cmd)
if err != nil {
// converts SSH error code to tc.ExitStatus
exitError, _ := trace.Unwrap(err).(*ssh.ExitError)
if exitError != nil {
tc.ExitStatus = exitError.ExitStatus()
}
return err

}

return nil
}

// SCP securely copies file(s) from one SSH server to another
func (tc *TeleportClient) SCP(ctx context.Context, args []string, port int, recursive bool, quiet bool) (err error) {
if len(args) < 2 {
Expand Down Expand Up @@ -1003,7 +1056,7 @@ func (tc *TeleportClient) SCP(ctx context.Context, args []string, port int, recu
}
// upload:
if isRemoteDest(last) {
login, host, dest := parseSCPDestination(last)
login, host, dest := scp.ParseSCPDestination(last)
if login != "" {
tc.HostLogin = login
}
Expand All @@ -1013,16 +1066,32 @@ func (tc *TeleportClient) SCP(ctx context.Context, args []string, port int, recu
if err != nil {
return trace.Wrap(err)
}

// copy everything except the last arg (that's destination)
for _, src := range args[:len(args)-1] {
err = client.Upload(src, dest, recursive, tc.Stderr, progressWriter)
scpConfig := scp.Config{
User: tc.Username,
ProgressWriter: progressWriter,
RemoteLocation: dest,
Flags: scp.Flags{
Target: []string{src},
Recursive: recursive,
},
}

cmd, err := scp.CreateUploadCommand(scpConfig)
if err != nil {
return trace.Wrap(err)
}

err = client.ExecuteSCP(cmd)
if err != nil {
return onError(err)
}
}
// download:
} else {
login, host, src := parseSCPDestination(first)
login, host, src := scp.ParseSCPDestination(first)
addr := net.JoinHostPort(host, strconv.Itoa(port))
if login != "" {
tc.HostLogin = login
Expand All @@ -1033,7 +1102,22 @@ func (tc *TeleportClient) SCP(ctx context.Context, args []string, port int, recu
}
// copy everything except the last arg (that's destination)
for _, dest := range args[1:] {
err = client.Download(src, dest, recursive, tc.Stderr, progressWriter)
scpConfig := scp.Config{
User: tc.Username,
Flags: scp.Flags{
Recursive: recursive,
Target: []string{dest},
},
RemoteLocation: src,
ProgressWriter: progressWriter,
}

cmd, err := scp.CreateDownloadCommand(scpConfig)
if err != nil {
return trace.Wrap(err)
}

err = client.ExecuteSCP(cmd)
if err != nil {
return onError(err)
}
Expand All @@ -1042,28 +1126,6 @@ func (tc *TeleportClient) SCP(ctx context.Context, args []string, port int, recu
return nil
}

// parseSCPDestination takes a string representing a remote resource for SCP
// to download/upload, like "user@host:/path/to/resource.txt" and returns
// 3 components of it
func parseSCPDestination(s string) (login, host, dest string) {
parts := strings.SplitN(s, "@", 2)
if len(parts) > 1 {
login = parts[0]
host = parts[1]
} else {
host = parts[0]
}
parts = strings.SplitN(host, ":", 2)
if len(parts) > 1 {
host = parts[0]
dest = parts[1]
}
if len(dest) == 0 {
dest = "."
}
return login, host, dest
}

func isRemoteDest(name string) bool {
return strings.IndexRune(name, ':') >= 0
}
Expand Down
21 changes: 3 additions & 18 deletions lib/client/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ limitations under the License.
package client

import (
"testing"

"github.com/gravitational/teleport/lib/utils"

"gopkg.in/check.v1"
"testing"
)

// register test suite
Expand Down Expand Up @@ -105,23 +107,6 @@ func (s *APITestSuite) TestParseLabels(c *check.C) {
c.Assert(err, check.NotNil)
}

func (s *APITestSuite) TestSCPParsing(c *check.C) {
user, host, dest := parseSCPDestination("[email protected]:/etc/nginx.conf")
c.Assert(user, check.Equals, "root")
c.Assert(host, check.Equals, "remote.host")
c.Assert(dest, check.Equals, "/etc/nginx.conf")

user, host, dest = parseSCPDestination("remote.host:/etc/nginx.co:nf")
c.Assert(user, check.Equals, "")
c.Assert(host, check.Equals, "remote.host")
c.Assert(dest, check.Equals, "/etc/nginx.co:nf")

user, host, dest = parseSCPDestination("remote.host:")
c.Assert(user, check.Equals, "")
c.Assert(host, check.Equals, "remote.host")
c.Assert(dest, check.Equals, ".")
}

func (s *APITestSuite) TestPortsParsing(c *check.C) {
// empty:
ports, err := ParsePortForwardSpec(nil)
Expand Down
45 changes: 7 additions & 38 deletions lib/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -520,45 +520,14 @@ func (proxy *ProxyClient) Close() error {
return proxy.Client.Close()
}

// Upload uploads local file(s) or to the remote server's destination path
func (client *NodeClient) Upload(srcPath, rDestPath string, recursive bool, stderr, progressWriter io.Writer) error {
scpConf := scp.Command{
Source: true,
Recursive: recursive,
Target: []string{srcPath},
Terminal: progressWriter,
}

// "impersonate" scp to a server
shellCmd := "/usr/bin/scp -t"
if recursive {
shellCmd += " -r"
}
shellCmd += (" " + rDestPath)
return client.scp(scpConf, shellCmd, stderr)
}

// Download downloads file or dir from the remote server
func (client *NodeClient) Download(remoteSourcePath, localDestinationPath string, recursive bool, stderr, progressWriter io.Writer) error {
scpConf := scp.Command{
Sink: true,
Recursive: recursive,
Target: []string{localDestinationPath},
Terminal: progressWriter,
}

// "impersonate" scp to a server
shellCmd := "/usr/bin/scp -f"
if recursive {
shellCmd += " -r"
// ExecuteSCP runs remote scp command(shellCmd) on the remote server and
// runs local scp handler using SCP Command
func (client *NodeClient) ExecuteSCP(cmd scp.Command) error {
shellCmd, err := cmd.GetRemoteShellCmd()
if err != nil {
return trace.Wrap(err)
}
shellCmd += (" " + remoteSourcePath)
return client.scp(scpConf, shellCmd, stderr)
}

// scp runs remote scp command(shellCmd) on the remote server and
// runs local scp handler using scpConf
func (client *NodeClient) scp(scpCommand scp.Command, shellCmd string, errWriter io.Writer) error {
s, err := client.Client.NewSession()
if err != nil {
return trace.Wrap(err)
Expand All @@ -585,7 +554,7 @@ func (client *NodeClient) scp(scpCommand scp.Command, shellCmd string, errWriter

closeC := make(chan interface{}, 1)
go func() {
if err = scpCommand.Execute(ch); err != nil {
if err = cmd.Execute(ch); err != nil {
log.Error(err)
}
stdin.Close()
Expand Down
Loading