Skip to content

Commit 66272a4

Browse files
authored
Feat/twilio devtools (#4)
This PR introduces comprehensive Twilio DevTools integration for the kernel browser project, adding secure credential management and enhanced development capabilities. The main purpose is to integrate Twilio's Network Traversal Service for TURN credentials and add Chrome DevTools frontend support for better debugging. - Adds Twilio TURN credential management with dynamic generation and fallback handling - Integrates enhanced Chrome DevTools frontend with dedicated service configuration - Implements secure credential storage using Google Secret Manager with automated deployment workflows
1 parent b6ee1fd commit 66272a4

23 files changed

+1389
-106
lines changed

.env.example

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Twilio Network Traversal Service Credentials
2+
# Get these from your Twilio Console:
3+
# 1. Go to https://console.twilio.com/
4+
# 2. Navigate to Account > API Keys & Tokens
5+
# 3. Create a new API Key
6+
# 4. Use the SID as TWILIO_ACCOUNT_SID
7+
# 5. Use the Secret as TWILIO_AUTH_TOKEN
8+
TWILIO_ACCOUNT_SID=SK...your_api_key_sid_here
9+
TWILIO_AUTH_TOKEN=your_api_key_secret_here
10+
11+
# Google Cloud Configuration
12+
# If not provided, will use current gcloud config
13+
PROJECT_ID=your-gcp-project-id
14+
# REGION=us-central1
15+
16+
# Optional: Service Configuration
17+
# SERVICE_NAME=kernel-browser

.gitignore

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# Environment variables
2+
.env
3+
.env.local
4+
*.env
5+
!.env.example
6+
7+
# Node modules
8+
node_modules/
9+
10+
# Build outputs
11+
dist/
12+
build/
13+
out/
14+
15+
# Logs
16+
*.log
17+
npm-debug.log*
18+
yarn-debug.log*
19+
yarn-error.log*
20+
21+
# OS files
22+
.DS_Store
23+
Thumbs.db
24+
25+
# IDE files
26+
.vscode/
27+
.idea/
28+
*.swp
29+
*.swo
30+
31+
# Temporary files
32+
tmp/
33+
temp/
34+
*.tmp
35+
36+
# Python
37+
__pycache__/
38+
*.py[cod]
39+
*$py.class
40+
.Python
41+
venv/
42+
env/
43+
44+
# Google Cloud
45+
.gcloudignore
46+
gcs-key.json
47+
service-account-key.json
48+
49+
# Docker
50+
.dockerignore
51+
52+
# Backup files
53+
*.bak
54+
*.backup

DEPLOYMENT.md

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
# Kernel Browser - Cloud Run Deployment Guide
2+
3+
This guide explains how to deploy the Kernel Browser to Google Cloud Run with secure Twilio credential management.
4+
5+
## Prerequisites
6+
7+
- Google Cloud SDK (`gcloud`) installed
8+
- Docker installed
9+
- Git installed
10+
- A Google Cloud Project with billing enabled
11+
- Twilio account with API credentials (for WebRTC TURN servers)
12+
13+
## Quick Start
14+
15+
### 1. Clone the repository
16+
```bash
17+
git clone <repository-url>
18+
cd browser-web-agent
19+
git submodule update --init --recursive
20+
```
21+
22+
### 2. Set up Twilio credentials
23+
```bash
24+
# Copy the example environment file
25+
cp .env.example .env
26+
27+
# Edit .env and add your Twilio credentials
28+
# Get these from https://console.twilio.com/ > Account > API Keys & Tokens
29+
```
30+
31+
Your `.env` file should contain:
32+
```
33+
TWILIO_ACCOUNT_SID=SK...your_api_key_sid_here
34+
TWILIO_AUTH_TOKEN=your_api_key_secret_here
35+
```
36+
37+
### 3. Deploy to Cloud Run
38+
```bash
39+
./deploy.sh
40+
```
41+
42+
The script will:
43+
- Load credentials from `.env`
44+
- Create/update secrets in Google Secret Manager
45+
- Build and deploy the container to Cloud Run
46+
- Configure all necessary permissions
47+
48+
## Deployment Options
49+
50+
### Using Cloud Build (recommended)
51+
```bash
52+
./deploy.sh
53+
```
54+
55+
### Using local Docker build
56+
```bash
57+
./deploy.sh --local
58+
```
59+
60+
### Specify project and region
61+
```bash
62+
./deploy.sh --project YOUR_PROJECT_ID --region us-central1
63+
```
64+
65+
## How It Works
66+
67+
### Credential Management
68+
69+
1. **Local Development**: Credentials are stored in `.env` file (gitignored)
70+
2. **Secret Manager**: Deploy script automatically creates/updates secrets in Google Secret Manager
71+
3. **Cloud Run**: Service uses `secretKeyRef` to securely access credentials at runtime
72+
4. **Dynamic TURN**: Container fetches fresh TURN credentials from Twilio on startup
73+
74+
### Security Features
75+
76+
- Credentials never appear in code or logs
77+
- Secrets are encrypted at rest and in transit
78+
- Service account has minimal required permissions
79+
- Automatic credential rotation support
80+
81+
### Files Overview
82+
83+
- `.env.example` - Template for environment variables
84+
- `.env` - Your local credentials (gitignored)
85+
- `deploy.sh` - Main deployment script with Secret Manager integration
86+
- `service-secrets.yaml` - Cloud Run config with secret references
87+
- `service.yaml` - Fallback config (for deployments without secrets)
88+
- `cloudbuild.yaml` - Cloud Build configuration
89+
- `twilio/` - Twilio credential management scripts
90+
91+
## Updating Credentials
92+
93+
To update Twilio credentials:
94+
95+
1. Update `.env` with new credentials
96+
2. Run `./deploy.sh` again
97+
3. Script will update secrets and redeploy
98+
99+
## Manual Secret Management
100+
101+
If you need to manage secrets manually:
102+
103+
```bash
104+
# Create secrets
105+
echo -n "YOUR_SID" | gcloud secrets create twilio-account-sid --data-file=-
106+
echo -n "YOUR_TOKEN" | gcloud secrets create twilio-auth-token --data-file=-
107+
108+
# Update secrets
109+
echo -n "NEW_SID" | gcloud secrets versions add twilio-account-sid --data-file=-
110+
echo -n "NEW_TOKEN" | gcloud secrets versions add twilio-auth-token --data-file=-
111+
112+
# Grant access to service account
113+
gcloud secrets add-iam-policy-binding twilio-account-sid \
114+
--member="serviceAccount:kernel-browser-sa@PROJECT_ID.iam.gserviceaccount.com" \
115+
--role="roles/secretmanager.secretAccessor"
116+
```
117+
118+
## Service Endpoints
119+
120+
After deployment, you'll have access to:
121+
122+
- **Main Interface**: `https://SERVICE_URL/`
123+
- **WebRTC Client**: `https://SERVICE_URL/`
124+
- **Chrome DevTools**: `https://SERVICE_URL/devtools/`
125+
- **DevTools WebSocket**: `wss://SERVICE_URL/cdp/ws`
126+
- **Recording API**: `https://SERVICE_URL/api`
127+
- **Health Check**: `https://SERVICE_URL/health`
128+
129+
## Troubleshooting
130+
131+
### Deployment fails
132+
- Check that all prerequisites are installed
133+
- Ensure billing is enabled on your GCP project
134+
- Verify you have sufficient quota in your region
135+
136+
### WebRTC not working
137+
- Ensure Twilio credentials are correct
138+
- Check Cloud Run logs: `gcloud run services logs read kernel-browser --region=us-central1`
139+
- Verify TURN servers are accessible from your network
140+
141+
### Secrets not found
142+
- Run `gcloud secrets list` to verify secrets exist
143+
- Check service account permissions
144+
- Ensure Secret Manager API is enabled
145+
146+
## Architecture
147+
148+
```
149+
┌─────────────┐ ┌──────────────────┐ ┌─────────────────┐
150+
│ Client │────▶│ Cloud Run │────▶│ Secret Manager │
151+
│ (Browser) │ │ (Container) │ │ (Credentials) │
152+
└─────────────┘ └──────────────────┘ └─────────────────┘
153+
154+
155+
┌──────────────────┐
156+
│ Twilio API │
157+
│ (TURN Servers) │
158+
└──────────────────┘
159+
```
160+
161+
## Support
162+
163+
For issues or questions:
164+
- Check logs: `gcloud run services logs read kernel-browser --region=us-central1`
165+
- Review service status: `gcloud run services describe kernel-browser --region=us-central1`
166+
- File an issue on GitHub

Dockerfile.cloudrun

Lines changed: 86 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,57 @@
1+
# DevTools Frontend build stage using browser-operator-core
2+
FROM --platform=linux/amd64 ubuntu:22.04 AS devtools-builder
3+
4+
# Cache bust argument to force rebuilds
5+
ARG CACHE_BUST
6+
7+
# Install required packages for DevTools frontend build
8+
RUN apt-get update && apt-get install -y \
9+
curl \
10+
git \
11+
python3 \
12+
python3-pip \
13+
python-is-python3 \
14+
wget \
15+
unzip \
16+
sudo \
17+
ca-certificates \
18+
build-essential \
19+
&& rm -rf /var/lib/apt/lists/*
20+
21+
# Install Node.js 18.x
22+
RUN curl -fsSL https://deb.nodesource.com/setup_18.x | bash - && \
23+
apt-get install -y nodejs && \
24+
rm -rf /var/lib/apt/lists/*
25+
26+
WORKDIR /workspace
27+
28+
# Clone depot_tools
29+
RUN git clone https://chromium.googlesource.com/chromium/tools/depot_tools.git
30+
ENV PATH="/workspace/depot_tools:${PATH}"
31+
ENV DEPOT_TOOLS_UPDATE=0
32+
33+
# Follow README instructions exactly - fetching code
34+
RUN mkdir devtools
35+
WORKDIR /workspace/devtools
36+
RUN fetch devtools-frontend
37+
38+
# Build steps
39+
WORKDIR /workspace/devtools/devtools-frontend
40+
41+
RUN gclient sync
42+
RUN /workspace/depot_tools/ensure_bootstrap
43+
44+
# Build standard DevTools first
45+
RUN npm run build
46+
47+
# Add Browser Operator fork and switch to it
48+
RUN git remote add upstream https://github.com/BrowserOperator/browser-operator-core.git
49+
RUN git fetch upstream
50+
RUN git checkout upstream/main
51+
52+
# Build Browser Operator version
53+
RUN npm run build
54+
155
# Multi-stage build using kernel-images as base
256
FROM docker.io/golang:1.25.0 AS server-builder
357
WORKDIR /workspace/server
@@ -90,6 +144,12 @@ RUN apt-get update && \
90144
nginx \
91145
# PPA req
92146
software-properties-common && \
147+
# Disable nginx auto-start to prevent conflicts with custom config
148+
systemctl disable nginx || true && \
149+
systemctl mask nginx || true && \
150+
# Remove default nginx config to prevent conflicts
151+
rm -f /etc/nginx/sites-enabled/default && \
152+
rm -f /etc/nginx/nginx.conf && \
93153
# Userland apps
94154
sudo add-apt-repository ppa:mozillateam/ppa && \
95155
sudo apt-get install -y --no-install-recommends \
@@ -186,19 +246,40 @@ COPY kernel-images/images/chromium-headful/supervisor/services/ /etc/supervisor/
186246
# Copy the kernel-images API binary
187247
COPY --from=server-builder /out/kernel-images-api /usr/local/bin/kernel-images-api
188248

189-
# Cloud Run specific: nginx configuration for port proxying
190-
COPY nginx.conf /etc/nginx/nginx.conf
249+
# ============================================================================
250+
# DevTools Integration
251+
# ============================================================================
252+
253+
# Copy DevTools static files from builder
254+
COPY --from=devtools-builder /workspace/devtools/devtools-frontend/out/Default/gen/front_end /usr/share/nginx/devtools
255+
256+
# Set permissions for DevTools files
257+
RUN chown -R kernel:kernel /usr/share/nginx/devtools
258+
259+
# Cloud Run specific: wrapper scripts (nginx config is inline)
260+
# DO NOT copy nginx.conf to avoid auto-start conflicts
191261
COPY cloudrun-wrapper.sh /cloudrun-wrapper.sh
192-
RUN chmod +x /cloudrun-wrapper.sh
262+
COPY twilio/twilio-credential-updater.sh /twilio-credential-updater.sh
263+
RUN chmod +x /cloudrun-wrapper.sh /twilio-credential-updater.sh
264+
265+
# Add essential services for neko WebRTC and Chromium
266+
COPY supervisor/services-cloudrun/dbus.conf /etc/supervisor/conf.d/services-cloudrun/dbus.conf
267+
COPY supervisor/services-cloudrun/xorg.conf /etc/supervisor/conf.d/services-cloudrun/xorg.conf
268+
COPY supervisor/services-cloudrun/neko.conf /etc/supervisor/conf.d/services-cloudrun/neko.conf
269+
COPY supervisor/services-cloudrun/chromium.conf /etc/supervisor/conf.d/services-cloudrun/chromium.conf
270+
COPY supervisor/services-cloudrun/devtools-frontend.conf /etc/supervisor/conf.d/services-cloudrun/devtools-frontend.conf
193271

194272
# Create nginx temp directories for non-root execution
195273
RUN mkdir -p /tmp/nginx_client_temp /tmp/nginx_proxy_temp /tmp/nginx_fastcgi_temp \
196-
/tmp/nginx_uwsgi_temp /tmp/nginx_scgi_temp && \
274+
/tmp/nginx_uwsgi_temp /tmp/nginx_scgi_temp \
275+
/tmp/nginx_devtools_client_temp /tmp/nginx_devtools_proxy_temp /tmp/nginx_devtools_fastcgi_temp \
276+
/tmp/nginx_devtools_uwsgi_temp /tmp/nginx_devtools_scgi_temp && \
197277
chown -R kernel:kernel /tmp/nginx_*
198278

199279
# Create supervisor log directories
200280
RUN mkdir -p /var/log/supervisord/chromium /var/log/supervisord/neko /var/log/supervisord/xorg \
201-
/var/log/supervisord/dbus /var/log/supervisord/kernel-images-api /var/log/supervisord/mutter && \
281+
/var/log/supervisord/dbus /var/log/supervisord/kernel-images-api /var/log/supervisord/mutter \
282+
/var/log/supervisord/nginx /var/log/supervisord/devtools-frontend && \
202283
chown -R kernel:kernel /var/log/supervisord
203284

204285
# Create health check endpoint

0 commit comments

Comments
 (0)