Skip to content

Commit 1c9b29b

Browse files
authored
Merge pull request #84 from arduino/ssl_ctx_attr
misc: Fix exceptions on missing attributes on MicroPython.
2 parents cfb5de9 + 184561c commit 1c9b29b

File tree

6 files changed

+20
-21
lines changed

6 files changed

+20
-21
lines changed

.github/workflows/python-linter.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,4 @@ jobs:
3535
run: |
3636
# stop the build if there are Python syntax errors or undefined names
3737
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
38-
flake8 . --count --max-complexity=15 --max-line-length=120 --statistics
38+
flake8 . --count --ignore=C901 --max-complexity=15 --max-line-length=120 --statistics

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ DEVICE_ID = "" # Provided by Arduino cloud when creating a device.
4141
SECRET_KEY = "" # Provided by Arduino cloud when creating a device.
4242
```
4343

44-
Note that by default, the client runs in asynchronous mode. In this mode, the client takes runs an asyncio loop that updates tasks and records, polls networking events, etc. The client also supports a synchronous mode, which requires periodic client polling. To run the client in synchronous mode, pass `sync_mode=True` when creating a client object and call `client.update()` periodically after connecting. For example:
44+
Note that by default, the client runs in asynchronous mode. In this mode, the client runs an asyncio loop that updates tasks and records, polls networking events, etc. The client also supports a synchronous mode, which requires periodic client polling. To run the client in synchronous mode, pass `sync_mode=True` when creating a client object and call `client.update()` periodically after connecting. For example:
4545

4646
```Python
4747
# Run the client in synchronous mode.

examples/example.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ def user_task(client):
4343
if __name__ == "__main__":
4444
# Parse command line args.
4545
parser = argparse.ArgumentParser(description="arduino_iot_cloud.py")
46-
parser.add_argument("-d", "--debug", action="store_true", help="Enable debugging messages")
47-
parser.add_argument("-s", "--sync", action="store_true", help="Run in synchronous mode")
46+
parser.add_argument("-d", "--debug", action="store_true", help="Enable debugging messages")
47+
parser.add_argument("-s", "--sync", action="store_true", help="Run in synchronous mode")
4848
args = parser.parse_args()
4949

5050
# Assume the host has an active Internet connection.

src/arduino_iot_cloud/ucloud.py

+7-14
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
66

77
import time
8-
import sys
98
import logging
109
import cbor2
1110
from senml import SenmlPack
@@ -22,7 +21,7 @@ class InvalidStateError(Exception):
2221
try:
2322
from arduino_iot_cloud._version import __version__
2423
except (ImportError, AttributeError):
25-
__version__ = "1.3.0"
24+
__version__ = "1.3.1"
2625

2726
# Server/port for basic auth.
2827
_DEFAULT_SERVER = "iot.arduino.cc"
@@ -40,7 +39,7 @@ def timestamp():
4039

4140

4241
def timestamp_ms():
43-
return time.time_ns()//1000000
42+
return time.time_ns() // 1000000
4443

4544

4645
def log_level_enabled(level):
@@ -199,14 +198,6 @@ def __init__(
199198
self.async_mode = not sync_mode
200199
self.connected = False
201200

202-
if "pin" in ssl_params:
203-
try:
204-
# Use M2Crypto to load key and cert from HSM.
205-
import M2Crypto # noqa
206-
except (ImportError, AttributeError):
207-
logging.error("The m2crypto module is required to use HSM.")
208-
sys.exit(1)
209-
210201
# Convert args to bytes if they are passed as strings.
211202
if isinstance(device_id, str):
212203
device_id = bytes(device_id, "utf-8")
@@ -372,9 +363,11 @@ def poll_discovery(self, aiot=None):
372363
self.mqtt.subscribe(self.create_topic("shadow", "i"), qos=1)
373364
self.mqtt.publish(self.create_topic("shadow", "o"), self.senmlpack.to_cbor(), qos=1)
374365

375-
# Push library version and mode.
376-
libv = "%s-%s" % (__version__, "async" if self.async_mode else "sync")
377-
self.mqtt.publish(self.command_topic, cbor2.dumps(cbor2.CBORTag(67328, [libv])), qos=1)
366+
if hasattr(cbor2, "dumps"):
367+
# Push library version and mode.
368+
libv = "%s-%s" % (__version__, "async" if self.async_mode else "sync")
369+
# Note we have to add the tag manually because python-ecosys's cbor2 doesn't suppor CBORTags.
370+
self.mqtt.publish(self.command_topic, b"\xda\x00\x01\x07\x00" + cbor2.dumps([libv]), qos=1)
378371
logging.info("Device configured via discovery protocol.")
379372
if self.async_mode:
380373
raise DoneException()

src/arduino_iot_cloud/ussl.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
# SSL module with m2crypto backend for HSM support.
88

99
import ssl
10+
import sys
11+
import logging
1012

1113
pkcs11 = None
1214

@@ -29,7 +31,7 @@ def wrap_socket(sock, ssl_params={}):
2931
ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
3032
if hasattr(ctx, "set_default_verify_paths"):
3133
ctx.set_default_verify_paths()
32-
if verify != ssl.CERT_REQUIRED:
34+
if hasattr(ctx, "check_hostname") and verify != ssl.CERT_REQUIRED:
3335
ctx.check_hostname = False
3436
ctx.verify_mode = verify
3537
if keyfile is not None and certfile is not None:
@@ -41,7 +43,11 @@ def wrap_socket(sock, ssl_params={}):
4143
return ctx.wrap_socket(sock, server_hostname=hostname)
4244
else:
4345
# Use M2Crypto to load key and cert from HSM.
44-
from M2Crypto import m2, SSL, Engine
46+
try:
47+
from M2Crypto import m2, SSL, Engine
48+
except (ImportError, AttributeError):
49+
logging.error("The m2crypto module is required to use HSM.")
50+
sys.exit(1)
4551

4652
global pkcs11
4753
if pkcs11 is None:

tests/ci.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def wdt_task(client, ts=[None]):
4848
"-f", "--file-auth", action="store_true", help="Use key/cert files"
4949
)
5050
parser.add_argument(
51-
"-s", "--sync", action="store_true", help="Run in synchronous mode"
51+
"-s", "--sync", action="store_true", help="Run in synchronous mode"
5252
)
5353
args = parser.parse_args()
5454

0 commit comments

Comments
 (0)