【问题标题】:boto3 aws iot - how can I check if certificate has attached things to it?boto3 aws iot - 我如何检查证书是否附加了东西?
【发布时间】:2022-04-21 16:56:56
【问题描述】:

在aws iot中-

使用 boto3 (python) 我想检查证书是否附加了东西。 我怎样才能做到这一点? 谢谢。

【问题讨论】:

标签: python amazon-web-services boto3 aws-iot


【解决方案1】:
import boto3, time, csv
from concurrent.futures import ThreadPoolExecutor
from csv import writer
from pyjavaproperties import Properties

#########################################################################
# Properties reading from properties file
p = Properties()
p.load(open('prop.properties'))
# AWS Credentilas
aws_region='eu-west-1'
aws_access_key_id=p['aws_access_key_id']
aws_secret_access_key=p['aws_secret_access_key']
aws_session_token=p['aws_session_token']
#########################################################################
file_name1='QA-Ireland-Certificate-Attached-With-Thing.csv'
file_name2='QA-Ireland-Certificate-NOT-Attached-With-Thing.csv'
#########################################################################

client = boto3.client('iot',
region_name=aws_region,
aws_access_key_id= aws_access_key_id,  
aws_secret_access_key= aws_secret_access_key,
aws_session_token=aws_session_token)

##Function to Append List in CSV
def append_list_as_row(file_name, list_of_elem):
    with open(file_name, 'a') as write_obj:
        csv_writer = csv.writer(write_obj)
        csv_writer.writerow(list_of_elem)

results = []
attached = []
non_attached = []

def check_certificates(certificate):
    principal = client.list_principal_things(principal=certificate["certificateArn"])
    # print("%s - number of things: %d" % (certificate["certificateId"], len(principal["things"])))
    if len(principal["things"])==0:
        non_attached.append(certificate["certificateArn"])
        print("Total orphan certificates are: ", len(non_attached))
        list_var = []
        list_var.append('iot')
        list_var.append(certificate["certificateArn"])
        append_list_as_row(file_name2, list_var)
        #### add upir extra logic here.
    else:
        attached.append(certificate["certificateArn"])
        print("Total certificates attached are: ", len(attached))
        list_var = []
        list_var.append('iot')
        list_var.append(certificate["certificateArn"])
        append_list_as_row(file_name1, list_var)

def lambda_handler():
    paginator = client.get_paginator('list_certificates')
    page_iterator = paginator.paginate()
    for page in page_iterator:
        results.extend(page['certificates'])
        print("Total certificates scanned are: ", len(results))
    print("Total certificates scanned are: ", len(results))
    start_time = time.time()
    with ThreadPoolExecutor(max_workers=10) as executor:
        future = [executor.submit(check_certificates, item) for item in results]
    print("Certs have thing(s) attached: ", len(attached))
    print("Certs have NO thing(s) attached: ", len(non_attached))
    print("--- %s seconds using MultiThreading---" % (time.time() - start_time))
    
def main():
    lambda_handler()
if __name__== "__main__":
    main()

【讨论】:

  • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
猜你喜欢
  • 2019-10-14
  • 1970-01-01
  • 2020-12-08
  • 2022-08-24
  • 2019-02-18
  • 1970-01-01
  • 1970-01-01
  • 2011-06-06
  • 1970-01-01
相关资源
最近更新 更多