-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathsetup-devcontainer.sh
More file actions
executable file
·479 lines (416 loc) · 15.3 KB
/
setup-devcontainer.sh
File metadata and controls
executable file
·479 lines (416 loc) · 15.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
#!/bin/bash
# setup-devcontainer.sh - Create secure devcontainer for Claude Code with --dangerously-skip-permissions
#
# This script implements Anthropic's official devcontainer approach for running
# Claude Code with full autonomy in an isolated container environment.
#
# Reference: https://docs.anthropic.com/en/docs/claude-code/devcontainer
set -euo pipefail
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Configuration
DEVCONTAINER_DIR=".devcontainer"
DEVCONTAINER_JSON="${DEVCONTAINER_DIR}/devcontainer.json"
DOCKERFILE="${DEVCONTAINER_DIR}/Dockerfile"
# Default settings
NETWORK_FIREWALL=true
FULL_TOOLING=true
DRY_RUN=false
FORCE=false
STRICT_MODE=false
# Allowed domains for network firewall (default set)
DEFAULT_ALLOWED_DOMAINS=(
"api.anthropic.com"
"github.com"
"registry.npmjs.org"
"pypi.org"
"files.pythonhosted.org"
)
# Initialize with defaults; extra domains added via --allow-domain or env var
ALLOWED_DOMAINS=("${DEFAULT_ALLOWED_DOMAINS[@]}")
# Support additional domains via environment variable (space or comma separated)
# Example: DEVCONTAINER_EXTRA_DOMAINS="internal.registry.com private.npm.org"
if [[ -n "${DEVCONTAINER_EXTRA_DOMAINS:-}" ]]; then
# Convert commas to spaces and split
IFS=', ' read -ra EXTRA_DOMAINS <<< "$DEVCONTAINER_EXTRA_DOMAINS"
ALLOWED_DOMAINS+=("${EXTRA_DOMAINS[@]}")
fi
usage() {
cat << EOF
Usage: $(basename "$0") [OPTIONS]
Create a secure devcontainer configuration for running Claude Code with
--dangerously-skip-permissions safely.
OPTIONS:
-h, --help Show this help message
--no-network-firewall Skip network firewall rules (allows all outbound traffic)
--minimal Minimal tooling (Node.js, Git only - no Python, AWS CLI, etc.)
--dry-run Show what would be created without writing files
--force Overwrite existing devcontainer configuration
--strict Fail immediately if prerequisites are missing (for CI use)
--allow-domain DOMAIN Add domain to firewall allowlist (can be used multiple times)
ENVIRONMENT VARIABLES:
DEVCONTAINER_EXTRA_DOMAINS Space or comma separated list of additional allowed domains
Example: "internal.registry.com,private.npm.org"
EXAMPLES:
# Create devcontainer with recommended security settings
$(basename "$0")
# Create minimal devcontainer without network restrictions
$(basename "$0") --minimal --no-network-firewall
# Preview what would be created
$(basename "$0") --dry-run
# Add custom domains for enterprise private registries
$(basename "$0") --allow-domain internal.registry.com --allow-domain npm.mycompany.com
# Or use environment variable
DEVCONTAINER_EXTRA_DOMAINS="internal.registry.com,npm.mycompany.com" $(basename "$0")
AFTER SETUP:
# Using VS Code
Cmd/Ctrl+Shift+P → "Dev Containers: Reopen in Container"
# Using CLI
devcontainer up --workspace-folder .
devcontainer exec --workspace-folder . claude --dangerously-skip-permissions
For more information, see:
https://docs.anthropic.com/en/docs/claude-code/devcontainer
EOF
}
log_info() {
echo -e "${BLUE}INFO:${NC} $1"
}
log_success() {
echo -e "${GREEN}SUCCESS:${NC} $1"
}
log_warning() {
echo -e "${YELLOW}WARNING:${NC} $1"
}
log_error() {
echo -e "${RED}ERROR:${NC} $1"
}
parse_args() {
while [[ $# -gt 0 ]]; do
case $1 in
-h|--help)
usage
exit 0
;;
--no-network-firewall)
NETWORK_FIREWALL=false
shift
;;
--minimal)
FULL_TOOLING=false
shift
;;
--dry-run)
DRY_RUN=true
shift
;;
--force)
FORCE=true
shift
;;
--strict)
STRICT_MODE=true
shift
;;
--allow-domain)
if [[ -z "${2:-}" ]]; then
log_error "--allow-domain requires a domain argument"
exit 1
fi
ALLOWED_DOMAINS+=("$2")
shift 2
;;
*)
log_error "Unknown option: $1"
usage
exit 1
;;
esac
done
}
check_prerequisites() {
log_info "Checking prerequisites..."
local has_errors=false
# Check for Docker command
if ! command -v docker &> /dev/null; then
if [[ "$STRICT_MODE" == "true" ]]; then
log_error "Docker not found. Install Docker to use devcontainers."
has_errors=true
else
log_warning "Docker not found. You'll need Docker to use the devcontainer."
fi
else
log_success "Docker found: $(docker --version)"
# Check if Docker daemon is running
if ! docker info &> /dev/null; then
if [[ "$STRICT_MODE" == "true" ]]; then
log_error "Docker daemon is not running. Start Docker and try again."
has_errors=true
else
log_warning "Docker daemon is not running. Start Docker before using the devcontainer."
fi
else
log_success "Docker daemon is running"
fi
fi
# Check for ANTHROPIC_API_KEY
if [[ -z "${ANTHROPIC_API_KEY:-}" ]]; then
if [[ "$STRICT_MODE" == "true" ]]; then
log_error "ANTHROPIC_API_KEY environment variable is not set."
has_errors=true
else
log_warning "ANTHROPIC_API_KEY not set. You'll need to set it before running Claude."
fi
else
log_success "ANTHROPIC_API_KEY is set"
fi
# Check for devcontainer CLI (optional but helpful)
if command -v devcontainer &> /dev/null; then
log_success "devcontainer CLI found: $(devcontainer --version)"
else
log_info "devcontainer CLI not found. Install with: npm install -g @devcontainers/cli"
fi
# In strict mode, exit if any required prerequisites are missing
if [[ "$STRICT_MODE" == "true" ]] && [[ "$has_errors" == "true" ]]; then
log_error "Prerequisites check failed. Fix the errors above and try again."
exit 1
fi
}
check_existing_config() {
if [[ -d "$DEVCONTAINER_DIR" ]] && [[ "$FORCE" != "true" ]]; then
log_error "Devcontainer configuration already exists at ${DEVCONTAINER_DIR}/"
log_info "Use --force to overwrite existing configuration"
exit 1
fi
}
generate_devcontainer_json() {
local features=""
local post_create_command=""
if [[ "$FULL_TOOLING" == "true" ]]; then
features='{
"ghcr.io/devcontainers/features/node:1": {},
"ghcr.io/devcontainers/features/python:1": {},
"ghcr.io/devcontainers/features/git:1": {},
"ghcr.io/devcontainers/features/github-cli:1": {},
"ghcr.io/devcontainers/features/aws-cli:1": {},
"ghcr.io/devcontainers/features/docker-in-docker:1": {}
}'
post_create_command="npm install -g @anthropic-ai/claude-code && pip install --user boto3 requests"
else
features='{
"ghcr.io/devcontainers/features/node:1": {},
"ghcr.io/devcontainers/features/git:1": {},
"ghcr.io/devcontainers/features/github-cli:1": {}
}'
post_create_command="npm install -g @anthropic-ai/claude-code"
fi
cat << EOF
{
"name": "Claude Code Sandbox",
"build": {
"dockerfile": "Dockerfile"
},
"features": ${features},
"postCreateCommand": "${post_create_command}",
"remoteEnv": {
"ANTHROPIC_API_KEY": "\${localEnv:ANTHROPIC_API_KEY}",
"GITHUB_TOKEN": "\${localEnv:GITHUB_TOKEN}",
"AWS_ACCESS_KEY_ID": "\${localEnv:AWS_ACCESS_KEY_ID}",
"AWS_SECRET_ACCESS_KEY": "\${localEnv:AWS_SECRET_ACCESS_KEY}",
"AWS_DEFAULT_REGION": "\${localEnv:AWS_DEFAULT_REGION}"
},
"runArgs": [
"--cap-drop=ALL",
"--security-opt=no-new-privileges"
],
"mounts": [],
"customizations": {
"vscode": {
"extensions": [
"anthropic.claude-code"
],
"settings": {
"terminal.integrated.defaultProfile.linux": "bash"
}
}
}
}
EOF
}
# Generate the firewall script portion of the Dockerfile
# iptables Firewall Strategy:
# - Uses a whitelist approach with rules processed in order (first match wins)
# - Prevents Claude from accessing arbitrary internet resources
# - Allows essential services (Anthropic API, GitHub, package registries)
generate_firewall_script() {
if [[ "$NETWORK_FIREWALL" != "true" ]]; then
echo "# Network firewall disabled - all outbound traffic allowed"
echo "# To enable, run setup-devcontainer.sh without --no-network-firewall"
return
fi
cat << 'FIREWALL_HEADER'
# Network firewall - only allow specific domains
RUN apt-get update && apt-get install -y iptables dnsutils && \
apt-get clean && rm -rf /var/lib/apt/lists/*
# Create firewall setup script (runs at container start)
# The script builds iptables rules in order - first match wins
RUN echo '#!/bin/bash' > /usr/local/bin/setup-firewall.sh && \
echo 'set -e' >> /usr/local/bin/setup-firewall.sh && \
echo '# Allow loopback - required for local IPC (localhost communication)' >> /usr/local/bin/setup-firewall.sh && \
echo 'iptables -A OUTPUT -o lo -j ACCEPT' >> /usr/local/bin/setup-firewall.sh && \
echo '# Allow established connections - permit responses to our outbound requests' >> /usr/local/bin/setup-firewall.sh && \
echo 'iptables -A OUTPUT -m state --state ESTABLISHED,RELATED -j ACCEPT' >> /usr/local/bin/setup-firewall.sh && \
echo '# Allow DNS - required for domain name resolution (both UDP and TCP)' >> /usr/local/bin/setup-firewall.sh && \
echo 'iptables -A OUTPUT -p udp --dport 53 -j ACCEPT' >> /usr/local/bin/setup-firewall.sh && \
echo 'iptables -A OUTPUT -p tcp --dport 53 -j ACCEPT' >> /usr/local/bin/setup-firewall.sh && \
echo '# Allow specific domains (HTTPS) - whitelist of permitted destinations' >> /usr/local/bin/setup-firewall.sh && \
FIREWALL_HEADER
# Add domain-specific rules
for domain in "${ALLOWED_DOMAINS[@]}"; do
echo " echo 'iptables -A OUTPUT -p tcp -d ${domain} --dport 443 -j ACCEPT' >> /usr/local/bin/setup-firewall.sh && \\"
done
cat << 'FIREWALL_FOOTER'
echo '# Block all other outbound web traffic - catch-all deny rules (must be last)' >> /usr/local/bin/setup-firewall.sh && \
echo 'iptables -A OUTPUT -p tcp --dport 443 -j DROP' >> /usr/local/bin/setup-firewall.sh && \
echo 'iptables -A OUTPUT -p tcp --dport 80 -j DROP' >> /usr/local/bin/setup-firewall.sh && \
chmod +x /usr/local/bin/setup-firewall.sh
# Note: Firewall requires NET_ADMIN capability or running as root
# For strict isolation, run: docker run --cap-add=NET_ADMIN ...
FIREWALL_FOOTER
}
# Generate the base Dockerfile content (header, labels, base packages)
generate_base_dockerfile() {
cat << 'EOF'
# Anthropic's recommended devcontainer for Claude Code
# Reference: https://docs.anthropic.com/en/docs/claude-code/devcontainer
FROM mcr.microsoft.com/devcontainers/base:ubuntu
# Security labels
LABEL org.opencontainers.image.title="Claude Code Sandbox"
LABEL org.opencontainers.image.description="Secure container for running Claude Code with --dangerously-skip-permissions"
LABEL org.opencontainers.image.vendor="Generated by setup-devcontainer.sh"
# Install essential security tools
RUN apt-get update && apt-get install -y \
curl \
ca-certificates \
gnupg \
lsb-release \
&& apt-get clean && rm -rf /var/lib/apt/lists/*
EOF
}
# Generate the Dockerfile footer (workspace, user, healthcheck)
generate_dockerfile_footer() {
cat << 'EOF'
# Create non-root user workspace
RUN mkdir -p /workspace && chown vscode:vscode /workspace
WORKDIR /workspace
# Default to non-root user
USER vscode
# Health check
HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \
CMD curl -sf https://api.anthropic.com/health || exit 1
EOF
}
# Orchestrate Dockerfile generation by combining all parts
generate_dockerfile() {
generate_base_dockerfile
generate_firewall_script
generate_dockerfile_footer
}
write_files() {
if [[ "$DRY_RUN" == "true" ]]; then
log_info "DRY RUN - Would create the following files:"
echo ""
echo "=== ${DEVCONTAINER_JSON} ==="
generate_devcontainer_json
echo ""
echo "=== ${DOCKERFILE} ==="
generate_dockerfile
echo ""
return
fi
# Create directory
mkdir -p "$DEVCONTAINER_DIR"
log_success "Created ${DEVCONTAINER_DIR}/"
# Write devcontainer.json
generate_devcontainer_json > "$DEVCONTAINER_JSON"
log_success "Created ${DEVCONTAINER_JSON}"
# Write Dockerfile
generate_dockerfile > "$DOCKERFILE"
log_success "Created ${DOCKERFILE}"
}
# Print a summary of the applied configuration
print_configuration_summary() {
echo ""
log_success "Devcontainer configuration created successfully!"
echo ""
echo "Configuration:"
echo " - Network firewall: $([ "$NETWORK_FIREWALL" == "true" ] && echo "ENABLED (allowlisted domains only)" || echo "DISABLED")"
echo " - Tooling: $([ "$FULL_TOOLING" == "true" ] && echo "Full (Node, Python, Git, GitHub CLI, AWS CLI, Docker)" || echo "Minimal (Node, Git, GitHub CLI)")"
echo " - Capabilities: Dropped (--cap-drop=ALL)"
echo " - Privilege escalation: Blocked (--security-opt=no-new-privileges)"
}
# Print instructions for using the devcontainer
print_usage_instructions() {
echo ""
echo "Next steps:"
echo ""
echo " 1. Set your API key (if not already set):"
echo " export ANTHROPIC_API_KEY=\"sk-ant-...\""
echo ""
echo " 2. Start the container:"
echo ""
echo " # Using VS Code:"
echo " Cmd/Ctrl+Shift+P → \"Dev Containers: Reopen in Container\""
echo ""
echo " # Using CLI:"
echo " devcontainer up --workspace-folder ."
echo " devcontainer exec --workspace-folder . bash"
echo ""
echo " 3. Run Claude with full autonomy (safe inside container):"
echo " claude --dangerously-skip-permissions"
}
# Print the list of allowed domains when firewall is enabled
print_allowed_domains() {
if [[ "$NETWORK_FIREWALL" == "true" ]]; then
echo ""
echo "Allowed outbound domains:"
for domain in "${ALLOWED_DOMAINS[@]}"; do
echo " - ${domain}"
done
fi
}
# Print security notes and documentation link
print_security_notes() {
echo ""
echo "Security notes:"
echo " - Safe for your own trusted projects"
echo " - Avoid using with untrusted code (prompt injection risk)"
echo " - Credentials inside container are accessible to Claude"
echo ""
echo "Documentation: https://docs.anthropic.com/en/docs/claude-code/devcontainer"
}
# Orchestrate printing all next steps information
print_next_steps() {
if [[ "$DRY_RUN" == "true" ]]; then
return
fi
print_configuration_summary
print_usage_instructions
print_allowed_domains
print_security_notes
}
main() {
echo "=============================================="
echo " Claude Code Devcontainer Setup"
echo "=============================================="
echo ""
parse_args "$@"
check_prerequisites
check_existing_config
write_files
print_next_steps
}
main "$@"