Created
June 9, 2017 21:18
-
-
Save olibob/c668c885fae129d95498476b83c2bc23 to your computer and use it in GitHub Desktop.
This file contains 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
import boto3 | |
import json | |
import os, glob, sys | |
# Helpers | |
def jDump(dict): | |
return json.dumps(dict, sort_keys = True, indent = 4) | |
def printInfo(info, dict): | |
print("="*10 + '\n' + info + "\n" + "="*10) | |
print(jDump(dict)) | |
# Vars | |
# Set AWS_ACCOUNT_ID as environment variable | |
AWSAccountID = os.environ.get("AWS_ACCOUNT_ID") | |
policyTemplate = { | |
"Version": "2012-10-17", | |
"Statement": [ | |
{ | |
"Effect": "Allow", | |
"Action": "iot:Connect", | |
"Resource": "*" | |
}, | |
{ | |
"Effect": "Allow", | |
"Action": "iot:Publish", | |
"Resource": "" | |
} | |
] | |
} | |
# Create a aws IOT client | |
client = boto3.client('iot') | |
# Create a thing | |
thingName = "testThing" | |
thingAttributes = {"SN": "bla123"} | |
thing = client.create_thing( | |
thingName = thingName, | |
attributePayload = { "attributes": thingAttributes, "merge": False} | |
) | |
arn = thing['thingArn'] | |
print('ARN: %s ' % arn) | |
# Create certificate and key for thing | |
cert = client.create_keys_and_certificate( | |
setAsActive=True | |
) | |
certID = cert['certificateId'] | |
certArn = cert['certificateArn'] | |
certFile =thing['thingName']+"cert.pem" | |
keyFile =thing['thingName']+"key.pem" | |
with open(certFile, 'w') as f: | |
f.write(cert['certificatePem']) | |
with open(keyFile, 'w') as f: | |
f.write(cert['keyPair']['PrivateKey']) | |
# Create a policy | |
IOTTopic = thingName + "/001" | |
policyTemplate['Statement'][1]['Resource'] = "arn:aws:iot:eu-central-1:%s:topic/%s" % (AWSAccountID, IOTTopic) | |
policyName = thingName + "Pol" | |
policyDocument = str(json.dumps(policyTemplate)) | |
policy = client.create_policy( | |
policyName = policyName, | |
policyDocument = policyDocument | |
) | |
# Attach Thing | |
response = client.attach_thing_principal( | |
thingName=thingName, | |
principal=certArn | |
) | |
# Attach Policy | |
response = client.attach_principal_policy( | |
policyName=policyName, | |
principal=certArn | |
) | |
# Delete everything | |
delete = input("Delete thing? ") | |
if delete == 'y': | |
# update cert (deactivate) | |
deActivateCert = response = client.update_certificate( | |
certificateId=certID, | |
newStatus='INACTIVE' | |
) | |
printInfo("Deactivating Cert", deActivateCert) | |
# detach policy | |
detachPolicy = client.detach_principal_policy( | |
policyName=policyName, | |
principal=certArn | |
) | |
printInfo("Detach policy", detachPolicy) | |
# detach thing | |
detachThing = client.detach_thing_principal( | |
thingName=thingName, | |
principal=certArn | |
) | |
printInfo("Detach thing", detachThing) | |
# delete policy | |
# if you use versions other then default, delete versions before! | |
deletePolicy = client.delete_policy( | |
policyName=policyName | |
) | |
printInfo("Delete policy", deletePolicy) | |
# delete thing | |
deleteThing = client.delete_thing( | |
thingName='testThing', | |
) | |
printInfo("Delete thing", deleteThing) | |
# delete certificate | |
deleteCert = client.delete_certificate( | |
certificateId=certID | |
) | |
filelist = glob.glob("*.pem") | |
for f in filelist: | |
os.remove(f) | |
printInfo("Deleting Cert", deleteCert) | |
print("Done!") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment