|
1 | 1 |
|
2 | | -# Welcome to your CDK Python project! |
| 2 | +# Amazon Verified Permissions REST API (AWS CDK Python) |
3 | 3 |
|
4 | | -This is a blank project for CDK development with Python. |
| 4 | +This example deploys a REST API secured by Amazon Verified Permissions (AVP) and Amazon Cognito. The stack demonstrates how to combine a Cedar policy store with an API Gateway Request Authorizer so that only members of the appropriate Cognito group can invoke protected routes. |
5 | 5 |
|
6 | | -The `cdk.json` file tells the CDK Toolkit how to execute your app. |
| 6 | +> :information_source: Detailed guides live in the `docs/` directory: |
| 7 | +> - [`docs/architecture.md`](docs/architecture.md) – system design and authorization model |
| 8 | +> - [`docs/deployment.md`](docs/deployment.md) – environment setup and deployment steps |
| 9 | +> - [`docs/operations.md`](docs/operations.md) – testing, troubleshooting, and cleanup |
7 | 10 |
|
8 | | -This project is set up like a standard Python project. The initialization |
9 | | -process also creates a virtualenv within this project, stored under the `.venv` |
10 | | -directory. To create the virtualenv it assumes that there is a `python3` |
11 | | -(or `python` for Windows) executable in your path with access to the `venv` |
12 | | -package. If for any reason the automatic creation of the virtualenv fails, |
13 | | -you can create the virtualenv manually. |
| 11 | +## Solution Highlights |
14 | 12 |
|
15 | | -To manually create a virtualenv on MacOS and Linux: |
| 13 | +- **End-to-end RBAC** – Cognito users receive access tokens that are evaluated by AVP before API Gateway invokes your Lambda handlers. |
| 14 | +- **Cedar-first design** – The Cedar schema and policies are provisioned with the `cdklabs.cdk_verified_permissions` library for repeatable deployments. |
| 15 | +- **Composable infrastructure** – Cognito, Verified Permissions, and API Gateway live in dedicated nested stacks to make future customization straightforward. |
| 16 | +- **Language mix** – The authorizer runs on Node.js (to use the AWS SDK for AVP), while the demo business logic stays in simple Python Lambda handlers. |
16 | 17 |
|
17 | | -``` |
18 | | -$ python3 -m venv .venv |
19 | | -``` |
| 18 | +| Resource | Description | |
| 19 | +|----------|-------------| |
| 20 | +| Cognito User Pool & Client | Provides user management and issues JWT access tokens. Adds `admin` and `user` groups. | |
| 21 | +| Verified Permissions Policy Store | Hosts the Cedar schema and static policies mapping Cognito groups to REST actions. | |
| 22 | +| API Gateway REST API | Exposes `/`, `/user`, and `/admin` endpoints secured by a custom Request Authorizer. | |
| 23 | +| Lambda Functions | Node.js authorizer for AVP calls plus Python handlers that simulate protected resources. | |
20 | 24 |
|
21 | | -After the init process completes and the virtualenv is created, you can use the following |
22 | | -step to activate your virtualenv. |
| 25 | +## Quick Start |
23 | 26 |
|
24 | | -``` |
25 | | -$ source .venv/bin/activate |
| 27 | +### 1. Set Up the Environment |
| 28 | + |
| 29 | +```bash |
| 30 | +python3 -m venv .venv |
| 31 | +source .venv/bin/activate |
| 32 | +pip install -r requirements.txt |
26 | 33 | ``` |
27 | 34 |
|
28 | | -If you are a Windows platform, you would activate the virtualenv like this: |
| 35 | +Install or upgrade the AWS CDK Toolkit if needed: |
29 | 36 |
|
30 | | -``` |
31 | | -% .venv\Scripts\activate.bat |
| 37 | +```bash |
| 38 | +npm install -g aws-cdk@latest |
32 | 39 | ``` |
33 | 40 |
|
34 | | -Once the virtualenv is activated, you can install the required dependencies. |
| 41 | +Bootstrap the target account and region the first time you deploy CDK there: |
35 | 42 |
|
| 43 | +```bash |
| 44 | +cdk bootstrap aws://<ACCOUNT_ID>/<REGION> |
36 | 45 | ``` |
37 | | -$ pip install -r requirements.txt |
| 46 | + |
| 47 | +### 2. Deploy the Stack |
| 48 | + |
| 49 | +```bash |
| 50 | +cdk deploy |
38 | 51 | ``` |
39 | 52 |
|
40 | | -At this point you can now synthesize the CloudFormation template for this code. |
| 53 | +Note the `ApiEndpoint` output once the deployment completes. |
| 54 | + |
| 55 | +For additional deployment options, refer to [`docs/deployment.md`](docs/deployment.md). |
| 56 | + |
| 57 | +## Trying the API |
| 58 | + |
| 59 | +1. Create or confirm a Cognito user and add them to the `user` and/or `admin` groups. The implementation guide provides ready-to-run CLI snippets. |
| 60 | +2. Authenticate against the user pool (for example, with `aws cognito-idp initiate-auth`) and capture the access token. |
| 61 | +3. Call the API with the bearer token: |
| 62 | + |
| 63 | + ```bash |
| 64 | + curl -i \ |
| 65 | + -H "Authorization: Bearer $ACCESS_TOKEN" \ |
| 66 | + "$API_URL/user" |
| 67 | + ``` |
| 68 | + |
| 69 | + - Members of the `user` group receive `Hello from User!`. |
| 70 | + - Only `admin` members may call `/admin`. |
| 71 | + |
| 72 | +## Project Layout |
41 | 73 |
|
42 | 74 | ``` |
43 | | -$ cdk synth |
| 75 | +├── app.py # CDK entry point |
| 76 | +├── stack/ |
| 77 | +│ ├── main.py # Root stack wiring together nested stacks |
| 78 | +│ ├── apigw/ # REST API, Lambda integrations, authorizer |
| 79 | +│ ├── cognito/ # Cognito user pool, client, and groups |
| 80 | +│ ├── verified_permissions/ # Cedar schema and policies |
| 81 | +│ └── lambdas/ # Authorizer (Node.js) and demo handlers (Python) |
| 82 | +└── docs/implementation-guide.md # In-depth deployment and operations guide |
44 | 83 | ``` |
45 | 84 |
|
46 | | -To add additional dependencies, for example other CDK libraries, just add |
47 | | -them to your `setup.py` file and rerun the `pip install -r requirements.txt` |
48 | | -command. |
| 85 | +## Customizing Verified Permissions |
| 86 | + |
| 87 | +1. Update `stack/verified_permissions/schema.py` with new entity types or actions. |
| 88 | +2. Modify or add policy definitions under `stack/verified_permissions/policy/`. |
| 89 | +3. Redeploy with `cdk deploy` to publish the schema and policies. |
49 | 90 |
|
50 | | -## Useful commands |
| 91 | +Consider migrating to policy templates if you need per-tenant or per-resource authorization decisions. |
| 92 | + |
| 93 | +## Cleanup |
| 94 | + |
| 95 | +Destroy the stack when you are finished to avoid unexpected charges: |
| 96 | + |
| 97 | +```bash |
| 98 | +cdk destroy |
| 99 | +``` |
51 | 100 |
|
52 | | - * `cdk ls` list all stacks in the app |
53 | | - * `cdk synth` emits the synthesized CloudFormation template |
54 | | - * `cdk deploy` deploy this stack to your default AWS account/region |
55 | | - * `cdk diff` compare deployed stack with current state |
56 | | - * `cdk docs` open CDK documentation |
| 101 | +## Additional Reading |
57 | 102 |
|
58 | | -Enjoy! |
| 103 | +- [`docs/architecture.md`](docs/architecture.md) – architecture diagrams, component breakdown, and Cedar policy model. |
| 104 | +- [`docs/deployment.md`](docs/deployment.md) – prerequisites, bootstrapping, and deployment workflows. |
| 105 | +- [`docs/operations.md`](docs/operations.md) – post-deployment tasks, troubleshooting tips, and sample CLI commands. |
| 106 | +- [Amazon Verified Permissions User Guide](https://docs.aws.amazon.com/verifiedpermissions/latest/userguide/) |
| 107 | +- [Cedar policy language](https://www.cedarpolicy.com/) |
0 commit comments