Skip to content

Commit cfa38ed

Browse files
committed
bluezdbus: Add BLEAK_DBUS_UID override
1 parent 9c26e49 commit cfa38ed

File tree

5 files changed

+23
-4
lines changed

5 files changed

+23
-4
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 DBUS UID override for BlueZ
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: 2 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,7 @@ 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, negotiate_unix_fd=True, auth=get_dbus_authenticator()
134134
).connect()
135135

136136
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: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# -*- coding: utf-8 -*-
22
import re
33

4+
from dbus_fast.auth import AuthExternal
45
from dbus_fast.constants import MessageType
56
from dbus_fast.message import Message
67

@@ -43,3 +44,14 @@ def bdaddr_from_device_path(device_path: str) -> str:
4344
A Bluetooth address as a string.
4445
"""
4546
return ":".join(device_path[-17:].split("_"))
47+
48+
49+
def get_dbus_authenticator():
50+
uid = 0
51+
try:
52+
uid = int(os.environ("BLEAK_DBUS_UID"))
53+
except ValueError as e:
54+
pass
55+
auth = None
56+
if uid:
57+
auth = AuthExternal(uid=uid)

docs/backends/linux.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@ 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+
User Namespaces
45+
---------------
46+
47+
DBus authentication will fail if the client is invoked from within a user namespace.
48+
To work around this, use the `BLEAK_DBUS_UID` environment variable to hardcode a UID.
49+
4450
API
4551
---
4652

0 commit comments

Comments
 (0)