1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452
|
"""APIClient for connecting to Podman service."""
import json
import warnings
import urllib.parse
from typing import (
Any,
ClassVar,
IO,
Optional,
Union,
)
from collections.abc import Iterable, Mapping
import requests
from requests.adapters import HTTPAdapter
from podman.api.api_versions import VERSION, COMPATIBLE_VERSION
from podman.api.ssh import SSHAdapter
from podman.api.uds import UDSAdapter
from podman.errors import APIError, NotFound
from podman.tlsconfig import TLSConfig
from podman.version import __version__
_Data = Union[
None,
str,
bytes,
Mapping[str, Any],
Iterable[tuple[str, Optional[str]]],
IO,
]
"""Type alias for request data parameter."""
_Timeout = Union[None, float, tuple[float, float], tuple[float, None]]
"""Type alias for request timeout parameter."""
class ParameterDeprecationWarning(DeprecationWarning):
"""
Custom DeprecationWarning for deprecated parameters.
"""
# Make the ParameterDeprecationWarning visible for user.
warnings.simplefilter('always', ParameterDeprecationWarning)
class APIResponse:
"""APIResponse proxy requests.Response objects.
Override raise_for_status() to implement Podman API binding errors.
All other methods and attributes forwarded to original Response.
"""
def __init__(self, response: requests.Response):
"""Initialize APIResponse.
Args:
response: the requests.Response to provide implementation
"""
self._response = response
def __getattr__(self, item: str):
"""Forward any query for an attribute not defined in this proxy class to wrapped class."""
return getattr(self._response, item)
def raise_for_status(self, not_found: type[APIError] = NotFound) -> None:
"""Raises exception when Podman service reports one."""
if self.status_code < 400:
return
try:
body = self.json()
cause = body["cause"]
message = body["message"]
except (json.decoder.JSONDecodeError, KeyError):
cause = message = self.text
if self.status_code == requests.codes.not_found:
raise not_found(cause, response=self._response, explanation=message)
raise APIError(cause, response=self._response, explanation=message)
class APIClient(requests.Session):
"""Client for Podman service API."""
# Abstract methods (delete,get,head,post) are specialized and pylint cannot walk hierarchy.
# pylint: disable=too-many-instance-attributes,arguments-differ,arguments-renamed
supported_schemes: ClassVar[list[str]] = (
"unix",
"http+unix",
"ssh",
"http+ssh",
"tcp",
"http",
)
def __init__(
self,
base_url: str = None,
version: Optional[str] = None,
timeout: Optional[float] = None,
tls: Union[TLSConfig, bool] = False,
user_agent: Optional[str] = None,
num_pools: Optional[int] = None,
credstore_env: Optional[Mapping[str, str]] = None,
use_ssh_client=True,
max_pool_size=None,
**kwargs,
): # pylint: disable=unused-argument,too-many-positional-arguments
"""Instantiate APIClient object.
Args:
base_url: Address to use for connecting to Podman service.
version: Override version prefix for Podman resource URLs.
timeout: Time in seconds to allow for Podman service operation.
tls: Configuration for TLS connections.
user_agent: Override User-Agent HTTP header.
num_pools: The number of connection pools to cache.
credstore_env: Environment for storing credentials.
use_ssh_client: Use system ssh agent rather than ssh module. Always, True.
max_pool_size: Override number of connections pools to maintain.
Default: requests.adapters.DEFAULT_POOLSIZE
Keyword Args:
compatible_version (str): Override version prefix for compatible resource URLs.
identity (str): Provide SSH key to authenticate SSH connection.
Raises:
ValueError: when a parameter is incorrect
"""
super().__init__()
self.base_url = self._normalize_url(base_url)
adapter_kwargs = kwargs.copy()
# The HTTPAdapter doesn't handle the "**kwargs", so it needs special structure
# where the parameters are set specifically.
http_adapter_kwargs = {}
if num_pools is not None:
adapter_kwargs["pool_connections"] = num_pools
http_adapter_kwargs["pool_connections"] = num_pools
if max_pool_size is not None:
adapter_kwargs["pool_maxsize"] = max_pool_size
http_adapter_kwargs["pool_maxsize"] = max_pool_size
if timeout is not None:
adapter_kwargs["timeout"] = timeout
if self.base_url.scheme == "http+unix":
self.mount("http://", UDSAdapter(self.base_url.geturl(), **adapter_kwargs))
self.mount("https://", UDSAdapter(self.base_url.geturl(), **adapter_kwargs))
# ignore proxies from the env vars
self.trust_env = False
elif self.base_url.scheme == "http+ssh":
self.mount("http://", SSHAdapter(self.base_url.geturl(), **adapter_kwargs))
self.mount("https://", SSHAdapter(self.base_url.geturl(), **adapter_kwargs))
elif self.base_url.scheme == "http":
self.mount("http://", HTTPAdapter(**http_adapter_kwargs))
self.mount("https://", HTTPAdapter(**http_adapter_kwargs))
else:
raise PodmanError("APIClient.supported_schemes changed without adding a branch here.")
self.version = version or VERSION
self.path_prefix = f"/v{self.version}/libpod/"
self.compatible_version = kwargs.get("compatible_version", COMPATIBLE_VERSION)
self.compatible_prefix = f"/v{self.compatible_version}/"
self.timeout = timeout
self.pool_maxsize = num_pools or requests.adapters.DEFAULT_POOLSIZE
self.credstore_env = credstore_env or {}
self.user_agent = user_agent or (
f"PodmanPy/{__version__} (API v{self.version}; Compatible v{self.compatible_version})"
)
self.headers.update({"User-Agent": self.user_agent})
@staticmethod
def _normalize_url(base_url: str) -> urllib.parse.ParseResult:
uri = urllib.parse.urlparse(base_url)
if uri.scheme not in APIClient.supported_schemes:
raise ValueError(
f"The scheme '{uri.scheme}' must be one of {APIClient.supported_schemes}"
)
# Normalize URL scheme, needs to match up with adapter mounts
if uri.scheme == "unix":
uri = uri._replace(scheme="http+unix")
elif uri.scheme == "ssh":
uri = uri._replace(scheme="http+ssh")
elif uri.scheme == "tcp":
uri = uri._replace(scheme="http")
# Normalize URL netloc, needs to match up with transport adapters expectations
if uri.netloc == "":
uri = uri._replace(netloc=uri.path)._replace(path="")
if "/" in uri.netloc:
uri = uri._replace(netloc=urllib.parse.quote_plus(uri.netloc))
return uri
def delete(
self,
path: Union[str, bytes],
*,
params: Union[None, bytes, Mapping[str, str]] = None,
headers: Optional[Mapping[str, str]] = None,
timeout: _Timeout = None,
stream: Optional[bool] = False,
**kwargs,
) -> APIResponse:
"""HTTP DELETE operation against configured Podman service.
Args:
path: Relative path to RESTful resource.
params: Optional parameters to include with URL.
headers: Optional headers to include in request.
timeout: Number of seconds to wait on request, or (connect timeout, read timeout) tuple
stream: Return iterator for content vs reading all content into memory
Keyword Args:
compatible: Will override the default path prefix with compatible prefix
Raises:
APIError: when service returns an error
"""
return self._request(
"DELETE",
path=path,
params=params,
headers=headers,
timeout=timeout,
stream=stream,
**kwargs,
)
def get(
self,
path: Union[str, bytes],
*,
params: Union[None, bytes, Mapping[str, list[str]]] = None,
headers: Optional[Mapping[str, str]] = None,
timeout: _Timeout = None,
stream: Optional[bool] = False,
**kwargs,
) -> APIResponse:
"""HTTP GET operation against configured Podman service.
Args:
path: Relative path to RESTful resource.
params: Optional parameters to include with URL.
headers: Optional headers to include in request.
timeout: Number of seconds to wait on request, or (connect timeout, read timeout) tuple
stream: Return iterator for content vs reading all content into memory
Keyword Args:
compatible: Will override the default path prefix with compatible prefix
Raises:
APIError: when service returns an error
"""
return self._request(
"GET",
path=path,
params=params,
headers=headers,
timeout=timeout,
stream=stream,
**kwargs,
)
def head(
self,
path: Union[str, bytes],
*,
params: Union[None, bytes, Mapping[str, str]] = None,
headers: Optional[Mapping[str, str]] = None,
timeout: _Timeout = None,
stream: Optional[bool] = False,
**kwargs,
) -> APIResponse:
"""HTTP HEAD operation against configured Podman service.
Args:
path: Relative path to RESTful resource.
params: Optional parameters to include with URL.
headers: Optional headers to include in request.
timeout: Number of seconds to wait on request, or (connect timeout, read timeout) tuple
stream: Return iterator for content vs reading all content into memory
Keyword Args:
compatible: Will override the default path prefix with compatible prefix
Raises:
APIError: when service returns an error
"""
return self._request(
"HEAD",
path=path,
params=params,
headers=headers,
timeout=timeout,
stream=stream,
**kwargs,
)
def post(
self,
path: Union[str, bytes],
*,
params: Union[None, bytes, Mapping[str, str]] = None,
data: _Data = None,
headers: Optional[Mapping[str, str]] = None,
timeout: _Timeout = None,
stream: Optional[bool] = False,
**kwargs,
) -> APIResponse:
"""HTTP POST operation against configured Podman service.
Args:
path: Relative path to RESTful resource.
data: HTTP body for operation
params: Optional parameters to include with URL.
headers: Optional headers to include in request.
timeout: Number of seconds to wait on request, or (connect timeout, read timeout) tuple
stream: Return iterator for content vs reading all content into memory
Keyword Args:
compatible: Will override the default path prefix with compatible prefix
verify: Whether to verify TLS certificates.
Raises:
APIError: when service returns an error
"""
return self._request(
"POST",
path=path,
params=params,
data=data,
headers=headers,
timeout=timeout,
stream=stream,
**kwargs,
)
def put(
self,
path: Union[str, bytes],
*,
params: Union[None, bytes, Mapping[str, str]] = None,
data: _Data = None,
headers: Optional[Mapping[str, str]] = None,
timeout: _Timeout = None,
stream: Optional[bool] = False,
**kwargs,
) -> APIResponse:
"""HTTP PUT operation against configured Podman service.
Args:
path: Relative path to RESTful resource.
data: HTTP body for operation
params: Optional parameters to include with URL.
headers: Optional headers to include in request.
timeout: Number of seconds to wait on request, or (connect timeout, read timeout) tuple
stream: Return iterator for content vs reading all content into memory
Keyword Args:
compatible: Will override the default path prefix with compatible prefix
Raises:
APIError: when service returns an error
"""
return self._request(
"PUT",
path=path,
params=params,
data=data,
headers=headers,
timeout=timeout,
stream=stream,
**kwargs,
)
def _request(
self,
method: str,
path: Union[str, bytes],
*,
data: _Data = None,
params: Union[None, bytes, Mapping[str, str]] = None,
headers: Optional[Mapping[str, str]] = None,
timeout: _Timeout = None,
stream: Optional[bool] = None,
**kwargs,
) -> APIResponse:
"""HTTP operation against configured Podman service.
Args:
method: HTTP method to use for request
path: Relative path to RESTful resource.
params: Optional parameters to include with URL.
headers: Optional headers to include in request.
timeout: Number of seconds to wait on request, or (connect timeout, read timeout) tuple
Keyword Args:
compatible: Will override the default path prefix with compatible prefix
verify: Whether to verify TLS certificates.
Raises:
APIError: when service returns an error
"""
# Only set timeout if one is given, lower level APIs will not override None
timeout_kw = {}
timeout = timeout or self.timeout
if timeout_kw is not None:
timeout_kw["timeout"] = timeout
compatible = kwargs.get("compatible", False)
path_prefix = self.compatible_prefix if compatible else self.path_prefix
path = path.lstrip("/") # leading / makes urljoin crazy...
scheme = "https" if kwargs.get("verify", None) else "http"
# Build URL for operation from base_url
uri = urllib.parse.ParseResult(
scheme,
self.base_url.netloc,
urllib.parse.urljoin(path_prefix, path),
self.base_url.params,
self.base_url.query,
self.base_url.fragment,
)
try:
return APIResponse(
self.request(
method.upper(),
uri.geturl(),
params=params,
data=data,
headers=(headers or {}),
stream=stream,
verify=kwargs.get("verify", None),
**timeout_kw,
)
)
except OSError as e:
raise APIError(uri.geturl(), explanation=f"{method.upper()} operation failed") from e
|