Sub topic 15
Attribute-based access control with session tags
This chapter shows how to use attributes of a user, such as department or cost center, to decide what that user can do in AWS. One permission set then covers many teams. It assumes you know IAM policy JSON and condition keys, have built a permission set and an account assignment in earlier chapters, and can run aws sso-admin commands.
Why ABAC with permission sets
A group-based design needs one group and one assignment for each team and account. Suppose three departments each need to manage only their own EC2 instances. You would end up with three permission sets that differ only by a tag value.
Attribute-based access control (ABAC) removes that duplication. It compares a tag on the user's session with a tag on the resource. A single permission set says "you may act on resources whose Department tag equals your own Department". Adding a fourth department then needs no new permission set and no new policy. You add a user with the right attribute and tag the resources.
How attributes reach a session
Identity Center turns user attributes into session tags. A session tag is a key-value pair attached to a role session when the role is assumed. Inside the session, IAM exposes it as the condition key aws:PrincipalTag/<key>.
The flow has four steps:
- Your identity source holds the attribute. This can be the Identity Center directory, Active Directory, or an external IdP that syncs users by SCIM.
- You enable "attributes for access control" and map the attributes you want to tag keys.
- When a user opens an account and permission set from the access portal, Identity Center assumes the provisioned role and attaches the mapped tags.
- IAM evaluates the permission set's policies with those tags available as
aws:PrincipalTag/....
Configure the attribute mapping
The mapping is a property of the Identity Center instance, not of a permission set. Each entry has a tag key and a source, which is a path into the user's identity store record.
INSTANCE_ARN=$(aws sso-admin list-instances --query 'Instances[0].InstanceArn' --output text)
aws sso-admin create-instance-access-control-attribute-configuration \
--instance-arn "$INSTANCE_ARN" \
--instance-access-control-attribute-configuration '{
"AccessControlAttributes": [
{"Key": "Department", "Value": {"Source": ["${path:enterprise.department}"]}},
{"Key": "CostCenter", "Value": {"Source": ["${path:enterprise.costCenter}"]}}
]
}'
aws sso-admin describe-instance-access-control-attribute-configuration \
--instance-arn "$INSTANCE_ARN"The second command returns the stored mapping and its status, so use it to confirm the change. To change an existing mapping, use update-instance-access-control-attribute-configuration. Run these in the management account or a delegated administrator account, as with other Identity Center calls.
The same mapping as CloudFormation:
AWSTemplateFormatVersion: '2010-09-09'
Parameters:
InstanceArn:
Type: String
Resources:
AbacAttributes:
Type: AWS::SSO::InstanceAccessControlAttributeConfiguration
Properties:
InstanceArn: !Ref InstanceArn
AccessControlAttributes:
- Key: Department
Value:
Source:
- "${path:enterprise.department}"
- Key: CostCenter
Value:
Source:
- "${path:enterprise.costCenter}"The ${path:...} values are literal Identity Center expressions. They are not CloudFormation substitutions, so do not wrap them in Fn::Sub.
Check these points about the source attributes:
- With an external IdP, the attribute must exist on the user in the Identity Center identity store. That normally means your SCIM provisioning sends it. Changing a field in the IdP alone does not help if it never syncs.
- With the Identity Center directory, someone must fill in the user's attribute fields.
- With Active Directory, map the AD attribute into the identity store first, then reference it.
Write the permission policy
The inline policy below allows starting and stopping only instances whose Department tag matches the user's session tag.
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": ["ec2:StartInstances", "ec2:StopInstances"],
"Resource": "*",
"Condition": {
"StringEquals": {
"ec2:ResourceTag/Department": "${aws:PrincipalTag/Department}"
}
}
}
]
}Attach it to a permission set and push it to the accounts:
aws sso-admin put-inline-policy-to-permission-set \
--instance-arn "$INSTANCE_ARN" \
--permission-set-arn "$PS_ARN" \
--inline-policy file://department-ec2.json
aws sso-admin provision-permission-set \
--instance-arn "$INSTANCE_ARN" \
--permission-set-arn "$PS_ARN" \
--target-type ALL_PROVISIONED_ACCOUNTSProvisioning is required because the policy lives in a role in each account, as covered in the provisioning chapter. The attribute mapping does not need a re-provision. It applies to the next session a user starts.
Behavior and edge cases
- Missing attribute. If a user has no
Departmentvalue, the tag is not set. The policy variable cannot resolve, the condition does not match, and the Allow does not apply. The result is a denial, which is a safe default. - Missing resource tag. An instance without a
Departmenttag never matches. Untagged resources are unreachable by this statement. - Case.
StringEqualscompares values case-sensitively.financeandFinanceare different. Standardize values in the IdP, or useStringEqualsIgnoreCasedeliberately. - Session freshness. Tags are fixed when the session starts. If someone moves departments, their existing session keeps the old tag until it expires or they sign in again. A short session duration limits this window.
- Multi-valued attributes. A tag holds one string. Do not map a list-valued attribute and expect set matching.
- Not every action supports tag conditions.
ec2:ResourceTagworks forStartInstancesandStopInstances, but some actions and services do not support resource tags in conditions. Check the service authorization reference for each action before you rely on it.
Protect the tags themselves
ABAC is only as strong as who can change the tags. If a user can edit a resource's Department tag, they can pull any resource into their own department. If they can edit their own attribute in the directory, they can change their own access. Apply these controls:
- Limit who can edit user attributes in the identity source. Treat HR-sourced attributes as authoritative.
- Deny tag changes on the key in question to everyone except a tagging role. Use an IAM or SCP deny on
ec2:CreateTagsandec2:DeleteTags, with a condition onaws:TagKeysand an exception for the role that manages tags. - Require the tag at creation. Add a condition so
ec2:RunInstancesis allowed only whenaws:RequestTag/Departmentequals${aws:PrincipalTag/Department}.
Verify the setup
- Sign in as a user in Finance, with the permission set assigned to their group.
- Run
aws ec2 stop-instancesagainst an instance taggedDepartment=Finance. It should succeed. - Run it against an instance tagged
Department=Marketing. It should fail withUnauthorizedOperation. - If the first call fails, check three things in order: the mapping with
describe-instance-access-control-attribute-configuration, the user's attribute in the identity store, and the resource tag value (spelling and case).
Exercise
A user in the Finance department stops an instance tagged Department=Finance successfully on Monday. On Tuesday HR changes the user's department to Marketing in the IdP, and the change syncs to Identity Center. The user's session from Monday is still open. Can they still stop the Finance-tagged instance, and what do they need to do to be evaluated as Marketing?
Show answer
Yes, they can still stop it until the session ends. Session tags are set when the role is assumed and do not update mid-session, so the session still carries Department=Finance. The user must sign in again through the access portal or aws sso login. The new session is tagged Department=Marketing, and the Finance instance is then denied.
Exercise
You enabled the attribute mapping for Department, but every ec2:StopInstances call fails for users whose IdP shows a department. Name two likely causes you would check first.
Show answer
First, the attribute may not exist in the Identity Center identity store, for example because SCIM does not sync the enterprise department field, so no tag is set. Second, the resource tag value may not exactly match the attribute value, for example because of different case or spelling, because StringEquals is case-sensitive. A third cause is that the mapped Source path does not match the real attribute name.