def test_no_local_network(testdir):
    testdir.makepyfile("""
        import pytest

        @pytest.mark.requires_network
        def test_one():
            assert True
        """)
    mock_socket = mock.MagicMock()
    mock_socket.bind = mock.MagicMock(side_effect=socket.error)
    with mock.patch(
            "saltfactories.utils.ports.get_unused_localhost_port",
            side_effect=[
                ports.get_unused_localhost_port(),
                ports.get_unused_localhost_port()
            ],
    ):
        with mock.patch("socket.socket", return_value=mock_socket):
            res = testdir.runpytest_inprocess("-p",
                                              "no:salt-factories-log-server")
            res.assert_outcomes(skipped=1)
    try:
        res.stdout.no_fnmatch_line("*PytestUnknownMarkWarning*")
    except AttributeError:  # pragma: no cover
        # PyTest 4.6.x
        from _pytest.outcomes import Failed

        with pytest.raises(Failed):
            res.stdout.fnmatch_lines([
                "*PytestUnknownMarkWarning*",
            ])
Beispiel #2
0
def salt_secondary_master(request, salt_factories):
    #
    # Enable a secondary Salt master so we can disable follow_symlinks
    #
    publish_port = get_unused_localhost_port()
    ret_port = get_unused_localhost_port()

    config_defaults = {
        "open_mode": True,
        "transport": request.config.getoption("--transport"),
    }
    config_overrides = {
        "interface": "127.0.0.1",
        "fileserver_followsymlinks": False,
        "publish_port": publish_port,
        "ret_port": ret_port,
    }

    factory = salt_factories.salt_master_daemon(
        "secondary-master",
        defaults=config_defaults,
        overrides=config_overrides,
        extra_cli_arguments_after_first_start_failure=["--log-level=debug"],
    )
    with factory.started(start_timeout=120):
        yield factory
    def default_config(root_dir,
                       minion_id,
                       config_defaults=None,
                       config_overrides=None,
                       master_port=None):
        if config_defaults is None:
            config_defaults = salt.config.DEFAULT_MINION_OPTS.copy()
            config_defaults.pop("user", None)

        conf_dir = root_dir / "conf"
        conf_dir.mkdir(parents=True, exist_ok=True)
        conf_file = str(conf_dir / "minion")

        _config_defaults = {
            "id": minion_id,
            "conf_file": conf_file,
            "root_dir": str(root_dir),
            "interface": "127.0.0.1",
            "master": "127.0.0.1",
            "master_port": master_port or ports.get_unused_localhost_port(),
            "tcp_pub_port": ports.get_unused_localhost_port(),
            "tcp_pull_port": ports.get_unused_localhost_port(),
            "pidfile": "run/minion.pid",
            "pki_dir": "pki",
            "cachedir": "cache",
            "sock_dir": "run/minion",
            "log_file": "logs/minion.log",
            "log_level_logfile": "debug",
            "loop_interval": 0.05,
            #'multiprocessing': False,
            "log_fmt_console":
            "%(asctime)s,%(msecs)03.0f [%(name)-17s:%(lineno)-4d][%(levelname)-8s][%(processName)18s(%(process)d)] %(message)s",
            "log_fmt_logfile":
            "[%(asctime)s,%(msecs)03.0f][%(name)-17s:%(lineno)-4d][%(levelname)-8s][%(processName)18s(%(process)d)] %(message)s",
            "hash_type": "sha256",
            "transport": "zeromq",
            "pytest-minion": {
                "log": {
                    "prefix": "{{cli_name}}({})".format(minion_id)
                },
            },
            "acceptance_wait_time": 0.5,
            "acceptance_wait_time_max": 5,
        }
        # Merge in the initial default options with the internal _config_defaults
        salt.utils.dictupdate.update(config_defaults,
                                     _config_defaults,
                                     merge_lists=True)

        if config_overrides:
            # Merge in the default options with the minion_config_overrides
            salt.utils.dictupdate.update(config_defaults,
                                         config_overrides,
                                         merge_lists=True)

        return config_defaults
def test_get_unused_localhost_port_cached():
    """
    Tests that test_get_unused_localhost_port only returns unique ports on consecutive calls
    """
    num_calls = 10
    start_port = 1000
    # The ports we're gonna get back
    ports = []
    for port in range(start_port, start_port + num_calls):
        for _ in range(num_calls):
            # We make sure each port is repeated consecutively
            ports.append(port)

    # Hold a reference to the list of unique ports
    unique = set(ports)

    # This list will hold all ports that the function returns
    got_ports = []

    # We'll get the unique ports
    with mock.patch(
            "saltfactories.utils.socket.socket",
            new_callable=functools.partial(MockedCreateSocket, ports),
    ):
        for _ in range(num_calls):
            got_ports.append(
                ports_utils.get_unused_localhost_port(use_cache=True))
        assert len(got_ports) == num_calls
        assert set(got_ports) == unique

    with mock.patch(
            "saltfactories.utils.socket.socket",
            new_callable=functools.partial(MockedCreateSocket, ports + ports),
    ):
        for _ in range(num_calls):
            with pytest.raises(IndexError):
                # we won't have enough ports
                got_ports.append(
                    ports_utils.get_unused_localhost_port(use_cache=True))
        # Since we couldn't get repeated ports, got_ports remains as it was
        assert len(got_ports) == num_calls
        assert set(got_ports) == unique

    # If we don't cache the port, we'll get repeated ports
    with mock.patch(
            "saltfactories.utils.socket.socket",
            new_callable=functools.partial(MockedCreateSocket, ports),
    ):
        for _ in range(num_calls):
            got_ports.append(ports_utils.get_unused_localhost_port())

        assert len(got_ports) == 2 * len(unique)
        assert set(got_ports) == unique
def test_get_unused_localhost_port_unique():
    """
    Tests that test_get_unused_localhost_port only returns unique ports on consecutive calls
    """
    num_calls = 10
    start_port = 1000
    # The ports we're gonna get back
    ports = []
    for port in range(start_port, start_port + num_calls):
        for _ in range(num_calls):
            # We make sure each port is repeated consecutively
            ports.append(port)

    # Hold a reference to the list of unique ports
    unique = set(ports)

    # This list will hold all ports that the function returns
    got_ports = []

    # We'll get the unique ports
    with mock.patch("socket.socket",
                    new_callable=functools.partial(MockedCreateSocket,
                                                   ports)) as mocked_socket:
        for _ in range(num_calls):
            got_ports.append(
                ports_utils.get_unused_localhost_port(cached_seconds=1))
        assert len(got_ports) == num_calls
        assert set(got_ports) == unique

    # Let's get ports again. Since not enough time has passed, we won't get any ports
    with mock.patch("socket.socket",
                    new_callable=functools.partial(MockedCreateSocket, ports +
                                                   ports)) as mocked_socket:
        for _ in range(num_calls):
            with pytest.raises(IndexError):
                # we won't have enough ports
                got_ports.append(
                    ports_utils.get_unused_localhost_port(cached_seconds=1))
        # Since we couldn't get repeated ports, got_ports remains as it was
        assert len(got_ports) == num_calls
        assert set(got_ports) == unique

    # Now, if we sleep one second, the cached ports will be gone and we'll get repeated ports
    time.sleep(1)
    with mock.patch("socket.socket",
                    new_callable=functools.partial(MockedCreateSocket,
                                                   ports)) as mocked_socket:
        for _ in range(num_calls):
            got_ports.append(
                ports_utils.get_unused_localhost_port(cached_seconds=1))

        assert len(got_ports) == 2 * len(unique)
        assert set(got_ports) == unique
Beispiel #6
0
def master_config(tmp_path):
    master_conf = salt.config.master_config("")
    master_conf["id"] = "master"
    master_conf["root_dir"] = str(tmp_path)
    master_conf["sock_dir"] = str(tmp_path)
    master_conf["interface"] = "127.0.0.1"
    master_conf["publish_port"] = get_unused_localhost_port()
    master_conf["ret_port"] = get_unused_localhost_port()
    master_conf["pki_dir"] = str(tmp_path / "pki")
    os.makedirs(master_conf["pki_dir"])
    salt.crypt.gen_keys(master_conf["pki_dir"], "master", 4096)
    minions_keys = os.path.join(master_conf["pki_dir"], "minions")
    os.makedirs(minions_keys)
    yield master_conf
Beispiel #7
0
def test_no_local_network():
    mock_socket = mock.MagicMock()
    mock_socket.bind = mock.MagicMock(side_effect=socket.error)
    with mock.patch(
            "saltfactories.utils.ports.get_unused_localhost_port",
            side_effect=[
                ports.get_unused_localhost_port(),
                ports.get_unused_localhost_port()
            ],
    ):
        with mock.patch("socket.socket", return_value=mock_socket):
            skip_reason = markers.skip_if_no_local_network()
            assert skip_reason is not None
            assert skip_reason == "No local network was detected"
Beispiel #8
0
    def default_config(
        cls, root_dir, proxy_minion_id, config_defaults=None, config_overrides=None, master=None
    ):
        if config_defaults is None:
            config_defaults = {}

        master_id = master_port = None
        if master is not None:
            master_id = master.id
            master_port = master.config["ret_port"]
            # Match transport if not set
            config_defaults.setdefault("transport", master.config["transport"])

        conf_dir = root_dir / "conf"
        conf_dir.mkdir(parents=True, exist_ok=True)
        conf_file = str(conf_dir / "proxy")

        _config_defaults = {
            "id": proxy_minion_id,
            "conf_file": conf_file,
            "root_dir": str(root_dir),
            "interface": "127.0.0.1",
            "master": "127.0.0.1",
            "master_port": master_port or ports.get_unused_localhost_port(),
            "tcp_pub_port": ports.get_unused_localhost_port(),
            "tcp_pull_port": ports.get_unused_localhost_port(),
            "pidfile": "run/proxy.pid",
            "pki_dir": "pki",
            "cachedir": "cache",
            "sock_dir": "run/proxy",
            "log_file": "logs/proxy.log",
            "log_level_logfile": "debug",
            "loop_interval": 0.05,
            "log_fmt_console": "%(asctime)s,%(msecs)03.0f [%(name)-17s:%(lineno)-4d][%(levelname)-8s][%(processName)18s(%(process)d)] %(message)s",
            "log_fmt_logfile": "[%(asctime)s,%(msecs)03.0f][%(name)-17s:%(lineno)-4d][%(levelname)-8s][%(processName)18s(%(process)d)] %(message)s",
            "proxy": {"proxytype": "dummy"},
            "pytest-minion": {
                "master-id": master_id,
                "log": {"prefix": "{}(id={!r})".format(cls.__name__, proxy_minion_id)},
            },
        }
        # Merge in the initial default options with the internal _config_defaults
        salt.utils.dictupdate.update(config_defaults, _config_defaults, merge_lists=True)

        if config_overrides:
            # Merge in the default options with the proxy_config_overrides
            salt.utils.dictupdate.update(config_defaults, config_overrides, merge_lists=True)

        return config_defaults
Beispiel #9
0
 def __init__(self, *args, **kwargs):
     config_dir = kwargs.pop("config_dir")
     serve_port = kwargs.pop("serve_port", None)
     super().__init__(*args, **kwargs)
     self.config_dir = config_dir
     self.serve_port = serve_port or ports.get_unused_localhost_port()
     self._write_default_config()
def skip_if_no_local_network():
    """
    Helper function to check for existing local network

    Returns:
        str: The reason for the skip.
        None: Should not be skipped.
    """
    check_port = ports.get_unused_localhost_port()
    has_local_network = False
    try:
        with contextlib.closing(
                socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as pubsock:
            pubsock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
            pubsock.bind(("", check_port))
        has_local_network = True
    except socket.error:
        # I wonder if we just have IPV6 support?
        try:
            with contextlib.closing(
                    socket.socket(socket.AF_INET6,
                                  socket.SOCK_STREAM)) as pubsock:
                pubsock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
                pubsock.bind(("", check_port))
            has_local_network = True
        except socket.error:
            # Let's continue
            pass
    if has_local_network is False:
        return "No local network was detected"
 def __attrs_post_init__(self):
     self.store = deque(maxlen=10000)
     self.address = "tcp://127.0.0.1:{}".format(ports.get_unused_localhost_port())
     self.running_event = threading.Event()
     self.running_thread = threading.Thread(target=self._run)
     self.sentinel = msgpack.dumps(None)
     self.auth_event_handlers = weakref.WeakValueDictionary()
Beispiel #12
0
 def setUp(self):
     self.listen_on = "127.0.0.1"
     self.port = get_unused_localhost_port()
     self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
     self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
     self.sock.bind((self.listen_on, self.port))
     self.sock.listen(1)
 def setUp(self):
     self.test_server = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
     port = get_unused_localhost_port()
     self.test_server.bind(("127.0.0.1", port))
     self.test_server.settimeout(2)
     self.logger = logging.getLogger("test_logstash_logger")
     self.logger.setLevel(logging.DEBUG)
     self.logger.addHandler(DatagramLogstashHandler("127.0.0.1", port))
Beispiel #14
0
def master(salt_factories):
    config_defaults = {
        "rest_tornado": {"port": ports.get_unused_localhost_port(), "disable_ssl": True}
    }
    factory = salt_factories.get_salt_master_daemon(
        random_string("master-"), config_defaults=config_defaults
    )
    with factory.started():
        yield factory
 def __init__(self, timeout=60, auth_events_callback=None):
     self.store = deque(maxlen=10000)
     self.address = "tcp://127.0.0.1:{}".format(
         ports.get_unused_localhost_port())
     self.timeout = timeout
     self.auth_events_callback = auth_events_callback
     self.running_event = threading.Event()
     self.running_thread = threading.Thread(target=self._run)
     self.sentinel = msgpack.dumps(None)
    def setUp(self):
        self.context = zmq.Context()
        port = get_unused_localhost_port()

        self.zmq_server = self.context.socket(zmq.SUB)
        self.zmq_server.setsockopt(zmq.SUBSCRIBE, b"")
        self.zmq_server.bind("tcp://127.0.0.1:{}".format(port))

        self.logger = logging.getLogger("test_logstash_logger")
        self.logger.setLevel(logging.DEBUG)
        self.logger.addHandler(ZMQLogstashHander("tcp://127.0.0.1:{}".format(port)))
Beispiel #17
0
def salt_message_client():
    io_loop_mock = MagicMock(spec=salt.ext.tornado.ioloop.IOLoop)
    io_loop_mock.call_later.side_effect = lambda *args, **kwargs: (args, kwargs)

    client = salt.transport.tcp.MessageClient(
        {}, "127.0.0.1", get_unused_localhost_port(), io_loop=io_loop_mock
    )

    try:
        yield client
    finally:
        client.close()
Beispiel #18
0
 def setUpClass(cls):
     ret_port = get_unused_localhost_port()
     publish_port = get_unused_localhost_port()
     tcp_master_pub_port = get_unused_localhost_port()
     tcp_master_pull_port = get_unused_localhost_port()
     tcp_master_publish_pull = get_unused_localhost_port()
     tcp_master_workers = get_unused_localhost_port()
     cls.master_config = cls.get_temp_config(
         "master", **{
             "transport": "zeromq",
             "auto_accept": True,
             "ret_port": ret_port,
             "publish_port": publish_port,
             "tcp_master_pub_port": tcp_master_pub_port,
             "tcp_master_pull_port": tcp_master_pull_port,
             "tcp_master_publish_pull": tcp_master_publish_pull,
             "tcp_master_workers": tcp_master_workers,
             "sign_pub_messages": False,
         })
     salt.master.SMaster.secrets["aes"] = {
         "secret":
         multiprocessing.Array(
             ctypes.c_char,
             six.b(salt.crypt.Crypticle.generate_key_string()),
         ),
     }
     cls.minion_config = cls.get_temp_config(
         "minion", **{
             "transport": "zeromq",
             "master_ip": "127.0.0.1",
             "master_port": ret_port,
             "auth_timeout": 5,
             "auth_tries": 1,
             "master_uri": "tcp://127.0.0.1:{}".format(ret_port),
         })
def test_no_local_network(testdir):
    testdir.makepyfile("""
        import pytest

        @pytest.mark.requires_network
        def test_one():
            assert True
        """)
    mock_socket = mock.MagicMock()
    mock_socket.bind = mock.MagicMock(side_effect=socket.error)
    with mock.patch(
            "saltfactories.utils.ports.get_unused_localhost_port",
            side_effect=[
                ports.get_unused_localhost_port(),
                ports.get_unused_localhost_port()
            ],
    ):
        with mock.patch("socket.socket", return_value=mock_socket):
            res = testdir.runpytest_inprocess("-p",
                                              "no:salt-factories-log-server")
            res.assert_outcomes(skipped=1)
    res.stdout.no_fnmatch_line("*PytestUnknownMarkWarning*")
Beispiel #20
0
    def setUpClass(cls):
        ret_port = get_unused_localhost_port()
        publish_port = get_unused_localhost_port()
        tcp_master_pub_port = get_unused_localhost_port()
        tcp_master_pull_port = get_unused_localhost_port()
        tcp_master_publish_pull = get_unused_localhost_port()
        tcp_master_workers = get_unused_localhost_port()
        cls.master_config = cls.get_temp_config(
            "master",
            **{
                "transport": "zeromq",
                "auto_accept": True,
                "ret_port": ret_port,
                "publish_port": publish_port,
                "tcp_master_pub_port": tcp_master_pub_port,
                "tcp_master_pull_port": tcp_master_pull_port,
                "tcp_master_publish_pull": tcp_master_publish_pull,
                "tcp_master_workers": tcp_master_workers,
            }
        )

        cls.minion_config = salt.config.minion_config(
            os.path.join(RUNTIME_VARS.TMP_CONF_DIR, "minion")
        )
        cls.minion_config = cls.get_temp_config(
            "minion",
            **{
                "transport": "zeromq",
                "master_ip": "127.0.0.1",
                "master_port": ret_port,
                "master_uri": "tcp://127.0.0.1:{0}".format(ret_port),
            }
        )

        cls.process_manager = salt.utils.process.ProcessManager(
            name="ReqServer_ProcessManager"
        )

        cls.server_channel = salt.transport.server.PubServerChannel.factory(
            cls.master_config
        )
        cls.server_channel.pre_fork(cls.process_manager)

        # we also require req server for auth
        cls.req_server_channel = salt.transport.server.ReqServerChannel.factory(
            cls.master_config
        )
        cls.req_server_channel.pre_fork(cls.process_manager)

        cls._server_io_loop = salt.ext.tornado.ioloop.IOLoop()
        cls.evt = threading.Event()
        cls.req_server_channel.post_fork(
            cls._handle_payload, io_loop=cls._server_io_loop
        )
        cls.server_thread = threading.Thread(
            target=run_loop_in_thread, args=(cls._server_io_loop, cls.evt)
        )
        cls.server_thread.start()
Beispiel #21
0
def datagram_server():
    logger = logging.getLogger("test_logstash_logger")
    server = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    port = get_unused_localhost_port()
    handler = DatagramLogstashHandler("127.0.0.1", port)
    try:
        server.bind(("127.0.0.1", port))
        server.settimeout(2)
        logger.setLevel(logging.DEBUG)
        logger.addHandler(handler)
        yield server
    finally:
        logger.removeHandler(handler)
        server.close()
Beispiel #22
0
 def apply_pre_start_states(self, salt_call_cli, testclass, username):
     if self.listen_port in self.check_ports:
         self.check_ports.remove(self.listen_port)
     if self.listen_port in self.listen_ports:
         self.listen_ports.remove(self.listen_port)
     self.listen_port = get_unused_localhost_port()
     self.check_ports.append(self.listen_port)
     self.listen_ports.append(self.listen_port)
     url = "ssh://{username}@127.0.0.1:{port}/~/repo.git".format(
         username=testclass.username, port=self.listen_port)
     url_extra_repo = "ssh://{username}@127.0.0.1:{port}/~/extra_repo.git".format(
         username=testclass.username, port=self.listen_port)
     home = "/root/.ssh"
     testclass.ext_opts = {
         "url":
         url,
         "url_extra_repo":
         url_extra_repo,
         "privkey_nopass":
         os.path.join(home, testclass.id_rsa_nopass),
         "pubkey_nopass":
         os.path.join(home, testclass.id_rsa_nopass + ".pub"),
         "privkey_withpass":
         os.path.join(home, testclass.id_rsa_withpass),
         "pubkey_withpass":
         os.path.join(home, testclass.id_rsa_withpass + ".pub"),
         "passphrase":
         testclass.passphrase,
     }
     ret = salt_call_cli.run(
         "state.apply",
         mods="git_pillar.ssh",
         pillar={
             "git_pillar": {
                 "git_ssh": testclass.git_ssh,
                 "id_rsa_nopass": testclass.id_rsa_nopass,
                 "id_rsa_withpass": testclass.id_rsa_withpass,
                 "sshd_bin": self.get_script_path(),
                 "sshd_port": self.listen_port,
                 "sshd_config_dir": str(self.config_dir),
                 "master_user": username,
                 "user": testclass.username,
             }
         },
         _timeout=240,
     )
     if ret.exitcode != 0:
         pytest.fail("Failed to apply the 'git_pillar.ssh' state")
     if next(iter(ret.json.values()))["result"] is not True:
         pytest.fail("Failed to apply the 'git_pillar.ssh' state")
Beispiel #23
0
    def apply_pre_start_states(self, salt_call_cli, testclass, root_dir):
        if self.listen_port in self.check_ports:
            self.check_ports.remove(self.listen_port)
        if self.listen_port in self.listen_ports:
            self.listen_ports.remove(self.listen_port)
        self.listen_port = get_unused_localhost_port()
        self.check_ports.append(self.listen_port)
        self.listen_ports.append(self.listen_port)

        config_dir = os.path.join(root_dir, "config")
        git_dir = os.path.join(root_dir, "git")
        testclass.repo_dir = repo_dir = os.path.join(git_dir, "repos")
        venv_dir = os.path.join(root_dir, "venv")
        uwsgi_bin = os.path.join(venv_dir, "bin", "uwsgi")

        pillar = {
            "git_pillar": {
                "config_dir": config_dir,
                "git_dir": git_dir,
                "venv_dir": venv_dir,
                "root_dir": root_dir,
                "uwsgi_port": self.listen_port,
            }
        }

        # Different libexec dir for git backend on Debian and FreeBSD-based systems
        if salt.utils.platform.is_freebsd():
            git_core = "/usr/local/libexec/git-core"
        else:
            git_core = "/usr/libexec/git-core"
        if not os.path.exists(git_core):
            git_core = "/usr/lib/git-core"

        if not os.path.exists(git_core):
            pytest.fail(
                "{} not found. Either git is not installed, or the test "
                "class needs to be updated.".format(git_core)
            )

        pillar["git_pillar"]["git-http-backend"] = os.path.join(
            git_core, "git-http-backend"
        )

        ret = salt_call_cli.run(
            "state.apply", mods="git_pillar.http.uwsgi", pillar=pillar, _timeout=120
        )
        if ret.exitcode != 0:
            pytest.fail("Failed to apply the 'git_pillar.http.uwsgi' state")
        if next(iter(ret.json.values()))["result"] is not True:
            pytest.fail("Failed to apply the 'git_pillar.http.uwsgi' state")
Beispiel #24
0
    def __attrs_post_init__(self):
        if self.authorized_keys is None:
            self.authorized_keys = []
        if self.sshd_config_dict is None:
            self.sshd_config_dict = {}
        if self.listen_address is None:
            self.listen_address = "127.0.0.1"
        if self.listen_port is None:
            self.listen_port = ports.get_unused_localhost_port()
        self.check_ports = [self.listen_port]
        if isinstance(self.config_dir, str):
            self.config_dir = pathlib.Path(self.config_dir)
        elif not isinstance(self.config_dir, pathlib.Path):
            # A py local path?
            self.config_dir = pathlib.Path(self.config_dir.strpath)
        self.config_dir.chmod(0o0700)
        authorized_keys_file = self.config_dir / "authorized_keys"

        # Let's generate the client key
        self.client_key = self._generate_client_ecdsa_key()
        with open("{}.pub".format(self.client_key)) as rfh:
            pubkey = rfh.read().strip()
            log.debug("SSH client pub key: %r", pubkey)
            self.authorized_keys.append(pubkey)

        # Write the authorized pub keys to file
        with open(str(authorized_keys_file), "w") as wfh:
            wfh.write("\n".join(self.authorized_keys))

        authorized_keys_file.chmod(0o0600)

        with open(str(authorized_keys_file)) as rfh:
            log.debug("AuthorizedKeysFile contents:\n%s", rfh.read())

        _default_config = {
            "ListenAddress": self.listen_address,
            "PermitRootLogin": "******" if running_username() == "root" else "no",
            "ChallengeResponseAuthentication": "no",
            "PasswordAuthentication": "no",
            "PubkeyAuthentication": "yes",
            "PrintMotd": "no",
            "PidFile": self.config_dir / "sshd.pid",
            "AuthorizedKeysFile": authorized_keys_file,
        }
        if self.sshd_config_dict:
            _default_config.update(self.sshd_config_dict)
        self.sshd_config = _default_config
        self._write_config()
        super().__attrs_post_init__()
Beispiel #25
0
 def setUpClass(cls):
     overrides = {
         "publish_port": get_unused_localhost_port(),
         "ret_port": get_unused_localhost_port(),
         "tcp_master_pub_port": get_unused_localhost_port(),
         "tcp_master_pull_port": get_unused_localhost_port(),
         "tcp_master_publish_pull": get_unused_localhost_port(),
         "tcp_master_workers": get_unused_localhost_port(),
         "runtests_conn_check_port": get_unused_localhost_port(),
         "runtests_log_port": get_unused_localhost_port(),
     }
     overrides["pytest_engine_port"] = overrides["runtests_conn_check_port"]
     temp_config = AdaptedConfigurationTestCaseMixin.get_temp_config(
         "master", **overrides)
     cls.root_dir = temp_config["root_dir"]
     cls.config_dir = os.path.dirname(temp_config["conf_file"])
     if temp_config["transport"] == "tcp":
         raise SkipTest("Test only applicable to the ZMQ transport")
Beispiel #26
0
def zmq_server():
    logger = logging.getLogger("test_logstash_logger")
    context = zmq.Context()
    server = context.socket(zmq.SUB)
    port = get_unused_localhost_port()
    handler = ZMQLogstashHander("tcp://127.0.0.1:{}".format(port))
    try:
        server.setsockopt(zmq.SUBSCRIBE, b"")
        server.bind("tcp://127.0.0.1:{}".format(port))

        logger.setLevel(logging.DEBUG)
        logger.addHandler(handler)
        yield server
    finally:
        logger.removeHandler(handler)
        server.close()
        context.term()
Beispiel #27
0
def test_multiple_start_stops(salt_factories):
    config_defaults = {
        "rest_tornado": {"port": ports.get_unused_localhost_port(), "disable_ssl": True}
    }
    master = salt_factories.get_salt_master_daemon(
        random_string("master-"), config_defaults=config_defaults
    )
    with master.started():
        factory = master.get_salt_api_daemon()
        assert factory.is_running() is False
        pid = None
        with factory.started():
            assert factory.is_running() is True
            pid = factory.pid
        assert factory.is_running() is False
        with factory.started():
            assert factory.is_running() is True
            assert factory.pid != pid
Beispiel #28
0
async def test_ipc_connect_sync_wrapped(io_loop, tmp_path):
    """
    Ensure IPCMessageSubscriber.connect gets wrapped by
    salt.utils.asynchronous.SyncWrapper.
    """
    if salt.utils.platform.is_windows():
        socket_path = get_unused_localhost_port()
    else:
        socket_path = str(tmp_path / "noexist.ipc")
    subscriber = salt.utils.asynchronous.SyncWrapper(
        salt.transport.ipc.IPCMessageSubscriber,
        args=(socket_path, ),
        kwargs={"io_loop": io_loop},
        loop_kwarg="io_loop",
    )
    with pytest.raises(salt.ext.tornado.iostream.StreamClosedError):
        # Don't `await subscriber.connect()`, that's the purpose of the SyncWrapper
        subscriber.connect()
Beispiel #29
0
    def setUpClass(cls):
        if not hasattr(cls, "_handle_payload"):
            return
        ret_port = get_unused_localhost_port()
        publish_port = get_unused_localhost_port()
        tcp_master_pub_port = get_unused_localhost_port()
        tcp_master_pull_port = get_unused_localhost_port()
        tcp_master_publish_pull = get_unused_localhost_port()
        tcp_master_workers = get_unused_localhost_port()
        cls.master_config = cls.get_temp_config(
            "master",
            **{
                "transport": "zeromq",
                "auto_accept": True,
                "ret_port": ret_port,
                "publish_port": publish_port,
                "tcp_master_pub_port": tcp_master_pub_port,
                "tcp_master_pull_port": tcp_master_pull_port,
                "tcp_master_publish_pull": tcp_master_publish_pull,
                "tcp_master_workers": tcp_master_workers,
            }
        )

        cls.minion_config = cls.get_temp_config(
            "minion",
            **{
                "transport": "zeromq",
                "master_ip": "127.0.0.1",
                "master_port": ret_port,
                "auth_timeout": 5,
                "auth_tries": 1,
                "master_uri": "tcp://127.0.0.1:{0}".format(ret_port),
            }
        )

        cls.process_manager = salt.utils.process.ProcessManager(
            name="ReqServer_ProcessManager"
        )

        cls.server_channel = salt.transport.server.ReqServerChannel.factory(
            cls.master_config
        )
        cls.server_channel.pre_fork(cls.process_manager)

        cls.io_loop = salt.ext.tornado.ioloop.IOLoop()
        cls.evt = threading.Event()
        cls.server_channel.post_fork(cls._handle_payload, io_loop=cls.io_loop)
        cls.server_thread = threading.Thread(
            target=run_loop_in_thread, args=(cls.io_loop, cls.evt)
        )
        cls.server_thread.start()
Beispiel #30
0
    def apply_pre_start_states(self, salt_call_cli, testclass, root_dir):
        if self.listen_port in self.check_ports:
            self.check_ports.remove(self.listen_port)
        if self.listen_port in self.listen_ports:
            self.listen_ports.remove(self.listen_port)
        self.listen_port = get_unused_localhost_port()
        self.check_ports.append(self.listen_port)
        self.listen_ports.append(self.listen_port)

        config_dir = os.path.join(root_dir, "config")
        git_dir = os.path.join(root_dir, "git")
        url = "http://127.0.0.1:{port}/repo.git".format(port=self.listen_port)
        url_extra_repo = "http://127.0.0.1:{port}/extra_repo.git".format(
            port=self.listen_port)
        ext_opts = {"url": url, "url_extra_repo": url_extra_repo}
        # Add auth params if present (if so this will trigger the spawned
        # server to turn on HTTP basic auth).
        for credential_param in ("user", "password"):
            if hasattr(testclass, credential_param):
                ext_opts[credential_param] = getattr(testclass,
                                                     credential_param)
        testclass.ext_opts = ext_opts
        testclass.nginx_port = self.listen_port

        auth_enabled = hasattr(testclass, "username") and hasattr(
            testclass, "password")
        pillar = {
            "git_pillar": {
                "config_dir": config_dir,
                "git_dir": git_dir,
                "uwsgi_port": self.uwsgi_port,
                "nginx_port": self.listen_port,
                "auth_enabled": auth_enabled,
            }
        }

        ret = salt_call_cli.run("state.apply",
                                mods="git_pillar.http.nginx",
                                pillar=pillar)
        if ret.exitcode != 0:
            pytest.fail("Failed to apply the 'git_pillar.http.nginx' state")
        if next(iter(ret.json.values()))["result"] is not True:
            pytest.fail("Failed to apply the 'git_pillar.http.nginx' state")