Esempio n. 1
0
    def test_unique_dont_allow_dict_or_set(self):
        """
        Test unique with bad arguments.
         - Instantiating with a set leads to exception
         - Instantiating with a dictionary leads to exception
        """
        with pytest.raises(
                TypeError,
                match=r"<(class|type) 'set'> types are always unique"):
            unique({1, 2, 3}, preserve_order=False)

        with pytest.raises(
                TypeError,
                match=r"<(class|type) 'dict'> types are always unique"):
            unique(dict.fromkeys((1, 2, 3)), preserve_order=False)
Esempio n. 2
0
    def _parse_haproxy_sockets(socket_lines):
        """
        Find the sockets in the HAProxy configuration file.

        We assume all sockets should be informed of new staples for any of the
        cert paths we find. If paths are not absolute we assume they are
        relative to the config's directory.
        :param list socket_lines: Lines that concern sockets.
        :returns list: Socket paths.
        """
        # The list returned below may be empty (``[]``).
        # de-dupe and return the sockets
        return unique(socket_lines)
Esempio n. 3
0
def __get_haproxy_socket_mapping(args):
    """
    Get a mapping of configured sockets and certificate directories.

    From: haproxy config, stapled config and command line arguments.

    :param Namespace args: Argparser argument list.
    :return dict Of cert-paths and sockets for inform of changes.
    """
    # Parse the cert_paths argument
    arg_cert_paths = __get_arg_cert_paths(args)
    # Parse haproxy_sockets argument.
    arg_haproxy_sockets = __get_arg_haproxy_sockets(args)
    # Make a mapping from certificate paths to sockets in a dict.
    mapping = dict(zip(arg_cert_paths, arg_haproxy_sockets))

    # Parse HAProxy config files.
    try:
        conf_cert_paths, conf_haproxy_sockets = parse_haproxy_config(
            args.haproxy_config
        )
    except (OSError) as exc:
        logger.critical(exc)
        exit(1)

    # Combine the socket and certificate paths of the arguments and config
    # files in the sockets dictionary.
    for i, paths in enumerate(conf_cert_paths):
        for path in paths:
            # When certificate path is already in the mapping add the socket
            # file to the mapping if haproxy_sockets is not disabled.
            if args.no_haproxy_sockets:
                # haproxy_sockets are disabled, just ensure the path is in the
                # mapping without sockets.
                mapping[path] = []
            else:
                # Make sure paths are mapped to all unique sockets.
                mapping[path] = unique(
                    mapping.get(path, []) + conf_haproxy_sockets[i],
                    preserve_order=False
                )
    logger.debug("Paths to socket mappings: %s", str(mapping))
    return mapping
Esempio n. 4
0
    def _parse_haproxy_cert_paths(cls, cert_paths_lines, cert_base):
        """
        Find certificate paths in the relevant lines.

        We take all paths from all bind, server and default-server directives.

        :param list cert_paths_lines: Lines that concern cert paths.
        :param str cert_base: The directory that relative paths relate to.
        :returns list: Cert paths.
        """
        abs_cert_paths = []
        for path in cert_paths_lines:
            if path.startswith("'"):
                # Strong quoted, only remove quotes.
                path = path.strip("'")
            else:
                # Weak, or not quoted, remove quotes and unescape spaces.
                path = cls.PAT_UNESCAPE.sub("\\1", path.strip('"'))
            if not os.path.isabs(path):
                path = os.path.join(cert_base, path)
            abs_cert_paths.append(path)
            # de-dupe the cert paths
            abs_cert_paths = unique(abs_cert_paths)
        return abs_cert_paths
Esempio n. 5
0
 def test_unique_not_preserving_order(self, data, expected):
     """
     Test unique function with order preserving disabled.
      - Returned values are as expected (unique).
     """
     assert set(unique(data, False)) == set(expected)
Esempio n. 6
0
 def test_unique_preserving_order(self, data, expected):
     """
     Test unique function with order preserving enabled.
      - Returned values are as expected (unique).
     """
     assert unique(data, True) == expected