範本程式碼:使用多重要素驗證請求憑證 - AWS Identity and Access Management

範本程式碼:使用多重要素驗證請求憑證

以下範例顯示如何呼叫 GetSessionTokenAssumeRole 操作,並傳遞 MFA 驗證參數。呼叫 GetSessionToken 不需要許可,但您必須擁有一個可讓您呼叫 AssumeRole 的政策。傳回的憑證隨後用於列出帳戶中的所有 S3 儲存貯體。

使用 MFA 身分驗證呼叫 GetSessionToken (Python 和 C#)

以下範例使用 AWS SDK for Python (Boto)AWS SDK for .NET 寫入,示範如何呼叫 GetSessionToken 並傳遞 MFA 身分驗證資訊。然後,由 GetSessionToken 操作傳回的臨時安全憑證用於列出帳戶中的所有 S3 儲存貯體。

連接到執行此程式碼的使用者的政策 (或群組中的使用者),會針對傳回的臨時憑證提供許可。對於此範本程式碼,政策必須授予使用者許可來請求 Amazon S3 ListBuckets 操作。

使用 Python

import boto from boto.s3.connection import S3Connection from boto.sts import STSConnection # Prompt for MFA time-based one-time password (TOTP) mfa_TOTP = raw_input("Enter the MFA code: ") # The calls to AWS STS GetSessionToken must be signed with the access key ID and secret # access key of an IAM user. The credentials can be in environment variables or in # a configuration file and will be discovered automatically # by the STSConnection() function. For more information, see the Python SDK # documentation: http://boto.readthedocs.org/en/latest/boto_config_tut.html sts_connection = STSConnection() # Use the appropriate device ID (serial number for hardware device or ARN for virtual device). # Replace ACCOUNT-NUMBER-WITHOUT-HYPHENS and MFA-DEVICE-ID with appropriate values. tempCredentials = sts_connection.get_session_token( duration=3600, mfa_serial_number="&region-arn;iam::ACCOUNT-NUMBER-WITHOUT-HYPHENS:mfa/MFA-DEVICE-ID", mfa_token=mfa_TOTP ) # Use the temporary credentials to list the contents of an S3 bucket s3_connection = S3Connection( aws_access_key_id=tempCredentials.access_key, aws_secret_access_key=tempCredentials.secret_key, security_token=tempCredentials.session_token ) # Replace BUCKET-NAME with an appropriate value. bucket = s3_connection.get_bucket(bucket_name="BUCKET-NAME") objectlist = bucket.list() for obj in objectlist: print obj.name

使用 C#

Console.Write("Enter MFA code: "); string mfaTOTP = Console.ReadLine(); // Get string from user /* The calls to AWS STS GetSessionToken must be signed using the access key ID and secret access key of an IAM user. The credentials can be in environment variables or in a configuration file and will be discovered automatically by the AmazonSecurityTokenServiceClient constructor. For more information, see https://docs.aws.amazon.com/sdk-for-net/latest/developer-guide/net-dg-config-creds.html */ AmazonSecurityTokenServiceClient stsClient = new AmazonSecurityTokenServiceClient(); GetSessionTokenRequest getSessionTokenRequest = new GetSessionTokenRequest(); getSessionTokenRequest.DurationSeconds = 3600; // Replace ACCOUNT-NUMBER-WITHOUT-HYPHENS and MFA-DEVICE-ID with appropriate values getSessionTokenRequest.SerialNumber = "arn:aws:iam::ACCOUNT-NUMBER-WITHOUT-HYPHENS:mfa/MFA-DEVICE-ID"; getSessionTokenRequest.TokenCode = mfaTOTP; GetSessionTokenResponse getSessionTokenResponse = stsClient.GetSessionToken(getSessionTokenRequest); // Extract temporary credentials from result of GetSessionToken call GetSessionTokenResult getSessionTokenResult = getSessionTokenResponse.GetSessionTokenResult; string tempAccessKeyId = getSessionTokenResult.Credentials.AccessKeyId; string tempSessionToken = getSessionTokenResult.Credentials.SessionToken; string tempSecretAccessKey = getSessionTokenResult.Credentials.SecretAccessKey; SessionAWSCredentials tempCredentials = new SessionAWSCredentials(tempAccessKeyId, tempSecretAccessKey, tempSessionToken); // Use the temporary credentials to list the contents of an S3 bucket // Replace BUCKET-NAME with an appropriate value ListObjectsRequest S3ListObjectsRequest = new ListObjectsRequest(); S3ListObjectsRequest.BucketName = "BUCKET-NAME"; S3Client = AWSClientFactory.CreateAmazonS3Client(tempCredentials); ListObjectsResponse S3ListObjectsResponse = S3Client.ListObjects(S3ListObjectsRequest); foreach (S3Object s3Object in S3ListObjectsResponse.S3Objects) { Console.WriteLine(s3Object.Key); }

使用 MFA 身分驗證呼叫 AssumeRole (Python)

以下範例使用 AWS SDK for Python (Boto) 寫入,示範如何呼叫 AssumeRole 並傳遞 MFA 身分驗證資訊。然後,由 AssumeRole 傳回的臨時安全憑證用於列出帳戶中的所有 Amazon S3 儲存貯體。

如需有關此案例的詳細資訊,請參閱 使用案例:跨帳戶委派的 MFA 防護

import boto from boto.s3.connection import S3Connection from boto.sts import STSConnection # Prompt for MFA time-based one-time password (TOTP) mfa_TOTP = raw_input("Enter the MFA code: ") # The calls to AWS STS AssumeRole must be signed with the access key ID and secret # access key of an IAM user. (The AssumeRole API operation can also be called using temporary # credentials, but this example does not show that scenario.) # The IAM user credentials can be in environment variables or in # a configuration file and will be discovered automatically # by the STSConnection() function. For more information, see the Python SDK # documentation: http://boto.readthedocs.org/en/latest/boto_config_tut.html sts_connection = STSConnection() # Use appropriate device ID (serial number for hardware device or ARN for virtual device) # Replace ACCOUNT-NUMBER-WITHOUT-HYPHENS, ROLE-NAME, and MFA-DEVICE-ID with appropriate values tempCredentials = sts_connection.assume_role( role_arn="arn:aws:iam::ACCOUNT-NUMBER-WITHOUT-HYPHENS:role/ROLE-NAME", role_session_name="AssumeRoleSession1", mfa_serial_number="arn:aws:iam::ACCOUNT-NUMBER-WITHOUT-HYPHENS:mfa/MFA-DEVICE-ID", mfa_token=mfa_TOTP ) # Use the temporary credentials to list the contents of an S3 bucket s3_connection = S3Connection( aws_access_key_id=tempCredentials.credentials.access_key, aws_secret_access_key=tempCredentials.credentials.secret_key, security_token=tempCredentials.credentials.session_token ) # Replace BUCKET-NAME with a real bucket name bucket = s3_connection.get_bucket(bucket_name="BUCKET-NAME") objectlist = bucket.list() for obj in objectlist: print obj.name