You can use Security Token Service (STS) to generate temporary access credentials for users. These credentials limit access to TOS resources and expire after a set period. This topic describes how to use the temporary credentials of STS to access TOS.
For security purposes, we recommend using temporary credentials instead of permanent ones in the client code when files are uploaded to TOS directly from mobile applications or PC clients. Specifically, the application server obtains the access key ID (AK) and secret access key (SK) via the STS mechanism and delivers the temporary credentials to the client. This solution has the following strengths:
- Temporary credentials become invalid upon expiration, protecting data security.
- You can restrict the permissions of temporary credentials to mitigate the risks associated with leakage, such as cracking or hijacking.
Steps
The steps are as follows.
Step 1: Create an IAM user and grant it the STSAssumeRoleAccess permission
This section describes how to create an IAM user, for example, tos_user, and grant it the permission.
- In the left navigation pane, select User management > User.
- On the User page, click Create New User.
- On the Create User page, choose your preferred method for creating a user.
tip
In this topic, the user is created by username. For more information on other methods, see Create user. - In the Basic information settings wizard, enter your username and other information. Then, check the box for Programmatic access and click Next.
- In the Permission settings wizard, check the box for STSAssumeRoleAccess and click Next.
tip
This is a predefined system policy. You can search for and link it at the top of the pane.
- In the Review wizard, verify your username and click Next.
Step 2: Create an IAM role
This section describes how to create an IAM role, for example, tos_role.
- In the left navigation pane, click Role Management.
- On the Role Management page, click Create New Role.
- In the Create New Role panel, select Account under Select trust identity, and The current account under Select identity. Then, click Next.
- In the Configure role wizard, enter your Role name and other information. Then, click Next.
- In the Authorize wizard, click Skip.
Step 3: Specify the user in the trust policy
This section describes how to specify a user in the trust policy, for example, to change the user to tos_user.
- On the Role Management page, click the role name created in Step 2.
- On the Role detail page, click the Trust relationship tab.
- On the Trust relationship tab, click Edit trust policy.
- Replace the root user with the user created in Step 1 and click Save.
- Below is the sample trust policy.
tip
The trust policy should be in the following format:
- The default trust policy is trn:iam::2100*****4:root. You need to replace root with the created IAM user, for example, trn:iam::2100*****4:user/tos_user.
- The trn user is in the format of trn:iam::{accountID}:user/{userName}, where {accountID} is the account ID of the role, and {userName} is the username.
Step 4: Create an IAM policy
Create an authorization policy based on your business scenario.
- In the left navigation pane, click Permission policy.
- On the Permission policy page, click Create Policy.
- On the Create Policy page, enter your Policy name. Then, click the JSON editor tab and enter the policy content.
- This section describes how to grant the upload and download permissions for bucket tos-sts. Below is the sample policy.
- The parameters are described below.
- Once you have finished configuring, click Submit.
Step 5: Grant corresponding permissions to the role
After creating the policy, grant policy permissions to the role created in Step 2.
- On the Permission policy page, click the name of the policy you just created.
- On the Policy detail page, click Authorize.
- On the Authorize page, select the Role tab under Identity. Then, select the role created in Step 2 and click Submit.
Step 6: Obtain the temporary credentials through the AssumeRole API
warning
- The permissions of the temporary credentials are the intersection of the permissions of the role and the IAM policy.
- If no IAM policy is specified, the temporary credentials have the permissions of the prelinked policy of the specified role.
- If the role has no prelinked policy, the temporary credentials will have no permissions, even if the IAM policy is specified.
Although some headers differ, Volcano Engine APIs use signature algorithms that are largely consistent with AWS Signature Version 4. For more information, see Signature Mechanism . You can call the AssumeRole API of STS to get temporary credentials. You can call this API through the following SDKs: Below are the complete request parameters.
tip
When calling AssumeRole to obtain temporary access credentials, you can specify the duration of the credentials using the DurationSeconds parameter. For more information, see AssumeRole. Alternatively, you can calculate a signature and request temporary access credentials using the code examples provided below, which are illustrated in Python and Go.
The parameters are described below.
Below is an example of a successful response.
Step 7: Use the temporary credentials to access TOS
After obtaining the temporary credentials, you can access TOS with either TOS SDKs or AWS S3 SDKs. This section describes how to do so with the TOS Go SDK.
tip
- For sample code to configure STS using TOS SDKs, see the following documents: