Skip to content

Commit 34f7e7e

Browse files
authored
Fix pkcs11_connect.py sample (#312)
**Issue:** Missed a `return` when revamping the samples a few commits back, and this wasn't tested as part of CI so the mistake wasn't caught. **Changes:** Trivial fix. Add pkcs11_connect.py sample to our CI so we catch any future breaks.
1 parent e2b3929 commit 34f7e7e

File tree

4 files changed

+41
-2
lines changed

4 files changed

+41
-2
lines changed

codebuild/samples/linux-smoke-tests.yml

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@ phases:
44
commands:
55
- add-apt-repository ppa:ubuntu-toolchain-r/test
66
- apt-get update -y
7-
- apt-get install python3 -y
7+
- apt-get install python3 softhsm -y
88
build:
99
commands:
1010
- echo Build started on `date`
1111
- $CODEBUILD_SRC_DIR/codebuild/samples/setup-linux.sh
1212
- $CODEBUILD_SRC_DIR/codebuild/samples/connect-linux.sh
13+
- $CODEBUILD_SRC_DIR/codebuild/samples/pkcs11-connect-linux.sh
1314
- $CODEBUILD_SRC_DIR/codebuild/samples/pubsub-linux.sh
1415
post_build:
1516
commands:
+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#!/bin/bash
2+
3+
set -e
4+
set -o pipefail
5+
6+
pushd $CODEBUILD_SRC_DIR/samples/
7+
8+
ENDPOINT=$(aws secretsmanager get-secret-value --secret-id "unit-test/endpoint" --query "SecretString" | cut -f2 -d":" | sed -e 's/[\\\"\}]//g')
9+
10+
# from hereon commands are echoed. don't leak secrets
11+
set -x
12+
13+
softhsm2-util --version
14+
15+
# SoftHSM2's default tokendir path might be invalid on this machine
16+
# so set up a conf file that specifies a known good tokendir path
17+
mkdir -p /tmp/tokens
18+
export SOFTHSM2_CONF=/tmp/softhsm2.conf
19+
echo "directories.tokendir = /tmp/tokens" > /tmp/softhsm2.conf
20+
21+
# create token
22+
softhsm2-util --init-token --free --label my-token --pin 0000 --so-pin 0000
23+
24+
# add private key to token (must be in PKCS#8 format)
25+
openssl pkcs8 -topk8 -in /tmp/privatekey.pem -out /tmp/privatekey.p8.pem -nocrypt
26+
softhsm2-util --import /tmp/privatekey.p8.pem --token my-token --label my-key --id BEEFCAFE --pin 0000
27+
28+
# run sample
29+
python3 pkcs11_connect.py \
30+
--endpoint $ENDPOINT \
31+
--cert /tmp/certificate.pem \
32+
--pkcs11_lib /usr/lib/softhsm/libsofthsm2.so \
33+
--pin 0000 \
34+
--token_label my-token \
35+
--key_label my-key
36+
37+
popd

samples/command_line_utils.py

+1
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ def build_pkcs11_mqtt_connection(self, on_connection_interrupted, on_connection_
127127
client_id=self.get_command_required("client_id"),
128128
clean_session=False,
129129
keep_alive_secs=30)
130+
return mqtt_connection
130131

131132
def build_websocket_mqtt_connection(self, on_connection_interrupted, on_connection_resumed):
132133
proxy_options = self.get_proxy_options_for_mqtt_connection()

samples/pkcs11_connect.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def on_connection_resumed(connection, return_code, session_present, **kwargs):
4747
if __name__ == '__main__':
4848
# Create a connection using websockets.
4949
# Note: The data for the connection is gotten from cmdUtils.
50-
# (see build_websocket_mqtt_connection for implementation)
50+
# (see build_pkcs11_mqtt_connection for implementation)
5151
mqtt_connection = cmdUtils.build_pkcs11_mqtt_connection(on_connection_interrupted, on_connection_resumed)
5252

5353
print("Connecting to {} with client ID '{}'...".format(

0 commit comments

Comments
 (0)