Skip to content

Commit

Permalink
xRPC com.atproto.identity.resolveHandle (#10)
Browse files Browse the repository at this point in the history
* Create handle resolution service with SAM deployment

This commit introduces the Handle Resolution Service for ATProto, developed using AWS SAM. It includes the SAM template, Lambda functions, Redis and DynamoDB integrations, Route53 configuration, and deployment scripts. Additionally, local development and testing setup are documented in the README file.

* Refactor handle resolution service for improved reliability.

Updated configurations to optimize performance (e.g., reduced timeout, increased memory). Enhanced Redis and DynamoDB error handling, added logging, and configured retries in boto3 to improve service reliability. Removed unnecessary Route53 DNS records and provisioned concurrency settings in template.yaml.

* Add VPC endpoints and optimize DynamoDB data handling.

Introduced private route tables and a DynamoDB VPC Gateway Endpoint for improved network control. Updated the app to only retrieve and return the 'did' field from DynamoDB, optimizing resource usage and reducing extraneous data processing.
  • Loading branch information
arkavo-com authored Dec 28, 2024
1 parent a36c915 commit 8badeca
Show file tree
Hide file tree
Showing 11 changed files with 841 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -228,3 +228,7 @@ cython_debug/
/gitlab-ce/
env.py
/arkavo-production/
/webapp/.idea/inspectionProfiles/Project_Default.xml
/webapp/.idea/webapp.iml
/webapp/.idea/inspectionProfiles/Project_Default.xml
/webapp/.idea/webapp.iml
9 changes: 9 additions & 0 deletions handle-resolution-service/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
.aws-sam/
dependencies/python/
__pycache__/
*.pyc
.env
.venv/
.idea/
.vscode/
/certs/
174 changes: 174 additions & 0 deletions handle-resolution-service/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
# Handle Resolution Service

ATProto handle resolution service built with AWS SAM.

## Prerequisites

- Python 3.12
```shell
brew install [email protected]
```
- AWS SAM CLI
- AWS CLI configured with appropriate credentials

## Development Setup

1. Create and activate a virtual environment:
```bash
python3.12 -m venv .venv
source .venv/bin/activate
```

2. Install development dependencies:
```bash
# Update pip
python -m pip install --upgrade pip

# Install development requirements
pip install aws-sam-cli awscli
```

## Project Structure

```
handle-resolution-service/
├── template.yaml # SAM template
├── requirements.txt # Python dependencies
├── build.sh # Build script
├── src/ # Lambda function code
└── dependencies/ # Lambda layer dependencies
```

## Building and Deployment

1. Build the project:
```bash
chmod +x build.sh
./build.sh
```

2. Deploy (first time):
```bash
sam deploy --guided
```

3. Subsequent deployments:
```bash
sam build && sam deploy --no-confirm-changeset
```

```shell
sam delete --stack-name handle-resolution-service --no-prompts
```

### Production Test

```shell
curl https://xrpc.arkavo.net/xrpc/com.atproto.identity.resolveHandle?handle=test.bsky.social
```

```shell
curl https://q8ku4t7uxj.execute-api.us-east-1.amazonaws.com/Prod/xrpc/com.atproto.identity.resolveHandle?handle=test.bsky.social
```

## Local Development

1. Install local dependencies in your virtual environment:
```bash
pip install -r requirements.txt
```

2. Colima
```shell
colima start
export DOCKER_HOST="unix://${HOME}/.colima/docker.sock"
````

3. Redis
```shell
docker run -d --name redis -p 6379:6379 redis
```

4. DynamoDB
```shell
docker run -d --name dynamodb -p 8000:8000 amazon/dynamodb-local
```

```shell
# Create the table
aws dynamodb create-table \
--table-name dev-handles \
--attribute-definitions AttributeName=handle,AttributeType=S \
--key-schema AttributeName=handle,KeyType=HASH \
--billing-mode PAY_PER_REQUEST \
--endpoint-url http://localhost:8000
# Add test data
aws dynamodb put-item \
--table-name dev-handles \
--item '{"handle": {"S": "test.arkavo.social"}, "did": {"S": "did:plc:testuser123"}}' \
--endpoint-url http://localhost:8000
```


3. Local testing:
```bash
# Test GET request
sam local invoke HandleCheckFunction --env-vars env.json -e events/get-request.json
# Test HEAD request
sam local invoke HandleCheckFunction --env-vars env.json -e events/head-request.json
# Start local API
sam local start-api
```

4. Test the API:
```bash
# Test GET endpoint
curl "http://localhost:3000/xrpc/com.atproto.identity.resolveHandle?handle=test.arkavo.social"
# Test HEAD endpoint
curl -I "http://localhost:3000/xrpc/com.atproto.identity.resolveHandle?handle=test.arkavo.social"
```

## API Endpoints

The service provides two endpoints:

### GET /xrpc/com.atproto.identity.resolveHandle
Resolves a handle to a DID.

Query Parameters:
- `handle`: The handle to resolve (e.g., `username.arkavo.social`)

### HEAD /xrpc/com.atproto.identity.resolveHandle
Quick check for handle validity and existence.

Query Parameters:
- `handle`: The handle to check (e.g., `username.arkavo.social`)

## Contributing

1. Create a new branch for your feature
2. Make changes in your branch
3. Test locally using SAM CLI
4. Submit a pull request

## Common Issues

### Virtual Environment Issues

If you see "command not found" after activating the virtual environment:
```bash
# Deactivate and remove the current venv
deactivate
rm -rf .venv
# Create a new venv with --clear flag
python3.12 -m venv .venv --clear
# Reactivate and reinstall dependencies
source .venv/bin/activate
pip install -r requirements.txt
```
21 changes: 21 additions & 0 deletions handle-resolution-service/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/bin/bash
set -e

# Ensure we're using Python 3.12
PYTHON=python3.12

# Install dependencies to the Lambda layer directory
$PYTHON -m pip install -r requirements.txt -t dependencies/python

# Clean up unnecessary files
find dependencies/python -type d -name "tests" -exec rm -rf {} +
find dependencies/python -type d -name "__pycache__" -exec rm -rf {} +
find dependencies/python -type f -name "*.pyc" -delete
find dependencies/python -type f -name "*.pyo" -delete
find dependencies/python -type f -name "*.dist-info" -exec rm -rf {} +

# Build SAM application
sam build

# Deploy (optional, can be run separately)
# sam deploy --guided
6 changes: 6 additions & 0 deletions handle-resolution-service/env.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"HandleCheckFunction": {
"REDIS_ENDPOINT": "host.docker.internal",
"DYNAMODB_TABLE": "dev-handles"
}
}
6 changes: 6 additions & 0 deletions handle-resolution-service/events/get-request.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"httpMethod": "GET",
"queryStringParameters": {
"handle": "test.arkavo.social"
}
}
6 changes: 6 additions & 0 deletions handle-resolution-service/events/head-request.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"httpMethod": "HEAD",
"queryStringParameters": {
"handle": "test.arkavo.social"
}
}
2 changes: 2 additions & 0 deletions handle-resolution-service/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
redis==5.0.1
boto3==1.34.64
10 changes: 10 additions & 0 deletions handle-resolution-service/samconfig.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
version = 0.1
[default.deploy.parameters]
stack_name = "handle-resolution-service"
resolve_s3 = true
s3_prefix = "handle-resolution-service"
region = "us-east-1"
confirm_changeset = true
capabilities = "CAPABILITY_IAM"
parameter_overrides = "Environment=\"prod\" VpcCIDR=\"10.0.0.0/16\" RedisNodeType=\"cache.t4g.micro\" DomainName=\"arkavo.net\" XrpcSubdomain=\"xrpc\""
image_repositories = []
Loading

0 comments on commit 8badeca

Please sign in to comment.