如何使用boto检查AWS中的root帐户是否启用了MFA?

upa*_*ena 7 python api boto amazon-web-services

我正在研究受信任的顾问,需要检查是否还为根级别启用了MFA?它位于Trusted advisor Dashboard的Security部分.我正在使用Boto在Python中工作.

gar*_*aat 7

您可以GetAccountSummary在IAM中使用API调用,该get_account_summary调用可用作方法调用boto.iam.IAMConnection.

import boto.iam
conn = boto.iam.connect_to_region('us-east-1')
summary = conn.get_account_summary()
Run Code Online (Sandbox Code Playgroud)

这将返回一个Python字典,其中包含有关您帐户的大量信息.具体来说,要确定是否启用了MFA;

if summary['AccountMFAEnabled']:
    # MFA is enabled
else:
    # MFA is not enabled
Run Code Online (Sandbox Code Playgroud)


eat*_*ood 5

此答案更新为 boto3 并假设您在 ~/.aws/config 或 ~/.aws/credentials 文件中仅配置了一个帐户:

import boto3

client = boto3.client('iam')

if client.get_account_summary()['SummaryMap']['AccountMFAEnabled']:
    root_has_mfa = True
else:
    root_has_mfa = False
Run Code Online (Sandbox Code Playgroud)

如果您希望 get_account_summary 返回的字典,您也可以这样做:

import boto3

client = boto3.client('iam')

summary = client.get_account_summary()['SummaryMap']

if summary['AccountMFAEnabled']:
    root_has_mfa = True
else:
    root_has_mfa = False
Run Code Online (Sandbox Code Playgroud)