Skip to content

Commit 0eff875

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

File tree

3 files changed

+176
-0
lines changed

3 files changed

+176
-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: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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+
"-b", "--basic-auth", action="store_true", help="Use basic authentication"
32+
)
33+
parser.add_argument(
34+
"-k", "--crypto-files", action="store_true", help="Use key and cert files"
35+
)
36+
parser.add_argument(
37+
"-p", "--crypto-dev", action="store_true", help="Use crypto device"
38+
)
39+
args = parser.parse_args()
40+
41+
# Configure the logger.
42+
# All message equal or higher to the logger level are printed.
43+
# To see more debugging messages, pass --debug on the command line.
44+
logging.basicConfig(
45+
datefmt="%H:%M:%S",
46+
format="%(asctime)s.%(msecs)03d %(message)s",
47+
level=logging.DEBUG if args.debug else logging.INFO,
48+
)
49+
50+
# Create a client object to connect to the Arduino IoT cloud.
51+
# To use a secure element, set the token's "pin" and URI in "keyfile" and "certfile", and
52+
# the CA certificate (if any) in "ssl_params". Alternatively, a username and password can
53+
# be used to authenticate, for example:
54+
# client = ArduinoCloudClient(device_id=DEVICE_ID, username=DEVICE_ID, password=SECRET_KEY)
55+
if args.basic_auth:
56+
client = ArduinoCloudClient(
57+
device_id=os.environ["DEVICE_ID"],
58+
username=os.environ["DEVICE_ID"],
59+
password=os.environ["SECRET_KEY"],
60+
)
61+
elif args.crypto_files:
62+
client = ArduinoCloudClient(
63+
device_id=os.environ["DEVICE_ID"],
64+
ssl_params={
65+
"keyfile": "key.pem",
66+
"certfile": "cert.pem",
67+
"ca_certs": "ca_root.pem",
68+
"cert_reqs": ssl.CERT_REQUIRED,
69+
},
70+
)
71+
elif args.crypto_files:
72+
client = ArduinoCloudClient(
73+
device_id=os.environ["DEVICE_ID"],
74+
ssl_params={
75+
"pin": "1234",
76+
"keyfile": "pkcs11:token=arduino",
77+
"certfile": "pkcs11:token=arduino",
78+
"ca_certs": "ca_root.pem",
79+
"cert_reqs": ssl.CERT_REQUIRED,
80+
},
81+
)
82+
83+
# Register cloud objects.
84+
# Note: The following objects must be created first in the dashboard and linked to the device.
85+
# This cloud object is initialized with its last known value from the cloud. When this object is updated
86+
# from the dashboard, the on_switch_changed function is called with the client object and the new value.
87+
client.register("answer", value=None, on_write=on_value_changed)
88+
89+
# Start the Arduino IoT cloud client.
90+
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)