Use temporary credentials instead of static credentials
Creating an AccessKey for a Resource Access Management (RAM) user or the root user of an Alibaba Cloud account for programmatic access results in a static credential. A static credential consists of a fixed AccessKey ID and AccessKey secret that remain valid until deleted. Improper use of static credentials introduces significant security risks. For example, an application developer might hardcode a plaintext AccessKey into the application code and upload it to a public repository, such as GitHub. This can cause an AccessKey leak and lead to business losses. On Alibaba Cloud, you should use role assumption to obtain a temporary Security Token Service (STS) token instead of a static AccessKey whenever possible. Each STS token automatically expires after the maximum session duration of the role, which is typically measured in hours. This greatly reduces the risk of a credential leak.
Priority
High
Not recommended
Operations and maintenance (O&M), developer, and other personnel who need to debug APIs locally or run O&M scripts should not use a RAM user's AccessKey to run command-line interface (CLI) commands or scripts. In this scenario, a plaintext AccessKey can be easily written into code, scripts, or configuration files and accidentally committed to a public repository, such as GitHub, which can cause an AccessKey leak. Instead, you can use methods such as single sign-on (SSO) to assume a role and obtain an STS token.
Developers, product managers, and other personnel who use an OSS client (such as ossbrowser) to manage OSS files should not use a RAM user's AccessKey for authentication. In this case, the AccessKey is stored locally. If the device is lost or compromised, the AccessKey can be leaked, which can lead to a data leak. As more employees use the client, more AccessKeys are distributed, which significantly increases the risk of leaks. Instead, employees should use methods such as SSO to assume a role, obtain an STS token, and use the STS token to log on to the OSS client.
Developers hardcode a plaintext AccessKey in application code and commit the code to a public repository, such as GitHub.
Developers configure an AccessKey in a mobile app and publish the app to a public marketplace, such as the App Store.
Developers use an STS token in a mobile app but fail to grant fine-grained permissions to the corresponding RAM role or use a session policy to further restrict session-level permissions. If a client request is hijacked by an attacker, the STS token can be leaked, which creates a security risk.
Multiple people share an AccessKey. This practice increases the risk of leaks and makes it impossible to identify the user of the AccessKey in audit logs. If an employee leaves the company and the AccessKey is not promptly revoked, it can lead to security risks, such as data leaks. This also makes it difficult to determine who performed specific operations by reviewing the logs.
Implementation guide
For all scenarios, you should use STS tokens instead of RAM user AccessKeys whenever possible.
Employees use STS tokens
For O&M, product, developer, and other employees who need an AccessKey for O&M, management, or local development and debugging, you should assume a RAM role to obtain an STS token instead of using an AccessKey. For more information, see Solution for using temporary credentials for local development and O&M.
You can use role-based SSO to obtain an STS token.
For single-account scenarios, you can configure SAML-based role-based SSO within your Alibaba Cloud account and assign RAM roles to employees based on their job functions. Employees can then use the saml2alibabacloud tool on the command line to authenticate with the identity provider (IdP) and obtain an STS token.
For multi-account scenarios, you can use CloudSSO to configure single sign-on for multiple accounts. After the configuration is complete, employees can use the CLI to log on to CloudSSO and access Alibaba Cloud resources.
For third-party vendors who need to perform local development and debugging, if their application is deployed in their own cloud environment or they have their own Alibaba Cloud account, you can create a RAM role for them. The vendor can then assume this role from their Alibaba Cloud account to obtain an STS token. For more information, see Create a RAM role for a trusted Alibaba Cloud account. Note the following:
Assign different credentials to different workloads and environments. Do not reuse this RAM role. You should provide it to the vendor only for local debugging.
Grant permissions based on the principle of least privilege. The permissions that you grant to the RAM role must follow the principle of least privilege.
For other scenarios where you do not use SSO or where access with an STS token is not supported, you may still need to use a static AccessKey. If you use a static AccessKey, you must follow these guidelines to ensure its security.
Centrally manage static credentials. You should prevent personnel from directly handling credentials to reduce the risk of leaks.
Assign different credentials to different workloads and environments. You should use this AccessKey only for the employee's local tasks. Do not share it among employees or across different scenarios.
Grant permissions based on the principle of least privilege. You should grant only the minimum permissions required to use the AccessKey.
You can harden the security of the AccessKey by setting a network access restriction policy to allow calls only from your internal corporate environment. For more information, see AccessKey network access restriction policy.
Use STS tokens in applications deployed on the cloud
You can choose the appropriate solution to use STS token credentials based on how your application is deployed on the cloud:
For applications deployed on ECS instances, you can use an . For more information, see Obtain and use temporary credentials with an ECS instance role. You can attach a RAM role to the instance. The application can then obtain an STS token through the instance RAM role.
For applications deployed on Container Service for Kubernetes (ACK), a cluster can host multiple services. A single container node might contain pods from different services. In a multi-tenant scenario, if an untrusted service is deployed, it can directly access the ECS metadata service to obtain the STS token of the instance RAM role attached to the worker node. This can cause an identity and permission leak. To , see Obtain and use temporary credentials with Container Service RRSA. This lets you assume a role at the pod level to obtain an STS token.
For serverless applications deployed in Function Compute, you can see Obtain and use temporary credentials using an FC function role.
Regardless of the deployment method, you can use an official Alibaba Cloud software development kit (SDK) in your application code. By setting the appropriate credential configuration based on the deployment type, you can easily obtain an STS token without managing credential caching or expiration and renewal logic.
For enterprises that use multiple Alibaba Cloud accounts and require cross-account access, see Obtain and use temporary credentials for cross-account access using role assumption. You should use an STS token instead of an AccessKey to prevent security issues caused by AccessKey leaks.
Use STS tokens in mobile applications
For mobile apps that access Alibaba Cloud APIs from the client, we also recommend using STS tokens. This avoids placing an AccessKey on the client, where it could be reverse-engineered or decompiled by hackers to expose the plaintext AccessKey.
Common mobile scenarios involve accessing OSS, such as uploading files to or downloading files from OSS. We strongly recommend that you use the solution in Implement direct upload to and download from OSS on a client using presigned URLs.
For other mobile scenarios, such as accessing Simple Log Service (SLS) to upload logs or using middleware, your server can issue a temporary STS token to the mobile client for accessing cloud resources. For more information, see Obtain and use temporary credentials with a Token Vending Machine (TVM).
Note that you must grant permissions based on the principle of least privilege. The permissions of the RAM role must follow this principle. In the Obtain and use temporary credentials with a Token Vending Machine (TVM) solution, you must call the AssumeRole API to obtain an STS token. When you call this API, you can use the `Policy` parameter to narrow the scope of permissions for the session. For example, for an upload request, you can generate a session policy based on request characteristics, such as the user ID, request ID, and access path, to further restrict the resources that the STS token can operate on. This method prevents an attacker who hijacks a client request and leaks the STS token from gaining the full permissions of the RAM role.
Emergency response to an STS token leak
Although an STS token is a temporary ID token, its validity period can range from 15 minutes to 12 hours, depending on the maximum session duration of its corresponding role. If an STS token is leaked, it still poses a significant security risk. You can revoke all issued STS tokens by performing the following steps.
Log on to the RAM console using your Alibaba Cloud account.
Delete the RAM role. After you delete the RAM role, all unexpired STS tokens obtained by assuming this role are immediately invalidated.
If you still need the role, you can re-create a role with the same name and grant it the same access policies after you resolve the root cause of the leak. You can then use the new role to continue your tasks.
Related resources
Video tutorials
Best practices for using temporary credentials in cloud applications
Related practices
For scenarios where you cannot use an STS token, you can centrally manage AccessKeys. For more information, see Centrally manage static credentials.
Related solutions
Obtain and use temporary credentials with an ECS instance role
Obtain and use temporary credentials with Container Service RRSA
Obtain and use temporary credentials with an FC function role
Implement direct upload to and download from OSS on a client using presigned URLs
Obtain and use temporary credentials with a Token Vending Machine (TVM)
Obtain and use temporary credentials for cross-account access using role assumption
Solution for using temporary credentials for local development and O&M