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 C++ 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 must update the token periodically if you directly specify the token.

  1. #include <iostream>
  2. #include <alibabacloud/core/AlibabaCloud.h>
  3. #include <alibabacloud/core/sts/StsClient.h>
  4. #include <alibabacloud/core/StsAssumeRoleCredentialsProvider.h>
  5. #include <alibabacloud/ecs/EcsClient.h>
  6. using namespace AlibabaCloud;
  7. using namespace AlibabaCloud::Ecs;
  8. int main(int argc, char** argv)
  9. {
  10. // Initialize the SDK
  11. AlibabaCloud::InitializeSdk();
  12. // Configure the ECS instance
  13. ClientConfiguration configuration("<your-region-id>");
  14. EcsClient client(Credentials("<your-access-key-id>", "<your-access-key-secret>", <your-session-token>),
  15. configuration);
  16. // Create an API request and set parameters
  17. Model::DescribeInstancesRequest request;
  18. request.setPageSize(10);
  19. auto outcome = client.describeInstances(request);
  20. if (!outcome.isSuccess())
  21. {
  22. // Handle exceptions
  23. std::cout << outcome.error().errorCode() << std::endl;
  24. AlibabaCloud::ShutdownSdk();
  25. return -1;
  26. }
  27. std::cout << "totalCount: " << outcome.result().getTotalCount() << std::endl;
  28. // Close the SDK
  29. AlibabaCloud::ShutdownSdk();
  30. return 0;
  31. }

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 credentialsProvider object to allow Alibaba Cloud C++ SDK to create and maintain STS tokens.

  1. #include <iostream>
  2. #include <alibabacloud/core/AlibabaCloud.h>
  3. #include <alibabacloud/core/sts/StsClient.h>
  4. #include <alibabacloud/core/StsAssumeRoleCredentialsProvider.h>
  5. #include <alibabacloud/ecs/EcsClient.h>
  6. using namespace AlibabaCloud;
  7. using namespace AlibabaCloud::Ecs;
  8. int main(int argc, char** argv)
  9. {
  10. // Initialize the SDK
  11. AlibabaCloud::InitializeSdk();
  12. // Configure the ECS instance
  13. ClientConfiguration configuration("<your-region-id>");
  14. auto stsClient = std::make_shared<Sts::StsClient>("<your-access-key-id>", "<your-access-key-secret>", configuration);
  15. auto credentialsProvider = std::make_shared<StsAssumeRoleCredentialsProvider>(stsClient, "<your-role-arn>", "<your-role-session-name>");
  16. EcsClient client(credentialsProvider, configuration);
  17. // Create an API request and set parameters
  18. Model::DescribeInstancesRequest request;
  19. request.setPageSize(10);
  20. auto outcome = client.describeInstances(request);
  21. if (!outcome.isSuccess())
  22. {
  23. // Handle exceptions
  24. std::cout << outcome.error().errorCode() << std::endl;
  25. AlibabaCloud::ShutdownSdk();
  26. return -1;
  27. }
  28. std::cout << "totalCount: " << outcome.result().getTotalCount() << std::endl;
  29. // Close the SDK
  30. AlibabaCloud::ShutdownSdk();
  31. return 0;
  32. }

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.