podman-py: Unix-Sockets connection not working

Using podman-py we cannot connect to a local unix-socket. Irrespective of how we communicate the protocol, it will always end up as http.

The following does not work, it will end up making an http-request to http://%2frun%2fpodman.sock/v4.0.0/libpod/version instead of the unix-socket.

def podman_py():
    from podman import PodmanClient

    uri = "unix:///run/podman.sock"

    with PodmanClient(base_url=uri) as client:
        version = client.version()  # fails
        print("Release: ", version["Version"])
        print("Compatible API: ", version["ApiVersion"])
        print("Podman API: ", version["Components"][0]["Details"]["APIVersion"], "\n")

For comparison, the following, manual connection to the socket works:

def podman_requests_unixsocket():
    import requests_unixsocket

    url = "http+unix://%2frun%2fpodman.sock/v4.0.0/libpod/version"

    with requests_unixsocket.Session() as session:
        response = session.get(url)
        response.raise_for_status()
        version = json.loads(response.text)
        print("Release: ", version["Version"])
        print("Compatible API: ", version["ApiVersion"])
        print("Podman API: ", version["Components"][0]["Details"]["APIVersion"], "\n")

My suspicion is that we are doing something wrong, or that something is lost in translation when hard-coding the protocol to http and then mapping to UDSAdapter down the line.


We made some manual changes to podman-py that seem to do the trick (although they look wrong to me):

podman/api/uds.py#108:

    # Map supported schemes to Pool Classes
    _pool_classes_by_scheme = {
        "http": UDSConnectionPool,
        "http+unix": UDSConnectionPool,
        "http+ssh": UDSConnectionPool,
    }

    # Map supported schemes to Pool Key index generator
    _key_fn_by_scheme = {
        "http": functools.partial(_key_normalizer, _PoolKey),
        "http+unix": functools.partial(_key_normalizer, _PoolKey),
        "http+ssh": functools.partial(_key_normalizer, _PoolKey),
    }

podman/api/client.py#126

        if self.base_url.scheme == "http+unix":
            self.mount("http+unix://", UDSAdapter(self.base_url.geturl(), **adapter_kwargs))

podman/api/client.py#396

        uri = urllib.parse.ParseResult(
            "http+unix",
            self.base_url.netloc,
            urllib.parse.urljoin(path_prefix, path),
            self.base_url.params,
            self.base_url.query,
            self.base_url.fragment,
        )

We are using version 4.0.0

About this issue

  • Original URL
  • State: open
  • Created 2 years ago
  • Reactions: 1
  • Comments: 16 (6 by maintainers)

Most upvoted comments