Skip to content

Commit

Permalink
remove ssh-agent requirement for integration tests
Browse files Browse the repository at this point in the history
Thanks @tv42 for this code. I took it from his PR; pkg#92.
  • Loading branch information
eikenb committed Aug 23, 2017
1 parent 13ec216 commit 98203f5
Showing 1 changed file with 48 additions and 1 deletion.
49 changes: 48 additions & 1 deletion server_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@ package sftp

import (
"bytes"
"crypto/ecdsa"
"crypto/elliptic"
crand "crypto/rand"
"crypto/x509"
"encoding/hex"
"encoding/pem"
"flag"
"fmt"
"io/ioutil"
Expand Down Expand Up @@ -357,17 +362,59 @@ func testServer(t *testing.T, useSubsystem bool, readonly bool) (net.Listener, s
return listener, host, port
}

func makeDummyKey() (string, error) {
priv, err := ecdsa.GenerateKey(elliptic.P256(), crand.Reader)
if err != nil {
return "", fmt.Errorf("cannot generate key: %v", err)
}
der, err := x509.MarshalECPrivateKey(priv)
if err != nil {
return "", fmt.Errorf("cannot marshal key: %v", err)
}
block := &pem.Block{Type: "EC PRIVATE KEY", Bytes: der}
f, err := ioutil.TempFile("", "sftp-test-key-")
if err != nil {
return "", fmt.Errorf("cannot create temp file: %v", err)
}
defer func() {
if f != nil {
_ = f.Close()
_ = os.Remove(f.Name())
}
}()
if err := pem.Encode(f, block); err != nil {
return "", fmt.Errorf("cannot write key: %v", err)
}
if err := f.Close(); err != nil {
return "", fmt.Errorf("error closing key file: %v", err)
}
path := f.Name()
f = nil
return path, nil
}

func runSftpClient(t *testing.T, script string, path string, host string, port int) (string, error) {
// if sftp client binary is unavailable, skip test
if _, err := os.Stat(*testSftpClientBin); err != nil {
t.Skip("sftp client binary unavailable")
}

// make a dummy key so we don't rely on ssh-agent
dummyKey, err := makeDummyKey()
if err != nil {
return "", err
}
defer os.Remove(dummyKey)

args := []string{
// "-vvvv",
"-b", "-",
"-o", "StrictHostKeyChecking=no",
"-o", "LogLevel=ERROR",
"-o", "UserKnownHostsFile /dev/null",
// do not trigger ssh-agent prompting
"-o", "IdentityFile=" + dummyKey,
"-o", "IdentitiesOnly=yes",
"-P", fmt.Sprintf("%d", port), fmt.Sprintf("%s:%s", host, path),
}
cmd := exec.Command(*testSftpClientBin, args...)
Expand All @@ -378,7 +425,7 @@ func runSftpClient(t *testing.T, script string, path string, host string, port i
if err := cmd.Start(); err != nil {
return "", err
}
err := cmd.Wait()
err = cmd.Wait()
return string(stdout.Bytes()), err
}

Expand Down

0 comments on commit 98203f5

Please sign in to comment.