Skip to content

Commit 5710148

Browse files
gguusskurtisvg
authored andcommitted
Post-GA nits (#1040)
* Fixes keyFactory type for ES vs EC keys in HTTP example (MQTT was done before) and adds more comprehensive examples to README * Correct environment variable and improved style in manager examples.
1 parent d9b6bec commit 5710148

File tree

2 files changed

+89
-57
lines changed

2 files changed

+89
-57
lines changed

iot/api-client/manager/README.md

+88-56
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ This sample app demonstrates device management for Google Cloud IoT Core.
88
Note that before you can run the sample, you must configure a Google Cloud
99
PubSub topic for Cloud IoT as described in [the parent README](../README.md).
1010

11+
Before running the samples, you can set the `GOOGLE_CLOUD_PROJECT` and
12+
`GOOGLE_APPLICATION_CREDENTIALS` environment variables to avoid passing them to
13+
the sample every time you run it.
14+
1115
## Setup
1216
Run the following command to install the libraries and build the sample with
1317
Maven:
@@ -24,7 +28,7 @@ The following description summarizes the sample usage:
2428

2529
Cloud IoT Core Commandline Example (Device / Registry management):
2630

27-
--cloud_region <arg> GCP cloud region.
31+
--cloud_region <arg> GCP cloud region (default us-central1).
2832
--command <arg> Command to run:
2933
create-iot-topic
3034
create-rsa
@@ -58,85 +62,109 @@ run the sample as:
5862
Create a PubSub topic, `hello-java`, for the project, `blue-jet-123`:
5963

6064
mvn exec:java \
61-
-Dexec.mainClass="com.example.cloud.iot.examples.DeviceRegistryExample" \
62-
-Dexec.args="-project_id=blue-jet-123 \
63-
-command=create-iot-topic \
64-
-pubsub_topic=hello-java "
65+
-Dexec.mainClass="com.example.cloud.iot.examples.DeviceRegistryExample" \
66+
-Dexec.args="-project_id=blue-jet-123 \
67+
-command=create-iot-topic \
68+
-pubsub_topic=hello-java"
6569

6670
Create an ES device:
6771

68-
java -cp target/cloudiot-manager-demo-1.0-jar-with-dependencies.jar \
69-
com.example.cloud.iot.examples.DeviceRegistryExample \
70-
-project_id=blue-jet-123 -pubsub_topic=hello-java \
71-
-registry_name=hello-java -ec_public_key_file ../ec_public.pem \
72-
-device_id="java-device-0" -command=create-es
72+
mvn exec:java \
73+
-Dexec.mainClass="com.example.cloud.iot.examples.DeviceRegistryExample" \
74+
-Dexec.args="-project_id=blue-jet-123 \
75+
-pubsub_topic=hello-java \
76+
-cloud_region=us-central1 \
77+
-registry_name=hello-java \
78+
-ec_public_key_file ../ec_public.pem \
79+
-device_id=java-device-0 \
80+
-command=create-es"
7381

7482
Create an RSA device:
7583

76-
java -cp target/cloudiot-manager-demo-1.0-jar-with-dependencies.jar \
77-
com.example.cloud.iot.examples.DeviceRegistryExample \
78-
-project_id=blue-jet-123 -pubsub_topic=hello-java \
79-
-registry_name=hello-java -rsa_certificate_file ../rsa_cert.pem \
80-
-device_id="java-device-1" -command=create-rsa
84+
mvn exec:java \
85+
-Dexec.mainClass="com.example.cloud.iot.examples.DeviceRegistryExample" \
86+
-Dexec.args="-project_id=blue-jet-123 \
87+
-pubsub_topic=hello-java \
88+
-registry_name=hello-java \
89+
-rsa_certificate_file ../rsa_cert.pem \
90+
-device_id=java-device-1 \
91+
-command=create-rsa"
8192

8293
Create a device without authorization:
8394

84-
java -cp target/cloudiot-manager-demo-1.0-jar-with-dependencies.jar \
85-
com.example.cloud.iot.examples.DeviceRegistryExample \
86-
-project_id=blue-jet-123 -pubsub_topic=hello-java \
87-
-registry_name=hello-java -device_id="java-device-3" \
88-
-command=create-unauth
95+
mvn exec:java \
96+
-Dexec.mainClass="com.example.cloud.iot.examples.DeviceRegistryExample" \
97+
-Dexec.args="-project_id=blue-jet-123 \
98+
-pubsub_topic=hello-java \
99+
-registry_name=hello-java \
100+
-device_id=java-device-3 \
101+
-command=create-unauth"
89102

90103
Create a device registry:
91104

92-
java -cp target/cloudiot-manager-demo-1.0-jar-with-dependencies.jar \
93-
com.example.cloud.iot.examples.DeviceRegistryExample \
94-
-project_id=blue-jet-123 -pubsub_topic=hello-java \
95-
-registry_name=hello-java -command=create-registry
105+
mvn exec:java \
106+
-Dexec.mainClass="com.example.cloud.iot.examples.DeviceRegistryExample" \
107+
-Dexec.args="-project_id=blue-jet-123 \
108+
-pubsub_topic=hello-java \
109+
-registry_name=hello-java \
110+
-command=create-registry"
96111

97112
Delete a device registry:
98113

99-
java -cp target/cloudiot-manager-demo-1.0-jar-with-dependencies.jar \
100-
com.example.cloud.iot.examples.DeviceRegistryExample \
101-
-project_id=blue-jet-123 -pubsub_topic=hello-java \
102-
-registry_name=hello-java -command=delete-registry
114+
mvn exec:java \
115+
-Dexec.mainClass="com.example.cloud.iot.examples.DeviceRegistryExample" \
116+
-Dexec.args="-project_id=blue-jet-123 \
117+
-pubsub_topic=hello-java \
118+
-registry_name=hello-java \
119+
-command=delete-registry"
103120

104121
Get a device registry:
105122

106-
java -cp target/cloudiot-manager-demo-1.0-jar-with-dependencies.jar \
107-
com.example.cloud.iot.examples.DeviceRegistryExample \
108-
-project_id=blue-jet-123 -pubsub_topic=hello-java \
109-
-registry_name=hello-java -command=get-registry
123+
mvn exec:java \
124+
-Dexec.mainClass="com.example.cloud.iot.examples.DeviceRegistryExample" \
125+
-Dexec.args="-project_id=blue-jet-123 \
126+
-pubsub_topic=hello-java \
127+
-registry_name=hello-java \
128+
-command=get-registry"
110129

111130
List devices:
112131

113-
java -cp target/cloudiot-manager-demo-1.0-jar-with-dependencies.jar \
114-
com.example.cloud.iot.examples.DeviceRegistryExample \
115-
-project_id=blue-jet-123 -pubsub_topic=hello-java \
116-
-registry_name=hello-java -command=list-devices
132+
mvn exec:java \
133+
-Dexec.mainClass="com.example.cloud.iot.examples.DeviceRegistryExample" \
134+
-Dexec.args="-project_id=blue-jet-123 \
135+
-pubsub_topic=hello-java \
136+
-registry_name=hello-java \
137+
-command=list-devices"
117138

118139
List device registries:
119140

120-
java -cp target/cloudiot-manager-demo-1.0-jar-with-dependencies.jar \
121-
com.example.cloud.iot.examples.DeviceRegistryExample \
122-
-project_id=blue-jet-123 -pubsub_topic=hello-java \
123-
-registry_name=hello-java -command=list-registries
141+
mvn exec:java \
142+
-Dexec.mainClass="com.example.cloud.iot.examples.DeviceRegistryExample" \
143+
-Dexec.args="-project_id=blue-jet-123 \
144+
-pubsub_topic=hello-java \
145+
-registry_name=hello-java \
146+
-command=list-registries"
124147

125148
Patch a device with ES:
126149

127-
java -cp target/cloudiot-manager-demo-1.0-jar-with-dependencies.jar \
128-
com.example.cloud.iot.examples.DeviceRegistryExample \
129-
-project_id=blue-jet-123 -pubsub_topic=hello-java \
130-
-registry_name=hello-java -ec_public_key_file ../ec_public.pem \
131-
-device_id="java-device-1" -command=patch-device-es
150+
mvn exec:java \
151+
-Dexec.mainClass="com.example.cloud.iot.examples.DeviceRegistryExample" \
152+
-Dexec.args="-project_id=blue-jet-123 \
153+
-pubsub_topic=hello-java \
154+
-registry_name=hello-java \
155+
-ec_public_key_file ../ec_public.pem \
156+
-device_id=java-device-1 -command=patch-device-es"
132157

133158
Patch a device with RSA:
134159

135-
java -cp target/cloudiot-manager-demo-1.0-jar-with-dependencies.jar \
136-
com.example.cloud.iot.examples.DeviceRegistryExample \
137-
-project_id=blue-jet-123 -pubsub_topic=hello-java \
138-
-registry_name=hello-java -rsa_certificate_file ../rsa_cert.pem \
139-
-device_id="java-device-0" -command=patch-device-rsa
160+
mvn exec:java \
161+
-Dexec.mainClass="com.example.cloud.iot.examples.DeviceRegistryExample" \
162+
-Dexec.args="-project_id=blue-jet-123 \
163+
-pubsub_topic=hello-java \
164+
-registry_name=hello-java \
165+
-rsa_certificate_file ../rsa_cert.pem \
166+
-device_id=java-device-0 \
167+
-command=patch-device-rsa"
140168

141169

142170
# Cloud IoT Core Java HTTP example
@@ -169,16 +197,17 @@ The following command summarizes the sample usage:
169197
-algorithm=<RS256|ES256>"
170198
```
171199

172-
For example, if your project ID is `blue-jet-123`, your service account
173-
credentials are stored in your home folder in creds.json and you have generated
174-
your credentials using the [`generate_keys.sh`](../generate_keys.sh) script
200+
For example, if your project ID is `blue-jet-123`, the Cloud region associated
201+
with your device registry is europe-west1, and you have generated your
202+
credentials using the [`generate_keys.sh`](../generate_keys.sh) script
175203
provided in the parent folder, you can run the sample as:
176204

177205
```
178206
mvn exec:java \
179207
-Dexec.mainClass="com.example.cloud.iot.examples.HttpExample" \
180208
-Dexec.args="-project_id=blue-jet-123 \
181209
-registry_id=my-registry \
210+
-cloud_region=europe-west1 \
182211
-device_id=my-java-device \
183212
-private_key_file=../rsa_private_pkcs8 \
184213
-algorithm=RS256"
@@ -191,6 +220,7 @@ To publish state messages, run the sample as follows:
191220
-Dexec.mainClass="com.example.cloud.iot.examples.HttpExample" \
192221
-Dexec.args="-project_id=blue-jet-123 \
193222
-registry_id=my-registry \
223+
-cloud_region=us-central1 \
194224
-device_id=my-java-device \
195225
-private_key_file=../rsa_private_pkcs8 \
196226
-message_type=state \
@@ -238,19 +268,21 @@ The following command summarizes the sample usage:
238268
-Dexec.mainClass="com.example.cloud.iot.examples.MqttExample" \
239269
-Dexec.args="-project_id=my-iot-project \
240270
-registry_id=my-registry \
271+
-cloud_region=us-central1 \
241272
-device_id=my-device \
242273
-private_key_file=rsa_private_pkcs8 \
243274
-algorithm=RS256"
244275

245-
For example, if your project ID is `blue-jet-123`, your service account
246-
credentials are stored in your home folder in creds.json and you have generated
247-
your credentials using the [`generate_keys.sh`](../generate_keys.sh) script
276+
For example, if your project ID is `blue-jet-123`, your device registry is
277+
located in the `asia-east1` region, and you have generated your
278+
credentials using the [`generate_keys.sh`](../generate_keys.sh) script
248279
provided in the parent folder, you can run the sample as:
249280

250281
mvn exec:java \
251282
-Dexec.mainClass="com.example.cloud.iot.examples.MqttExample" \
252283
-Dexec.args="-project_id=blue-jet-123 \
253284
-registry_id=my-registry \
285+
-cloud_region=asia-east1 \
254286
-device_id=my-device \
255287
-private_key_file=../rsa_private_pkcs8 \
256288
-algorithm=RS256"

iot/api-client/manager/src/main/java/com/example/cloud/iot/examples/HttpExample.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ private static String createJwtEs(String projectId, String privateKeyFile) throw
102102

103103
byte[] keyBytes = Files.readAllBytes(Paths.get(privateKeyFile));
104104
PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(keyBytes);
105-
KeyFactory kf = KeyFactory.getInstance("ES256");
105+
KeyFactory kf = KeyFactory.getInstance("EC");
106106

107107
return jwtBuilder.signWith(SignatureAlgorithm.ES256, kf.generatePrivate(spec)).compact();
108108
}

0 commit comments

Comments
 (0)