# Enforce private-file authorization for Starlette

## Summary
Private file access for a Starlette app on Ample. Verified on Starlette: a role-to-document access model with negative tests (401, 403) and an authorized private-bucket round-trip (private=ok). Build and start: pip install into .ample/python from requirements.txt, then uvicorn from run.py reading PORT on the python-3.12 template.

## Prerequisites
- A Starlette project (pip install into .ample/python from requirements.txt, then uvicorn from run.py reading PORT on the python-3.12 template)
- A PostgreSQL driver reading DATABASE_URL at runtime
- A bucket from `ample bucket create` with credentials passed as encrypted S3_* environment variables
- An Ample account token with servers:write, databases:read, buckets:read

## Resource Requirements
- primitive:compute
- primitive:postgres
- primitive:s3-compatible-object-storage

## Workflow Steps
1. **Build and start**  
pip install into .ample/python from requirements.txt, then uvicorn from run.py reading PORT on the python-3.12 template; the server must bind 0.0.0.0 on PORT.
2. **Create the bucket and pass its credentials**  
Create it once with `ample bucket create`, then pass endpoint, region, bucket and keys with --env; use path-style addressing.
    ```bash
    ample deploy . --name  --public --start "python3 run.py" --env S3_ENDPOINT=... --env S3_REGION=... --env S3_BUCKET=... --env S3_ACCESS_KEY_ID=... --env S3_SECRET_ACCESS_KEY=...`
    ```
3. **Authorize before touching storage**  
Return 401 for unauthenticated requests and 403 for the wrong role; keep the bucket unpublished and stream objects through the app.
4. **Verify**  
Fetch the live URL and /p/private-document-library on the example; on failure read the build and runtime logs.
    ```bash
    ample logs  --kind build
    ```

## Success Checks
- App responds on its public URL
    - **kind**: http_get
    - **path**: /
    - **expect**: ample canary starlette patterns
- Private-document-library check from the example
    - **kind**: http_get
    - **path**: /p/private-document-library
    - **expect**: see the pattern fixture checks

## Limitations
- Verified on the python-3.12 template at s-1vcpu-1gb; other sizes and Starlette major versions are not verified.
- Region, compliance attestations and request-duration limits are unknown and not claimed.
- Managed PostgreSQL 16 only; extensions, connection limits and backup or restore procedures are not verified.
- PutObject and GetObject with path-style addressing are verified; other S3 operations are not.

## Cost Estimate
- **Currency**: USD
- **Monthly Amount**: $10.00
- **Components**:
  - App server: s-1vcpu-1gb - $5.00
  - Managed PostgreSQL database: s-1vcpu-1gb - $5.00
  - **Note**: Always-on monthly price of the tested sizes; apps auto-pause when idle. Buckets are allocation-priced per quota and not included.

## Evidence Summary
- **Kind**: canary_run
- **Summary**: Starlette pattern fixture deployed on Ample (pip install into .ample/python from requirements.txt, then uvicorn from run.py reading PORT on the python-3.12 template); for this guide: a role-to-document access model with negative tests (401, 403) and an authorized private-bucket round-trip (private=ok).
- **Observed At**: 2026-09-21T01:21:36Z
- **Implementation Revision**: 50dbac567d2c5cc8e55e6c748a646be0025d9cfb-dirty (CLI 0.1.21)
- **Expires At**: 2027-03-20T01:21:36Z

## Examples
### Starlette pattern fixture
- **Description**: Verified private file access on Starlette.
- **Source Reference**: tests/deploy-canaries/starlette-patterns

## Next Actions
- **Browse the catalog index**
- **Search published recipes by intent, stack and constraints**
- **Prepare a side-effect-free deployment plan for an authorized project**
- **Read the existing agent authentication setup**
- **Browse Starlette**
- **Browse Connect app to storage**
- **Browse Private document library**
