|
29 | 29 | import static software.amazon.awssdk.iot.discovery.DiscoveryClient.TLS_EXT_ALPN; |
30 | 30 |
|
31 | 31 | import utils.commandlineutils.CommandLineUtils; |
| 32 | +/* |
| 33 | +
|
| 34 | +# Required Arguments |
| 35 | +required.add_argument("--cert", required=True, metavar="", dest="input_cert", |
| 36 | + help="Path to the certificate file to use during mTLS connection establishment") |
| 37 | +required.add_argument("--key", required=True, metavar="", dest="input_key", |
| 38 | + help="Path to the private key file to use during mTLS connection establishment") |
| 39 | +required.add_argument("--region", required=True, metavar="", dest="input_signing_region", |
| 40 | + help="The region to connect through.") |
| 41 | +required.add_argument("--thing_name", required=True, metavar="", dest="input_thing_name", |
| 42 | + help="The name assigned to your IoT Thing.") |
| 43 | +
|
| 44 | +# Optional Arguments |
| 45 | +optional.add_argument("--ca_file", metavar="", dest="input_ca", |
| 46 | + help="Path to optional CA bundle (PEM)") |
| 47 | +optional.add_argument("--topic", default=f"test/topic/{uuid.uuid4().hex[:8]}", metavar="", dest="input_topic", |
| 48 | + help="Topic") |
| 49 | +
|
| 50 | +optional.add_argument("--print_discover_resp_only", type=bool, default=False, metavar="", dest="input_print_discovery_resp_only", |
| 51 | + help="(optional, default='False').") |
| 52 | +optional.add_argument("--mode", default='both', metavar="", dest="input_mode", |
| 53 | + help=f"The operation mode (optional, default='both').\nModes:{allowed_actions}") |
| 54 | +optional.add_argument("--proxy_host", metavar="", dest="input_proxy_host", |
| 55 | + help="HTTP proxy host") |
| 56 | +optional.add_argument("--proxy_port", type=int, default=0, metavar="", dest="input_proxy_port", |
| 57 | + help="HTTP proxy port") |
| 58 | +optional.add_argument("--client_id", metavar="", dest="input_clientId", default=f"mqtt5-sample-{uuid.uuid4().hex[:8]}", |
| 59 | + help="Client ID") |
| 60 | +
|
| 61 | +
|
| 62 | +optional.add_argument("--message", default="Hello World!", metavar="", dest="input_message", |
| 63 | + help="Message payload") |
| 64 | +optional.add_argument("--max_pub_ops", type=int, default=10, metavar="", dest="input_max_pub_ops", |
| 65 | + help="The maximum number of publish operations (optional, default='10').") |
| 66 | +
|
| 67 | +
|
| 68 | +input_thingName = cmdData.input_thingName; |
| 69 | + input_certPath = cmdData.input_cert; |
| 70 | + input_keyPath = cmdData.input_key; |
| 71 | +if (cmdData.input_ca != null) { |
| 72 | + tlsCtxOptions.overrideDefaultTrustStoreFromPath(null, cmdData.input_ca); |
| 73 | +
|
| 74 | +
|
| 75 | +(cmdData.input_proxyHost != null && cmdData.input_proxyPort > 0) { |
| 76 | + cmdData.input_signingRegion |
| 77 | +
|
| 78 | + cmdData.inputPrintDiscoverRespOnly |
| 79 | + cmdData.input_mode |
| 80 | + cmdData.input_topic |
| 81 | + */ |
| 82 | + // ------------------------- ARGUMENT PARSING ------------------------- |
| 83 | + static class Args { |
| 84 | + String certPath; |
| 85 | + String keyPath; |
| 86 | + String region; |
| 87 | + String thingName; |
| 88 | + Boolean printDiscoveryRespOnly = false; |
| 89 | + String mode; |
| 90 | + String proxyHost; |
| 91 | + Boolean isProxyPortSet = false; |
| 92 | + int proxyPort; |
| 93 | + String clientId = "mqtt5-sample-" + UUID.randomUUID().toString().replace("-", "").substring(0, 8); |
| 94 | + String topic = "test/topic"; |
| 95 | + } |
| 96 | + |
| 97 | + private static void printHelpAndExit(int code) { |
| 98 | + System.out.println("MQTT5 X509 Sample (mTLS)\n"); |
| 99 | + System.out.println("Required:"); |
| 100 | + System.out.println(" --cert <CERTIFICATE> Path to certificate file (PEM)"); |
| 101 | + System.out.println(" --key <PRIVATE_KEY> Path to private key file (PEM)"); |
| 102 | + System.out.println(" --region <REGION> The region to connect through"); |
| 103 | + System.out.println(" --thing_name <THING_NAME> The name assigned to your IoT Thing"); |
| 104 | + System.out.println("\nOptional:"); |
| 105 | + System.out.println(" --print_discover_resp_only <PRINT_DISC_RESPONSE> (optional, default='False')"); |
| 106 | + System.out.println(" --mode <MODE> The operation mode (optional, default='both').\nModes:{allowed_actions}"); |
| 107 | + System.out.println(" --proxy_host <PROXY_HOST> HTTP proxy host"); |
| 108 | + System.out.println(" --proxy_port <PROXY_PORT> HTTP proxy port"); |
| 109 | + System.out.println(" --client_id <CLIENT_ID> MQTT client ID (default: generated)"); |
| 110 | + System.out.println(" --topic <TOPIC> Topic to use (default: test/topic)"); |
| 111 | + System.exit(code); |
| 112 | + } |
| 113 | + |
| 114 | + private static Args parseArgs(String[] argv) { |
| 115 | + if (argv.length == 0 || Arrays.asList(argv).contains("--help")) { |
| 116 | + printHelpAndExit(0); |
| 117 | + } |
| 118 | + Args a = new Args(); |
| 119 | + for (int i = 0; i < argv.length; i++) { |
| 120 | + String k = argv[i]; |
| 121 | + String v = (i + 1 < argv.length) ? argv[i + 1] : null; |
| 122 | + |
| 123 | + switch (k) { |
| 124 | + case "--endpoint": a.endpoint = v; i++; break; |
| 125 | + case "--cert": a.certPath = v; i++; break; |
| 126 | + case "--key": a.keyPath = v; i++; break; |
| 127 | + case "--client_id": a.clientId = v; i++; break; |
| 128 | + case "--topic": a.topic = v; i++; break; |
| 129 | + case "--message": a.message = v; i++; break; |
| 130 | + case "--count": |
| 131 | + a.count = Integer.parseInt(v); i++; break; |
| 132 | + default: |
| 133 | + System.err.println("Unknown arg: " + k); |
| 134 | + printHelpAndExit(2); |
| 135 | + } |
| 136 | + } |
| 137 | + if (a.endpoint == null || a.certPath == null || a.keyPath == null) { |
| 138 | + System.err.println("Missing required arguments."); |
| 139 | + printHelpAndExit(2); |
| 140 | + } |
| 141 | + return a; |
| 142 | + } |
| 143 | + // ------------------------- ARGUMENT PARSING END --------------------- |
32 | 144 |
|
33 | 145 | public class BasicDiscovery { |
34 | 146 |
|
|
0 commit comments