def test_getPrivateKeyPassphrase(self): """ L{SSHUserAuthClient} can get a private key from a file, and return a Deferred called back with a private L{Key} object, even if the key is encrypted. """ rsaPrivate = Key.fromString(keydata.privateRSA_openssh) passphrase = 'this is the passphrase' self.rsaFile.setContent(rsaPrivate.toString('openssh', passphrase)) options = ConchOptions() options.identitys = [self.rsaFile.path] client = SSHUserAuthClient("user", options, None) # Populate the list of used files client.getPublicKey() def _getPassword(prompt): self.assertEqual(prompt, "Enter passphrase for key '%s': " % ( self.rsaFile.path,)) return passphrase def _cbGetPrivateKey(key): self.assertEqual(key.isPublic(), False) self.assertEqual(key, rsaPrivate) self.patch(client, '_getPassword', _getPassword) return client.getPrivateKey().addCallback(_cbGetPrivateKey)
def test_getPublicKeyFromFile(self): """ L{SSHUserAuthClient.getPublicKey()} is able to get a public key from the first file described by its options' C{identitys} list, and return the corresponding public L{Key} object. """ options = ConchOptions() options.identitys = [self.rsaFile.path] client = SSHUserAuthClient("user", options, None) key = client.getPublicKey() self.assertEqual(key.isPublic(), True) self.assertEqual(key, self.rsaPublic)
def test_getPublicKeyAgentFallback(self): """ If an agent is present, but doesn't return a key, L{SSHUserAuthClient.getPublicKey} continue with the normal key lookup. """ options = ConchOptions() options.identitys = [self.rsaFile.path] agent = SSHAgentClient() client = SSHUserAuthClient("user", options, None) client.keyAgent = agent key = client.getPublicKey() self.assertEqual(key.isPublic(), True) self.assertEqual(key, self.rsaPublic)
def test_host_key_algorithms(self): """ Specify host key algorithms. """ opts = ConchOptions() e = self.assertRaises(SystemExit, opts.opt_host_key_algorithms, "invalid-key") self.assertIn("Unknown host key type", e.code) opts = ConchOptions() opts.opt_host_key_algorithms("ssh-rsa") self.assertEqual(opts['host-key-algorithms'], [b"ssh-rsa"]) opts.opt_host_key_algorithms(b"ssh-dss") self.assertEqual(opts['host-key-algorithms'], [b"ssh-dss"]) opts.opt_host_key_algorithms("ssh-rsa,ssh-dss") self.assertEqual(opts['host-key-algorithms'], [b"ssh-rsa", b"ssh-dss"])
def test_macs(self): """ Specify MAC algorithms. """ opts = ConchOptions() e = self.assertRaises(SystemExit, opts.opt_macs, "invalid-mac") self.assertIn("Unknown mac type", e.code) opts = ConchOptions() opts.opt_macs("hmac-sha2-512") self.assertEqual(opts['macs'], [b"hmac-sha2-512"]) opts.opt_macs(b"hmac-sha2-512") self.assertEqual(opts['macs'], [b"hmac-sha2-512"]) opts.opt_macs("hmac-sha2-256,hmac-sha1,hmac-md5") self.assertEqual(opts['macs'], [b"hmac-sha2-256", b"hmac-sha1", b"hmac-md5"])
def test_getGenericAnswers(self): """ L{twisted.conch.client.default.SSHUserAuthClient.getGenericAnswers} """ options = ConchOptions() client = SSHUserAuthClient(b"user", options, None) def getpass(prompt): self.assertEqual(prompt, "pass prompt") return "getpass" self.patch(default.getpass, "getpass", getpass) def raw_input(prompt): self.assertEqual(prompt, "raw_input prompt") return "raw_input" self.patch(default, "_input", raw_input) d = client.getGenericAnswers( b"Name", b"Instruction", [(b"pass prompt", False), (b"raw_input prompt", True)], ) d.addCallback(self.assertListEqual, ["getpass", "raw_input"]) return d
def test_getPassword(self): """ Get the password using L{twisted.conch.client.default.SSHUserAuthClient.getPassword} """ class FakeTransport: def __init__(self, host): self.transport = self self.host = host def getPeer(self): return self options = ConchOptions() client = SSHUserAuthClient(b"user", options, None) client.transport = FakeTransport("127.0.0.1") def getpass(prompt): self.assertEqual(prompt, "[email protected]'s password: "******"bad password" self.patch(default.getpass, "getpass", getpass) d = client.getPassword() d.addCallback(self.assertEqual, b"bad password") return d
def connectionSecure(self): self._secured = True conn = _CommandConnection(self.factory) userauth = SSHUserAuthClient(self.factory.server.user, ConchOptions(), conn) userauth.preferredOrder = ['publickey'] self.requestService(userauth)
def test_getPublicKeyBadKeyError(self): """ If L{keys.Key.fromFile} raises a L{keys.BadKeyError}, the L{SSHUserAuthClient.getPublicKey} tries again to get a public key by calling itself recursively. """ options = ConchOptions() self.tmpdir.child('id_dsa.pub').setContent(keydata.publicDSA_openssh) dsaFile = self.tmpdir.child('id_dsa') dsaFile.setContent(keydata.privateDSA_openssh) options.identitys = [self.rsaFile.path, dsaFile.path] self.tmpdir.child('id_rsa.pub').setContent('not a key!') client = SSHUserAuthClient("user", options, None) key = client.getPublicKey() self.assertEqual(key.isPublic(), True) self.assertEqual(key, Key.fromString(keydata.publicDSA_openssh)) self.assertEqual(client.usedFiles, [self.rsaFile.path, dsaFile.path])
def connectionSecure(self): self._secured = True command = _CommandConnection(self.factory.command, self.factory.commandProtocolFactory, self.factory.commandConnected) userauth = SSHUserAuthClient(os.environ['USER'], ConchOptions(), command) self.requestService(userauth)
def connectionSecure(self): self._secured = True connection = _CommandConnection(self.factory.command) self.connection = connection userauth = SSHUserAuthClient( self.factory.user, ConchOptions(), connection) self.requestService(userauth)
def test_getPrivateKey(self): """ L{SSHUserAuthClient.getPrivateKey} will load a private key from the last used file populated by L{SSHUserAuthClient.getPublicKey}, and return a L{Deferred} which fires with the corresponding private L{Key}. """ rsaPrivate = Key.fromString(keydata.privateRSA_openssh) options = ConchOptions() options.identitys = [self.rsaFile.path] client = SSHUserAuthClient("user", options, None) # Populate the list of used files client.getPublicKey() def _cbGetPrivateKey(key): self.assertEqual(key.isPublic(), False) self.assertEqual(key, rsaPrivate) return client.getPrivateKey().addCallback(_cbGetPrivateKey)
def _ssh_options_from_config(self, ssh_conf): ssh_options = ConchOptions() if ssh_conf.agent: if 'SSH_AUTH_SOCK' in os.environ: ssh_options['agent'] = True else: raise ConfigError("No SSH Agent available ($SSH_AUTH_SOCK)") else: ssh_options['noagent'] = True for file_name in ssh_conf.identities: file_path = os.path.expanduser(file_name) if not os.path.exists(file_path): raise ConfigError("Private key file '%s' doesn't exist" % file_name) if not os.path.exists(file_path + ".pub"): raise ConfigError("Public key '%s' doesn't exist" % (file_name + ".pub")) ssh_options.opt_identity(file_name) return ssh_options
def test_signDataWithAgent(self): """ When connected to an agent, L{SSHUserAuthClient} can use it to request signatures of particular data with a particular L{Key}. """ client = SSHUserAuthClient("user", ConchOptions(), None) agent = SSHAgentClient() transport = StringTransport() agent.makeConnection(transport) client.keyAgent = agent cleartext = "Sign here" client.signData(self.rsaPublic, cleartext) self.assertEqual( transport.value(), "\x00\x00\x00\x8b\r\x00\x00\x00u" + self.rsaPublic.blob() + "\x00\x00\x00\t" + cleartext + "\x00\x00\x00\x00")
def test_getPasswordPrompt(self): """ Get the password using L{twisted.conch.client.default.SSHUserAuthClient.getPassword} using a different prompt. """ options = ConchOptions() client = SSHUserAuthClient(b"user", options, None) prompt = b"Give up your password" def getpass(p): self.assertEqual(p, nativeString(prompt)) return "bad password" self.patch(default.getpass, "getpass", getpass) d = client.getPassword(prompt) d.addCallback(self.assertEqual, b"bad password") return d
def test_getPasswordConchError(self): """ Get the password using L{twisted.conch.client.default.SSHUserAuthClient.getPassword} and trigger a {twisted.conch.error import ConchError}. """ options = ConchOptions() client = SSHUserAuthClient(b"user", options, None) def getpass(prompt): raise KeyboardInterrupt("User pressed CTRL-C") self.patch(default.getpass, 'getpass', getpass) stdout, stdin = sys.stdout, sys.stdin d = client.getPassword(b'?') @d.addErrback def check_sys(fail): self.assertEqual( [stdout, stdin], [sys.stdout, sys.stdin]) return fail self.assertFailure(d, ConchError)
def _defaultAuthFactory(self, command): return SSHUserAuthClient( os.environ['USER'], ConchOptions(), command)