Nitrokey / pynitrokey

Python client for Nitrokey devices
Apache License 2.0
94 stars 28 forks source link

dfu_cc_pb2.py incompatible with recent protobuf version #548

Open an0nfunc opened 6 days ago

an0nfunc commented 6 days ago

Hi :wave:

since a recent version of protobuf, protobuf and the generated dfu_cc_pb2.py (in trussed/bootloader/nrf52_upload/dfu) seem to be incompatible:

Traceback (most recent call last):
  File "/usr/bin/nitropy", line 5, in <module>
    from pynitrokey.cli import main
  File "/usr/lib/python3.12/site-packages/pynitrokey/cli/__init__.py", line 26, in <module>
    from pynitrokey.cli.nk3 import nk3
  File "/usr/lib/python3.12/site-packages/pynitrokey/cli/nk3/__init__.py", line 19, in <module>
    from pynitrokey.nk3 import NK3_DATA
  File "/usr/lib/python3.12/site-packages/pynitrokey/nk3/__init__.py", line 14, in <module>
    from pynitrokey.trussed.bootloader.nrf52 import SignatureKey
  File "/usr/lib/python3.12/site-packages/pynitrokey/trussed/bootloader/nrf52.py", line 29, in <module>
    from .nrf52_upload.dfu.init_packet_pb import InitPacketPB
  File "/usr/lib/python3.12/site-packages/pynitrokey/trussed/bootloader/nrf52_upload/dfu/init_packet_pb.py", line 39, in <module>
    from . import dfu_cc_pb2 as pb
  File "/usr/lib/python3.12/site-packages/pynitrokey/trussed/bootloader/nrf52_upload/dfu/dfu_cc_pb2.py", line 34, in <module>
    _descriptor.EnumValueDescriptor(
  File "/usr/lib/python3.12/site-packages/google/protobuf/descriptor.py", line 917, in __new__
    _message.Message._CheckCalledFromGeneratedFile()
TypeError: Descriptors cannot be created directly.
If this call came from a _pb2.py file, your generated code is out of date and must be regenerated with protoc >= 3.19.0.
If you cannot immediately regenerate your protos, some other possible workarounds are:
 1. Downgrade the protobuf package to 3.20.x or lower.
 2. Set PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=python (but this will use pure-Python parsing and will be much slower).

More information: https://developers.google.com/protocol-buffers/docs/news/2022-05-06#python-updates

I regenerated the python definition from the protobuf definition found at https://github.com/NordicSemiconductor/pc-nrfutil/blob/master/nordicsemi/dfu/dfu-cc.proto, and it fixes the problem.

I think in the long run this protobuf definition needs to be included in this repo to regenerate the python files as needed.

I'm happy to open a pull request that includes the .proto file and the newly generated dfu_cc_pb2.py. Let me know if you'd like me to proceed with that.

protobuf: 27.1 pynitrokey: 0.4.47

sosthene-nitrokey commented 6 days ago

Hi,

We are also encountering the same issue. A temporary fix you can use is to set the following environment variable:

export PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=python

We will see how we can solve this issue.