Skip to content

Commit 72ab23a

Browse files
committed
misc: Add client workflow.
1 parent 38c4e1f commit 72ab23a

File tree

3 files changed

+186
-0
lines changed

3 files changed

+186
-0
lines changed

.github/workflows/client-test.yml

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
name: '🧪 Test Cloud Client'
2+
3+
on:
4+
push:
5+
branches:
6+
- 'main'
7+
paths:
8+
- '*.py'
9+
- '.github/workflows/*.yml'
10+
- '.github/workflows/*.json'
11+
- '!**/README.md'
12+
13+
pull_request:
14+
types:
15+
- opened
16+
- edited
17+
- reopened
18+
- synchronize
19+
branches:
20+
- 'main'
21+
paths:
22+
- '*.py'
23+
- '.github/workflows/*.yml'
24+
- '.github/workflows/*.json'
25+
- '!**/README.md'
26+
27+
jobs:
28+
build:
29+
runs-on: ubuntu-latest
30+
steps:
31+
- name: '⏳ Checkout repository'
32+
uses: actions/checkout@v3
33+
34+
- name: '🐍 Set up Python'
35+
uses: actions/setup-python@v4
36+
with:
37+
cache: 'pip'
38+
python-version: "3.10"
39+
40+
- name: '🛠 Install dependencies'
41+
run: |
42+
python -m pip install --upgrade pip
43+
python -m pip install build==0.10.0 cbor2==5.4.6 M2Crypto==0.38.0 micropython-senml==0.1.0
44+
sudo apt-get install softhsm2 gnutls-bin libengine-pkcs11-openssl
45+
46+
- name: '📦 Build package'
47+
run: python3 -m build
48+
49+
- name: '🛠 Install package'
50+
run: |
51+
python3 -m build
52+
pip install dist/arduino_iot_cloud-*.whl
53+
54+
- name: '🔑 Configure soft crypto device'
55+
env:
56+
KEY_PEM: ${{ secrets.KEY_PEM }}
57+
CERT_PEM: ${{ secrets.CERT_PEM }}
58+
CA_PEM: ${{ secrets.CA_PEM }}
59+
run: |
60+
source tests/ci.sh && ci_configure_softhsm
61+
62+
- name: '☁️ Connect to IoT cloud (basic auth)'
63+
env:
64+
DEVICE_ID: ${{ secrets.DEVICE_ID1 }}
65+
SECRET_KEY: ${{ secrets.SECRET_KEY }}
66+
run: |
67+
python tests/ci.py
68+
69+
- name: '☁️ Connect to IoT cloud (using crypto device)'
70+
env:
71+
DEVICE_ID: ${{ secrets.DEVICE_ID2 }}
72+
SECRET_KEY: ${{ secrets.SECRET_KEY }}
73+
run: |
74+
export SOFTHSM2_CONF="${HOME}/softhsm/tokens/softhsm2.conf"
75+
python tests/ci.py --crypto-device

tests/ci.py

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# This file is part of the Python Arduino IoT Cloud.
2+
# Any copyright is dedicated to the Public Domain.
3+
# https://creativecommons.org/publicdomain/zero/1.0/
4+
import logging
5+
import os
6+
import sys
7+
import asyncio
8+
from arduino_iot_cloud import ArduinoCloudClient
9+
import argparse
10+
import arduino_iot_cloud.ussl as ssl
11+
12+
13+
def exception_handler(loop, context):
14+
pass
15+
16+
17+
def on_value_changed(client, value):
18+
logging.info(f"The answer to life, the universe, and everything is {value}")
19+
loop = asyncio.get_event_loop()
20+
loop.set_exception_handler(exception_handler)
21+
sys.exit(0)
22+
23+
24+
if __name__ == "__main__":
25+
# Parse command line args.
26+
parser = argparse.ArgumentParser(description="arduino_iot_cloud.py")
27+
parser.add_argument(
28+
"-d", "--debug", action="store_true", help="Enable debugging messages"
29+
)
30+
parser.add_argument(
31+
"-c", "--crypto-device", action="store_true", help="Use crypto device"
32+
)
33+
args = parser.parse_args()
34+
35+
# Configure the logger.
36+
# All message equal or higher to the logger level are printed.
37+
# To see more debugging messages, pass --debug on the command line.
38+
logging.basicConfig(
39+
datefmt="%H:%M:%S",
40+
format="%(asctime)s.%(msecs)03d %(message)s",
41+
level=logging.DEBUG if args.debug else logging.INFO,
42+
)
43+
44+
# Create a client object to connect to the Arduino IoT cloud.
45+
# To use a secure element, set the token's "pin" and URI in "keyfile" and "certfile", and
46+
# the CA certificate (if any) in "ssl_params". Alternatively, a username and password can
47+
# be used to authenticate, for example:
48+
# client = ArduinoCloudClient(device_id=DEVICE_ID, username=DEVICE_ID, password=SECRET_KEY)
49+
if args.crypto_device:
50+
client = ArduinoCloudClient(
51+
device_id=os.environ["DEVICE_ID"],
52+
ssl_params={
53+
"pin": "1234",
54+
"keyfile": "pkcs11:token=arduino",
55+
"certfile": "pkcs11:token=arduino",
56+
"ca_certs": "ca-root.pem",
57+
"cert_reqs": ssl.CERT_REQUIRED,
58+
"engine_path": "/lib/x86_64-linux-gnu/engines-3/libpkcs11.so",
59+
"module_path": "/lib/x86_64-linux-gnu/softhsm/libsofthsm2.so",
60+
},
61+
)
62+
else:
63+
client = ArduinoCloudClient(
64+
device_id=os.environ["DEVICE_ID"],
65+
username=os.environ["DEVICE_ID"],
66+
password=os.environ["SECRET_KEY"],
67+
)
68+
69+
# Register cloud objects.
70+
# Note: The following objects must be created first in the dashboard and linked to the device.
71+
# This cloud object is initialized with its last known value from the cloud. When this object is updated
72+
# from the dashboard, the on_switch_changed function is called with the client object and the new value.
73+
client.register("answer", value=None, on_write=on_value_changed)
74+
75+
# Start the Arduino IoT cloud client.
76+
client.start()

tests/ci.sh

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#!/bin/bash
2+
3+
ci_configure_softhsm() {
4+
TOKEN_DIR=${HOME}/softhsm/tokens/
5+
TOKEN_URI="pkcs11:token=arduino"
6+
PROVIDER=/lib/x86_64-linux-gnu/softhsm/libsofthsm2.so
7+
8+
mkdir -p ${TOKEN_DIR}
9+
cat > ${TOKEN_DIR}/softhsm2.conf << EOF
10+
directories.tokendir = ${TOKEN_DIR}
11+
objectstore.backend = file
12+
13+
# ERROR, WARNING, INFO, DEBUG
14+
log.level = ERROR
15+
16+
# If CKF_REMOVABLE_DEVICE flag should be set
17+
slots.removable = false
18+
19+
# Enable and disable PKCS#11 mechanisms using slots.mechanisms.
20+
slots.mechanisms = ALL
21+
22+
# If the library should reset the state on fork
23+
library.reset_on_fork = false
24+
EOF
25+
26+
export SOFTHSM2_CONF=${TOKEN_DIR}/softhsm2.conf
27+
28+
echo "$KEY_PEM" >> key.pem
29+
echo "$CERT_PEM" >> cert.pem
30+
echo "$CA_PEM" >> ca-root.pem
31+
32+
softhsm2-util --init-token --slot 0 --label "arduino" --pin 1234 --so-pin 1234
33+
p11tool --provider=${PROVIDER} --login --set-pin=1234 --write ${TOKEN_URI} --load-privkey key.pem --label "mykey"
34+
p11tool --provider=${PROVIDER} --login --set-pin=1234 --write ${TOKEN_URI} --load-certificate cert.pem --label "mycert"
35+
}

0 commit comments

Comments
 (0)