From 5710148c90a6e2392c0597604ed81cd101b50858 Mon Sep 17 00:00:00 2001 From: Gus Class Date: Fri, 2 Mar 2018 17:04:24 -0800 Subject: [PATCH] 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. --- iot/api-client/manager/README.md | 144 +++++++++++------- .../cloud/iot/examples/HttpExample.java | 2 +- 2 files changed, 89 insertions(+), 57 deletions(-) diff --git a/iot/api-client/manager/README.md b/iot/api-client/manager/README.md index f4b7d3031c7..02271d0bbba 100644 --- a/iot/api-client/manager/README.md +++ b/iot/api-client/manager/README.md @@ -8,6 +8,10 @@ This sample app demonstrates device management for Google Cloud IoT Core. Note that before you can run the sample, you must configure a Google Cloud PubSub topic for Cloud IoT as described in [the parent README](../README.md). +Before running the samples, you can set the `GOOGLE_CLOUD_PROJECT` and +`GOOGLE_APPLICATION_CREDENTIALS` environment variables to avoid passing them to +the sample every time you run it. + ## Setup Run the following command to install the libraries and build the sample with Maven: @@ -24,7 +28,7 @@ The following description summarizes the sample usage: Cloud IoT Core Commandline Example (Device / Registry management): - --cloud_region GCP cloud region. + --cloud_region GCP cloud region (default us-central1). --command Command to run: create-iot-topic create-rsa @@ -58,85 +62,109 @@ run the sample as: Create a PubSub topic, `hello-java`, for the project, `blue-jet-123`: mvn exec:java \ - -Dexec.mainClass="com.example.cloud.iot.examples.DeviceRegistryExample" \ - -Dexec.args="-project_id=blue-jet-123 \ - -command=create-iot-topic \ - -pubsub_topic=hello-java " + -Dexec.mainClass="com.example.cloud.iot.examples.DeviceRegistryExample" \ + -Dexec.args="-project_id=blue-jet-123 \ + -command=create-iot-topic \ + -pubsub_topic=hello-java" Create an ES device: - java -cp target/cloudiot-manager-demo-1.0-jar-with-dependencies.jar \ - com.example.cloud.iot.examples.DeviceRegistryExample \ - -project_id=blue-jet-123 -pubsub_topic=hello-java \ - -registry_name=hello-java -ec_public_key_file ../ec_public.pem \ - -device_id="java-device-0" -command=create-es + mvn exec:java \ + -Dexec.mainClass="com.example.cloud.iot.examples.DeviceRegistryExample" \ + -Dexec.args="-project_id=blue-jet-123 \ + -pubsub_topic=hello-java \ + -cloud_region=us-central1 \ + -registry_name=hello-java \ + -ec_public_key_file ../ec_public.pem \ + -device_id=java-device-0 \ + -command=create-es" Create an RSA device: - java -cp target/cloudiot-manager-demo-1.0-jar-with-dependencies.jar \ - com.example.cloud.iot.examples.DeviceRegistryExample \ - -project_id=blue-jet-123 -pubsub_topic=hello-java \ - -registry_name=hello-java -rsa_certificate_file ../rsa_cert.pem \ - -device_id="java-device-1" -command=create-rsa + mvn exec:java \ + -Dexec.mainClass="com.example.cloud.iot.examples.DeviceRegistryExample" \ + -Dexec.args="-project_id=blue-jet-123 \ + -pubsub_topic=hello-java \ + -registry_name=hello-java \ + -rsa_certificate_file ../rsa_cert.pem \ + -device_id=java-device-1 \ + -command=create-rsa" Create a device without authorization: - java -cp target/cloudiot-manager-demo-1.0-jar-with-dependencies.jar \ - com.example.cloud.iot.examples.DeviceRegistryExample \ - -project_id=blue-jet-123 -pubsub_topic=hello-java \ - -registry_name=hello-java -device_id="java-device-3" \ - -command=create-unauth + mvn exec:java \ + -Dexec.mainClass="com.example.cloud.iot.examples.DeviceRegistryExample" \ + -Dexec.args="-project_id=blue-jet-123 \ + -pubsub_topic=hello-java \ + -registry_name=hello-java \ + -device_id=java-device-3 \ + -command=create-unauth" Create a device registry: - java -cp target/cloudiot-manager-demo-1.0-jar-with-dependencies.jar \ - com.example.cloud.iot.examples.DeviceRegistryExample \ - -project_id=blue-jet-123 -pubsub_topic=hello-java \ - -registry_name=hello-java -command=create-registry + mvn exec:java \ + -Dexec.mainClass="com.example.cloud.iot.examples.DeviceRegistryExample" \ + -Dexec.args="-project_id=blue-jet-123 \ + -pubsub_topic=hello-java \ + -registry_name=hello-java \ + -command=create-registry" Delete a device registry: - java -cp target/cloudiot-manager-demo-1.0-jar-with-dependencies.jar \ - com.example.cloud.iot.examples.DeviceRegistryExample \ - -project_id=blue-jet-123 -pubsub_topic=hello-java \ - -registry_name=hello-java -command=delete-registry + mvn exec:java \ + -Dexec.mainClass="com.example.cloud.iot.examples.DeviceRegistryExample" \ + -Dexec.args="-project_id=blue-jet-123 \ + -pubsub_topic=hello-java \ + -registry_name=hello-java \ + -command=delete-registry" Get a device registry: - java -cp target/cloudiot-manager-demo-1.0-jar-with-dependencies.jar \ - com.example.cloud.iot.examples.DeviceRegistryExample \ - -project_id=blue-jet-123 -pubsub_topic=hello-java \ - -registry_name=hello-java -command=get-registry + mvn exec:java \ + -Dexec.mainClass="com.example.cloud.iot.examples.DeviceRegistryExample" \ + -Dexec.args="-project_id=blue-jet-123 \ + -pubsub_topic=hello-java \ + -registry_name=hello-java \ + -command=get-registry" List devices: - java -cp target/cloudiot-manager-demo-1.0-jar-with-dependencies.jar \ - com.example.cloud.iot.examples.DeviceRegistryExample \ - -project_id=blue-jet-123 -pubsub_topic=hello-java \ - -registry_name=hello-java -command=list-devices + mvn exec:java \ + -Dexec.mainClass="com.example.cloud.iot.examples.DeviceRegistryExample" \ + -Dexec.args="-project_id=blue-jet-123 \ + -pubsub_topic=hello-java \ + -registry_name=hello-java \ + -command=list-devices" List device registries: - java -cp target/cloudiot-manager-demo-1.0-jar-with-dependencies.jar \ - com.example.cloud.iot.examples.DeviceRegistryExample \ - -project_id=blue-jet-123 -pubsub_topic=hello-java \ - -registry_name=hello-java -command=list-registries + mvn exec:java \ + -Dexec.mainClass="com.example.cloud.iot.examples.DeviceRegistryExample" \ + -Dexec.args="-project_id=blue-jet-123 \ + -pubsub_topic=hello-java \ + -registry_name=hello-java \ + -command=list-registries" Patch a device with ES: - java -cp target/cloudiot-manager-demo-1.0-jar-with-dependencies.jar \ - com.example.cloud.iot.examples.DeviceRegistryExample \ - -project_id=blue-jet-123 -pubsub_topic=hello-java \ - -registry_name=hello-java -ec_public_key_file ../ec_public.pem \ - -device_id="java-device-1" -command=patch-device-es + mvn exec:java \ + -Dexec.mainClass="com.example.cloud.iot.examples.DeviceRegistryExample" \ + -Dexec.args="-project_id=blue-jet-123 \ + -pubsub_topic=hello-java \ + -registry_name=hello-java \ + -ec_public_key_file ../ec_public.pem \ + -device_id=java-device-1 -command=patch-device-es" Patch a device with RSA: - java -cp target/cloudiot-manager-demo-1.0-jar-with-dependencies.jar \ - com.example.cloud.iot.examples.DeviceRegistryExample \ - -project_id=blue-jet-123 -pubsub_topic=hello-java \ - -registry_name=hello-java -rsa_certificate_file ../rsa_cert.pem \ - -device_id="java-device-0" -command=patch-device-rsa + mvn exec:java \ + -Dexec.mainClass="com.example.cloud.iot.examples.DeviceRegistryExample" \ + -Dexec.args="-project_id=blue-jet-123 \ + -pubsub_topic=hello-java \ + -registry_name=hello-java \ + -rsa_certificate_file ../rsa_cert.pem \ + -device_id=java-device-0 \ + -command=patch-device-rsa" # Cloud IoT Core Java HTTP example @@ -169,9 +197,9 @@ The following command summarizes the sample usage: -algorithm=" ``` -For example, if your project ID is `blue-jet-123`, your service account -credentials are stored in your home folder in creds.json and you have generated -your credentials using the [`generate_keys.sh`](../generate_keys.sh) script +For example, if your project ID is `blue-jet-123`, the Cloud region associated +with your device registry is europe-west1, and you have generated your +credentials using the [`generate_keys.sh`](../generate_keys.sh) script provided in the parent folder, you can run the sample as: ``` @@ -179,6 +207,7 @@ provided in the parent folder, you can run the sample as: -Dexec.mainClass="com.example.cloud.iot.examples.HttpExample" \ -Dexec.args="-project_id=blue-jet-123 \ -registry_id=my-registry \ + -cloud_region=europe-west1 \ -device_id=my-java-device \ -private_key_file=../rsa_private_pkcs8 \ -algorithm=RS256" @@ -191,6 +220,7 @@ To publish state messages, run the sample as follows: -Dexec.mainClass="com.example.cloud.iot.examples.HttpExample" \ -Dexec.args="-project_id=blue-jet-123 \ -registry_id=my-registry \ + -cloud_region=us-central1 \ -device_id=my-java-device \ -private_key_file=../rsa_private_pkcs8 \ -message_type=state \ @@ -238,19 +268,21 @@ The following command summarizes the sample usage: -Dexec.mainClass="com.example.cloud.iot.examples.MqttExample" \ -Dexec.args="-project_id=my-iot-project \ -registry_id=my-registry \ + -cloud_region=us-central1 \ -device_id=my-device \ -private_key_file=rsa_private_pkcs8 \ -algorithm=RS256" -For example, if your project ID is `blue-jet-123`, your service account -credentials are stored in your home folder in creds.json and you have generated -your credentials using the [`generate_keys.sh`](../generate_keys.sh) script +For example, if your project ID is `blue-jet-123`, your device registry is +located in the `asia-east1` region, and you have generated your +credentials using the [`generate_keys.sh`](../generate_keys.sh) script provided in the parent folder, you can run the sample as: mvn exec:java \ -Dexec.mainClass="com.example.cloud.iot.examples.MqttExample" \ -Dexec.args="-project_id=blue-jet-123 \ -registry_id=my-registry \ + -cloud_region=asia-east1 \ -device_id=my-device \ -private_key_file=../rsa_private_pkcs8 \ -algorithm=RS256" diff --git a/iot/api-client/manager/src/main/java/com/example/cloud/iot/examples/HttpExample.java b/iot/api-client/manager/src/main/java/com/example/cloud/iot/examples/HttpExample.java index 18ad0aa7239..f7eb77e14f0 100644 --- a/iot/api-client/manager/src/main/java/com/example/cloud/iot/examples/HttpExample.java +++ b/iot/api-client/manager/src/main/java/com/example/cloud/iot/examples/HttpExample.java @@ -102,7 +102,7 @@ private static String createJwtEs(String projectId, String privateKeyFile) throw byte[] keyBytes = Files.readAllBytes(Paths.get(privateKeyFile)); PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(keyBytes); - KeyFactory kf = KeyFactory.getInstance("ES256"); + KeyFactory kf = KeyFactory.getInstance("EC"); return jwtBuilder.signWith(SignatureAlgorithm.ES256, kf.generatePrivate(spec)).compact(); }