Use temporary credentials instead of static credentials

Updated at:
Copy as MD

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.

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.

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.

  1. Log on to the RAM console using your Alibaba Cloud account.

  2. Remove all access policies from the RAM role.

  3. 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

Related solutions