Skip to content

Commit

Permalink
Post-GA nits (#1040)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
gguuss authored and kurtisvg committed Mar 16, 2018
1 parent d9b6bec commit 5710148
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 57 deletions.
144 changes: 88 additions & 56 deletions iot/api-client/manager/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -24,7 +28,7 @@ The following description summarizes the sample usage:

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

--cloud_region <arg> GCP cloud region.
--cloud_region <arg> GCP cloud region (default us-central1).
--command <arg> Command to run:
create-iot-topic
create-rsa
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -169,16 +197,17 @@ The following command summarizes the sample usage:
-algorithm=<RS256|ES256>"
```

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:

```
mvn exec:java \
-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"
Expand All @@ -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 \
Expand Down Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down

0 comments on commit 5710148

Please sign in to comment.