Skip to content

Commit ea263af

Browse files
authored
X509 provider support in PubSub (#59)
* PubSub x509 support * Bump crt version
1 parent edc02c7 commit ea263af

File tree

2 files changed

+91
-15
lines changed

2 files changed

+91
-15
lines changed

samples/BasicPubSub/src/main/java/pubsub/PubSub.java

Lines changed: 90 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,13 @@
1616

1717
import software.amazon.awssdk.crt.CRT;
1818
import software.amazon.awssdk.crt.CrtRuntimeException;
19+
import software.amazon.awssdk.crt.auth.credentials.X509CredentialsProvider;
1920
import software.amazon.awssdk.crt.http.HttpProxyOptions;
2021
import software.amazon.awssdk.crt.io.ClientBootstrap;
22+
import software.amazon.awssdk.crt.io.ClientTlsContext;
2123
import software.amazon.awssdk.crt.io.EventLoopGroup;
2224
import software.amazon.awssdk.crt.io.HostResolver;
25+
import software.amazon.awssdk.crt.io.TlsContextOptions;
2326
import software.amazon.awssdk.crt.mqtt.MqttClientConnection;
2427
import software.amazon.awssdk.crt.mqtt.MqttClientConnectionEvents;
2528
import software.amazon.awssdk.crt.mqtt.MqttMessage;
@@ -47,24 +50,38 @@ class PubSub {
4750
static int proxyPort;
4851
static String region = "us-east-1";
4952
static boolean useWebsockets = false;
53+
static boolean useX509Credentials = false;
54+
static String x509RoleAlias;
55+
static String x509Endpoint;
56+
static String x509Thing;
57+
static String x509CertPath;
58+
static String x509KeyPath;
59+
static String x509RootCaPath;
5060

5161
static void printUsage() {
5262
System.out.println(
5363
"Usage:\n"+
54-
" --help This message\n"+
55-
" --clientId Client ID to use when connecting (optional)\n"+
56-
" -e|--endpoint AWS IoT service endpoint hostname\n"+
57-
" -p|--port Port to connect to on the endpoint\n"+
58-
" -r|--rootca Path to the root certificate\n"+
59-
" -c|--cert Path to the IoT thing certificate\n"+
60-
" -k|--key Path to the IoT thing private key\n"+
61-
" -t|--topic Topic to subscribe/publish to (optional)\n"+
62-
" -m|--message Message to publish (optional)\n"+
63-
" -n|--count Number of messages to publish (optional)\n" +
64-
" -w|--websockets Use websockets\n" +
65-
" --proxyhost Websocket proxy host to use\n" +
66-
" --proxyport Websocket proxy port to use\n" +
67-
" --region Websocket signing region to use\n"
64+
" --help This message\n"+
65+
" --clientId Client ID to use when connecting (optional)\n"+
66+
" -e|--endpoint AWS IoT service endpoint hostname\n"+
67+
" -p|--port Port to connect to on the endpoint\n"+
68+
" -r|--rootca Path to the root certificate\n"+
69+
" -c|--cert Path to the IoT thing certificate\n"+
70+
" -k|--key Path to the IoT thing private key\n"+
71+
" -t|--topic Topic to subscribe/publish to (optional)\n"+
72+
" -m|--message Message to publish (optional)\n"+
73+
" -n|--count Number of messages to publish (optional)\n" +
74+
" -w|--websockets Use websockets\n" +
75+
" --proxyhost Websocket proxy host to use\n" +
76+
" --proxyport Websocket proxy port to use\n" +
77+
" --region Websocket signing region to use\n" +
78+
" --x509 Use the x509 credentials provider while using websockets\n" +
79+
" --x509rolealias Role alias to use with the x509 credentials provider\n" +
80+
" --x509endpoint Endpoint to fetch x509 credentials from\n" +
81+
" --x509thing Thing name to fetch x509 credentials on behalf of\n" +
82+
" --x509cert Path to the IoT thing certificate used in fetching x509 credentials\n" +
83+
" --x509key Path to the IoT thing private key used in fetching x509 credentials\n" +
84+
" --x509rootca Path to the root certificate used in fetching x509 credentials\n"
6885
);
6986
}
7087

@@ -130,6 +147,40 @@ static void parseCommandLine(String[] args) {
130147
case "-w":
131148
useWebsockets = true;
132149
break;
150+
case "--x509":
151+
useX509Credentials = true;
152+
useWebsockets = true;
153+
break;
154+
case "--x509rolealias":
155+
if (idx + 1 < args.length) {
156+
x509RoleAlias = args[++idx];
157+
}
158+
break;
159+
case "--x509endpoint":
160+
if (idx + 1 < args.length) {
161+
x509Endpoint = args[++idx];
162+
}
163+
break;
164+
case "--x509thing":
165+
if (idx + 1 < args.length) {
166+
x509Thing = args[++idx];
167+
}
168+
break;
169+
case "--x509cert":
170+
if (idx + 1 < args.length) {
171+
x509CertPath = args[++idx];
172+
}
173+
break;
174+
case "--x509key":
175+
if (idx + 1 < args.length) {
176+
x509KeyPath = args[++idx];
177+
}
178+
break;
179+
case "--x509rootca":
180+
if (idx + 1 < args.length) {
181+
x509RootCaPath = args[++idx];
182+
}
183+
break;
133184
case "--proxyhost":
134185
if (idx + 1 < args.length) {
135186
proxyHost = args[++idx];
@@ -167,6 +218,11 @@ public static void main(String[] args) {
167218
printUsage();
168219
return;
169220
}
221+
} else if (useX509Credentials) {
222+
if (x509RoleAlias == null || x509Endpoint == null || x509Thing == null || x509CertPath == null || x509KeyPath == null) {
223+
printUsage();
224+
return;
225+
}
170226
}
171227

172228
MqttClientConnectionEvents callbacks = new MqttClientConnectionEvents() {
@@ -209,6 +265,26 @@ public void onConnectionResumed(boolean sessionPresent) {
209265

210266
builder.withWebsocketProxyOptions(proxyOptions);
211267
}
268+
269+
if (useX509Credentials) {
270+
try (TlsContextOptions x509TlsOptions = TlsContextOptions.createWithMtlsFromPath(x509CertPath, x509KeyPath)) {
271+
if (x509RootCaPath != null) {
272+
x509TlsOptions.withCertificateAuthorityFromPath(null, x509RootCaPath);
273+
}
274+
275+
try (ClientTlsContext x509TlsContext = new ClientTlsContext(x509TlsOptions)) {
276+
X509CredentialsProvider.X509CredentialsProviderBuilder x509builder = new X509CredentialsProvider.X509CredentialsProviderBuilder()
277+
.withClientBootstrap(clientBootstrap)
278+
.withTlsContext(x509TlsContext)
279+
.withEndpoint(x509Endpoint)
280+
.withRoleAlias(x509RoleAlias)
281+
.withThingName(x509Thing);
282+
try (X509CredentialsProvider provider = x509builder.build()) {
283+
builder.withWebsocketCredentialsProvider(provider);
284+
}
285+
}
286+
}
287+
}
212288
}
213289

214290
try(MqttClientConnection connection = builder.build()) {

sdk/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
<dependency>
4242
<groupId>software.amazon.awssdk.crt</groupId>
4343
<artifactId>aws-crt</artifactId>
44-
<version>0.5.5</version>
44+
<version>0.5.6</version>
4545
<scope>compile</scope>
4646
</dependency>
4747
<dependency>

0 commit comments

Comments
 (0)