diff --git a/iot/api-client/http_example/README.md b/iot/api-client/http_example/README.md deleted file mode 100644 index 825640f7d58..00000000000 --- a/iot/api-client/http_example/README.md +++ /dev/null @@ -1,75 +0,0 @@ -# Cloud IoT Core Java HTTP example - -This sample app publishes data to Cloud Pub/Sub using the HTTP bridge provided -as part of Google Cloud IoT Core. - -Note that before you can run the sample, you must configure a Google Cloud -PubSub topic for Cloud IoT Core and register a device as described in the -[parent README](../README.md). - -## Setup - -Run the following command to install the dependencies using Maven: - - mvn clean compile - -## Running the sample - -The following command summarizes the sample usage: - -``` - mvn exec:java \ - -Dexec.mainClass="com.google.cloud.iot.examples.HttpExample" \ - -Dexec.args="-project_id= \ - -registry_id= \ - -device_id= \ - -private_key_file= \ - -message_type= \ - -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 -provided in the parent folder, you can run the sample as: - -``` - mvn exec:java \ - -Dexec.mainClass="com.google.cloud.iot.examples.HttpExample" \ - -Dexec.args="-project_id=blue-jet-123 \ - -registry_id=my-registry \ - -device_id=my-java-device \ - -private_key_file=../rsa_private_pkcs8 \ - -algorithm=RS256" -``` - -To publish state messages, run the sample as follows: - -``` - mvn exec:java \ - -Dexec.mainClass="com.google.cloud.iot.examples.HttpExample" \ - -Dexec.args="-project_id=blue-jet-123 \ - -registry_id=my-registry \ - -device_id=my-java-device \ - -private_key_file=../rsa_private_pkcs8 \ - -message_type=state \ - -algorithm=RS256" -``` - - -## Reading the messages written by the sample client - -1. Create a subscription to your topic. - -``` - gcloud beta pubsub subscriptions create \ - projects/your-project-id/subscriptions/my-subscription \ - --topic device-events -``` - -2. Read messages published to the topic - -``` - gcloud beta pubsub subscriptions pull --auto-ack \ - projects/my-iot-project/subscriptions/my-subscription -``` diff --git a/iot/api-client/http_example/pom.xml b/iot/api-client/http_example/pom.xml deleted file mode 100644 index a60c66ba065..00000000000 --- a/iot/api-client/http_example/pom.xml +++ /dev/null @@ -1,62 +0,0 @@ - - - 4.0.0 - com.google.cloud.iot.examples - cloudiot-http-example - jar - 1.0 - cloudiot-http-example - http://maven.apache.org - - - 1.7 - 1.7 - - - - - doc-samples - com.google.cloud - 1.0.0 - ../../../ - - - - - io.jsonwebtoken - jjwt - 0.7.0 - - - joda-time - joda-time - 2.1 - - - commons-cli - commons-cli - 1.3 - - - org.json - json - 20090211 - - - - diff --git a/iot/api-client/manager/README.md b/iot/api-client/manager/README.md index 70fa6a6984c..1935be2cf4e 100644 --- a/iot/api-client/manager/README.md +++ b/iot/api-client/manager/README.md @@ -134,3 +134,80 @@ Patch a device with RSA: -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 + +This sample app publishes data to Cloud Pub/Sub using the HTTP bridge provided +as part of Google Cloud IoT Core. + +Note that before you can run the sample, you must configure a Google Cloud +PubSub topic for Cloud IoT Core and register a device as described in the +[parent README](../README.md). + +## Setup + +Run the following command to install the dependencies using Maven: + + mvn clean compile + +## Running the sample + +The following command summarizes the sample usage: + +``` + mvn exec:java \ + -Dexec.mainClass="com.example.cloud.iot.examples.HttpExample" \ + -Dexec.args="-project_id= \ + -registry_id= \ + -device_id= \ + -private_key_file= \ + -message_type= \ + -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 +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 \ + -device_id=my-java-device \ + -private_key_file=../rsa_private_pkcs8 \ + -algorithm=RS256" +``` + +To publish state messages, run the sample as follows: + +``` + mvn exec:java \ + -Dexec.mainClass="com.example.cloud.iot.examples.HttpExample" \ + -Dexec.args="-project_id=blue-jet-123 \ + -registry_id=my-registry \ + -device_id=my-java-device \ + -private_key_file=../rsa_private_pkcs8 \ + -message_type=state \ + -algorithm=RS256" +``` + + +## Reading the messages written by the sample client + +1. Create a subscription to your topic. + +``` + gcloud beta pubsub subscriptions create \ + projects/your-project-id/subscriptions/my-subscription \ + --topic device-events +``` + +2. Read messages published to the topic + +``` + gcloud beta pubsub subscriptions pull --auto-ack \ + projects/my-iot-project/subscriptions/my-subscription +``` diff --git a/iot/api-client/manager/pom.xml b/iot/api-client/manager/pom.xml index 59f142d84e3..df8573e75ad 100644 --- a/iot/api-client/manager/pom.xml +++ b/iot/api-client/manager/pom.xml @@ -37,6 +37,21 @@ + + org.json + json + 20090211 + + + io.jsonwebtoken + jjwt + 0.7.0 + + + joda-time + joda-time + 2.1 + com.google.apis google-api-services-cloudiot diff --git a/iot/api-client/manager/resources/README.md b/iot/api-client/manager/resources/README.md new file mode 100644 index 00000000000..27fbefe8215 --- /dev/null +++ b/iot/api-client/manager/resources/README.md @@ -0,0 +1,5 @@ +# Test public certificate files + +The certificates in this folder are only provided for testing and should not be +used for registering your devices. Instructions and a shell script are in the +parent folder for getting started. diff --git a/iot/api-client/manager/resources/ec_public.pem b/iot/api-client/manager/resources/ec_public.pem index d14a5b6fc3f..74b7a7240f8 100644 --- a/iot/api-client/manager/resources/ec_public.pem +++ b/iot/api-client/manager/resources/ec_public.pem @@ -1,4 +1,4 @@ -----BEGIN PUBLIC KEY----- -MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAECfwA4OrF9Pcr1W5mXUa+Dx8hpPYd -+pQ5153zNtSSaeEnA/4hrY2AKxUHmKIPJXYRkZrxTxsFElkkpLcoi/CUNQ== +MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEJ/zj75PhGtoayWD9pm/IbXRXi9ai +RQDMFQ87sOOQIWDTxnsHFplqb8cDCtO1h5QsutcAVTPZL0CZiqIg5j7b1w== -----END PUBLIC KEY----- diff --git a/iot/api-client/manager/resources/rsa_cert.pem b/iot/api-client/manager/resources/rsa_cert.pem index ccaf7eded0a..53cdf27f607 100644 --- a/iot/api-client/manager/resources/rsa_cert.pem +++ b/iot/api-client/manager/resources/rsa_cert.pem @@ -1,18 +1,18 @@ -----BEGIN CERTIFICATE----- -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 -----END CERTIFICATE----- diff --git a/iot/api-client/manager/resources/rsa_private.pem b/iot/api-client/manager/resources/rsa_private.pem new file mode 100644 index 00000000000..bcab9fb0092 --- /dev/null +++ b/iot/api-client/manager/resources/rsa_private.pem @@ -0,0 +1,28 @@ +-----BEGIN PRIVATE KEY----- +MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDdWhkFaxeyeXKe +2Sa2pfuzIGH5jmiuDq+bETmVRSMRuNL3o1hyxMAH+Pch1h8yXacWLqrwg4gSnLfo +SRIVCbTyjB8miabCZ0IhLHG4Bs+3Yz2Bf4sNiNQU3Z68mkUBMPQxg8tlYJR/xK9w +nQv14jF0lGqqBO3I9Pu6AHN4PpBAnos7PIM8B2mLDFkltvoZx0/Sa9yImk01RlO0 +QGhkNS60AXdD8PQZaE94b5L5A1v7PqVUmyqQXQHKO4BaniNOc9zMXg7EklYWSpOR +VK9ExYV7IAo/3G8p41cHim3QuKbxAAEEt0Cd7zm1v9W0PQLSDUcCaOd7DWO/HwGF +F29z82NZAgMBAAECggEBAKMHcfhQqRJ1apteWqoZ1ClZR2/zmFviSypN5HhxhGPQ +Gen0b/ePu9PjZbmRKEcDPUYUNyiP58+1h6pY7y15icE+vM64gY8bWFRwlI0yzxhF +yo4vby4XwgRgOkK06O/4q1IwvMS8/oKgTYsG0EFYSI6RpYH1wWDKNwEGxgUhJ0O/ +YxTlFpEObuBg+aILeEemU6+QoULwNgfCqVUEkbSZFy5Z9D66PdRoiLum6FmwICBd +rChr06ACqCxuiFhMb1VoHPinIvJNZeLClUGShi9R/hgUZfZxddpNKDtEjZGFWgNw +dVpDFbpVLsqiV+B2TlfX10u9RnWoVi5r8Iv3gMdpqqUCgYEA82c9rm/fEi7fp03U +Y5C1z1YX/7UP+bbsOEZi6BsnJOSptbnBt0g4Ph0TRIbWsAAjd9psU3rSK73U8kso +6xCRe05RzhXWbnl80TUGQpLFcAlZd7R6eXe9kESGu+sheIXGcRJnzM6CFykerkhV +nGp29t/sQBVggqBm7/o8oJg1cLcCgYEA6M608xHaMr1Xw+hEdYqAnxJ4nLwG5Gja +T5NyeGyuJnPP9ullza/n2mOm6ujznzD3JGLqhixR4TI3h2vQ7oMDtkbytRFmishW +rrW59OM3hohvdd0GMfI4boNCyEBMc1s1855nqHprpje+l3S9HuPUHYI4KXlQzz4o +fqCoWHSonG8CgYEAjYhO2Yu7jnsrGcGr6mlPDszlD49dRrsTCHYFNSn0zQMZm83z +0ntwGM4XUhWF6Llr5llf+/rDEyRDGtCRbn4n2idkcg1Irtx9s7i9dyCZJdiJ6Y1M +Wi1CUQ1HuyWyRL6k8ULekLzT8Fu8t2LgRR+zJUJ3wZusjshuksRTVJ6tFGkCgYBl +VxWNeYfx7ln1/XVPdSroE7JNB8w4QSxG13hM04I7Ws58PL0D/D6A4kAWobDq6vWp +8SUFouVBkgp5+ITKTl2fSuL7zsdEQlD5XEHGR+IzvdBHDh9UPowyDxFS+ipNTLb/ +hUS6IhAaGsnsQ/TPLX14Guc3pod2rygRdv3RA3ciIQKBgEhWmdwDjB3c8/7c/vhV +gU+OYziQPHJb7IYrvCxAMym+lM6SVAR62vDSkGBwZdgnr4X1LwO9/qbV+eOR07hv +KhDQ9AX10y1t7FHybZMatvr0l283/BVanXonq7ihzrWjDnZU97N05gIj6Ew4EHy7 +EGgOLKa63rYJQK4WLwP4mMEF +-----END PRIVATE KEY----- diff --git a/iot/api-client/manager/resources/rsa_private_pkcs8 b/iot/api-client/manager/resources/rsa_private_pkcs8 new file mode 100644 index 00000000000..c1970933319 Binary files /dev/null and b/iot/api-client/manager/resources/rsa_private_pkcs8 differ diff --git a/iot/api-client/http_example/src/main/java/com/google/cloud/iot/examples/HttpExample.java b/iot/api-client/manager/src/main/java/com/example/cloud/iot/examples/HttpExample.java similarity index 82% rename from iot/api-client/http_example/src/main/java/com/google/cloud/iot/examples/HttpExample.java rename to iot/api-client/manager/src/main/java/com/example/cloud/iot/examples/HttpExample.java index b1c72cc0e52..0e4cd6bed3d 100644 --- a/iot/api-client/http_example/src/main/java/com/google/cloud/iot/examples/HttpExample.java +++ b/iot/api-client/manager/src/main/java/com/example/cloud/iot/examples/HttpExample.java @@ -15,13 +15,14 @@ */ -package com.google.cloud.iot.examples; +package com.example.cloud.iot.examples; // [START cloudiotcore_http_imports] import io.jsonwebtoken.JwtBuilder; import io.jsonwebtoken.Jwts; import io.jsonwebtoken.SignatureAlgorithm; import java.io.IOException; +import java.io.InputStream; import java.io.UnsupportedEncodingException; import java.net.HttpURLConnection; import java.net.ProtocolException; @@ -88,6 +89,42 @@ private static String createJwtEs(String projectId, String privateKeyFile) throw } // [END cloudiotcore_http_createjwt] + // [START cloudiotcore_http_getconfig] + /** Publish an event or state message using Cloud IoT Core via the HTTP API. */ + public static void getConfig(String urlPath, String token, String projectId, + String cloudRegion, String registryId, String deviceId, String version) + throws UnsupportedEncodingException, IOException, JSONException, ProtocolException { + // Build the resource path of the device that is going to be authenticated. + String devicePath = + String.format( + "projects/%s/locations/%s/registries/%s/devices/%s", + projectId, cloudRegion, registryId, deviceId); + + urlPath = urlPath + devicePath + "/config?local_version=" + version; + System.out.println(urlPath); + URL url = new URL(urlPath); + HttpURLConnection httpCon = (HttpURLConnection) url.openConnection(); + httpCon.setDoOutput(true); + httpCon.setRequestMethod("GET"); + + // Add headers. + httpCon.setRequestProperty("authorization", String.format("Bearer %s", token)); + httpCon.setRequestProperty("content-type", "application/json; charset=UTF-8"); + httpCon.setRequestProperty("cache-control", "no-cache"); + + System.out.println(httpCon.getResponseCode()); + System.out.println(httpCon.getResponseMessage()); + byte[] buffer = new byte[1024]; + InputStream in = httpCon.getInputStream(); + int len = in.read(buffer); + while (len != -1) { + System.out.write(buffer, 0, len); + len = in.read(buffer); + } + } + // [END cloudiotcore_http_getconfig] + + // [START cloudiotcore_http_publishmessage] /** Publish an event or state message using Cloud IoT Core via the HTTP API. */ public static void publishMessage(String payload, String urlPath, String messageType, @@ -105,7 +142,6 @@ public static void publishMessage(String payload, String urlPath, String message String encPayload = encoder.encodeToString(payload.getBytes("UTF-8")); - urlPath = urlPath + devicePath + ":" + urlSuffix; URL url = new URL(urlPath); HttpURLConnection httpCon = (HttpURLConnection) url.openConnection(); @@ -138,6 +174,7 @@ public static void publishMessage(String payload, String urlPath, String message /** Parse arguments and publish messages. */ public static void main(String[] args) throws Exception { HttpExampleOptions options = HttpExampleOptions.fromFlags(args); + if (options == null) { // Could not parse the flags. System.exit(1); @@ -158,6 +195,10 @@ public static void main(String[] args) throws Exception { String urlPath = String.format("%s/%s/", options.httpBridgeAddress, options.apiVersion); System.out.format("Using URL: '%s'\n", urlPath); + // Show the latest configuration + getConfig(urlPath, token, options.projectId, options.cloudRegion, options.registryId, + options.deviceId, "0"); + // Publish numMessages messages to the HTTP bridge. for (int i = 1; i <= options.numMessages; ++i) { String payload = String.format("%s/%s-payload-%d", options.registryId, options.deviceId, i); diff --git a/iot/api-client/http_example/src/main/java/com/google/cloud/iot/examples/HttpExampleOptions.java b/iot/api-client/manager/src/main/java/com/example/cloud/iot/examples/HttpExampleOptions.java similarity index 99% rename from iot/api-client/http_example/src/main/java/com/google/cloud/iot/examples/HttpExampleOptions.java rename to iot/api-client/manager/src/main/java/com/example/cloud/iot/examples/HttpExampleOptions.java index f743c12cd0f..2b4bc48e3c3 100644 --- a/iot/api-client/http_example/src/main/java/com/google/cloud/iot/examples/HttpExampleOptions.java +++ b/iot/api-client/manager/src/main/java/com/example/cloud/iot/examples/HttpExampleOptions.java @@ -12,7 +12,7 @@ * limitations under the License. */ -package com.google.cloud.iot.examples; +package com.example.cloud.iot.examples; import org.apache.commons.cli.CommandLine; import org.apache.commons.cli.CommandLineParser; diff --git a/iot/api-client/manager/src/test/java/com/example/cloud/iot/examples/ManagerIT.java b/iot/api-client/manager/src/test/java/com/example/cloud/iot/examples/ManagerIT.java index f163a2eafba..8aeeff5fdce 100644 --- a/iot/api-client/manager/src/test/java/com/example/cloud/iot/examples/ManagerIT.java +++ b/iot/api-client/manager/src/test/java/com/example/cloud/iot/examples/ManagerIT.java @@ -43,6 +43,7 @@ public class ManagerIT { private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); private static final String REGISTRY_ID = "java-reg-" + (System.currentTimeMillis() / 1000L); private static final String RSA_PATH = "resources/rsa_cert.pem"; + private static final String PKCS_PATH = "resources/rsa_private_pkcs8"; private static final String TOPIC_ID = "java-pst-" + (System.currentTimeMillis() / 1000L); private static Topic topic; @@ -233,4 +234,113 @@ public void testCreateGetRegistry() throws Exception { topicAdminClient.deleteTopic(topic.getNameAsTopicName()); } } + + @Test + public void testHttpDeviceEvent() throws Exception { + final String deviceName = "rsa-device-http-event"; + topic = DeviceRegistryExample.createIotTopic( + PROJECT_ID, + TOPIC_ID); + DeviceRegistryExample.createRegistry(CLOUD_REGION, PROJECT_ID, REGISTRY_ID, TOPIC_ID); + DeviceRegistryExample.createDeviceWithRs256( + deviceName, RSA_PATH, PROJECT_ID, CLOUD_REGION, REGISTRY_ID); + DeviceRegistryExample.listDevices(PROJECT_ID, CLOUD_REGION, REGISTRY_ID); + + // Device bootstrapped, time to connect and run. + String[] testArgs = { + "-project_id=" + PROJECT_ID, + "-registry_id=" + REGISTRY_ID, + "-device_id=" + deviceName, + "-private_key_file=" + PKCS_PATH, + "-message_type=event", + "-algorithm=RS256" + }; + com.example.cloud.iot.examples.HttpExample.main(testArgs); + // End device test. + + // Assertions + String got = bout.toString(); + Assert.assertTrue(got.contains("200")); + Assert.assertTrue(got.contains("OK")); + + // Clean up + DeviceRegistryExample.deleteDevice(deviceName, PROJECT_ID, CLOUD_REGION, REGISTRY_ID); + DeviceRegistryExample.deleteRegistry(CLOUD_REGION, PROJECT_ID, REGISTRY_ID); + try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) { + topicAdminClient.deleteTopic(topic.getNameAsTopicName()); + } + } + + @Test + public void testHttpDeviceState() throws Exception { + final String deviceName = "rsa-device-http-state"; + topic = DeviceRegistryExample.createIotTopic( + PROJECT_ID, + TOPIC_ID); + DeviceRegistryExample.createRegistry(CLOUD_REGION, PROJECT_ID, REGISTRY_ID, TOPIC_ID); + DeviceRegistryExample.createDeviceWithRs256( + deviceName, RSA_PATH, PROJECT_ID, CLOUD_REGION, REGISTRY_ID); + DeviceRegistryExample.listDevices(PROJECT_ID, CLOUD_REGION, REGISTRY_ID); + + // Device bootstrapped, time to connect and run. + String[] testArgs = { + "-project_id=" + PROJECT_ID, + "-registry_id=" + REGISTRY_ID, + "-device_id=" + deviceName, + "-private_key_file=" + PKCS_PATH, + "-message_type=state", + "-algorithm=RS256" + }; + com.example.cloud.iot.examples.HttpExample.main(testArgs); + // End device test. + + // Assertions + String got = bout.toString(); + Assert.assertTrue(got.contains("200")); + Assert.assertTrue(got.contains("OK")); + + // Clean up + DeviceRegistryExample.deleteDevice(deviceName, PROJECT_ID, CLOUD_REGION, REGISTRY_ID); + DeviceRegistryExample.deleteRegistry(CLOUD_REGION, PROJECT_ID, REGISTRY_ID); + try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) { + topicAdminClient.deleteTopic(topic.getNameAsTopicName()); + } + } + + @Test + public void testHttpDeviceConfig() throws Exception { + final String deviceName = "rsa-device-http-state"; + topic = DeviceRegistryExample.createIotTopic( + PROJECT_ID, + TOPIC_ID); + DeviceRegistryExample.createRegistry(CLOUD_REGION, PROJECT_ID, REGISTRY_ID, TOPIC_ID); + DeviceRegistryExample.createDeviceWithRs256( + deviceName, RSA_PATH, PROJECT_ID, CLOUD_REGION, REGISTRY_ID); + DeviceRegistryExample.listDevices(PROJECT_ID, CLOUD_REGION, REGISTRY_ID); + + // Device bootstrapped, time to connect and run. + String[] testArgs = { + "-project_id=" + PROJECT_ID, + "-registry_id=" + REGISTRY_ID, + "-device_id=" + deviceName, + "-private_key_file=" + PKCS_PATH, + "-message_type=event", + "-algorithm=RS256" + }; + com.example.cloud.iot.examples.HttpExample.main(testArgs); + // End device test. + + // Assertions + String got = bout.toString(); + Assert.assertTrue(got.contains("200")); + Assert.assertTrue(got.contains("OK")); + Assert.assertTrue(got.contains("\"binaryData\": \"\"")); + + // Clean up + DeviceRegistryExample.deleteDevice(deviceName, PROJECT_ID, CLOUD_REGION, REGISTRY_ID); + DeviceRegistryExample.deleteRegistry(CLOUD_REGION, PROJECT_ID, REGISTRY_ID); + try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) { + topicAdminClient.deleteTopic(topic.getNameAsTopicName()); + } + } }