Skip to content

Commit

Permalink
bluezdbus: Add BLEAK_DBUS_AUTH_UID override
Browse files Browse the repository at this point in the history
  • Loading branch information
kiwiz authored and dlech committed Dec 27, 2022
1 parent 7c63658 commit 4a6d1c4
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 39 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ and this project adheres to `Semantic Versioning <https://semver.org/spec/v2.0.0

Added
-----
* Added ``BLEAK_DBUS_AUTH_UID`` environment var for hardcoding DBus UID.
* Added return type None to some scanner methods.
* Added optional hack to use Bluetooth address instead of UUID on macOS.
* Added ``BleakScanner.find_device_by_name()`` class method.
Expand Down
6 changes: 4 additions & 2 deletions bleak/backends/bluezdbus/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
from .characteristic import BleakGATTCharacteristicBlueZDBus
from .manager import get_global_bluez_manager
from .scanner import BleakScannerBlueZDBus
from .utils import assert_reply
from .utils import assert_reply, get_dbus_authenticator
from .version import BlueZFeatures

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -130,7 +130,9 @@ async def connect(self, dangerous_use_bleak_cache: bool = False, **kwargs) -> bo
# Each BLE connection session needs a new D-Bus connection to avoid a
# BlueZ quirk where notifications are automatically enabled on reconnect.
self._bus = await MessageBus(
bus_type=BusType.SYSTEM, negotiate_unix_fd=True
bus_type=BusType.SYSTEM,
negotiate_unix_fd=True,
auth=get_dbus_authenticator(),
).connect()

def on_connected_changed(connected: bool) -> None:
Expand Down
4 changes: 2 additions & 2 deletions bleak/backends/bluezdbus/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
from .descriptor import BleakGATTDescriptorBlueZDBus
from .service import BleakGATTServiceBlueZDBus
from .signals import MatchRules, add_match
from .utils import assert_reply
from .utils import assert_reply, get_dbus_authenticator

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -187,7 +187,7 @@ async def async_init(self):
# We need to create a new MessageBus each time as
# dbus-next will destory the underlying file descriptors
# when the previous one is closed in its finalizer.
bus = MessageBus(bus_type=BusType.SYSTEM)
bus = MessageBus(bus_type=BusType.SYSTEM, auth=get_dbus_authenticator())
await bus.connect()

try:
Expand Down
16 changes: 16 additions & 0 deletions bleak/backends/bluezdbus/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# -*- coding: utf-8 -*-
import os
import re

from dbus_fast.auth import AuthExternal
from dbus_fast.constants import MessageType
from dbus_fast.message import Message

Expand Down Expand Up @@ -43,3 +45,17 @@ def bdaddr_from_device_path(device_path: str) -> str:
A Bluetooth address as a string.
"""
return ":".join(device_path[-17:].split("_"))


def get_dbus_authenticator():
uid = None
try:
uid = int(os.environ.get("BLEAK_DBUS_AUTH_UID", ""))
except ValueError:
pass

auth = None
if uid is not None:
auth = AuthExternal(uid=uid)

return auth
9 changes: 9 additions & 0 deletions docs/backends/linux.rst
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,15 @@ individual Bleak objects should not be shared between event loops. Otherwise,
RuntimeErrors similar to ``[...] got Future <Future pending> attached to a
different loop`` will be thrown.

D-Bus Authentication
--------------------

Connecting to the host DBus from within a user namespace will fail. This is
because the remapped UID will not match the UID that the hosts sees. To work
around this, you can hardcode a UID with the `BLEAK_DBUS_AUTH_UID` environment
variable.


API
---

Expand Down
65 changes: 31 additions & 34 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pyobjc-core = { version = "^8.5.1", markers = "platform_system=='Darwin'" }
pyobjc-framework-CoreBluetooth = { version = "^8.5.1", markers = "platform_system=='Darwin'" }
pyobjc-framework-libdispatch = { version = "^8.5.1", markers = "platform_system=='Darwin'" }
bleak-winrt = { version = "^1.2.0", markers = "platform_system=='Windows'" }
dbus-fast = { version = "^1.22.0", markers = "platform_system == 'Linux'" }
dbus-fast = { version = "^1.83.0", markers = "platform_system == 'Linux'" }

[tool.poetry.group.docs.dependencies]
Sphinx = { version = "^5.1.1", python = ">=3.8" }
Expand Down

0 comments on commit 4a6d1c4

Please sign in to comment.