3333from aws_lambda_powertools .utilities .data_classes import S3Event
3434from aws_lambda_powertools .utilities .typing import LambdaContext
3535from aws_lambda_powertools .utilities .validation import validator
36- from schemas import INPUT_SCHEMA , OUTPUT_SCHEMA
37-
38- _LAMBDA_S3_RESOURCE = { "resource" : boto3resource ('s3' ),
39- "bucket_name" : os .environ .get ("S3_BUCKET_NAME" ,"NONE" ) }
40- _LAMBDA_SQS_RESOURCE = { "resource" : boto3resource ('sqs' ),
41- "bucket_name" : os .environ .get ("S3_BUCKET_NAME" ,"NONE" ) }
42- _LAMBDA_IOT_RESOURCE = { "resource" : boto3resource ('iot' ),
43- "bucket_name" : os .environ .get ("S3_BUCKET_NAME" ,"NONE" ) }
4436
4537logger = logging .getLogger ()
4638logger .setLevel ("INFO" )
5143 100 : "d" ,
5244}
5345
54-
5546verbose = True
5647config = None
5748
49+ # Verify that the certificate is in IoT Core
5850def get_certificate (certificateId ):
5951 try :
6052 response = iot_client .describe_certificate (certificateId = certificateId )
@@ -63,6 +55,7 @@ def get_certificate(certificateId):
6355 print ("Certificate [" + certificateId + "] not found in IoT Core." )
6456 return None
6557
58+ # Retrieve the certificate Arn.
6659def get_certificate_arn (certificateId ):
6760 try :
6861 response = iot_client .describe_certificate (certificateId = certificateId )
@@ -109,11 +102,11 @@ def get_thing_group(thingGroupName):
109102
110103def get_thing_type (typeName ):
111104 try :
112- response = iot_client .describeThingType (thingTypeName = thingTypeName )
105+ response = iot_client .describeThingType (thingTypeName = typeName )
113106 return response .get ('thingTypeArn' )
114107 except botocore .exceptions .ClientError as error :
115108 if error .response ['Error' ]['Code' ] == 'ResourceNotFoundException' :
116- print ("ERROR: You need to configure the Thing Type [" + thingTypeName + "] in your target region first." )
109+ print ("ERROR: You need to configure the Thing Type [" + typeName + "] in your target region first." )
117110 if error .response ['Error' ]['Code' ] == 'UnauthorizedException' :
118111 print ("ERROR: There is a deployment problem with the attached Role. Unable to reach IoT Core object." )
119112 return None
@@ -157,22 +150,24 @@ def process_thing(thingName, certificateId, thingTypeName):
157150 return None
158151
159152def requeue ():
160- sqs_client = boto3 . client ('sqs' )
153+ sqs_client = boto3client ('sqs' )
161154 queueUrl = os .environ .get ('QUEUE_TARGET' )
162155 sqs_client .send_message ( QueueUrl = queueUrl ,
163- MessageBody = json .dumps (config ))
156+ MessageBody = json .dumps (config ))
157+
158+ def get_certificate_fingerprint (certificate : x509 .Certificate ):
159+ return binascii .hexlify (certificate .fingerprint (hashes .SHA256 ())).decode ('UTF-8' )
164160
165161def process_certificate (payload ):
166- client = boto3 . client ('iot' )
162+ client = boto3client ('iot' )
167163
168164 certificateText = base64 .b64decode (eval (payload ))
169165
170166 # See if the certificate has already been registered. If so, bail.
171167 certificateObj = x509 .load_pem_x509_certificate (data = certificateText ,
172168 backend = default_backend ())
173169
174- fingerprint = binascii .hexlify (certificateObj .fingerprint (hashes .SHA256 ())).decode ('UTF-8' )
175- print ("Fingerprint: " + fingerprint )
170+ fingerprint = get_certificate_fingerprint (certificateObj )
176171
177172 if (get_certificate (fingerprint )):
178173 try :
@@ -183,7 +178,6 @@ def process_certificate(payload):
183178 except :
184179 print ("Certificate [" + fingerprint + "] not found in IoT Core. Importing." )
185180
186-
187181 try :
188182 response = iot_client .register_certificate_without_ca (certificatePem = certificateText .decode ('ascii' ),
189183 status = 'ACTIVE' )
0 commit comments