|
| 1 | +""" |
| 2 | +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | +# SPDX-License-Identifier: MIT-0 |
| 4 | +
|
| 5 | +Lambda function to decompose Infineon based certificate manifest(s) and begin |
| 6 | +the import processing pipeline |
| 7 | +""" |
1 | 8 | import os |
2 | 9 | import io |
3 | 10 | import json |
| 11 | +from xml.etree import ElementTree |
| 12 | +from base64 import b64encode |
4 | 13 | from botocore import exceptions as botoexceptions |
5 | 14 | from boto3 import resource as boto3resource, client as boto3client |
6 | | -import binascii |
7 | | -from xml.etree import ElementTree |
8 | 15 | from cryptography import x509 |
9 | 16 | from cryptography.hazmat.backends import default_backend |
10 | 17 | from cryptography.hazmat.primitives import serialization |
11 | | -from base64 import b64encode |
12 | 18 |
|
13 | | -# Given a bucket and object, verify its existence and return the resource. |
14 | 19 | def s3_object_stream(bucket_name: str, object_name: str): |
| 20 | + """Given a bucket and object, verify its existence and return the resource.""" |
15 | 21 | s3 = boto3resource('s3') |
16 | 22 | res = s3.Object(bucket_name=bucket_name, key=object_name) |
17 | | - try: |
| 23 | + try: |
18 | 24 | fs = io.BytesIO() |
19 | 25 | res.download_fileobj(fs) |
20 | 26 | return fs |
21 | 27 | except botoexceptions.ClientError as ce: |
22 | 28 | raise ce |
23 | 29 |
|
24 | | -# Given a bucket name and object name, return bytes representing |
25 | | -# the object content. |
| 30 | + |
26 | 31 | def s3_filebuf_bytes(bucket_name: str, object_name: str): |
| 32 | + """ Given a bucket name and object name, return bytes representing |
| 33 | + the object content.""" |
27 | 34 | object_stream = s3_object_stream(bucket_name=bucket_name, |
28 | 35 | object_name=object_name) |
29 | 36 | return object_stream.getvalue() |
30 | 37 |
|
31 | | -def format_certificate(certString): |
32 | | - encodedCert = certString.encode('ascii') |
| 38 | +def format_certificate(cert_string): |
| 39 | + """Encode certificate so that it can safely travel via sqs""" |
| 40 | + cert_encoded = cert_string.encode('ascii') |
33 | 41 |
|
34 | | - pem_obj = x509.load_pem_x509_certificate(encodedCert, |
| 42 | + pem_obj = x509.load_pem_x509_certificate(cert_encoded, |
35 | 43 | backend=default_backend()) |
36 | 44 | block = pem_obj.public_bytes(encoding=serialization.Encoding.PEM).decode('ascii') |
37 | | - return {'certificate': str(b64encode(block.encode('ascii')))} |
| 45 | + return str(b64encode(block.encode('ascii'))) |
38 | 46 |
|
39 | | - |
| 47 | +def queue_certificate(identity, certificate, queue_url): |
| 48 | + """Send the thing name and certificate to sqs queue""" |
| 49 | + sqs_client = boto3client("sqs") |
| 50 | + payload = { |
| 51 | + 'thing': identity, |
| 52 | + 'certificate': certificate |
| 53 | + } |
| 54 | + sqs_client.send_message( QueueUrl=queue_url, |
| 55 | + MessageBody=json.dumps(payload) ) |
40 | 56 |
|
41 | | -def invoke_export(manifest, queueUrl): |
42 | | - client = boto3client("sqs") |
43 | | - |
| 57 | +def invoke_export(manifest, queue_url): |
| 58 | + """Function to Iterate through the certificate list and queue for processing""" |
44 | 59 | root = ElementTree.fromstring(manifest) |
45 | 60 |
|
46 | 61 | for group in root.findall('group'): # /binaryhex |
47 | 62 | thing_name = '' |
48 | 63 |
|
| 64 | + # TODO: Evaluate what happens when this fails |
49 | 65 | for hex_element in group.findall('hex'): |
50 | 66 | if hex_element.get('name') == 'TpmMAC': |
51 | 67 | thing_name = hex_element.get('value') |
52 | 68 |
|
53 | | - # There can be more than one certificate |
54 | 69 | for hexdata_element in group.findall('binaryhex'): |
55 | 70 | certificate_data = format_certificate(hexdata_element.text) |
56 | | - # Need to send each certificate separately |
57 | | - certificate_data['thing'] = thing_name |
58 | | - print(certificate_data) |
59 | | - client.send_message( QueueUrl=queueUrl, |
60 | | - MessageBody=json.dumps(certificate_data) ) |
61 | | - |
| 71 | + queue_certificate(thing_name, certificate_data, queue_url) |
| 72 | + |
62 | 73 | def lambda_handler(event, context): |
63 | | - queueUrl = os.environ['QUEUE_TARGET'] |
| 74 | + """Lambda function main entry point""" |
| 75 | + queue_url = os.environ['QUEUE_TARGET'] |
64 | 76 |
|
65 | 77 | bucket = event['Records'][0]['s3']['bucket']['name'] |
66 | | - manifest = event['Records'][0]['s3']['object']['key'] |
| 78 | + manifest = event['Records'][0]['s3']['object']['key'] |
67 | 79 |
|
68 | | - manifestContent = s3_filebuf_bytes(bucket, manifest) |
| 80 | + manifest_content = s3_filebuf_bytes(bucket, manifest) |
69 | 81 |
|
70 | | - invoke_export(manifestContent, queueUrl) |
| 82 | + invoke_export(manifest_content, queue_url) |
0 commit comments