Skip to content

Commit bc1ffac

Browse files
authored
Merge pull request #677 from GoogleCloudPlatform/cloud-iot-core
Changes script to use Java for IAM.
2 parents 8ebf743 + f87f620 commit bc1ffac

File tree

9 files changed

+199
-216
lines changed

9 files changed

+199
-216
lines changed

iot/api-client/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Google Cloud IoT Core platform.
44

55
## Quickstart
66

7-
1. Install the gCloud CLI as described in [the device manager guide](https://cloud-dot-devsite.googleplex.com/iot/docs/device_manager_guide).
7+
1. Install the gCloud CLI as described in [the device manager guide](https://cloud.google.com/iot/docs/device_manager_guide).
88
2. Create a PubSub topic:
99

1010
gcloud beta pubsub topics create projects/my-iot-project/topics/device-events

iot/api-client/mqtt_example/src/main/java/com/google/cloud/iot/examples/MqttExample.java

+33-20
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,26 @@
3939
* </pre>
4040
*/
4141
public class MqttExample {
42-
/** Load a PKCS8 encoded keyfile from the given path. */
43-
private static PrivateKey loadKeyFile(String filename, String algorithm) throws Exception {
44-
byte[] keyBytes = Files.readAllBytes(Paths.get(filename));
42+
/** Create a Cloud IoT Core JWT for the given project id, signed with the given private key. */
43+
private static String createJwtRsa(String projectId, String privateKeyFile) throws Exception {
44+
DateTime now = new DateTime();
45+
// Create a JWT to authenticate this device. The device will be disconnected after the token
46+
// expires, and will have to reconnect with a new token. The audience field should always be set
47+
// to the GCP project id.
48+
JwtBuilder jwtBuilder =
49+
Jwts.builder()
50+
.setIssuedAt(now.toDate())
51+
.setExpiration(now.plusMinutes(20).toDate())
52+
.setAudience(projectId);
53+
54+
byte[] keyBytes = Files.readAllBytes(Paths.get(privateKeyFile));
4555
PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(keyBytes);
46-
KeyFactory kf = KeyFactory.getInstance(algorithm);
47-
return kf.generatePrivate(spec);
56+
KeyFactory kf = KeyFactory.getInstance("RSA256");
57+
58+
return jwtBuilder.signWith(SignatureAlgorithm.RS256, kf.generatePrivate(spec)).compact();
4859
}
4960

50-
/** Create a Cloud IoT Core JWT for the given project id, signed with the given private key. */
51-
private static String createJwt(String projectId, String privateKeyFile, String algorithm)
52-
throws Exception {
61+
private static String createJwtEs(String projectId, String privateKeyFile) throws Exception {
5362
DateTime now = new DateTime();
5463
// Create a JWT to authenticate this device. The device will be disconnected after the token
5564
// expires, and will have to reconnect with a new token. The audience field should always be set
@@ -60,16 +69,11 @@ private static String createJwt(String projectId, String privateKeyFile, String
6069
.setExpiration(now.plusMinutes(20).toDate())
6170
.setAudience(projectId);
6271

63-
if (algorithm.equals("RS256")) {
64-
PrivateKey privateKey = loadKeyFile(privateKeyFile, "RSA");
65-
return jwtBuilder.signWith(SignatureAlgorithm.RS256, privateKey).compact();
66-
} else if (algorithm.equals("ES256")) {
67-
PrivateKey privateKey = loadKeyFile(privateKeyFile, "EC");
68-
return jwtBuilder.signWith(SignatureAlgorithm.ES256, privateKey).compact();
69-
} else {
70-
throw new IllegalArgumentException(
71-
"Invalid algorithm " + algorithm + ". Should be one of 'RS256' or 'ES256'.");
72-
}
72+
byte[] keyBytes = Files.readAllBytes(Paths.get(privateKeyFile));
73+
PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(keyBytes);
74+
KeyFactory kf = KeyFactory.getInstance("ES256");
75+
76+
return jwtBuilder.signWith(SignatureAlgorithm.ES256, kf.generatePrivate(spec)).compact();
7377
}
7478

7579
public static void main(String[] args) throws Exception {
@@ -102,8 +106,17 @@ public static void main(String[] args) throws Exception {
102106
// Paho client library to send the password field. The password field is used to transmit a JWT
103107
// to authorize the device.
104108
connectOptions.setUserName("unused");
105-
connectOptions.setPassword(
106-
createJwt(options.projectId, options.privateKeyFile, options.algorithm).toCharArray());
109+
110+
if (options.algorithm == "RSA256") {
111+
connectOptions.setPassword(
112+
createJwtRsa(options.projectId, options.privateKeyFile).toCharArray());
113+
} else if (options.algorithm == "ES256") {
114+
connectOptions.setPassword(
115+
createJwtEs(options.projectId, options.privateKeyFile).toCharArray());
116+
} else {
117+
throw new IllegalArgumentException(
118+
"Invalid algorithm " + options.algorithm + ". Should be one of 'RS256' or 'ES256'.");
119+
}
107120

108121
// Create a client, and connect to the Google MQTT bridge.
109122
MqttClient client = new MqttClient(mqttServerAddress, mqttClientId, new MemoryPersistence());

iot/api-client/scripts/README.md

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Getting Started with Cloud Pub/Sub and the Google Cloud Client libraries
2+
3+
[Google Cloud IoT Core](https://cloud.google.com/iot-core/)
4+
is a fully-managed, globally distributed solution for managing devices and
5+
sending / receiving messages from devices.
6+
7+
This script manages the [Google Cloud Pub/Sub][pubsub] project associated with
8+
your Google Cloud IoT Core project to grant permissions to the protocol bridge.
9+
10+
Create your PubSub topic noting the project ID and topic ID, then build and run
11+
the sample to configure your topic.
12+
13+
[pubsub]: https://cloud.google.com/pubsub/
14+
15+
#### Setup
16+
17+
* Install [Maven](http://maven.apache.org/)
18+
* Build your project with:
19+
20+
mvn clean compile assembly:single
21+
22+
#### Running the script
23+
24+
The following code will run the helper script:
25+
26+
java -cp target/pubsub-policy-helper-1.0.0-jar-with-dependencies.jar \
27+
com.example.pubsub.AddCloudIotService <topicName> <projectId>
28+
29+
For example, the following example will configure the `device-events` topic
30+
for the `my-iot-project` project.
31+
32+
java -cp target/pubsub-policy-helper-1.0.0-jar-with-dependencies.jar \
33+
com.example.pubsub.AddCloudIotService device-events my-iot-project

iot/api-client/scripts/README.rst

-115
This file was deleted.

iot/api-client/scripts/README.rst.in

-22
This file was deleted.

iot/api-client/scripts/iam.py

-57
This file was deleted.

iot/api-client/scripts/pom.xml

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<!--
2+
Copyright 2017 Google Inc.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
-->
16+
<project>
17+
<modelVersion>4.0.0</modelVersion>
18+
<groupId>com.example.pubsub</groupId>
19+
<artifactId>pubsub-policy-helper</artifactId>
20+
<packaging>jar</packaging>
21+
22+
<!-- Parent defines config for testing & linting. -->
23+
<parent>
24+
<artifactId>doc-samples</artifactId>
25+
<groupId>com.google.cloud</groupId>
26+
<version>1.0.0</version>
27+
<relativePath>../../..</relativePath>
28+
</parent>
29+
30+
<properties>
31+
<maven.compiler.target>1.8</maven.compiler.target>
32+
<maven.compiler.source>1.8</maven.compiler.source>
33+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
34+
<pubsub.version>0.17.2-alpha</pubsub.version>
35+
</properties>
36+
37+
<build>
38+
<plugins>
39+
<plugin>
40+
<artifactId>maven-assembly-plugin</artifactId>
41+
<configuration>
42+
<archive>
43+
<manifest>
44+
<mainClass>com.example.pubsub.AddCloudIotService</mainClass>
45+
</manifest>
46+
</archive>
47+
<descriptorRefs>
48+
<descriptorRef>jar-with-dependencies</descriptorRef>
49+
</descriptorRefs>
50+
</configuration>
51+
</plugin>
52+
</plugins>
53+
</build>
54+
55+
<dependencies>
56+
<dependency>
57+
<groupId>com.google.cloud</groupId>
58+
<artifactId>google-cloud-pubsub</artifactId>
59+
<version>${pubsub.version}</version>
60+
</dependency>
61+
62+
<!-- Test dependencies -->
63+
<dependency>
64+
<groupId>junit</groupId>
65+
<artifactId>junit</artifactId>
66+
<version>4.12</version>
67+
<scope>test</scope>
68+
</dependency>
69+
<dependency>
70+
<groupId>com.google.truth</groupId>
71+
<artifactId>truth</artifactId>
72+
<version>0.32</version>
73+
<scope>test</scope>
74+
</dependency>
75+
</dependencies>
76+
</project>

iot/api-client/scripts/requirements.txt

-1
This file was deleted.

0 commit comments

Comments
 (0)