【问题标题】:How to check whether MFA is enabled for root account in AWS using boto?如何使用 boto 检查是否为 AWS 中的根账户启用了 MFA?
【发布时间】:2018-09-18 07:19:55
【问题描述】:

我正在使用受信任的顾问,并且需要检查是否也为根级别启用了 MFA? 它位于可信顾问仪表板的安全部分。 我正在使用 Boto 在 Python 中工作。

【问题讨论】:

    标签: python api amazon-web-services boto


    【解决方案1】:

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

    import boto.iam
    conn = boto.iam.connect_to_region('us-east-1')
    summary = conn.get_account_summary()
    

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

    if summary['AccountMFAEnabled']:
        # MFA is enabled
    else:
        # MFA is not enabled
    

    【讨论】:

    • 但这会检查是否为ROOT帐户启用了MFA吗?
    • 非常感谢 :) 我现在没有 15 个学分...这就是为什么不能接受答案的原因 :( 很快就会这样做!! :)
    • 您不必拥有 15 个声望点来接受答案。通过接受答案,答案的作者将获得 15 分,而您获得 2 分。将您的名誉归功于正确的答案永远不会花费您的声誉。这对堆栈溢出社区会适得其反。
    【解决方案2】:

    此答案更新到 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
    

    如果您希望使用 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-02-19
      • 1970-01-01
      • 2013-11-12
      • 1970-01-01
      • 2020-03-12
      • 1970-01-01
      • 1970-01-01
      • 2018-12-16
      相关资源
      最近更新 更多