The curious case of AWS IAM's policies
Tags: aws, security, cloud
Published: 2023-06-07 18:22:37

Do you know what the first thing you will encounter when you start to get to know AWS? Permissions! ⛔

As part of my recently found wisdom that _"If I want to learn something, I should teach it"_, in this article I'll try to write a concise guide about AWS IAM (Identity and Access Management service) and its permissions document AKA _policies_. 

(This piece is based on the [official AWS documentation](https://docs.aws.amazon.com/IAM/latest/UserGuide/access_policies.html) that talks about IAM policies. Consider it as its summary 😉)

## Table of contents
- [Intro](#intro)
- [Policy Types](#policy-types)
    - [Identity-based policies](#identitybased-policies)
    - [Resource-based policies](#resourcebased-policies)
    - [IAM permissions boundaries](#iam-permissions-boundaries)
    - [Service control policies (SCPs)](#service-control-policies-scps)
    - [Access control lists (ACLs)](#access-control-lists-acls)
    - [Session policies](#session-policies)
- [JSON policies structure](#json-policies-structure)

### Intro
I'll ask you a question, what's chaotic and big and don't have permissions? ... The Jungle 🦁🐒🦒
That's what will your AWS be without permissions! Even worse, it could (and probably would) cost you a fortune or make you lose business (or both)! 
AWS has its IAM service in place to manage your account's permissions and saves you from all that! How it does that? By using policies. And because I'm a visual learner, I've made this diagram to show you:

![policies](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dxti7yhvu9q7in7v0zwu.png)

Policies are attached to either an Identity or a Resources with permissions as `allow` or `deny` and are evaluate when a principle perform a request. They are JSON documents (except for ACLs) and consist of 6 types that we will discuss next.

### Policy Types
As I've mentioned, there are 6 types of IAM policies ranked from the most common to the least common as below:

![Types](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7yusc1clpi5asqsinlkg.png)
#### Identity-based policies
Those are the policies that you attach to an AWS identity (user, group or role). They can be managed or inline (one-to-one relation with an identity).

#### Resource-based policies
When the policy is attached to a resource like S3, it's call a "Resource-based policy". You must explicity state the principles in this case.

#### IAM permissions boundaries
Think of those as the guard-rails for your permissions. They set the maximum allowed permissions for an IAM identity but they don't give those permissions.

#### Service control policies (SCPs)
When you use "Organizations", Those can be applied to the organization member accounts or OUs (Organization Units). They can control the maximum set of permissions (like IAM permissions boundaries) for identities AND resources.

#### Access control lists (ACLs)
The only non-JSON policy type. Similar to Resource-based policies but that give permissions to principals outside of the current account and can't be used for principals inside the same account.

#### Session policies
When using the AWS CLI or AWS API, you can set advanced policies to assume a role or a federated user by using session policies.

And this is a summary for all the policy types:
<table>
<tr>
<th>Policy Type</th>
<th>Format</th>
<th>Applied To</th>
<th>Gives permissions?</th>
<th>Can be attached to/affect the Root user</th>
</tr>
<tr>
<td>Identity-based policies</td>
<td>JSON</td>
<td>IAM identities</td>
<td>Yes</td>
<td>No</td>
</tr>
<tr>
<td>Resource-based policies</td>
<td>JSON</td>
<td>Resources</td>
<td>Yes</td>
<td>Yes</td>
</tr>
<tr>
<td>IAM permissions boundaries</td>
<td>JSON</td>
<td>IAM identities only</td>
<td>No</td>
<td>No</td>
</tr>
<tr>
<td>Service control policies (SCPs)</td>
<td>JSON</td>
<td>IAM Identities/Resource</td>
<td>No</td>
<td>Yes (for member accounts)</td>
</tr>
<tr>
<td>Access control lists (ACLs)</td>
<td>non-JSON</td>
<td>Resources</td>
<td>Yes</td>
<td>Yes (of other accounts)</td>
</tr>
<tr>
<td>Session policies</td>
<td>JSON</td>
<td>IAM Identities</td>
<td>Yes</td>
<td>No (probably!)</td>
</tr>
</table>

### JSON policies structure
The structure of a policy is well explained in the docs. It is comprised of the following: (the pic is straight out of AWS Docs)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9axg7zfwozjgjrbv0ew3.png)

So for the following Resource-based policy, it just state that the  root user of AWS account _account-id_ is authorized to call every S3 API on a bucket called "mybucket" and all the objects within.

![JSON policy](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b3c6ru8hmjw2kxdx6ynn.png)

And this is a summarization of the different fields:
<table>
<tr>
<th>Field
<th>Short Description
<th>Required
<th>Type
<th>Values
<th>Notes
</tr>
<tr>
<td>Version
<td>Specify the version of the policy language
<td>Yes (probably!)
<td>Text
<td>"2008-10-17" / "2012-10-17"
<td>
</tr>
<tr>
<td>Statement
<td>Use this main policy element as a container for the following elements
<td>Yes
<td>JSON Object
<td>Varies
<td>
</tr>
<tr>
<td>Sid
<td>Include an optional statement ID
<td>No
<td>Any
<td>Varies
<td>
</tr>
<tr>
<td>Effect
<td>Indicates whether the policy allows or denies access
<td>Yes
<td>Text
<td>"Allow" / "Deny"
<td>
</tr>
<tr>
<td>Principal
<td>The principal which the statement applies to
<td>Required in only some circumstances
<td>List
<td>Varies
<td>If you create a resource-based policy, you must indicate the account, user, role, or federated user as the principle. 
</tr>
<tr>
<td>Action
<td>Include a list of actions that the policy allows or denies
<td>Yes
<td>List
<td>Varies
<td>Notes
</tr>
<tr>
<td>Resource
<td>The resource to which the action applies
<td>Required in only some circumstances
<td>List
<td>Varies
<td>If you create an IAM permissions policy, you must specify a list of resources to which the actions apply
</tr>
<tr>
<td>Condition
<td>Specify the circumstances under which the policy grants permission
<td>No
<td>Special Syntax
<td>Varies
<td>
</tr>
</table>

Finally, I hope that you find this article useful 😊 and see you in upcoming pieces 👋