edit-icon download-icon

Use STS Token as user credentials

Last Updated: Mar 20, 2018

Using your Alibaba Cloud AccessKey directly to develop applications will have potential security risk. To enhance your account security, you can use the Security Token Service (STS) token issued for a subaccount to access Alibaba Cloud services.

Using the STS token as an access credential has the following advantages:

  • Using STS token will reduce the risks of a compromised AccessKey ID and AccessKey Secret, particularly reducing risks for your mobile devices.

  • STS token has flexible permission control. You can control the access permission in a finer granularity for products including SLB and ECS, according to the RAM role.

This document illustrates how to set up the STS token in the Python SDK to allow access to Alibaba Cloud services. For more information about STS token, see RAM and STS.

Note: Make sure that the product you are calling support STS.

Two methods are available to set up the STS token:

Use STS token directly

You can directly specify the STS token using the StsTokenCredential method. However, you must update the token periodically if you directly specify the token.

The StsTokenCredential method is as follows:

  1. from aliyunsdkcore.client import AcsClient
  2. from aliyunsdkcore.auth.credentials import StsTokenCredential
  3. sts_token_credential = StsTokenCredential('sts_access_key_id', 'sts_access_key_secret', 'sts_session_token')
  4. acs_client = AcsClient(region_id='cn-hangzhou', credential=sts_token_credential)

where:

  • region-id is the ID of the region you are using. For more information, see Regions and zones.
  • sts-access-key-id, sts-access-key-secret, and sts-session-token are the access information returned after you make an STS AssumeRole request.

Use SDK to manage STS tokens

You can create a new STSAssumeRoleSessionCredentialsProvider object to allow Alibaba Cloud Python SDK to create and maintain STS tokens.

The STSAssumeRoleSessionCredentialsProvider object is as follows:

  1. from aliyunsdkcore.client import AcsClient
  2. from aliyunsdkcore.auth.credentials import RamRoleArnCredential
  3. ram_role_arn_credential = RamRoleArnCredential('access_key_id', 'access_key_secret', 'role_arn', 'role_session_name')
  4. acs_client = AcsClient(region_id='cn-hangzhou', credential=ram_role_arn_credential)

where:

  • role-arn is the role resource descriptor. You can obtain it on the Role Details page from the RAM console.

  • role-session-name is a temporary role name. You can call the AssumeRole API to create a temporary identity. After the temporary identity is created, you can use the value set for the role-session-name parameter when calling the API.

Thank you! We've received your feedback.