Advertisement
Guest User

Untitled

a guest
Feb 20th, 2020
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.90 KB | None | 0 0
  1. import json
  2. import boto3
  3. import io
  4. import gzip
  5. import base64
  6. from io import StringIO
  7.  
  8. mf = io.BytesIO()
  9. gg = boto3.client('greengrass')
  10. iot = boto3.client('iot')
  11.  
  12. def gzip_b64encode(data):
  13.     compressed = io.BytesIO()
  14.     with gzip.GzipFile(fileobj=compressed, mode='w') as f:
  15.         json_response = json.dumps(data)
  16.         f.write(json_response.encode('utf-8'))
  17.     return base64.b64encode(compressed.getvalue()).decode('ascii')
  18.  
  19. def lambda_handler(event, context):
  20.    
  21.     group = gg.create_group(Name="group_test")
  22.  
  23.     keys_cert = iot.create_keys_and_certificate(setAsActive=True)
  24.     core_thing = iot.create_thing(thingName="core_group_test")
  25.     keys = keys_cert['keyPair']
  26.  
  27.     cert_file = io.StringIO(keys_cert['certificatePem'])
  28.  
  29.     private_key_file = io.StringIO(keys['PrivateKey'])
  30.  
  31.     public_key_file = io.StringIO(keys['PublicKey'])
  32.  
  33.     iot.attach_thing_principal(
  34.         thingName=core_thing['thingName'],
  35.         principal=keys_cert['certificateArn'])
  36.  
  37.     core_policy_doc = {
  38.         "Version": "2012-10-17",
  39.         "Statement": [
  40.             {
  41.                 "Effect": "Allow",
  42.                 "Action": ["iot:Publish", "iot:Subscribe", "iot:Connect", "iot:Receive", "iot:GetThingShadow", "iot:DeleteThingShadow", "iot:UpdateThingShadow"],
  43.                 "Resource": ["arn:aws:iot:" + boto3.session.Session().region_name + ":*:*"]
  44.             },
  45.             {
  46.                 "Effect": "Allow",
  47.                 "Action": ["greengrass:AssumeRoleForGroup", "greengrass:CreateCertificate", "greengrass:GetConnectivityInfo", "greengrass:GetDeployment", "greengrass:GetDeploymentArtifacts", "greengrass:UpdateConnectivityInfo", "greengrass:UpdateCoreDeploymentStatus"],
  48.                 "Resource": ["*"]
  49.             }
  50.         ]
  51.     }
  52.     policy = iot.create_policy(
  53.         policyName="my_core_23_policy",
  54.         policyDocument=json.dumps(core_policy_doc))
  55.  
  56.     iot.attach_principal_policy(
  57.         policyName=policy['policyName'],
  58.         principal=keys_cert['certificateArn'])
  59.  
  60.     initial_version = {'Cores': [
  61.         {
  62.             'Id': core_thing['thingName'], # Quite intuitive, eh?
  63.             'CertificateArn': keys_cert['certificateArn'],
  64.             'SyncShadow': False, # Up to you, True|False
  65.             'ThingArn': core_thing['thingArn']
  66.         }
  67.     ]}
  68.  
  69.     core_definition = gg.create_core_definition(
  70.         Name="{0}_core_def".format(group['Name']),
  71.         InitialVersion=initial_version)
  72.  
  73.     retrieved_data = {}
  74.     retrieved_data['cert.pem'] = keys_cert['certificatePem']
  75.     retrieved_data['private.key'] = keys['PrivateKey']
  76.     retrieved_data['public.key'] = keys['PublicKey']
  77.     # # Create in memory zip and add files
  78.     # zippedfiles = zipfile.ZipFile(mf, mode='w',compression=zipfile.ZIP_DEFLATED)
  79.     # zippedfiles.writestr('cert.pem', io.StringIO(keys_cert['certificatePem'])
  80.     # zippedfiles.writestr('private.key', io.StringIO(keys_cert['certificatePem']['PrivateKey'])
  81.     # zippedfiles.writestr('public.key', io.StringIO(keys_cert['certificatePem']['PublicKey'])
  82.     # zippedfiles.close()
  83.  
  84.     # with zipfile.ZipFile(mf, mode="w",compression=zipfile.ZIP_DEFLATED) as zf:
  85.     #     zf.writestr('cert.pem', io.StringIO(keys_cert['certificatePem']))
  86.     #     # zf.writestr('private.key', io.StringIO(keys_cert['certificatePem']['PrivateKey'])
  87.     #     # zf.writestr('public.key', io.StringIO(keys_cert['certificatePem']['PublicKey'])
  88.  
  89.     # with open("zf.zip", "wb") as f: # use `wb` mode
  90.     #     f.write(mf.getvalue())
  91.  
  92.     return {
  93.             "isBase64Encoded": True,
  94.             "statusCode": 200,
  95.             "headers": {
  96.                 "Content-Type": "application/zip",
  97.                 #"Content-Encoding": "gzip",
  98.                 #"Content-Disposition": "attachment; filename=test.zip",
  99.                 #"Access-Control-Allow-Origin": "*"
  100.             },
  101.             "body": gzip_b64encode(retrieved_data)
  102.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement