Skip to content

Adds tests for HTTP device sample. #954

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Dec 19, 2017
75 changes: 0 additions & 75 deletions iot/api-client/http_example/README.md

This file was deleted.

62 changes: 0 additions & 62 deletions iot/api-client/http_example/pom.xml

This file was deleted.

77 changes: 77 additions & 0 deletions iot/api-client/manager/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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" \
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems long winded, but Ok

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, for the Manager sample, there are a few things that can are put in environment variables but for the device samples it would pretty much just be the project ID, which doesn't help much with the command length.

-Dexec.args="-project_id=<your-iot-project> \
-registry_id=<your-registry-id> \
-device_id=<device-id> \
-private_key_file=<path-to-keyfile> \
-message_type=<event|state> \
-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
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
```
15 changes: 15 additions & 0 deletions iot/api-client/manager/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,21 @@
</properties>

<dependencies>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20090211</version>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt</artifactId>
<version>0.7.0</version>
</dependency>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.1</version>
</dependency>
<dependency>
<groupId>com.google.apis</groupId>
<artifactId>google-api-services-cloudiot</artifactId>
Expand Down
5 changes: 5 additions & 0 deletions iot/api-client/manager/resources/README.md
Original file line number Diff line number Diff line change
@@ -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.
4 changes: 2 additions & 2 deletions iot/api-client/manager/resources/ec_public.pem
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
-----BEGIN PUBLIC KEY-----
MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAECfwA4OrF9Pcr1W5mXUa+Dx8hpPYd
+pQ5153zNtSSaeEnA/4hrY2AKxUHmKIPJXYRkZrxTxsFElkkpLcoi/CUNQ==
MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEJ/zj75PhGtoayWD9pm/IbXRXi9ai
RQDMFQ87sOOQIWDTxnsHFplqb8cDCtO1h5QsutcAVTPZL0CZiqIg5j7b1w==
-----END PUBLIC KEY-----
32 changes: 16 additions & 16 deletions iot/api-client/manager/resources/rsa_cert.pem
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
-----BEGIN CERTIFICATE-----
MIIC+DCCAeCgAwIBAgIJAJW4zZX4mjtpMA0GCSqGSIb3DQEBCwUAMBExDzANBgNV
BAMMBnVudXNlZDAeFw0xNzA5MjUyMjM3MzhaFw0xNzEwMjUyMjM3MzhaMBExDzAN
BgNVBAMMBnVudXNlZDCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAKaB
voBJf6cr+Q+S1l/n5Bp0i7BEYeeCnUta+MIZle38y0E3TzXQvjMr8uk081RcWqFq
wkRjY+OM7hLIMbs3C+Qvg6uxbjaJM0LE+gjwnU8Wg76Y4jjhl+tPYP9njWxRZF0d
7WGRMpaztKpukEpgUszC75YM9XVQCal6m3eegu5BraiXrAjngGOAninBe56jhw/b
HIqF85PXczI9BbUoJeq4VycRsdUa2dJSqMxKoF7T2blYLiLBFTyo72ZF6m8SAIzv
eMw78pgtwJK4ZIzONrSe2PaPtctyRmFQBGnnZaMGi3ToiYgQ/pQ4LkuPN1sCCv7y
n3ljavj+QM+IiM5DvZMCAwEAAaNTMFEwHQYDVR0OBBYEFFK2kAhd7rYZInQxdk8T
MZrXXBCrMB8GA1UdIwQYMBaAFFK2kAhd7rYZInQxdk8TMZrXXBCrMA8GA1UdEwEB
/wQFMAMBAf8wDQYJKoZIhvcNAQELBQADggEBAKCB1nQ2rFsnQpRRtvjDqlI03Opc
8sfHQ4GxJ3L7ZuXx6MzH37k0+g5/dgGYhRM+Zm9fDnxD6a1c5fek+0iGGIHOg1Cw
9lwqZN15w7SXxMiwxDVYoMBvx7JQEZSFeMfP3ZcKdwSaFRYXNRtbeC45VS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GhIrbg2HNIdhXkKxWtqc5bmcmH
NI65zGFn96gPGwYiKM8zb/muJDu6oOsmtwLuaHdxiNy18bzG7dlA9/FNyMSTCPLr
KxTNeE+hz+PEl9C0TCuFMh++0xU/raZdGhEugdg3vAB37lqzfsqBrO1LLms=
-----END CERTIFICATE-----
28 changes: 28 additions & 0 deletions iot/api-client/manager/resources/rsa_private.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
-----BEGIN PRIVATE KEY-----
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you really mean to publish this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it's used after the device is registered to connect and test the "http device" samples. The authorization credential is revoked during tear-down of the tests.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At any rate, this file is needed to connect and providing a dummy / static one is probably simpler than generating them with code or shelling out to openssl in the tests.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sg - It won't surprise me for this to trigger some scanners, in which case, I'll ask you to encrypt a new one and we can decrypt when we test.

MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDdWhkFaxeyeXKe
2Sa2pfuzIGH5jmiuDq+bETmVRSMRuNL3o1hyxMAH+Pch1h8yXacWLqrwg4gSnLfo
SRIVCbTyjB8miabCZ0IhLHG4Bs+3Yz2Bf4sNiNQU3Z68mkUBMPQxg8tlYJR/xK9w
nQv14jF0lGqqBO3I9Pu6AHN4PpBAnos7PIM8B2mLDFkltvoZx0/Sa9yImk01RlO0
QGhkNS60AXdD8PQZaE94b5L5A1v7PqVUmyqQXQHKO4BaniNOc9zMXg7EklYWSpOR
VK9ExYV7IAo/3G8p41cHim3QuKbxAAEEt0Cd7zm1v9W0PQLSDUcCaOd7DWO/HwGF
F29z82NZAgMBAAECggEBAKMHcfhQqRJ1apteWqoZ1ClZR2/z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