如何使用 boto3 在 AWS 中禁用用户的密码

eat*_*ood 5 amazon-web-services amazon-iam boto3

我正在使用 boto3 在 AWS 中审计用户密码,但我没有找到实现以下 CIS 基准的方法:“确保禁用 90 天或更长时间未使用的凭证(启用密码)被禁用。”

我有提取密码年龄并提取上次使用密码的代码,但我没有找到任何使密码无效的代码。

对于访问密钥(但不是密码),我们有以下内容:

client = session.client('iam')

... (get user and keyid) ...

last_used = client.get_access_key_last_used(AccessKeyId=keyid)

... (determine the age of the key) ...

if age >= 90:

    client.update_access_key(AccessKeyId=keyid, Status='Inactive', UserName=user)
Run Code Online (Sandbox Code Playgroud)

有没有人有任何指示?

eat*_*ood 1

感谢响应者,delete_login_profile 然后使用 create_login_profile 重置密码正是我所需要的。我在文档中看到了它,但“删除”听起来太可怕了。

def getPassword(client, user):
    ''' get the password data from aws '''
    try:
        response = client.get_login_profile(UserName=user)
        return response

    except client.exceptions.NoSuchEntityException as e:
        print(e)
        return ''

# setup the client handler
client = session.client('iam')

# set the user
user = 'some.user'

# if the user has a password, execute this code block
if getPassword(client=client, user=user):

    ... code to test the password age here ...
    ... if it's too old, then ...

    # remove the login_profile/password/ability to use the Console
    client.delete_login_profile(UserName=user)

    # set the new password
    passwd = raw_input('Enter New Password: ')

    # create the new login_profile with the new password and force the user to change the password on the next login
    client.create_login_profile(UserName=user, Password=passwd, PasswordResetRequired=True)
Run Code Online (Sandbox Code Playgroud)