Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 43 additions & 5 deletions dataconnect/transport/arrow_flight/transport.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@

from __future__ import annotations

import base64
import json
import platform
import subprocess

from pyarrow import flight
import pyarrow.flight as flight

from dataconnect.transport.base import Transport
from dataconnect.transport.errors import (
Expand Down Expand Up @@ -56,17 +59,52 @@ def __init__(
self._call_headers: list[tuple[bytes, bytes]] = []

scheme = "grpc+tls" if use_tls else "grpc"
location = f"{scheme}://{host}:{port}"
uri = f"{scheme}://{host}:{port}"

try:
tls_root_certs = None # pending
self._client = flight.FlightClient(location, tls_root_certs=tls_root_certs)
self._client = self._get_client(uri, use_tls)
except Exception as exc:
raise TransportConnectionError(f"Failed to connect to {location}: {exc}") from exc
raise TransportConnectionError(f"Failed to create FlightClient: {exc}") from exc

if token:
self._call_headers.append((b"authorization", f"Bearer {token}".encode()))

def _get_client(self, uri: str, use_tls: bool) -> flight.FlightClient:
is_windows = platform.system() == "Windows"

if use_tls and is_windows:
result = subprocess.run(
[
Comment thread
slingampalli-mdsol marked this conversation as resolved.
"powershell.exe",
"-Command",
(
"Get-ChildItem -Path Cert:\\LocalMachine\\Root | "
"ForEach-Object { [System.Convert]::ToBase64String($_.RawData) }"
),
],
capture_output=True,
text=True,
check=True,
)
Comment thread
slingampalli-mdsol marked this conversation as resolved.

pem_parts = []
for b64 in result.stdout.splitlines():
b64 = b64.strip()
if not b64:
continue
base64.b64decode(b64, validate=True)
lines = ["-----BEGIN CERTIFICATE-----"]
lines += [b64[i : i + 64] for i in range(0, len(b64), 64)]
lines.append("-----END CERTIFICATE-----")
pem_parts.append("\n".join(lines))

pem_certs = "\n".join(pem_parts).encode("utf-8")
client = flight.FlightClient(uri, tls_root_certs=pem_certs)
else:
client = flight.FlightClient(uri)

return client

def _options(self) -> flight.FlightCallOptions:
return flight.FlightCallOptions(headers=self._call_headers)

Expand Down
Loading