Authorization and Amazon Verified Permissions: A New Way to Manage Permissions - Part VI - Policy Templates
Tags: aws, avp, cedar, authorization
Published: 2023-07-20 20:48:43

Welcome back to my series on AWS Verified Permissions (AVP)! If you've been following along, you'll know that we've covered a lot of ground so far. We've explored the basics of AVP, looked at how to use the [Test Bench](https://dev.to/aws-builders/authorization-and-amazon-verified-permissions-a-new-way-to-manage-permissions-part-v-test-bench-4emp) feature, and much more. In this sixth blogpost, we're going to dive into a powerful feature of AVP: [Policy Templates](https://docs.aws.amazon.com/verifiedpermissions/latest/userguide/policy-templates.html). I'll explain what they are, why they're useful, and how to use them in the context of our ongoing ecommerce example. As always, we'll provide step-by-step instructions for both the AWS console and our handy [AVP CLI tool](https://www.npmjs.com/package/avp-cli).
 
## Policy Templates
Policy Templates in AVP are reusable policy definitions that can be linked to multiple principals and resources. They are a way to define a policy once and then apply it to many different entities. This concept is from [Cedar](https://docs.cedarpolicy.com/templates.html), a policy management system that also uses templates to simplify policy creation and management.

In the context of AVP, a Policy Template is a blueprint for a policy that can be applied to different principals and resources (but not for actions). It's a way to define the permissions once and then apply them to multiple entities, ensuring consistency and reducing the complexity of your policy management.

For example, in our ecommerce system, we might have a policy that allows sellers to list products. Instead of creating a separate policy for each seller, we can create a single policy template and link it to each seller. This not only reduces the number of policies we need to manage, but also ensures consistency across all sellers.

## Why Use Policy Templates?
Policy Templates offer several benefits:

**Efficiency**: Instead of creating individual policies for each entity, you can create one policy template and link it to multiple entities. This can save time and reduce the complexity of your policy management.

**Consistency**: By using a policy template, you ensure that all entities linked to the template have the same permissions. This can help prevent errors and inconsistencies that might arise from manually creating individual policies.

**Flexibility**: Policy templates can be linked to different types of entities (e.g., users, roles, groups) and resources, providing a flexible way to manage permissions across your system.

**Granular Control**: Policy templates provide a more granular level of control compared to using groups or allowing all access. They allow you to specify exactly what actions a principal can perform on a resource, reducing the risk of over-permissioning.

## Static Policies vs Template-Linked Policies vs Policy Templates
Before we dive into how to use policy templates, let's clarify the difference between static policies, template-linked policies, and policy templates.

**Static Policies:** These are standalone policies that define permissions for a specific principal and resource. They are not linked to a template and can't be reused for other principals or resources.

**Template-Linked Policies:** These are policies that are linked to a policy template. They inherit the permissions defined in the template and apply them to a specific principal and resource.

**Policy Templates:** These are reusable policy definitions that can be linked to multiple principals and resources through template-linked policies. They define a set of permissions that can be applied consistently across multiple entities.

## Policy Templates in action
Let's walk through the process of creating and using a policy template for our ecommerce scenario.

Two approaches:

## Manually in AWS Console

- Open the AWS Management Console, find the AVP service, and create a new policy store. Select Empty policy store.


![Empty Policy Store](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kg88pwmpdl9nyldqs04s.png)

- Navigate to Schema and add below json:
```json
{
    "EcommercePlatform": {
        "entityTypes": {
            "Product": {
                "shape": {
                    "type": "Record",
                    "attributes": {}
                }
            },
            "Seller": {
                "shape": {
                    "type": "Record",
                    "attributes": {}
                }
            }
        },
        "actions": {
            "List": {
                "appliesTo": {
                    "principalTypes": [
                        "Seller"
                    ],
                    "resourceTypes": [
                        "Product"
                    ]
                }
            },
            "Remove": {
                "appliesTo": {
                    "principalTypes": [
                        "Seller"
                    ],
                    "resourceTypes": [
                        "Product"
                    ]
                }
            },
            "Update": {
                "appliesTo": {
                    "resourceTypes": [
                        "Product"
                    ],
                    "principalTypes": [
                        "Seller"
                    ]
                }
            }
        }
    }
}

```


![Schema](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/79lu0cofzgvkz4tbnwtb.png)


- Navigate to the "Policy Templates" section. Click on "Create Policy Template". You'll be asked to provide a name and a policy definition. The policy definition should be a JSON object that describes the permissions granted by the policy. For our ecommerce example, we might create a policy template that allows a seller to list a product.

- For description use "Policy template that allows a seller to list a product", and for "Policy template body" use:

```bash
permit (
    principal == ?principal,
    action in [
        EcommercePlatform::Action::"List"
    ],
    resource == ?resource
);
``` 

![Policy template](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dh7d9euxrqxow5bd83bg.png)


- Once the policy template is created, you can link it to principals and resources. Navigate to the "Polices" section  and click on "Create Template-Linked Policy". 


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

- Select already existing policy template, and click next.


![Select policy template](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cfppueyk1xd0c2mlivx4.png)

- Now you need to provide values that are injected into the template to create the policy (principal - seller and resource - product), and create a policy.

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

- After creating the template-linked policy, you can review it in the "Template-Linked Policies" section. You'll see the policy template, the principal, and the resource, along with any other template-linked policies you've created.

- At the end we can test it in Test Bench section.


![Test Bench](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eardcdvmyi9n3q8m226s.png)

That's it!

## Via AVP-CLI
We can also avoid doing it manually within the AWS console, and use [avp-cli](https://www.npmjs.com/package/avp-cli) for deploying it via CLI.

For the purpose of this blogpost I've created scenario [Ecommerce policy template scenario](https://github.com/Pigius/avp-cli/tree/main/scenarios/ecommercePolicyTemplateScenario) which is the exact usecase.

Simply download it, and run "Ecommerce with Template-Linked Policies Scenario" from the avaliable scenarios.

```bash
➜  avp-cli git:(main) node index.js
Welcome to AVP CLI!
This tool is designed to help you interact with the AWS Verified Permissions (AVP) service. You can use it to create, manage, and delete policy stores, schemas, and policies.
Please ensure that you have set up your AWS credentials correctly to use this tool.
? What would you like to do? Use prepared scenarios
? Choose a scenario 
  Documents Scenario 
  Ecommerce with Context Scenario 
  Ecommerce with Group Scenario 
❯ Ecommerce with Template-Linked Policies Scenario 
```
After it will be successfully finished, open the AVP in AWS console to play around with that. 
## Next steps

That's all for today. I encourage you to play around it more. In addition, I encourage you to try the [official AVP workshop from AWS](https://catalog.workshops.aws/verified-permissions-in-action/en-US) where there are also policy templates.

As for the next blogpost, I will [describe](https://dev.to/aws-builders/authorization-and-amazon-verified-permissions-a-new-way-to-manage-permissions-part-vii-audit-and-pricing-2k5) the topics of audit and pricing of the service.