使用 boto3,如何检查 AWS IAM 用户是否有密码?

Using boto3, how to check if AWS IAM user has password?

在 IAM 中的用户中,我想以编程方式获取所有启用密码的用户的列表。从 AWS 控制台,我可以轻松发现它们。但是,如何以编程方式获取他们的列表?我想使用 python boto 来获取它。

我正在阅读这里 http://boto3.readthedocs.io/en/latest/reference/services/iam.html#iam,但是通过本文档中列出的大多数方法,我只能看到使用 'PasswordLastUsed' 的选项,这在三种情况下为 null

  1. 用户没有密码
  2. 密码存在但从未使用过
  3. 没有与用户关联的登录数据。

因此,仅通过检查 'PasswordLastUsed' 是否为空,我不能声称用户没有密码,因此无法获得所有具有密码的用户。我在这里错过了什么吗?我可以使用任何其他方式或任何其他 python 资源来执行此操作吗?

我可以看到有一种方法,就在您期望的地方...

http://boto3.readthedocs.io/en/latest/reference/services/iam.html#IAM.Client.get_credential_report

在报告中,字段 password_enabled 设置为 false 表示没有密码。

您可以使用 GetLoginProfile API 请求来确定 IAM 用户是否具有登录配置文件。如果没有与用户关联的登录配置文件,此请求将 return 404 响应。像这样的一些代码应该可以工作:

import boto3
iam = boto3.client('iam')
user_name = 'bob'
try:
    response = iam.get_login_profile(UserName=user_name)
except Exception, e:
    if e.response['ResponseMetadata']['HTTPStatusCode'] == 404:
        print('User {} has no login profile'.format(user_name))
import boto3

iam = boto3.resource('iam')
def isPasswordEnabled(user):
   login_profile = iam.LoginProfile(user)
   try:
     login_profile.create_date
     print True
  except:
     print False

>>> isPasswordEnabled('user1')
True
>>> isPasswordEnabled('user2')
False