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 Go 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. However, you must update the token periodically if you directly specify the token.

  • Do not use the custom configuration:

    1. // If you do not need to customize the config, we provide a shortcut to build a client
    2. client, err = ecs.NewClientWithStsToken("region-id", "sts-access-key-id",
    3. "sts-access-key-secret", "sts-session-token")
  • Use the custom configuration:

    1. // If you need to customize the config, you can use the following codes to build a client
    2. config := sdk.NewConfig().WithEnableAsync(true)
    3. // Create the credential object
    4. credential := credentials.NewStsTokenCredential("sts-access-key-id",
    5. "sts-access-key-secret", "sts-session-token")
    6. // Initialize the client
    7. client, err := ecs.NewClientWithOptions("region-id", config, credential)

where:

  • region-id is the ID of the region that you are using. See Regions and zones to obtain the region ID.

  • sts-access-key-id, sts-access-key-secret, and sts-session-token are credentials returned by the AssumeRole API.

Use SDK to manage STS tokens

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

  • Do not use the custom configuration:

    1. // If you do not need to customize the config, we provide a shortcut to build a client
    2. client, err = ecs.NewClientWithRamRoleArn("region-id", "sts-access-key-id",
    3. "sts-access-key-secret", "role-arn", "session-role-name")
  • Use the custom configuration:

    1. // If you need to customize the config, you can use the following codes to build a client
    2. config := sdk.NewConfig()
    3. // Create the credential object
    4. credential := credentials.NewRamRoleArnCredential("sts-access-key-id",
    5. "sts-access-key-secret", "role-arn", "role-session-name")
    6. // Initialize the client
    7. client, err := ecs.NewClientWithOptions("region-id", config, 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.