Exemple #1
0
    def provider_hosts(self):
        """Retrieve SecureCRT sessions from xml config."""
        result_hosts = []
        tree = self.parser.parse_hosts()

        root_group = Group(label='SecureCRT')

        identity_paths = self.parser.parse_identity()
        if identity_paths:
            self.logger.info('Found private key path: %s', identity_paths[0])
            self.logger.info('Found public key path: %s', identity_paths[1])
            try:
                key = self.create_key(identity_paths)
                root_group.ssh_config = SshConfig(
                    identity=Identity(
                        ssh_key=key,
                        label='SecureCRT',
                        is_visible=False
                    )
                )
            except IOError:
                self.logger.info(
                    'Warning: cannot import SSH2 raw key %s',
                    identity_paths[1]
                )

        self.create_entries_from_tree(tree, result_hosts, root_group)
        self.logger.info('Parsed hosts %i', len(result_hosts))
        self.logger.info('Importing...')
        return result_hosts
Exemple #2
0
 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')
Exemple #3
0
    def create_entries_from_tree(self, tree, result_hosts, parent_group=None):
        """Create instances from groups tree."""
        for label, node in tree.items():
            if not isinstance(node, dict):
                continue

            if not node.get('__group', None):
                result_hosts.append(self.create_host(node, parent_group))
            else:
                group = Group(label=label, parent_group=parent_group)
                self.create_entries_from_tree(node, result_hosts, group)
Exemple #4
0
    def provider_hosts(self):
        """Retrieve host instances from ssh config."""
        parser = SSHConfigParser()
        with Path(self.user_config).open() as config:
            parser.parse(config)

        parsed_hosts = [
            i for i in parser.get_hostnames() if self.is_endhost(i)
        ]

        to_import = []
        parsed_group = None
        for alias in parsed_hosts:
            parsed_group_name = parser._config[parsed_hosts.index(alias) + 1]\
                .get("group", None)
            if parsed_group_name:
                matching_groups = [
                    x for x in self.storage.get_all(Group)
                    if x['label'] == parsed_group_name
                ]
                if len(matching_groups) == 0:
                    g = Group(label=parsed_group_name)
                    self.storage.create(g)
                    matching_groups = [g]

                item = matching_groups.pop()
                parsed_group = Group(**item)
            parsed_host = parser.lookup(alias)

            if 'ignore' in parsed_host:
                continue

            to_import.append(
                self.adapter.adapt_ssh_config_host_to_instance(
                    alias, parsed_host, parsed_group))

        return to_import
    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_tree_parsing(self):
        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 name="hosts">
                <key name="folder0">
                    <key name="host1">
                        <dword name="[SSH2] Port">1</dword>
                        <string name="Hostname">addr1</string>
                        <string name="Username">user1</string>
                    </key>
                </key>
                <key name="folder1">
                    <key name="host2">
                        <dword name="[SSH2] Port">2</dword>
                        <string name="Hostname">addr2</string>
                        <string name="Username">user2</string>
                    </key>
                    <key name="serial">
                        <string name="Username">serial_user</string>
                    </key>
                </key>
            </key>
        </key>
    </VanDyke>
    """
        xml_path = '/some/path/securecrt.xml'
        root_group = Group(label='SecureCRT')
        hosts_folder = Group(
            label='hosts', parent_group=root_group
        )
        expected = [
            Host(
                label='host2',
                address='addr2',
                group=Group(label='folder1', parent_group=hosts_folder),
                ssh_config=SshConfig(
                    port='2',
                    identity=Identity(
                        label='user2', username='******', is_visible=False
                    )
                )
            ),
            Host(
                label='host1',
                address='addr1',
                group=Group(label='folder0', parent_group=hosts_folder),
                ssh_config=SshConfig(
                    port='1',
                    identity=Identity(
                        label='user1', username='******', is_visible=False
                    )
                )
            ),
            Host(
                label='host0', address='addr0', group=root_group,
                ssh_config=SshConfig(
                    port='0',
                    identity=Identity(
                        label='user0', username='******', is_visible=False
                    )
                )
            )
        ]

        with patch('xml.etree.ElementTree.open',
                   mock_open(read_data=securecrt_config)) as mocked_xml:
            service = SecureCRTPortingProvider(xml_path, None, '')
            hosts = service.provider_hosts()
            mocked_xml.assert_called_once_with(xml_path, 'rb')
            self.assertEquals(
                self.sort_hosts(hosts),
                self.sort_hosts(expected)
            )