Skip to content

Commit eba7e72

Browse files
committed
misc: Add client workflow.
1 parent f3e4500 commit eba7e72

File tree

3 files changed

+178
-0
lines changed

3 files changed

+178
-0
lines changed

.github/workflows/client-test.yml

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

tests/ci.py

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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+
ssl._ENGINE_PATH = "/lib/x86_64-linux-gnu/engines-3/libpkcs11.so"
13+
ssl._MODULE_PATH = "/lib/x86_64-linux-gnu/softhsm/libsofthsm2.so"
14+
15+
def exception_handler(loop, context):
16+
pass
17+
18+
19+
def on_value_changed(client, value):
20+
logging.info(f"The answer to life, the universe, and everything is {value}")
21+
loop = asyncio.get_event_loop()
22+
loop.set_exception_handler(exception_handler)
23+
sys.exit(0)
24+
25+
26+
if __name__ == "__main__":
27+
# Parse command line args.
28+
parser = argparse.ArgumentParser(description="arduino_iot_cloud.py")
29+
parser.add_argument(
30+
"-d", "--debug", action="store_true", help="Enable debugging messages"
31+
)
32+
parser.add_argument(
33+
"-b", "--basic-auth", action="store_true", help="Use basic authentication"
34+
)
35+
parser.add_argument(
36+
"-k", "--crypto-files", action="store_true", help="Use key and cert files"
37+
)
38+
parser.add_argument(
39+
"-p", "--crypto-dev", action="store_true", help="Use crypto device"
40+
)
41+
args = parser.parse_args()
42+
43+
# Configure the logger.
44+
# All message equal or higher to the logger level are printed.
45+
# To see more debugging messages, pass --debug on the command line.
46+
logging.basicConfig(
47+
datefmt="%H:%M:%S",
48+
format="%(asctime)s.%(msecs)03d %(message)s",
49+
level=logging.DEBUG if args.debug else logging.INFO,
50+
)
51+
52+
# Create a client object to connect to the Arduino IoT cloud.
53+
# To use a secure element, set the token's "pin" and URI in "keyfile" and "certfile", and
54+
# the CA certificate (if any) in "ssl_params". Alternatively, a username and password can
55+
# be used to authenticate, for example:
56+
# client = ArduinoCloudClient(device_id=DEVICE_ID, username=DEVICE_ID, password=SECRET_KEY)
57+
if args.basic_auth:
58+
client = ArduinoCloudClient(
59+
device_id=os.environ["DEVICE_ID"],
60+
username=os.environ["DEVICE_ID"],
61+
password=os.environ["SECRET_KEY"],
62+
)
63+
elif args.crypto_files:
64+
client = ArduinoCloudClient(
65+
device_id=os.environ["DEVICE_ID"],
66+
ssl_params={
67+
"keyfile": "key.pem",
68+
"certfile": "cert.pem",
69+
"ca_certs": "ca_root.pem",
70+
"cert_reqs": ssl.CERT_REQUIRED,
71+
},
72+
)
73+
elif args.crypto_files:
74+
client = ArduinoCloudClient(
75+
device_id=os.environ["DEVICE_ID"],
76+
ssl_params={
77+
"pin": "1234",
78+
"keyfile": "pkcs11:token=arduino",
79+
"certfile": "pkcs11:token=arduino",
80+
"ca_certs": "ca_root.pem",
81+
"cert_reqs": ssl.CERT_REQUIRED,
82+
},
83+
)
84+
85+
# Register cloud objects.
86+
# Note: The following objects must be created first in the dashboard and linked to the device.
87+
# This cloud object is initialized with its last known value from the cloud. When this object is updated
88+
# from the dashboard, the on_switch_changed function is called with the client object and the new value.
89+
client.register("answer", value=None, on_write=on_value_changed)
90+
91+
# Start the Arduino IoT cloud client.
92+
client.start()

tests/ci.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/bin/bash
2+
3+
ci_configure_softhsm() {
4+
TOKEN="pkcs11:token=arduino"
5+
PROVIDER=/usr/lib/softhsm/libsofthsm2.so
6+
7+
echo "$KEY_PEM" >> key.pem
8+
echo "$CERT_PEM" >> cert.pem
9+
echo "$CA_PEM" >> ca-root.pem
10+
11+
sudo softhsm2-util --init-token --slot 0 --label "arduino" --pin 1234 --so-pin 1234
12+
sudo p11tool --provider=${PROVIDER} --login --set-pin=1234 --write ${TOKEN} --load-privkey key.pem --label "mykey"
13+
sudo p11tool --provider=${PROVIDER} --login --set-pin=1234 --write ${TOKEN} --load-certificate cert.pem --label "mycert"
14+
}

0 commit comments

Comments
 (0)