summaryrefslogtreecommitdiff
path: root/internal/ssh
diff options
context:
space:
mode:
authorPaul Buetow <pbuetow@mimecast.com>2021-10-27 09:54:06 +0300
committerPaul Buetow <pbuetow@mimecast.com>2021-10-27 09:54:06 +0300
commitd0492e3f63f86ce053f59362a55d23cf6397d526 (patch)
treeefe6668328b58db3e14d3b6c408295e0f3bf85dd /internal/ssh
parent7c927e7e6d913168798d245a7ceea32a9ca85643 (diff)
integration tests use separate ssh private key file
Diffstat (limited to 'internal/ssh')
-rw-r--r--internal/ssh/client/authmethods.go40
-rw-r--r--internal/ssh/client/clientkeypair.go91
2 files changed, 117 insertions, 14 deletions
diff --git a/internal/ssh/client/authmethods.go b/internal/ssh/client/authmethods.go
index 2ee32ad..49cf938 100644
--- a/internal/ssh/client/authmethods.go
+++ b/internal/ssh/client/authmethods.go
@@ -25,27 +25,44 @@ func InitSSHAuthMethods(sshAuthMethods []gossh.AuthMethod,
return initKnownHostsAuthMethods(trustAllHosts, throttleCh, privateKeyPath)
}
+func initIntegrationTestKnownHostsAuthMethods() []gossh.AuthMethod {
+ var sshAuthMethods []gossh.AuthMethod
+ privateKeyPath := "./id_rsa"
+
+ GeneratePrivatePublicKeyPairIfNotExists(privateKeyPath, 4096)
+ authMethod, err := ssh.PrivateKey(privateKeyPath)
+ if err != nil {
+ dlog.Client.FatalPanic("Unable to use private SSH key", privateKeyPath, err)
+ }
+
+ sshAuthMethods = append(sshAuthMethods, authMethod)
+ dlog.Client.Debug("initKnownHostsAuthMethods",
+ "Added path to list of auth methods, not adding further methods", privateKeyPath)
+ return sshAuthMethods
+}
+
func initKnownHostsAuthMethods(trustAllHosts bool, throttleCh chan struct{},
privateKeyPath string) ([]gossh.AuthMethod, HostKeyCallback) {
- var sshAuthMethods []gossh.AuthMethod
knownHostsFile := config.SSHKnownHostsFile()
- knownHostsCallback, err := NewKnownHostsCallback(knownHostsFile, trustAllHosts,
- throttleCh)
+ knownHostsCallback, err := NewKnownHostsCallback(knownHostsFile, trustAllHosts, throttleCh)
if err != nil {
dlog.Client.FatalPanic(knownHostsFile, err)
}
-
dlog.Client.Debug("initKnownHostsAuthMethods", "Added known hosts file path", knownHostsFile)
+ if config.Env("DTAIL_INTEGRATION_TEST_RUN_MODE") {
+ return initIntegrationTestKnownHostsAuthMethods(), knownHostsCallback
+ }
+
+ var sshAuthMethods []gossh.AuthMethod
// First try to read custom private key path.
if privateKeyPath != "" {
authMethod, err := ssh.PrivateKey(privateKeyPath)
if err == nil {
sshAuthMethods = append(sshAuthMethods, authMethod)
dlog.Client.Debug("initKnownHostsAuthMethods",
- "Added path to list of auth methods, not adding further methods",
- privateKeyPath)
+ "Added path to list of auth methods, not adding further methods", privateKeyPath)
return sshAuthMethods, knownHostsCallback
}
dlog.Client.FatalPanic("Unable to use private SSH key", privateKeyPath, err)
@@ -59,8 +76,7 @@ func initKnownHostsAuthMethods(trustAllHosts bool, throttleCh chan struct{},
"to list of auth methods, not adding further methods")
return sshAuthMethods, knownHostsCallback
}
- dlog.Client.Debug("initKnownHostsAuthMethods",
- "Unable to init SSH Agent auth method", err)
+ dlog.Client.Debug("initKnownHostsAuthMethods", "Unable to init SSH Agent auth method", err)
// Third, try Linux/UNIX default key paths
privateKeyPath = os.Getenv("HOME") + "/.ssh/id_rsa"
@@ -71,8 +87,7 @@ func initKnownHostsAuthMethods(trustAllHosts bool, throttleCh chan struct{},
"Added path to list of auth methods, not adding further methods", privateKeyPath)
return sshAuthMethods, knownHostsCallback
}
- dlog.Client.Debug("initKnownHostsAuthMethods", "Unable to use private key",
- privateKeyPath, err)
+ dlog.Client.Debug("initKnownHostsAuthMethods", "Unable to use private key", privateKeyPath, err)
privateKeyPath = os.Getenv("HOME") + "/.ssh/id_dsa"
authMethod, err = ssh.PrivateKey(privateKeyPath)
@@ -92,10 +107,7 @@ func initKnownHostsAuthMethods(trustAllHosts bool, throttleCh chan struct{},
return sshAuthMethods, knownHostsCallback
}
- dlog.Client.Debug("initKnownHostsAuthMethods", "Unable to use private key",
- privateKeyPath, err)
-
- dlog.Client.FatalPanic("Unable to find private SSH key information")
+ dlog.Client.FatalPanic("Unable to find private SSH key information", privateKeyPath, err)
// Never reach this point.
return sshAuthMethods, knownHostsCallback
}
diff --git a/internal/ssh/client/clientkeypair.go b/internal/ssh/client/clientkeypair.go
new file mode 100644
index 0000000..0e21d0c
--- /dev/null
+++ b/internal/ssh/client/clientkeypair.go
@@ -0,0 +1,91 @@
+package client
+
+import (
+ "crypto/rand"
+ "crypto/rsa"
+ "crypto/x509"
+ "encoding/pem"
+ "fmt"
+ "io/ioutil"
+ "os"
+
+ "github.com/mimecast/dtail/internal/io/dlog"
+ "golang.org/x/crypto/ssh"
+)
+
+// GeneratePrivatePublicKeyPairIfNotExists generates a SSH key pair (used by the integration tests)
+func GeneratePrivatePublicKeyPairIfNotExists(keyPath string, bitSize int) {
+ if _, err := os.Stat(keyPath); err == nil {
+ dlog.Common.Debug("Private/public key pair already exists", keyPath)
+ return
+ }
+ GeneratePrivatePublicKeyPair(keyPath, bitSize)
+}
+
+// GeneratePrivatePublicKeyPair generates a SSH key pair (used by the integration tests)
+func GeneratePrivatePublicKeyPair(keyPath string, bitSize int) {
+ privateKeyPath := keyPath
+ publicKeyPath := fmt.Sprintf("%s.pub", keyPath)
+
+ dlog.Common.Debug("Generating private/public key pair", privateKeyPath, publicKeyPath)
+
+ privateKey, err := generatePrivateKey(bitSize)
+ if err != nil {
+ dlog.Common.FatalPanic(err)
+ }
+ publicKeyBytes, err := generatePublicKey(&privateKey.PublicKey)
+ if err != nil {
+ dlog.Common.FatalPanic(err)
+ }
+ privateKeyBytes := encodePrivateKeyToPEM(privateKey)
+ err = writeKey(privateKeyBytes, privateKeyPath)
+ if err != nil {
+ dlog.Common.FatalPanic(err)
+ }
+ err = writeKey([]byte(publicKeyBytes), publicKeyPath)
+ if err != nil {
+ dlog.Common.FatalPanic(err)
+ }
+
+ dlog.Common.Debug("Done generating private/public key pair", privateKeyPath, publicKeyPath)
+}
+
+func generatePrivateKey(bitSize int) (*rsa.PrivateKey, error) {
+ privateKey, err := rsa.GenerateKey(rand.Reader, bitSize)
+ if err != nil {
+ return nil, err
+ }
+ err = privateKey.Validate()
+ if err != nil {
+ return nil, err
+ }
+ return privateKey, nil
+}
+
+func encodePrivateKeyToPEM(privateKey *rsa.PrivateKey) []byte {
+ privDER := x509.MarshalPKCS1PrivateKey(privateKey)
+ privBlock := pem.Block{
+ Type: "RSA PRIVATE KEY",
+ Headers: nil,
+ Bytes: privDER,
+ }
+ privatePEM := pem.EncodeToMemory(&privBlock)
+ return privatePEM
+}
+
+func generatePublicKey(privatekey *rsa.PublicKey) ([]byte, error) {
+ publicRsaKey, err := ssh.NewPublicKey(privatekey)
+ if err != nil {
+ return nil, err
+ }
+ pubKeyBytes := ssh.MarshalAuthorizedKey(publicRsaKey)
+ return pubKeyBytes, nil
+}
+
+func writeKey(keyBytes []byte, saveFileTo string) error {
+ err := ioutil.WriteFile(saveFileTo, keyBytes, 0600)
+ if err != nil {
+ return err
+ }
+ return nil
+}