예제 #1
0
def create_xml_config_content(
        entries,
        config_d_dir="/etc/clickhouse-server/config.d",
        config_file="ldap_external_user_directories.xml"):
    """Create XML configuration file from a dictionary.
    """
    uid = getuid()
    path = os.path.join(config_d_dir, config_file)
    name = config_file
    root = xmltree.Element("yandex")
    root.append(xmltree.Comment(text=f"config uid: {uid}"))

    def create_xml_tree(entries, root):
        for k, v in entries.items():
            if type(v) is dict:
                xml_element = xmltree.Element(k)
                create_xml_tree(v, xml_element)
                root.append(xml_element)
            elif type(v) in (list, tuple):
                xml_element = xmltree.Element(k)
                for e in v:
                    create_xml_tree(e, xml_element)
                root.append(xml_element)
            else:
                xml_append(root, k, v)

    create_xml_tree(entries, root)
    xml_indent(root)
    content = xml_with_utf8 + str(
        xmltree.tostring(root, short_empty_elements=False, encoding="utf-8"),
        "utf-8")

    return Config(content, path, name, uid, "config.xml")
예제 #2
0
def create_entries_ldap_external_user_directory_config_content(
    entries,
    config_d_dir="/etc/clickhouse-server/config.d",
    config_file="ldap_external_user_directories.xml",
):
    """Create configurattion file content that contains
    one or more entries for the LDAP external user directory.

    For example,

    ```xml
        <user_directories>
            <ldap>
                <server>my_ldap_server</server>
                <roles>
                    <my_local_role1 />
                    <my_local_role2 />
                </roles>
            </ldap>
        </user_directories>
    ```
    """
    uid = getuid()
    path = os.path.join(config_d_dir, config_file)
    name = config_file

    root = xmltree.fromstring(
        "<clickhouse><user_directories></user_directories></clickhouse>"
    )
    xml_user_directories = root.find("user_directories")
    xml_user_directories.append(
        xmltree.Comment(text=f"LDAP external user directories {uid}")
    )

    for entry in entries:
        servers, roles_entries = entry
        xml_directory = xmltree.Element("ldap")
        for server in servers:
            if server is not None:
                xml_append(xml_directory, "server", server)
        if roles_entries:
            for roles_entry in roles_entries:
                xml_roles = xmltree.Element("roles")
                if roles_entry:
                    for role in roles_entry:
                        if role is not None:
                            xml_append(xml_roles, role, "")
                xml_directory.append(xml_roles)
        xml_user_directories.append(xml_directory)

    xml_indent(root)
    content = xml_with_utf8 + str(
        xmltree.tostring(root, short_empty_elements=False, encoding="utf-8"), "utf-8"
    )

    return Config(content, path, name, uid, "config.xml")