def setUp(self): super(IDStrategyCase, self).setUp() self.group = Group(label='host_test') self.host = Host(label='host_test') self.sshconfig = SshConfig(port=2) self.identity = Identity(username='******') self.sshkey = SshKey(label='label')
def test_single_sshconfig_with_keys(path_in_providers, path_in_adapters): sshconfig_content = """ Host firstone HostName localhost User ubuntu IdentityFile ~/.ssh/id_rsa IdentityFile ~/.ssh/id_dsa """ private_key_content = 'private_key' fake_files = FakePathsObj(**{ '~/.ssh/config': sshconfig_content, '~/.ssh/id_rsa': private_key_content }) path_in_providers.side_effect = fake_files.generate_path_obj path_in_adapters.side_effect = fake_files.generate_path_obj service = SSHPortingProvider(None, '') ssh_hosts = service.provider_hosts() eq_(ssh_hosts, [ Host(label='firstone', address='localhost', ssh_config=SshConfig( port=None, timeout=None, keep_alive_packages=None, use_ssh_key=None, strict_host_key_check=None, identity=Identity(username='******', ssh_key=SshKey(label='id_rsa', private_key='private_key')))) ])
def create_key(self, config): """Construct new application ssh key instance.""" if 'identityfile' not in config: return None identityfile = self.choose_ssh_key(config['identityfile'], config) if not identityfile: return None content = identityfile.read_text() return SshKey(label=identityfile.name, private_key=content)
def test_create_transformer_nosync_key(self): self.account_manager.set_settings({ 'synchronize_key': False, 'agent_forwarding': True }) self.transformer = BulkTransformer( storage=self.storage, crypto_controller=self.crypto_controller, account_manager=self.account_manager, ) ssh_config = SshConfig() identity = Identity(label='identity') ssh_key = SshKey(label='ssh_key') host = Host(label='host') identity.ssh_key = ssh_key.id = self.storage.save(ssh_key).id ssh_config.identity = identity.id = self.storage.save(identity).id host.ssh_config = ssh_config.id = self.storage.save(ssh_config).id host = self.storage.save(host) last_synced_data = dict(last_synced='') payload = self.transformer.to_payload(last_synced_data) self.assertEqual( payload, { 'host_set': [{ 'label': payload['host_set'][0]['label'], 'group': None, 'ssh_config': 'sshconfig_set/{}'.format(ssh_config.id), 'local_id': host.id, 'address': '', 'interaction_date': host.interaction_date, }], 'sshconfig_set': [{ 'font_size': None, 'keep_alive_packages': None, 'charset': None, 'local_id': ssh_config.id, 'use_ssh_key': None, 'timeout': None, 'color_scheme': None, 'is_forward_ports': None, 'strict_host_key_check': None, 'port': None, 'startup_snippet': None, 'cursor_blink': None }], 'snippet_set': [], 'last_synced': '', 'group_set': [], 'tag_set': [], 'taghost_set': [], 'pfrule_set': [], 'delete_sets': {}, }) eq_(host.label, self.cryptor.decrypt(payload['host_set'][0]['label']))
def create_key(self, identity_paths): """Create ssh key instance.""" with open(identity_paths[0], 'rb') as private_key_file: private_key = private_key_file.read() with open(identity_paths[1], 'rb') as public_key_file: public_key = public_key_file.read() return SshKey(label='SecureCRT', private_key=private_key, public_key=public_key)
def test_ssh2_identity_parsing(self): public_key = b'public' private_key = b'private' xml_path = '/some/path/securecrt.xml' root_group = Group( label='SecureCRT', ssh_config=SshConfig( identity=Identity( label='SecureCRT', is_visible=False, ssh_key=SshKey( label='SecureCRT', private_key=private_key, public_key=public_key ) ) ) ) securecrt_config = """<?xml version="1.0" encoding="UTF-8"?> <VanDyke version="3.0"> <key name="Sessions"> <key name="host0"> <dword name="[SSH2] Port">0</dword> <string name="Hostname">addr0</string> <string name="Username">user0</string> </key> </key> <key name="SSH2"> <string name="Identity Filename V2">/Users/termius/folder/key.pub::rawkey</string> </key> </VanDyke> """ expected = [ Host( label='host0', group=root_group, address='addr0', ssh_config=SshConfig(port='0', identity=Identity( label='user0', is_visible=False, username='******' ) ) ) ] expected_calls = [ call('/Users/termius/folder/key', 'rb'), call('/Users/termius/folder/key.pub', 'rb') ] with patch('xml.etree.ElementTree.open', mock_open(read_data=securecrt_config)) as mocked_xml: with patch('termius.porting.providers.securecrt.provider.open', mock_open(read_data='')) as mocked_open: service = SecureCRTPortingProvider(xml_path, None, '') mocked_open.side_effect = [ BytesIO(private_key), BytesIO(public_key) ] hosts = service.provider_hosts() self.assertEquals( self.sort_hosts(hosts), self.sort_hosts(expected) ) mocked_xml.assert_called_once_with(xml_path, 'rb') mocked_open.assert_has_calls(expected_calls)
def test_create_transformer_sync_key(self): self.transformer = self.get_transformer() ssh_config = SshConfig() identity = Identity(label='identity') ssh_key = SshKey(label='ssh_key') host = Host(label='host') identity.ssh_key = ssh_key.id = self.storage.save(ssh_key).id ssh_config.identity = identity.id = self.storage.save(identity).id host.ssh_config = ssh_config.id = self.storage.save(ssh_config).id host = self.storage.save(host) last_synced_data = dict(last_synced='') payload = self.transformer.to_payload(last_synced_data) self.assertEqual( payload, { 'host_set': [{ 'label': payload['host_set'][0]['label'], 'group': None, 'ssh_config': 'sshconfig_set/{}'.format(ssh_config.id), 'local_id': host.id, 'address': None, 'interaction_date': host.interaction_date, }], 'sshconfig_set': [{ 'font_size': None, 'keep_alive_packages': None, 'charset': None, 'identity': 'identity_set/{}'.format(identity.id), 'local_id': ssh_config.id, 'use_ssh_key': None, 'timeout': None, 'color_scheme': None, 'is_forward_ports': None, 'strict_host_key_check': None, 'port': None, 'startup_snippet': None, 'cursor_blink': None }], 'snippet_set': [], 'last_synced': '', 'sshkeycrypt_set': [ { 'public_key': None, 'private_key': None, 'local_id': ssh_key.id, 'label': payload['sshkeycrypt_set'][0]['label'] } ], 'group_set': [], 'tag_set': [], 'taghost_set': [], 'pfrule_set': [], 'delete_sets': {}, 'identity_set': [ { 'username': None, 'is_visible': None, 'ssh_key': 'sshkeycrypt_set/{}'.format(ssh_key.id), 'label': payload['identity_set'][0]['label'], 'local_id': identity.id } ] }) eq_(host.label, self.cryptor.decrypt(payload['host_set'][0]['label'])) eq_(ssh_key.label, self.cryptor.decrypt(payload['sshkeycrypt_set'][0]['label'])) eq_(identity.label, self.cryptor.decrypt(payload['identity_set'][0]['label']))