|
16 | 16 |
|
17 | 17 | import software.amazon.awssdk.crt.CRT; |
18 | 18 | import software.amazon.awssdk.crt.CrtRuntimeException; |
| 19 | +import software.amazon.awssdk.crt.auth.credentials.X509CredentialsProvider; |
19 | 20 | import software.amazon.awssdk.crt.http.HttpProxyOptions; |
20 | 21 | import software.amazon.awssdk.crt.io.ClientBootstrap; |
| 22 | +import software.amazon.awssdk.crt.io.ClientTlsContext; |
21 | 23 | import software.amazon.awssdk.crt.io.EventLoopGroup; |
22 | 24 | import software.amazon.awssdk.crt.io.HostResolver; |
| 25 | +import software.amazon.awssdk.crt.io.TlsContextOptions; |
23 | 26 | import software.amazon.awssdk.crt.mqtt.MqttClientConnection; |
24 | 27 | import software.amazon.awssdk.crt.mqtt.MqttClientConnectionEvents; |
25 | 28 | import software.amazon.awssdk.crt.mqtt.MqttMessage; |
@@ -47,24 +50,38 @@ class PubSub { |
47 | 50 | static int proxyPort; |
48 | 51 | static String region = "us-east-1"; |
49 | 52 | 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; |
50 | 60 |
|
51 | 61 | static void printUsage() { |
52 | 62 | System.out.println( |
53 | 63 | "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" |
68 | 85 | ); |
69 | 86 | } |
70 | 87 |
|
@@ -130,6 +147,40 @@ static void parseCommandLine(String[] args) { |
130 | 147 | case "-w": |
131 | 148 | useWebsockets = true; |
132 | 149 | 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; |
133 | 184 | case "--proxyhost": |
134 | 185 | if (idx + 1 < args.length) { |
135 | 186 | proxyHost = args[++idx]; |
@@ -167,6 +218,11 @@ public static void main(String[] args) { |
167 | 218 | printUsage(); |
168 | 219 | return; |
169 | 220 | } |
| 221 | + } else if (useX509Credentials) { |
| 222 | + if (x509RoleAlias == null || x509Endpoint == null || x509Thing == null || x509CertPath == null || x509KeyPath == null) { |
| 223 | + printUsage(); |
| 224 | + return; |
| 225 | + } |
170 | 226 | } |
171 | 227 |
|
172 | 228 | MqttClientConnectionEvents callbacks = new MqttClientConnectionEvents() { |
@@ -209,6 +265,26 @@ public void onConnectionResumed(boolean sessionPresent) { |
209 | 265 |
|
210 | 266 | builder.withWebsocketProxyOptions(proxyOptions); |
211 | 267 | } |
| 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 | + } |
212 | 288 | } |
213 | 289 |
|
214 | 290 | try(MqttClientConnection connection = builder.build()) { |
|
0 commit comments