Skip to content

Commit 81283ce

Browse files
committed
bluezdbus: Add BLEAK_DBUS_AUTH_UID override
1 parent 9c26e49 commit 81283ce

File tree

6 files changed

+33
-5
lines changed

6 files changed

+33
-5
lines changed

CHANGELOG.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ and this project adheres to `Semantic Versioning <https://semver.org/spec/v2.0.0
1212

1313
Added
1414
-----
15+
* Added ``BLEAK_DBUS_AUTH_UID`` environment var for hardcoding DBus UID.
1516
* Added return type None to some scanner methods.
1617
* Added optional hack to use Bluetooth address instead of UUID on macOS.
1718
* Added ``BleakScanner.find_device_by_name()`` class method.

bleak/backends/bluezdbus/client.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
from .characteristic import BleakGATTCharacteristicBlueZDBus
3131
from .manager import get_global_bluez_manager
3232
from .scanner import BleakScannerBlueZDBus
33-
from .utils import assert_reply
33+
from .utils import assert_reply, get_dbus_authenticator
3434
from .version import BlueZFeatures
3535

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

136138
def on_connected_changed(connected: bool) -> None:

bleak/backends/bluezdbus/manager.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
from .descriptor import BleakGATTDescriptorBlueZDBus
3737
from .service import BleakGATTServiceBlueZDBus
3838
from .signals import MatchRules, add_match
39-
from .utils import assert_reply
39+
from .utils import assert_reply, get_dbus_authenticator
4040

4141
logger = logging.getLogger(__name__)
4242

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

193193
try:

bleak/backends/bluezdbus/utils.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# -*- coding: utf-8 -*-
2+
import os
23
import re
34

5+
from dbus_fast.auth import AuthExternal
46
from dbus_fast.constants import MessageType
57
from dbus_fast.message import Message
68

@@ -43,3 +45,17 @@ def bdaddr_from_device_path(device_path: str) -> str:
4345
A Bluetooth address as a string.
4446
"""
4547
return ":".join(device_path[-17:].split("_"))
48+
49+
50+
def get_dbus_authenticator():
51+
uid = None
52+
try:
53+
uid = int(os.environ.get("BLEAK_DBUS_AUTH_UID", ""))
54+
except ValueError:
55+
pass
56+
57+
auth = None
58+
if uid is not None:
59+
auth = AuthExternal(uid=uid)
60+
61+
return auth

docs/backends/linux.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,15 @@ individual Bleak objects should not be shared between event loops. Otherwise,
4141
RuntimeErrors similar to ``[...] got Future <Future pending> attached to a
4242
different loop`` will be thrown.
4343

44+
D-Bus Authentication
45+
--------------------
46+
47+
Connecting to the host DBus from within a user namespace will fail. This is
48+
because the remapped UID will not match the UID that the hosts sees. To work
49+
around this, you can hardcode a UID with the `BLEAK_DBUS_AUTH_UID` environment
50+
variable.
51+
52+
4453
API
4554
---
4655

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ pyobjc-core = { version = "^8.5.1", markers = "platform_system=='Darwin'" }
2929
pyobjc-framework-CoreBluetooth = { version = "^8.5.1", markers = "platform_system=='Darwin'" }
3030
pyobjc-framework-libdispatch = { version = "^8.5.1", markers = "platform_system=='Darwin'" }
3131
bleak-winrt = { version = "^1.2.0", markers = "platform_system=='Windows'" }
32-
dbus-fast = { version = "^1.22.0", markers = "platform_system == 'Linux'" }
32+
dbus-fast = { version = "^1.83.0", markers = "platform_system == 'Linux'" }
3333

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

0 commit comments

Comments
 (0)