Example #1
0
def _create_host(ws, host_data, command=None):
    hostnames = host_data.pop('hostnames', [])
    services = host_data.pop('services')
    credentials = host_data.pop('credentials')
    vulns = host_data.pop('vulnerabilities')
    (created, host) = get_or_create(ws, Host, host_data)
    for name in set(hostnames).difference(
            set(map(lambda x: x.name, host.hostnames))):
        db.session.add(Hostname(name=name, host=host, workspace=ws))
    db.session.commit()

    if command is not None:
        _create_command_object_for(ws, created, host, command)

    total_services = len(services)
    if total_services > 0:
        logger.debug(f"Needs to create {total_services} services...")
        for service_data in services:
            _create_service(ws, host, service_data, command)

    total_vulns = len(vulns)
    if total_vulns > 0:
        logger.debug(f"Needs to create {total_vulns} vulns...")
        for vuln_data in vulns:
            _create_hostvuln(ws, host, vuln_data, command)

    total_credentials = len(credentials)
    if total_credentials > 0:
        logger.debug(f"Needs to create {total_credentials} credentials...")
        for cred_data in credentials:
            _create_credential(ws, cred_data, command, host=host)
Example #2
0
    def test_all(self, host, session):
        a = Hostname(workspace=host.workspace, host=host, name='a')
        b = Hostname(workspace=host.workspace, host=host, name='b')
        session.add(a)
        session.add(b)
        session.commit()

        host.set_hostnames(['b', 'c'])

        # a should be deleted
        assert len(session.deleted) == 1
        assert session.deleted.pop() is a

        # c should be created
        assert len(session.new) == 1
        c = session.new.pop()
        assert c.name == 'c'

        session.commit()
        assert set(hn.name for hn in host.hostnames) == {'b', 'c'}
Example #3
0
    def test_change_one(self, host, session):
        hn = Hostname(workspace=host.workspace, host=host, name='x')
        session.add(hn)
        session.commit()
        host.set_hostnames(['y'])

        assert len(session.deleted) == 1
        assert session.deleted.pop() is hn

        assert len(session.new) == 1
        new = session.new.pop()
        assert new.name == 'y'

        session.commit()
        assert len(host.hostnames) == 1
        assert host.hostnames[0].name == 'y'
Example #4
0
def _create_host(ws, host_data, command=None):
    hostnames = host_data.pop('hostnames', [])
    services = host_data.pop('services')
    credentials = host_data.pop('credentials')
    vulns = host_data.pop('vulnerabilities')
    (created, host) = get_or_create(ws, Host, host_data)
    if created:
        for name in hostnames:
            db.session.add(Hostname(name=name, host=host, workspace=ws))
    db.session.commit()

    if command is not None:
        _create_command_object_for(ws, created, host, command)

    for service_data in services:
        _create_service(ws, host, service_data, command)

    for vuln_data in vulns:
        _create_hostvuln(ws, host, vuln_data, command)

    for cred_data in credentials:
        _create_credential(ws, cred_data, command, host=host)