Beispiel #1
0
def bastio_main():
    """Main application entry point."""
    signal.signal(signal.SIGINT, __sig_handler)
    signal.signal(signal.SIGTERM, __sig_handler)
    cfg = GlobalConfigStore()

    # Parse command line arguments
    cmd = CommandLine()
    command = cmd.parse()

    if command == 'generate-key':
        try:
            key = RSAKey.generate(cfg.bits)
            key.write_private_key_file(cfg.agentkey)
        except Exception as ex:
            _die(ex.message)
        _die("generated {}-bit key successfully".format(cfg.bits), True)
    elif command == 'upload-key':
        try:
            agentkey = RSAKey.from_private_key_file(cfg.agentkey)
        except Exception as ex:
            _die(ex.message)

        try:
            # Replace old key
            if cfg.new_agentkey:
                new_agentkey = RSAKey.from_private_key_file(cfg.new_agentkey)
                upload_public_key(cfg.apikey, new_agentkey.get_public_key(),
                        agentkey.get_public_key())
            else:
                upload_public_key(cfg.apikey, agentkey.get_public_key())
        except Exception as ex:
            _die(ex.message)
        _die("uploaded public key successfully", True)
    elif command == 'start':
        try:
            cfg.agent_username = cfg.apikey
            cfg.agentkey = RSAKey.from_private_key_file(cfg.agentkey)
            cfg.backend_hostkey = download_backend_hostkey()
        except Exception as ex:
            _die(ex.message)

    ### All that is below is part of the ``start`` command

    # Set logging based on debug status
    if cfg.debug:
        Logger().enable_stream()
    else:
        Logger().enable_syslog()

    cfg.threadpool = GlobalThreadPool(cfg.minthreads)
    cfg.processor = Processor()
    cfg.connector = BackendConnector()
    cfg.connector.register(cfg.processor.endpoint())
    cfg.connector.start()
    signal.pause()