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

时间:2018-03-23 22:43:58

标签: amazon-web-services boto3 amazon-iam

我正在使用boto3审核AWS中的用户密码,而我没有找到完成以下CIS基准的方法:"确保禁用未使用90天或更长时间的凭据(启用密码)。 #34;

我有代码来提取密码期限并提取上次使用密码的时间,但我找不到任何可以使密码无效的内容。

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

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)

有没有人有任何指示?

3 个答案:

答案 0 :(得分:1)

如果要删除指定IAM用户的密码,

delete_login_profile是您应该使用的密码,这将终止用户通过AWS管理控制台访问AWS服务的能力。

但是,为防止所有用户访问(包括CLI和API访问),您还必须使任何访问密钥处于非活动状态或删除它们。

来自Boto3文档:

  

警告

     

删除用户密码不会阻止用户访问AWS   通过命令行界面或API。防止所有用户   访问您还必须使任何访问密钥不活动或删除   他们。有关使密钥无效或删除的详细信息   它们,请参阅UpdateAccessKey和DeleteAccessKey。

答案 1 :(得分:0)

如果要更改密码,则应使用update_login_profile boto3 API。如果要禁用密码,则需要使用delete_login_profile

  • boto3 update_login_profile的文档可以找到here
  • boto3 delete_login_profile的文档可以找到here

答案 2 :(得分:0)

感谢响应者,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)