Creates a new user with no permissions. Creates a new virtual MFA device. Displays the QR code to seed the device. Asks for two codes from the MFA device. Registers the MFA device for the user. Creates an access key pair for the user. Creates a role with a policy that le
(iam_resource)
| 32 | |
| 33 | # snippet-start:[python.example_code.sts.Scenario_AssumeRoleMfa_setup] |
| 34 | def setup(iam_resource): |
| 35 | """ |
| 36 | Creates a new user with no permissions. |
| 37 | Creates a new virtual MFA device. |
| 38 | Displays the QR code to seed the device. |
| 39 | Asks for two codes from the MFA device. |
| 40 | Registers the MFA device for the user. |
| 41 | Creates an access key pair for the user. |
| 42 | Creates a role with a policy that lets the user assume the role and requires MFA. |
| 43 | Creates a policy that allows listing Amazon S3 buckets. |
| 44 | Attaches the policy to the role. |
| 45 | Creates an inline policy for the user that lets the user assume the role. |
| 46 | |
| 47 | For demonstration purposes, the user is created in the same account as the role, |
| 48 | but in practice the user would likely be from another account. |
| 49 | |
| 50 | Any MFA device that can scan a QR code will work with this demonstration. |
| 51 | Common choices are mobile apps like LastPass Authenticator, |
| 52 | Microsoft Authenticator, or Google Authenticator. |
| 53 | |
| 54 | :param iam_resource: A Boto3 AWS Identity and Access Management (IAM) resource |
| 55 | that has permissions to create users, roles, and policies |
| 56 | in the account. |
| 57 | :return: The newly created user, user key, virtual MFA device, and role. |
| 58 | """ |
| 59 | user = iam_resource.create_user(UserName=unique_name("user")) |
| 60 | print(f"Created user {user.name}.") |
| 61 | |
| 62 | virtual_mfa_device = iam_resource.create_virtual_mfa_device( |
| 63 | VirtualMFADeviceName=unique_name("mfa") |
| 64 | ) |
| 65 | print(f"Created virtual MFA device {virtual_mfa_device.serial_number}") |
| 66 | |
| 67 | print( |
| 68 | f"Showing the QR code for the device. Scan this in the MFA app of your " |
| 69 | f"choice." |
| 70 | ) |
| 71 | with open("qr.png", "wb") as qr_file: |
| 72 | qr_file.write(virtual_mfa_device.qr_code_png) |
| 73 | webbrowser.open(qr_file.name) |
| 74 | |
| 75 | print(f"Enter two consecutive code from your MFA device.") |
| 76 | mfa_code_1 = input("Enter the first code: ") |
| 77 | mfa_code_2 = input("Enter the second code: ") |
| 78 | user.enable_mfa( |
| 79 | SerialNumber=virtual_mfa_device.serial_number, |
| 80 | AuthenticationCode1=mfa_code_1, |
| 81 | AuthenticationCode2=mfa_code_2, |
| 82 | ) |
| 83 | os.remove(qr_file.name) |
| 84 | print(f"MFA device is registered with the user.") |
| 85 | |
| 86 | user_key = user.create_access_key_pair() |
| 87 | print(f"Created access key pair for user.") |
| 88 | |
| 89 | print(f"Wait for user to be ready.", end="") |
| 90 | progress_bar(10) |
| 91 |
no test coverage detected