RAM Roles for Service Accounts (RRSA) allows you to implement pod-level OpenAPI permission isolation within your cluster. This provides fine-grained control over access to cloud resources and reduces security risks. This topic describes how to use the RRSA feature.
Background
OIDC (OpenID Connect) is an authentication protocol built on top of OAuth 2.0. Alibaba Cloud RAM supports OIDC-based role SSO. Applications within a Kubernetes cluster use temporary credentials from Security Token Service (STS) to access cloud resource APIs. Alibaba Cloud Container Service (ACS) supports RRSA. In multi-tenant scenarios, RRSA provides fine-grained permission isolation for cloud resource access between application pods in a cluster. In ACS, RRSA also allows you to control the validity period of temporary credentials.
RRSA works as follows:
-
You deploy an application pod that uses the service account token volume projection feature.
NoteThe service account token volume projection feature is enabled by default in ACS clusters.
-
The cluster creates and mounts the corresponding service account OIDC token file for the application pod.
-
The application in the pod uses the mounted OIDC token file to call the STS
AssumeRoleWithOIDCAPI to obtain a temporary credential for a specified RAM role.NoteYou must modify the trust policy of the RAM role in advance to allow the pod's service account to assume the RAM role. For more information, see AssumeRoleWithOIDC.
-
The application in the pod uses the obtained temporary credential to access cloud resource APIs.
Enable RRSA
-
Log on to the ACS console. In the left navigation pane, click Clusters.
-
On the Clusters page, click the name of the target cluster. In the left navigation pane, choose Cluster Information.
-
On the cluster details page, click the Basic Information tab. In the Security and Auditing section, click Enable next to RRSA OIDC.
-
In the Enable RRSA dialog box, click OK.
Note-
Enabling the RRSA feature takes about 2 to 3 minutes. If the Cluster Status remains in the Updating state, click the
button in the upper-right corner of the page to refresh. -
When the cluster status changes from Updating to Running, the RRSA feature is enabled for the cluster. You can hover over Enabled next to RRSA OIDC to view the URL and ARN of the OIDC provider.
-
After enabling the RRSA feature for a cluster, the ACS console performs the following operations in the background:
-
It automatically creates a dedicated OIDC Issuer service for the cluster. This service is managed by ACS and does not require manual maintenance.
-
It creates an OIDC identity provider in your account that uses this OIDC Issuer. The OIDC identity provider is named
ack-rrsa-<cluster_id>, where<cluster_id>is your cluster ID.
Use RRSA
After you enable the RRSA feature, you can follow these steps to allow applications in your cluster to obtain temporary credentials to access cloud resource APIs by using RRSA.
Create and authorize a new RAM role
To use an existing RAM role instead of creating a new one, you can add the required permissions to the existing RAM role.
The application deployed in this example uses the RRSA feature to assume a specified role and retrieve a list of clusters in the current account. The main resources used are as follows:
-
Namespace:
rrsa-demo. -
Service account:
demo-sa. -
RAM role:
demo-role-for-rrsa.
-
Create a RAM role named
demo-role-for-rrsa.-
Log on to the RAM console by using your Alibaba Cloud account.
-
In the left-side navigation pane, choose . On the Roles page, click Create Role.
-
In the Create Role panel, click Switch to Policy Editor to create a role.
-
In the Visual Editor tab, configure the role information as described in the following table and then click OK.
Parameter
Description
Effect
The default value is Allow.
Principal
Select Identity Provider and click Edit.
Set Identity Provider Type to OIDC and select the OIDC identity provider of the cluster, such as
ack-rrsa-<cluster_id>.<cluster_id>is your cluster ID.Action
By default, sts:AssumeRole is selected.
Condition
-
oidc:iss: This condition is automatically added after you select the Identity Provider. Use the default setting.
-
oidc:aud: This condition is automatically added after you select the Identity Provider. Use the default setting.
-
oidc:sub: You need to manually Add Condition.
-
Key:
oidc:sub -
Operator:
StringEquals -
Value: The format is
system:serviceaccount:<namespace>:<serviceAccountName>.-
<namespace>: the namespace where the application resides. -
<serviceAccountName>: the name of the service account.
Based on the information about the test application, enter
system:serviceaccount:rrsa-demo:demo-sa. -
-
-
-
In the dialog box that appears, enter the role name
demo-role-for-rrsaand click OK.
-
-
Grant the created role the AliyunCSReadOnlyAccess system policy, which is required by the test application. For more information, see Manage permissions for a RAM role.
-
Deploy the test application.
-
Create a file named
demo.yamlwith the following content.NoteReplace the following placeholders in the sample application template.
-
<role_arn>: Replace this with the ARN of the RAM role that the application uses. You can obtain the ARN from the ARN field on the Basic Information page of the role on the RAM console. -
<oidc_provider_arn>: Replace this with the OIDC provider ARN of the cluster. You can obtain this ARN by hovering over Enabled next to RRSA OIDC on the Basic Information tab of the Cluster Information page.
-
-
Run the following command to deploy the test application.
kubectl apply -f demo.yamlAfter the application is deployed, the application can use the mounted OIDC token, the RAM role ARN, and the OIDC identity provider ARN to call the
AssumeRoleWithOIDCAPI of STS to obtain a temporary credential for the specified RAM role. The application then uses this temporary credential to access cloud resource APIs. For more information, see AssumeRoleWithOIDC.
-
-
Run the following command to view the logs of the test application.
kubectl -n rrsa-demo logs demoThe expected output is a list of ACK clusters:
20**/**/** 08:35:23 ======= [begin] list ACK clusters with RRSA ======= clusters: cluster id: cf***, cluster name: foo* cluster id: c8***, cluster name: bar* cluster id: c4***, cluster name: foob* 20**/**/** 08:35:24 ======= [end] list ACK clusters with RRSA ======= -
Optional: Detach the AliyunCSReadOnlyAccess system policy from the role. For more information, see Remove permissions from a RAM role.
Wait for about 30 seconds, and then run the following command to view the application logs again:
kubectl -n rrsa-demo logs demoA permission denied error is expected:
20**/**/** 10:09:33 ======= [begin] list ACK clusters with RRSA ======= 20**/**/** 10:09:33 SDKError: StatusCode: 403 Code: StatusForbidden Message: code: 403, STSToken policy Forbidden for action cs:DescribeClusters request id: XXXX Data: {"accessDeniedDetail":{"AuthAction":"cs:DescribeClusters","AuthPrincipalDisplayName":"demo-role-for-rrsa:ack-ram-tool","AuthPrincipalOwnerId":"XXXX","AuthPrincipalType":"AssumedRoleUser","EncodedDiagnosticMessage":"XXXX","NoPermissionType":"ImplicitDeny","PolicyType":"ResourceGroupLevelIdentityBasedPolicy"},"code":"StatusForbidden","message":"STSToken policy Forbidden for action cs:DescribeClusters","requestId":"XXXX","status":403,"statusCode":403}
Authorize an existing RAM role
If your application needs to use an existing RAM role instead of a new one, you can modify the trust policy of the RAM role by adding a statement to allow applications that use a specific service account to assume this RAM role and obtain temporary credentials. For more information, see Modify the trust policy of a RAM role.
The following code is an example Statement entry to add to the trust policy of the RAM role.
Replace the following placeholders in the example Statement entry.
-
<oidc_issuer_url>: Replace this with the URL of the OIDC provider for the cluster. You can obtain the URL by hovering over Enabled next to RRSA OIDC on the Basic Information tab of the Cluster Information page. -
<oidc_provider_arn>: Replace this with the OIDC provider ARN for the cluster. You can obtain the ARN by hovering over Enabled next to RRSA OIDC on the Basic Information tab of the Cluster Information page. -
<namespace>: Replace this with the namespace where the application resides. -
<service_account>: Replace this with the service account that the application uses.
{
"Action": "sts:AssumeRole",
"Condition": {
"StringEquals": {
"oidc:aud": "sts.aliyuncs.com",
"oidc:iss": "<oidc_issuer_url>",
"oidc:sub": "system:serviceaccount:<namespace>:<service_account>"
}
},
"Effect": "Allow",
"Principal": {
"Federated": [
"<oidc_provider_arn>"
]
}
}
References
-
Some official Alibaba Cloud SDKs provide built-in support for authentication by using RRSA OIDC tokens. The following table describes the SDK versions that support this feature and provides links to sample code.
Language
SDK version
Sample code
Go
Alibaba Cloud Credentials for Go 1.2.6 and later
Java
Alibaba Cloud Credentials for Java 0.2.10 and later
Python 3
Alibaba Cloud Credentials for Python 0.3.1 and later
Node.js and TypeScript
Alibaba Cloud Credentials for TypeScript/Node.js 2.2.6 and later
-
When you perform OIDC role-based SSO, you can call the AssumeRoleWithOIDC API to obtain a temporary credential to assume a RAM role.