-
Notifications
You must be signed in to change notification settings - Fork 221
PKCS12 connect sample #445
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
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
{ | ||
"language": "Python", | ||
"sample_file": "./aws-iot-device-sdk-python-v2/samples/pkcs12_connect.py", | ||
"sample_region": "us-east-1", | ||
"sample_main_class": "", | ||
"arguments": [ | ||
{ | ||
"name": "--endpoint", | ||
"secret": "ci/endpoint" | ||
}, | ||
{ | ||
"name": "--pkcs12_file", | ||
"data": "./pkcs12-key.p12" | ||
}, | ||
{ | ||
"name": "--pkcs12_password", | ||
"secret": "ci/PubSub/key_pkcs12_password" | ||
} | ||
] | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
# PKCS12 Connect | ||
|
||
[**Return to main sample list**](../README.md) | ||
|
||
This sample is similar to the [Basic Connect](../BasicConnect/README.md) sample, in that it connects via Mutual TLS (mTLS) using a certificate and key file. However, unlike the Basic Connect where the certificate and private key file are stored on disk, this sample uses a PKCS#12 file instead. | ||
|
||
**WARNING: MacOS only**. Currently, TLS integration with PKCS12 is only available on MacOS devices. | ||
|
||
Your IoT Core Thing's [Policy](https://docs.aws.amazon.com/iot/latest/developerguide/iot-policies.html) must provide privileges for this sample to connect. Below is a sample policy that can be used on your IoT Core Thing that will allow this sample to run as intended. | ||
|
||
<details> | ||
<summary>(see sample policy)</summary> | ||
<pre> | ||
{ | ||
"Version": "2012-10-17", | ||
"Statement": [ | ||
{ | ||
"Effect": "Allow", | ||
"Action": [ | ||
"iot:Connect" | ||
], | ||
"Resource": [ | ||
"arn:aws:iot:<b>region</b>:<b>account</b>:client/test-*" | ||
] | ||
} | ||
] | ||
} | ||
</pre> | ||
|
||
Replace with the following with the data from your AWS account: | ||
* `<region>`: The AWS IoT Core region where you created your AWS IoT Core thing you wish to use with this sample. For example `us-east-1`. | ||
* `<account>`: Your AWS IoT Core account ID. This is the set of numbers in the top right next to your AWS account name when using the AWS IoT Core website. | ||
|
||
Note that in a real application, you may want to avoid the use of wildcards in your ClientID or use them selectively. Please follow best practices when working with AWS on production applications using the SDK. Also, for the purposes of this sample, please make sure your policy allows a client ID of `test-*` to connect or use `--client_id <client ID here>` to send the client ID your policy supports. | ||
|
||
</details> | ||
|
||
## How to run | ||
|
||
To run the PKCS12 connect use the following command: | ||
|
||
```sh | ||
python3 pkcs12_connect --endpoint <endpoint> --pkcs12_file <path to PKCS12 file> --pkcs12_password <password for PKCS12 file> | ||
``` | ||
|
||
You can also pass a Certificate Authority file (CA) if your certificate and key combination requires it: | ||
|
||
```sh | ||
python3 pkcs12_connect --endpoint <endpoint> --pkcs12_file <path to PKCS12 file> --pkcs12_password <password for PKCS12 file> --ca_file <path to CA file> | ||
``` | ||
|
||
### How to setup and run | ||
|
||
To use the certificate and key files provided by AWS IoT Core, you will need to convert them into PKCS#12 format and then import them into your Java keystore. You can convert the certificate and key file to PKCS12 using the following command: | ||
|
||
```sh | ||
openssl pkcs12 -export -in <my-certificate.pem.crt> -inkey <my-private-key.pem.key> -out <my-pkcs12-key.pem.key> -name <alias here> -password pass:<password here> | ||
``` | ||
|
||
Once converted, you can then run the PKCS12 connect sample with the following: | ||
|
||
```sh | ||
python3 pkcs12_connect --endpoint <endpoint> --pkcs12_file <my-pkcs12-key.pem.key> --pkcs12_password <password here> | ||
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
# SPDX-License-Identifier: Apache-2.0. | ||
|
||
from awscrt import http, io | ||
from awsiot import mqtt_connection_builder | ||
from utils.command_line_utils import CommandLineUtils | ||
|
||
# This sample shows how to create a MQTT connection using a certificate file and key file. | ||
# This sample is intended to be used as a reference for making MQTT connections. | ||
|
||
# Callback when connection is accidentally lost. | ||
def on_connection_interrupted(connection, error, **kwargs): | ||
print("Connection interrupted. error: {}".format(error)) | ||
|
||
# Callback when an interrupted connection is re-established. | ||
def on_connection_resumed(connection, return_code, session_present, **kwargs): | ||
print("Connection resumed. return_code: {} session_present: {}".format(return_code, session_present)) | ||
|
||
|
||
if __name__ == '__main__': | ||
|
||
io.init_logging(log_level=io.LogLevel.Trace, file_name="stderr") | ||
|
||
# cmdData is the arguments/input from the command line placed into a single struct for | ||
# use in this sample. This handles all of the command line parsing, validating, etc. | ||
# See the Utils/CommandLineUtils for more information. | ||
cmdData = CommandLineUtils.parse_sample_input_pkcs12_connect() | ||
|
||
# Create the proxy options if the data is present in cmdData | ||
proxy_options = None | ||
if cmdData.input_proxy_host is not None and cmdData.input_proxy_port != 0: | ||
proxy_options = http.HttpProxyOptions( | ||
host_name=cmdData.input_proxy_host, | ||
port=cmdData.input_proxy_port) | ||
|
||
# Create a MQTT connection from the command line data | ||
mqtt_connection = mqtt_connection_builder.mtls_with_pkcs12( | ||
endpoint=cmdData.input_endpoint, | ||
port=cmdData.input_port, | ||
pkcs12_filepath=cmdData.input_pkcs12_file, | ||
pkcs12_password=cmdData.input_pkcs12_password, | ||
on_connection_interrupted=on_connection_interrupted, | ||
on_connection_resumed=on_connection_resumed, | ||
client_id=cmdData.input_clientId, | ||
clean_session=False, | ||
keep_alive_secs=30, | ||
http_proxy_options=proxy_options) | ||
|
||
if not cmdData.input_is_ci: | ||
print(f"Connecting to {cmdData.input_endpoint} with client ID '{cmdData.input_clientId}'...") | ||
else: | ||
print("Connecting to endpoint with client ID") | ||
|
||
connect_future = mqtt_connection.connect() | ||
# Future.result() waits until a result is available | ||
connect_future.result() | ||
print("Connected!") | ||
|
||
# Disconnect | ||
print("Disconnecting...") | ||
disconnect_future = mqtt_connection.disconnect() | ||
disconnect_future.result() | ||
print("Disconnected!") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.