fbpx

How to enforce creation of roles in a specific path: Use IAM role naming in hierarchy models

An AWS Identity and Access Management (IAM) role is an IAM identity that you create in your AWS account that has specific permissions. An IAM role is similar to an IAM user because it’s an AWS identity with permission policies that determine what the identity can and cannot do on AWS. However, as outlined in security best practices in IAM, AWS recommends that you use IAM roles instead of IAM users. An IAM user is uniquely associated with one person, while a role is intended to be assumable by anyone who needs it. An IAM role doesn’t have standard long-term credentials such as a password or access keys associated with it. Instead, when you assume a role, it provides you with temporary security credentials for your role session that are only valid for certain period of time.

   <p>This blog post explores the effective implementation of security controls within IAM roles, placing a specific focus on the IAM role’s <a href="https://aws.amazon.com/blogs/security/optimize-aws-administration-with-iam-paths/" target="_blank" rel="noopener">path feature</a>. By organizing IAM roles hierarchically using paths, you can address key challenges and achieve practical solutions to enhance IAM role management.</p> 
   <h2>Benefits of using IAM paths</h2> 
   <p>A fundamental benefit of using paths is the establishment of a clear and organized organizational structure. By using paths, you can handle diverse use cases while creating a well-defined framework for organizing roles on AWS. This organizational clarity can help you navigate complex IAM setups and establish a cohesive structure that’s aligned with your organizational needs.</p> 
   <p>Furthermore, by enforcing a specific structure, you can gain precise control over the scope of permissions assigned to roles, helping to reduce the risk of accidental assignment of overly permissive policies. By assisting in preventing inadvertent policy misconfigurations and assisting in coordinating permissions with the planned organizational structure, this proactive solution improves security. This approach is highly effective when you consistently apply established naming conventions to paths, role names, and policies. Enforcing a uniform approach to role naming enhances the standardization and efficiency of IAM role management. This practice fosters smooth collaboration and reduces the risk of naming conflicts.</p> 
   <h2>Path example</h2> 
   <p>In IAM, a role path is a way to organize and group IAM roles within your AWS account. You specify the role path as part of the role’s Amazon Resource Name (ARN).</p> 
   <p>As an example, imagine that you have a group of IAM roles related to development teams, and you want to organize them under a path. You might structure it like this:</p> 
   <p>Role name: Dev App1 admin<br>Role path: <span>/D1/app1/admin/</span><br>Full ARN: <span>arn:aws:iam::123456789012:role/D1/app1/admin/DevApp1admin</span></p> 
   <p>Role name: Dev App2 admin<br>Role path: <span>/D2/app2/admin/</span><br>Full ARN: <span>arn:aws:iam::123456789012:role/D2/app2/admin/DevApp2admin</span></p> 
   <p>In this example, the IAM roles DevApp1admin and DevApp2admin are organized under two different development team paths: D1/app1/admin and D2/app2/admin, respectively. The role path provides a way to group roles logically, making it simpler to manage and understand their purpose within the context of your organization.</p> 
   <h2>Solution overview</h2> 
   <div id="attachment_33310" class="wp-caption aligncenter"> 
    <img aria-describedby="caption-attachment-33310" src="https://infracom.com.sg/wp-content/uploads/2024/02/img1_v2.png" alt="Figure 1: Sample architecture" width="846" height="589" class="size-full wp-image-33310"> 
    <p id="caption-attachment-33310" class="wp-caption-text">Figure 1: Sample architecture</p> 
   </div> 
   <p>The sample architecture in Figure 1 shows how you can separate and categorize the enterprise roles and development team roles into a hierarchy model by using a path in an IAM role. Using this hierarchy model, you can enable several security controls at the level of the <a href="https://docs.aws.amazon.com/organizations/latest/userguide/orgs_manage_policies_scps.html" target="_blank" rel="noopener">service control policy (SCP)</a>, IAM policy, permissions boundary, or the pipeline. I recommend that you avoid incorporating business unit names in paths because they could change over time.</p> 
   <p>Here is what the IAM role path looks like as an ARN:</p> 
   <div class="hide-language"> 
    <pre class="unlimited-height-code"><code>arn:aws:iam::123456789012:role/EnT/iam/adm/IAMAdmin</code></pre> 
   </div> 
   <p>In this example, in the resource name, <span>/EnT/iam/adm/</span> is the role path, and <span>IAMAdmin</span> is the role name.</p> 
   <p>You can now use the role path as part of a policy, such as the following:</p> 
   <div class="hide-language"> 
    <pre class="unlimited-height-code"><code>arn:aws:iam::123456789012:role/EnT/iam/adm/*</code></pre> 
   </div> 
   <p>In this example, in the resource name, <span>/EnT/iam/adm/</span> is the role path, and <span>*</span> indicates any IAM role inside this path.</p> 
   <h2>Walkthrough of examples for preventative controls</h2> 
   <p>Now let’s walk through some example use cases and SCPs for a preventative control that you can use based on the path of an IAM role.</p> 
   <h3>PassRole preventative control example</h3> 
   <p>The following SCP denies <a href="https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_policies_examples_iam-passrole-service.html" target="_blank" rel="noopener">passing a role</a> for enterprise roles, except for roles that are part of the IAM admin hierarchy within the overall enterprise hierarchy.</p> 
   <div class="hide-language"> 
    <pre class="unlimited-height-code"><code class="lang-text">     {
"Version": "2012-10-17",
"Statement": [
    {
        "Sid": "DenyEnTPassRole",
        "Effect": "Deny",
        "Action": "iam:PassRole",
        "Resource": "<span>arn:aws:iam::*:role/EnT/*</span>",
        "Condition": {
            <span>"ArnNotLike": {</span>
                <span>"aws:PrincipalArn": "arn:aws:iam::*:role/EnT/fed/iam/*"</span>
            }
        }
    }
]

}

   With just a couple of statements in the SCP, this preventative control helps provide protection to your high-privilege roles for enterprise roles, regardless of the role’s name or current status.</p> 
   <p>This example uses the following paths:</p> 
   <ul> 
    <li><span>/EnT/</span> — enterprise roles (roles owned by the central teams, such as cloud center of excellence, central security, and networking teams)</li> 
    <li><span>/fed/</span> — federated roles, which have interactive access</li> 
    <li><span>/iam/</span> — roles that are allowed to perform IAM actions, such as CreateRole, AttachPolicy, or DeleteRole</li> 
   </ul> 
   <h3>IAM actions preventative control example</h3> 
   <p>The following SCP restricts IAM actions, including CreateRole, DeleteRole, AttachRolePolicy, and DetachRolePolicy, on the enterprise path.</p> 
   <div class="hide-language"> 
    <pre class="unlimited-height-code"><code class="lang-text">{
"Version": "2012-10-17",
"Statement": [
    {
        "Sid": "DenyIAMActionsonEnTRoles",
        "Effect": "Deny",
        "Action": [
            "iam:CreateRole",
            "iam:DeleteRole",
            "iam:DetachRolePolicy",
            "iam:AttachRolePolicy"
        ],
        "Resource": "<span>arn:aws:iam::<em>:role/EnT/</em></span>",
        "Condition": {
            "ArnNotLike": {
                <span>"aws:PrincipalArn": "arn:aws:iam::<em>:role/EnT/fed/iam/</em>"</span>
            }
        }
    }
]

}

This preventative control denies an IAM role that is outside of the enterprise hierarchy from performing the actions CreateRole, DeleteRole, DetachRolePolicy, and AttachRolePolicy in this hierarchy. Every IAM role will be denied those API actions except the one with the path as arn:aws:iam:::role/EnT/fed/iam/

The example uses the following paths:

  • /EnT/ — enterprise roles (roles owned by the central teams, such as cloud center of excellence, central security, or network automation teams)
  • /fed/ — federated roles, which have interactive access
  • /iam/ — roles that are allowed to perform IAM actions (in this case, CreateRole, DeteleRole, DetachRolePolicy, and AttachRolePolicy)

IAM policies preventative control example

The following SCP policy denies attaching certain high-privilege AWS managed policies such as AdministratorAccess outside of certain IAM admin roles. This is especially important in an environment where business units have self-service capabilities.

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "RolePolicyAttachment",
            "Effect": "Deny",
            "Action": "iam:AttachRolePolicy",
            "Resource": "arn:aws:iam:::role/EnT/fed/iam/",
            "Condition": {
                "ArnNotLike": {
                    "iam:PolicyARN": "arn:aws:iam::aws:policy/AdministratorAccess"
                }
            }
        }
    ]
}

AssumeRole preventative control example

The following SCP doesn’t allow non-production roles to assume a role in production accounts. Make sure to replace and with your own information.

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "DenyAssumeRole",
            "Effect": "Deny",
            "Action": "sts:AssumeRole",
            "Resource": "",
            "Condition": {
                "StringLike": {
                    "aws:PrincipalArn": "arn:aws:iam:::role/np/"
                },
                "ForAnyValue:StringLike": {
                    "aws:ResourceOrgPaths": "/r-xxxx//"
                }
            }
        }
    ]
}

This example uses the /np/ path, which specifies non-production roles. The SCP denies non-production IAM roles from assuming a role in the production organizational unit (OU) (in our example, this is represented by /r-xxxx//). Depending on the structure of your organization, the ResourceOrgPaths will have one of the following formats:

  • “o-a1b2c3d4e5/”
  • “o-a1b2c3d4e5/r-ab12/ou-ab12-11111111/*”
  • “o-a1b2c3d4e5/r-ab12/ou-ab12-11111111/ou-ab12-22222222/”

Walkthrough of examples for monitoring IAM roles (detective control)

Now let’s walk through two examples of detective controls.

AssumeRole in CloudTrail Lake

The following is an example of a detective control to monitor IAM roles in AWS CloudTrail Lake.

SELECT
    userIdentity.arn as "Username", eventTime, eventSource, eventName, sourceIPAddress, errorCode, errorMessage
FROM

WHERE
    userIdentity.arn IS NOT NULL
    AND eventName = 'AssumeRole'
    AND userIdentity.arn LIKE '%/np/%'
    AND errorCode = 'AccessDenied'
    AND eventTime > '2023-07-01 14:00:00'
    AND eventTime < '2023-11-08 18:00:00';

This query lists out AssumeRole events for non-production roles in the organization for AccessDenied errors. The output is stored in an Amazon Simple Storage Service (Amazon S3) bucket from CloudTrail Lake, from which the csv file can be downloaded. The following shows some example output:

Username,eventTime,eventSource,eventName,sourceIPAddress,errorCode,errorMessage
arn:aws:sts::123456789012:assumed-role/np/test,2023-12-09 10:35:45.000,iam.amazonaws.com,AssumeRole,11.11.113.113,AccessDenied,User: arn:aws:sts::123456789012:assumed-role/np/test is not authorized to perform: sts:AssumeRole on resource: arn:aws:iam::123456789012:role/hello because no identity-based policy allows the sts:AssumeRole action

You can modify the query to audit production roles as well.

CreateRole in CloudTrail Lake

Another example of a CloudTrail Lake query for a detective control is as follows:

SELECT
    userIdentity.arn as "Username", eventTime, eventSource, eventName, sourceIPAddress, errorCode, errorMessage
FROM

WHERE
    userIdentity.arn IS NOT NULL
    AND eventName = 'CreateRole'
    AND userIdentity.arn LIKE '%/EnT/fed/iam/%'
    AND eventTime > '2023-07-01 14:00:00'
    AND eventTime < '2023-11-08 18:00:00';

This query lists out CreateRole events for roles in the /EnT/fed/iam/ hierarchy. The following are some example outputs:

Username,eventTime,eventSource,eventName,sourceIPAddress,errorCode,errorMessage

arn:aws:sts::123456789012:assumed-role/EnT/fed/iam/security/test,2023-12-09 16:31:11.000,iam.amazonaws.com,CreateRole,10.10.10.10,AccessDenied,User: arn:aws:sts::123456789012:assumed-role/EnT/fed/iam/security/test is not authorized to perform: iam:CreateRole on resource: arn:aws:iam::123456789012:role/EnT/fed/iam/security because no identity-based policy allows the iam:CreateRole action

arn:aws:sts::123456789012:assumed-role/EnT/fed/iam/security/test,2023-12-09 16:33:10.000,iam.amazonaws.com,CreateRole,10.10.10.10,AccessDenied,User: arn:aws:sts::123456789012:assumed-role/EnT/fed/iam/security/test is not authorized to perform: iam:CreateRole on resource: arn:aws:iam::123456789012:role/EnT/fed/iam/security because no identity-based policy allows the iam:CreateRole action

Because these roles can create additional enterprise roles, you should audit roles created in this hierarchy.

Important considerations

When you implement specific paths for IAM roles, make sure to consider the following:

  • The path of an IAM role is part of the ARN. After you define the ARN, you can’t change it later. Therefore, just like the name of the role, consider what the path should be during the early discussions of design.
  • IAM roles can’t have the same name, even on different paths.
  • When you switch roles through the console, you need to include the path because it’s part of the role’s ARN.
  • The path of an IAM role can’t exceed 512 characters. For more information, see IAM and AWS STS quotas.
  • The role name can’t exceed 64 characters. If you intend to use a role with the Switch Role feature in the AWS Management Console, then the combined path and role name can’t exceed 64 characters.
  • When you create a role through the console, you can’t set an IAM role path. To set a path for the role, you need to use automation, such as AWS Command Line Interface (AWS CLI) commands or SDKs. For example, you might use an AWS CloudFormation template or a script that interacts with AWS APIs to create the role with the desired path.

Conclusion

By adopting the path strategy, you can structure IAM roles within a hierarchical model, facilitating the implementation of security controls on a scalable level. You can make these controls effective for IAM roles by applying them to a path rather than specific roles, which sets this approach apart.

This strategy can help you elevate your overall security posture within IAM, offering a forward-looking solution for enterprises. By establishing a scalable IAM hierarchy, you can help your organization navigate dynamic changes through a robust identity management structure. A well-crafted hierarchy reduces operational overhead by providing a versatile framework that makes it simpler to add or modify roles and policies. This scalability can help streamline the administration of IAM and help your organization manage access control in evolving environments.

If you have feedback about this post, submit comments in the Comments section below. If you have questions about this post, start a new thread on the AWS Security, Identity, & Compliance re:Post or contact AWS Support.

Want more AWS Security news? Follow us on Twitter.

   <!-- '"` -->