AWS STS 使用 SDK 獲取需要 MFA 令牌的會話令 AWS 牌 - AWS SDK 程式碼範例

AWS 文件 AWS SDK 範例 GitHub 存放庫中提供了更多 SDK 範例

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

AWS STS 使用 SDK 獲取需要 MFA 令牌的會話令 AWS 牌

下列程式碼範例示範如何獲取需要 MFA 權杖的工作階段字符。

警告

為避免安全風險,在開發專用軟體或使用真實資料時,請勿使用 IAM 使用者進行身分驗證。相反地,搭配使用聯合功能和身分提供者,例如 AWS IAM Identity Center

  • 建立一個可授予許可的 IAM 角色,以列出 Amazon S3 儲存貯體。

  • 建立 IAM 使用者,該使用者只有在提供 MFA 憑證時才具有擔任該角色的許可。

  • 為使用者註冊 MFA 裝置。

  • 提供 MFA 憑證以取得工作階段權杖,並使用暫時性憑證列出 S3 儲存貯體。

Python
適用於 Python (Boto3) 的 SDK
注意

還有更多關於 GitHub。尋找完整範例,並了解如何在AWS 設定和執行程式碼範例儲存庫

建立 IAM 使用者,註冊 MFA 裝置,並建立角色,授予許可以便在使用 MFA 憑證時讓使用者僅列出 S3 儲存貯體。

def setup(iam_resource): """ Creates a new user with no permissions. Creates a new virtual multi-factor authentication (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 an inline policy for the user that lets the user list Amazon S3 buckets, but only when MFA credentials are used. Any MFA device that can scan a QR code will work with this demonstration. Common choices are mobile apps like LastPass Authenticator, Microsoft Authenticator, or Google Authenticator. :param iam_resource: A Boto3 AWS Identity and Access Management (IAM) resource that has permissions to create users, MFA devices, and policies in the account. :return: The newly created user, user key, and virtual MFA device. """ user = iam_resource.create_user(UserName=unique_name("user")) print(f"Created user {user.name}.") virtual_mfa_device = iam_resource.create_virtual_mfa_device( VirtualMFADeviceName=unique_name("mfa") ) print(f"Created virtual MFA device {virtual_mfa_device.serial_number}") print( f"Showing the QR code for the device. Scan this in the MFA app of your " f"choice." ) with open("qr.png", "wb") as qr_file: qr_file.write(virtual_mfa_device.qr_code_png) webbrowser.open(qr_file.name) print(f"Enter two consecutive code from your MFA device.") mfa_code_1 = input("Enter the first code: ") mfa_code_2 = input("Enter the second code: ") user.enable_mfa( SerialNumber=virtual_mfa_device.serial_number, AuthenticationCode1=mfa_code_1, AuthenticationCode2=mfa_code_2, ) os.remove(qr_file.name) print(f"MFA device is registered with the user.") user_key = user.create_access_key_pair() print(f"Created access key pair for user.") print(f"Wait for user to be ready.", end="") progress_bar(10) user.create_policy( PolicyName=unique_name("user-policy"), PolicyDocument=json.dumps( { "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": "s3:ListAllMyBuckets", "Resource": "arn:aws:s3:::*", "Condition": {"Bool": {"aws:MultiFactorAuthPresent": True}}, } ], } ), ) print( f"Created an inline policy for {user.name} that lets the user list buckets, " f"but only when MFA credentials are present." ) print("Give AWS time to propagate these new resources and connections.", end="") progress_bar(10) return user, user_key, virtual_mfa_device

透過傳遞 MFA 字符獲取暫時性工作階段憑證,並使用憑證列出該帳戶的 S3 儲存貯體。

def list_buckets_with_session_token_with_mfa(mfa_serial_number, mfa_totp, sts_client): """ Gets a session token with MFA credentials and uses the temporary session credentials to list Amazon S3 buckets. Requires an MFA device serial number and token. :param mfa_serial_number: The serial number of the MFA device. For a virtual MFA device, this is an Amazon Resource Name (ARN). :param mfa_totp: A time-based, one-time password issued by the MFA device. :param sts_client: A Boto3 STS instance that has permission to assume the role. """ if mfa_serial_number is not None: response = sts_client.get_session_token( SerialNumber=mfa_serial_number, TokenCode=mfa_totp ) else: response = sts_client.get_session_token() temp_credentials = response["Credentials"] s3_resource = boto3.resource( "s3", aws_access_key_id=temp_credentials["AccessKeyId"], aws_secret_access_key=temp_credentials["SecretAccessKey"], aws_session_token=temp_credentials["SessionToken"], ) print(f"Buckets for the account:") for bucket in s3_resource.buckets.all(): print(bucket.name)

銷毀為示範所建立的資源。

def teardown(user, virtual_mfa_device): """ Removes all resources created during setup. :param user: The demo user. :param role: The demo MFA device. """ for user_pol in user.policies.all(): user_pol.delete() print("Deleted inline user policy.") for key in user.access_keys.all(): key.delete() print("Deleted user's access key.") for mfa in user.mfa_devices.all(): mfa.disassociate() virtual_mfa_device.delete() user.delete() print(f"Deleted {user.name}.")

使用先前定義的函數執行此案例。

def usage_demo(): """Drives the demonstration.""" print("-" * 88) print( f"Welcome to the AWS Security Token Service assume role demo, " f"starring multi-factor authentication (MFA)!" ) print("-" * 88) iam_resource = boto3.resource("iam") user, user_key, virtual_mfa_device = setup(iam_resource) try: sts_client = boto3.client( "sts", aws_access_key_id=user_key.id, aws_secret_access_key=user_key.secret ) try: print("Listing buckets without specifying MFA credentials.") list_buckets_with_session_token_with_mfa(None, None, sts_client) except ClientError as error: if error.response["Error"]["Code"] == "AccessDenied": print("Got expected AccessDenied error.") mfa_totp = input("Enter the code from your registered MFA device: ") list_buckets_with_session_token_with_mfa( virtual_mfa_device.serial_number, mfa_totp, sts_client ) finally: teardown(user, virtual_mfa_device) print("Thanks for watching!")
  • 如需 API 的詳細資訊,請參閱AWS 開發套件GetSessionToken中的 Python (博托 3) API 參考。