AWS code-library documentation change
Summary
Added Python code example for creating keys and certificates using AWS IoT
Security assessment
The change adds documentation about certificate creation which is a security feature, but doesn't address any specific vulnerability. Certificates are fundamental for device authentication in IoT security.
Diff
diff --git a/code-library/latest/ug/iot_example_iot_CreateKeysAndCertificate_section.md b/code-library/latest/ug/iot_example_iot_CreateKeysAndCertificate_section.md index e31ca5049..994dd1c38 100644 --- a//code-library/latest/ug/iot_example_iot_CreateKeysAndCertificate_section.md +++ b//code-library/latest/ug/iot_example_iot_CreateKeysAndCertificate_section.md @@ -289,0 +290,58 @@ There's more on GitHub. Find the complete example and learn how to set up and ru +Python + + +**SDK for Python (Boto3)** + + +###### Note + +There's more on GitHub. Find the complete example and learn how to set up and run in the [AWS Code Examples Repository](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/python/example_code/iot#code-examples). + + + class IoTWrapper: + """Encapsulates AWS IoT actions.""" + + def __init__(self, iot_client, iot_data_client=None): + """ + :param iot_client: A Boto3 AWS IoT client. + :param iot_data_client: A Boto3 AWS IoT Data Plane client. + """ + self.iot_client = iot_client + self.iot_data_client = iot_data_client + + @classmethod + def from_client(cls): + iot_client = boto3.client("iot") + iot_data_client = boto3.client("iot-data") + return cls(iot_client, iot_data_client) + + def create_keys_and_certificate(self): + """ + Creates keys and a certificate for an AWS IoT thing. + + :return: The certificate ID, ARN, and PEM. + """ + try: + response = self.iot_client.create_keys_and_certificate(setAsActive=True) + logger.info("Created certificate %s.", response["certificateId"]) + except ClientError as err: + if err.response["Error"]["Code"] == "ThrottlingException": + logger.error("Request throttled. Please try again later.") + else: + logger.error( + "Couldn't create keys and certificate. Here's why: %s: %s", + err.response["Error"]["Code"], + err.response["Error"]["Message"], + ) + raise + else: + return response + + + + + * For API details, see [CreateKeysAndCertificate](https://docs.aws.amazon.com/goto/boto3/iot-2015-05-28/CreateKeysAndCertificate) in _AWS SDK for Python (Boto3) API Reference_. + + + +