Skip to content

sts:AssumeRoleWithWebIdentity and keycloak oidc trust policy to filter by role access

0
  1. I have created OIDC iDP
  2. I have created role with the following trust policy
  3. It always fails: Failed to assume role arn:aws:iam::111:role/keycloak-role: AccessDenied - Not authorized to perform sts:AssumeRoleWithWebIdentity
  4. If I remove 2nd (bool) condition - everything works properly.
{
	"Version": "2012-10-17",
	"Statement": [
		{
			"Effect": "Allow",
			"Principal": {
				"Federated": "arn:aws:iam::1111:oidc-provider/keycloak.site.com/realms/test-realm"
			},
			"Action": "sts:AssumeRoleWithWebIdentity",
			"Condition": {
				"StringEquals": {
					"keycloak.site.com/realms/test-realm:aud": "client-aws"
				},
				"Bool": {
					"keycloak.site.com/realms/test-realm:email_verified": "true"
				}
			}
		}
	]
}

So it seems that current IAM implementation allows me to use only

  • aud
  • sub
  • email

fields to filter the access and nothing else.

Questions

  1. So I have roles within the keycloak client, how do I filter access based on roles assigned? They are exported in the access and ID tokens, but nothing has worked for me so far.
  2. I have a conclusion that if I need to provide AssumeRoleWithWebIdentity for the role - I need to have N clients (i.e. one per group of users), which is confusing to manage.

Am I missing something?

I have groups

  • devops
  • developers

and they should be able to assume different IAM roles via sts:AssumeRoleWithWebIdentity and I hoped to filter their access at the trust policy level, by filtering with something like this

"Condition": {
    "StringEquals": {
        "keycloak.site.com/realms/test-realm:aud": "client-aws"
    },
    "ForAnyValue:StringEquals": {
        "keycloak.site.com/realms/test-realm:roles": [
            "devops-role"
        ]
    }
}
1 Answer
0

Based on the information provided, it appears you're facing some limitations with the IAM trust policy for OIDC federation using Keycloak. You're correct that the current IAM implementation for web identity federation primarily allows filtering based on 'aud', 'sub', and 'email' claims.

For your specific questions:

  1. Unfortunately, filtering access based on Keycloak roles directly in the IAM trust policy is not supported out-of-the-box. The IAM service doesn't have built-in support for custom claims like roles from Keycloak in the trust policy.

  2. Your conclusion about needing multiple clients for different groups of users is a common workaround, although it can indeed be cumbersome to manage.

However, there are a few alternative approaches you could consider:

  1. Use the 'sub' claim: If your Keycloak setup allows for unique 'sub' claims for different roles or groups, you could use that in your trust policy.

  2. Implement a custom solution: You could create a Lambda function that acts as an intermediary. This function could verify the Keycloak token, check the roles, and then assume the appropriate AWS role.

  3. Use Amazon Cognito: If possible, you could integrate Keycloak with Amazon Cognito. Cognito has more flexible support for mapping custom attributes to IAM roles.

  4. Use AWS SSO (Single Sign-On): If feasible for your use case, AWS SSO provides more granular control over role assignments based on identity provider attributes.

While these aren't direct solutions to using Keycloak roles in the IAM trust policy, they offer alternative ways to achieve role-based access control with OIDC federation. The limitation you've encountered is a known constraint of the current IAM implementation for web identity federation.
Sources
AssumeRoleWithWebIdentity - AWS Security Token Service
IAM and AWS STS condition context keys - AWS Identity and Access Management

answered 2 years ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.