Alibaba Cloud Assume Role
Guide to assume roles in Alibaba Cloud using OpenID Connect and Web Identity Federation
This guide will walk you through the process of authenticating with Alibaba Cloud services using Identity Federation. By following these steps, you can obtain temporary security credentials without embedding long-term credentials in your applications.
The temporary security credentials returned by the alicloud API consist of an access key ID, a secret access key, and a security token. Applications can use these temporary security credentials to securely access Alibaba Cloud Resources.
Prerequisites
- An active client in usegrant.dev
- An Alibaba Cloud account
- Alibaba Cloud CLI installed on your system
Before proceeding, you must plan your security strategy to ensure that access tokens are only allocated in a predictable way. To control how your cloud provider issues access tokens, you must define at least one condition to your openid connect provider, so that untrusted entities can't request access tokens for your cloud resources.
Step 1: Set Up an OpenID Connect Provider in Alibaba Cloud
- Log in to the Alibaba Cloud Management Console and navigate to the RAM service.
- In the sidebar, select "Integrations -> SSO".
- Select "OIDC -> IDP" under "Role based SSO".
- Fill in the required details and "Create".
For the issuer URL use the url of your project or the custom domain you set up.
Ensure that the audience matches the one used to create the client in usegrant.dev.
For Alibaba Cloud, the audience can be ram.aliyuncs.com
or the audience you used while creating the client in usegrant.dev.
Refer to the Alibaba Cloud documentation for more details and options.
Step 2: Create a Role in Alibaba Cloud
- Log in to the Alibaba Cloud Management Console and navigate to the RAM service.
- In the sidebar, select "Identities -> Roles".
- On the Create Role page, select IdP in the Select Trusted Entity section and click Next.
- Specify the RAM Role Name and Note parameters.
- Select OIDC for the IdP Type parameter.
- Select a trusted IdP and specify the conditions.
- Audience: Select the audience you used while creating the idp in previous step.
- Subject: Enter the subject generated while creating the client in useGrant
- Click Create.
Refer to the Alibaba Cloud documentation for more details and options.
Step 3: Assume Role with Alibaba Cloud CLI
Run the following command to assume the role:
This would return the temporary security credentials with AccessKeyId, SecretAccessKey and SessionToken. You can use these temporary credentials to access Alibaba Cloud Resources depending on the policy attached to the role.
Alibaba expects the JWT to have audience as an array and the token header type to be jwt
instead of at+jwt
.
Pass the following options to the create access token REST API,
so the JWT generated is compatible with Alibaba Cloud.
audienceAsArray
useJwtType
For example,
You can also interactively test the AliCloud API's on Alibaba OpenApi Explorer.
By default, the temporary security credentials created by AssumeRoleWithOIDC last for one hour. However, you can use the optional DurationSeconds parameter to specify the duration of your session. You can provide a value from 900 seconds (15 minutes) up to the maximum session duration setting for the role. This setting can have a value from 1 hour to 12 hours.
Learn more about the AssumeRoleWithOIDC API from the official Alibaba Cloud documentation.
Using ROS Template
You can also use ROS(Resource Orchestration Service) to create the role and the trust policy instead of manually creating them as shown in the previous steps.
ROS doesn't support creating OIDC providers using JSON/YAML format use the following terraformtemplate to create the OIDC provider.
Example ROS template:
You can also use the ALIYUN::ROS::Stack
to inline the terraform template inside your YAML/JSON ROS template.
Copy the template and replace the placeholders like PROVIDER_NAME
, AUDIENCE
, PROVIDER_URL
, SUBJECT_FROM_CLIENT
with your own values.
PROVIDER_NAME
is the name you want to give to the provider.AUDIENCE
can bests.aliyuncs.com
orram.aliyuncs.com
. But you can also whatever the one you used while creating the client in useGrant.dev.PROVIDER_URL
is the url of your project or the custom domain you set up.SUBJECT_FROM_CLIENT
is the subject from the client, this will be generated after you create the client.- Replace the
Fingerprint
with the fingerprint of your project. You can retrieve the fingerprint from the project domain section.